Got rid of some gtk+-warnings about str being NULL
[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 //   Joel Basson  (jstrike@mweb.co.za)
8 //   Philip Van Hoof (me@freax.org)
9 // (C) 2002 Ximian, Inc
10 //
11
12 using System;
13 using System.Drawing;
14 using Gtk;
15 using GtkSharp;
16 using System.ComponentModel;
17 using System.Collections;
18
19 namespace System.Windows.Forms {
20         public class Control : Component {
21                 internal Widget widget;
22                 Control parent;
23                 string text, name;
24                 Size size;
25                 int left, top, width, height, tabindex, index;
26                 ControlCollection controls;
27                 Point location = new System.Drawing.Point (0, 0);
28                 Gtk.Layout layout = null;
29                 AnchorStyles anchor = AnchorStyles.Top|AnchorStyles.Left;
30                 bool tabStop=true;
31                 static int init_me;
32                 RightToLeft rightToLeft;
33
34                 public class ControlCollection : IList, ICollection, IEnumerable, ICloneable 
35                 {
36                         ArrayList list = new ArrayList ();
37                         Control owner;
38
39                         public ControlCollection (Control owner)
40                         {
41                                 this.owner = owner;
42                         }
43
44                         private ControlCollection ()
45                         {
46                         }
47
48                         // ControlCollection
49                         public virtual void Add (Control value, bool doevent) {
50                                 if (doevent == true) {
51                                         this.Add (value);
52                                         return;
53                                 }
54                                 list.Add(value);
55                         }
56                         public virtual void Add (Control value) {
57                                 list.Add (value);
58                                 owner.OnControlAdded (new ControlEventArgs (value));
59                         }
60                         public virtual void AddRange (Control[] controls) {
61                                 list.AddRange (controls);
62                                 foreach (Control c in controls) 
63                                         owner.OnControlAdded (new ControlEventArgs (c));
64                         }
65                         
66                         public bool Contains (Control value) { return list.Contains (value); }
67                         public virtual void Remove (Control value) {
68                                 list.Remove (value);
69                                 owner.OnControlAdded (new ControlEventArgs (value));
70                         }
71                         public virtual Control this[int index] { get { return (Control) list[index]; } }
72                         public int GetChildIndex (Control child) {
73                                 return GetChildIndex (child, true);
74                         }
75                         public int GetChildIndex (Control child, bool throwException) {
76                                 if (throwException && !Contains (child))
77                                         throw new Exception ();
78                                 return list.IndexOf (child);
79                         }
80                         public int IndexOf (Control value) { return list.IndexOf (value); }
81                         public void SetChildIndex (Control child, int newIndex) {
82                                 int oldIndex = GetChildIndex (child);
83                                 if (oldIndex == newIndex)
84                                         return;
85                                 // is this correct behavior?
86                                 Control other = (Control) list[newIndex];
87                                 list[oldIndex] = other;
88                                 list[newIndex] = child;
89                         }
90
91                         // IList
92                         public bool IsFixedSize { get { return list.IsFixedSize; } }
93                         public bool IsReadOnly { get { return list.IsReadOnly; } }
94                         int IList.Add (object value) { return list.Add (value); }
95                         public void Clear () { list.Clear (); }
96                         bool IList.Contains (object value) { return list.Contains (value); }
97                         int IList.IndexOf (object value) { return list.IndexOf (value); }
98                         void IList.Insert (int index, object value) { list.Insert (index, value); }
99                         void IList.Remove (object value) { list.Remove (value); }
100                         public void RemoveAt (int index) { list.RemoveAt (index); }
101
102                         // ICollection
103                         public int Count { get { return list.Count; } }
104                         public bool IsSynchronized { get { return list.IsSynchronized; } }
105                         public object SyncRoot { get { return list.SyncRoot; } }
106                         public void CopyTo (Array array, int index) { list.CopyTo (array, index); }
107                         
108                         // IEnumerable
109                         public IEnumerator GetEnumerator () { return list.GetEnumerator (); }
110
111                         // ICloneable
112                         public object Clone () {
113                                 ControlCollection c = new ControlCollection ();
114                                 c.list = (ArrayList) list.Clone ();
115                                 c.owner = owner;
116                                 return c;
117                         }
118
119                         object IList.this[int index]
120                         {
121                                 get { return list[index]; }
122                                 set { list[index] = value; }
123                         }
124         
125                 }
126
127                 static Control ()
128                 {
129                         Gtk.Application.Init ();
130                         init_me = 1;
131                 }
132                 
133                 public Control () : this ("")
134                 {
135                         this.text = "";
136                 }
137
138                 public Control (string text) : this (null, text)
139                 {
140                 }
141
142                 public Control (Control parent, string text)
143                 {
144                         this.parent = parent;
145                         this.text = text;
146                         
147                 }
148
149                 public Control (string text, int left, int top, int width, int height)
150                 {                       
151                 }
152
153                 public Control (Control parent, string text, int left, int top, int width, int height)
154                 {
155                         
156                 }
157
158                 internal Widget Widget {
159                         get {
160                                 if (widget == null)
161                                         widget = CreateWidget ();
162                                 return widget;
163                         }
164                 }
165                 
166                 internal virtual Widget CreateWidget ()
167                 {
168                         layout = new Gtk.Layout (new Gtk.Adjustment (IntPtr.Zero), new Gtk.Adjustment (IntPtr.Zero));
169                         layout.Show ();
170                         return layout;
171                 }
172
173                 public virtual string Text {
174                         get {
175                                 return text;
176                         }
177
178                         set {
179                                 text = value;
180                                 OnTextChanged (EventArgs.Empty);
181                         }
182                 }
183                 
184                 public event EventHandler TextChanged;
185
186                 protected virtual void OnTextChanged (EventArgs e) {
187                         if (TextChanged != null)
188                          TextChanged (this, e);
189                 }
190
191
192                 public virtual string Name {
193                         get {
194                                 return name;
195                         }
196
197                         set {
198                                 name = value;
199                                 Widget.Name = value;
200                         }
201                 }
202
203                 public bool Enabled {
204                         get {
205                                 return Widget.Sensitive;
206                         }
207                         set {
208                                 Widget.Sensitive = value;
209                         }
210                 }
211
212                 public Size Size {
213                         get { 
214                                 return size;
215                         }
216                         set {
217                                 size = value;
218                                 Widget.SetSizeRequest (value.Width,value.Height);
219                         }
220                 }
221
222                 public int TabIndex {
223                         get { 
224                                 return tabindex;
225                         }
226                         set {
227                                 tabindex = value;
228                         }
229                 }
230
231                 public int Index {
232                         get { 
233                                 return index;
234                         }
235                         set {
236                                 index = value;
237                         }
238                 }
239
240                 public void Show ()
241                 {
242                         Widget.Show ();
243                 }
244
245                 public void Hide ()
246                 {
247                         Widget.Hide ();
248                 }
249
250                 public bool Visible {
251                         get {
252                                 return Widget.Visible;
253                         }
254
255                         set {
256                                 Widget.Visible = value;
257                         }
258                 }
259                 
260                 
261                 public ControlCollection Controls {
262                         get { if (controls == null) controls = new ControlCollection (this); return controls;}
263                 }
264                 
265                 public event ControlEventHandler ControlAdded;
266                 public event ControlEventHandler ControlRemoved;
267
268                 internal void ControlLocationChanged (object o, EventArgs e)
269                 {
270                         Control c = (Control) o;
271                         Point l = c.Location;
272                         if (layout == null) {
273                                 Widget w = Widget;
274                         }
275                                 
276                         layout.Move (c.Widget, l.X, l.Y); 
277                 }
278
279                 protected virtual void OnControlAdded(ControlEventArgs e) {
280                         e.Control.Visible = true;
281                         
282                         if (ControlAdded != null)
283                                 ControlAdded (this, e);
284
285                         Point l = e.Control.Location;
286                         if (layout == null) { 
287                                 Widget w = Widget;
288                         }
289                         layout.Put (e.Control.Widget, l.X, l.Y);
290                         e.Control.LocationChanged += new EventHandler (ControlLocationChanged);
291                 }
292
293                 protected virtual void OnControlRemoved(ControlEventArgs e) {
294                         if (ControlRemoved != null)
295                                 ControlRemoved (this, e);
296                 }
297
298
299                 public Point Location {
300                         get { return location; }
301                         set {
302                                 location = value;
303                                 OnLocationChanged (EventArgs.Empty);
304                         }
305                 }
306
307                 public event EventHandler LocationChanged;
308
309                 public virtual void OnLocationChanged (EventArgs e) {
310                         
311                         if (LocationChanged != null)
312                                 LocationChanged (this, e);
313                 }
314                 
315                 public event EventHandler Click;
316
317                 protected virtual void OnClick (EventArgs e) {
318                         if (Click != null)
319                                 Click (this, e);
320                 }
321                 
322                 public virtual AnchorStyles Anchor {
323                         get { return anchor; }
324                         set { anchor=value; }
325                 }
326                 
327                 [MonoTODO]
328                 protected virtual void OnEnabledChanged(EventArgs e)
329                 {
330                         throw new NotImplementedException();
331                 }
332
333                 [MonoTODO]
334                 protected virtual void OnHandleCreated(EventArgs e)
335                 {
336                         throw new NotImplementedException();
337                 }
338
339                 [MonoTODO]
340                 public virtual Color ForeColor {
341                         get {
342                                 throw new NotImplementedException();
343                         }
344                         set {
345                                 this.widget.ModifyFg (Gtk.StateType.Normal, new Gdk.Color (value));
346                         }
347                 }
348
349                 [MonoTODO]
350                 public virtual System.Drawing.Image BackgroundImage {
351                         get {
352                                 throw new NotImplementedException();
353                         }
354                         set {
355                                 throw new NotImplementedException();
356                         }
357                 }
358
359                 [MonoTODO]
360                 public virtual Color BackColor {
361                         get {
362                                 throw new NotImplementedException();
363                         }
364                         set {
365                                 this.widget.ModifyBg (Gtk.StateType.Normal, new Gdk.Color (value));
366                         }
367                 }
368
369                 public bool TabStop {
370                         get {
371                                 return tabStop;
372                         }
373                         set {
374                                 tabStop = value;
375                         }
376                 }
377
378                 [MonoTODO]
379                 public virtual RightToLeft RightToLeft {
380                         get {
381                                 return rightToLeft;
382                         }
383                         set {
384                                 rightToLeft = value;
385                         }
386                 }
387                 
388                 [MonoTODO]
389                 protected virtual void OnLayout(LayoutEventArgs e)
390                 {
391                         
392                 }
393
394                 [MonoTODO]
395                 protected virtual void OnMouseDown(MouseEventArgs e)
396                 {
397                 }
398
399                 [MonoTODO]
400                 protected virtual void OnResize(EventArgs e)
401                 {
402                 }
403
404                 [MonoTODO]
405                 protected virtual void OnHandleDestroyed(EventArgs e)
406                 {
407                 }
408
409                 [MonoTODO]
410                 public virtual Font Font {
411
412                         get { throw new NotImplementedException(); }
413                         set { throw new NotImplementedException(); }
414                 }
415
416                 protected virtual Size DefaultSize {
417                         get { return new Size ( 100, 100 ); }
418                 }
419         }
420 }