[gtk+/font-chooser-api] fixup! GtkFontChooser implementation for GtkFontButton



commit 30f8b1db49b8ed4e96e5e19d5751d60af011be7a
Author: Christian Persch <chpe gnome org>
Date:   Mon Sep 5 15:21:08 2011 +0200

    fixup! GtkFontChooser implementation for GtkFontButton
    
    The font property and its notification shouldn't be directly forwarded
    to the font chooser dialogue, since we only want to update the button
    from the dialogue's response handler.
    
    This still isn't quite right since the family, face, and size accessors
    will return NULL resp. -1 when the dialogue hasn't been poppup once yet.

 gtk/gtkfontbutton.c |   81 +++++++++++++++++++++++++++++----------------------
 1 files changed, 46 insertions(+), 35 deletions(-)
---
diff --git a/gtk/gtkfontbutton.c b/gtk/gtkfontbutton.c
index f15fec1..7492a9a 100644
--- a/gtk/gtkfontbutton.c
+++ b/gtk/gtkfontbutton.c
@@ -75,6 +75,9 @@ struct _GtkFontButtonPrivate
   GtkWidget     *font_label;
   GtkWidget     *size_label;
 
+  PangoFontFamily   *font_family;
+  PangoFontFace     *font_face;
+  gint               font_size;
   gchar             *preview_text;
   GtkFontFilterFunc  font_filter;
   gpointer           font_filter_data;
@@ -213,10 +216,7 @@ gtk_font_button_font_chooser_get_font_family (GtkFontChooser *chooser)
   GtkFontButton *font_button = GTK_FONT_BUTTON (chooser);
   GtkFontButtonPrivate *priv = font_button->priv;
 
-  if (priv->font_dialog)
-    return gtk_font_chooser_get_family (GTK_FONT_CHOOSER (priv->font_dialog));
-
-  return NULL;
+  return priv->font_family;
 }
 
 static PangoFontFace *
@@ -225,10 +225,7 @@ gtk_font_button_font_chooser_get_font_face (GtkFontChooser *chooser)
   GtkFontButton *font_button = GTK_FONT_BUTTON (chooser);
   GtkFontButtonPrivate *priv = font_button->priv;
 
-  if (priv->font_dialog)
-    return gtk_font_chooser_get_face (GTK_FONT_CHOOSER (priv->font_dialog));
-
-  return NULL;
+  return priv->font_face;
 }
 
 static int
@@ -237,10 +234,7 @@ gtk_font_button_font_chooser_get_font_size (GtkFontChooser *chooser)
   GtkFontButton *font_button = GTK_FONT_BUTTON (chooser);
   GtkFontButtonPrivate *priv = font_button->priv;
 
-  if (priv->font_dialog)
-    return gtk_font_chooser_get_size (GTK_FONT_CHOOSER (priv->font_dialog));
-
-  return -1;
+  return priv->font_size;
 }
 
 static void
@@ -272,22 +266,10 @@ gtk_font_button_font_chooser_notify (GObject    *object,
                                      GParamSpec *pspec,
                                      gpointer    user_data)
 {
-  gpointer iface;
-
-  iface = g_type_interface_peek (g_type_class_peek (G_OBJECT_TYPE (object)),
-                                 GTK_TYPE_FONT_CHOOSER);
-  if (g_object_interface_find_property (iface, pspec->name))
+  /* We do not forward the notification of the "font" property to the dialog! */
+  if (pspec->name == I_("preview-text") ||
+      pspec->name == I_("show-preview-entry"))
     g_object_notify_by_pspec (user_data, pspec);
-  if (strcmp (pspec->name, "font") == 0)
-    g_object_notify (user_data, "font-name");
-}
-
-static void
-gtk_font_button_font_chooser_font_activated (GtkFontChooser *receiver,
-                                             const gchar    *fontname,
-                                             GtkFontChooser *delegate)
-{
-  _gtk_font_chooser_font_activated (delegate, fontname);
 }
 
 static void
@@ -461,6 +443,9 @@ gtk_font_button_init (GtkFontButton *font_button)
   font_button->priv->show_size = TRUE;
   font_button->priv->show_preview_entry = FALSE;
   font_button->priv->font_dialog = NULL;
+  font_button->priv->font_family = NULL;
+  font_button->priv->font_face = NULL;
+  font_button->priv->font_size = -1;
   font_button->priv->title = g_strdup (_("Pick a Font"));
 
   font_button->priv->inside = gtk_font_button_create_inside (font_button);
@@ -490,6 +475,14 @@ gtk_font_button_finalize (GObject *object)
   g_free (font_button->priv->preview_text);
   font_button->priv->preview_text = NULL;
 
+  if (font_button->priv->font_family)
+    g_object_unref (font_button->priv->font_family);
+  font_button->priv->font_family = NULL;
+
+  if (font_button->priv->font_face)
+    g_object_unref (font_button->priv->font_face);
+  font_button->priv->font_face = NULL;
+
   G_OBJECT_CLASS (gtk_font_button_parent_class)->finalize (object);
 }
 
@@ -901,6 +894,7 @@ gtk_font_button_set_font_name (GtkFontButton *font_button,
   else
     result = FALSE;
 
+  g_object_notify (G_OBJECT (font_button), "font");
   g_object_notify (G_OBJECT (font_button), "font-name");
 
   return result;
@@ -942,11 +936,6 @@ gtk_font_button_clicked (GtkButton *button)
           priv->font_filter_data_destroy = NULL;
         }
 
-      g_signal_connect (font_dialog, "notify",
-                        G_CALLBACK (gtk_font_button_font_chooser_notify), button);
-      g_signal_connect (font_dialog, "font-activated",
-                        G_CALLBACK (gtk_font_button_font_chooser_font_activated), button);
-
       if (gtk_widget_is_toplevel (parent) && GTK_IS_WINDOW (parent))
         {
           if (GTK_WINDOW (parent) != gtk_window_get_transient_for (GTK_WINDOW (font_dialog)))
@@ -956,6 +945,9 @@ gtk_font_button_clicked (GtkButton *button)
                                 gtk_window_get_modal (GTK_WINDOW (parent)));
         }
 
+      g_signal_connect (font_dialog, "notify",
+                        G_CALLBACK (gtk_font_button_font_chooser_notify), button);
+
       g_signal_connect (font_dialog, "response",
                         G_CALLBACK (response_cb), font_button);
 
@@ -979,14 +971,33 @@ response_cb (GtkDialog *dialog,
              gpointer   data)
 {
   GtkFontButton *font_button = GTK_FONT_BUTTON (data);
+  GtkFontButtonPrivate *priv = font_button->priv;
+  GtkFontChooser *font_chooser;
+
   gtk_widget_hide (font_button->priv->font_dialog);
 
   if (response_id != GTK_RESPONSE_OK)
     return;
 
-  g_free (font_button->priv->fontname);
-  font_button->priv->fontname = gtk_font_chooser_get_font (GTK_FONT_CHOOSER (font_button->priv->font_dialog));
-  
+  font_chooser = GTK_FONT_CHOOSER (priv->font_dialog);
+
+  g_free (priv->fontname);
+  priv->fontname = gtk_font_chooser_get_font (font_chooser);
+
+  if (priv->font_family)
+    g_object_unref (priv->font_family);
+  priv->font_family = gtk_font_chooser_get_family (font_chooser);
+  if (priv->font_family)
+    g_object_ref (priv->font_family);
+
+  if (priv->font_face)
+    g_object_unref (priv->font_face);
+  priv->font_face = gtk_font_chooser_get_face (font_chooser);
+  if (priv->font_face)
+    g_object_ref (priv->font_face);
+
+  priv->font_size = gtk_font_chooser_get_size (font_chooser);
+
   /* Set label font */
   gtk_font_button_update_font_info (font_button);
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]