~kroq-gar78/ubuntu/precise/gnome-control-center/fix-885947

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Rodrigo Moya
  • Date: 2011-05-17 10:47:27 UTC
  • mfrom: (0.1.11 experimental) (1.1.45 upstream)
  • Revision ID: james.westby@ubuntu.com-20110517104727-lqel6m8vhfw5jby1
Tags: 1:3.0.1.1-1ubuntu1
* Rebase on Debian, remaining Ubuntu changes:
* debian/control:
  - Build-Depend on hardening-wrapper, dpkg-dev and dh-autoreconf
  - Add dependency on ubuntu-system-service
  - Remove dependency on gnome-icon-theme-symbolic
  - Move dependency on apg, gnome-icon-theme-symbolic and accountsservice to
    be a Recommends: until we get them in main
* debian/rules:
  - Use autoreconf
  - Add binary-post-install rule for gnome-control-center-data
  - Run dh-autoreconf
* debian/gnome-control-center.dirs:
* debian/gnome-control-center.links:
  - Add a link to the control center shell for indicators
* debian/patches/00_disable-nm.patch:
  - Temporary patch to disable building with NetworkManager until we get
    the new one in the archive
* debian/patches/01_git_remove_gettext_calls.patch:
  - Remove calls to AM_GNU_GETTEXT, IT_PROG_INTLTOOL should be enough
* debian/patches/01_git_kill_warning.patch:
  - Kill warning
* debian/patches/50_ubuntu_systemwide_prefs.patch:
  - Ubuntu specific proxy preferences
* debian/patches/51_ubuntu_system_keyboard.patch:
  - Implement the global keyboard spec at https://wiki.ubuntu.com/DefaultKeyboardSettings

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-entry.h"
 
24
 
 
25
#define EMPTY_TEXT "\xe2\x80\x94"
 
26
 
 
27
struct _UmEditableEntryPrivate {
 
28
        GtkNotebook *notebook;
 
29
        GtkLabel    *label;
 
30
        GtkButton   *button;
 
31
        GtkEntry    *entry;
 
32
 
 
33
        gchar *text;
 
34
        gboolean editable;
 
35
        gint weight;
 
36
        gboolean weight_set;
 
37
        gdouble scale;
 
38
        gboolean scale_set;
 
39
};
 
40
 
 
41
#define UM_EDITABLE_ENTRY_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), UM_TYPE_EDITABLE_ENTRY, UmEditableEntryPrivate))
 
42
 
 
43
enum {
 
44
        PROP_0,
 
45
        PROP_TEXT,
 
46
        PROP_EDITABLE,
 
47
        PROP_SCALE,
 
48
        PROP_SCALE_SET,
 
49
        PROP_WEIGHT,
 
50
        PROP_WEIGHT_SET
 
51
};
 
52
 
 
53
enum {
 
54
        EDITING_DONE,
 
55
        LAST_SIGNAL
 
56
};
 
57
 
 
58
static guint signals [LAST_SIGNAL] = { 0, };
 
59
 
 
60
G_DEFINE_TYPE (UmEditableEntry, um_editable_entry, GTK_TYPE_ALIGNMENT);
 
61
 
 
62
void
 
63
um_editable_entry_set_text (UmEditableEntry *e,
 
64
                             const gchar    *text)
 
65
{
 
66
        UmEditableEntryPrivate *priv;
 
67
        gchar *tmp;
 
68
        GtkWidget *label;
 
69
 
 
70
        priv = e->priv;
 
71
 
 
72
        tmp = g_strdup (text);
 
73
        g_free (priv->text);
 
74
        priv->text = tmp;
 
75
 
 
76
        gtk_entry_set_text (priv->entry, tmp);
 
77
 
 
78
        if (tmp == NULL || tmp[0] == '\0')
 
79
                tmp = EMPTY_TEXT;
 
80
 
 
81
        gtk_label_set_text (priv->label, tmp);
 
82
        label = gtk_bin_get_child (GTK_BIN (priv->button));
 
83
        gtk_label_set_text (GTK_LABEL (label), tmp);
 
84
 
 
85
        g_object_notify (G_OBJECT (e), "text");
 
86
}
 
87
 
 
88
const gchar *
 
89
um_editable_entry_get_text (UmEditableEntry *e)
 
90
{
 
91
        return e->priv->text;
 
92
}
 
93
 
 
94
void
 
95
um_editable_entry_set_editable (UmEditableEntry *e,
 
96
                                 gboolean        editable)
 
97
{
 
98
        UmEditableEntryPrivate *priv;
 
99
 
 
100
        priv = e->priv;
 
101
 
 
102
        if (priv->editable != editable) {
 
103
                priv->editable = editable;
 
104
 
 
105
                gtk_notebook_set_current_page (priv->notebook, editable ? 1 : 0);
 
106
 
 
107
                g_object_notify (G_OBJECT (e), "editable");
 
108
        }
 
109
}
 
110
 
 
111
gboolean
 
112
um_editable_entry_get_editable (UmEditableEntry *e)
 
113
{
 
114
        return e->priv->editable;
 
115
}
 
116
 
 
117
static void
 
118
update_entry_font (GtkWidget        *widget,
 
119
                   UmEditableEntry *e)
 
120
{
 
121
        UmEditableEntryPrivate *priv = e->priv;
 
122
        PangoFontDescription *desc;
 
123
        GtkStyleContext *style;
 
124
        gint size;
 
125
 
 
126
        if (!priv->weight_set && !priv->scale_set)
 
127
                return;
 
128
 
 
129
        g_signal_handlers_block_by_func (widget, update_entry_font, e);
 
130
 
 
131
        gtk_widget_override_font (widget, NULL);        
 
132
 
 
133
        style = gtk_widget_get_style_context (widget);
 
134
        desc = pango_font_description_copy 
 
135
                (gtk_style_context_get_font (style, gtk_widget_get_state_flags (widget)));
 
136
 
 
137
        if (priv->weight_set)
 
138
                pango_font_description_set_weight (desc, priv->weight);
 
139
        if (priv->scale_set) {
 
140
                size = pango_font_description_get_size (desc);
 
141
                pango_font_description_set_size (desc, priv->scale * size);
 
142
        }
 
143
        gtk_widget_override_font (widget, desc);
 
144
 
 
145
        pango_font_description_free (desc);
 
146
 
 
147
        g_signal_handlers_unblock_by_func (widget, update_entry_font, e);
 
148
}
 
149
 
 
150
static void
 
151
update_fonts (UmEditableEntry *e)
 
152
{
 
153
        PangoAttrList *attrs;
 
154
        PangoAttribute *attr;
 
155
        GtkWidget *label;
 
156
 
 
157
        UmEditableEntryPrivate *priv = e->priv;
 
158
 
 
159
        attrs = pango_attr_list_new ();
 
160
        if (priv->scale_set) {
 
161
                attr = pango_attr_scale_new (priv->scale);
 
162
                pango_attr_list_insert (attrs, attr);
 
163
        }
 
164
        if (priv->weight_set) {
 
165
                attr = pango_attr_weight_new (priv->weight);
 
166
                pango_attr_list_insert (attrs, attr);
 
167
        }
 
168
 
 
169
        gtk_label_set_attributes (priv->label, attrs);
 
170
 
 
171
        label = gtk_bin_get_child (GTK_BIN (priv->button));
 
172
        gtk_label_set_attributes (GTK_LABEL (label), attrs);
 
173
 
 
174
        pango_attr_list_unref (attrs);
 
175
 
 
176
        update_entry_font ((GtkWidget *)priv->entry, e);
 
177
}
 
178
 
 
179
void
 
180
um_editable_entry_set_weight (UmEditableEntry *e,
 
181
                               gint            weight)
 
182
{
 
183
        UmEditableEntryPrivate *priv = e->priv;
 
184
 
 
185
        if (priv->weight == weight && priv->weight_set)
 
186
                return;
 
187
 
 
188
        priv->weight = weight;
 
189
        priv->weight_set = TRUE;
 
190
 
 
191
        update_fonts (e);
 
192
 
 
193
        g_object_notify (G_OBJECT (e), "weight");
 
194
        g_object_notify (G_OBJECT (e), "weight-set");
 
195
}
 
196
 
 
197
gint
 
198
um_editable_entry_get_weight (UmEditableEntry *e)
 
199
{
 
200
        return e->priv->weight;
 
201
}
 
202
 
 
203
void
 
204
um_editable_entry_set_scale (UmEditableEntry *e,
 
205
                              gdouble         scale)
 
206
{
 
207
        UmEditableEntryPrivate *priv = e->priv;
 
208
 
 
209
        if (priv->scale == scale && priv->scale_set)
 
210
                return;
 
211
 
 
212
        priv->scale = scale;
 
213
        priv->scale_set = TRUE;
 
214
 
 
215
        update_fonts (e);
 
216
 
 
217
        g_object_notify (G_OBJECT (e), "scale");
 
218
        g_object_notify (G_OBJECT (e), "scale-set");
 
219
}
 
220
 
 
221
gdouble
 
222
um_editable_entry_get_scale (UmEditableEntry *e)
 
223
{
 
224
        return e->priv->scale;
 
225
}
 
226
 
 
227
static void
 
228
um_editable_entry_set_property (GObject      *object,
 
229
                                guint         prop_id,
 
230
                                const GValue *value,
 
231
                                GParamSpec   *pspec)
 
232
{
 
233
        UmEditableEntry *e = UM_EDITABLE_ENTRY (object);
 
234
 
 
235
        switch (prop_id) {
 
236
        case PROP_TEXT:
 
237
                um_editable_entry_set_text (e, g_value_get_string (value));
 
238
                break;
 
239
        case PROP_EDITABLE:
 
240
                um_editable_entry_set_editable (e, g_value_get_boolean (value));
 
241
                break;
 
242
        case PROP_WEIGHT:
 
243
                um_editable_entry_set_weight (e, g_value_get_int (value));
 
244
                break;
 
245
        case PROP_WEIGHT_SET:
 
246
                e->priv->weight_set = g_value_get_boolean (value);
 
247
                break;
 
248
        case PROP_SCALE:
 
249
                um_editable_entry_set_scale (e, g_value_get_double (value));
 
250
                break;
 
251
        case PROP_SCALE_SET:
 
252
                e->priv->scale_set = g_value_get_boolean (value);
 
253
                break;
 
254
        default:
 
255
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
256
                break;
 
257
        }
 
258
}
 
259
 
 
260
static void
 
261
um_editable_entry_get_property (GObject    *object,
 
262
                                guint       prop_id,
 
263
                                GValue     *value,
 
264
                                GParamSpec *pspec)
 
265
{
 
266
        UmEditableEntry *e = UM_EDITABLE_ENTRY (object);
 
267
 
 
268
        switch (prop_id) {
 
269
        case PROP_TEXT:
 
270
                g_value_set_string (value,
 
271
                                    um_editable_entry_get_text (e));
 
272
                break;
 
273
        case PROP_EDITABLE:
 
274
                g_value_set_boolean (value,
 
275
                                     um_editable_entry_get_editable (e));
 
276
                break;
 
277
        case PROP_WEIGHT:
 
278
                g_value_set_int (value,
 
279
                                 um_editable_entry_get_weight (e));
 
280
                break;
 
281
        case PROP_WEIGHT_SET:
 
282
                g_value_set_boolean (value, e->priv->weight_set);
 
283
                break;
 
284
        case PROP_SCALE:
 
285
                g_value_set_double (value,
 
286
                                    um_editable_entry_get_scale (e));
 
287
                break;
 
288
        case PROP_SCALE_SET:
 
289
                g_value_set_boolean (value, e->priv->scale_set);
 
290
                break;
 
291
        default:
 
292
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
293
                break;
 
294
        }
 
295
}
 
296
 
 
297
static void
 
298
um_editable_entry_finalize (GObject *object)
 
299
{
 
300
        UmEditableEntry *e = (UmEditableEntry*)object;
 
301
 
 
302
        g_free (e->priv->text);
 
303
 
 
304
        G_OBJECT_CLASS (um_editable_entry_parent_class)->finalize (object);
 
305
}
 
306
 
 
307
static void
 
308
um_editable_entry_class_init (UmEditableEntryClass *class)
 
309
{
 
310
        GObjectClass *object_class;
 
311
 
 
312
        object_class = G_OBJECT_CLASS (class);
 
313
 
 
314
        object_class->set_property = um_editable_entry_set_property;
 
315
        object_class->get_property = um_editable_entry_get_property;
 
316
        object_class->finalize = um_editable_entry_finalize;
 
317
 
 
318
        signals[EDITING_DONE] =
 
319
                g_signal_new ("editing-done",
 
320
                              G_TYPE_FROM_CLASS (class),
 
321
                              G_SIGNAL_RUN_LAST,
 
322
                              G_STRUCT_OFFSET (UmEditableEntryClass, editing_done),
 
323
                              NULL, NULL,
 
324
                              g_cclosure_marshal_VOID__VOID,
 
325
                              G_TYPE_NONE, 0);
 
326
 
 
327
        g_object_class_install_property (object_class, PROP_TEXT,
 
328
                g_param_spec_string ("text",
 
329
                                     "Text", "The text of the button",
 
330
                                     NULL,
 
331
                                     G_PARAM_READWRITE));
 
332
 
 
333
        g_object_class_install_property (object_class, PROP_EDITABLE,
 
334
                g_param_spec_boolean ("editable",
 
335
                                      "Editable", "Whether the text can be edited",
 
336
                                      FALSE,
 
337
                                      G_PARAM_READWRITE));
 
338
 
 
339
        g_object_class_install_property (object_class, PROP_WEIGHT,
 
340
                g_param_spec_int ("weight",
 
341
                                  "Font Weight", "The font weight to use",
 
342
                                  0, G_MAXINT, PANGO_WEIGHT_NORMAL,
 
343
                                  G_PARAM_READWRITE));
 
344
 
 
345
        g_object_class_install_property (object_class, PROP_WEIGHT_SET,
 
346
                g_param_spec_boolean ("weight-set",
 
347
                                      "Font Weight Set", "Whether a font weight is set",
 
348
                                      FALSE,
 
349
                                      G_PARAM_READWRITE));
 
350
 
 
351
        g_object_class_install_property (object_class, PROP_SCALE,
 
352
                g_param_spec_double ("scale",
 
353
                                     "Font Scale", "The font scale to use",
 
354
                                     0.0, G_MAXDOUBLE, 1.0,
 
355
                                     G_PARAM_READWRITE));
 
356
 
 
357
        g_object_class_install_property (object_class, PROP_SCALE_SET,
 
358
                g_param_spec_boolean ("scale-set",
 
359
                                      "Font Scale Set", "Whether a font scale is set",
 
360
                                      FALSE,
 
361
                                      G_PARAM_READWRITE));
 
362
 
 
363
        g_type_class_add_private (class, sizeof (UmEditableEntryPrivate));
 
364
}
 
365
 
 
366
static void
 
367
start_editing (UmEditableEntry *e)
 
368
{
 
369
        gtk_notebook_set_current_page (e->priv->notebook, 2);
 
370
}
 
371
 
 
372
static void
 
373
stop_editing (UmEditableEntry *e)
 
374
{
 
375
        um_editable_entry_set_text (e, gtk_entry_get_text (e->priv->entry));
 
376
        gtk_notebook_set_current_page (e->priv->notebook, 1);
 
377
        g_signal_emit (e, signals[EDITING_DONE], 0);
 
378
}
 
379
 
 
380
static void
 
381
cancel_editing (UmEditableEntry *e)
 
382
{
 
383
        gtk_entry_set_text (e->priv->entry, um_editable_entry_get_text (e));
 
384
        gtk_notebook_set_current_page (e->priv->notebook, 1);
 
385
}
 
386
 
 
387
static void
 
388
button_clicked (GtkWidget       *widget,
 
389
                UmEditableEntry *e)
 
390
{
 
391
        start_editing (e);
 
392
}
 
393
 
 
394
static void
 
395
entry_activated (GtkWidget       *widget,
 
396
                 UmEditableEntry *e)
 
397
{
 
398
        stop_editing (e);
 
399
}
 
400
 
 
401
static gboolean
 
402
entry_focus_out (GtkWidget       *widget,
 
403
                 GdkEventFocus   *event,
 
404
                 UmEditableEntry *e)
 
405
{
 
406
        stop_editing (e);
 
407
        return FALSE;
 
408
}
 
409
 
 
410
static gboolean
 
411
entry_key_press (GtkWidget       *widget,
 
412
                 GdkEventKey     *event,
 
413
                 UmEditableEntry *e)
 
414
{
 
415
        if (event->keyval == GDK_KEY_Escape) {
 
416
                cancel_editing (e);
 
417
        }
 
418
        return FALSE;
 
419
}
 
420
 
 
421
static void
 
422
update_button_padding (GtkWidget       *widget,
 
423
                       GtkAllocation   *allocation,
 
424
                       UmEditableEntry *e)
 
425
{
 
426
        UmEditableEntryPrivate *priv = e->priv;
 
427
        GtkAllocation alloc;
 
428
        gint offset;
 
429
        gint pad;
 
430
 
 
431
        gtk_widget_get_allocation (gtk_widget_get_parent (widget), &alloc);
 
432
 
 
433
        offset = allocation->x - alloc.x;
 
434
 
 
435
        gtk_misc_get_padding  (GTK_MISC (priv->label), &pad, NULL);
 
436
        if (offset != pad)
 
437
                gtk_misc_set_padding (GTK_MISC (priv->label), offset, 0);
 
438
}
 
439
 
 
440
static void
 
441
um_editable_entry_init (UmEditableEntry *e)
 
442
{
 
443
        UmEditableEntryPrivate *priv;
 
444
 
 
445
        priv = e->priv = UM_EDITABLE_ENTRY_GET_PRIVATE (e);
 
446
 
 
447
        priv->weight = PANGO_WEIGHT_NORMAL;
 
448
        priv->weight_set = FALSE;
 
449
        priv->scale = 1.0;
 
450
        priv->scale_set = FALSE;
 
451
 
 
452
        priv->notebook = (GtkNotebook*)gtk_notebook_new ();
 
453
        gtk_notebook_set_show_tabs (priv->notebook, FALSE);
 
454
        gtk_notebook_set_show_border (priv->notebook, FALSE);
 
455
 
 
456
        priv->label = (GtkLabel*)gtk_label_new (EMPTY_TEXT);
 
457
        gtk_misc_set_alignment (GTK_MISC (priv->label), 0.0, 0.5);
 
458
        gtk_notebook_append_page (priv->notebook, (GtkWidget*)priv->label, NULL);
 
459
 
 
460
        priv->button = (GtkButton*)gtk_button_new_with_label (EMPTY_TEXT);
 
461
        gtk_widget_set_receives_default ((GtkWidget*)priv->button, TRUE);
 
462
        gtk_button_set_relief (priv->button, GTK_RELIEF_NONE);
 
463
        gtk_button_set_alignment (priv->button, 0.0, 0.5);
 
464
        gtk_notebook_append_page (priv->notebook, (GtkWidget*)priv->button, NULL);
 
465
        g_signal_connect (priv->button, "clicked", G_CALLBACK (button_clicked), e);
 
466
 
 
467
        priv->entry = (GtkEntry*)gtk_entry_new ();
 
468
        gtk_notebook_append_page (priv->notebook, (GtkWidget*)priv->entry, NULL);
 
469
 
 
470
        g_signal_connect (priv->entry, "activate", G_CALLBACK (entry_activated), e);
 
471
        g_signal_connect (priv->entry, "focus-out-event", G_CALLBACK (entry_focus_out), e);
 
472
        g_signal_connect (priv->entry, "key-press-event", G_CALLBACK (entry_key_press), e);
 
473
        g_signal_connect (priv->entry, "style-updated", G_CALLBACK (update_entry_font), e);
 
474
        g_signal_connect (gtk_bin_get_child (GTK_BIN (priv->button)), "size-allocate", G_CALLBACK (update_button_padding), e);
 
475
 
 
476
        gtk_container_add (GTK_CONTAINER (e), (GtkWidget*)priv->notebook);
 
477
 
 
478
        gtk_widget_show ((GtkWidget*)priv->notebook);
 
479
        gtk_widget_show ((GtkWidget*)priv->label);
 
480
        gtk_widget_show ((GtkWidget*)priv->button);
 
481
        gtk_widget_show ((GtkWidget*)priv->entry);
 
482
 
 
483
        gtk_notebook_set_current_page (priv->notebook, 0);
 
484
}
 
485
 
 
486
GtkWidget *
 
487
um_editable_entry_new (void)
 
488
{
 
489
        return (GtkWidget *) g_object_new (UM_TYPE_EDITABLE_ENTRY, NULL);
 
490
}