~ubuntu-branches/ubuntu/wily/baobab/wily-proposed

« back to all changes in this revision

Viewing changes to libgd/libgd/gd-header-button.c

  • Committer: Package Import Robot
  • Author(s): Matthew Fischer
  • Date: 2013-05-23 19:36:13 UTC
  • mfrom: (1.1.13)
  • Revision ID: package-import@ubuntu.com-20130523193613-6wrnnsmgwidm86c3
Tags: 3.8.2-0ubuntu1
* New upstream release (LP: #1181921)
* Use a menu button instead of toolbar items
* Update to the latest libgd and egglistbox
* Add an High Contrast icon
* Use the newest libgd to animate tranistion to and from the results
  page
* Minor bugfixes

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2013 Red Hat, Inc.
 
3
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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
 
17
 *
 
18
 */
 
19
 
 
20
#include "gd-header-button.h"
 
21
 
 
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))
 
25
 
 
26
G_DEFINE_INTERFACE (GdHeaderButton, gd_header_button, GTK_TYPE_BUTTON)
 
27
 
 
28
enum {
 
29
  PROP_0,
 
30
  PROP_LABEL,
 
31
  PROP_USE_MARKUP,
 
32
  PROP_SYMBOLIC_ICON_NAME
 
33
};
 
34
 
 
35
static void
 
36
gd_header_button_default_init (GdHeaderButtonIface *iface)
 
37
{
 
38
  GParamSpec *pspec;
 
39
 
 
40
  /**
 
41
   * GdHeaderButton:label:
 
42
   *
 
43
   * The label of the #GdHeaderButton object.
 
44
   */
 
45
  pspec = g_param_spec_string ("label",
 
46
                               "Text label",
 
47
                               "Label displayed by the button",
 
48
                               NULL,
 
49
                               G_PARAM_READWRITE |
 
50
                               G_PARAM_STATIC_STRINGS);
 
51
  g_object_interface_install_property (iface, pspec);
 
52
 
 
53
  /**
 
54
   * GdHeaderButton:use-markup:
 
55
   *
 
56
   * Whether the label of the #GdHeaderButton object should use markup.
 
57
   */
 
58
  pspec = g_param_spec_boolean ("use-markup",
 
59
                                "Use markup",
 
60
                                "Whether the label should use markup",
 
61
                                FALSE,
 
62
                                G_PARAM_READWRITE |
 
63
                                G_PARAM_STATIC_STRINGS);
 
64
  g_object_interface_install_property (iface, pspec);
 
65
 
 
66
  /**
 
67
   * GdHeaderButton:symbolic-icon-name:
 
68
   *
 
69
   * The symbolic icon name of the #GdHeaderButton object.
 
70
   */
 
71
  pspec = g_param_spec_string ("symbolic-icon-name",
 
72
                               "Symbolic icon name",
 
73
                               "The name of the symbolic icon displayed by the button",
 
74
                               NULL,
 
75
                               G_PARAM_READWRITE |
 
76
                               G_PARAM_STATIC_STRINGS);
 
77
  g_object_interface_install_property (iface, pspec);
 
78
}
 
79
 
 
80
/**
 
81
 * gd_header_button_get_label:
 
82
 * @self:
 
83
 *
 
84
 * Returns: (transfer full):
 
85
 */
 
86
gchar *
 
87
gd_header_button_get_label (GdHeaderButton *self)
 
88
{
 
89
  gchar *label;
 
90
  g_object_get (self, "label", &label, NULL);
 
91
 
 
92
  return label;
 
93
}
 
94
 
 
95
/**
 
96
 * gd_header_button_set_label:
 
97
 * @self:
 
98
 * @label: (allow-none):
 
99
 *
 
100
 */
 
101
void
 
102
gd_header_button_set_label (GdHeaderButton *self,
 
103
                            const gchar    *label)
 
104
{
 
105
  g_object_set (self, "label", label, NULL);
 
106
}
 
107
 
 
108
/**
 
109
 * gd_header_button_get_symbolic_icon_name:
 
110
 * @self:
 
111
 *
 
112
 * Returns: (transfer full):
 
113
 */
 
114
gchar *
 
115
gd_header_button_get_symbolic_icon_name (GdHeaderButton *self)
 
116
{
 
117
  gchar *symbolic_icon_name;
 
118
  g_object_get (self, "symbolic-icon-name", &symbolic_icon_name, NULL);
 
119
 
 
120
  return symbolic_icon_name;
 
121
}
 
122
 
 
123
/**
 
124
 * gd_header_button_set_symbolic_icon_name:
 
125
 * @self:
 
126
 * @symbolic_icon_name: (allow-none):
 
127
 *
 
128
 */
 
129
void
 
130
gd_header_button_set_symbolic_icon_name (GdHeaderButton *self,
 
131
                                         const gchar    *symbolic_icon_name)
 
132
{
 
133
  if (symbolic_icon_name != NULL &&
 
134
      !g_str_has_suffix (symbolic_icon_name, "-symbolic"))
 
135
    {
 
136
      g_warning ("gd_header_button_set_symbolic_icon_name was called with "
 
137
                 "a non-symbolic name.");
 
138
      return;
 
139
    }
 
140
 
 
141
  g_object_set (self, "symbolic-icon-name", symbolic_icon_name, NULL);
 
142
}
 
143
 
 
144
/**
 
145
 * gd_header_button_get_use_markup:
 
146
 * @self:
 
147
 *
 
148
 * Returns:
 
149
 */
 
150
gboolean
 
151
gd_header_button_get_use_markup (GdHeaderButton *self)
 
152
{
 
153
  gboolean use_markup;
 
154
 
 
155
  g_object_get (self, "use-markup", &use_markup, NULL);
 
156
  return use_markup;
 
157
}
 
158
 
 
159
/**
 
160
 * gd_header_button_set_use_markup:
 
161
 * @self:
 
162
 * @use_markup:
 
163
 *
 
164
 */
 
165
void
 
166
gd_header_button_set_use_markup (GdHeaderButton *self,
 
167
                                 gboolean        use_markup)
 
168
{
 
169
  g_object_set (self, "use-markup", use_markup, NULL);
 
170
}
 
171
 
 
172
/* generic implementation for all private subclasses */
 
173
typedef struct _GdHeaderButtonPrivate GdHeaderButtonPrivate;
 
174
struct _GdHeaderButtonPrivate {
 
175
  gchar *label;
 
176
  gchar *symbolic_icon_name;
 
177
 
 
178
  gboolean use_markup;
 
179
};
 
180
 
 
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))
 
183
 
 
184
static void
 
185
rebuild_child (GdHeaderButton *self)
 
186
{
 
187
  GdHeaderButtonPrivate *priv = GET_PRIVATE (self);
 
188
  GtkStyleContext *context;
 
189
  GtkWidget *button_child, *label;
 
190
 
 
191
  gtk_widget_set_valign (GTK_WIDGET (self), GTK_ALIGN_CENTER);
 
192
 
 
193
  button_child = gtk_bin_get_child (GTK_BIN (self));
 
194
  if (button_child != NULL)
 
195
    gtk_widget_destroy (button_child);
 
196
 
 
197
  button_child = NULL;
 
198
  context = gtk_widget_get_style_context (GTK_WIDGET (self));
 
199
 
 
200
  if (priv->symbolic_icon_name != NULL)
 
201
    {
 
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);
 
205
 
 
206
      gtk_style_context_remove_class (context, "text-button");
 
207
      gtk_style_context_add_class (context, "image-button");
 
208
    }
 
209
  else if (priv->label != NULL)
 
210
    {
 
211
      label = gtk_label_new (priv->label);
 
212
      gtk_label_set_use_markup (GTK_LABEL (label), priv->use_markup);
 
213
 
 
214
      if (GTK_IS_MENU_BUTTON (self))
 
215
        {
 
216
          GtkWidget *arrow;
 
217
 
 
218
          button_child = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
 
219
          gtk_container_add (GTK_CONTAINER (button_child), label);
 
220
 
 
221
          arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
 
222
          gtk_container_add (GTK_CONTAINER (button_child), arrow);
 
223
        }
 
224
      else
 
225
        {
 
226
          button_child = label;
 
227
        }
 
228
 
 
229
      gtk_style_context_remove_class (context, "image-button");
 
230
      gtk_style_context_add_class (context, "text-button");
 
231
    }
 
232
 
 
233
  if (button_child)
 
234
    {
 
235
      gtk_widget_show_all (button_child);
 
236
      gtk_container_add (GTK_CONTAINER (self), button_child);
 
237
    }
 
238
}
 
239
 
 
240
static void
 
241
button_set_label (GdHeaderButton *self,
 
242
                  const gchar    *label)
 
243
{
 
244
  GdHeaderButtonPrivate *priv = GET_PRIVATE (self);
 
245
 
 
246
  if (g_strcmp0 (priv->label, label) != 0)
 
247
    {
 
248
      g_free (priv->label);
 
249
      priv->label = g_strdup (label);
 
250
 
 
251
      rebuild_child (self);
 
252
      g_object_notify (G_OBJECT (self), "label");
 
253
    }
 
254
}
 
255
 
 
256
static void
 
257
button_set_use_markup (GdHeaderButton *self,
 
258
                       gboolean        use_markup)
 
259
{
 
260
  GdHeaderButtonPrivate *priv = GET_PRIVATE (self);
 
261
 
 
262
  if (priv->use_markup != use_markup)
 
263
    {
 
264
      priv->use_markup = use_markup;
 
265
 
 
266
      rebuild_child (self);
 
267
      g_object_notify (G_OBJECT (self), "use-markup");
 
268
    }
 
269
}
 
270
 
 
271
static void
 
272
button_set_symbolic_icon_name (GdHeaderButton *self,
 
273
                               const gchar    *symbolic_icon_name)
 
274
{
 
275
  GdHeaderButtonPrivate *priv = GET_PRIVATE (self);
 
276
 
 
277
  if (g_strcmp0 (priv->symbolic_icon_name, symbolic_icon_name) != 0)
 
278
    {
 
279
      g_free (priv->symbolic_icon_name);
 
280
      priv->symbolic_icon_name = g_strdup (symbolic_icon_name);
 
281
 
 
282
      rebuild_child (self);
 
283
      g_object_notify (G_OBJECT (self), "symbolic-icon-name");
 
284
    }
 
285
}
 
286
 
 
287
static void
 
288
gd_header_button_generic_finalize (GObject *object)
 
289
{
 
290
  GdHeaderButton *self = GD_HEADER_BUTTON (object);
 
291
  GdHeaderButtonPrivate *priv = GET_PRIVATE (self);
 
292
 
 
293
  g_free (priv->label);
 
294
  g_free (priv->symbolic_icon_name);
 
295
 
 
296
  G_OBJECT_CLASS (GET_PARENT_CLASS (object))->finalize (object);
 
297
}
 
298
 
 
299
static void
 
300
gd_header_button_generic_set_property (GObject      *object,
 
301
                                       guint         prop_id,
 
302
                                       const GValue *value,
 
303
                                       GParamSpec   *pspec)
 
304
{
 
305
  GdHeaderButton *self = GD_HEADER_BUTTON (object);
 
306
 
 
307
  switch (prop_id)
 
308
    {
 
309
    case PROP_LABEL:
 
310
      button_set_label (self, g_value_get_string (value));
 
311
      break;
 
312
    case PROP_USE_MARKUP:
 
313
      button_set_use_markup (self, g_value_get_boolean (value));
 
314
      break;
 
315
    case PROP_SYMBOLIC_ICON_NAME:
 
316
      button_set_symbolic_icon_name (self, g_value_get_string (value));
 
317
      break;
 
318
    default:
 
319
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
320
      break;
 
321
    }
 
322
}
 
323
 
 
324
static void
 
325
gd_header_button_generic_get_property (GObject      *object,
 
326
                                       guint         prop_id,
 
327
                                       GValue       *value,
 
328
                                       GParamSpec   *pspec)
 
329
{
 
330
  GdHeaderButton *self = GD_HEADER_BUTTON (object);
 
331
  GdHeaderButtonPrivate *priv = GET_PRIVATE (self);
 
332
 
 
333
  switch (prop_id)
 
334
    {
 
335
    case PROP_LABEL:
 
336
      g_value_set_string (value, priv->label);
 
337
      break;
 
338
    case PROP_USE_MARKUP:
 
339
      g_value_set_boolean (value, priv->use_markup);
 
340
      break;
 
341
    case PROP_SYMBOLIC_ICON_NAME:
 
342
      g_value_set_string (value, priv->symbolic_icon_name);
 
343
      break;
 
344
    default:
 
345
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
346
      break;
 
347
    }
 
348
}
 
349
 
 
350
static void
 
351
gd_header_button_generic_iface_init (GdHeaderButtonIface *iface)
 
352
{
 
353
}
 
354
 
 
355
static void
 
356
gd_header_button_generic_class_init (gpointer klass)
 
357
{
 
358
  GObjectClass *oclass = G_OBJECT_CLASS (klass);
 
359
 
 
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;
 
363
 
 
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");
 
367
 
 
368
  g_type_class_add_private (klass, sizeof (GdHeaderButtonPrivate));
 
369
}
 
370
 
 
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))
 
375
 
 
376
static void
 
377
gd_header_simple_button_class_init (GdHeaderSimpleButtonClass *klass)
 
378
{
 
379
  gd_header_button_generic_class_init (klass);
 
380
}
 
381
 
 
382
static void
 
383
gd_header_simple_button_init (GdHeaderSimpleButton *self)
 
384
{
 
385
}
 
386
 
 
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))
 
390
 
 
391
static void
 
392
gd_header_toggle_button_class_init (GdHeaderToggleButtonClass *klass)
 
393
{
 
394
  gd_header_button_generic_class_init (klass);
 
395
}
 
396
 
 
397
static void
 
398
gd_header_toggle_button_init (GdHeaderToggleButton *self)
 
399
{
 
400
}
 
401
 
 
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))
 
405
 
 
406
static void
 
407
gd_header_menu_button_class_init (GdHeaderMenuButtonClass *klass)
 
408
{
 
409
  gd_header_button_generic_class_init (klass);
 
410
}
 
411
 
 
412
static void
 
413
gd_header_menu_button_init (GdHeaderMenuButton *self)
 
414
{
 
415
}
 
416
 
 
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))
 
420
 
 
421
static void
 
422
gd_header_radio_button_constructed (GObject *object)
 
423
{
 
424
  GdHeaderRadioButton *self = (GdHeaderRadioButton *) (object);
 
425
 
 
426
  G_OBJECT_CLASS (GET_PARENT_CLASS (object))->constructed (object);
 
427
 
 
428
  gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (self), FALSE);
 
429
}
 
430
 
 
431
static void
 
432
gd_header_radio_button_class_init (GdHeaderRadioButtonClass *klass)
 
433
{
 
434
  GObjectClass *oclass = G_OBJECT_CLASS (klass);
 
435
 
 
436
  oclass->constructed = gd_header_radio_button_constructed;
 
437
 
 
438
  gd_header_button_generic_class_init (klass);
 
439
}
 
440
 
 
441
static void
 
442
gd_header_radio_button_init (GdHeaderRadioButton *self)
 
443
{
 
444
}
 
445
 
 
446
/**
 
447
 * gd_header_simple_button_new:
 
448
 *
 
449
 * Returns: (transfer none):
 
450
 */
 
451
GtkWidget *
 
452
gd_header_simple_button_new (void)
 
453
{
 
454
  return g_object_new (GD_TYPE_HEADER_SIMPLE_BUTTON, NULL);
 
455
}
 
456
 
 
457
/**
 
458
 * gd_header_toggle_button_new:
 
459
 *
 
460
 * Returns: (transfer none):
 
461
 */
 
462
GtkWidget *
 
463
gd_header_toggle_button_new (void)
 
464
{
 
465
  return g_object_new (GD_TYPE_HEADER_TOGGLE_BUTTON, NULL);
 
466
}
 
467
 
 
468
/**
 
469
 * gd_header_radio_button_new:
 
470
 *
 
471
 * Returns: (transfer none):
 
472
 */
 
473
GtkWidget *
 
474
gd_header_radio_button_new (void)
 
475
{
 
476
  return g_object_new (GD_TYPE_HEADER_RADIO_BUTTON, NULL);
 
477
}
 
478
 
 
479
/**
 
480
 * gd_header_menu_button_new:
 
481
 *
 
482
 * Returns: (transfer none):
 
483
 */
 
484
GtkWidget *
 
485
gd_header_menu_button_new (void)
 
486
{
 
487
  return g_object_new (GD_TYPE_HEADER_MENU_BUTTON, NULL);
 
488
}