Control.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. if (this.owner.GetType() == typeof (System.Windows.Forms.Button))
  47. { // This makes Gtk throw a warning about a label already being added
  48. // This is actually support that Gtk just does not have :(
  49. // Should we reinvent the Button-object for that? (Inherit from Gtk.Container
  50. // and implement our own OnClicked handlers and stuff like that)
  51. // Or .. we remove the label and replace it with a Container which
  52. // we fill with the added Controls .. (question: how do I remove the
  53. // label and/or get the Gtk.Widget inside the button ?)
  54. Gtk.Button gtkbutton = (Gtk.Button)this.owner.widget;
  55. gtkbutton.Add (value.widget);
  56. list.Add (value);
  57. }
  58. else if (value.GetType() == typeof(System.Windows.Forms.StatusBar))
  59. {
  60. // SWF on Windows adds above the last added StatusBar
  61. // I think that this adds below the last one ..
  62. // So a reorderchild operation might be required here..
  63. this.owner.vbox.PackEnd(value.widget, false, false, 0);
  64. // this.vbox.ReorderChild (value.widget, 0);
  65. this.owner.vbox.ShowAll();
  66. list.Add (value);
  67. }
  68. else if (value.GetType() == typeof(System.Windows.Forms.MainMenu))
  69. {
  70. MainMenu m = (MainMenu)value;
  71. this.owner.vbox.PackStart(m.mb, false, false, 0);
  72. m.mb.ShowAll();
  73. this.owner.vbox.ReorderChild (m.mb, 0);
  74. this.owner.vbox.ShowAll();
  75. list.Add (value);
  76. }
  77. // TODO System.Windows.Forms.ToolBar
  78. // But we don't have this type yet :-)
  79. else {
  80. list.Add (value);
  81. owner.OnControlAdded (new ControlEventArgs (value));
  82. }
  83. }
  84. public virtual void AddRange (Control[] controls) {
  85. // Because we really do have to check for a few
  86. // special cases we cannot use the AddRange and
  87. // will have to check each Control that we add
  88. // list.AddRange (controls);
  89. foreach (Control c in controls)
  90. this.Add (c);
  91. // owner.OnControlAdded (new ControlEventArgs (c));
  92. }
  93. public bool Contains (Control value) { return list.Contains (value); }
  94. public virtual void Remove (Control value) {
  95. list.Remove (value);
  96. owner.OnControlAdded (new ControlEventArgs (value));
  97. }
  98. public virtual Control this[int index] { get { return (Control) list[index]; } }
  99. public int GetChildIndex (Control child) {
  100. return GetChildIndex (child, true);
  101. }
  102. public int GetChildIndex (Control child, bool throwException) {
  103. if (throwException && !Contains (child))
  104. throw new Exception ();
  105. return list.IndexOf (child);
  106. }
  107. public int IndexOf (Control value) { return list.IndexOf (value); }
  108. public void SetChildIndex (Control child, int newIndex) {
  109. int oldIndex = GetChildIndex (child);
  110. if (oldIndex == newIndex)
  111. return;
  112. // is this correct behavior?
  113. Control other = (Control) list[newIndex];
  114. list[oldIndex] = other;
  115. list[newIndex] = child;
  116. }
  117. // IList
  118. public bool IsFixedSize { get { return list.IsFixedSize; } }
  119. public bool IsReadOnly { get { return list.IsReadOnly; } }
  120. int IList.Add (object value) { return list.Add (value); }
  121. public void Clear () { list.Clear (); }
  122. bool IList.Contains (object value) { return list.Contains (value); }
  123. int IList.IndexOf (object value) { return list.IndexOf (value); }
  124. void IList.Insert (int index, object value) { list.Insert (index, value); }
  125. void IList.Remove (object value) { list.Remove (value); }
  126. public void RemoveAt (int index) { list.RemoveAt (index); }
  127. // ICollection
  128. public int Count { get { return list.Count; } }
  129. public bool IsSynchronized { get { return list.IsSynchronized; } }
  130. public object SyncRoot { get { return list.SyncRoot; } }
  131. public void CopyTo (Array array, int index) { list.CopyTo (array, index); }
  132. // IEnumerable
  133. public IEnumerator GetEnumerator () { return list.GetEnumerator (); }
  134. // ICloneable
  135. public object Clone () {
  136. ControlCollection c = new ControlCollection ();
  137. c.list = (ArrayList) list.Clone ();
  138. c.owner = owner;
  139. return c;
  140. }
  141. object IList.this[int index]
  142. {
  143. get { return list[index]; }
  144. set { list[index] = value; }
  145. }
  146. }
  147. static Control ()
  148. {
  149. Gtk.Application.Init ();
  150. init_me = 1;
  151. }
  152. public Control () : this ("")
  153. {
  154. this.text = "";
  155. }
  156. public Control (string text) : this (null, text)
  157. {
  158. }
  159. public Control (Control parent, string text)
  160. {
  161. this.parent = parent;
  162. this.text = text;
  163. }
  164. public Control (string text, int left, int top, int width, int height)
  165. {
  166. }
  167. public Control (Control parent, string text, int left, int top, int width, int height)
  168. {
  169. }
  170. internal Widget Widget {
  171. get {
  172. if (widget == null)
  173. widget = CreateWidget ();
  174. return widget;
  175. }
  176. }
  177. internal virtual Widget CreateWidget ()
  178. {
  179. vbox = new Gtk.VBox(false, 0);
  180. layout = new Gtk.Layout (new Gtk.Adjustment (IntPtr.Zero), new Gtk.Adjustment (IntPtr.Zero));
  181. vbox.PackStart(layout, true, true, 0);
  182. vbox.ShowAll ();
  183. return vbox;
  184. }
  185. public virtual string Text {
  186. get {
  187. return text;
  188. }
  189. set {
  190. text = value;
  191. OnTextChanged (EventArgs.Empty);
  192. }
  193. }
  194. public event EventHandler TextChanged;
  195. protected virtual void OnTextChanged (EventArgs e) {
  196. if (TextChanged != null)
  197. TextChanged (this, e);
  198. }
  199. public virtual string Name {
  200. get {
  201. return name;
  202. }
  203. set {
  204. name = value;
  205. Widget.Name = value;
  206. }
  207. }
  208. public bool Enabled {
  209. get {
  210. return Widget.Sensitive;
  211. }
  212. set {
  213. Widget.Sensitive = value;
  214. }
  215. }
  216. public Size Size {
  217. get {
  218. return size;
  219. }
  220. set {
  221. size = value;
  222. Widget.SetSizeRequest (value.Width,value.Height);
  223. }
  224. }
  225. public int TabIndex {
  226. get {
  227. return tabindex;
  228. }
  229. set {
  230. tabindex = value;
  231. }
  232. }
  233. public int Index {
  234. get {
  235. return index;
  236. }
  237. set {
  238. index = value;
  239. }
  240. }
  241. public void Show ()
  242. {
  243. Widget.Show ();
  244. }
  245. public void Hide ()
  246. {
  247. Widget.Hide ();
  248. }
  249. public bool Visible {
  250. get {
  251. return Widget.Visible;
  252. }
  253. set {
  254. Widget.Visible = value;
  255. }
  256. }
  257. public ControlCollection Controls {
  258. get { if (controls == null) controls = new ControlCollection (this); return controls;}
  259. }
  260. public event ControlEventHandler ControlAdded;
  261. public event ControlEventHandler ControlRemoved;
  262. internal void ControlLocationChanged (object o, EventArgs e)
  263. {
  264. Control c = (Control) o;
  265. Point l = c.Location;
  266. if (layout == null) {
  267. Widget w = Widget;
  268. }
  269. layout.Move (c.Widget, l.X, l.Y);
  270. }
  271. protected virtual void OnControlAdded(ControlEventArgs e) {
  272. e.Control.Visible = true;
  273. if (ControlAdded != null)
  274. ControlAdded (this, e);
  275. Point l = e.Control.Location;
  276. if (layout == null) {
  277. Widget w = Widget;
  278. }
  279. layout.Put (e.Control.Widget, l.X, l.Y);
  280. e.Control.LocationChanged += new EventHandler (ControlLocationChanged);
  281. }
  282. protected virtual void OnControlRemoved(ControlEventArgs e) {
  283. if (ControlRemoved != null)
  284. ControlRemoved (this, e);
  285. }
  286. public Point Location {
  287. get { return location; }
  288. set {
  289. location = value;
  290. OnLocationChanged (EventArgs.Empty);
  291. }
  292. }
  293. public event EventHandler LocationChanged;
  294. public virtual void OnLocationChanged (EventArgs e) {
  295. if (LocationChanged != null)
  296. LocationChanged (this, e);
  297. }
  298. public event EventHandler Click;
  299. protected virtual void OnClick (EventArgs e) {
  300. if (Click != null)
  301. Click (this, e);
  302. }
  303. public virtual AnchorStyles Anchor {
  304. get { return anchor; }
  305. set { anchor=value; }
  306. }
  307. [MonoTODO]
  308. protected virtual void OnEnabledChanged(EventArgs e)
  309. {
  310. throw new NotImplementedException();
  311. }
  312. [MonoTODO]
  313. protected virtual void OnHandleCreated(EventArgs e)
  314. {
  315. throw new NotImplementedException();
  316. }
  317. [MonoTODO]
  318. public virtual Color ForeColor {
  319. get {
  320. throw new NotImplementedException();
  321. }
  322. set {
  323. this.widget.ModifyFg (Gtk.StateType.Normal, new Gdk.Color (value));
  324. }
  325. }
  326. [MonoTODO]
  327. public virtual System.Drawing.Image BackgroundImage {
  328. get {
  329. throw new NotImplementedException();
  330. }
  331. set {
  332. throw new NotImplementedException();
  333. }
  334. }
  335. [MonoTODO]
  336. public virtual Color BackColor {
  337. get {
  338. throw new NotImplementedException();
  339. }
  340. set {
  341. this.widget.ModifyBg (Gtk.StateType.Normal, new Gdk.Color (value));
  342. }
  343. }
  344. public bool TabStop {
  345. get {
  346. return tabStop;
  347. }
  348. set {
  349. tabStop = value;
  350. }
  351. }
  352. [MonoTODO]
  353. public virtual RightToLeft RightToLeft {
  354. get {
  355. return rightToLeft;
  356. }
  357. set {
  358. rightToLeft = value;
  359. }
  360. }
  361. [MonoTODO]
  362. protected virtual void OnLayout(LayoutEventArgs e)
  363. {
  364. }
  365. [MonoTODO]
  366. protected virtual void OnMouseDown(MouseEventArgs e)
  367. {
  368. }
  369. [MonoTODO]
  370. protected virtual void OnResize(EventArgs e)
  371. {
  372. }
  373. [MonoTODO]
  374. protected virtual void OnHandleDestroyed(EventArgs e)
  375. {
  376. }
  377. [MonoTODO]
  378. public virtual Font Font {
  379. get { throw new NotImplementedException(); }
  380. set { throw new NotImplementedException(); }
  381. }
  382. protected virtual Size DefaultSize {
  383. get { return new Size ( 100, 100 ); }
  384. }
  385. public event EventHandler BindingContextChanged;
  386. protected virtual void OnBindingContextChanged (EventArgs e)
  387. {
  388. if (BindingContextChanged != null)
  389. BindingContextChanged (this, e);
  390. }
  391. }
  392. }