2002-06-30 Rachel Hestilow <hestilow@ximian.com>
[mono.git] / mcs / class / System.Windows.Forms / Gtk / Control.cs
1 //
2 // System.Windows.Forms.Form
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //   Rachel Hestilow (hestilow@ximian.com)
7 //
8 // (C) 2002 Ximian, Inc
9 //
10
11 using System;
12 using System.Drawing;
13 using Gtk;
14 using GtkSharp;
15 using System.ComponentModel;
16 using System.Collections;
17
18 namespace System.Windows.Forms {
19         public class Control : Component {
20                 internal Widget widget;
21                 Control parent;
22                 string text;
23                 int left, top, width, height;
24                 ControlCollection controls = CreateControlsInstance ();
25                 Point location = new Point (0, 0);
26                 Gtk.Layout layout = null;
27                 AnchorStyles anchor = AnchorStyles.Top|AnchorStyles.Left;
28                 
29                 static int init_me;
30
31                 public class ControlCollection : IList, ICollection, IEnumerable, ICloneable 
32                 {
33                         ArrayList list = new ArrayList ();
34                         Control owner;
35
36                         public ControlCollection (Control owner)
37                         {
38                                 this.owner = owner;
39                         }
40
41                         private ControlCollection ()
42                         {
43                         }
44
45                         // ControlCollection
46                         public virtual void Add (Control value) {
47                                 list.Add (value);
48                                 owner.OnControlAdded (new ControlEventArgs (value));
49                         }
50                         public virtual void AddRange (Control[] controls) {
51                                 list.AddRange (controls);
52                                 foreach (Control c in controls)
53                                         owner.OnControlAdded (new ControlEventArgs (c));
54                         }
55                         
56                         public bool Contains (Control value) { return list.Contains (value); }
57                         public int IndexOf (Control value) { return list.IndexOf (value); }
58                         public virtual void Remove (Control value) {
59                                 list.Remove (value);
60                                 owner.OnControlAdded (new ControlEventArgs (value));
61                         }
62                         public virtual Control this[int index] { get { return (Control) list[index]; } }
63                         public int GetChildIndex (Control child) {
64                                 return GetChildIndex (child, true);
65                         }
66                         public int GetChildIndex (Control child, bool throwException) {
67                                 if (throwException && !Contains (child))
68                                         throw new Exception ();
69                                 return list.IndexOf (child);
70                         }
71                         public int IndexOf (Control value) { return list.IndexOf (value); }
72                         public void SetChildIndex (Control child, int newIndex) {
73                                 int oldIndex = GetChildIndex (child);
74                                 if (oldIndex == newIndex)
75                                         return;
76                                 // is this correct behavior?
77                                 Control other = (Control) list[newIndex];
78                                 list[oldIndex] = other;
79                                 list[newIndex] = child;
80                         }
81
82                         // IList
83                         public bool IsFixedSize { get { return list.IsFixedSize; } }
84                         public bool IsReadOnly { get { return list.IsReadOnly; } }
85                         public object this[int index]
86                         {
87                                 get { return list[index]; }
88                                 set { list[index] = value; }
89                         }
90                         int IList.Add (object value) { return list.Add (value); }
91                         public void Clear () { list.Clear (); }
92                         bool IList.Contains (object value) { return list.Contains (value); }
93                         int IList.IndexOf (object value) { return list.IndexOf (value); }
94                         void IList.Insert (int index, object value) { list.Insert (index, value); }
95                         void IList.Remove (object value) { list.Remove (value); }
96                         public void RemoveAt (int index) { list.RemoveAt (index); }
97
98                         // ICollection
99                         public int Count { get { return list.Count; } }
100                         public bool IsSynchronized { get { return list.IsSynchronized; } }
101                         public object SyncRoot { get { return list.SyncRoot; } }
102                         public void CopyTo (Array array, int index) { list.CopyTo (array, index); }
103                         
104                         // IEnumerable
105                         public IEnumerator GetEnumerator () { return list.GetEnumerator (); }
106
107                         // ICloneable
108                         public object Clone () {
109                                 ControlCollection c = new ControlCollection ();
110                                 c.list = (ArrayList) list.Clone ();
111                                 c.owner = owner;
112                                 return c;
113                         }
114                 }
115                 
116                 static Control ()
117                 {
118                         init_me = 1;
119                 }
120                 
121                 public Control () : this ("")
122                 {
123                 }
124
125                 public Control (string text) : this (null, text)
126                 {
127                 }
128
129                 public Control (Control parent, string text)
130                 {
131                         this.parent = parent;
132                         this.text = text;
133                 }
134
135                 public Control (string text, int left, int top, int width, int height)
136                 {
137                 }
138
139                 public Control (Control parent, string text, int left, int top, int width, int height)
140                 {
141                 }
142
143                 internal Widget Widget {
144                         get {
145                                 if (widget == null)
146                                         widget = CreateWidget ();
147                                 return widget;
148                         }
149                 }
150                 
151                 internal virtual Widget CreateWidget ()
152                 {
153                         layout = new Gtk.Layout (new Gtk.Adjustment (IntPtr.Zero), new Gtk.Adjustment (IntPtr.Zero));
154                         layout.Show ();
155                         return layout;
156                 }
157
158                 public virtual string Text {
159                         get {
160                                 return text;
161                         }
162
163                         set {
164                                 text = value;
165                                 OnTextChanged (EventArgs.Empty);
166                         }
167                 }
168                 
169                 public event EventHandler TextChanged;
170
171                 protected virtual void OnTextChanged (EventArgs e) {
172                         if (TextChanged != null)
173                          TextChanged (this, e);
174                 }
175                 
176                 public void Show ()
177                 {
178                         Widget.Show ();
179                 }
180
181                 public void Hide ()
182                 {
183                         Widget.Hide ();
184                 }
185
186                 public bool Visible {
187                         get {
188                                 return Widget.Visible;
189                         }
190
191                         set {
192                                 Widget.Visible = value;
193                         }
194                 }
195                 
196                 public ControlCollection Controls {
197                         get { return controls;}
198                 }
199                 
200                 protected virtual ControlCollection CreateControlsInstance() {
201                         controls = new ControlCollection (this);
202                         return controls;
203                 }
204                 
205                 public event ControlEventHandler ControlAdded;
206                 public event ControlEventHandler ControlRemoved;
207
208                 internal void ControlLocationChanged (object o, EventArgs e)
209                 {
210                         Control c = (Control) o;
211                         Point l = c.Location;
212                         if (layout == null) {
213                                 Widget w = Widget;
214                         }
215                                 
216                         layout.Move (c.Widget, l.X, l.Y); 
217                 }
218
219                 protected virtual void OnControlAdded(ControlEventArgs e) {
220                         e.Control.Visible = true;
221                         
222                         if (ControlAdded != null)
223                                 ControlAdded (this, e);
224
225                         Point l = e.Control.Location;
226                         if (layout == null) { 
227                                 Widget w = Widget;
228                         }
229                         layout.Put (e.Control.Widget, l.X, l.Y);
230                         e.Control.LocationChanged += new EventHandler (ControlLocationChanged);
231                 }
232
233                 protected virtual void OnControlRemoved(ControlEventArgs e) {
234                         if (ControlRemoved != null)
235                                 ControlRemoved (this, e);
236                 }
237
238                 public Point Location {
239                         get { return location; }
240                         set {
241                                 location = value;
242                                 OnLocationChanged (EventArgs.Empty);
243                         }
244                 }
245
246                 public event EventHandler LocationChanged;
247
248                 public virtual void OnLocationChanged (EventArgs e) {
249                         
250                         if (LocationChanged != null)
251                                 LocationChanged (this, e);
252                 }
253
254                 public event EventHandler Click;
255
256                 protected virtual void OnClick (EventArgs e) {
257                         if (Click != null)
258                                 Click (this, e);
259                 }
260                 
261                 public virtual AnchorStyles Anchor {
262                         get { return anchor; }
263                         set { anchor=value; }
264     }
265         }
266 }