DockItemGrip.cs 12 KB

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