DockBar.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2005 John Luke <john.luke@gmail.com>
  5. *
  6. * based on work by:
  7. * Copyright (C) 2002 Gustavo Giráldez <gustavo.giraldez@gmx.net>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place - Suite 330,
  22. * Boston, MA 02111-1307, USA.
  23. */
  24. using System;
  25. using System.Collections;
  26. using Gtk;
  27. namespace Gdl
  28. {
  29. /// <summary>
  30. /// This class is used to represent a number of iconified DockItems.
  31. /// It shows the icons by means of an according number of DockBarButtons.
  32. /// </summary>
  33. public class DockBar : VBox
  34. {
  35. DockMaster master;
  36. ArrayList items;
  37. Tooltips tooltips;
  38. public DockBar (Dock dock)
  39. {
  40. items = new ArrayList ();
  41. tooltips = new Tooltips ();
  42. Master = dock.Master;
  43. }
  44. public DockMaster Master
  45. {
  46. get { return master; }
  47. set { this.Attach (value); }
  48. }
  49. public void AddItem (DockItem item)
  50. {
  51. // check if already there
  52. if (items.Contains (item)) {
  53. Console.WriteLine ("WARNING: Item has already been added to the dockbar");
  54. return;
  55. }
  56. items.Add (item);
  57. // create a button for the item
  58. DockBarButton button = new DockBarButton (item);
  59. this.PackStart (button, false, false, 0);
  60. tooltips.SetTip (button, item.LongName, item.LongName);
  61. item.DockBar = this;
  62. item.DockBarButton = button;
  63. button.Clicked += new EventHandler (OnDockButtonClicked);
  64. this.ShowAll ();
  65. }
  66. public void Attach (DockMaster master)
  67. {
  68. if (master == null)
  69. return;
  70. master.LayoutChanged -= new EventHandler (OnLayoutChanged);
  71. this.master = master;
  72. master.LayoutChanged += new EventHandler (OnLayoutChanged);
  73. }
  74. public override void Destroy ()
  75. {
  76. if (master != null) {
  77. master.LayoutChanged -= new EventHandler (OnLayoutChanged);
  78. master = null;
  79. }
  80. if (tooltips != null) {
  81. tooltips = null;
  82. }
  83. base.Destroy ();
  84. }
  85. public void RemoveItem (DockItem item)
  86. {
  87. // we can only remove if it is there
  88. if (items.Contains (item)) {
  89. items.Remove (item);
  90. this.Remove (item.DockBarButton);
  91. // item.DockBarButton = null;
  92. }
  93. else {
  94. Console.WriteLine ("WARNING: {0} has not been added to the dockbar", item.Name);
  95. }
  96. }
  97. void UpdateDockItems ()
  98. {
  99. if (master == null)
  100. return;
  101. foreach (object o in master.DockObjects)
  102. {
  103. DockItem item = o as DockItem;
  104. if (item == null)
  105. continue;
  106. // in items but shouldn't be, remove it
  107. if (items.Contains (item) && !item.Iconified)
  108. this.RemoveItem (item);
  109. // not in items but should be, add it
  110. else if (!items.Contains (item) && item.Iconified)
  111. this.AddItem (item);
  112. }
  113. }
  114. void OnLayoutChanged (object o, EventArgs args)
  115. {
  116. UpdateDockItems ();
  117. }
  118. void OnDockButtonClicked (object o, EventArgs args)
  119. {
  120. DockItem item = ((DockBarButton) o).DockItem;
  121. item.DockBar = null;
  122. item.ShowItem ();
  123. item.Master.Controller.QueueResize ();
  124. }
  125. }
  126. }