build-basic-autotools:bgo#688293 - build-basic-autotools: Fix invalid read
[anjuta.git] / libanjuta / anjuta-command-bar.c
blobdc447aa0df57f9364dcc8cc850bf7e0286257795
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-command-bar.h"
22 /**
23 * SECTION: anjuta-command-bar
24 * @short_description: Widget that lays out commands in a vertical row of
25 * buttons and frames.
26 * @see_also: #AnjutaDock, #GtkAction
27 * @include: libanjuta/anjuta-command-bar.h
29 * AnjutaCommandBar provides a convenient way to arrange several sets of
30 * commands into one widget. It separates commands into different groups of
31 * actions, with only one group visible at a time.
34 G_DEFINE_TYPE (AnjutaCommandBar, anjuta_command_bar, GTK_TYPE_NOTEBOOK);
36 struct _AnjutaCommandBarPriv
38 GHashTable *action_groups;
39 GHashTable *widgets;
42 static void
43 anjuta_command_bar_init (AnjutaCommandBar *self)
45 self->priv = g_new0 (AnjutaCommandBarPriv, 1);
47 gtk_notebook_set_show_border (GTK_NOTEBOOK (self), FALSE);
48 gtk_notebook_set_show_tabs (GTK_NOTEBOOK (self), FALSE);
50 /* The action groups table contains the GtkActionGroup objects that
51 * correspond to each page of the action bar. The widgets table contain's
52 * each group's set of buttons and frames. */
53 self->priv->action_groups = g_hash_table_new_full (g_str_hash, g_str_equal,
54 NULL, g_object_unref);
55 self->priv->widgets = g_hash_table_new (g_str_hash, g_str_equal);
58 static void
59 anjuta_command_bar_finalize (GObject *object)
61 AnjutaCommandBar *self;
63 self = ANJUTA_COMMAND_BAR (object);
65 g_hash_table_destroy (self->priv->action_groups);
66 g_hash_table_destroy (self->priv->widgets);
68 G_OBJECT_CLASS (anjuta_command_bar_parent_class)->finalize (object);
71 static void
72 anjuta_command_bar_class_init (AnjutaCommandBarClass *klass)
74 GObjectClass* object_class = G_OBJECT_CLASS (klass);
76 object_class->finalize = anjuta_command_bar_finalize;
79 /**
80 * anjuta_command_bar_new:
82 * Creates a new AnjutaCommandBar.
83 * Returns: A new AnjutaCommandBar
85 GtkWidget *
86 anjuta_command_bar_new (void)
88 return g_object_new (ANJUTA_TYPE_COMMAND_BAR, NULL);
91 /**
92 * anjuta_command_bar_add_action_group:
93 * @self: An AnjutaCommandBar
94 * @group_name: A unique name for this group of entries
95 * @entries: A list of entries to add
96 * @num_entries: The number of items pointed to by entries
97 * @user_data: User data to pass to the entry callback
99 * Adds a group of entries to an AnjutaCommandBar.
101 void
102 anjuta_command_bar_add_action_group (AnjutaCommandBar *self,
103 const gchar *group_name,
104 const AnjutaCommandBarEntry *entries,
105 int num_entries, gpointer user_data)
107 GtkWidget *vbox;
108 GtkWidget *current_vbox;
109 GtkActionGroup *action_group;
110 int i;
111 GtkAction *action;
112 GtkWidget *button;
113 GtkWidget *button_image;
114 gchar *frame_label_text;
115 GtkWidget *frame_label;
116 GtkWidget *frame;
117 GtkWidget *frame_vbox;
119 vbox = gtk_vbox_new (FALSE, 2);
121 g_hash_table_insert (self->priv->widgets, (gchar *) group_name,
122 vbox);
124 action_group = gtk_action_group_new (group_name);
126 g_hash_table_insert (self->priv->action_groups, (gchar *) group_name,
127 action_group);
129 /* The current_vbox is the vbox we're currently adding buttons to. As
130 * frame entries are encountered, the current box changes to the newly
131 * created frame vbox. But start by adding any other buttons to the top
132 * level vbox. */
133 current_vbox = vbox;
135 for (i = 0; i < num_entries; i++)
137 if (entries[i].type == ANJUTA_COMMAND_BAR_ENTRY_BUTTON)
139 action = gtk_action_new (entries[i].action_name, _(entries[i].label),
140 _(entries[i].tooltip), entries[i].stock_icon);
141 button = gtk_button_new();
143 gtk_action_group_add_action (action_group, action);
145 gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
147 if (entries[i].stock_icon)
149 button_image = gtk_action_create_icon (action,
150 GTK_ICON_SIZE_BUTTON);
151 gtk_button_set_image (GTK_BUTTON (button), button_image);
154 gtk_activatable_set_related_action (GTK_ACTIVATABLE (button),
155 action);
156 gtk_activatable_set_use_action_appearance (GTK_ACTIVATABLE (button),
157 TRUE);
159 g_signal_connect (G_OBJECT (action), "activate",
160 entries[i].callback,
161 user_data);
163 /* Left-align button contents */
164 g_object_set (G_OBJECT (button), "xalign", 0.0, NULL);
166 gtk_box_pack_start (GTK_BOX (current_vbox), button, FALSE, FALSE,
169 else
171 frame_label_text = g_strdup_printf ("<b>%s</b>", _(entries[i].label));
172 frame_label = gtk_label_new (NULL);
173 frame = gtk_frame_new (NULL);
175 gtk_label_set_markup(GTK_LABEL (frame_label), frame_label_text);
176 gtk_frame_set_label_widget (GTK_FRAME (frame), frame_label);
178 g_free (frame_label_text);
180 frame_vbox = gtk_vbox_new (TRUE, 2);
182 g_object_set (G_OBJECT (frame), "shadow-type", GTK_SHADOW_NONE,
183 NULL);
185 gtk_container_add (GTK_CONTAINER (frame), frame_vbox);
186 gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 2);
188 current_vbox = frame_vbox;
192 gtk_widget_show_all (vbox);
193 gtk_notebook_append_page (GTK_NOTEBOOK (self), vbox, NULL);
197 * anjuta_command_bar_remove_action_group:
198 * @self: An AnjutaCommandBar
199 * @group_name: Name of the action group to remove
201 * Removes an action group from an AnjutaCommandBar.
203 void
204 anjuta_command_bar_remove_action_group (AnjutaCommandBar *self,
205 const gchar *group_name)
207 GtkWidget *page_widget;
208 int page_num;
210 page_widget = g_hash_table_lookup (self->priv->action_groups, group_name);
212 if (page_widget)
214 page_num = gtk_notebook_page_num (GTK_NOTEBOOK (self), page_widget);
216 gtk_notebook_remove_page (GTK_NOTEBOOK (self), page_num);
218 g_hash_table_remove (self->priv->action_groups, group_name);
219 g_hash_table_remove (self->priv->widgets, group_name);
221 else
222 g_warning ("Action group %s not found.", group_name);
227 * anjuta_command_bar_show_action_group:
228 * @self: An AnjutaCommandBar
229 * @group_name: The name of the action group to show
231 * Causes the actions in the given group to become visible, replacing the
232 * previously visible group.
234 void
235 anjuta_command_bar_show_action_group (AnjutaCommandBar *self,
236 const gchar *group_name)
238 GtkWidget *page_widget;
239 int page_num;
241 page_widget = g_hash_table_lookup (self->priv->widgets, group_name);
243 if (page_widget)
245 page_num = gtk_notebook_page_num (GTK_NOTEBOOK (self), page_widget);
247 gtk_notebook_set_current_page (GTK_NOTEBOOK (self), page_num);
249 else
250 g_warning ("Action group %s not found.", group_name);
255 * anjuta_command_bar_get_action_group:
256 * @self An AnjutaCommandBar
257 * @group_name: The name of the action group
259 * Returns the #GtkActionGroup with the given @group_name
261 GtkActionGroup *
262 anjuta_command_bar_get_action_group (AnjutaCommandBar *self,
263 const gchar *group_name)
265 GtkActionGroup *action_group;
267 action_group = g_hash_table_lookup (self->priv->action_groups, group_name);
269 if (!action_group)
270 g_warning ("Action group %s not found.", group_name);
272 return action_group;
276 * anjuta_command_bar_get_action:
277 * @self: An AnjutaCommandBar
278 * @group_name: The name of the #GtkActionGroup to look for the action in
279 * @action_name: The name of the action
281 * Retrieves a #GtkAction object in the given group with the given name
283 GtkAction *
284 anjuta_command_bar_get_action (AnjutaCommandBar *self, const gchar *group_name,
285 const gchar *action_name)
287 GtkActionGroup *action_group;
288 GtkAction *action;
290 action = NULL;
292 action_group = anjuta_command_bar_get_action_group (self, group_name);
294 if (action_group)
295 action = gtk_action_group_get_action (action_group, action_name);
297 return action;