2
* Copyright (c) 2013 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
20
#include "gd-header-button.h"
22
typedef GTypeInterface GdHeaderButtonIface;
23
typedef GdHeaderButtonIface GdHeaderButtonInterface;
24
#define GD_HEADER_BUTTON_GET_IFACE(inst) (G_TYPE_INSTANCE_GET_INTERFACE ((inst), GD_TYPE_HEADER_BUTTON, GdHeaderButtonIface))
26
G_DEFINE_INTERFACE (GdHeaderButton, gd_header_button, GTK_TYPE_BUTTON)
32
PROP_SYMBOLIC_ICON_NAME
36
gd_header_button_default_init (GdHeaderButtonIface *iface)
41
* GdHeaderButton:label:
43
* The label of the #GdHeaderButton object.
45
pspec = g_param_spec_string ("label",
47
"Label displayed by the button",
50
G_PARAM_STATIC_STRINGS);
51
g_object_interface_install_property (iface, pspec);
54
* GdHeaderButton:use-markup:
56
* Whether the label of the #GdHeaderButton object should use markup.
58
pspec = g_param_spec_boolean ("use-markup",
60
"Whether the label should use markup",
63
G_PARAM_STATIC_STRINGS);
64
g_object_interface_install_property (iface, pspec);
67
* GdHeaderButton:symbolic-icon-name:
69
* The symbolic icon name of the #GdHeaderButton object.
71
pspec = g_param_spec_string ("symbolic-icon-name",
73
"The name of the symbolic icon displayed by the button",
76
G_PARAM_STATIC_STRINGS);
77
g_object_interface_install_property (iface, pspec);
81
* gd_header_button_get_label:
84
* Returns: (transfer full):
87
gd_header_button_get_label (GdHeaderButton *self)
90
g_object_get (self, "label", &label, NULL);
96
* gd_header_button_set_label:
98
* @label: (allow-none):
102
gd_header_button_set_label (GdHeaderButton *self,
105
g_object_set (self, "label", label, NULL);
109
* gd_header_button_get_symbolic_icon_name:
112
* Returns: (transfer full):
115
gd_header_button_get_symbolic_icon_name (GdHeaderButton *self)
117
gchar *symbolic_icon_name;
118
g_object_get (self, "symbolic-icon-name", &symbolic_icon_name, NULL);
120
return symbolic_icon_name;
124
* gd_header_button_set_symbolic_icon_name:
126
* @symbolic_icon_name: (allow-none):
130
gd_header_button_set_symbolic_icon_name (GdHeaderButton *self,
131
const gchar *symbolic_icon_name)
133
if (symbolic_icon_name != NULL &&
134
!g_str_has_suffix (symbolic_icon_name, "-symbolic"))
136
g_warning ("gd_header_button_set_symbolic_icon_name was called with "
137
"a non-symbolic name.");
141
g_object_set (self, "symbolic-icon-name", symbolic_icon_name, NULL);
145
* gd_header_button_get_use_markup:
151
gd_header_button_get_use_markup (GdHeaderButton *self)
155
g_object_get (self, "use-markup", &use_markup, NULL);
160
* gd_header_button_set_use_markup:
166
gd_header_button_set_use_markup (GdHeaderButton *self,
169
g_object_set (self, "use-markup", use_markup, NULL);
172
/* generic implementation for all private subclasses */
173
typedef struct _GdHeaderButtonPrivate GdHeaderButtonPrivate;
174
struct _GdHeaderButtonPrivate {
176
gchar *symbolic_icon_name;
181
#define GET_PRIVATE(inst) G_TYPE_INSTANCE_GET_PRIVATE (inst, G_OBJECT_TYPE (inst), GdHeaderButtonPrivate)
182
#define GET_PARENT_CLASS(inst) g_type_class_peek_parent (G_OBJECT_GET_CLASS (inst))
185
rebuild_child (GdHeaderButton *self)
187
GdHeaderButtonPrivate *priv = GET_PRIVATE (self);
188
GtkStyleContext *context;
189
GtkWidget *button_child, *label;
191
gtk_widget_set_valign (GTK_WIDGET (self), GTK_ALIGN_CENTER);
193
button_child = gtk_bin_get_child (GTK_BIN (self));
194
if (button_child != NULL)
195
gtk_widget_destroy (button_child);
198
context = gtk_widget_get_style_context (GTK_WIDGET (self));
200
if (priv->symbolic_icon_name != NULL)
202
button_child = gtk_image_new_from_icon_name (priv->symbolic_icon_name, GTK_ICON_SIZE_MENU);
203
if (priv->label != NULL)
204
gtk_widget_set_tooltip_text (GTK_WIDGET (self), priv->label);
206
gtk_style_context_remove_class (context, "text-button");
207
gtk_style_context_add_class (context, "image-button");
209
else if (priv->label != NULL)
211
label = gtk_label_new (priv->label);
212
gtk_label_set_use_markup (GTK_LABEL (label), priv->use_markup);
214
if (GTK_IS_MENU_BUTTON (self))
218
button_child = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
219
gtk_container_add (GTK_CONTAINER (button_child), label);
221
arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
222
gtk_container_add (GTK_CONTAINER (button_child), arrow);
226
button_child = label;
229
gtk_style_context_remove_class (context, "image-button");
230
gtk_style_context_add_class (context, "text-button");
235
gtk_widget_show_all (button_child);
236
gtk_container_add (GTK_CONTAINER (self), button_child);
241
button_set_label (GdHeaderButton *self,
244
GdHeaderButtonPrivate *priv = GET_PRIVATE (self);
246
if (g_strcmp0 (priv->label, label) != 0)
248
g_free (priv->label);
249
priv->label = g_strdup (label);
251
rebuild_child (self);
252
g_object_notify (G_OBJECT (self), "label");
257
button_set_use_markup (GdHeaderButton *self,
260
GdHeaderButtonPrivate *priv = GET_PRIVATE (self);
262
if (priv->use_markup != use_markup)
264
priv->use_markup = use_markup;
266
rebuild_child (self);
267
g_object_notify (G_OBJECT (self), "use-markup");
272
button_set_symbolic_icon_name (GdHeaderButton *self,
273
const gchar *symbolic_icon_name)
275
GdHeaderButtonPrivate *priv = GET_PRIVATE (self);
277
if (g_strcmp0 (priv->symbolic_icon_name, symbolic_icon_name) != 0)
279
g_free (priv->symbolic_icon_name);
280
priv->symbolic_icon_name = g_strdup (symbolic_icon_name);
282
rebuild_child (self);
283
g_object_notify (G_OBJECT (self), "symbolic-icon-name");
288
gd_header_button_generic_finalize (GObject *object)
290
GdHeaderButton *self = GD_HEADER_BUTTON (object);
291
GdHeaderButtonPrivate *priv = GET_PRIVATE (self);
293
g_free (priv->label);
294
g_free (priv->symbolic_icon_name);
296
G_OBJECT_CLASS (GET_PARENT_CLASS (object))->finalize (object);
300
gd_header_button_generic_set_property (GObject *object,
305
GdHeaderButton *self = GD_HEADER_BUTTON (object);
310
button_set_label (self, g_value_get_string (value));
312
case PROP_USE_MARKUP:
313
button_set_use_markup (self, g_value_get_boolean (value));
315
case PROP_SYMBOLIC_ICON_NAME:
316
button_set_symbolic_icon_name (self, g_value_get_string (value));
319
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
325
gd_header_button_generic_get_property (GObject *object,
330
GdHeaderButton *self = GD_HEADER_BUTTON (object);
331
GdHeaderButtonPrivate *priv = GET_PRIVATE (self);
336
g_value_set_string (value, priv->label);
338
case PROP_USE_MARKUP:
339
g_value_set_boolean (value, priv->use_markup);
341
case PROP_SYMBOLIC_ICON_NAME:
342
g_value_set_string (value, priv->symbolic_icon_name);
345
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
351
gd_header_button_generic_iface_init (GdHeaderButtonIface *iface)
356
gd_header_button_generic_class_init (gpointer klass)
358
GObjectClass *oclass = G_OBJECT_CLASS (klass);
360
oclass->get_property = gd_header_button_generic_get_property;
361
oclass->set_property = gd_header_button_generic_set_property;
362
oclass->finalize = gd_header_button_generic_finalize;
364
g_object_class_override_property (oclass, PROP_LABEL, "label");
365
g_object_class_override_property (oclass, PROP_USE_MARKUP, "use-markup");
366
g_object_class_override_property (oclass, PROP_SYMBOLIC_ICON_NAME, "symbolic-icon-name");
368
g_type_class_add_private (klass, sizeof (GdHeaderButtonPrivate));
371
/* private subclasses */
372
typedef GtkButtonClass GdHeaderSimpleButtonClass;
373
G_DEFINE_TYPE_WITH_CODE (GdHeaderSimpleButton, gd_header_simple_button, GTK_TYPE_BUTTON,
374
G_IMPLEMENT_INTERFACE (GD_TYPE_HEADER_BUTTON, gd_header_button_generic_iface_init))
377
gd_header_simple_button_class_init (GdHeaderSimpleButtonClass *klass)
379
gd_header_button_generic_class_init (klass);
383
gd_header_simple_button_init (GdHeaderSimpleButton *self)
387
typedef GtkToggleButtonClass GdHeaderToggleButtonClass;
388
G_DEFINE_TYPE_WITH_CODE (GdHeaderToggleButton, gd_header_toggle_button, GTK_TYPE_TOGGLE_BUTTON,
389
G_IMPLEMENT_INTERFACE (GD_TYPE_HEADER_BUTTON, gd_header_button_generic_iface_init))
392
gd_header_toggle_button_class_init (GdHeaderToggleButtonClass *klass)
394
gd_header_button_generic_class_init (klass);
398
gd_header_toggle_button_init (GdHeaderToggleButton *self)
402
typedef GtkMenuButtonClass GdHeaderMenuButtonClass;
403
G_DEFINE_TYPE_WITH_CODE (GdHeaderMenuButton, gd_header_menu_button, GTK_TYPE_MENU_BUTTON,
404
G_IMPLEMENT_INTERFACE (GD_TYPE_HEADER_BUTTON, gd_header_button_generic_iface_init))
407
gd_header_menu_button_class_init (GdHeaderMenuButtonClass *klass)
409
gd_header_button_generic_class_init (klass);
413
gd_header_menu_button_init (GdHeaderMenuButton *self)
417
typedef GtkRadioButtonClass GdHeaderRadioButtonClass;
418
G_DEFINE_TYPE_WITH_CODE (GdHeaderRadioButton, gd_header_radio_button, GTK_TYPE_RADIO_BUTTON,
419
G_IMPLEMENT_INTERFACE (GD_TYPE_HEADER_BUTTON, gd_header_button_generic_iface_init))
422
gd_header_radio_button_constructed (GObject *object)
424
GdHeaderRadioButton *self = (GdHeaderRadioButton *) (object);
426
G_OBJECT_CLASS (GET_PARENT_CLASS (object))->constructed (object);
428
gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (self), FALSE);
432
gd_header_radio_button_class_init (GdHeaderRadioButtonClass *klass)
434
GObjectClass *oclass = G_OBJECT_CLASS (klass);
436
oclass->constructed = gd_header_radio_button_constructed;
438
gd_header_button_generic_class_init (klass);
442
gd_header_radio_button_init (GdHeaderRadioButton *self)
447
* gd_header_simple_button_new:
449
* Returns: (transfer none):
452
gd_header_simple_button_new (void)
454
return g_object_new (GD_TYPE_HEADER_SIMPLE_BUTTON, NULL);
458
* gd_header_toggle_button_new:
460
* Returns: (transfer none):
463
gd_header_toggle_button_new (void)
465
return g_object_new (GD_TYPE_HEADER_TOGGLE_BUTTON, NULL);
469
* gd_header_radio_button_new:
471
* Returns: (transfer none):
474
gd_header_radio_button_new (void)
476
return g_object_new (GD_TYPE_HEADER_RADIO_BUTTON, NULL);
480
* gd_header_menu_button_new:
482
* Returns: (transfer none):
485
gd_header_menu_button_new (void)
487
return g_object_new (GD_TYPE_HEADER_MENU_BUTTON, NULL);