Control.cs 9.1 KB

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