Control.cs 9.2 KB

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