[gtk+/font-chooser-api: 4/4] GtkFontChooser implementation for GtkFontButton



commit 672c113afa4f6cfc3faf9698f9b017ca524a0b60
Author: Christian Persch <chpe gnome org>
Date:   Mon Sep 5 14:10:20 2011 +0200

    GtkFontChooser implementation for GtkFontButton

 gtk/gtkfontbutton.c |  248 +++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 240 insertions(+), 8 deletions(-)
---
diff --git a/gtk/gtkfontbutton.c b/gtk/gtkfontbutton.c
index 05a5ee4..a3fec49 100644
--- a/gtk/gtkfontbutton.c
+++ b/gtk/gtkfontbutton.c
@@ -43,6 +43,8 @@
 
 #include <string.h>
 #include <stdio.h>
+#include "gtkfontchooserutils.h"
+#include <X11/Xlib.h>
 
 
 /**
@@ -67,11 +69,17 @@ struct _GtkFontButtonPrivate
   guint         use_size : 1;
   guint         show_style : 1;
   guint         show_size : 1;
+  guint         show_preview_entry : 1;
    
   GtkWidget     *font_dialog;
   GtkWidget     *inside;
   GtkWidget     *font_label;
   GtkWidget     *size_label;
+
+  gchar             *preview_text;
+  GtkFontFilterFunc  font_filter;
+  gpointer           font_filter_data;
+  GDestroyNotify     font_filter_data_destroy;
 };
 
 /* Signals */
@@ -119,7 +127,184 @@ static void gtk_font_button_update_font_info        (GtkFontButton     *gfs);
 
 static guint font_button_signals[LAST_SIGNAL] = { 0 };
 
-G_DEFINE_TYPE (GtkFontButton, gtk_font_button, GTK_TYPE_BUTTON)
+static void
+clear_font_filter_data (GtkFontButton *font_button)
+{
+  GtkFontButtonPrivate *priv = font_button->priv;
+
+  if (priv->font_filter_data_destroy)
+    priv->font_filter_data_destroy (priv->font_filter_data);
+  priv->font_filter = NULL;
+  priv->font_filter_data = NULL;
+  priv->font_filter_data_destroy = NULL;
+}
+
+static gchar *
+gtk_font_button_get_preview_text (GtkFontButton *font_button)
+{
+  GtkFontButtonPrivate *priv = font_button->priv;
+
+  if (priv->font_dialog)
+    return gtk_font_chooser_get_preview_text (GTK_FONT_CHOOSER (priv->font_dialog));
+
+  return g_strdup (priv->preview_text);
+}
+
+static void
+gtk_font_button_set_preview_text (GtkFontButton *font_button,
+                                  const gchar   *preview_text)
+{
+  GtkFontButtonPrivate *priv = font_button->priv;
+
+  if (priv->font_dialog)
+    {
+      gtk_font_chooser_set_preview_text (GTK_FONT_CHOOSER (priv->font_dialog),
+                                         preview_text);
+      return;
+    }
+
+  g_free (priv->preview_text);
+  priv->preview_text = g_strdup (preview_text);
+}
+
+
+static gboolean
+gtk_font_button_get_show_preview_entry (GtkFontButton *font_button)
+{
+  GtkFontButtonPrivate *priv = font_button->priv;
+
+  if (priv->font_dialog)
+    return gtk_font_chooser_get_show_preview_entry (GTK_FONT_CHOOSER (priv->font_dialog));
+
+  return priv->show_preview_entry;
+}
+
+static void
+gtk_font_button_set_show_preview_entry (GtkFontButton *font_button,
+                                        gboolean       show)
+{
+  GtkFontButtonPrivate *priv = font_button->priv;
+
+  if (priv->font_dialog)
+    return gtk_font_chooser_set_show_preview_entry (GTK_FONT_CHOOSER (priv->font_dialog), show);
+
+  priv->show_preview_entry = show != FALSE;
+}
+
+static gchar *
+gtk_font_button_font_chooser_get_font (GtkFontChooser *chooser)
+{
+  GtkFontButton *font_button = GTK_FONT_BUTTON (chooser);
+
+  return g_strdup (gtk_font_button_get_font_name (font_button));
+}
+
+static gboolean
+gtk_font_button_font_chooser_set_font (GtkFontChooser *chooser,
+                                       const gchar    *fontname)
+{
+  GtkFontButton *font_button = GTK_FONT_BUTTON (chooser);
+
+  return gtk_font_button_set_font_name (font_button, fontname);
+}
+
+static PangoFontFamily *
+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;
+}
+
+static PangoFontFace *
+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;
+}
+
+static int
+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;
+}
+
+static void
+gtk_font_button_font_chooser_set_filter_func (GtkFontChooser    *chooser,
+                                              GtkFontFilterFunc  filter_func,
+                                              gpointer           filter_data,
+                                              GDestroyNotify     data_destroy)
+{
+  GtkFontButton *font_button = GTK_FONT_BUTTON (chooser);
+  GtkFontButtonPrivate *priv = font_button->priv;
+
+  if (priv->font_dialog)
+    {
+      gtk_font_chooser_set_filter_func (GTK_FONT_CHOOSER (priv->font_dialog),
+                                        filter_func,
+                                        filter_data,
+                                        data_destroy);
+      return;
+    }
+
+  clear_font_filter_data (font_button);
+  priv->font_filter = filter_func;
+  priv->font_filter_data = filter_data;
+  priv->font_filter_data_destroy = data_destroy;
+}
+
+static void
+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))
+    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
+gtk_font_button_font_chooser_iface_init (GtkFontChooserIface *iface)
+{
+  iface->get_font = gtk_font_button_font_chooser_get_font;
+  iface->set_font = gtk_font_button_font_chooser_set_font;
+  iface->get_font_family = gtk_font_button_font_chooser_get_font_family;
+  iface->get_font_face = gtk_font_button_font_chooser_get_font_face;
+  iface->get_font_size = gtk_font_button_font_chooser_get_font_size;
+  iface->set_filter_func = gtk_font_button_font_chooser_set_filter_func;
+}
+
+G_DEFINE_TYPE_WITH_CODE (GtkFontButton, gtk_font_button, GTK_TYPE_BUTTON,
+                         G_IMPLEMENT_INTERFACE (GTK_TYPE_FONT_CHOOSER,
+                                                gtk_font_button_font_chooser_iface_init))
 
 static void
 gtk_font_button_class_init (GtkFontButtonClass *klass)
@@ -138,6 +323,8 @@ gtk_font_button_class_init (GtkFontButtonClass *klass)
   
   klass->font_set = NULL;
 
+  _gtk_font_chooser_install_properties (gobject_class);
+
   /**
    * GtkFontButton:title:
    * 
@@ -273,6 +460,7 @@ gtk_font_button_init (GtkFontButton *font_button)
   font_button->priv->use_size = FALSE;
   font_button->priv->show_style = TRUE;
   font_button->priv->show_size = TRUE;
+  font_button->priv->show_preview_entry = FALSE;
   font_button->priv->font_dialog = NULL;
   font_button->priv->title = g_strdup (_("Pick a Font"));
 
@@ -297,7 +485,12 @@ gtk_font_button_finalize (GObject *object)
   
   g_free (font_button->priv->title);
   font_button->priv->title = NULL;
-  
+
+  clear_font_filter_data (font_button);
+
+  g_free (font_button->priv->preview_text);
+  font_button->priv->preview_text = NULL;
+
   G_OBJECT_CLASS (gtk_font_button_parent_class)->finalize (object);
 }
 
@@ -308,12 +501,19 @@ gtk_font_button_set_property (GObject      *object,
                               GParamSpec   *pspec)
 {
   GtkFontButton *font_button = GTK_FONT_BUTTON (object);
-  
+
   switch (param_id) 
     {
+    case GTK_FONT_CHOOSER_PROP_PREVIEW_TEXT:
+      gtk_font_button_set_preview_text (font_button, g_value_get_string (value));
+      break;
+    case GTK_FONT_CHOOSER_PROP_SHOW_PREVIEW_ENTRY:
+      gtk_font_button_set_show_preview_entry (font_button, g_value_get_boolean (value));
+      break;
     case PROP_TITLE:
       gtk_font_button_set_title (font_button, g_value_get_string (value));
       break;
+    case GTK_FONT_CHOOSER_PROP_FONT:
     case PROP_FONT_NAME:
       gtk_font_button_set_font_name (font_button, g_value_get_string (value));
       break;
@@ -345,9 +545,16 @@ gtk_font_button_get_property (GObject    *object,
   
   switch (param_id) 
     {
+    case GTK_FONT_CHOOSER_PROP_PREVIEW_TEXT:
+      g_value_set_string (value, gtk_font_button_get_preview_text (font_button));
+      break;
+    case GTK_FONT_CHOOSER_PROP_SHOW_PREVIEW_ENTRY:
+      g_value_set_boolean (value, gtk_font_button_get_show_preview_entry (font_button));
+      break;
     case PROP_TITLE:
       g_value_set_string (value, gtk_font_button_get_title (font_button));
       break;
+    case GTK_FONT_CHOOSER_PROP_FONT:
     case PROP_FONT_NAME:
       g_value_set_string (value, gtk_font_button_get_font_name (font_button));
       break;
@@ -705,18 +912,42 @@ gtk_font_button_clicked (GtkButton *button)
 {
   GtkFontChooser *font_dialog;
   GtkFontButton  *font_button = GTK_FONT_BUTTON (button);
+  GtkFontButtonPrivate *priv = font_button->priv;
   
   if (!font_button->priv->font_dialog) 
     {
       GtkWidget *parent;
       
       parent = gtk_widget_get_toplevel (GTK_WIDGET (font_button));
-      
-      font_button->priv->font_dialog = gtk_font_chooser_dialog_new (font_button->priv->title,
-                                                                    NULL);
-      
+
+      priv->font_dialog = gtk_font_chooser_dialog_new (priv->title, NULL);
       font_dialog = GTK_FONT_CHOOSER (font_button->priv->font_dialog);
-      
+
+      gtk_font_chooser_set_show_preview_entry (font_dialog, priv->show_preview_entry);
+
+      if (priv->preview_text)
+        {
+          gtk_font_chooser_set_preview_text (font_dialog, priv->preview_text);
+          g_free (priv->preview_text);
+          priv->preview_text = NULL;
+        }
+
+      if (priv->font_filter)
+        {
+          gtk_font_chooser_set_filter_func (font_dialog,
+                                            priv->font_filter,
+                                            priv->font_filter_data,
+                                            priv->font_filter_data_destroy);
+          priv->font_filter = NULL;
+          priv->font_filter_data = NULL;
+          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)))
@@ -760,6 +991,7 @@ response_cb (GtkDialog *dialog,
   /* Set label font */
   gtk_font_button_update_font_info (font_button);
 
+  g_object_notify (G_OBJECT (font_button), "font");
   g_object_notify (G_OBJECT (font_button), "font-name");
   
   /* Emit font_set signal */



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