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 "um-editable-button.h"
25
#define EMPTY_TEXT "\xe2\x80\x94"
27
struct _UmEditableButtonPrivate {
28
GtkNotebook *notebook;
40
#define UM_EDITABLE_BUTTON_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), UM_TYPE_EDITABLE_BUTTON, UmEditableButtonPrivate))
57
static guint signals [LAST_SIGNAL] = { 0, };
59
G_DEFINE_TYPE (UmEditableButton, um_editable_button, GTK_TYPE_ALIGNMENT);
62
um_editable_button_set_text (UmEditableButton *button,
65
UmEditableButtonPrivate *priv;
71
tmp = g_strdup (text);
75
if (tmp == NULL || tmp[0] == '\0')
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);
82
g_object_notify (G_OBJECT (button), "text");
86
um_editable_button_get_text (UmEditableButton *button)
88
return button->priv->text;
92
um_editable_button_set_editable (UmEditableButton *button,
95
UmEditableButtonPrivate *priv;
99
if (priv->editable != editable) {
100
priv->editable = editable;
102
gtk_notebook_set_current_page (priv->notebook, editable ? 1 : 0);
104
g_object_notify (G_OBJECT (button), "editable");
109
um_editable_button_get_editable (UmEditableButton *button)
111
return button->priv->editable;
115
update_fonts (UmEditableButton *button)
117
PangoAttrList *attrs;
118
PangoAttribute *attr;
121
UmEditableButtonPrivate *priv = button->priv;
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);
128
if (priv->weight_set) {
129
attr = pango_attr_weight_new (priv->weight);
130
pango_attr_list_insert (attrs, attr);
133
gtk_label_set_attributes (priv->label, attrs);
135
label = gtk_bin_get_child (GTK_BIN (priv->button));
136
gtk_label_set_attributes (GTK_LABEL (label), attrs);
138
pango_attr_list_unref (attrs);
142
um_editable_button_set_weight (UmEditableButton *button,
145
UmEditableButtonPrivate *priv = button->priv;
147
if (priv->weight == weight && priv->weight_set)
150
priv->weight = weight;
151
priv->weight_set = TRUE;
153
update_fonts (button);
155
g_object_notify (G_OBJECT (button), "weight");
156
g_object_notify (G_OBJECT (button), "weight-set");
160
um_editable_button_get_weight (UmEditableButton *button)
162
return button->priv->weight;
166
um_editable_button_set_scale (UmEditableButton *button,
169
UmEditableButtonPrivate *priv = button->priv;
171
if (priv->scale == scale && priv->scale_set)
175
priv->scale_set = TRUE;
177
update_fonts (button);
179
g_object_notify (G_OBJECT (button), "scale");
180
g_object_notify (G_OBJECT (button), "scale-set");
184
um_editable_button_get_scale (UmEditableButton *button)
186
return button->priv->scale;
190
um_editable_button_set_property (GObject *object,
195
UmEditableButton *button = UM_EDITABLE_BUTTON (object);
199
um_editable_button_set_text (button, g_value_get_string (value));
202
um_editable_button_set_editable (button, g_value_get_boolean (value));
205
um_editable_button_set_weight (button, g_value_get_int (value));
207
case PROP_WEIGHT_SET:
208
button->priv->weight_set = g_value_get_boolean (value);
211
um_editable_button_set_scale (button, g_value_get_double (value));
214
button->priv->scale_set = g_value_get_boolean (value);
217
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
223
um_editable_button_get_property (GObject *object,
228
UmEditableButton *button = UM_EDITABLE_BUTTON (object);
232
g_value_set_string (value,
233
um_editable_button_get_text (button));
236
g_value_set_boolean (value,
237
um_editable_button_get_editable (button));
240
g_value_set_int (value,
241
um_editable_button_get_weight (button));
243
case PROP_WEIGHT_SET:
244
g_value_set_boolean (value,
245
button->priv->weight_set);
248
g_value_set_double (value,
249
um_editable_button_get_scale (button));
252
g_value_set_boolean (value,
253
button->priv->scale_set);
256
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
262
um_editable_button_finalize (GObject *object)
264
UmEditableButton *button = (UmEditableButton*)object;
266
g_free (button->priv->text);
268
G_OBJECT_CLASS (um_editable_button_parent_class)->finalize (object);
272
um_editable_button_class_init (UmEditableButtonClass *class)
274
GObjectClass *object_class;
276
object_class = G_OBJECT_CLASS (class);
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;
282
signals[START_EDITING] =
283
g_signal_new ("start-editing",
284
G_TYPE_FROM_CLASS (class),
286
G_STRUCT_OFFSET (UmEditableButtonClass, start_editing),
288
g_cclosure_marshal_VOID__VOID,
291
g_object_class_install_property (object_class, PROP_TEXT,
292
g_param_spec_string ("text",
293
"Text", "The text of the button",
297
g_object_class_install_property (object_class, PROP_EDITABLE,
298
g_param_spec_boolean ("editable",
299
"Editable", "Whether the text can be edited",
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,
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",
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,
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",
327
g_type_class_add_private (class, sizeof (UmEditableButtonPrivate));
331
start_editing (UmEditableButton *button)
333
g_signal_emit (button, signals[START_EDITING], 0);
337
button_clicked (GtkWidget *widget,
338
UmEditableButton *button)
340
start_editing (button);
344
update_button_padding (GtkWidget *widget,
345
GtkAllocation *allocation,
346
UmEditableButton *button)
348
UmEditableButtonPrivate *priv = button->priv;
349
GtkAllocation parent_allocation;
353
gtk_widget_get_allocation (gtk_widget_get_parent (widget), &parent_allocation);
355
offset = allocation->x - parent_allocation.x;
357
gtk_misc_get_padding (GTK_MISC (priv->label), &pad, NULL);
359
gtk_misc_set_padding (GTK_MISC (priv->label), offset, 0);
363
um_editable_button_init (UmEditableButton *button)
365
UmEditableButtonPrivate *priv;
367
priv = button->priv = UM_EDITABLE_BUTTON_GET_PRIVATE (button);
369
priv->weight = PANGO_WEIGHT_NORMAL;
370
priv->weight_set = FALSE;
372
priv->scale_set = FALSE;
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);
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);
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);
390
gtk_container_add (GTK_CONTAINER (button), (GtkWidget*)priv->notebook);
392
gtk_widget_show ((GtkWidget*)priv->notebook);
393
gtk_widget_show ((GtkWidget*)priv->label);
394
gtk_widget_show ((GtkWidget*)priv->button);
396
gtk_notebook_set_current_page (priv->notebook, 0);
400
um_editable_button_new (void)
402
return (GtkWidget *) g_object_new (UM_TYPE_EDITABLE_BUTTON, NULL);