[accounts-dialog/control-center-panel: 7/15] Extract the editable label as a separate widget
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [accounts-dialog/control-center-panel: 7/15] Extract the editable label as a separate widget
- Date: Mon, 14 Jun 2010 05:05:58 +0000 (UTC)
commit f1ce0f87c693f5e16b15024328309896a7047640
Author: Matthias Clasen <mclasen redhat com>
Date: Sun Jun 13 16:47:40 2010 -0400
Extract the editable label as a separate widget
This removes a ton of code duplication and makes things
easier to read.
src/um-editable-button.c | 451 ++++++++++++++++++++++++++++++++++++++++++++++
src/um-editable-button.h | 70 +++++++
2 files changed, 521 insertions(+), 0 deletions(-)
---
diff --git a/src/um-editable-button.c b/src/um-editable-button.c
new file mode 100644
index 0000000..06a33e3
--- /dev/null
+++ b/src/um-editable-button.c
@@ -0,0 +1,451 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright 2009-2010 Red Hat, Inc,
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Written by: Matthias Clasen <mclasen redhat com>
+ */
+
+#include <gdk/gdkkeysyms.h>
+#include "um-editable-button.h"
+
+#define EMPTY_TEXT "\xe2\x80\x94"
+
+struct _UmEditableButtonPrivate {
+ GtkNotebook *notebook;
+ GtkLabel *label;
+ GtkButton *button;
+ GtkEntry *entry;
+
+ gchar *text;
+ gboolean editable;
+ gint weight;
+ gboolean weight_set;
+ gdouble scale;
+ gboolean scale_set;
+};
+
+#define UM_EDITABLE_BUTTON_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), UM_TYPE_EDITABLE_BUTTON, UmEditableButtonPrivate))
+
+enum {
+ PROP_0,
+ PROP_TEXT,
+ PROP_EDITABLE,
+ PROP_SCALE,
+ PROP_SCALE_SET,
+ PROP_WEIGHT,
+ PROP_WEIGHT_SET
+};
+
+G_DEFINE_TYPE (UmEditableButton, um_editable_button, GTK_TYPE_ALIGNMENT);
+
+void
+um_editable_button_set_text (UmEditableButton *button,
+ const gchar *text)
+{
+ UmEditableButtonPrivate *priv;
+ gchar *tmp;
+ GtkWidget *label;
+
+ priv = button->priv;
+
+ tmp = g_strdup (text);
+ g_free (priv->text);
+ priv->text = tmp;
+
+ gtk_entry_set_text (priv->entry, tmp);
+
+ if (tmp == NULL || tmp[0] == '\0')
+ tmp = EMPTY_TEXT;
+
+ gtk_label_set_text (priv->label, tmp);
+ label = gtk_bin_get_child (GTK_BIN (priv->button));
+ gtk_label_set_text (GTK_LABEL (label), tmp);
+
+ g_object_notify (G_OBJECT (button), "text");
+}
+
+const gchar *
+um_editable_button_get_text (UmEditableButton *button)
+{
+ return button->priv->text;
+}
+
+void
+um_editable_button_set_editable (UmEditableButton *button,
+ gboolean editable)
+{
+ UmEditableButtonPrivate *priv;
+
+ priv = button->priv;
+
+ if (priv->editable != editable) {
+ priv->editable = editable;
+
+ gtk_notebook_set_current_page (priv->notebook, editable ? 1 : 0);
+
+ g_object_notify (G_OBJECT (button), "editable");
+ }
+}
+
+gboolean
+um_editable_button_get_editable (UmEditableButton *button)
+{
+ return button->priv->editable;
+}
+
+static void
+update_entry_font (GtkWidget *widget,
+ GtkStyle *previous_style,
+ UmEditableButton *button)
+{
+ UmEditableButtonPrivate *priv = button->priv;
+ PangoFontDescription *desc;
+ gint size;
+
+ if (!priv->weight_set && !priv->scale_set)
+ return;
+
+ desc = pango_font_description_copy (widget->style->font_desc);
+ if (priv->weight_set)
+ pango_font_description_set_weight (desc, priv->weight);
+ if (priv->scale_set) {
+ size = pango_font_description_get_size (desc);
+ pango_font_description_set_size (desc, priv->scale * size);
+ }
+ g_signal_handlers_block_by_func (widget, update_entry_font, button);
+ gtk_widget_modify_font (widget, desc);
+ g_signal_handlers_unblock_by_func (widget, update_entry_font, button);
+
+ pango_font_description_free (desc);
+}
+
+static void
+update_fonts (UmEditableButton *button)
+{
+ PangoAttrList *attrs;
+ PangoAttribute *attr;
+ GtkWidget *label;
+
+ UmEditableButtonPrivate *priv = button->priv;
+
+ attrs = pango_attr_list_new ();
+ if (priv->scale_set) {
+ attr = pango_attr_scale_new (priv->scale);
+ pango_attr_list_insert (attrs, attr);
+ }
+ if (priv->weight_set) {
+ attr = pango_attr_weight_new (priv->weight);
+ pango_attr_list_insert (attrs, attr);
+ }
+
+ gtk_label_set_attributes (priv->label, attrs);
+
+ label = gtk_bin_get_child (GTK_BIN (priv->button));
+ gtk_label_set_attributes (GTK_LABEL (label), attrs);
+
+ pango_attr_list_unref (attrs);
+
+ update_entry_font ((GtkWidget *)priv->entry, NULL, button);
+}
+
+void
+um_editable_button_set_weight (UmEditableButton *button,
+ gint weight)
+{
+ UmEditableButtonPrivate *priv = button->priv;
+
+ if (priv->weight == weight && priv->weight_set)
+ return;
+
+ priv->weight = weight;
+ priv->weight_set = TRUE;
+
+ update_fonts (button);
+
+ g_object_notify (G_OBJECT (button), "weight");
+ g_object_notify (G_OBJECT (button), "weight-set");
+}
+
+gint
+um_editable_button_get_weight (UmEditableButton *button)
+{
+ return button->priv->weight;
+}
+
+void
+um_editable_button_set_scale (UmEditableButton *button,
+ gdouble scale)
+{
+ UmEditableButtonPrivate *priv = button->priv;
+
+ if (priv->scale == scale && priv->scale_set)
+ return;
+
+ priv->scale = scale;
+ priv->scale_set = TRUE;
+
+ update_fonts (button);
+
+ g_object_notify (G_OBJECT (button), "scale");
+ g_object_notify (G_OBJECT (button), "scale-set");
+}
+
+gdouble
+um_editable_button_get_scale (UmEditableButton *button)
+{
+ button->priv->scale;
+}
+
+static void
+um_editable_button_set_property (GObject *object,
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ UmEditableButton *button = UM_EDITABLE_BUTTON (object);
+
+ switch (prop_id) {
+ case PROP_TEXT:
+ um_editable_button_set_text (button, g_value_get_string (value));
+ break;
+ case PROP_EDITABLE:
+ um_editable_button_set_editable (button, g_value_get_boolean (value));
+ break;
+ case PROP_WEIGHT:
+ um_editable_button_set_weight (button, g_value_get_int (value));
+ break;
+ case PROP_WEIGHT_SET:
+ button->priv->weight_set = g_value_get_boolean (value);
+ break;
+ case PROP_SCALE:
+ um_editable_button_set_scale (button, g_value_get_double (value));
+ break;
+ case PROP_SCALE_SET:
+ button->priv->scale_set = g_value_get_boolean (value);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ break;
+ }
+}
+
+static void
+um_editable_button_get_property (GObject *object,
+ guint prop_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ UmEditableButton *button = UM_EDITABLE_BUTTON (object);
+
+ switch (prop_id) {
+ case PROP_TEXT:
+ g_value_set_string (value,
+ um_editable_button_get_text (button));
+ break;
+ case PROP_EDITABLE:
+ g_value_set_boolean (value,
+ um_editable_button_get_editable (button));
+ break;
+ case PROP_WEIGHT:
+ g_value_set_int (value,
+ um_editable_button_get_weight (button));
+ break;
+ case PROP_WEIGHT_SET:
+ g_value_set_boolean (value,
+ button->priv->weight_set);
+ break;
+ case PROP_SCALE:
+ g_value_set_double (value,
+ um_editable_button_get_scale (button));
+ break;
+ case PROP_SCALE_SET:
+ g_value_set_boolean (value,
+ button->priv->scale_set);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ break;
+ }
+}
+
+static void
+um_editable_button_finalize (GObject *object)
+{
+ UmEditableButton *button = (UmEditableButton*)object;
+
+ g_free (button->priv->text);
+
+ G_OBJECT_CLASS (um_editable_button_parent_class)->finalize (object);
+}
+
+static void
+um_editable_button_class_init (UmEditableButtonClass *class)
+{
+ GObjectClass *object_class;
+
+ object_class = G_OBJECT_CLASS (class);
+
+ object_class->set_property = um_editable_button_set_property;
+ object_class->get_property = um_editable_button_get_property;
+ object_class->finalize = um_editable_button_finalize;
+
+ g_object_class_install_property (object_class, PROP_TEXT,
+ g_param_spec_string ("text",
+ "Text", "The text of the button",
+ NULL,
+ G_PARAM_READWRITE));
+
+ g_object_class_install_property (object_class, PROP_EDITABLE,
+ g_param_spec_boolean ("editable",
+ "Editable", "Whether the text can be edited",
+ FALSE,
+ G_PARAM_READWRITE));
+
+ g_object_class_install_property (object_class, PROP_WEIGHT,
+ g_param_spec_int ("weight",
+ "Font Weight", "The font weight to use",
+ 0, G_MAXINT, PANGO_WEIGHT_NORMAL,
+ G_PARAM_READWRITE));
+
+ g_object_class_install_property (object_class, PROP_WEIGHT_SET,
+ g_param_spec_boolean ("weight-set",
+ "Font Weight Set", "Whether a font weight is set",
+ FALSE,
+ G_PARAM_READWRITE));
+
+ g_object_class_install_property (object_class, PROP_SCALE,
+ g_param_spec_double ("scale",
+ "Font Scale", "The font scale to use",
+ 0.0, G_MAXDOUBLE, 1.0,
+ G_PARAM_READWRITE));
+
+ g_object_class_install_property (object_class, PROP_SCALE_SET,
+ g_param_spec_boolean ("scale-set",
+ "Font Scale Set", "Whether a font scale is set",
+ FALSE,
+ G_PARAM_READWRITE));
+
+ g_type_class_add_private (class, sizeof (UmEditableButtonPrivate));
+}
+
+static void
+start_editing (UmEditableButton *button)
+{
+ gtk_notebook_set_current_page (button->priv->notebook, 2);
+}
+
+static void
+stop_editing (UmEditableButton *button)
+{
+ um_editable_button_set_text (button,
+ gtk_entry_get_text (button->priv->entry));
+ gtk_notebook_set_current_page (button->priv->notebook, 1);
+}
+
+static void
+cancel_editing (UmEditableButton *button)
+{
+ gtk_entry_set_text (button->priv->entry,
+ um_editable_button_get_text (button));
+ gtk_notebook_set_current_page (button->priv->notebook, 1);
+}
+
+static void
+button_clicked (GtkWidget *widget,
+ UmEditableButton *button)
+{
+ start_editing (button);
+}
+
+static void
+entry_activated (GtkWidget *widget,
+ UmEditableButton *button)
+{
+ stop_editing (button);
+}
+
+static gboolean
+entry_focus_out (GtkWidget *widget,
+ GdkEventFocus *event,
+ UmEditableButton *button)
+{
+ stop_editing (button);
+ return FALSE;
+}
+
+static gboolean
+entry_key_press (GtkWidget *widget,
+ GdkEventKey *event,
+ UmEditableButton *button)
+{
+ if (event->keyval == GDK_Escape)
+ cancel_editing (button);
+ return FALSE;
+}
+
+static void
+um_editable_button_init (UmEditableButton *button)
+{
+ UmEditableButtonPrivate *priv;
+
+ priv = button->priv = UM_EDITABLE_BUTTON_GET_PRIVATE (button);
+
+ priv->weight = PANGO_WEIGHT_NORMAL;
+ priv->weight_set = FALSE;
+ priv->scale = 1.0;
+ priv->scale_set = FALSE;
+
+ priv->notebook = (GtkNotebook*)gtk_notebook_new ();
+ gtk_notebook_set_show_tabs (priv->notebook, FALSE);
+ gtk_notebook_set_show_border (priv->notebook, FALSE);
+
+ priv->label = (GtkLabel*)gtk_label_new (EMPTY_TEXT);
+ gtk_misc_set_alignment (GTK_MISC (priv->label), 0.0, 0.5);
+ gtk_notebook_append_page (priv->notebook, (GtkWidget*)priv->label, NULL);
+
+ priv->button = (GtkButton*)gtk_button_new_with_label (EMPTY_TEXT);
+ gtk_widget_set_receives_default ((GtkWidget*)priv->button, TRUE);
+ gtk_button_set_relief (priv->button, GTK_RELIEF_NONE);
+ gtk_button_set_alignment (priv->button, 0.0, 0.5);
+ gtk_notebook_append_page (priv->notebook, (GtkWidget*)priv->button, NULL);
+ g_signal_connect (priv->button, "clicked", G_CALLBACK (button_clicked), button);
+
+ priv->entry = (GtkEntry*)gtk_entry_new ();
+ gtk_notebook_append_page (priv->notebook, (GtkWidget*)priv->entry, NULL);
+
+ g_signal_connect (priv->entry, "activate", G_CALLBACK (entry_activated), button);
+ g_signal_connect (priv->entry, "focus-out-event", G_CALLBACK (entry_focus_out), button);
+ g_signal_connect (priv->entry, "key-press-event", G_CALLBACK (entry_key_press), button);
+ g_signal_connect (priv->entry, "style-set", G_CALLBACK (update_entry_font), button);
+
+ gtk_container_add (GTK_CONTAINER (button), (GtkWidget*)priv->notebook);
+
+ gtk_widget_show ((GtkWidget*)priv->notebook);
+ gtk_widget_show ((GtkWidget*)priv->label);
+ gtk_widget_show ((GtkWidget*)priv->button);
+ gtk_widget_show ((GtkWidget*)priv->entry);
+
+ gtk_notebook_set_current_page (priv->notebook, 0);
+}
+
+GtkWidget *
+um_editable_button_new (void)
+{
+ return (GtkWidget *) g_object_new (UM_TYPE_EDITABLE_BUTTON, NULL);
+}
+
+
diff --git a/src/um-editable-button.h b/src/um-editable-button.h
new file mode 100644
index 0000000..7e8d4f9
--- /dev/null
+++ b/src/um-editable-button.h
@@ -0,0 +1,70 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright 2009-2010 Red Hat, Inc,
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Written by: Matthias Clasen <mclasen redhat com>
+ */
+
+#ifndef _UM_EDITABLE_BUTTON_H
+#define _UM_EDITABLE_BUTTON_H
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define UM_TYPE_EDITABLE_BUTTON um_editable_button_get_type()
+
+#define UM_EDITABLE_BUTTON(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), UM_TYPE_EDITABLE_BUTTON, UmEditableButton))
+#define UM_EDITABLE_BUTTON_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), UM_TYPE_EDITABLE_BUTTON, UmEditableButtonClass))
+#define UM_IS_EDITABLE_BUTTON(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), UM_TYPE_EDITABLE_BUTTON))
+#define UM_IS_EDITABLE_BUTTON_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), UM_TYPE_EDITABLE_BUTTON))
+#define UM_EDITABLE_BUTTON_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), UM_TYPE_EDITABLE_BUTTON, UmEditableButtonClass))
+
+typedef struct _UmEditableButton UmEditableButton;
+typedef struct _UmEditableButtonClass UmEditableButtonClass;
+typedef struct _UmEditableButtonPrivate UmEditableButtonPrivate;
+
+struct _UmEditableButton
+{
+ GtkAlignment parent;
+
+ UmEditableButtonPrivate *priv;
+};
+
+struct _UmEditableButtonClass
+{
+ GtkAlignmentClass parent_class;
+};
+
+GType um_editable_button_get_type (void) G_GNUC_CONST;
+GtkWidget *um_editable_button_new (void);
+void um_editable_button_set_text (UmEditableButton *button,
+ const gchar *text);
+const gchar *um_editable_button_get_text (UmEditableButton *button);
+void um_editable_button_set_editable (UmEditableButton *button,
+ gboolean editable);
+gboolean um_editable_button_get_editable (UmEditableButton *button);
+void um_editable_button_set_weight (UmEditableButton *button,
+ gint weight);
+gint um_editable_button_get_weight (UmEditableButton *button);
+void um_editable_button_set_scale (UmEditableButton *button,
+ gdouble scale);
+gdouble um_editable_button_get_scale (UmEditableButton *button);
+
+G_END_DECLS
+
+#endif /* _UM_EDITABLE_BUTTON_H_ */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]