am-project: rh #702849 crash when removing a target in the project root node
[anjuta.git] / libanjuta / anjuta-dock.c
blob78d8c96bd59651f030c5df6f4d5a558885c9fcef
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 * git-shell-test
4 * Copyright (C) James Liggett 2009 <jrliggett@cox.net>
5 *
6 * git-shell-test is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * git-shell-test is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "anjuta-dock.h"
22 /**
23 * SECTION: AnjutaDock
24 * @short_description: Docking system for context-driven user interfaces.
25 * @see_also: #AnjutaCommandBar
26 * @include; libanjuta/anjuta-dock.h
28 * AnjutaDock provides an alternative to the traditional menu and toolbar based
29 * methodologies used by most GNOME programs. Instead, it focuses on tasks, or
30 * related sets of tasks. Each pane in the dock corresponds to a different set
31 * of related tasks.
33 * Optionally, you can also associate an #AnjutaCommandBar with an AnjutaDock to
34 * provide contextually appropriate sets of commands depending on the currently
35 * visible pane
38 struct _AnjutaDockPriv
40 GHashTable *panes;
41 GHashTable *dock_items;
42 GtkWidget *command_bar;
44 AnjutaDockPane* command_pane;
47 G_DEFINE_TYPE (AnjutaDock, anjuta_dock, GDL_TYPE_DOCK);
49 static void
50 anjuta_dock_init (AnjutaDock *self)
52 self->priv = g_new0 (AnjutaDockPriv, 1);
53 self->priv->panes = g_hash_table_new_full (g_str_hash, g_str_equal,
54 NULL, g_object_unref);
55 self->priv->dock_items = g_hash_table_new (g_str_hash, g_str_equal);
56 self->priv->command_pane = 0;
59 static void
60 anjuta_dock_finalize (GObject *object)
62 AnjutaDock *self;
64 self = ANJUTA_DOCK (object);
66 g_hash_table_destroy (self->priv->panes);
67 g_hash_table_destroy (self->priv->dock_items);
68 g_free (self->priv);
70 G_OBJECT_CLASS (anjuta_dock_parent_class)->finalize (object);
73 static void
74 on_pane_selected (GdlDockItem *item, AnjutaCommandBar *command_bar)
76 const gchar *pane_name;
78 pane_name = (const gchar *) g_object_get_data (G_OBJECT (item),
79 "pane-name");
80 anjuta_command_bar_show_action_group (command_bar, pane_name);
84 static void
85 disconnect_select_signals (gpointer key, GdlDockItem *value,
86 AnjutaCommandBar *command_bar)
88 g_signal_handlers_disconnect_by_func (value, G_CALLBACK (on_pane_selected),
89 command_bar);
92 static void
93 anjuta_dock_dispose (GObject *object)
95 AnjutaDock *self;
97 self = ANJUTA_DOCK (object);
99 if (self->priv->command_bar)
101 /* Disconnect pane selection signals before dropping the command bar
102 * reference. It is possible that we may be holding the last reference
103 * of the bar, creating the possibility that the parent implementation
104 * of dispose might do something that would call the signal handler that
105 * would reference the already destroyed command bar. */
106 g_hash_table_foreach (self->priv->dock_items,
107 (GHFunc) disconnect_select_signals,
108 self->priv->command_bar);
110 g_object_unref (self->priv->command_bar);
111 self->priv->command_bar = NULL;
114 G_OBJECT_CLASS (anjuta_dock_parent_class)->dispose (object);
117 static void
118 anjuta_dock_class_init (AnjutaDockClass *klass)
120 GObjectClass* object_class = G_OBJECT_CLASS (klass);
122 object_class->finalize = anjuta_dock_finalize;
123 object_class->dispose = anjuta_dock_dispose;
127 * anjuta_dock_new:
129 * Creates a new AnjutaDock.
131 GtkWidget *
132 anjuta_dock_new (void)
134 return g_object_new (ANJUTA_TYPE_DOCK, NULL);
138 * anjuta_dock_add_pane:
139 * @self: An AnjutaDock
140 * @pane_name: A unique name for this pane
141 * @pane_label: Label to display in this pane's grip
142 * @pane: The #AnjutaDockPane to add to the dock. The dock takes ownership of
143 * the pane object.
144 * @stock_id: Stock icon to display in this pane's grip
145 * @placement: A #GdlDockPlacement value indicating where the pane should be
146 * placed
147 * @entries: #AnjutaCommandBar entries for this pane. Can be %NULL
148 * @num_entries: The number of entries pointed to by entries, or 0.
149 * @user_data: User data to pass to the entry callback
151 * Adds a pane, with optional #AnjutaCommandBar entries, to an AnjutaDock. This
152 * method adds a pane with no grip that cannot be closed, floating or iconified.
154 void
155 anjuta_dock_add_pane (AnjutaDock *self, const gchar *pane_name,
156 const gchar *pane_label, const gchar *stock_icon,
157 AnjutaDockPane *pane, GdlDockPlacement placement,
158 AnjutaCommandBarEntry *entries, int num_entries,
159 gpointer user_data)
161 int behavior;
163 behavior = 0;
164 behavior |= GDL_DOCK_ITEM_BEH_NO_GRIP;
165 behavior |= GDL_DOCK_ITEM_BEH_CANT_CLOSE;
166 behavior |= GDL_DOCK_ITEM_BEH_CANT_ICONIFY;
167 behavior |= GDL_DOCK_ITEM_BEH_NEVER_FLOATING;
169 anjuta_dock_add_pane_full (self, pane_name, pane_label, stock_icon,
170 pane, placement, entries, num_entries,
171 user_data, behavior);
175 * anjuta_dock_add_pane_full:
176 * @self: An AnjutaDock
177 * @pane_name: A unique name for this pane
178 * @pane_label: Label to display in this pane's grip
179 * @stock_id: Stock icon to display in this pane's grip
180 * @pane: The #AnjutaDockPane to add to the dock. The dock takes ownership of
181 * the pane object.
182 * @placement: A #GdlDockPlacement value indicating where the pane should be
183 * placed
184 * @entries: #AnjutaCommandBar entries for this pane. Can be %NULL
185 * @num_entries: The number of entries pointed to by entries, or 0.
186 * @user_data: User data to pass to the entry callback
187 * @behavior: Any combination of #GdlDockItemBehavior flags
189 * Does the same thing as anjuta_dock_add_pane, but allows GDL dock behavior
190 * flags to be specified.
192 void
193 anjuta_dock_add_pane_full (AnjutaDock *self, const gchar *pane_name,
194 const gchar *pane_label, const gchar *stock_icon,
195 AnjutaDockPane *pane,
196 GdlDockPlacement placement,
197 AnjutaCommandBarEntry *entries, int num_entries,
198 gpointer user_data,
199 GdlDockItemBehavior behavior)
201 GtkWidget *dock_item;
202 GtkWidget *child;
204 dock_item = gdl_dock_item_new (pane_name, pane_label, behavior);
205 child = anjuta_dock_pane_get_widget (pane);
206 g_object_set_data (G_OBJECT (child), "dock-item", dock_item);
208 /* Make sure there isn't another dock with the same name */
209 if (!g_hash_table_lookup_extended (self->priv->panes, pane_name, NULL,
210 NULL))
212 /* Take ownership of the pane object */
213 g_hash_table_insert (self->priv->panes, (gchar *) pane_name, pane);
215 gtk_container_add (GTK_CONTAINER (dock_item), child);
216 gdl_dock_add_item (GDL_DOCK (self), GDL_DOCK_ITEM (dock_item), placement);
218 g_object_set_data (G_OBJECT (dock_item), "pane-name", (gchar *) pane_name);
220 /* Don't add anything to the action bar if there are no entries */
221 if (self->priv->command_bar && entries)
223 anjuta_command_bar_add_action_group (ANJUTA_COMMAND_BAR (self->priv->command_bar),
224 pane_name, entries, num_entries,
225 user_data);
227 g_signal_connect (G_OBJECT (dock_item), "selected",
228 G_CALLBACK (on_pane_selected),
229 self->priv->command_bar);
231 g_hash_table_insert (self->priv->dock_items, (gchar *) pane_name,
232 dock_item);
234 /* Show the new pane's commands in the command bar */
235 anjuta_command_bar_show_action_group (ANJUTA_COMMAND_BAR (self->priv->command_bar),
236 pane_name);
242 * anjuta_dock_replace_command_pane:
243 * @self: An AnjutaDock
244 * @pane_name: A unique name for this pane
245 * @pane_label: Label to display in this pane's grip
246 * @pane: The #AnjutaDockPane to add to the dock. The dock takes ownership of
247 * the pane object.
248 * @stock_id: Stock icon to display in this pane's grip
249 * @placement: A #GdlDockPlacement value indicating where the pane should be
250 * placed
251 * @entries: #AnjutaCommandBar entries for this pane. Can be %NULL
252 * @num_entries: The number of entries pointed to by entries, or 0.
253 * @user_data: User data to pass to the entry callback
255 * Adds a pane, with optional #AnjutaCommandBar entries, to an AnjutaDock. This
256 * method adds a pane with no grip that cannot be closed, floating or iconified.
257 * If there was an old command pane, that pane is removed in favour of the new pane.
259 void
260 anjuta_dock_replace_command_pane (AnjutaDock *self,
261 const gchar *pane_name,
262 const gchar *pane_label, const gchar *stock_icon,
263 AnjutaDockPane *pane, GdlDockPlacement placement,
264 AnjutaCommandBarEntry *entries, int num_entries,
265 gpointer user_data)
267 if (self->priv->command_pane)
269 anjuta_dock_remove_pane (self, self->priv->command_pane);
272 anjuta_dock_add_pane (self, pane_name, pane_label, stock_icon,
273 pane, placement, entries, num_entries, user_data);
275 self->priv->command_pane = pane;
279 * anjuta_dock_remove_pane:
280 * @self An AnjutaDock
281 * @pane_name: Name of the pane to remove
283 * Removes a pane from a dock
285 void
286 anjuta_dock_remove_pane (AnjutaDock *self, AnjutaDockPane *pane)
288 GtkWidget *child;
289 GtkContainer *dock_item;
291 child = anjuta_dock_pane_get_widget (pane);
293 if (self->priv->command_pane == pane)
295 self->priv->command_pane = NULL;
298 if (child)
300 /* Remove the child from its dock item and destroy it */
301 dock_item = g_object_get_data (G_OBJECT (child), "dock-item");
302 g_hash_table_remove (self->priv->panes,
303 g_object_get_data (G_OBJECT (dock_item),
304 "pane-name"));
305 gtk_container_remove (dock_item, child);
307 gdl_dock_item_unbind (GDL_DOCK_ITEM (dock_item));
312 * anjuta_dock_show_pane:
313 * @self: An AnjutaDock
314 * @pane_name: Name of the pane to show
316 * Makes the given pane visible
318 void
319 anjuta_dock_show_pane (AnjutaDock *self, AnjutaDockPane *pane)
321 GtkWidget *child;
322 GdlDockItem *dock_item;
324 child = anjuta_dock_pane_get_widget (pane);
326 if (child)
328 dock_item = g_object_get_data (G_OBJECT (child), "dock-item");
329 gdl_dock_item_show_item (dock_item);
334 * anjuta_dock_hide_pane:
335 * @self: An AnjutaDock
336 * @pane_name: Name of the pane to hide
338 * Makes the given pane invisible
340 void
341 anjuta_dock_hide_pane (AnjutaDock *self, AnjutaDockPane *pane)
343 GtkWidget *child;
344 GdlDockItem *dock_item;
346 child = anjuta_dock_pane_get_widget (pane);
348 if (child)
350 dock_item = g_object_get_data (G_OBJECT (child), "dock-item");
351 gdl_dock_item_hide_item (dock_item);
356 * anjuta_dock_set_command_bar:
357 * @self: An AnjutaDock
358 * @command_bar: An #AnjutaCommandBar to associate with this dock
360 * Associates an #AnjutaCommandBar with this dock. Command bars can be used to
361 * provide different sets of commands based on the currently visible pane.
363 void
364 anjuta_dock_set_command_bar (AnjutaDock *self, AnjutaCommandBar *command_bar)
366 if (self->priv->command_bar)
367 g_object_unref (self->priv->command_bar);
369 self->priv->command_bar = g_object_ref (command_bar);
373 * anjuta_dock_get_command_bar:
374 * @self: An AnjutaDock
376 * Returns: the #AnjutaCommandBar associated with this dock or %NULL.
378 AnjutaCommandBar *
379 anjuta_dock_get_command_bar (AnjutaDock *self)
381 return ANJUTA_COMMAND_BAR (self->priv->command_bar);