~ubuntu-branches/ubuntu/trusty/unity-control-center/trusty

« back to all changes in this revision

Viewing changes to shell/cc-editable-entry.c

  • Committer: Package Import Robot
  • Author(s): Robert Ancell
  • Date: 2014-01-08 16:29:18 UTC
  • Revision ID: package-import@ubuntu.com-20140108162918-g29dd08tr913y2qh
Tags: upstream-14.04.0
ImportĀ upstreamĀ versionĀ 14.04.0

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