DockBar.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. public DockBar (Dock dock)
  38. {
  39. items = new ArrayList ();
  40. Master = dock.Master;
  41. }
  42. public DockMaster Master
  43. {
  44. get { return master; }
  45. set { this.Attach (value); }
  46. }
  47. public void AddItem (DockItem item)
  48. {
  49. // check if already there
  50. if (items.Contains (item)) {
  51. Console.WriteLine ("WARNING: Item has already been added to the dockbar");
  52. return;
  53. }
  54. items.Add (item);
  55. // create a button for the item
  56. DockBarButton button = new DockBarButton (item);
  57. this.PackStart (button, false, false, 0);
  58. button.TooltipText = item.LongName;
  59. item.DockBar = this;
  60. item.DockBarButton = button;
  61. button.Clicked += new EventHandler (OnDockButtonClicked);
  62. this.ShowAll ();
  63. }
  64. public void Attach (DockMaster master)
  65. {
  66. if (master == null)
  67. return;
  68. master.LayoutChanged -= new EventHandler (OnLayoutChanged);
  69. this.master = master;
  70. master.LayoutChanged += new EventHandler (OnLayoutChanged);
  71. }
  72. public override void Destroy ()
  73. {
  74. if (master != null) {
  75. master.LayoutChanged -= new EventHandler (OnLayoutChanged);
  76. master = null;
  77. }
  78. base.Destroy ();
  79. }
  80. public void RemoveItem (DockItem item)
  81. {
  82. // we can only remove if it is there
  83. if (items.Contains (item)) {
  84. items.Remove (item);
  85. this.Remove (item.DockBarButton);
  86. // item.DockBarButton = null;
  87. }
  88. else {
  89. Console.WriteLine ("WARNING: {0} has not been added to the dockbar", item.Name);
  90. }
  91. }
  92. void UpdateDockItems ()
  93. {
  94. if (master == null)
  95. return;
  96. foreach (object o in master.DockObjects)
  97. {
  98. DockItem item = o as DockItem;
  99. if (item == null)
  100. continue;
  101. // in items but shouldn't be, remove it
  102. if (items.Contains (item) && !item.Iconified)
  103. this.RemoveItem (item);
  104. // not in items but should be, add it
  105. else if (!items.Contains (item) && item.Iconified)
  106. this.AddItem (item);
  107. }
  108. }
  109. void OnLayoutChanged (object o, EventArgs args)
  110. {
  111. UpdateDockItems ();
  112. }
  113. void OnDockButtonClicked (object o, EventArgs args)
  114. {
  115. DockItem item = ((DockBarButton) o).DockItem;
  116. item.DockBar = null;
  117. item.ShowItem ();
  118. item.Master.Controller.QueueResize ();
  119. }
  120. }
  121. }