DockNotebook.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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.Xml;
  28. using Gtk;
  29. namespace Gdl
  30. {
  31. public class DockNotebook : DockItem
  32. {
  33. private struct DockInfo
  34. {
  35. public DockPlacement position;
  36. public object data;
  37. public DockInfo (DockPlacement position, object data)
  38. {
  39. this.position = position;
  40. this.data = data;
  41. }
  42. }
  43. private DockInfo dockInfo;
  44. protected DockNotebook (IntPtr raw) : base (raw) { }
  45. static DockNotebook ()
  46. {
  47. Rc.ParseString ("style \"gdl-dock-notebook-default\" {\n" +
  48. "xthickness = 2\n" +
  49. "ythickness = 2\n" +
  50. "}\n" +
  51. "widget_class \"*.GtkNotebook.Gdl_DockItem\" " +
  52. "style : gtk \"gdl-dock-notebook-default\"\n");
  53. }
  54. public DockNotebook ()
  55. {
  56. Child = new Notebook ();
  57. Child.Parent = this;
  58. ((Notebook)Child).TabPos = PositionType.Bottom;
  59. // FIXME: enable these if we do a DockTabLabel
  60. //((Notebook)Child).SwitchPage += new SwitchPageHandler (SwitchPageCb);
  61. //((Notebook)Child).ButtonPressEvent += new ButtonPressEvent (ButtonPressCb);
  62. //((Notebook)Child).ButtonReleaseEvent += new ButtonReleaseEvent (ButtonReleaseCb);
  63. ((Notebook)Child).Scrollable = true;
  64. Child.Show ();
  65. DockObjectFlags &= ~(DockObjectFlags.Automatic);
  66. }
  67. protected void SwitchPageHandler (object o, SwitchPageArgs e)
  68. {
  69. // FIXME: port this if we do a DockTabLabel
  70. }
  71. protected override void OnDestroyed ()
  72. {
  73. // this first
  74. base.OnDestroyed ();
  75. // after that we can remove the GtkNotebook
  76. if (Child != null) {
  77. Child.Unparent ();
  78. Child = null;
  79. }
  80. }
  81. protected override void OnAdded (Widget widget)
  82. {
  83. if (widget == null || !(widget is DockItem))
  84. return;
  85. Dock ((DockObject)widget, DockPlacement.Center, null);
  86. }
  87. protected override void ForAll (bool includeInternals, Callback cb)
  88. {
  89. if (includeInternals) {
  90. base.ForAll (includeInternals, cb);
  91. } else {
  92. if (Child != null) {
  93. ((Notebook)Child).Foreach (cb);
  94. }
  95. }
  96. }
  97. private void DockChild (Widget w)
  98. {
  99. if (w is DockObject)
  100. Dock ((DockObject)w, dockInfo.position, dockInfo.data);
  101. }
  102. public override void OnDocked (DockObject requestor, DockPlacement position, object data)
  103. {
  104. /* we only add support for DockPlacement.Center docking
  105. strategy here... for the rest use our parent class' method */
  106. if (position == DockPlacement.Center) {
  107. /* we can only dock simple (not compound) items */
  108. if (requestor.IsCompound) {
  109. requestor.Freeze ();
  110. dockInfo = new DockInfo (position, data);
  111. requestor.Foreach (new Callback (DockChild));
  112. requestor.Thaw ();
  113. } else {
  114. DockItem requestorItem = requestor as DockItem;
  115. Widget label = requestorItem.TabLabel;
  116. if (label == null) {
  117. label = new Label (requestorItem.LongName);
  118. requestorItem.TabLabel = label;
  119. }
  120. int tabPosition = -1;
  121. if (data is Int32)
  122. tabPosition = Convert.ToInt32 (data);
  123. ((Notebook)Child).InsertPage (requestor, label, tabPosition);
  124. requestor.DockObjectFlags |= DockObjectFlags.Attached;
  125. }
  126. } else {
  127. base.OnDocked (requestor, position, data);
  128. }
  129. }
  130. public override void SetOrientation (Orientation orientation)
  131. {
  132. if (Child != null && Child is Notebook) {
  133. if (orientation == Orientation.Horizontal)
  134. ((Notebook)Child).TabPos = PositionType.Left;
  135. else
  136. ((Notebook)Child).TabPos = PositionType.Bottom;
  137. }
  138. base.SetOrientation (orientation);
  139. }
  140. public override bool OnChildPlacement (DockObject child, ref DockPlacement position)
  141. {
  142. DockPlacement pos = DockPlacement.None;
  143. if (Child != null) {
  144. foreach (Widget widget in ((Notebook)Child).Children) {
  145. if (widget == child) {
  146. pos = DockPlacement.Center;
  147. break;
  148. }
  149. }
  150. }
  151. if (pos != DockPlacement.None) {
  152. position = pos;
  153. return true;
  154. }
  155. return false;
  156. }
  157. public override void OnPresent (DockObject child)
  158. {
  159. Notebook nb = Child as Notebook;
  160. int i = nb.PageNum (child);
  161. if (i >= 0)
  162. nb.CurrentPage = i;
  163. base.OnPresent (child);
  164. }
  165. public override bool OnReorder (DockObject requestor, DockPlacement position, object other_data)
  166. {
  167. bool handled = false;
  168. int current_position, new_pos = -1;
  169. if (Child != null && position == DockPlacement.Center) {
  170. current_position = ((Notebook)Child).PageNum (requestor);
  171. if (current_position >= 0) {
  172. handled = true;
  173. if (other_data is Int32)
  174. new_pos = Convert.ToInt32 (other_data);
  175. ((Notebook)Child).ReorderChild (requestor, new_pos);
  176. }
  177. }
  178. return handled;
  179. }
  180. public override bool IsCompound {
  181. get { return true; }
  182. }
  183. [After]
  184. [Export]
  185. public int Page {
  186. get { return ((Notebook)Child).CurrentPage; }
  187. set { ((Notebook)Child).CurrentPage = value; }
  188. }
  189. public override bool HasGrip {
  190. get { return false; }
  191. }
  192. }
  193. }