DockItemGrip.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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 Gtk;
  28. namespace Gdl
  29. {
  30. /// <summary>
  31. /// This class represents header part of a DockItem.
  32. /// It provided buttons for iconifying and closing the DockItem.
  33. /// In addition it lets the user drag the DockItem.
  34. /// </summary>
  35. public class DockItemGrip : Container
  36. {
  37. private DockItem item;
  38. private Gdk.Window titleWindow;
  39. private Button closeButton;
  40. private Button iconifyButton;
  41. private Tooltips tooltips;
  42. private Gdk.Pixbuf icon = null;
  43. private string title;
  44. private Pango.Layout layout = null;
  45. protected DockItemGrip (IntPtr raw) : base (raw) { }
  46. protected DockItemGrip ()
  47. {
  48. WidgetFlags |= WidgetFlags.NoWindow;
  49. Widget.PushCompositeChild ();
  50. closeButton = new Button ();
  51. Widget.PopCompositeChild ();
  52. closeButton.WidgetFlags &= ~WidgetFlags.CanFocus;
  53. closeButton.Parent = this;
  54. closeButton.Relief = ReliefStyle.None;
  55. closeButton.Show ();
  56. Image image = new Image (GdlStock.Close, IconSize.Menu);
  57. closeButton.Add (image);
  58. image.Show ();
  59. closeButton.Clicked += new EventHandler (CloseClicked);
  60. Widget.PushCompositeChild ();
  61. iconifyButton = new Button ();
  62. Widget.PopCompositeChild ();
  63. iconifyButton.WidgetFlags &= ~WidgetFlags.CanFocus;
  64. iconifyButton.Parent = this;
  65. iconifyButton.Relief = ReliefStyle.None;
  66. iconifyButton.Show ();
  67. image = new Image (GdlStock.MenuLeft, IconSize.Menu);
  68. iconifyButton.Add (image);
  69. image.Show ();
  70. iconifyButton.Clicked += new EventHandler (IconifyClicked);
  71. tooltips = new Tooltips ();
  72. tooltips.SetTip (iconifyButton, "Iconify", "Iconify this dock");
  73. tooltips.SetTip (closeButton, "Close", "Close this dock");
  74. }
  75. public DockItemGrip (DockItem item) : this ()
  76. {
  77. if (item == null)
  78. throw new ArgumentNullException ("item", "A valid DockItem must be given");
  79. Item = item;
  80. }
  81. private Gdk.Pixbuf Icon {
  82. get {
  83. if (icon == null && item.StockId != null)
  84. icon = RenderIcon (item.StockId, IconSize.Menu, "");
  85. return icon;
  86. }
  87. set {
  88. icon = value;
  89. QueueDraw ();
  90. }
  91. }
  92. public new DockItem Item {
  93. get {
  94. return item;
  95. }
  96. set {
  97. if (item != null)
  98. item.PropertyChanged -= new PropertyChangedHandler (OnPropertyChanged);
  99. item = value;
  100. item.PropertyChanged += new PropertyChangedHandler (OnPropertyChanged);
  101. if (!item.CantClose)
  102. closeButton.Show ();
  103. else
  104. closeButton.Hide ();
  105. if (!item.CantIconify)
  106. iconifyButton.Show ();
  107. else
  108. iconifyButton.Hide ();
  109. icon = null;
  110. layout = null;
  111. title = null;
  112. }
  113. }
  114. private Pango.Layout Layout {
  115. get {
  116. if (layout == null) {
  117. layout = CreatePangoLayout (Title);
  118. layout.SingleParagraphMode = true;
  119. }
  120. return layout;
  121. }
  122. }
  123. private string Title {
  124. get {
  125. if (title == null) {
  126. if (item.LongName != null)
  127. title = item.LongName;
  128. else
  129. title = "";
  130. }
  131. return title;
  132. }
  133. set {
  134. title = value;
  135. if (layout != null)
  136. layout.SetMarkup (Title);
  137. QueueDraw ();
  138. }
  139. }
  140. private Gdk.Rectangle TitleArea {
  141. get {
  142. Gdk.Rectangle area;
  143. int bw = (int)BorderWidth;
  144. int height, width;
  145. area.Width = Allocation.Width - 2 * bw;
  146. Layout.GetPixelSize (out width, out height);
  147. height = Math.Max (height, closeButton.Allocation.Height);
  148. height = Math.Max (height, iconifyButton.Allocation.Height);
  149. if (closeButton.Visible) {
  150. area.Width -= closeButton.Allocation.Width;
  151. }
  152. if (iconifyButton.Visible) {
  153. area.Width -= iconifyButton.Allocation.Width;
  154. }
  155. area.X = Allocation.X + bw;
  156. area.Y = Allocation.Y + bw;
  157. area.Height = height;
  158. if (Direction == TextDirection.Rtl)
  159. area.X += (Allocation.Width - 2 * bw) - area.Width;
  160. return area;
  161. }
  162. }
  163. public Gdk.Window TitleWindow {
  164. get {
  165. return titleWindow;
  166. }
  167. set {
  168. titleWindow = value;
  169. }
  170. }
  171. private void OnPropertyChanged (object o, string name)
  172. {
  173. switch (name) {
  174. case "StockId":
  175. Icon = RenderIcon (item.StockId, IconSize.Menu, "");
  176. break;
  177. case "LongName":
  178. Title = item.LongName;
  179. break;
  180. case "Locked":
  181. case "Behavior":
  182. bool cursor = false;
  183. if (item.CantClose || item.Locked) {
  184. closeButton.Hide ();
  185. }
  186. else {
  187. closeButton.Show ();
  188. cursor = true;
  189. }
  190. if (item.CantIconify || item.Locked) {
  191. iconifyButton.Hide ();
  192. }
  193. else {
  194. iconifyButton.Show ();
  195. cursor = true;
  196. }
  197. if (!cursor && titleWindow != null)
  198. titleWindow.Cursor = null;
  199. break;
  200. default:
  201. break;
  202. }
  203. }
  204. protected override void OnDestroyed ()
  205. {
  206. if (layout != null)
  207. layout = null;
  208. if (icon != null)
  209. icon = null;
  210. if (tooltips != null)
  211. tooltips = null;
  212. if (item != null) {
  213. // FIXME: Disconnect future signal handlers for notify.
  214. }
  215. item = null;
  216. base.OnDestroyed ();
  217. }
  218. protected override bool OnExposeEvent (Gdk.EventExpose evnt)
  219. {
  220. Gdk.Rectangle titleArea = TitleArea;
  221. Gdk.Rectangle exposeArea;
  222. if (Icon != null) {
  223. Gdk.Rectangle pixbufRect;
  224. pixbufRect.Width = icon.Width;
  225. pixbufRect.Height = icon.Height;
  226. if (Direction == TextDirection.Rtl) {
  227. pixbufRect.X = titleArea.X + titleArea.Width - pixbufRect.Width;
  228. } else {
  229. pixbufRect.X = titleArea.X;
  230. titleArea.X += pixbufRect.Width + 4;
  231. }
  232. titleArea.Width -= pixbufRect.Width - 4;
  233. pixbufRect.Y = titleArea.Y + (titleArea.Height - pixbufRect.Height) / 2;
  234. if (evnt.Area.Intersect (pixbufRect, out exposeArea)) {
  235. Gdk.GC gc = Style.BackgroundGC (State);
  236. GdkWindow.DrawPixbuf (gc, icon, 0, 0, pixbufRect.X,
  237. pixbufRect.Y, pixbufRect.Width,
  238. pixbufRect.Height, Gdk.RgbDither.None,
  239. 0, 0);
  240. }
  241. }
  242. /* TODO this crashs win32 at the moment...
  243. if (titleArea.Intersect (evnt.Area, out exposeArea)) {
  244. int width, height, textX, textY;
  245. Layout.GetPixelSize (out width, out height);
  246. if (Direction == TextDirection.Rtl)
  247. textX = titleArea.X + titleArea.Width - width;
  248. else
  249. textX = titleArea.X;
  250. textY = titleArea.Y + (titleArea.Height - height) / 2;
  251. Style.PaintLayout (Style, GdkWindow, State, true,
  252. exposeArea, this, null, textX,
  253. textY, layout);
  254. }
  255. */
  256. return base.OnExposeEvent (evnt);
  257. }
  258. private void CloseClicked (object o, EventArgs e)
  259. {
  260. item.HideItem ();
  261. }
  262. private void IconifyClicked (object o, EventArgs e)
  263. {
  264. item.IconifyItem ();
  265. iconifyButton.InButton = false;
  266. iconifyButton.Leave ();
  267. }
  268. protected override void OnRealized ()
  269. {
  270. base.OnRealized ();
  271. if (titleWindow == null) {
  272. Gdk.WindowAttr attributes = new Gdk.WindowAttr ();
  273. Gdk.Rectangle area = TitleArea;
  274. attributes.X = area.X;
  275. attributes.Y = area.Y;
  276. attributes.Width = area.Width;
  277. attributes.Height = area.Height;
  278. attributes.WindowType = Gdk.WindowType.Temp;
  279. attributes.Wclass = Gdk.WindowClass.InputOnly;
  280. attributes.OverrideRedirect = true;
  281. attributes.EventMask = (int) (Events |
  282. Gdk.EventMask.ButtonPressMask |
  283. Gdk.EventMask.ButtonReleaseMask |
  284. Gdk.EventMask.ButtonMotionMask);
  285. titleWindow = new Gdk.Window (ParentWindow, attributes,
  286. (int) (Gdk.WindowAttributesType.X |
  287. Gdk.WindowAttributesType.Y |
  288. Gdk.WindowAttributesType.Noredir));
  289. titleWindow.UserData = Handle;
  290. if (item.CantClose || item.CantIconify)
  291. titleWindow.Cursor = null;
  292. else
  293. titleWindow.Cursor = new Gdk.Cursor (Display, Gdk.CursorType.Hand2);
  294. }
  295. }
  296. protected override void OnUnrealized ()
  297. {
  298. if (titleWindow != null) {
  299. titleWindow.UserData = IntPtr.Zero;
  300. titleWindow.Destroy ();
  301. titleWindow = null;
  302. }
  303. base.OnUnrealized ();
  304. }
  305. protected override void OnMapped ()
  306. {
  307. base.OnMapped ();
  308. if (titleWindow != null)
  309. titleWindow.Show ();
  310. }
  311. protected override void OnUnmapped ()
  312. {
  313. if (titleWindow != null)
  314. titleWindow.Hide ();
  315. base.OnUnmapped ();
  316. }
  317. protected override void OnSizeRequested (ref Requisition requisition)
  318. {
  319. requisition.Width = (int)BorderWidth * 2;
  320. requisition.Height = (int)BorderWidth * 2;
  321. // ensure_title_and_icon_pixbuf (grip);
  322. int layoutHeight, layoutWidth;
  323. this.Layout.GetPixelSize (out layoutWidth, out layoutHeight);
  324. Requisition childReq = closeButton.SizeRequest ();
  325. requisition.Width += childReq.Width;
  326. layoutHeight = Math.Max (requisition.Height, childReq.Height);
  327. childReq = iconifyButton.SizeRequest ();
  328. requisition.Width += childReq.Width;
  329. layoutHeight = Math.Max (requisition.Height, childReq.Height);
  330. requisition.Height = layoutHeight;
  331. if (Icon != null) {
  332. requisition.Width += icon.Width + 1;
  333. requisition.Height = Math.Max (requisition.Height, icon.Height);
  334. }
  335. }
  336. private void EllipsizeLayout (int width)
  337. {
  338. // no room to draw anything
  339. if (width < 1) {
  340. layout.SetMarkup ("");
  341. return;
  342. }
  343. // plenty of room
  344. int lw, lh;
  345. layout.GetPixelSize (out lw, out lh);
  346. if (width > lw)
  347. return;
  348. // not enough room for ...
  349. int ell_w, ell_h;
  350. Pango.Layout ell = layout.Copy ();
  351. ell.SetMarkup ("...");
  352. ell.GetPixelSize (out ell_w, out ell_h);
  353. if (width < ell_w) {
  354. layout.SetMarkup ("");
  355. return;
  356. }
  357. // subtract ellipses width
  358. width -= ell_w;
  359. int index, trailing;
  360. Pango.LayoutLine line = layout.GetLine (0);
  361. if (line.XToIndex (width * 1024, out index, out trailing)) {
  362. // Console.WriteLine ("length: {0} index: {1} trailing: {2}", layout.Text.Length, index, trailing);
  363. // FIXME: breaks on accented chars
  364. if (index < layout.Text.Length)
  365. layout.SetMarkup (layout.Text.Substring (0, index) + "...");
  366. }
  367. }
  368. protected override void OnSizeAllocated (Gdk.Rectangle allocation)
  369. {
  370. base.OnSizeAllocated (allocation);
  371. Gdk.Rectangle childAlloc;
  372. int bw = (int)BorderWidth;
  373. if (Direction == TextDirection.Rtl)
  374. childAlloc.X = allocation.X + bw;
  375. else
  376. childAlloc.X = allocation.X + allocation.Width - bw;
  377. childAlloc.Y = allocation.Y + bw;
  378. Requisition buttonReq = closeButton.SizeRequest ();
  379. if (Direction != TextDirection.Rtl)
  380. childAlloc.X -= buttonReq.Width;
  381. childAlloc.Width = buttonReq.Width;
  382. childAlloc.Height = buttonReq.Height;
  383. closeButton.SizeAllocate (childAlloc);
  384. if (Direction == TextDirection.Rtl)
  385. childAlloc.X += buttonReq.Width;
  386. buttonReq = iconifyButton.SizeRequest ();
  387. if (Direction != TextDirection.Rtl)
  388. childAlloc.X -= buttonReq.Width;
  389. childAlloc.Width = buttonReq.Width;
  390. childAlloc.Height = buttonReq.Height;
  391. iconifyButton.SizeAllocate (childAlloc);
  392. if (Direction == TextDirection.Rtl)
  393. childAlloc.X += buttonReq.Width;
  394. if (TitleWindow != null) {
  395. layout.SetMarkup (title);
  396. Gdk.Rectangle area = TitleArea;
  397. titleWindow.MoveResize (area.X, area.Y,area.Width, area.Height);
  398. if (Icon != null)
  399. area.Width -= icon.Width + 1;
  400. EllipsizeLayout (area.Width);
  401. }
  402. }
  403. protected override void OnAdded (Widget widget)
  404. {
  405. Console.WriteLine ("You can't add a widget to DockItemGrip directly");
  406. }
  407. protected override void OnRemoved (Widget widget)
  408. {
  409. Console.WriteLine ("You can't remove a widget from DockItemGrip directly");
  410. }
  411. protected override void ForAll (bool include_internals, Callback cb)
  412. {
  413. if (include_internals) {
  414. cb (closeButton);
  415. cb (iconifyButton);
  416. }
  417. }
  418. }
  419. }