Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MdiChildContext.cs « System.Windows.Forms « Managed.Windows.Forms « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b9198b2c031309b85dc74a323774a0d5ab260965 (plain)
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
using System;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace System.Windows.Forms {

	internal class MdiChildContext {

		private static readonly int MdiBorderStyle = 0xFFFF;
		private static Color titlebar_color;

		
		private MainMenu merged_menu;
		private int BorderWidth = 3;
//		private int TitleBarHeight = 26;
//		private int ToolTitleBarHeight = 19;
		private Size MinTitleBarSize = new Size (115, 25);

		private Form form;
		private MdiClient mdi_container;

		private TitleButton close_button;
		private TitleButton maximize_button;
		private TitleButton minimize_button;

		private TitleButton [] title_buttons = new TitleButton [3];
		
		// moving windows
		private Point start;
		private State state;
		private FormPos sizing_edge;
		private Rectangle virtual_position;
		private Rectangle prev_virtual_position;
		private Rectangle prev_bounds;
		private bool maximized;

		private class TitleButton {
			public Rectangle Rectangle;
			public ButtonState State;
			public CaptionButton Caption;
			public EventHandler Clicked;
			
			public TitleButton (CaptionButton caption, EventHandler clicked)
			{
				Caption = caption;
				Clicked = clicked;
			}
		}

		private enum State {
			Idle,
			Moving,
			Sizing,
		}

		[Flags]
		private enum FormPos {
			None,

			TitleBar = 1,

			Top = 2,
			Left = 4,
			Right = 8,
			Bottom = 16,

			TopLeft = Top | Left,
			TopRight = Top | Right,

			BottomLeft = Bottom | Left,
			BottomRight = Bottom | Right,

			AnyEdge = Top | Left | Right | Bottom,
		}

		public MdiChildContext (Form form, MdiClient mdi_container)
		{
			titlebar_color = Color.FromArgb (255, 0, 0, 255);
			this.form = form;
			this.mdi_container = mdi_container;

			/*
			minimize_button = new Button ();
			minimize_button.Bounds = new Rectangle (form.Width - 62,
					-26, 18, 22);
			minimize_button.Anchor = AnchorStyles.Top | AnchorStyles.Right;
			minimize_button.Paint += new PaintEventHandler (PaintButtonHandler);
			minimize_button.Click += new EventHandler (OnMinimizeHandler);
			
			maximize_button = new Button ();
			maximize_button.Bounds = new Rectangle (form.Width - 44,
					BorderWidth + 2, 18, 22);
			maximize_button.Anchor = AnchorStyles.Top | AnchorStyles.Right;
			maximize_button.Paint += new PaintEventHandler (PaintButtonHandler);
			maximize_button.Click += new EventHandler (OnMaximizeHandler);
			
			close_button = new Button ();
			close_button.Bounds = new Rectangle (form.Width - 24,
					BorderWidth + 2, 18, 22);
			close_button.Anchor = AnchorStyles.Top | AnchorStyles.Right;
			close_button.Paint += new PaintEventHandler (PaintButtonHandler);
			close_button.Click += new EventHandler (CloseButtonClicked);


			form.Controls.AddImplicit (close_button);
			form.Controls.AddImplicit (maximize_button);
			form.Controls.AddImplicit (minimize_button);
			*/

			CreateButtons ();
		}

		public bool Maximized {
			get { return maximized; }
		}

		public MainMenu MergedMenu {
			get {
				if (merged_menu == null)
					merged_menu = CreateMergedMenu ();
				return merged_menu;
			}
		}

		private MainMenu CreateMergedMenu ()
		{
			Form parent = (Form) mdi_container.Parent;
			MainMenu clone = (MainMenu) parent.Menu.CloneMenu ();
			clone.MergeMenu (form.Menu);
			clone.MenuChanged += new EventHandler (MenuChangedHandler);
			clone.SetForm (parent);
			return clone;
		}

		private void MenuChangedHandler (object sender, EventArgs e)
		{
			CreateMergedMenu ();
		}

		public bool HandleMessage (ref Message m)
		{
			switch ((Msg)m.Msg) {


				// The mouse handling messages are actually
				// not WM_NC* messages except for the first button and NCMOVEs
				// down because we capture on the form

			case Msg.WM_MOUSEMOVE:
				return HandleMouseMove (form, ref m);

			case Msg.WM_LBUTTONUP:
				HandleLButtonUp (ref m);
				break;

			case Msg.WM_RBUTTONDOWN:
			case Msg.WM_LBUTTONDOWN:
				return HandleButtonDown (ref m);

			case Msg.WM_NCMOUSEMOVE:
				return HandleNCMouseMove (form, ref m);

			case Msg.WM_NCLBUTTONUP:
				return HandleNCLButtonUp (ref m);

			case Msg.WM_NCLBUTTONDOWN:
				return HandleNCLButtonDown (ref m);

			case Msg.WM_MOUSE_LEAVE:
				FormMouseLeave (ref m);
				break;

			case Msg.WM_NCPAINT: {
				PaintEventArgs	pe;

				pe = XplatUI.PaintEventStart(m.HWnd, false);
//				form.UpdateStyles ();
				PaintWindowDecorations (pe);
				// g.Clear (Color.Red);
				XplatUI.PaintEventEnd(m.HWnd, false);
				break;
			}
			}
			return false;
		}

		public void UpdateBorderStyle (FormBorderStyle border_style)
		{
			if (border_style != FormBorderStyle.None)
				XplatUI.SetBorderStyle (form.Handle, (FormBorderStyle) MdiBorderStyle);
			else
				XplatUI.SetBorderStyle (form.Handle, FormBorderStyle.None);

			CreateButtons ();
		}

		private void CreateButtons ()
		{
			switch (form.FormBorderStyle) {
			case FormBorderStyle.None:
				close_button = null;
				minimize_button = null;
				maximize_button = null;
				break;
			case FormBorderStyle.FixedSingle:
			case FormBorderStyle.Fixed3D:
			case FormBorderStyle.FixedDialog:
			case FormBorderStyle.Sizable:
				close_button = new TitleButton (CaptionButton.Close, new EventHandler (CloseClicked));
				minimize_button = new TitleButton (CaptionButton.Minimize, new EventHandler (MinimizeClicked));
				maximize_button = new TitleButton (CaptionButton.Maximize, new EventHandler (MaximizeClicked));
				break;
			case FormBorderStyle.FixedToolWindow:
			case FormBorderStyle.SizableToolWindow:
				close_button = new TitleButton (CaptionButton.Close, new EventHandler (CloseClicked));
				break;
			}

			title_buttons [0] = close_button;
			title_buttons [1] = minimize_button;
			title_buttons [2] = maximize_button;

		}

		private bool HandleButtonDown (ref Message m)
		{
			form.BringToFront ();
			mdi_container.ActiveMdiChild = form;
			return false;
		}

		private bool HandleNCLButtonDown (ref Message m)
		{
			form.BringToFront ();
			mdi_container.ActiveMdiChild = form;

			int x = Control.LowOrder ((int) m.LParam.ToInt32 ());
			int y = Control.HighOrder ((int) m.LParam.ToInt32 ());

			form.PointToClient (ref x, ref y);

			start = new Point (x, y);
			virtual_position = form.Bounds;

			// Need to adjust because we are in NC land
			y += TitleBarHeight;
			FormPos pos = FormPosForCoords (x, y);

			if (pos == FormPos.TitleBar) {
				HandleTitleBarDown (x, y);
				return true;
			}

			if (IsSizable) {
				SetCursorForPos (pos);
			
				if ((pos & FormPos.AnyEdge) == 0)
					return false;

				state = State.Sizing;
				sizing_edge = pos;
				form.Capture = true;
				return true;
			}

			return false;
		}

		private void HandleTitleBarDown (int x, int y)
		{
			foreach (TitleButton button in title_buttons) {
				if (button != null && button.Rectangle.Contains (x, y)) {
					button.State = ButtonState.Pushed;
					return;
				}
			}

			if (maximized)
				return;

			state = State.Moving;			     
			form.Capture = true;
		}

		private bool HandleMouseMove (Form form, ref Message m)
		{
			switch (state) {
			case State.Moving:
				HandleWindowMove (m);
				return true;
			case State.Sizing:
				HandleSizing (m);
				return true;
			}

			/*
			if (IsSizable) {
				int x = Control.LowOrder ((int) m.LParam.ToInt32 ());
				int y = Control.HighOrder ((int) m.LParam.ToInt32 ());
				FormPos pos = FormPosForCoords (x, y);
				Console.WriteLine ("position:   " + pos);
				SetCursorForPos (pos);

				ClearVirtualPosition ();
				state = State.Idle;
			}
			*/
			
			return false;
		}

		private bool HandleNCMouseMove (Form form, ref Message m)
		{
			if (IsSizable) {
				int x = Control.LowOrder ((int) m.LParam.ToInt32 ());
				int y = Control.HighOrder ((int) m.LParam.ToInt32 ());
				FormPos pos = FormPosForCoords (x, y);

				SetCursorForPos (pos);

				ClearVirtualPosition ();
				state = State.Idle;
			}

			return false;
		}

		private void FormMouseLeave (ref Message m)
		{
			form.ResetCursor ();
		}

		private void SetCursorForPos (FormPos pos)
		{
			switch (pos) {
			case FormPos.TopLeft:
			case FormPos.BottomRight:
				form.Cursor = Cursors.SizeNWSE;
				break;
			case FormPos.TopRight:
			case FormPos.BottomLeft:
				form.Cursor = Cursors.SizeNESW;
				break;
			case FormPos.Top:
			case FormPos.Bottom:
				form.Cursor = Cursors.SizeNS;
				break;
			case FormPos.Left:
			case FormPos.Right:
				form.Cursor = Cursors.SizeWE;
				break;
			default:
				form.ResetCursor ();
				break;
			}
		}
	
		private void HandleWindowMove (Message m)
		{
			Point move = MouseMove (m);

			virtual_position.X = form.Left + move.X;
			virtual_position.Y = form.Top + move.Y;
			virtual_position.Width = form.Width;
			virtual_position.Height = form.Height;

			mdi_container.EnsureScrollBars (virtual_position.Right, virtual_position.Bottom);

			DrawVirtualPosition ();
		}

		private void HandleSizing (Message m)
		{
			Point move = MouseMove (m);
			Rectangle pos = virtual_position;
			int mw = MinTitleBarSize.Width + (BorderWidth * 2);
			int mh = MinTitleBarSize.Height + (BorderWidth * 2);
			
			if ((sizing_edge & FormPos.Top) != 0) {
				int height = form.Height - move.Y;
				if (height <= mh) {
					move.Y += height - mh;
					height = mh;
				}
				pos.Y = form.Top + move.Y;
				pos.Height = height;
			} else if ((sizing_edge & FormPos.Bottom) != 0) {
				int height = form.Height + move.Y;
				if (height <= mh)
					move.Y -= height - mh;
				pos.Height = form.Height + move.Y;
			}

			if ((sizing_edge & FormPos.Left) != 0) {
				int width = form.Width - move.X;
				if (width <= mw) {
					move.X += width - mw;
					width = mw;
				}
				pos.X = form.Left + move.X;
				pos.Width = width;
			} else if ((sizing_edge & FormPos.Right) != 0) {
				int width = form.Width + move.X;
				if (width <= mw)
					move.X -= width - mw;
				pos.Width = form.Width + move.X;
			}

			UpdateVP (pos);
		}

		private bool IsSizable {
			get {
				switch (form.FormBorderStyle) {
				case FormBorderStyle.Sizable:
				case FormBorderStyle.SizableToolWindow:
					return true;
				default:
					return false;
				}
			}
		}

		private bool HasBorders {
			get {
				return form.FormBorderStyle != FormBorderStyle.None;
			}
		}

		private bool IsToolWindow {
			get {
				if (form.FormBorderStyle == FormBorderStyle.SizableToolWindow ||
						form.FormBorderStyle == FormBorderStyle.FixedToolWindow)
					return true;
				return false;
			}
		}

		private int TitleBarHeight {
			get {
				if (IsToolWindow)
					return 19;
				if (form.FormBorderStyle == FormBorderStyle.None)
					return 0;
				return 26;
			}
		}

		private void UpdateVP (Rectangle r)
		{
			UpdateVP (r.X, r.Y, r.Width, r.Height);
		}

		private void UpdateVP (Point loc, int w, int h)
		{
			UpdateVP (loc.X, loc.Y, w, h);
		}

		private void UpdateVP (int x, int y, int w, int h)
		{
			virtual_position.X = x;
			virtual_position.Y = y;
			virtual_position.Width = w;
			virtual_position.Height = h;

			DrawVirtualPosition ();
		}

		private void HandleLButtonUp (ref Message m)
		{
			if (state == State.Idle)
				return;

			ClearVirtualPosition ();

			form.Capture = false;
			form.Bounds = virtual_position;
			state = State.Idle;
		}

		private bool HandleNCLButtonUp (ref Message m)
		{
			int x = Control.LowOrder ((int) m.LParam.ToInt32 ());
			int y = Control.HighOrder ((int) m.LParam.ToInt32 ());

			form.PointToClient (ref x, ref y);

			// Need to adjust because we are in NC land
			y += TitleBarHeight;

			foreach (TitleButton button in title_buttons) {
				if (button != null && button.Rectangle.Contains (x, y)) {
					button.Clicked (this, EventArgs.Empty);
					return true;
				}
			}

			return true;
		}

		private void PaintWindowDecorations (PaintEventArgs pe)
		{
			Graphics dc = pe.Graphics;

			if (HasBorders) {
				Rectangle borders = new Rectangle (0, 0, form.Width, form.Height);
//			dc.FillRectangle (new SolidBrush (Color.Black), borders);
				
/*			
			dc.DrawRectangle (new Pen (SystemColors.ControlLight, 1), borders);
			borders.Inflate (-2, -2);
			dc.DrawRectangle (new Pen (SystemColors.ControlDark, 1), borders);
			borders.X++;
			borders.Width -= 2;
			dc.DrawRectangle (new Pen (SystemColors.ControlLight, 1), borders);
*/
			
				ControlPaint.DrawBorder3D (dc, borders,	Border3DStyle.Raised);

				if (IsSizable) {
					borders.Inflate (-1, -1);
					ControlPaint.DrawFocusRectangle (dc, borders);
				}
			}

			Color color = ThemeEngine.Current.ColorControlDark;
			if (form == mdi_container.ActiveMdiChild && !maximized)
				color = titlebar_color;

			Rectangle tb = new Rectangle (BorderWidth, BorderWidth,
					form.Width - (BorderWidth * 2), TitleBarHeight - 1);

			dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (color), tb);

			dc.DrawLine (new Pen (Color.White, 1), BorderWidth,
					TitleBarHeight + BorderWidth, form.Width - BorderWidth,
					TitleBarHeight + BorderWidth);

			if (!IsToolWindow) {
				tb.X += 18; // Room for the icon and the buttons
				tb.Width = (form.Width - 62) - tb.X;
			}

			if (form.Text != null) {
				StringFormat format = new StringFormat ();
				format.FormatFlags = StringFormatFlags.NoWrap;
				format.Trimming = StringTrimming.EllipsisCharacter;
				format.LineAlignment = StringAlignment.Center;
				dc.DrawString (form.Text, form.Font,
						ThemeEngine.Current.ResPool.GetSolidBrush (Color.White),
						tb, format);
			}

			if (!IsToolWindow && HasBorders) {
				if (form.Icon != null) {
					dc.DrawIcon (form.Icon, new Rectangle (BorderWidth + 3,
								     BorderWidth + 3, 16, 16));
				}
					
				minimize_button.Rectangle = new Rectangle (form.Width - 62,
						BorderWidth + 2, 18, 22);

				maximize_button.Rectangle = new Rectangle (form.Width - 44,
						BorderWidth + 2, 18, 22);
				
				close_button.Rectangle = new Rectangle (form.Width - 24,
						BorderWidth + 2, 18, 22);

				DrawTitleButton (dc, minimize_button);
				DrawTitleButton (dc, maximize_button);
				DrawTitleButton (dc, close_button);
			} else {
				close_button.Rectangle = new Rectangle (form.Width - BorderWidth - 2 - 13,
						BorderWidth + 2, 13, 13);
				DrawTitleButton (dc, close_button);
			}
		}

		private void DrawTitleButton (Graphics dc, TitleButton button)
		{
			dc.FillRectangle (SystemBrushes.Control, button.Rectangle);

			ControlPaint.DrawCaptionButton (dc, button.Rectangle,
					button.Caption, ButtonState.Normal);
		}

		private void CloseClicked (object sender, EventArgs e)
		{
			form.Close ();
			// form.Close should set visibility to false somewhere
			// in it's closing chain but currently does not.
			form.Visible = false;
		}

		private void MinimizeClicked (object sender, EventArgs e)
		{
			form.SuspendLayout ();
			form.Width = MinTitleBarSize.Width + (BorderWidth * 2);
			form.Height = MinTitleBarSize.Height + (BorderWidth * 2);
			form.ResumeLayout ();
		}

		private void MaximizeClicked (object sender, EventArgs e)
		{
			if (maximized) {
				form.Bounds = prev_bounds;
				maximized = false;
			} else {
				prev_bounds = form.Bounds;
				form.Bounds = form.Parent.Bounds;
				maximized = true;
			}
		}

		private Point NewLocation (Message m)
		{
			int x = Control.LowOrder ((int) m.LParam.ToInt32 ());
			int y = Control.HighOrder ((int) m.LParam.ToInt32 ());
			int x_move = x - start.X;
			int y_move = y - start.Y;

			return new Point (form.Left + x_move, form.Top + y_move);
		}

		private Point MouseMove (Message m)
		{
			int x = Control.LowOrder ((int) m.LParam.ToInt32 ());
			int y = Control.HighOrder ((int) m.LParam.ToInt32 ());
			int x_move = x - start.X;
			int y_move = y - start.Y;

			return new Point (x_move, y_move);
		}

		private void DrawVirtualPosition ()
		{
			ClearVirtualPosition ();

			XplatUI.DrawReversibleRectangle (mdi_container.Handle, virtual_position, 2);
			prev_virtual_position = virtual_position;
		}

		private void ClearVirtualPosition ()
		{
			if (prev_virtual_position != Rectangle.Empty)
				XplatUI.DrawReversibleRectangle (mdi_container.Handle,
						prev_virtual_position, 2);
			prev_virtual_position = Rectangle.Empty;
		}

		private FormPos FormPosForCoords (int x, int y)
		{
			if (y < TitleBarHeight + BorderWidth) {

				if (y > BorderWidth && x > BorderWidth &&
						x < form.Width - BorderWidth)
					return FormPos.TitleBar;

				if (x < BorderWidth || (x < 20 && y < BorderWidth))
					return FormPos.TopLeft;

				if (x > form.Width - BorderWidth ||
					(x > form.Width - 20 && y < BorderWidth))
					return FormPos.TopRight;

				if (y < BorderWidth)
					return FormPos.Top;

			} else if (y > form.Height - 20) {

				if (x < BorderWidth ||
						(x < 20 && y > form.Height - BorderWidth))
					return FormPos.BottomLeft;

				if (x > form.Width - BorderWidth ||
						(x > form.Width - 20 &&
						 y > form.Height - BorderWidth))
					return FormPos.BottomRight;

				if (y > form.Height - BorderWidth)
					return FormPos.Bottom;


			} else if (x < BorderWidth) {
				return FormPos.Left;
			} else if (x > form.Width - BorderWidth) {
				return FormPos.Right;
			}
			
			return FormPos.None;
		}
	}

}