- Implemented RadioButton.ProcessMnemonic
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ContainerControl.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2004 Novell, Inc.
21 //
22 // Authors:
23 //      Peter Bartok    pbartok@novell.com
24 //
25 //
26
27
28 // NOT COMPLETE
29
30 using System.ComponentModel;
31 using System.ComponentModel.Design;
32
33 namespace System.Windows.Forms {
34         public class ContainerControl : ScrollableControl, IContainerControl {
35                 private Control active_control;
36                 private Control focused_control;
37                 private Control unvalidated_control;
38
39                 #region Public Constructors
40                 public ContainerControl() {
41                         active_control = null;
42                         focused_control = null;
43                         unvalidated_control = null;
44                 }
45                 #endregion      // Public Constructors
46
47                 #region Public Instance Properties
48                 [Browsable (false)]
49                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
50                 public Control ActiveControl {
51                         get {
52                                 return active_control;
53                         }
54
55                         set {
56                                 if ((active_control==value) || (value==null)) {
57                                         return;
58                                 }
59
60                                 active_control = value;
61
62                                 if (!Contains(value) && this != value) {
63                                         throw new ArgumentException("Not a child control");
64                                 }
65
66                                 // Scroll control into view
67
68                                 // Let the control know it's selected
69                                 Select(value);
70                         }
71                 }
72
73                 [Browsable (false)]
74                 public override BindingContext BindingContext {
75                         get {
76                                 if (base.BindingContext == null) {
77                                         base.BindingContext = new BindingContext();
78                                 }
79                                 return base.BindingContext;
80                         }
81
82                         set {
83                                 base.BindingContext = value;
84                         }
85                 }
86
87                 [Browsable (false)]
88                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
89                 public Form ParentForm {
90                         get {
91                                 Control parent;
92
93                                 parent = this.parent;
94
95                                 while (parent != null) {
96                                         if (parent is Form) {
97                                                 return (Form)parent;
98                                         }
99                                         parent = parent.parent;
100                                 }
101
102                                 return null;
103                         }
104                 }
105                 #endregion      // Public Instance Properties
106
107                 #region Protected Instance Methods
108                 protected override CreateParams CreateParams {
109                         get {
110                                 return base.CreateParams;
111                         }
112                 }
113                 #endregion      // Public Instance Methods
114
115                 #region Public Instance Methods
116                 [MonoTODO]
117                 public bool Validate() {
118                         throw new NotImplementedException();
119                 }
120
121                 public bool ActivateControl(Control control) {
122                         return Select(control);
123                 }
124                 #endregion      // Public Instance Methods
125
126                 #region Protected Instance Methods
127                 [EditorBrowsable (EditorBrowsableState.Advanced)]
128                 protected override void AdjustFormScrollbars(bool displayScrollbars) {
129                         // Internal MS
130                 }
131
132                 protected override void Dispose(bool disposing) {
133                         base.Dispose(disposing);
134                 }
135
136                 [EditorBrowsable (EditorBrowsableState.Advanced)]
137                 protected override void OnControlRemoved(ControlEventArgs e) {
138                         if (e.Control == this.unvalidated_control) {
139                                 this.unvalidated_control = null;
140                         }
141
142                         if (e.Control == this.active_control) {
143                                 this.unvalidated_control = null;
144                         }
145
146                         base.OnControlRemoved(e);
147                 }
148
149                 protected override void OnCreateControl() {
150                         base.OnCreateControl();
151                 }
152
153                 [EditorBrowsable (EditorBrowsableState.Advanced)]
154                 protected override bool ProcessDialogChar(char charCode) {
155                         if (GetTopLevel()) {
156                                 if (ProcessMnemonic(charCode)) {
157                                         return true;
158                                 }
159                         }
160                         return base.ProcessDialogChar(charCode);
161                 }
162
163                 protected override bool ProcessDialogKey(Keys keyData) {
164                         Keys    key;
165                         bool    forward;
166
167                         key = keyData & Keys.KeyCode;
168                         forward = true;
169
170                         switch (key) {
171                                 case Keys.Tab: {
172                                         if (ProcessTabKey((Control.ModifierKeys & Keys.Shift) == 0)) {
173                                                 return true;
174                                         }
175                                         break;
176                                 }
177
178                                 case Keys.Left: {
179                                         forward = false;
180                                         goto case Keys.Down;
181                                 }
182
183                                 case Keys.Up: {
184                                         forward = false;
185                                         goto case Keys.Down;
186                                 }
187
188                                 case Keys.Right: {
189                                         goto case Keys.Down;
190                                 }
191                                 case Keys.Down: {
192                                         if (SelectNextControl(active_control, forward, false, false, true)) {
193                                                 return true;
194                                         }
195                                         break;
196                                 } 
197
198
199                         }
200                         return base.ProcessDialogKey(keyData);
201                 }
202
203                 protected override bool ProcessMnemonic(char charCode) {
204                         bool    wrapped;
205                         Control c;
206
207                         wrapped = false;
208                         c = active_control;
209
210                         do {
211                                 c = GetNextControl(c, true);
212                                 if (c != null) {
213                                         // This is stupid. I want to be able to call c.ProcessMnemonic directly
214                                         if (c.ProcessControlMnemonic(charCode)) {
215                                                 return(true);
216                                         }
217                                         continue;
218                                 } else {
219                                         if (wrapped) {
220                                                 break;
221                                         }
222                                         wrapped = true;
223                                 }
224                         } while (c != active_control);
225                         
226                         return false;
227                 }
228
229                 protected virtual bool ProcessTabKey(bool forward) {
230                         return SelectNextControl(active_control, forward, true, true, true);
231                 }
232
233                 protected override void Select(bool directed, bool forward) {
234                         base.Select(directed, forward);
235                 }
236
237                 protected virtual void UpdateDefaultButton() {
238                         // MS Internal
239                 }
240
241                 [EditorBrowsable (EditorBrowsableState.Advanced)]
242                 protected override void WndProc(ref Message m) {
243                         base.WndProc(ref m);
244                 }
245                 #endregion      // Protected Instance Methods
246         }
247 }