DockObject.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2004 Todd Berman <tberman@off.net>
  5. * Copyright (C) 2004 Jeroen Zwartepoorte <jeroen@xs4all.nl>
  6. * Copyright (C) 2005 John Luke <john.luke@gmail.com>
  7. *
  8. * based on work by:
  9. * Copyright (C) 2002 Gustavo Giráldez <gustavo.giraldez@gmx.net>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place - Suite 330,
  24. * Boston, MA 02111-1307, USA.
  25. */
  26. using System;
  27. using System.Collections;
  28. using System.Reflection;
  29. using System.Xml;
  30. using Gtk;
  31. namespace Gdl
  32. {
  33. public delegate void PropertyChangedHandler (object o, string name);
  34. public class DockObject : Container
  35. {
  36. private DockObjectFlags flags = DockObjectFlags.Automatic;
  37. private int freezeCount = 0;
  38. private DockMaster master;
  39. private string name;
  40. private string longName;
  41. private string stockid;
  42. private bool reducePending;
  43. private PropertyInfo[] publicProps;
  44. Hashtable afterProps;
  45. Hashtable beforeProps;
  46. public event DetachedHandler Detached;
  47. public event DockedHandler Docked;
  48. public event PropertyChangedHandler PropertyChanged;
  49. protected DockObject (IntPtr raw) : base (raw) { }
  50. protected DockObject () : base () { }
  51. public DockObjectFlags DockObjectFlags {
  52. get {
  53. return flags;
  54. }
  55. set {
  56. flags = value;
  57. EmitPropertyEvent ("DockObjectFlags");
  58. }
  59. }
  60. public bool InDetach {
  61. get {
  62. return ((flags & DockObjectFlags.InDetach) != 0);
  63. }
  64. }
  65. public bool InReflow {
  66. get {
  67. return ((flags & DockObjectFlags.InReflow) != 0);
  68. }
  69. }
  70. public bool IsAttached {
  71. get {
  72. return ((flags & DockObjectFlags.Attached) != 0);
  73. }
  74. }
  75. public bool IsAutomatic {
  76. get {
  77. return ((flags & DockObjectFlags.Automatic) != 0);
  78. }
  79. }
  80. public bool IsBound {
  81. get {
  82. return master != null;
  83. }
  84. }
  85. public virtual bool IsCompound {
  86. get {
  87. return true;
  88. }
  89. }
  90. public bool IsFrozen {
  91. get {
  92. return freezeCount > 0;
  93. }
  94. }
  95. public string LongName {
  96. get {
  97. return longName;
  98. }
  99. set {
  100. longName = value;
  101. EmitPropertyEvent ("LongName");
  102. }
  103. }
  104. public DockMaster Master {
  105. get {
  106. return master;
  107. }
  108. set {
  109. if (value != null)
  110. Bind (master);
  111. else
  112. Unbind ();
  113. EmitPropertyEvent ("Master");
  114. }
  115. }
  116. [Export]
  117. public new string Name {
  118. get {
  119. return name;
  120. }
  121. set {
  122. name = value;
  123. EmitPropertyEvent ("Name");
  124. }
  125. }
  126. public DockObject ParentObject {
  127. get {
  128. Widget parent = Parent;
  129. while (parent != null && !(parent is DockObject)) {
  130. parent = parent.Parent;
  131. }
  132. return parent != null ? (DockObject)parent : null;
  133. }
  134. }
  135. public string StockId {
  136. get {
  137. return stockid;
  138. }
  139. set {
  140. stockid = value;
  141. EmitPropertyEvent ("StockId");
  142. }
  143. }
  144. private PropertyInfo[] PublicProps {
  145. get {
  146. if (publicProps == null)
  147. publicProps = this.GetType ().GetProperties (BindingFlags.Public | BindingFlags.Instance);
  148. return publicProps;
  149. }
  150. }
  151. void SetPropertyValue (string property, string val, bool after)
  152. {
  153. if (afterProps == null) {
  154. afterProps = new Hashtable ();
  155. beforeProps = new Hashtable ();
  156. foreach (PropertyInfo pp in PublicProps) {
  157. if (pp.IsDefined (typeof (AfterAttribute), true))
  158. afterProps [pp.Name.ToLower ()] = pp;
  159. else
  160. beforeProps [pp.Name.ToLower ()] = pp;
  161. }
  162. }
  163. property = property.ToLower ();
  164. PropertyInfo p = after ? (PropertyInfo) afterProps [property] : (PropertyInfo) beforeProps [property];
  165. if (p != null)
  166. SetPropertyValue (p, property, val);
  167. }
  168. void SetPropertyValue (PropertyInfo pi, string property, string val)
  169. {
  170. if (pi.PropertyType.IsEnum)
  171. pi.SetValue (this, Enum.Parse (pi.PropertyType, val, true), null);
  172. else if (pi.PropertyType == typeof (bool))
  173. pi.SetValue (this, val == "no" ? false : true, null);
  174. else if (pi.PropertyType == typeof (int))
  175. pi.SetValue (this, int.Parse (val), null);
  176. else
  177. pi.SetValue (this, val, null);
  178. }
  179. public void FromXml (XmlNode node)
  180. {
  181. foreach (XmlAttribute att in node.Attributes)
  182. SetPropertyValue (att.Name, att.Value, false);
  183. }
  184. public void FromXmlAfter (XmlNode node)
  185. {
  186. foreach (XmlAttribute att in node.Attributes)
  187. SetPropertyValue (att.Name, att.Value, true);
  188. }
  189. static string GetXmlName (Type t)
  190. {
  191. switch (t.ToString ()) {
  192. case "Gdl.Dock":
  193. return "dock";
  194. case "Gdl.DockItem":
  195. return "item";
  196. case "Gdl.DockNotebook":
  197. return "notebook";
  198. case "Gdl.DockPaned":
  199. return "paned";
  200. default:
  201. return "object";
  202. }
  203. }
  204. public XmlElement ToXml (XmlDocument doc)
  205. {
  206. Type t = this.GetType ();
  207. XmlElement element = doc.CreateElement (GetXmlName (t));
  208. // get object exported attributes
  209. ArrayList exported = new ArrayList ();
  210. foreach (PropertyInfo p in PublicProps) {
  211. if (p.IsDefined (typeof (ExportAttribute), true))
  212. exported.Add (p);
  213. }
  214. foreach (PropertyInfo p in exported) {
  215. if (p.PropertyType.IsSubclassOf (typeof (System.Enum)))
  216. element.SetAttribute (p.Name.ToLower (), p.GetValue (this, null).ToString ().ToLower ());
  217. else if (p.PropertyType == typeof (bool))
  218. element.SetAttribute (p.Name.ToLower (), ((bool) p.GetValue (this, null)) ? "yes" : "no");
  219. else if (p.GetValue (this, null) != null)
  220. element.SetAttribute (p.Name.ToLower (), p.GetValue (this, null).ToString ());
  221. }
  222. return element;
  223. }
  224. protected override void OnDestroyed ()
  225. {
  226. if (IsCompound) {
  227. /* detach our dock object children if we have some, and even
  228. if we are not attached, so they can get notification */
  229. Freeze ();
  230. foreach (DockObject child in Children)
  231. child.Detach (true);
  232. reducePending = false;
  233. Thaw ();
  234. }
  235. if (IsAttached)
  236. /* detach ourselves */
  237. Detach (false);
  238. if (Master != null)
  239. /* finally unbind us */
  240. Unbind ();
  241. base.OnDestroyed ();
  242. }
  243. protected override void OnShown ()
  244. {
  245. if (IsCompound)
  246. foreach (Widget child in Children)
  247. child.Show ();
  248. base.OnShown ();
  249. }
  250. protected override void OnHidden ()
  251. {
  252. if (IsCompound)
  253. foreach (Widget child in Children)
  254. child.Hide ();
  255. base.OnHidden ();
  256. }
  257. public virtual void OnDetached (bool recursive)
  258. {
  259. /* detach children */
  260. if (recursive && IsCompound) {
  261. foreach (DockObject child in Children) {
  262. child.Detach (recursive);
  263. }
  264. }
  265. /* detach the object itself */
  266. flags &= ~(DockObjectFlags.Attached);
  267. DockObject parent = ParentObject;
  268. if (Parent != null && Parent is Container)
  269. ((Container)Parent).Remove (this);
  270. if (parent != null)
  271. parent.Reduce ();
  272. }
  273. public virtual void OnReduce ()
  274. {
  275. if (!IsCompound)
  276. return;
  277. DockObject parent = ParentObject;
  278. Widget[] children = Children;
  279. if (children.Length <= 1) {
  280. if (parent != null)
  281. parent.Freeze ();
  282. Freeze ();
  283. Detach (false);
  284. foreach (Widget widget in children) {
  285. DockObject child = widget as DockObject;
  286. child.flags |= DockObjectFlags.InReflow;
  287. child.Detach (false);
  288. if (parent != null)
  289. parent.Add (child);
  290. child.flags &= ~(DockObjectFlags.InReflow);
  291. }
  292. reducePending = false;
  293. Thaw ();
  294. if (parent != null)
  295. parent.Thaw ();
  296. }
  297. }
  298. public virtual bool OnDockRequest (int x, int y, ref DockRequest request)
  299. {
  300. return false;
  301. }
  302. public virtual void OnDocked (DockObject requestor, DockPlacement position, object data)
  303. {
  304. }
  305. public virtual bool OnReorder (DockObject child, DockPlacement new_position, object data)
  306. {
  307. return false;
  308. }
  309. public virtual void OnPresent (DockObject child)
  310. {
  311. Show ();
  312. }
  313. public virtual bool OnChildPlacement (DockObject child, ref DockPlacement placement)
  314. {
  315. return false;
  316. }
  317. public bool ChildPlacement (DockObject child, ref DockPlacement placement)
  318. {
  319. if (!IsCompound)
  320. return false;
  321. return OnChildPlacement (child, ref placement);
  322. }
  323. public void Detach (bool recursive)
  324. {
  325. if (!IsAttached)
  326. return;
  327. /* freeze the object to avoid reducing while detaching children */
  328. Freeze ();
  329. DockObjectFlags |= DockObjectFlags.InDetach;
  330. OnDetached (recursive);
  331. DetachedHandler handler = Detached;
  332. if (handler != null)
  333. handler (this, new DetachedArgs (recursive));
  334. DockObjectFlags &= ~(DockObjectFlags.InDetach);
  335. Thaw ();
  336. }
  337. public void Dock (DockObject requestor, DockPlacement position, object data)
  338. {
  339. if (requestor == null || requestor == this)
  340. return;
  341. if (master == null) {
  342. Console.WriteLine ("Dock operation requested in a non-bound object {0}.", this);
  343. Console.WriteLine ("This might break.");
  344. }
  345. if (!requestor.IsBound)
  346. requestor.Bind (Master);
  347. if (requestor.Master != Master) {
  348. Console.WriteLine ("Cannot dock {0} to {1} as they belong to different masters.",
  349. requestor, this);
  350. return;
  351. }
  352. /* first, see if we can optimize things by reordering */
  353. if (position != DockPlacement.None) {
  354. DockObject parent = ParentObject;
  355. if (OnReorder (requestor, position, data) ||
  356. (parent != null && parent.OnReorder (requestor, position, data)))
  357. return;
  358. }
  359. /* freeze the object, since under some conditions it might
  360. be destroyed when detaching the requestor */
  361. Freeze ();
  362. /* detach the requestor before docking */
  363. if (requestor.IsAttached)
  364. requestor.Detach (false);
  365. /* notify interested parties that an object has been docked. */
  366. if (position != DockPlacement.None) {
  367. OnDocked (requestor, position, data);
  368. DockedHandler handler = Docked;
  369. if (handler != null) {
  370. DockedArgs args = new DockedArgs (requestor, position);
  371. handler (this, args);
  372. }
  373. }
  374. Thaw ();
  375. }
  376. public void Present (DockObject child)
  377. {
  378. if (ParentObject != null)
  379. /* chain the call to our parent */
  380. ParentObject.Present (this);
  381. OnPresent (child);
  382. }
  383. public void Reduce ()
  384. {
  385. if (IsFrozen) {
  386. reducePending = true;
  387. return;
  388. }
  389. OnReduce ();
  390. }
  391. public void Freeze ()
  392. {
  393. freezeCount++;
  394. }
  395. public void Thaw ()
  396. {
  397. if (freezeCount < 0) {
  398. Console.WriteLine ("DockObject.Thaw: freezeCount < 0");
  399. return;
  400. }
  401. freezeCount--;
  402. if (freezeCount == 0 && reducePending) {
  403. reducePending = false;
  404. Reduce ();
  405. }
  406. }
  407. public void Bind (DockMaster master)
  408. {
  409. if (master == null) {
  410. Console.WriteLine ("Passed master is null");
  411. Console.WriteLine (System.Environment.StackTrace);
  412. return;
  413. }
  414. if (this.master == master) {
  415. Console.WriteLine ("Passed master is this master");
  416. return;
  417. }
  418. if (this.master != null) {
  419. Console.WriteLine ("Attempt to bind an already bound object");
  420. return;
  421. }
  422. master.Add (this);
  423. this.master = master;
  424. EmitPropertyEvent ("Master");
  425. }
  426. public void Unbind ()
  427. {
  428. if (IsAttached)
  429. Detach (true);
  430. if (master != null) {
  431. DockMaster _master = master;
  432. master = null;
  433. _master.Remove (this);
  434. EmitPropertyEvent ("Master");
  435. }
  436. }
  437. protected void EmitPropertyEvent (string name)
  438. {
  439. // Make a local assignment of the handler here to prevent
  440. // any race conditions if the PropertyChanged value changes
  441. // to null after the != null check.
  442. PropertyChangedHandler handler = PropertyChanged;
  443. if (handler != null)
  444. handler (this, name);
  445. }
  446. }
  447. }