~ubuntu-branches/ubuntu/precise/gnome-control-center/precise-updates

« back to all changes in this revision

Viewing changes to panels/user-accounts/um-editable-button.c

Tags: upstream-3.0.1.1
ImportĀ upstreamĀ versionĀ 3.0.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 
2
 *
 
3
 * Copyright 2009-2010  Red Hat, Inc,
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 3 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
18
 *
 
19
 * Written by: Matthias Clasen <mclasen@redhat.com>
 
20
 */
 
21
 
 
22
#include <gdk/gdkkeysyms.h>
 
23
#include "um-editable-button.h"
 
24
 
 
25
#define EMPTY_TEXT "\xe2\x80\x94"
 
26
 
 
27
struct _UmEditableButtonPrivate {
 
28
        GtkNotebook *notebook;
 
29
        GtkLabel    *label;
 
30
        GtkButton   *button;
 
31
 
 
32
        gchar *text;
 
33
        gboolean editable;
 
34
        gint weight;
 
35
        gboolean weight_set;
 
36
        gdouble scale;
 
37
        gboolean scale_set;
 
38
};
 
39
 
 
40
#define UM_EDITABLE_BUTTON_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), UM_TYPE_EDITABLE_BUTTON, UmEditableButtonPrivate))
 
41
 
 
42
enum {
 
43
        PROP_0,
 
44
        PROP_TEXT,
 
45
        PROP_EDITABLE,
 
46
        PROP_SCALE,
 
47
        PROP_SCALE_SET,
 
48
        PROP_WEIGHT,
 
49
        PROP_WEIGHT_SET
 
50
};
 
51
 
 
52
enum {
 
53
        START_EDITING,
 
54
        LAST_SIGNAL
 
55
};
 
56
 
 
57
static guint signals [LAST_SIGNAL] = { 0, };
 
58
 
 
59
G_DEFINE_TYPE (UmEditableButton, um_editable_button, GTK_TYPE_ALIGNMENT);
 
60
 
 
61
void
 
62
um_editable_button_set_text (UmEditableButton *button,
 
63
                             const gchar      *text)
 
64
{
 
65
        UmEditableButtonPrivate *priv;
 
66
        gchar *tmp;
 
67
        GtkWidget *label;
 
68
 
 
69
        priv = button->priv;
 
70
 
 
71
        tmp = g_strdup (text);
 
72
        g_free (priv->text);
 
73
        priv->text = tmp;
 
74
 
 
75
        if (tmp == NULL || tmp[0] == '\0')
 
76
                tmp = EMPTY_TEXT;
 
77
 
 
78
        gtk_label_set_text (priv->label, tmp);
 
79
        label = gtk_bin_get_child (GTK_BIN (priv->button));
 
80
        gtk_label_set_text (GTK_LABEL (label), tmp);
 
81
 
 
82
        g_object_notify (G_OBJECT (button), "text");
 
83
}
 
84
 
 
85
const gchar *
 
86
um_editable_button_get_text (UmEditableButton *button)
 
87
{
 
88
        return button->priv->text;
 
89
}
 
90
 
 
91
void
 
92
um_editable_button_set_editable (UmEditableButton *button,
 
93
                                 gboolean          editable)
 
94
{
 
95
        UmEditableButtonPrivate *priv;
 
96
 
 
97
        priv = button->priv;
 
98
 
 
99
        if (priv->editable != editable) {
 
100
                priv->editable = editable;
 
101
 
 
102
                gtk_notebook_set_current_page (priv->notebook, editable ? 1 : 0);
 
103
 
 
104
                g_object_notify (G_OBJECT (button), "editable");
 
105
        }
 
106
}
 
107
 
 
108
gboolean
 
109
um_editable_button_get_editable (UmEditableButton *button)
 
110
{
 
111
        return button->priv->editable;
 
112
}
 
113
 
 
114
static void
 
115
update_fonts (UmEditableButton *button)
 
116
{
 
117
        PangoAttrList *attrs;
 
118
        PangoAttribute *attr;
 
119
        GtkWidget *label;
 
120
 
 
121
        UmEditableButtonPrivate *priv = button->priv;
 
122
 
 
123
        attrs = pango_attr_list_new ();
 
124
        if (priv->scale_set) {
 
125
                attr = pango_attr_scale_new (priv->scale);
 
126
                pango_attr_list_insert (attrs, attr);
 
127
        }
 
128
        if (priv->weight_set) {
 
129
                attr = pango_attr_weight_new (priv->weight);
 
130
                pango_attr_list_insert (attrs, attr);
 
131
        }
 
132
 
 
133
        gtk_label_set_attributes (priv->label, attrs);
 
134
 
 
135
        label = gtk_bin_get_child (GTK_BIN (priv->button));
 
136
        gtk_label_set_attributes (GTK_LABEL (label), attrs);
 
137
 
 
138
        pango_attr_list_unref (attrs);
 
139
}
 
140
 
 
141
void
 
142
um_editable_button_set_weight (UmEditableButton *button,
 
143
                               gint              weight)
 
144
{
 
145
        UmEditableButtonPrivate *priv = button->priv;
 
146
 
 
147
        if (priv->weight == weight && priv->weight_set)
 
148
                return;
 
149
 
 
150
        priv->weight = weight;
 
151
        priv->weight_set = TRUE;
 
152
 
 
153
        update_fonts (button);
 
154
 
 
155
        g_object_notify (G_OBJECT (button), "weight");
 
156
        g_object_notify (G_OBJECT (button), "weight-set");
 
157
}
 
158
 
 
159
gint
 
160
um_editable_button_get_weight (UmEditableButton *button)
 
161
{
 
162
        return button->priv->weight;
 
163
}
 
164
 
 
165
void
 
166
um_editable_button_set_scale (UmEditableButton *button,
 
167
                              gdouble           scale)
 
168
{
 
169
        UmEditableButtonPrivate *priv = button->priv;
 
170
 
 
171
        if (priv->scale == scale && priv->scale_set)
 
172
                return;
 
173
 
 
174
        priv->scale = scale;
 
175
        priv->scale_set = TRUE;
 
176
 
 
177
        update_fonts (button);
 
178
 
 
179
        g_object_notify (G_OBJECT (button), "scale");
 
180
        g_object_notify (G_OBJECT (button), "scale-set");
 
181
}
 
182
 
 
183
gdouble
 
184
um_editable_button_get_scale (UmEditableButton *button)
 
185
{
 
186
        return button->priv->scale;
 
187
}
 
188
 
 
189
static void
 
190
um_editable_button_set_property (GObject      *object,
 
191
                                 guint         prop_id,
 
192
                                 const GValue *value,
 
193
                                 GParamSpec   *pspec)
 
194
{
 
195
        UmEditableButton *button = UM_EDITABLE_BUTTON (object);
 
196
 
 
197
        switch (prop_id) {
 
198
        case PROP_TEXT:
 
199
                um_editable_button_set_text (button, g_value_get_string (value));
 
200
                break;
 
201
        case PROP_EDITABLE:
 
202
                um_editable_button_set_editable (button, g_value_get_boolean (value));
 
203
                break;
 
204
        case PROP_WEIGHT:
 
205
                um_editable_button_set_weight (button, g_value_get_int (value));
 
206
                break;
 
207
        case PROP_WEIGHT_SET:
 
208
                button->priv->weight_set = g_value_get_boolean (value);
 
209
                break;
 
210
        case PROP_SCALE:
 
211
                um_editable_button_set_scale (button, g_value_get_double (value));
 
212
                break;
 
213
        case PROP_SCALE_SET:
 
214
                button->priv->scale_set = g_value_get_boolean (value);
 
215
                break;
 
216
        default:
 
217
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
218
                break;
 
219
        }
 
220
}
 
221
 
 
222
static void
 
223
um_editable_button_get_property (GObject    *object,
 
224
                                 guint       prop_id,
 
225
                                 GValue     *value,
 
226
                                 GParamSpec *pspec)
 
227
{
 
228
        UmEditableButton *button = UM_EDITABLE_BUTTON (object);
 
229
 
 
230
        switch (prop_id) {
 
231
        case PROP_TEXT:
 
232
                g_value_set_string (value,
 
233
                                    um_editable_button_get_text (button));
 
234
                break;
 
235
        case PROP_EDITABLE:
 
236
                g_value_set_boolean (value,
 
237
                                     um_editable_button_get_editable (button));
 
238
                break;
 
239
        case PROP_WEIGHT:
 
240
                g_value_set_int (value,
 
241
                                 um_editable_button_get_weight (button));
 
242
                break;
 
243
        case PROP_WEIGHT_SET:
 
244
                g_value_set_boolean (value,
 
245
                                     button->priv->weight_set);
 
246
                break;
 
247
        case PROP_SCALE:
 
248
                g_value_set_double (value,
 
249
                                    um_editable_button_get_scale (button));
 
250
                break;
 
251
        case PROP_SCALE_SET:
 
252
                g_value_set_boolean (value,
 
253
                                     button->priv->scale_set);
 
254
                break;
 
255
        default:
 
256
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
257
                break;
 
258
        }
 
259
}
 
260
 
 
261
static void
 
262
um_editable_button_finalize (GObject *object)
 
263
{
 
264
        UmEditableButton *button = (UmEditableButton*)object;
 
265
 
 
266
        g_free (button->priv->text);
 
267
 
 
268
        G_OBJECT_CLASS (um_editable_button_parent_class)->finalize (object);
 
269
}
 
270
 
 
271
static void
 
272
um_editable_button_class_init (UmEditableButtonClass *class)
 
273
{
 
274
        GObjectClass *object_class;
 
275
 
 
276
        object_class = G_OBJECT_CLASS (class);
 
277
 
 
278
        object_class->set_property = um_editable_button_set_property;
 
279
        object_class->get_property = um_editable_button_get_property;
 
280
        object_class->finalize = um_editable_button_finalize;
 
281
 
 
282
        signals[START_EDITING] =
 
283
                g_signal_new ("start-editing",
 
284
                              G_TYPE_FROM_CLASS (class),
 
285
                              G_SIGNAL_RUN_LAST,
 
286
                              G_STRUCT_OFFSET (UmEditableButtonClass, start_editing),
 
287
                              NULL, NULL,
 
288
                              g_cclosure_marshal_VOID__VOID,
 
289
                              G_TYPE_NONE, 0);
 
290
 
 
291
        g_object_class_install_property (object_class, PROP_TEXT,
 
292
                g_param_spec_string ("text",
 
293
                                     "Text", "The text of the button",
 
294
                                     NULL,
 
295
                                     G_PARAM_READWRITE));
 
296
 
 
297
        g_object_class_install_property (object_class, PROP_EDITABLE,
 
298
                g_param_spec_boolean ("editable",
 
299
                                      "Editable", "Whether the text can be edited",
 
300
                                      FALSE,
 
301
                                      G_PARAM_READWRITE));
 
302
 
 
303
        g_object_class_install_property (object_class, PROP_WEIGHT,
 
304
                g_param_spec_int ("weight",
 
305
                                  "Font Weight", "The font weight to use",
 
306
                                  0, G_MAXINT, PANGO_WEIGHT_NORMAL,
 
307
                                  G_PARAM_READWRITE));
 
308
 
 
309
        g_object_class_install_property (object_class, PROP_WEIGHT_SET,
 
310
                g_param_spec_boolean ("weight-set",
 
311
                                      "Font Weight Set", "Whether a font weight is set",
 
312
                                      FALSE,
 
313
                                      G_PARAM_READWRITE));
 
314
 
 
315
        g_object_class_install_property (object_class, PROP_SCALE,
 
316
                g_param_spec_double ("scale",
 
317
                                     "Font Scale", "The font scale to use",
 
318
                                     0.0, G_MAXDOUBLE, 1.0,
 
319
                                     G_PARAM_READWRITE));
 
320
 
 
321
        g_object_class_install_property (object_class, PROP_SCALE_SET,
 
322
                g_param_spec_boolean ("scale-set",
 
323
                                      "Font Scale Set", "Whether a font scale is set",
 
324
                                      FALSE,
 
325
                                      G_PARAM_READWRITE));
 
326
 
 
327
        g_type_class_add_private (class, sizeof (UmEditableButtonPrivate));
 
328
}
 
329
 
 
330
static void
 
331
start_editing (UmEditableButton *button)
 
332
{
 
333
        g_signal_emit (button, signals[START_EDITING], 0);
 
334
}
 
335
 
 
336
static void
 
337
button_clicked (GtkWidget        *widget,
 
338
                UmEditableButton *button)
 
339
{
 
340
        start_editing (button);
 
341
}
 
342
 
 
343
static void
 
344
update_button_padding (GtkWidget        *widget,
 
345
                       GtkAllocation    *allocation,
 
346
                       UmEditableButton *button)
 
347
{
 
348
        UmEditableButtonPrivate *priv = button->priv;
 
349
        GtkAllocation parent_allocation;
 
350
        gint offset;
 
351
        gint pad;
 
352
 
 
353
        gtk_widget_get_allocation (gtk_widget_get_parent (widget), &parent_allocation);
 
354
 
 
355
        offset = allocation->x - parent_allocation.x;
 
356
 
 
357
        gtk_misc_get_padding  (GTK_MISC (priv->label), &pad, NULL);
 
358
        if (offset != pad)
 
359
                gtk_misc_set_padding (GTK_MISC (priv->label), offset, 0);
 
360
}
 
361
 
 
362
static void
 
363
um_editable_button_init (UmEditableButton *button)
 
364
{
 
365
        UmEditableButtonPrivate *priv;
 
366
 
 
367
        priv = button->priv = UM_EDITABLE_BUTTON_GET_PRIVATE (button);
 
368
 
 
369
        priv->weight = PANGO_WEIGHT_NORMAL;
 
370
        priv->weight_set = FALSE;
 
371
        priv->scale = 1.0;
 
372
        priv->scale_set = FALSE;
 
373
 
 
374
        priv->notebook = (GtkNotebook*)gtk_notebook_new ();
 
375
        gtk_notebook_set_show_tabs (priv->notebook, FALSE);
 
376
        gtk_notebook_set_show_border (priv->notebook, FALSE);
 
377
 
 
378
        priv->label = (GtkLabel*)gtk_label_new (EMPTY_TEXT);
 
379
        gtk_misc_set_alignment (GTK_MISC (priv->label), 0.0, 0.5);
 
380
        gtk_notebook_append_page (priv->notebook, (GtkWidget*)priv->label, NULL);
 
381
 
 
382
        priv->button = (GtkButton*)gtk_button_new_with_label (EMPTY_TEXT);
 
383
        gtk_widget_set_receives_default ((GtkWidget*)priv->button, TRUE);
 
384
        gtk_button_set_relief (priv->button, GTK_RELIEF_NONE);
 
385
        gtk_button_set_alignment (priv->button, 0.0, 0.5);
 
386
        gtk_notebook_append_page (priv->notebook, (GtkWidget*)priv->button, NULL);
 
387
        g_signal_connect (priv->button, "clicked", G_CALLBACK (button_clicked), button);
 
388
        g_signal_connect (gtk_bin_get_child (GTK_BIN (priv->button)), "size-allocate", G_CALLBACK (update_button_padding), button);
 
389
 
 
390
        gtk_container_add (GTK_CONTAINER (button), (GtkWidget*)priv->notebook);
 
391
 
 
392
        gtk_widget_show ((GtkWidget*)priv->notebook);
 
393
        gtk_widget_show ((GtkWidget*)priv->label);
 
394
        gtk_widget_show ((GtkWidget*)priv->button);
 
395
 
 
396
        gtk_notebook_set_current_page (priv->notebook, 0);
 
397
}
 
398
 
 
399
GtkWidget *
 
400
um_editable_button_new (void)
 
401
{
 
402
        return (GtkWidget *) g_object_new (UM_TYPE_EDITABLE_BUTTON, NULL);
 
403
}