* TreeView.cs: Don't draw the selected node when we lose
[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                         ControlRemoved += new ControlEventHandler(OnControlRemoved);
45                 }
46                 #endregion      // Public Constructors
47
48                 #region Public Instance Properties
49                 [Browsable (false)]
50                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
51                 public Control ActiveControl {
52                         get {
53                                 return active_control;
54                         }
55
56                         set {
57                                 if ((active_control==value) || (value==null)) {
58                                         return;
59                                 }
60
61                                 active_control = value;
62
63                                 if (!Contains(value) && this != value) {
64                                         throw new ArgumentException("Not a child control");
65                                 }
66
67                                 // Scroll control into view
68
69                                 // Let the control know it's selected
70                                 Select(value);
71                         }
72                 }
73
74                 [Browsable (false)]
75                 public override BindingContext BindingContext {
76                         get {
77                                 if (base.BindingContext == null) {
78                                         base.BindingContext = new BindingContext();
79                                 }
80                                 return base.BindingContext;
81                         }
82
83                         set {
84                                 base.BindingContext = value;
85                         }
86                 }
87
88                 [Browsable (false)]
89                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
90                 public Form ParentForm {
91                         get {
92                                 Control parent;
93
94                                 parent = this.parent;
95
96                                 while (parent != null) {
97                                         if (parent is Form) {
98                                                 return (Form)parent;
99                                         }
100                                         parent = parent.parent;
101                                 }
102
103                                 return null;
104                         }
105                 }
106                 #endregion      // Public Instance Properties
107
108                 #region Protected Instance Methods
109                 protected override CreateParams CreateParams {
110                         get {
111                                 return base.CreateParams;
112                         }
113                 }
114                 #endregion      // Public Instance Methods
115
116                 #region Public Instance Methods
117                 [MonoTODO]
118                 static bool ValidateWarned;
119                 public bool Validate() {
120                         //throw new NotImplementedException();
121                         if (!ValidateWarned) {
122                                 Console.WriteLine("ContainerControl.Validate is not yet implemented");
123                                 ValidateWarned = true;
124                         }
125                         return true;
126                 }
127
128                 bool IContainerControl.ActivateControl(Control control) {
129                         return Select(control);
130                 }
131                 #endregion      // Public Instance Methods
132
133                 #region Protected Instance Methods
134                 [EditorBrowsable (EditorBrowsableState.Advanced)]
135                 protected override void AdjustFormScrollbars(bool displayScrollbars) {
136                         base.AdjustFormScrollbars(displayScrollbars);
137                 }
138
139                 protected override void Dispose(bool disposing) {
140                         base.Dispose(disposing);
141                 }
142
143                 // LAMESPEC This used to be documented, but it's not in code 
144                 // and no longer listed in MSDN2
145                 // [EditorBrowsable (EditorBrowsableState.Advanced)]
146                 // protected override void OnControlRemoved(ControlEventArgs e) {
147                 private void OnControlRemoved(object sender, ControlEventArgs e) {
148                         if (e.Control == this.unvalidated_control) {
149                                 this.unvalidated_control = null;
150                         }
151
152                         if (e.Control == this.active_control) {
153                                 this.unvalidated_control = null;
154                         }
155
156                         // base.OnControlRemoved(e);
157                 }
158
159                 protected override void OnCreateControl() {
160                         base.OnCreateControl();
161                         // MS seems to call this here, it gets the whole databinding process started
162                         OnBindingContextChanged (EventArgs.Empty);
163                 }
164
165                 [EditorBrowsable (EditorBrowsableState.Advanced)]
166                 protected override bool ProcessDialogChar(char charCode) {
167                         if (GetTopLevel()) {
168                                 if (ProcessMnemonic(charCode)) {
169                                         return true;
170                                 }
171                         }
172                         return base.ProcessDialogChar(charCode);
173                 }
174
175                 protected override bool ProcessDialogKey(Keys keyData) {
176                         Keys    key;
177                         bool    forward;
178
179                         key = keyData & Keys.KeyCode;
180                         forward = true;
181
182                         switch (key) {
183                                 case Keys.Tab: {
184                                         if (ProcessTabKey((Control.ModifierKeys & Keys.Shift) == 0)) {
185                                                 return true;
186                                         }
187                                         break;
188                                 }
189
190                                 case Keys.Left: {
191                                         forward = false;
192                                         goto case Keys.Down;
193                                 }
194
195                                 case Keys.Up: {
196                                         forward = false;
197                                         goto case Keys.Down;
198                                 }
199
200                                 case Keys.Right: {
201                                         goto case Keys.Down;
202                                 }
203                                 case Keys.Down: {
204                                         if (SelectNextControl(active_control, forward, false, false, true)) {
205                                                 return true;
206                                         }
207                                         break;
208                                 } 
209
210
211                         }
212                         return base.ProcessDialogKey(keyData);
213                 }
214
215                 protected override bool ProcessMnemonic(char charCode) {
216                         bool    wrapped;
217                         Control c;
218
219                         wrapped = false;
220                         c = active_control;
221
222                         do {
223                                 c = GetNextControl(c, true);
224                                 if (c != null) {
225                                         // This is stupid. I want to be able to call c.ProcessMnemonic directly
226                                         if (c.ProcessControlMnemonic(charCode)) {
227                                                 return(true);
228                                         }
229                                         continue;
230                                 } else {
231                                         if (wrapped) {
232                                                 break;
233                                         }
234                                         wrapped = true;
235                                 }
236                         } while (c != active_control);
237                         
238                         return false;
239                 }
240
241                 protected virtual bool ProcessTabKey(bool forward) {
242                         return SelectNextControl(active_control, forward, true, true, true);
243                 }
244
245                 protected override void Select(bool directed, bool forward) {
246                         base.Select(directed, forward);
247                 }
248
249                 protected virtual void UpdateDefaultButton() {
250                         // MS Internal
251                 }
252
253                 [EditorBrowsable (EditorBrowsableState.Advanced)]
254                 protected override void WndProc(ref Message m) {
255                         base.WndProc(ref m);
256                 }
257                 #endregion      // Protected Instance Methods
258         }
259 }