Control.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. //
  2. // System.Windows.Forms.Form
  3. //
  4. // Author:
  5. // Miguel de Icaza ([email protected])
  6. // Rachel Hestilow ([email protected])
  7. // Joel Basson ([email protected])
  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, name;
  21. Size size;
  22. int left, top, width, height, tabindex, index;
  23. ControlCollection controls;
  24. Point location = new System.Drawing.Point (0, 0);
  25. Gtk.Layout layout = null;
  26. AnchorStyles anchor = AnchorStyles.Top|AnchorStyles.Left;
  27. static int init_me;
  28. public class ControlCollection : IList, ICollection, IEnumerable, ICloneable
  29. {
  30. ArrayList list = new ArrayList ();
  31. Control owner;
  32. public ControlCollection (Control owner)
  33. {
  34. this.owner = owner;
  35. }
  36. private ControlCollection ()
  37. {
  38. }
  39. // ControlCollection
  40. public virtual void Add (Control value) {
  41. list.Add (value);
  42. owner.OnControlAdded (new ControlEventArgs (value));
  43. }
  44. public virtual void AddRange (Control[] controls) {
  45. list.AddRange (controls);
  46. foreach (Control c in controls)
  47. owner.OnControlAdded (new ControlEventArgs (c));
  48. }
  49. public bool Contains (Control value) { return list.Contains (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. int IList.Add (object value) { return list.Add (value); }
  77. public void Clear () { list.Clear (); }
  78. bool IList.Contains (object value) { return list.Contains (value); }
  79. int IList.IndexOf (object value) { return list.IndexOf (value); }
  80. void IList.Insert (int index, object value) { list.Insert (index, value); }
  81. void IList.Remove (object value) { list.Remove (value); }
  82. public void RemoveAt (int index) { list.RemoveAt (index); }
  83. // ICollection
  84. public int Count { get { return list.Count; } }
  85. public bool IsSynchronized { get { return list.IsSynchronized; } }
  86. public object SyncRoot { get { return list.SyncRoot; } }
  87. public void CopyTo (Array array, int index) { list.CopyTo (array, index); }
  88. // IEnumerable
  89. public IEnumerator GetEnumerator () { return list.GetEnumerator (); }
  90. // ICloneable
  91. public object Clone () {
  92. ControlCollection c = new ControlCollection ();
  93. c.list = (ArrayList) list.Clone ();
  94. c.owner = owner;
  95. return c;
  96. }
  97. object IList.this[int index]
  98. {
  99. get { return list[index]; }
  100. set { list[index] = value; }
  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 virtual string Name {
  152. get {
  153. return name;
  154. }
  155. set {
  156. name = value;
  157. Widget.Name = value;
  158. }
  159. }
  160. public bool Enabled {
  161. get {
  162. return Widget.Sensitive;
  163. }
  164. set {
  165. Widget.Sensitive = value;
  166. }
  167. }
  168. public Size Size {
  169. get {
  170. return size;
  171. }
  172. set {
  173. size = value;
  174. Widget.SetSizeRequest (value.Width,value.Height);
  175. }
  176. }
  177. public int TabIndex {
  178. get {
  179. return tabindex;
  180. }
  181. set {
  182. tabindex = value;
  183. }
  184. }
  185. public int Index {
  186. get {
  187. return index;
  188. }
  189. set {
  190. index = value;
  191. }
  192. }
  193. public void Show ()
  194. {
  195. Widget.Show ();
  196. }
  197. public void Hide ()
  198. {
  199. Widget.Hide ();
  200. }
  201. public bool Visible {
  202. get {
  203. return Widget.Visible;
  204. }
  205. set {
  206. Widget.Visible = value;
  207. }
  208. }
  209. public ControlCollection Controls {
  210. get { if (controls == null) controls = new ControlCollection (this); return controls;}
  211. }
  212. public event ControlEventHandler ControlAdded;
  213. public event ControlEventHandler ControlRemoved;
  214. internal void ControlLocationChanged (object o, EventArgs e)
  215. {
  216. Control c = (Control) o;
  217. Point l = c.Location;
  218. if (layout == null) {
  219. Widget w = Widget;
  220. }
  221. layout.Move (c.Widget, l.X, l.Y);
  222. }
  223. protected virtual void OnControlAdded(ControlEventArgs e) {
  224. e.Control.Visible = true;
  225. if (ControlAdded != null)
  226. ControlAdded (this, e);
  227. Point l = e.Control.Location;
  228. if (layout == null) {
  229. Widget w = Widget;
  230. }
  231. layout.Put (e.Control.Widget, l.X, l.Y);
  232. e.Control.LocationChanged += new EventHandler (ControlLocationChanged);
  233. }
  234. protected virtual void OnControlRemoved(ControlEventArgs e) {
  235. if (ControlRemoved != null)
  236. ControlRemoved (this, e);
  237. }
  238. public Point Location {
  239. get { return location; }
  240. set {
  241. location = value;
  242. OnLocationChanged (EventArgs.Empty);
  243. }
  244. }
  245. public event EventHandler LocationChanged;
  246. public virtual void OnLocationChanged (EventArgs e) {
  247. if (LocationChanged != null)
  248. LocationChanged (this, e);
  249. }
  250. public event EventHandler Click;
  251. protected virtual void OnClick (EventArgs e) {
  252. if (Click != null)
  253. Click (this, e);
  254. }
  255. public virtual AnchorStyles Anchor {
  256. get { return anchor; }
  257. set { anchor=value; }
  258. }
  259. }
  260. }