Control.cs 11 KB

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