1 //
2 // MdiControlStrip.cs
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 //
23 // Copyright (c) 2006 Novell, Inc.
24 //
25 // Authors:
26 //	Jonathan Pobst (monkey@jpobst.com)
27 //
28 
29 using System;
30 using System.Runtime.InteropServices;
31 using System.ComponentModel;
32 using System.Drawing;
33 using System.Windows.Forms.Layout;
34 using System.Collections.Generic;
35 using System.ComponentModel.Design.Serialization;
36 
37 namespace System.Windows.Forms
38 {
39 	internal class MdiControlStrip
40 	{
41 		public class SystemMenuItem : ToolStripMenuItem
42 		{
43 			private Form form;
44 
SystemMenuItem(Form ownerForm)45 			public SystemMenuItem (Form ownerForm)
46 			{
47 				form = ownerForm;
48 
49 				base.AutoSize = false;
50 				base.Size = new Size (20, 20);
51 				base.Image = ownerForm.Icon.ToBitmap ();
52 				base.MergeIndex = int.MinValue;
53 				base.DisplayStyle = ToolStripItemDisplayStyle.Image;
54 
55 				DropDownItems.Add ("&Restore", null, RestoreItemHandler);
56 				ToolStripMenuItem tsiMove = (ToolStripMenuItem)DropDownItems.Add ("&Move");
57 				tsiMove.Enabled = false;
58 				ToolStripMenuItem tsiSize = (ToolStripMenuItem)DropDownItems.Add ("&Size");
59 				tsiSize.Enabled = false;
60 				DropDownItems.Add ("Mi&nimize", null, MinimizeItemHandler);
61 				ToolStripMenuItem tsiMaximize = (ToolStripMenuItem)DropDownItems.Add ("Ma&ximize");
62 				tsiMaximize.Enabled = false;
63 				DropDownItems.Add ("-");
64 				ToolStripMenuItem tsiClose = (ToolStripMenuItem)DropDownItems.Add ("&Close", null, CloseItemHandler);
65 				tsiClose.ShortcutKeys = Keys.Control | Keys.F4;
66 				DropDownItems.Add ("-");
67 				ToolStripMenuItem tsiNext = (ToolStripMenuItem)DropDownItems.Add ("Nex&t", null, NextItemHandler);
68 				tsiNext.ShortcutKeys = Keys.Control | Keys.F6;
69 			}
70 
OnPaint(PaintEventArgs e)71 			protected override void OnPaint (PaintEventArgs e)
72 			{
73 				// Can't render without an owner
74 				if (this.Owner == null)
75 					return;
76 
77 				// If DropDown.ShowImageMargin is false, we don't display the image
78 				Image draw_image = this.Image;
79 
80 				// Figure out where our text and image go
81 				Rectangle text_layout_rect;
82 				Rectangle image_layout_rect;
83 
84 				this.CalculateTextAndImageRectangles (out text_layout_rect, out image_layout_rect);
85 
86 				if (image_layout_rect != Rectangle.Empty)
87 					this.Owner.Renderer.DrawItemImage (new ToolStripItemImageRenderEventArgs (e.Graphics, this, draw_image, image_layout_rect));
88 
89 				return;
90 			}
91 
92 			public Form MdiForm {
93 				get { return form; }
94 				set { form = value; }
95 			}
96 
RestoreItemHandler(object sender, EventArgs e)97 			private void RestoreItemHandler (object sender, EventArgs e)
98 			{
99 				form.WindowState = FormWindowState.Normal;
100 			}
101 
MinimizeItemHandler(object sender, EventArgs e)102 			private void MinimizeItemHandler (object sender, EventArgs e)
103 			{
104 				form.WindowState = FormWindowState.Minimized;
105 			}
106 
CloseItemHandler(object sender, EventArgs e)107 			private void CloseItemHandler (object sender, EventArgs e)
108 			{
109 				form.Close ();
110 			}
111 
NextItemHandler(object sender, EventArgs e)112 			private void NextItemHandler (object sender, EventArgs e)
113 			{
114 				form.MdiParent.MdiContainer.ActivateNextChild ();
115 			}
116 		}
117 
118 		public class ControlBoxMenuItem : ToolStripMenuItem
119 		{
120 			private Form form;
121 			private ControlBoxType type;
122 
ControlBoxMenuItem(Form ownerForm, ControlBoxType type)123 			public ControlBoxMenuItem (Form ownerForm, ControlBoxType type)
124 			{
125 				form = ownerForm;
126 				this.type = type;
127 
128 				base.AutoSize = false;
129 				base.Alignment = ToolStripItemAlignment.Right;
130 				base.Size = new Size (20, 20);
131 				base.MergeIndex = int.MaxValue;
132 				base.DisplayStyle = ToolStripItemDisplayStyle.None;
133 
134 				switch (type) {
135 					case ControlBoxType.Close:
136 						this.Click += new EventHandler(CloseItemHandler);
137 						break;
138 					case ControlBoxType.Min:
139 						this.Click += new EventHandler (MinimizeItemHandler);
140 						break;
141 					case ControlBoxType.Max:
142 						this.Click += new EventHandler (RestoreItemHandler);
143 						break;
144 				}
145 			}
146 
OnPaint(PaintEventArgs e)147 			protected override void OnPaint (PaintEventArgs e)
148 			{
149 				base.OnPaint (e);
150 				Graphics g = e.Graphics;
151 
152 				switch (type) {
153 					case ControlBoxType.Close:
154 						g.FillRectangle (Brushes.Black, 8, 8, 4, 4);
155 						g.FillRectangle (Brushes.Black, 6, 6, 2, 2);
156 						g.FillRectangle (Brushes.Black, 6, 12, 2, 2);
157 						g.FillRectangle (Brushes.Black, 12, 6, 2, 2);
158 						g.FillRectangle (Brushes.Black, 12, 12, 2, 2);
159 						g.DrawLine (Pens.Black, 8, 7, 8, 12);
160 						g.DrawLine (Pens.Black, 7, 8, 12, 8);
161 						g.DrawLine (Pens.Black, 11, 7, 11, 12);
162 						g.DrawLine (Pens.Black, 7, 11, 12, 11);
163 						break;
164 					case ControlBoxType.Min:
165 						g.DrawLine (Pens.Black, 6, 12, 11, 12);
166 						g.DrawLine (Pens.Black, 6, 13, 11, 13);
167 						break;
168 					case ControlBoxType.Max:
169 						g.DrawLines (Pens.Black, new Point[] {new Point (7, 8), new Point (7, 5), new Point (13, 5), new Point (13, 10), new Point (11, 10)});
170 						g.DrawLine (Pens.Black, 7, 6, 12, 6);
171 
172 						g.DrawRectangle (Pens.Black, new Rectangle (5, 8, 6, 5));
173 						g.DrawLine (Pens.Black, 5, 9, 11, 9);
174 
175 						break;
176 				}
177 			}
178 
179 			public Form MdiForm {
180 				get { return form; }
181 				set { form = value; }
182 			}
183 
RestoreItemHandler(object sender, EventArgs e)184 			private void RestoreItemHandler (object sender, EventArgs e)
185 			{
186 				form.WindowState = FormWindowState.Normal;
187 			}
188 
MinimizeItemHandler(object sender, EventArgs e)189 			private void MinimizeItemHandler (object sender, EventArgs e)
190 			{
191 				form.WindowState = FormWindowState.Minimized;
192 			}
193 
CloseItemHandler(object sender, EventArgs e)194 			private void CloseItemHandler (object sender, EventArgs e)
195 			{
196 				form.Close ();
197 			}
198 		}
199 
200 		public enum ControlBoxType
201 		{
202 			Close,
203 			Min,
204 			Max
205 		}
206 	}
207 }
208