1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
3
* Copyright 2009-2010 Red Hat, Inc,
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.
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.
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.
19
* Written by: Matthias Clasen <mclasen@redhat.com>
22
#include <gdk/gdkkeysyms.h>
23
#include "cc-editable-entry.h"
25
#define EMPTY_TEXT "\xe2\x80\x94"
27
struct _CcEditableEntryPrivate {
28
GtkNotebook *notebook;
41
gboolean in_stop_editing;
44
#define CC_EDITABLE_ENTRY_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), CC_TYPE_EDITABLE_ENTRY, CcEditableEntryPrivate))
68
static guint signals [LAST_SIGNAL] = { 0, };
70
G_DEFINE_TYPE (CcEditableEntry, cc_editable_entry, GTK_TYPE_ALIGNMENT);
73
cc_editable_entry_set_text (CcEditableEntry *e,
76
CcEditableEntryPrivate *priv;
82
tmp = g_strdup (text);
86
gtk_entry_set_text (priv->entry, tmp);
88
if (tmp == NULL || tmp[0] == '\0')
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);
95
g_object_notify (G_OBJECT (e), "text");
99
cc_editable_entry_get_text (CcEditableEntry *e)
101
return e->priv->text;
105
cc_editable_entry_set_editable (CcEditableEntry *e,
108
CcEditableEntryPrivate *priv;
112
if (priv->editable != editable) {
113
priv->editable = editable;
115
gtk_notebook_set_current_page (priv->notebook, editable ? PAGE_BUTTON : PAGE_LABEL);
117
g_object_notify (G_OBJECT (e), "editable");
122
cc_editable_entry_get_editable (CcEditableEntry *e)
124
return e->priv->editable;
128
cc_editable_entry_set_selectable (CcEditableEntry *e,
131
CcEditableEntryPrivate *priv;
135
if (priv->selectable != selectable) {
136
priv->selectable = selectable;
138
gtk_label_set_selectable (priv->label, selectable);
140
g_object_notify (G_OBJECT (e), "selectable");
145
cc_editable_entry_get_selectable (CcEditableEntry *e)
147
return e->priv->selectable;
151
update_entry_font (GtkWidget *widget,
154
CcEditableEntryPrivate *priv = e->priv;
155
PangoFontDescription *desc;
156
GtkStyleContext *style;
159
if (!priv->weight_set && !priv->scale_set)
162
g_signal_handlers_block_by_func (widget, update_entry_font, e);
164
gtk_widget_override_font (widget, NULL);
166
style = gtk_widget_get_style_context (widget);
167
desc = pango_font_description_copy
168
(gtk_style_context_get_font (style, gtk_widget_get_state_flags (widget)));
170
if (priv->weight_set)
171
pango_font_description_set_weight (desc, priv->weight);
172
if (priv->scale_set) {
173
size = pango_font_description_get_size (desc);
174
pango_font_description_set_size (desc, priv->scale * size);
176
gtk_widget_override_font (widget, desc);
178
pango_font_description_free (desc);
180
g_signal_handlers_unblock_by_func (widget, update_entry_font, e);
184
update_fonts (CcEditableEntry *e)
186
PangoAttrList *attrs;
187
PangoAttribute *attr;
190
CcEditableEntryPrivate *priv = e->priv;
192
attrs = pango_attr_list_new ();
193
if (priv->scale_set) {
194
attr = pango_attr_scale_new (priv->scale);
195
pango_attr_list_insert (attrs, attr);
197
if (priv->weight_set) {
198
attr = pango_attr_weight_new (priv->weight);
199
pango_attr_list_insert (attrs, attr);
202
gtk_label_set_attributes (priv->label, attrs);
204
label = gtk_bin_get_child (GTK_BIN (priv->button));
205
gtk_label_set_attributes (GTK_LABEL (label), attrs);
207
pango_attr_list_unref (attrs);
209
update_entry_font ((GtkWidget *)priv->entry, e);
213
cc_editable_entry_set_weight (CcEditableEntry *e,
216
CcEditableEntryPrivate *priv = e->priv;
218
if (priv->weight == weight && priv->weight_set)
221
priv->weight = weight;
222
priv->weight_set = TRUE;
226
g_object_notify (G_OBJECT (e), "weight");
227
g_object_notify (G_OBJECT (e), "weight-set");
231
cc_editable_entry_get_weight (CcEditableEntry *e)
233
return e->priv->weight;
237
cc_editable_entry_set_scale (CcEditableEntry *e,
240
CcEditableEntryPrivate *priv = e->priv;
242
if (priv->scale == scale && priv->scale_set)
246
priv->scale_set = TRUE;
250
g_object_notify (G_OBJECT (e), "scale");
251
g_object_notify (G_OBJECT (e), "scale-set");
255
cc_editable_entry_get_scale (CcEditableEntry *e)
257
return e->priv->scale;
261
cc_editable_entry_set_property (GObject *object,
266
CcEditableEntry *e = CC_EDITABLE_ENTRY (object);
270
cc_editable_entry_set_text (e, g_value_get_string (value));
273
cc_editable_entry_set_editable (e, g_value_get_boolean (value));
275
case PROP_SELECTABLE:
276
cc_editable_entry_set_selectable (e, g_value_get_boolean (value));
279
cc_editable_entry_set_weight (e, g_value_get_int (value));
281
case PROP_WEIGHT_SET:
282
e->priv->weight_set = g_value_get_boolean (value);
285
cc_editable_entry_set_scale (e, g_value_get_double (value));
288
e->priv->scale_set = g_value_get_boolean (value);
291
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
297
cc_editable_entry_get_property (GObject *object,
302
CcEditableEntry *e = CC_EDITABLE_ENTRY (object);
306
g_value_set_string (value,
307
cc_editable_entry_get_text (e));
310
g_value_set_boolean (value,
311
cc_editable_entry_get_editable (e));
313
case PROP_SELECTABLE:
314
g_value_set_boolean (value,
315
cc_editable_entry_get_selectable (e));
318
g_value_set_int (value,
319
cc_editable_entry_get_weight (e));
321
case PROP_WEIGHT_SET:
322
g_value_set_boolean (value, e->priv->weight_set);
325
g_value_set_double (value,
326
cc_editable_entry_get_scale (e));
329
g_value_set_boolean (value, e->priv->scale_set);
332
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
338
cc_editable_entry_finalize (GObject *object)
340
CcEditableEntry *e = (CcEditableEntry*)object;
342
g_free (e->priv->text);
344
G_OBJECT_CLASS (cc_editable_entry_parent_class)->finalize (object);
348
cc_editable_entry_class_init (CcEditableEntryClass *class)
350
GObjectClass *object_class;
352
object_class = G_OBJECT_CLASS (class);
354
object_class->set_property = cc_editable_entry_set_property;
355
object_class->get_property = cc_editable_entry_get_property;
356
object_class->finalize = cc_editable_entry_finalize;
358
signals[EDITING_DONE] =
359
g_signal_new ("editing-done",
360
G_TYPE_FROM_CLASS (class),
362
G_STRUCT_OFFSET (CcEditableEntryClass, editing_done),
364
g_cclosure_marshal_VOID__VOID,
367
g_object_class_install_property (object_class, PROP_TEXT,
368
g_param_spec_string ("text",
369
"Text", "The text of the button",
373
g_object_class_install_property (object_class, PROP_EDITABLE,
374
g_param_spec_boolean ("editable",
375
"Editable", "Whether the text can be edited",
379
g_object_class_install_property (object_class, PROP_SELECTABLE,
380
g_param_spec_boolean ("selectable",
381
"Selectable", "Whether the text can be selected by mouse",
385
g_object_class_install_property (object_class, PROP_WEIGHT,
386
g_param_spec_int ("weight",
387
"Font Weight", "The font weight to use",
388
0, G_MAXINT, PANGO_WEIGHT_NORMAL,
391
g_object_class_install_property (object_class, PROP_WEIGHT_SET,
392
g_param_spec_boolean ("weight-set",
393
"Font Weight Set", "Whether a font weight is set",
397
g_object_class_install_property (object_class, PROP_SCALE,
398
g_param_spec_double ("scale",
399
"Font Scale", "The font scale to use",
400
0.0, G_MAXDOUBLE, 1.0,
403
g_object_class_install_property (object_class, PROP_SCALE_SET,
404
g_param_spec_boolean ("scale-set",
405
"Font Scale Set", "Whether a font scale is set",
409
g_type_class_add_private (class, sizeof (CcEditableEntryPrivate));
413
start_editing (CcEditableEntry *e)
415
gtk_notebook_set_current_page (e->priv->notebook, PAGE_ENTRY);
419
stop_editing (CcEditableEntry *e)
421
/* Avoid launching another "editing-done" signal
422
* caused by the notebook page change */
423
if (e->priv->in_stop_editing)
426
e->priv->in_stop_editing = TRUE;
427
gtk_notebook_set_current_page (e->priv->notebook, PAGE_BUTTON);
428
cc_editable_entry_set_text (e, gtk_entry_get_text (e->priv->entry));
429
g_signal_emit (e, signals[EDITING_DONE], 0);
430
e->priv->in_stop_editing = FALSE;
434
cancel_editing (CcEditableEntry *e)
436
gtk_entry_set_text (e->priv->entry, cc_editable_entry_get_text (e));
437
gtk_notebook_set_current_page (e->priv->notebook, PAGE_BUTTON);
441
button_clicked (GtkWidget *widget,
448
entry_activated (GtkWidget *widget,
455
entry_focus_out (GtkWidget *widget,
456
GdkEventFocus *event,
464
entry_key_press (GtkWidget *widget,
468
if (event->keyval == GDK_KEY_Escape) {
475
update_button_padding (GtkWidget *widget,
476
GtkAllocation *allocation,
479
CcEditableEntryPrivate *priv = e->priv;
484
gtk_widget_get_allocation (gtk_widget_get_parent (widget), &alloc);
486
offset = allocation->x - alloc.x;
488
gtk_misc_get_padding (GTK_MISC (priv->label), &pad, NULL);
490
gtk_misc_set_padding (GTK_MISC (priv->label), offset, 0);
494
cc_editable_entry_init (CcEditableEntry *e)
496
CcEditableEntryPrivate *priv;
498
priv = e->priv = CC_EDITABLE_ENTRY_GET_PRIVATE (e);
500
priv->weight = PANGO_WEIGHT_NORMAL;
501
priv->weight_set = FALSE;
503
priv->scale_set = FALSE;
505
priv->notebook = (GtkNotebook*)gtk_notebook_new ();
506
gtk_notebook_set_show_tabs (priv->notebook, FALSE);
507
gtk_notebook_set_show_border (priv->notebook, FALSE);
510
priv->label = (GtkLabel*)gtk_label_new (EMPTY_TEXT);
511
gtk_misc_set_alignment (GTK_MISC (priv->label), 0.0, 0.5);
512
gtk_notebook_append_page (priv->notebook, (GtkWidget*)priv->label, NULL);
515
priv->button = (GtkButton*)gtk_button_new_with_label (EMPTY_TEXT);
516
gtk_widget_set_receives_default ((GtkWidget*)priv->button, TRUE);
517
gtk_button_set_relief (priv->button, GTK_RELIEF_NONE);
518
gtk_button_set_alignment (priv->button, 0.0, 0.5);
519
gtk_notebook_append_page (priv->notebook, (GtkWidget*)priv->button, NULL);
520
g_signal_connect (priv->button, "clicked", G_CALLBACK (button_clicked), e);
523
priv->entry = (GtkEntry*)gtk_entry_new ();
524
gtk_notebook_append_page (priv->notebook, (GtkWidget*)priv->entry, NULL);
526
g_signal_connect (priv->entry, "activate", G_CALLBACK (entry_activated), e);
527
g_signal_connect (priv->entry, "focus-out-event", G_CALLBACK (entry_focus_out), e);
528
g_signal_connect (priv->entry, "key-press-event", G_CALLBACK (entry_key_press), e);
529
g_signal_connect (priv->entry, "style-updated", G_CALLBACK (update_entry_font), e);
530
g_signal_connect (gtk_bin_get_child (GTK_BIN (priv->button)), "size-allocate", G_CALLBACK (update_button_padding), e);
532
gtk_container_add (GTK_CONTAINER (e), (GtkWidget*)priv->notebook);
534
gtk_widget_show ((GtkWidget*)priv->notebook);
535
gtk_widget_show ((GtkWidget*)priv->label);
536
gtk_widget_show ((GtkWidget*)priv->button);
537
gtk_widget_show ((GtkWidget*)priv->entry);
539
gtk_notebook_set_current_page (priv->notebook, PAGE_LABEL);
543
cc_editable_entry_new (void)
545
return (GtkWidget *) g_object_new (CC_TYPE_EDITABLE_ENTRY, NULL);