Control.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. 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. 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) : this (text, left, top, width, height)
  165. {
  166. }
  167. public Control (Control parent, string text, int left, int top, int width, int height)
  168. {
  169. this.parent = parent;
  170. this.text = text;
  171. this.left = left;
  172. this.top = top;
  173. this.width = width;
  174. this.height = height;
  175. }
  176. internal Widget Widget {
  177. get {
  178. if (widget == null)
  179. widget = CreateWidget ();
  180. return widget;
  181. }
  182. }
  183. internal virtual Widget CreateWidget ()
  184. {
  185. vbox = new Gtk.VBox(false, 0);
  186. layout = new Gtk.Layout (
  187. new Gtk.Adjustment (0, 0, 1, .1, .1, .1),
  188. new Gtk.Adjustment (0, 0, 1, .1, .1, .1));
  189. vbox.PackStart(layout, true, true, 0);
  190. vbox.ShowAll ();
  191. return vbox;
  192. }
  193. public virtual string Text {
  194. get {
  195. return text;
  196. }
  197. set {
  198. text = value;
  199. OnTextChanged (EventArgs.Empty);
  200. }
  201. }
  202. public event EventHandler TextChanged;
  203. protected virtual void OnTextChanged (EventArgs e) {
  204. if (TextChanged != null)
  205. TextChanged (this, e);
  206. }
  207. public virtual string Name {
  208. get {
  209. return name;
  210. }
  211. set {
  212. name = value;
  213. Widget.Name = value;
  214. }
  215. }
  216. public bool Enabled {
  217. get {
  218. return Widget.Sensitive;
  219. }
  220. set {
  221. Widget.Sensitive = value;
  222. }
  223. }
  224. public Size Size {
  225. get {
  226. return size;
  227. }
  228. set {
  229. size = value;
  230. Widget.SetSizeRequest (value.Width,value.Height);
  231. }
  232. }
  233. public int Left {
  234. get{
  235. return location.X;
  236. }
  237. set{
  238. location.X = value;
  239. OnLocationChanged (EventArgs.Empty);
  240. }
  241. }
  242. public int Top {
  243. get{
  244. return location.Y;
  245. }
  246. set{
  247. top = value;
  248. location.Y = value;
  249. OnLocationChanged (EventArgs.Empty);
  250. }
  251. }
  252. public int Tag {
  253. get{
  254. return tag;
  255. }
  256. set{
  257. tag = value;
  258. }
  259. }
  260. public int TabIndex {
  261. get {
  262. return tabindex;
  263. }
  264. set {
  265. tabindex = value;
  266. }
  267. }
  268. public int Index {
  269. get {
  270. return index;
  271. }
  272. set {
  273. index = value;
  274. }
  275. }
  276. public void Show ()
  277. {
  278. Widget.Show ();
  279. }
  280. public void Hide ()
  281. {
  282. Widget.Hide ();
  283. }
  284. public bool Visible {
  285. get {
  286. return Widget.Visible;
  287. }
  288. set {
  289. Widget.Visible = value;
  290. }
  291. }
  292. public ControlCollection Controls {
  293. get { if (controls == null) controls = new ControlCollection (this); return controls;}
  294. }
  295. public event ControlEventHandler ControlAdded;
  296. public event ControlEventHandler ControlRemoved;
  297. internal void ControlLocationChanged (object o, EventArgs e)
  298. {
  299. Control c = (Control) o;
  300. Point l = c.Location;
  301. if (layout == null) {
  302. Widget w = Widget;
  303. }
  304. layout.Move (c.Widget, l.X, l.Y);
  305. }
  306. protected virtual void OnControlAdded(ControlEventArgs e) {
  307. e.Control.Visible = true;
  308. if (ControlAdded != null)
  309. ControlAdded (this, e);
  310. Point l = e.Control.Location;
  311. if (layout == null) {
  312. Widget w = Widget;
  313. }
  314. layout.Put (e.Control.Widget, l.X, l.Y);
  315. e.Control.LocationChanged += new EventHandler (ControlLocationChanged);
  316. }
  317. protected virtual void OnControlRemoved(ControlEventArgs e) {
  318. if (ControlRemoved != null)
  319. ControlRemoved (this, e);
  320. }
  321. public Point Location {
  322. get { return location; }
  323. set {
  324. location = value;
  325. OnLocationChanged (EventArgs.Empty);
  326. }
  327. }
  328. public event EventHandler LocationChanged;
  329. public virtual void OnLocationChanged (EventArgs e) {
  330. if (LocationChanged != null)
  331. LocationChanged (this, e);
  332. }
  333. public event EventHandler Click;
  334. protected virtual void OnClick (EventArgs e) {
  335. if (Click != null)
  336. Click (this, e);
  337. }
  338. public virtual AnchorStyles Anchor {
  339. get { return anchor; }
  340. set { anchor=value; }
  341. }
  342. [MonoTODO]
  343. protected virtual void OnEnabledChanged(EventArgs e)
  344. {
  345. throw new NotImplementedException();
  346. }
  347. [MonoTODO]
  348. protected virtual void OnHandleCreated(EventArgs e)
  349. {
  350. throw new NotImplementedException();
  351. }
  352. [MonoTODO]
  353. public virtual Color ForeColor {
  354. get {
  355. throw new NotImplementedException();
  356. }
  357. set {
  358. this.widget.ModifyFg (Gtk.StateType.Normal, new Gdk.Color (value));
  359. }
  360. }
  361. [MonoTODO]
  362. public virtual System.Drawing.Image BackgroundImage {
  363. get {
  364. throw new NotImplementedException();
  365. }
  366. set {
  367. throw new NotImplementedException();
  368. }
  369. }
  370. [MonoTODO]
  371. public virtual Color BackColor {
  372. get {
  373. throw new NotImplementedException();
  374. }
  375. set {
  376. this.widget.ModifyBg (Gtk.StateType.Normal, new Gdk.Color (value));
  377. }
  378. }
  379. public bool TabStop {
  380. get {
  381. return tabStop;
  382. }
  383. set {
  384. tabStop = value;
  385. }
  386. }
  387. [MonoTODO]
  388. public virtual RightToLeft RightToLeft {
  389. get {
  390. return rightToLeft;
  391. }
  392. set {
  393. rightToLeft = value;
  394. }
  395. }
  396. [MonoTODO]
  397. protected virtual void OnLayout(LayoutEventArgs e)
  398. {
  399. }
  400. [MonoTODO]
  401. protected virtual void OnMouseDown(MouseEventArgs e)
  402. {
  403. }
  404. [MonoTODO]
  405. protected virtual void OnResize(EventArgs e)
  406. {
  407. }
  408. [MonoTODO]
  409. protected virtual void OnHandleDestroyed(EventArgs e)
  410. {
  411. }
  412. [MonoTODO]
  413. public virtual Font Font {
  414. get { throw new NotImplementedException(); }
  415. set { throw new NotImplementedException(); }
  416. }
  417. protected virtual Size DefaultSize {
  418. get { return new Size ( 100, 100 ); }
  419. }
  420. public event EventHandler BindingContextChanged;
  421. protected virtual void OnBindingContextChanged (EventArgs e)
  422. {
  423. if (BindingContextChanged != null)
  424. BindingContextChanged (this, e);
  425. }
  426. }
  427. }