A
download ContainerControl.cs
Language: C#
License: GPL
Copyright: (C) 2003 Southern Storm Software, Pty Ltd. (C) 2003 Neil Cawse.
LOC: 663
Project Info
DotGNU Portable.NET(dotgnu-pnet)
Server: Savannah GNU
Type: cvs
...etlib\System.Windows.Forms\
   .cvsignore
   AccessibleEvents.cs
   AccessibleNavigation.cs
   AccessibleObject.cs
   AccessibleRole.cs
   AccessibleSelection.cs
   AccessibleStates.cs
   ActivationGesture.cs
   ActivationState.cs
   AmbientProperties.cs
   AnchorStyles.cs
   Appearance.cs
   Application.cs
   ApplicationContext.cs
   ArrangeDirection.cs
   ArrangeStartingPosition.cs
   BaseCollection.cs
   Binding.cs
   BindingContext.cs
   BindingManagerBase.cs
   BindingMemberInfo.cs
   BindingsCollection.cs
   BootMode.cs
   Border3DSide.cs
   Border3DStyle.cs
   BorderStyle.cs
   BoundsSpecified.cs
   Button.cs
   ButtonBase.cs
   ButtonBorderStyle.cs
   ButtonBoxLayout.cs
   ButtonState.cs
   CancelEventArgs.cs
   CancelEventHandler.cs
   CaptionButton.cs
   CharacterCasing.cs
   CheckBox.cs
   CheckListBox.cs
   CheckState.cs
   Clipboard.cs
   ColorDepth.cs
   ColorDialog.cs
   ColumnClickEventArgs.cs
   ColumnClickEventHandler.cs
   ColumnHeader.cs
   ColumnHeaderStyle.cs
   ComboBox.cs
   ComboBoxStyle.cs
   CommonDialog.cs
   ContainerControl.cs
   ...entsResizedEventArgs.cs
   ...sResizedEventHandler.cs
   ContextMenu.cs
   Control.cs
   ...olBindingsCollection.cs
   ControlEventArgs.cs
   ControlEventHandler.cs
   ControlPaint.cs
   ControlStyles.cs
   ControlToolkitMgr.cs
   ConvertEventArgs.cs
   ConvertEventHandler.cs
   CreateParams.cs
   CurrencyManager.cs
   Cursor.cs
   CursorConverter.cs
   Cursors.cs
   DataFormats.cs
   DataGrid.cs
   DataGridBoolColumn.cs
   DataGridCell.cs
   DataGridColumnStyle.cs
   DataGridLineStyle.cs
   ...ParentRowsLabelStyle.cs
   ...mnWidthTypeConverter.cs
   DataGridTableStyle.cs
   DataGridTextBox.cs
   DataGridTextBoxColumn.cs
   DataObject.cs
   DateBoldEventArgs.cs
   DateBoldEventHandler.cs
   DateRangeEventArgs.cs
   DateRangeEventHandler.cs
   DateTimePickerFormat.cs
   Day.cs
   DialogResult.cs
   DockStyle.cs
   DomainUpDown.cs
   DragAction.cs
   DragDropEffects.cs
   DragEventArgs.cs
   DragEventHandler.cs
   DrawItemEventArgs.cs
   DrawItemEventHandler.cs
   DrawItemState.cs
   DrawMode.cs
   ErrorBlinkStyle.cs
   ErrorIconAlignment.cs
   ErrorProvider.cs
   EventId.cs
   FeatureSupport.cs
   FileDialog.cs
   FlatStyle.cs
   FolderBrowserDialog.cs
   FontDialog.cs
   Form.cs
   FormBorderStyle.cs
   Forms.build
   FormStartPosition.cs
   FormWindowState.cs
   FrameStyle.cs
   GiveFeedbackEventArgs.cs
   ...FeedbackEventHandler.cs
   ...lumnStylesCollection.cs
   GridItem.cs
   GridItemCollection.cs
   GridItemType.cs
   GridLayout.cs
   ...ableStylesCollection.cs
   GroupBox.cs
   HBoxLayout.cs
   Help.cs
   HelpEventArgs.cs
   HelpEventHandler.cs
   HelpNavigator.cs
   HelpProvider.cs
   HorizontalAlignment.cs
   HScrollBar.cs
   IBindableComponent.cs
   IButtonControl.cs
   ICommandExecutor.cs
   ...ponentEditorPageSite.cs
   IContainerControl.cs
   ...gNotificationService.cs
   IDataGridEditingService.cs
   IDataObject.cs
   IDropTarget.cs
   IFeatureSupport.cs
   IFileReaderService.cs
   ImageIndexConverter.cs
   ImageList.cs
   ImageListStreamer.cs
   ImeMode.cs
   IMessageFilter.cs
   InputLanguage.cs
   ...uageChangedEventArgs.cs
   ...eChangedEventHandler.cs
   ...ageChangingEventArgs.cs
   ...ChangingEventHandler.cs
   InvalidateEventArgs.cs
   InvalidateEventHandler.cs
   IRecommendedSize.cs
   ItemActivation.cs
   ItemBoundsPortion.cs
   ItemChangedEventArgs.cs
   ItemChangedEventHandler.cs
   ItemCheckEventArgs.cs
   ItemCheckEventHandler.cs
   ItemDragEventArgs.cs
   ItemDragEventHandler.cs
   IWin32Window.cs
   IWindowTarget.cs
   KeyEventArgs.cs
   KeyEventHandler.cs
   KeyPressEventArgs.cs
   KeyPressEventHandler.cs
   Keys.cs
   Label.cs
   LabelEditEventArgs.cs
   LabelEditEventHandler.cs
   LayoutEventArgs.cs
   LayoutEventHandler.cs
   LeftRightAlignment.cs
   LinkArea.cs
   LinkBehavior.cs
   LinkClickedEventArgs.cs
   LinkClickedEventHandler.cs
   LinkLabel.cs
   ...LinkClickedEventArgs.cs
   ...kClickedEventHandler.cs
   LinkState.cs
   ListBindingConverter.cs
   ListBox.cs
   ListControl.cs
   ListView.cs
   ListViewAlignment.cs
   ListViewItem.cs
   ListViewItemConverter.cs
   ...ViewSubItemConverter.cs
   MainMenu.cs
   Makefile.am
   MdiClient.cs
   MdiLayout.cs
   MeasureItemEventArgs.cs
   MeasureItemEventHandler.cs
   Menu.cs
   MenuGlyph.cs
   MenuItem.cs
   MenuMerge.cs
   Message.cs
   MessageBox.cs
   MessageBoxButtons.cs
   MessageBoxDefaultButton.cs
   MessageBoxIcon.cs
   MessageBoxOptions.cs
   MethodInvoker.cs
   MonthCalendar.cs
   MouseButtons.cs
   MouseEventArgs.cs
   MouseEventHandler.cs
   NativeWindow.cs
   NavigateEventArgs.cs
   NavigateEventHandler.cs
   NodeLabelEditEventArgs.cs
   ...abelEditEventHandler.cs
   NotifyIcon.cs
   NumericUpDown.cs
   OpenFileDialog.cs
   Orientation.cs
   OSFeature.cs
   OwnerDrawPropertyBag.cs
   PaintEventArgs.cs
   PaintEventHandler.cs
   Panel.cs
   PictureBox.cs
   PictureBoxSizeMode.cs
   PopupControl.cs
   PrintDialog.cs
   ProgressBar.cs
   PropertySort.cs
   ...yTabChangedEventArgs.cs
   ...bChangedEventHandler.cs
   ...alueChangedEventArgs.cs
   ...eChangedEventHandler.cs
   ...ccessibilityHelpArgs.cs
   ...lityHelpEventHandler.cs
   ...ontinueDragEventArgs.cs
   ...inueDragEventHandler.cs
   RadioButton.cs
   RichTextBox.cs
   RichTextBoxFinds.cs
   RichTextBoxScrollBars.cs
   ...oxSelectionAttribute.cs
   ...extBoxSelectionTypes.cs
   RichTextBoxStreamType.cs
   ...tBoxWordPunctuations.cs
   RightToLeft.cs
   S.cs
   SaveFileDialog.cs
   Screen.cs
   ScrollableControl.cs
   ScrollBar.cs
   ScrollBars.cs
   ScrollButton.cs
   ScrollEventArgs.cs
   ScrollEventHandler.cs
   ScrollEventType.cs
   SecurityIDType.cs
   ...ItemChangedEventArgs.cs
   ...mChangedEventHandler.cs
   SelectionMode.cs
   Shortcut.cs
   SizeGripStyle.cs
   SortOrder.cs
   Splitter.cs
   SplitterEventArgs.cs
   SplitterEventHandler.cs
   StatusBar.cs
   ...BarDrawItemEventArgs.cs
   ...DrawItemEventHandler.cs
   StatusBarPanel.cs
   StatusBarPanelAutoSize.cs
   ...sBarPanelBorderStyle.cs
   ...rPanelClickEventArgs.cs
   ...nelClickEventHandler.cs
   StatusBarPanelStyle.cs
   StructFormat.cs
   SystemInformation.cs
   TabAlignment.cs
   TabAppearance.cs
   TabControl.cs
   TabDrawMode.cs
   TabPage.cs
   TabSizeMode.cs
   TextBox.cs
   TextBoxBase.cs
   TickStyle.cs
   Timer.cs
   TODOAttribute.cs
   ToolBar.cs
   ToolBarAppearance.cs
   ToolBarButton.cs
   ...ButtonClickEventArgs.cs
   ...tonClickEventHandler.cs
   ToolBarButtonStyle.cs
   ToolBarTextAlign.cs
   ToolTip.cs
   TrackBar.cs
   TreeNode.cs
   TreeNodeCollection.cs
   TreeView.cs
   TreeViewAction.cs
   TreeViewCancelEventArgs.cs
   ...ewCancelEventHandler.cs
   TreeViewEventArgs.cs
   TreeViewEventHandler.cs
   TriangleDirection.cs
   UICues.cs
   UICuesEventArgs.cs
   UICuesEventHandler.cs
   UpDownBase.cs
   UpDownEventArgs.cs
   UpDownEventHandler.cs
   UserControl.cs
   VBoxLayout.cs
   View.cs
   VScrollBar.cs
   Win32Constants.cs

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
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
/*
 * ContainerControl.cs - Implementation of the
 *			"System.Windows.Forms.ContainerControl" class.
 *
 * Copyright (C) 2003  Southern Storm Software, Pty Ltd.
 * Copyright (C) 2003  Neil Cawse.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/* Information on Keyboard handling
 * Key event processing always starts at the control that has the input focus (e.g. a text control). From there various methods are called upwards in the window hierarchy (from child to parent). This gives parent controls such as forms and panels an opportunity to handle and consume key events before they can reach the target control. There is only one method (ProcessMnemonic) that is called in the opposite direction (from parent to child). Each of the 8 methods returns a boolean value. If a method returns true the key event is considered handled (consumed) and processing of that particular key event will stop immediatly (no further processing methods will be called).

Key event processing actually happens in two phases: preprocessing and processing. The preprocessing phase starts with a call to PreProcessMessage, the processing phase with a call to ProcessKeyMessage. While the processing phase is very similar for all three types of key events (WM_KEYDOWN, WM_KEYUP and WM_CHAR) preprocessing differs completely for WM_KEYDOWN and WM_CHAR and there is no preprocessing phase for WM_KEYUP.

Here is what happens in detail:

Preprocessing Phase (WM_KEYDOWN):
  1. PreProcessMessage is called on the focused control.
     2. The focused control calls its own ProcessCmdKey method.
        3. ProcessCmdKey by default just returns the result of calling
           ProcessCmdKey on its parent. So effectivly this bubbles the event
           up the windows hierarchy until the top-most control is reached
           which - by default - simply returns false.
     4. The focused control calls IsInputKey on itself. If the result is true
        the PreProcessMessage terminates at this point with a return value of false.
     6. The focused control calls its own ProcessDialogKey method.
        7. Again, ProcessDialogKey is called from child to parent along the parent
           chaim until the top-most control is reached which usually returns false.
  7. PreProcessMessage returns.           
           
Preprocessing Phase (WM_CHAR):
  1. PreProcessMessage is called on the focused control.
     2. The focused control calls IsInputChar on itself. If the result is true
        the PreProcessMessage terminates at this point with a return value of false.
     3. The focused control calls its own ProcessDialogChar method.
        4. ProcessDialogChar is called from child to parent along the parent
           chain until the top-most control is reached which usually returns false.
           In addition, for each ContainerControl the following step is
           performed before the parent's ProcessDialogChar method is invoked:
           5. The container control calls its own ProcessMnemonic method.
              6. ProcessMnemonic is called for all child controls of the
                 container (which in turn might call ProcessMnemonic on their
                 children).
  7. PreProcessMessage returns.
   
Processing Phase (WM_KEYDOWN, WM_KEYUP and WM_CHAR):
  1. ProcessKeyMessage is called on the focused control.
     2. The focused control calls ProcessKeyPreview on the parent control.
        3. ProcessKeyPreview is called from child to parent along the parent
           chain until the top-most control is reached which usually returns false.
     4. The focused control calls ProcessKeyEventArgs on itself.
        5. ProcessKeyEventArgs calls one of the methods OnKeyDown, OnKeyUp
           or OnKeyPress depending on the type of key event.
           6. The On* method invokes all event handlers that have been registerd
              for the corresponding event.
   7. ProcessKeyMessage returns.
           
Note again that as soon as one method returns true processing will stop and the calling method will immediately return true as well. Also, if the preprocessing phase returns true the processing phase will be skipped.

You may have noticed that in preprocessing of WM_CHAR when there are several container controls in the parent chain the ProcessMnemonic method of child controls will be called unnecessarily often.
*/

namespace System.Windows.Forms
{

using System.Drawing;
using System.ComponentModel;

public class ContainerControl : ScrollableControl, IContainerControl
{
	// Internal state.
	private Control activeControl;
	private Control focusedControl;
	private Control unvalidatedControl;
	private bool validating = false;

	// Constructor.
	public ContainerControl()
	{
		SetStyle(ControlStyles.AllPaintingInWmPaint, false);
		SetStyle(ControlStyles.ContainerControl, true);
	}

#if CONFIG_COMPONENT_MODEL || CONFIG_EXTENDED_DIAGNOSTICS
	[Browsable(false)]
#endif // CONFIG_COMPONENT_MODEL || CONFIG_EXTENDED_DIAGNOSTICS
	public override BindingContext BindingContext
	{
		get
		{
			// Containers must always have a binding context.
			BindingContext context = base.BindingContext;
			if(context == null)
			{
				context = new BindingContext();
				base.BindingContext = context;
			}
			return context;
		}
		set
		{
			base.BindingContext = value;
		}
	}

	protected override CreateParams CreateParams
	{
		get
		{
			return base.CreateParams;
		}
	}

#if CONFIG_COMPONENT_MODEL || CONFIG_EXTENDED_DIAGNOSTICS
	[Browsable(false)]
	[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
#endif // CONFIG_COMPONENT_MODEL || CONFIG_EXTENDED_DIAGNOSTICS
	public virtual Control ActiveControl
	{
		get
		{
			return activeControl;
		}

		set
		{
			// Already set?
			if (activeControl == value && (value == null || value.Focused))
			{
				return;
			}

			// Can't set the active control to a control thats not a child of the container.
			if (value != null && !Contains(value))
			{
				throw new ArgumentException(S._("SWF_CouldNotActivate"));
			}
		
			bool activated;

			// Find outer container.
			ContainerControl parentContainer = this;
			if (value != null && value.Parent != null)
			{
				parentContainer = value.Parent.GetContainerControl() as ContainerControl;
			}

			if (parentContainer != null)
			{
				activated = parentContainer.ActivateControlInternal(value);
			}
			else
			{
				activated = SetActiveControl(value);
			}

			if (parentContainer != null && activated)
			{
				// Only focus if the control that currently has the focus falls in the same hierarchy as the control we are setting the focus to.
				ContainerControl outerContainer = this;

				while (outerContainer.Parent != null && outerContainer.Parent.GetContainerControl() is ContainerControl)
				{
					outerContainer = outerContainer.Parent.GetContainerControl() as ContainerControl;
				}
				if (outerContainer.ContainsFocus)
				{
					parentContainer.SetFocus(activeControl);
				}
			}
		}
	}

#if CONFIG_COMPONENT_MODEL || CONFIG_EXTENDED_DIAGNOSTICS
	[Browsable(false)]
	[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
#endif // CONFIG_COMPONENT_MODEL || CONFIG_EXTENDED_DIAGNOSTICS
	public Form ParentForm
	{
		get
		{
			Control parent = Parent;
			if (parent != null)
			{
				return parent.FindForm();
			}
			if (this is Form)
			{
				return null;
			}
			else
			{
				return FindForm();
			}
		}
	}

	// Activate a specific control.
	public bool ActivateControl(Control active)
	{
		bool activated = ActivateControlInternal(active);
		//TODO: Scroll active into view if its not visible.
		return activated;
	}

	private bool ActivateControlInternal(Control active)
	{

		bool activated = true;
		bool activate = false;
		ContainerControl container;
		Control parent = base.Parent;
		if (parent != null)
		{
			container = parent.GetContainerControl() as ContainerControl;
			if (container != null && container.ActiveControl != this)
			{
				activate = true;
				if (!container.ActivateControlInternal(this))
				{
					return false; 
				}
			}
		}
		
		if (active != activeControl || activate)
		{
			if (active == this)
			{
				activated = SetActiveControl(null);
			}
			else
			{
				activated = SetActiveControl(active);
			}
		}
		return activated;
	}

#if CONFIG_COMPONENT_MODEL
	[EditorBrowsable(EditorBrowsableState.Advanced)]
#endif // CONFIG_COMPONENT_MODEL
	protected override void AdjustFormScrollbars(bool displayScrollbars)
	{
		AdjustFormScrollbars(displayScrollbars);
		ScrollControlIntoView(activeControl);
	}

	protected virtual void UpdateDefaultButton()
	{  
	} 

	private bool SetActiveControl(Control value)
	{
		if (activeControl == value)
		{
			return true;
		}
		activeControl = value;

		if (!validating && unvalidatedControl == null)
		{
			// Set the unvalidatedControl to outermost active control.
			// Start with the focused control.
			unvalidatedControl = focusedControl;
			while (true)
			{
				ContainerControl c = unvalidatedControl as ContainerControl;
				if (c == null || c.activeControl == null)
				{
					break;
				}
				unvalidatedControl = c.activeControl;
			}
		}

		// Go through the hierarchy, doing the leave and enter events, as we change active control.
		Control currentFocusedControl = focusedControl;
		ReDo:
		while (activeControl != currentFocusedControl)
		{
			if (currentFocusedControl == null || IsParent(currentFocusedControl, activeControl))
			{
				Control currentActiveControl = activeControl;
				while (true)
				{
					Control parent = currentActiveControl.Parent;
					// Find which is lower in the hierarchy (which comes first), the active Control or the focused Control.
					// Set the focus to this if validated.
					if (parent == this || parent == currentFocusedControl)
					{
						Control prevFocusedControl = focusedControl = currentFocusedControl;
						ValidateEnter(currentActiveControl);
						if (focusedControl != prevFocusedControl)
						{
							currentFocusedControl = focusedControl;
						}
						else
						{
							currentFocusedControl = currentActiveControl;
							try
							{
								currentFocusedControl.DoEnter();
							}
							catch (Exception e)
							{
								Application.OnThreadException(e);
							}
						}
						goto ReDo;
					}
					currentActiveControl = currentActiveControl.Parent;
				}
			}
			
			// Find innermost focused container.
			
			ContainerControl innerMostFocusedContainer = this;
			while (innerMostFocusedContainer.focusedControl is ContainerControl)
			{
				innerMostFocusedContainer = innerMostFocusedContainer.focusedControl as ContainerControl;
			}

			// Reset the focusedControl and activeControl of the innermost container.
			Control highestResetControl = null;
			if (innerMostFocusedContainer.focusedControl == null)
			{
				// The container has the focus set.
				currentFocusedControl = innerMostFocusedContainer;
				if (innerMostFocusedContainer.Parent != null)
				{
					// Remove the active control and focused control of this containers container.
					ContainerControl parentContainer = innerMostFocusedContainer.Parent.GetContainerControl() as ContainerControl;
					highestResetControl = parentContainer;
					if (parentContainer != null && parentContainer != this)
					{
						parentContainer.activeControl = null;
						parentContainer.focusedControl = null;
					}
				}
			}
			else
			{
				// There is a focused control of the innermost container.
				currentFocusedControl = innerMostFocusedContainer.focusedControl;
				highestResetControl = innerMostFocusedContainer;
				if (innerMostFocusedContainer != this)
				{
					innerMostFocusedContainer.focusedControl = null;
					if (innerMostFocusedContainer.Parent == null || !(innerMostFocusedContainer.Parent is MdiClient))
					{
						innerMostFocusedContainer.activeControl = null;
					}
				}
			}

			// Do all the leave events up the hierarchy.
			do
			{
				Control prevFocusedControl = currentFocusedControl;
				if (currentFocusedControl != null)
				{
					currentFocusedControl = currentFocusedControl.Parent;
				}
				if (currentFocusedControl == this)
				{
					currentFocusedControl = null;
				}
				if (prevFocusedControl != null)
				{
					try
					{
						prevFocusedControl.DoLeave();
					}
					catch (Exception e)
					{
						Application.OnThreadException(e);
					}
				}
			}
			while (currentFocusedControl != null && currentFocusedControl != highestResetControl && !IsParent(currentFocusedControl, activeControl));
		}

		focusedControl = activeControl;
		if (activeControl != null)
		{
			ValidateEnter(activeControl);
		}
		
		if (activeControl == value)
		{
			Form form = FindForm();
			if (form != null)
			{
				form.UpdateDefaultButton();
			}
		}

		return (activeControl == value);
	}

	protected override void OnCreateControl()
	{
		base.OnCreateControl();
		OnBindingContextChanged(EventArgs.Empty);
	}

#if CONFIG_COMPONENT_MODEL
	[EditorBrowsable(EditorBrowsableState.Advanced)]
#endif // CONFIG_COMPONENT_MODEL
	protected override void OnControlRemoved(ControlEventArgs e)
	{
		Control control = e.Control;
		// Make sure the unvalidated control and active control are cleaned up.
		if (control == unvalidatedControl || control.Contains(unvalidatedControl))
		{
			unvalidatedControl = null;
		}
		if (control == activeControl || control.Contains(activeControl))
		{
			ActiveControl = null;
		}
		base.OnControlRemoved(e);
	}

	// Brubbel
	internal virtual void AfterControlRemoved(Control control)
	{
		// Select next control, if control was active and removed.
		if (control == activeControl || control.Contains(activeControl))
		{
			if( base.SelectNextControl( control, true, true, true, true ) ) {
				this.SetFocus( activeControl );
			}
			else {
				this.SetActiveControl( null );
			}
		}
		else if( (this.activeControl == null) && (this.Parent != null) ) {
			ContainerControl container = Parent.GetContainerControl() as ContainerControl;
			if( null != container && container.ActiveControl == this ) {
				Form form = base.FindForm();
				if( null != form ) {
					form.SelectNextControl( this, true, true, true, true );
				}
				
			}
		}
		else {
		}
		
		// Make sure the unvalidated control and active control are cleaned up.
		if (control == unvalidatedControl || control.Contains(unvalidatedControl))
		{
			unvalidatedControl = null;
		}
	}
	
#if CONFIG_COMPONENT_MODEL
	[EditorBrowsable(EditorBrowsableState.Advanced)]
#endif // CONFIG_COMPONENT_MODEL
	protected override bool ProcessDialogChar(char charCode)
	{
		if (GetTopLevel())
			{
				if(charCode != ' ' && ProcessMnemonic(charCode))
				{
					return true;
				}
			}
		return base.ProcessDialogChar(charCode);
	}

	protected override bool ProcessDialogKey(Keys keyData)
	{
		if ((keyData & (Keys.Alt | Keys.Control)) == 0)
		{
			Keys key = keyData & Keys.KeyCode;
			if (key == Keys.Tab)
			{
				if (ProcessTabKey((keyData & Keys.Shift) == 0))
				{
					return true;
				}
			}
			else if (key == Keys.Left || key == Keys.Up || key == Keys.Right || key == Keys.Down)
			{
				Control control;
				if (activeControl != null)
				{
					control = activeControl.Parent;
				}
				else
				{
					control = this;
				}
				bool forward = (key == Keys.Right || key == Keys.Down);
				if (control.SelectNextControl(activeControl, forward , false, false, true))
				{
					return true;
				}
			}
		}
		return base.ProcessDialogKey(keyData);

	}

	protected override bool ProcessMnemonic(char charCode)
	{

		if (Controls.Count == 0)
		{
			return false; 
		}
		Control active = ActiveControl;
		// Find the bottom most active control or the container if there isn't one.
		while (true)
		{
			ContainerControl container = active as ContainerControl;
			if (container == null)
			{
				break;
			}
			Control newActiveControl = container.ActiveControl;
			if (newActiveControl == null)
			{
				break;
			}
			active = newActiveControl;
		}

		// Process the mnemonics if needed.
		bool back = false;
		Control mnemonicControl = active;

		do
		{
			mnemonicControl = GetNextControl(mnemonicControl, true);
			if (mnemonicControl == null)
			{
				if (back)
				{
					break;
				}
				back = true;
			}
			else
			{
				if (mnemonicControl.ProcessMnemonicInternal(charCode))
				{
					return true;
				}
			}
		}
		while (mnemonicControl != active);
		return false;
	}

	private new void ScrollControlIntoView(Control control)
	{
		// Find scrollable parent.
		while (true)
		{
			control = control.Parent;
			if (control == null)
			{
				return;
			}
			ScrollableControl scrollableControl = control as ScrollableControl;
			if (scrollableControl != null)
			{
				scrollableControl.ScrollControlIntoView(activeControl);
				continue;
			}
		}
	}

	protected override void Select(bool directed, bool forward)
	{
		if (Parent != null)
		{
			IContainerControl container = Parent.GetContainerControl();
			if (container != null)
			{
				container.ActiveControl = this;
				if (directed && container.ActiveControl == this)
				{
					SelectNextControl(null, forward, true, true, false);		 
				}
			}
		}
		if (directed)
		{
			SelectNextControl(null, forward, true, true, false);
		}
	}

	private void ValidateEnter(Control control)
	{
		if (unvalidatedControl == null || control.CausesValidation)
		{
			return;
		}
		while (control != null && !IsParent(control, unvalidatedControl))
		{
			control = control.Parent;
		}
		ValidateHierarchy(control);
	}

	public bool Validate()
	{
		if (unvalidatedControl == null)
		{
			if (focusedControl is ContainerControl && focusedControl.CausesValidation)
			{
				(focusedControl as ContainerControl).Validate();
			}
			else
			{
				unvalidatedControl = focusedControl;
			}
		}
		return ValidateHierarchy(null);
	}

	// Validate from control back up to unvalidated or the currently focused control.
	private bool ValidateHierarchy(Control control)
	{
		if (unvalidatedControl == null)
		{
			unvalidatedControl = focusedControl;
			return true;
		}
		if (validating)
		{
			return false;
		}
		if (control == null)
		{
			control = this;
		}
		if (!IsParent(control, unvalidatedControl))
		{
			return false;
		}
		validating = true;
		bool cancelValidate = false;
		Control currentControl = unvalidatedControl;
		// Validate starts off uncancelled.
		if (activeControl != null)
		{
			activeControl.DoValidationCancel(false);
		}
		try
		{
			while(currentControl != control)
			{
				// Do the validating and validated events if needed.
				if (currentControl.CausesValidation)
				{
					bool cancelled = false;
					try
					{
						cancelled = currentControl.DoValidating();
					}
					catch (Exception)
					{
						cancelValidate = true;
						throw;
					}
					if (cancelled)
					{
						cancelValidate = true;
						break;
					}
					try
					{
						currentControl.DoValidated();
					}
					catch (Exception e)
					{
						Application.OnThreadException(e);
					}
				}
				// Move up the hierarchy.
				currentControl = currentControl.Parent;
			}
			if (cancelValidate)
			{
				if (activeControl != null)
				{
					activeControl.DoValidationCancel(true);
					if (activeControl is ContainerControl)
					{
						ContainerControl containerControl = activeControl as ContainerControl;
						if (containerControl.focusedControl != null)
						{
							containerControl.focusedControl.DoValidationCancel(true);
						}
						containerControl.ResetControlsRecursive();
					}
				}
				ActiveControl = unvalidatedControl;
			}
		}
		finally
		{
			unvalidatedControl = null;
			validating = false;
		}
		return !cancelValidate;
	}

	internal void ResetControlsRecursive()
	{
		if (activeControl is ContainerControl)
		{
			(activeControl as ContainerControl).ResetControlsRecursive();
		}
		focusedControl = null;
		activeControl = null;
	}

	private bool IsParent(Control parent, Control control)
	{
		while (control != null)
		{
			if (parent == control)
			{
				return true;
			}
			control = control.Parent;
		}
		return false;
	}

	// Dispose of this control.
	protected override void Dispose(bool disposing)
	{
		activeControl = null;
		focusedControl = null;
		unvalidatedControl = null;
	
		base.Dispose(disposing);
	}

	// Process the tab key.
	protected virtual bool ProcessTabKey(bool forward)
	{
		return SelectNextControl(activeControl, forward, true, true, false);
	}

	private void SetFocus(Control control)
	{
		if (control != null && control.toolkitWindow != null && control.Visible)
		{
			control.toolkitWindow.Focus();
			return; 
		}

		// Find the first visible container.
		ContainerControl container = this;
		Control parent;
		while (container != null && !container.Visible)
		{
			parent = container.Parent;
			if (parent == null)
			{
				if (container.Visible)
				{
					container.toolkitWindow.Focus();
				}
				break;
			}
			container = parent.GetContainerControl() as ContainerControl;
		}
	}

	protected override void OnGotFocus(EventArgs e)
	{
		if (ActiveControl != null)
		{
			if (!ActiveControl.Visible)
			{
				base.OnGotFocus(EventArgs.Empty);
			}
			SetFocus(activeControl);
			return;
		}
		if (Parent != null)
		{
			IContainerControl container = Parent.GetContainerControl();
			if (container != null)
			{
				if (!container.ActivateControl(this))
				{
					return;
				}
			}
		}
		base.OnGotFocus (e);
	}


#if !CONFIG_COMPACT_FORMS

	// Process a message.
#if CONFIG_COMPONENT_MODEL
	[EditorBrowsable(EditorBrowsableState.Advanced)]
#endif // CONFIG_COMPONENT_MODEL
	protected override void WndProc(ref Message m)
	{
		base.WndProc(ref m);
	}

#endif // !CONFIG_COMPACT_FORMS

}; // class ContainerControl

}; // namespace System.Windows.Forms

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us