DockMaster.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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 Gtk;
  29. namespace Gdl
  30. {
  31. public class DockMaster
  32. {
  33. private Hashtable dockObjects = new Hashtable ();
  34. private ArrayList toplevelDocks = new ArrayList ();
  35. private DockObject controller = null;
  36. private int dockNumber = 1;
  37. // for naming nameless manual objects
  38. private int number = 1;
  39. private string defaultTitle;
  40. private Gdk.GC rootXorGC;
  41. private bool rectDrawn;
  42. private Dock rectOwner;
  43. private DockRequest request;
  44. // hashes to quickly calculate the overall locked status:
  45. // if size(unlocked_items) == 0 then locked = 1
  46. // else if size(locked_items) == 0 then locked = 0
  47. // else locked = -1
  48. private Hashtable lockedItems = new Hashtable ();
  49. private Hashtable unlockedItems = new Hashtable ();
  50. public event EventHandler LayoutChanged;
  51. internal event EventHandler NotifyLocked;
  52. public DockMaster ()
  53. {
  54. }
  55. public string DefaultTitle {
  56. get {
  57. return defaultTitle;
  58. }
  59. set {
  60. defaultTitle = value;
  61. }
  62. }
  63. public int DockNumber {
  64. get {
  65. return dockNumber;
  66. }
  67. set {
  68. dockNumber = value;
  69. }
  70. }
  71. public ICollection DockObjects {
  72. get {
  73. return dockObjects.Values;
  74. }
  75. }
  76. // 1 = all the dock items bound to the master are locked
  77. // 0 = all the dock items bound to the master are unlocked
  78. // -1 = inconsistent
  79. public int Locked {
  80. get {
  81. return ComputeLocked ();
  82. }
  83. set {
  84. if (value >= 0)
  85. LockUnlock (value > 0);
  86. EmitNotifyLocked ();
  87. }
  88. }
  89. public ArrayList TopLevelDocks {
  90. get {
  91. return toplevelDocks;
  92. }
  93. }
  94. protected void ForeachLockUnlock (DockItem item, bool locked)
  95. {
  96. item.Locked = locked;
  97. if (item.IsCompound) {
  98. foreach (Widget w in item.Children) {
  99. DockItem i = w as DockItem;
  100. if (i != null)
  101. ForeachLockUnlock (i, locked);
  102. }
  103. }
  104. }
  105. public void LockUnlock (bool locked)
  106. {
  107. foreach (Dock dock in toplevelDocks) {
  108. if (dock.Root != null && dock.Root is DockItem)
  109. ForeachLockUnlock ((DockItem)dock.Root, locked);
  110. }
  111. // FIXME: not sure about which list to foreach here
  112. // just to be sure hidden items are set too
  113. foreach (Widget w in toplevelDocks) {
  114. DockItem i = w as DockItem;
  115. if (i != null)
  116. ForeachLockUnlock (i, locked);
  117. }
  118. }
  119. public void Add (DockObject obj)
  120. {
  121. if (obj == null)
  122. return;
  123. if (!obj.IsAutomatic) {
  124. /* create a name for the object if it doesn't have one */
  125. if (obj.Name == null)
  126. obj.Name = "__dock_" + number++;
  127. /* add the object to our hash list */
  128. if (dockObjects.Contains (obj.Name))
  129. Console.WriteLine ("Unable to add object, name \"{0}\" taken", obj.Name);
  130. else
  131. dockObjects.Add (obj.Name, obj);
  132. }
  133. if (obj is Dock) {
  134. /* if this is the first toplevel we are adding, name it controller */
  135. if (toplevelDocks.Count == 0)
  136. controller = obj;
  137. /* add dock to the toplevel list */
  138. if (((Dock)obj).Floating)
  139. toplevelDocks.Insert (0, obj);
  140. else
  141. toplevelDocks.Add (obj);
  142. /* we are interested in the dock request this toplevel
  143. * receives to update the layout */
  144. obj.Docked += new DockedHandler (OnItemDocked);
  145. } else if (obj is DockItem) {
  146. DockItem item = obj as DockItem;
  147. /* we need to connect the item's events */
  148. item.Detached += new DetachedHandler (OnItemDetached);
  149. item.Docked += new DockedHandler (OnItemDocked);
  150. item.DockItemDragBegin += new DockItemDragBeginHandler (OnDragBegin);
  151. item.DockItemMotion += new DockItemMotionHandler (OnDragMotion);
  152. item.DockItemDragEnd += new DockItemDragEndHandler (OnDragEnd);
  153. /* register to "locked" notification if the item has a grip,
  154. * and add the item to the corresponding hash */
  155. item.PropertyChanged += new PropertyChangedHandler (OnItemPropertyChanged);
  156. /* post a layout_changed emission if the item is not automatic
  157. * (since it should be added to the items model) */
  158. if (!item.IsAutomatic) {
  159. EmitLayoutChangedEvent ();
  160. }
  161. }
  162. }
  163. public void Remove (DockObject obj)
  164. {
  165. if (obj == null)
  166. return;
  167. // remove from locked/unlocked hashes and property change if that's the case
  168. if (obj is DockItem && ((DockItem)obj).HasGrip) {
  169. int locked = Locked;
  170. if (lockedItems.Contains (obj)) {
  171. lockedItems.Remove (obj);
  172. if (Locked != locked)
  173. EmitNotifyLocked ();
  174. }
  175. if (unlockedItems.Contains (obj)) {
  176. unlockedItems.Remove (obj);
  177. if (Locked != locked)
  178. EmitNotifyLocked ();
  179. }
  180. }
  181. if (obj is Dock) {
  182. toplevelDocks.Remove (obj);
  183. obj.Docked -= new DockedHandler (OnItemDocked);
  184. if (obj == controller) {
  185. DockObject newController = null;
  186. // now find some other non-automatic toplevel to use as a
  187. // new controller. start from the last dock, since it's
  188. // probably a non-floating and manual
  189. ArrayList reversed = toplevelDocks;
  190. reversed.Reverse ();
  191. foreach (DockObject item in reversed) {
  192. if (!item.IsAutomatic) {
  193. newController = item;
  194. break;
  195. }
  196. }
  197. if (newController != null) {
  198. controller = newController;
  199. } else {
  200. // no controller, no master
  201. controller = null;
  202. }
  203. }
  204. }
  205. // disconnect the signals
  206. if (obj is DockItem) {
  207. DockItem item = obj as DockItem;
  208. item.Detached -= new DetachedHandler (OnItemDetached);
  209. item.Docked -= new DockedHandler (OnItemDocked);
  210. item.DockItemDragBegin -= new DockItemDragBeginHandler (OnDragBegin);
  211. item.DockItemMotion -= new DockItemMotionHandler (OnDragMotion);
  212. item.DockItemDragEnd -= new DockItemDragEndHandler (OnDragEnd);
  213. item.PropertyChanged -= new PropertyChangedHandler (OnItemPropertyChanged);
  214. }
  215. // remove the object from the hash if it is there
  216. if (obj.Name != null && dockObjects.Contains (obj.Name))
  217. dockObjects.Remove (obj.Name);
  218. /* post a layout_changed emission if the item is not automatic
  219. * (since it should be removed from the items model) */
  220. if (!obj.IsAutomatic)
  221. EmitLayoutChangedEvent ();
  222. }
  223. public DockObject GetObject (string name)
  224. {
  225. if (name == null)
  226. return null;
  227. return (DockObject)dockObjects[name];
  228. }
  229. public DockObject Controller {
  230. get { return controller; }
  231. set {
  232. if (value != null) {
  233. if (value.IsAutomatic)
  234. Console.WriteLine ("New controller is automatic, only manual dock objects should be named controller");
  235. // check that the controller is in the toplevel list
  236. if (!toplevelDocks.Contains (value))
  237. Add (value);
  238. controller = value;
  239. } else {
  240. // no controller, no master
  241. controller = null;
  242. }
  243. }
  244. }
  245. internal void EmitLayoutChangedEvent ()
  246. {
  247. if (LayoutChanged != null)
  248. LayoutChanged (this, EventArgs.Empty);
  249. }
  250. private void OnItemDetached (object o, DetachedArgs args)
  251. {
  252. DockItem obj = o as DockItem;
  253. if (!obj.InReflow && !obj.IsAutomatic)
  254. EmitLayoutChangedEvent ();
  255. }
  256. private void OnItemDocked (object o, DockedArgs args)
  257. {
  258. DockItem requestor = args.Requestor as DockItem;
  259. // here we are in fact interested in the requestor, since it's
  260. // assumed that object will not change its visibility... for the
  261. // requestor, however, could mean that it's being shown
  262. if (!requestor.InReflow && !requestor.IsAutomatic)
  263. EmitLayoutChangedEvent ();
  264. }
  265. private void OnItemPropertyChanged (object o, string name)
  266. {
  267. DockItem item = o as DockItem;
  268. int locked = ComputeLocked ();
  269. bool item_locked = item.Locked;
  270. if (item_locked) {
  271. if (unlockedItems.ContainsKey (item))
  272. unlockedItems.Remove (item);
  273. if (!lockedItems.ContainsKey (item))
  274. lockedItems.Add (item, 1);
  275. }
  276. else {
  277. if (lockedItems.ContainsKey (item))
  278. lockedItems.Remove (item);
  279. if (!unlockedItems.ContainsKey (item))
  280. unlockedItems.Add (item, 1);
  281. }
  282. if (ComputeLocked () != locked)
  283. EmitNotifyLocked ();
  284. }
  285. private int ComputeLocked ()
  286. {
  287. if (unlockedItems.Count == 0)
  288. return 1;
  289. else if (lockedItems.Count == 0)
  290. return 0;
  291. else
  292. return -1;
  293. }
  294. private void OnDragBegin (DockItem item)
  295. {
  296. /* Set the target to itself so it won't go floating with just a click. */
  297. request = new DockRequest ();
  298. request.Applicant = item;
  299. request.Target = item;
  300. request.Position = DockPlacement.Floating;
  301. request.Extra = IntPtr.Zero;
  302. rectDrawn = false;
  303. rectOwner = null;
  304. }
  305. private void OnDragEnd (DockItem item, bool cancelled)
  306. {
  307. if (item != request.Applicant) {
  308. Console.WriteLine ("Dragged item is not the same as the one we started with");
  309. return;
  310. }
  311. /* Erase previously drawn rectangle */
  312. if (rectDrawn)
  313. XorRect ();
  314. /* cancel conditions */
  315. if (cancelled || request.Applicant == request.Target)
  316. return;
  317. // dock object to the requested position
  318. request.Target.Dock (request.Applicant,
  319. request.Position,
  320. request.Extra);
  321. EmitLayoutChangedEvent ();
  322. }
  323. private void OnDragMotion (DockItem item, int rootX, int rootY)
  324. {
  325. Dock dock = null;
  326. int winX, winY;
  327. int x, y;
  328. bool mayDock = false;
  329. DockRequest myRequest = new DockRequest (request);
  330. if (item != request.Applicant) {
  331. Console.WriteLine ("Dragged item is not the same as the one we started with");
  332. return;
  333. }
  334. /* first look under the pointer */
  335. Gdk.Window window = Gdk.Window.AtPointer (out winX, out winY);
  336. if (window != null && window.UserData != IntPtr.Zero) {
  337. /* ok, now get the widget who owns that window and see if we can
  338. get to a Dock by walking up the hierarchy */
  339. Widget widget = GLib.Object.GetObject (window.UserData, false) as Widget;
  340. while (widget != null && (!(widget is Dock) ||
  341. (widget is DockObject && ((DockObject)widget).Master != this)))
  342. widget = widget.Parent;
  343. if (widget != null) {
  344. int winW, winH, depth;
  345. /* verify that the pointer is still in that dock
  346. (the user could have moved it) */
  347. widget.GdkWindow.GetGeometry (out winX, out winY,
  348. out winW, out winH,
  349. out depth);
  350. widget.GdkWindow.GetOrigin (out winX, out winY);
  351. if (rootX >= winX && rootX < winX + winW &&
  352. rootY >= winY && rootY < winY + winH)
  353. dock = widget as Dock;
  354. }
  355. }
  356. if (dock != null) {
  357. /* translate root coordinates into dock object coordinates
  358. (i.e. widget coordinates) */
  359. dock.GdkWindow.GetOrigin (out winX, out winY);
  360. x = rootX - winX;
  361. y = rootY - winY;
  362. mayDock = dock.OnDockRequest (x, y, ref myRequest);
  363. } else {
  364. /* try to dock the item in all the docks in the ring in turn */
  365. foreach (Dock topDock in toplevelDocks) {
  366. if (topDock.GdkWindow == null)
  367. Console.WriteLine ("Dock has no GdkWindow: {0}, {1}", topDock.Name, topDock);
  368. /* translate root coordinates into dock object
  369. coordinates (i.e. widget coordinates) */
  370. topDock.GdkWindow.GetOrigin (out winX, out winY);
  371. x = rootX - winX;
  372. y = rootY - winY;
  373. mayDock = topDock.OnDockRequest (x, y, ref myRequest);
  374. if (mayDock)
  375. break;
  376. }
  377. }
  378. if (!mayDock) {
  379. dock = null;
  380. myRequest.Target = Dock.GetTopLevel (item);
  381. myRequest.Position = DockPlacement.Floating;
  382. Requisition preferredSize = item.PreferredSize;
  383. myRequest.Width = preferredSize.Width;
  384. myRequest.Height = preferredSize.Height;
  385. myRequest.X = rootX - item.DragOffX;
  386. myRequest.Y = rootY - item.DragOffY;
  387. Gdk.Rectangle rect = new Gdk.Rectangle (myRequest.X,
  388. myRequest.Y,
  389. myRequest.Width,
  390. myRequest.Height);
  391. // setup extra docking information
  392. myRequest.Extra = rect;
  393. }
  394. if (!(myRequest.X == request.X &&
  395. myRequest.Y == request.Y &&
  396. myRequest.Width == request.Width &&
  397. myRequest.Height == request.Height &&
  398. dock == rectOwner)) {
  399. /* erase the previous rectangle */
  400. if (rectDrawn)
  401. XorRect ();
  402. }
  403. // set the new values
  404. request = myRequest;
  405. rectOwner = dock;
  406. /* draw the previous rectangle */
  407. if (!rectDrawn)
  408. XorRect ();
  409. }
  410. private void XorRect ()
  411. {
  412. rectDrawn = !rectDrawn;
  413. if (rectOwner != null) {
  414. Gdk.Rectangle rect = new Gdk.Rectangle (request.X,
  415. request.Y,
  416. request.Width,
  417. request.Height);
  418. rectOwner.XorRect (rect);
  419. return;
  420. }
  421. Gdk.Window window = Gdk.Global.DefaultRootWindow;
  422. if (rootXorGC == null) {
  423. Gdk.GCValues values = new Gdk.GCValues ();
  424. values.Function = Gdk.Function.Invert;
  425. values.SubwindowMode = Gdk.SubwindowMode.IncludeInferiors;
  426. rootXorGC = new Gdk.GC (window);
  427. rootXorGC.SetValues (values, Gdk.GCValuesMask.Function |
  428. Gdk.GCValuesMask.Subwindow);
  429. }
  430. rootXorGC.SetLineAttributes (1, Gdk.LineStyle.OnOffDash,
  431. Gdk.CapStyle.NotLast,
  432. Gdk.JoinStyle.Bevel);
  433. rootXorGC.SetDashes (1, new sbyte[] {1, 1}, 2);
  434. window.DrawRectangle (rootXorGC, false, request.X, request.Y,
  435. request.Width, request.Height);
  436. rootXorGC.SetDashes (0, new sbyte[] {1, 1}, 2);
  437. window.DrawRectangle (rootXorGC, false, request.X + 1,
  438. request.Y + 1, request.Width - 2,
  439. request.Height - 2);
  440. }
  441. void EmitNotifyLocked ()
  442. {
  443. if (NotifyLocked != null)
  444. NotifyLocked (this, EventArgs.Empty);
  445. }
  446. }
  447. }