Control.cs 6.2 KB

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