File: MdiControlStrip.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (207 lines) | stat: -rw-r--r-- 6,278 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//
// MdiControlStrip.cs
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Copyright (c) 2006 Novell, Inc.
//
// Authors:
//	Jonathan Pobst (monkey@jpobst.com)
//

using System;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms.Layout;
using System.Collections.Generic;
using System.ComponentModel.Design.Serialization;

namespace System.Windows.Forms
{
	internal class MdiControlStrip
	{
		public class SystemMenuItem : ToolStripMenuItem
		{
			private Form form;
			
			public SystemMenuItem (Form ownerForm)
			{
				form = ownerForm;
				
				base.AutoSize = false;
				base.Size = new Size (20, 20);
				base.Image = ownerForm.Icon.ToBitmap ();
				base.MergeIndex = int.MinValue;
				base.DisplayStyle = ToolStripItemDisplayStyle.Image;

				DropDownItems.Add ("&Restore", null, RestoreItemHandler);
				ToolStripMenuItem tsiMove = (ToolStripMenuItem)DropDownItems.Add ("&Move");
				tsiMove.Enabled = false;
				ToolStripMenuItem tsiSize = (ToolStripMenuItem)DropDownItems.Add ("&Size");
				tsiSize.Enabled = false;
				DropDownItems.Add ("Mi&nimize", null, MinimizeItemHandler);
				ToolStripMenuItem tsiMaximize = (ToolStripMenuItem)DropDownItems.Add ("Ma&ximize");
				tsiMaximize.Enabled = false;
				DropDownItems.Add ("-");
				ToolStripMenuItem tsiClose = (ToolStripMenuItem)DropDownItems.Add ("&Close", null, CloseItemHandler);
				tsiClose.ShortcutKeys = Keys.Control | Keys.F4;
				DropDownItems.Add ("-");
				ToolStripMenuItem tsiNext = (ToolStripMenuItem)DropDownItems.Add ("Nex&t", null, NextItemHandler);
				tsiNext.ShortcutKeys = Keys.Control | Keys.F6;
			}

			protected override void OnPaint (PaintEventArgs e)
			{
				// Can't render without an owner
				if (this.Owner == null)
					return;

				// If DropDown.ShowImageMargin is false, we don't display the image
				Image draw_image = this.Image;

				// Figure out where our text and image go
				Rectangle text_layout_rect;
				Rectangle image_layout_rect;

				this.CalculateTextAndImageRectangles (out text_layout_rect, out image_layout_rect);

				if (image_layout_rect != Rectangle.Empty)
					this.Owner.Renderer.DrawItemImage (new ToolStripItemImageRenderEventArgs (e.Graphics, this, draw_image, image_layout_rect));

				return;
			}

			public Form MdiForm {
				get { return form; }
				set { form = value; }
			}

			private void RestoreItemHandler (object sender, EventArgs e)
			{
				form.WindowState = FormWindowState.Normal;
			}

			private void MinimizeItemHandler (object sender, EventArgs e)
			{
				form.WindowState = FormWindowState.Minimized;
			}

			private void CloseItemHandler (object sender, EventArgs e)
			{
				form.Close ();
			}

			private void NextItemHandler (object sender, EventArgs e)
			{
				form.MdiParent.MdiContainer.ActivateNextChild ();
			}
		}
		
		public class ControlBoxMenuItem : ToolStripMenuItem
		{
			private Form form;
			private ControlBoxType type;
			
			public ControlBoxMenuItem (Form ownerForm, ControlBoxType type)
			{
				form = ownerForm;
				this.type = type;
				
				base.AutoSize = false;
				base.Alignment = ToolStripItemAlignment.Right;
				base.Size = new Size (20, 20);
				base.MergeIndex = int.MaxValue;
				base.DisplayStyle = ToolStripItemDisplayStyle.None;

				switch (type) {
					case ControlBoxType.Close:
						this.Click += new EventHandler(CloseItemHandler);
						break;
					case ControlBoxType.Min:
						this.Click += new EventHandler (MinimizeItemHandler);
						break;
					case ControlBoxType.Max:
						this.Click += new EventHandler (RestoreItemHandler);
						break;
				}
			}

			protected override void OnPaint (PaintEventArgs e)
			{
				base.OnPaint (e);
				Graphics g = e.Graphics;
				
				switch (type) {
					case ControlBoxType.Close:
						g.FillRectangle (Brushes.Black, 8, 8, 4, 4);
						g.FillRectangle (Brushes.Black, 6, 6, 2, 2);
						g.FillRectangle (Brushes.Black, 6, 12, 2, 2);
						g.FillRectangle (Brushes.Black, 12, 6, 2, 2);
						g.FillRectangle (Brushes.Black, 12, 12, 2, 2);
						g.DrawLine (Pens.Black, 8, 7, 8, 12);
						g.DrawLine (Pens.Black, 7, 8, 12, 8);
						g.DrawLine (Pens.Black, 11, 7, 11, 12);
						g.DrawLine (Pens.Black, 7, 11, 12, 11);
						break;
					case ControlBoxType.Min:
						g.DrawLine (Pens.Black, 6, 12, 11, 12);
						g.DrawLine (Pens.Black, 6, 13, 11, 13);
						break;
					case ControlBoxType.Max:
						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)});
						g.DrawLine (Pens.Black, 7, 6, 12, 6);
						
						g.DrawRectangle (Pens.Black, new Rectangle (5, 8, 6, 5));
						g.DrawLine (Pens.Black, 5, 9, 11, 9);
						
						break;
				}
			}

			public Form MdiForm {
				get { return form; }
				set { form = value; }
			}
			
			private void RestoreItemHandler (object sender, EventArgs e)
			{
				form.WindowState = FormWindowState.Normal;
			}

			private void MinimizeItemHandler (object sender, EventArgs e)
			{
				form.WindowState = FormWindowState.Minimized;
			}

			private void CloseItemHandler (object sender, EventArgs e)
			{
				form.Close ();
			}
		}
		
		public enum ControlBoxType
		{
			Close,
			Min,
			Max
		}
	}
}