2
* Copyright (c) 2011 Red Hat, Inc.
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU Lesser General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or (at your
7
* option) any later version.
9
* This program is distributed in the hope that it will be useful, but
10
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
12
* License for more details.
14
* You should have received a copy of the GNU Lesser General Public License
15
* along with this program; if not, write to the Free Software Foundation,
16
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
* Author: Cosimo Cecchi <cosimoc@redhat.com>
22
#include "gd-main-toolbar.h"
25
#include <glib/gi18n.h>
27
G_DEFINE_TYPE (GdMainToolbar, gd_main_toolbar, GTK_TYPE_TOOLBAR)
35
struct _GdMainToolbarPrivate {
36
GtkSizeGroup *size_group;
37
GtkSizeGroup *vertical_size_group;
39
GtkToolItem *left_group;
40
GtkToolItem *center_group;
41
GtkToolItem *right_group;
45
GtkWidget *labels_grid;
46
GtkWidget *title_label;
47
GtkWidget *detail_label;
49
GtkWidget *center_menu;
50
GtkWidget *center_menu_child;
52
GtkWidget *right_grid;
56
gd_main_toolbar_dispose (GObject *obj)
58
GdMainToolbar *self = GD_MAIN_TOOLBAR (obj);
60
g_clear_object (&self->priv->size_group);
61
g_clear_object (&self->priv->vertical_size_group);
63
G_OBJECT_CLASS (gd_main_toolbar_parent_class)->dispose (obj);
67
get_icon_margin (void)
69
gint toolbar_size, menu_size;
71
gtk_icon_size_lookup (GTK_ICON_SIZE_MENU, &menu_size, NULL);
72
gtk_icon_size_lookup (GTK_ICON_SIZE_LARGE_TOOLBAR, &toolbar_size, NULL);
73
return (gint) floor ((toolbar_size - menu_size) / 2.0);
77
get_vertical_size_group (void)
83
icon_margin = get_icon_margin ();
85
dummy = gtk_toggle_button_new ();
86
w = gtk_image_new_from_stock (GTK_STOCK_OPEN, GTK_ICON_SIZE_MENU);
87
g_object_set (w, "margin", icon_margin, NULL);
88
gtk_container_add (GTK_CONTAINER (dummy), w);
89
gtk_widget_show_all (dummy);
91
retval = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
92
gtk_size_group_add_widget (retval, dummy);
98
get_empty_button (ChildType type)
105
button = gtk_menu_button_new ();
108
button = gtk_toggle_button_new ();
112
button = gtk_button_new ();
120
get_symbolic_button (const gchar *icon_name,
123
GtkWidget *button, *w;
128
button = gtk_menu_button_new ();
129
gtk_widget_destroy (gtk_bin_get_child (GTK_BIN (button)));
132
button = gtk_toggle_button_new ();
136
button = gtk_button_new ();
140
gtk_style_context_add_class (gtk_widget_get_style_context (button), "raised");
142
w = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
143
g_object_set (w, "margin", get_icon_margin (), NULL);
145
gtk_container_add (GTK_CONTAINER (button), w);
151
get_text_button (const gchar *label,
154
GtkWidget *button, *w;
159
button = gtk_menu_button_new ();
160
gtk_widget_destroy (gtk_bin_get_child (GTK_BIN (button)));
162
w = gtk_label_new (label);
164
gtk_container_add (GTK_CONTAINER (button), w);
167
button = gtk_toggle_button_new_with_label (label);
171
button = gtk_button_new_with_label (label);
175
gtk_widget_set_vexpand (button, TRUE);
176
gtk_style_context_add_class (gtk_widget_get_style_context (button), "raised");
182
gd_main_toolbar_constructed (GObject *obj)
184
GdMainToolbar *self = GD_MAIN_TOOLBAR (obj);
185
GtkToolbar *tb = GTK_TOOLBAR (obj);
188
G_OBJECT_CLASS (gd_main_toolbar_parent_class)->constructed (obj);
190
self->priv->vertical_size_group = get_vertical_size_group ();
193
self->priv->left_group = gtk_tool_item_new ();
194
gtk_widget_set_margin_right (GTK_WIDGET (self->priv->left_group), 12);
195
gtk_toolbar_insert (tb, self->priv->left_group, -1);
196
gtk_size_group_add_widget (self->priv->vertical_size_group,
197
GTK_WIDGET (self->priv->left_group));
199
/* left button group */
200
self->priv->left_grid = gtk_grid_new ();
201
gtk_grid_set_column_spacing (GTK_GRID (self->priv->left_grid), 12);
202
gtk_container_add (GTK_CONTAINER (self->priv->left_group), self->priv->left_grid);
203
gtk_widget_set_halign (self->priv->left_grid, GTK_ALIGN_START);
206
self->priv->center_group = gtk_tool_item_new ();
207
gtk_tool_item_set_expand (self->priv->center_group, TRUE);
208
gtk_toolbar_insert (tb, self->priv->center_group, -1);
209
gtk_size_group_add_widget (self->priv->vertical_size_group,
210
GTK_WIDGET (self->priv->center_group));
212
/* centered label group */
213
self->priv->labels_grid = grid = gtk_grid_new ();
214
gtk_widget_set_halign (grid, GTK_ALIGN_CENTER);
215
gtk_widget_set_valign (grid, GTK_ALIGN_CENTER);
216
gtk_grid_set_column_spacing (GTK_GRID (grid), 12);
217
gtk_container_add (GTK_CONTAINER (self->priv->center_group), grid);
219
self->priv->title_label = gtk_label_new (NULL);
220
gtk_label_set_ellipsize (GTK_LABEL (self->priv->title_label), PANGO_ELLIPSIZE_END);
221
gtk_container_add (GTK_CONTAINER (grid), self->priv->title_label);
223
self->priv->detail_label = gtk_label_new (NULL);
224
gtk_widget_set_no_show_all (self->priv->detail_label, TRUE);
225
gtk_style_context_add_class (gtk_widget_get_style_context (self->priv->detail_label), "dim-label");
226
gtk_container_add (GTK_CONTAINER (grid), self->priv->detail_label);
229
self->priv->right_group = gtk_tool_item_new ();
230
gtk_widget_set_margin_left (GTK_WIDGET (self->priv->right_group), 12);
231
gtk_toolbar_insert (tb, self->priv->right_group, -1);
232
gtk_size_group_add_widget (self->priv->vertical_size_group,
233
GTK_WIDGET (self->priv->right_group));
235
self->priv->right_grid = gtk_grid_new ();
236
gtk_grid_set_column_spacing (GTK_GRID (self->priv->right_grid), 12);
237
gtk_container_add (GTK_CONTAINER (self->priv->right_group), self->priv->right_grid);
238
gtk_widget_set_halign (self->priv->right_grid, GTK_ALIGN_END);
240
self->priv->size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
241
gtk_size_group_add_widget (self->priv->size_group, GTK_WIDGET (self->priv->left_group));
242
gtk_size_group_add_widget (self->priv->size_group, GTK_WIDGET (self->priv->right_group));
246
gd_main_toolbar_init (GdMainToolbar *self)
248
self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GD_TYPE_MAIN_TOOLBAR, GdMainToolbarPrivate);
252
gd_main_toolbar_class_init (GdMainToolbarClass *klass)
254
GObjectClass *oclass;
256
oclass = G_OBJECT_CLASS (klass);
257
oclass->constructed = gd_main_toolbar_constructed;
258
oclass->dispose = gd_main_toolbar_dispose;
260
g_type_class_add_private (klass, sizeof (GdMainToolbarPrivate));
264
gd_main_toolbar_clear (GdMainToolbar *self)
267
gtk_label_set_text (GTK_LABEL (self->priv->title_label), "");
268
gtk_label_set_text (GTK_LABEL (self->priv->detail_label), "");
270
/* clear all added buttons */
271
gtk_container_foreach (GTK_CONTAINER (self->priv->left_grid),
272
(GtkCallback) gtk_widget_destroy, self);
273
gtk_container_foreach (GTK_CONTAINER (self->priv->right_grid),
274
(GtkCallback) gtk_widget_destroy, self);
278
* gd_main_toolbar_set_labels:
280
* @primary: (allow-none):
281
* @detail: (allow-none):
285
gd_main_toolbar_set_labels (GdMainToolbar *self,
286
const gchar *primary,
289
gchar *real_primary = NULL;
292
real_primary = g_markup_printf_escaped ("<b>%s</b>", primary);
294
if (real_primary == NULL)
296
gtk_label_set_markup (GTK_LABEL (self->priv->title_label), "");
297
gtk_widget_hide (self->priv->title_label);
301
gtk_label_set_markup (GTK_LABEL (self->priv->title_label), real_primary);
302
gtk_widget_show (self->priv->title_label);
307
gtk_label_set_text (GTK_LABEL (self->priv->detail_label), "");
308
gtk_widget_hide (self->priv->detail_label);
312
gtk_label_set_text (GTK_LABEL (self->priv->detail_label), detail);
313
gtk_widget_show (self->priv->detail_label);
316
g_free (real_primary);
320
gd_main_toolbar_new (void)
322
return g_object_new (GD_TYPE_MAIN_TOOLBAR, NULL);
326
add_button_internal (GdMainToolbar *self,
327
const gchar *icon_name,
334
if (icon_name != NULL)
336
button = get_symbolic_button (icon_name, type);
338
gtk_widget_set_tooltip_text (button, label);
340
else if (label != NULL)
342
button = get_text_button (label, type);
346
button = get_empty_button (type);
349
gd_main_toolbar_add_widget (self, button, pack_start);
351
gtk_widget_show_all (button);
357
* gd_main_toolbar_set_labels_menu:
359
* @menu: (allow-none):
363
gd_main_toolbar_set_labels_menu (GdMainToolbar *self,
366
GtkWidget *button, *grid, *w;
369
((gtk_widget_get_parent (self->priv->labels_grid) == GTK_WIDGET (self->priv->center_group)) ||
370
self->priv->center_menu_child == NULL))
375
g_object_ref (self->priv->labels_grid);
376
gtk_container_remove (GTK_CONTAINER (self->priv->center_group),
377
self->priv->labels_grid);
379
self->priv->center_menu_child = grid = gtk_grid_new ();
380
gtk_grid_set_column_spacing (GTK_GRID (grid), 6);
381
gtk_container_add (GTK_CONTAINER (grid), self->priv->labels_grid);
382
g_object_unref (self->priv->labels_grid);
384
w = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
385
gtk_container_add (GTK_CONTAINER (grid), w);
387
self->priv->center_menu = button = gtk_menu_button_new ();
388
gtk_style_context_add_class (gtk_widget_get_style_context (self->priv->center_menu),
390
gtk_widget_destroy (gtk_bin_get_child (GTK_BIN (button)));
391
gtk_widget_set_halign (button, GTK_ALIGN_CENTER);
392
gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (button), menu);
393
gtk_container_add (GTK_CONTAINER (self->priv->center_menu), grid);
395
gtk_container_add (GTK_CONTAINER (self->priv->center_group), button);
399
g_object_ref (self->priv->labels_grid);
400
gtk_container_remove (GTK_CONTAINER (self->priv->center_menu_child),
401
self->priv->labels_grid);
402
gtk_widget_destroy (self->priv->center_menu);
404
self->priv->center_menu = NULL;
405
self->priv->center_menu_child = NULL;
407
gtk_container_add (GTK_CONTAINER (self->priv->center_group),
408
self->priv->labels_grid);
409
g_object_unref (self->priv->labels_grid);
412
gtk_widget_show_all (GTK_WIDGET (self->priv->center_group));
416
* gd_main_toolbar_add_button:
418
* @icon_name: (allow-none):
419
* @label: (allow-none):
422
* Returns: (transfer none):
425
gd_main_toolbar_add_button (GdMainToolbar *self,
426
const gchar *icon_name,
430
return add_button_internal (self, icon_name, label, pack_start, CHILD_NORMAL);
434
* gd_main_toolbar_add_menu:
436
* @icon_name: (allow-none):
437
* @label: (allow-none):
440
* Returns: (transfer none):
443
gd_main_toolbar_add_menu (GdMainToolbar *self,
444
const gchar *icon_name,
448
return add_button_internal (self, icon_name, label, pack_start, CHILD_MENU);
452
* gd_main_toolbar_add_toggle:
454
* @icon_name: (allow-none):
455
* @label: (allow-none):
458
* Returns: (transfer none):
461
gd_main_toolbar_add_toggle (GdMainToolbar *self,
462
const gchar *icon_name,
466
return add_button_internal (self, icon_name, label, pack_start, CHILD_TOGGLE);
470
* gd_main_toolbar_add_widget:
477
gd_main_toolbar_add_widget (GdMainToolbar *self,
482
gtk_container_add (GTK_CONTAINER (self->priv->left_grid), widget);
484
gtk_container_add (GTK_CONTAINER (self->priv->right_grid), widget);