[gtk+/wip/matthiasc/font-variations: 42/58] font chooser: introduce font chooser levels



commit 9a3e8b7d54a8ac7c1c1f8cc785aa4d4597086044
Author: Matthias Clasen <mclasen redhat com>
Date:   Thu Dec 21 10:51:04 2017 -0500

    font chooser: introduce font chooser levels
    
    This is a more systematic approach to reducing the
    level of detail in the font chooser.

 docs/reference/gtk/gtk4-sections.txt |    3 ++
 gtk/gtkfontbutton.c                  |   35 ++++++++++++++++++
 gtk/gtkfontchooser.c                 |   55 ++++++++++++++++++++++++++++
 gtk/gtkfontchooser.h                 |   24 ++++++++++++
 gtk/gtkfontchooserutils.c            |    3 ++
 gtk/gtkfontchooserutils.h            |    1 +
 gtk/gtkfontchooserwidget.c           |   66 +++++++++++++++++++++++++++++++---
 gtk/ui/gtkfontchooserwidget.ui       |    2 +-
 8 files changed, 183 insertions(+), 6 deletions(-)
---
diff --git a/docs/reference/gtk/gtk4-sections.txt b/docs/reference/gtk/gtk4-sections.txt
index f183557..df11937 100644
--- a/docs/reference/gtk/gtk4-sections.txt
+++ b/docs/reference/gtk/gtk4-sections.txt
@@ -1313,6 +1313,9 @@ gtk_font_chooser_get_preview_text
 gtk_font_chooser_set_preview_text
 gtk_font_chooser_get_show_preview_entry
 gtk_font_chooser_set_show_preview_entry
+GtkFontChooserLevel
+gtk_font_chooser_get_level
+gtk_font_chooser_set_level
 GtkFontFilterFunc
 gtk_font_chooser_set_filter_func
 gtk_font_chooser_set_font_map
diff --git a/gtk/gtkfontbutton.c b/gtk/gtkfontbutton.c
index d56a407..2c0023f 100644
--- a/gtk/gtkfontbutton.c
+++ b/gtk/gtkfontbutton.c
@@ -90,6 +90,8 @@ struct _GtkFontButtonPrivate
   gpointer              font_filter_data;
   GDestroyNotify        font_filter_data_destroy;
   GtkCssProvider       *provider;
+
+  GtkFontChooserLevel level;
 };
 
 /* Signals */
@@ -137,6 +139,8 @@ static void gtk_font_button_update_font_info        (GtkFontButton     *gfs);
 static void        gtk_font_button_set_font_name (GtkFontButton *button,
                                                   const char    *fontname);
 static const char *gtk_font_button_get_font_name (GtkFontButton *button);
+static void        gtk_font_button_set_level     (GtkFontButton       *font_button,
+                                                  GtkFontChooserLevel  level);
 
 static guint font_button_signals[LAST_SIGNAL] = { 0 };
 
@@ -693,6 +697,9 @@ gtk_font_button_set_property (GObject      *object,
     case GTK_FONT_CHOOSER_PROP_FONT_DESC:
       gtk_font_button_take_font_desc (font_button, g_value_dup_boxed (value));
       break;
+    case GTK_FONT_CHOOSER_PROP_LEVEL:
+      gtk_font_button_set_level (font_button, g_value_get_enum (value));
+      break;
     case GTK_FONT_CHOOSER_PROP_FONT:
       gtk_font_button_set_font_name (font_button, g_value_get_string (value));
       break;
@@ -721,6 +728,7 @@ gtk_font_button_get_property (GObject    *object,
                               GParamSpec *pspec)
 {
   GtkFontButton *font_button = GTK_FONT_BUTTON (object);
+  GtkFontButtonPrivate *priv = font_button->priv;
   
   switch (param_id) 
     {
@@ -736,6 +744,9 @@ gtk_font_button_get_property (GObject    *object,
     case GTK_FONT_CHOOSER_PROP_FONT_DESC:
       g_value_set_boxed (value, gtk_font_button_get_font_desc (font_button));
       break;
+    case GTK_FONT_CHOOSER_PROP_LEVEL:
+      g_value_set_enum (value, priv->level);
+      break;
     case GTK_FONT_CHOOSER_PROP_FONT:
       g_value_set_string (value, gtk_font_button_get_font_name (font_button));
       break;
@@ -1056,7 +1067,9 @@ gtk_font_button_clicked (GtkButton *button,
 
       if (priv->font_map)
         gtk_font_chooser_set_font_map (font_dialog, priv->font_map);
+
       gtk_font_chooser_set_show_preview_entry (font_dialog, priv->show_preview_entry);
+      g_object_set (font_dialog, "level", priv->level, NULL);
 
       if (priv->preview_text)
         {
@@ -1439,3 +1452,25 @@ gtk_font_button_update_font_info (GtkFontButton *font_button)
 
   gtk_font_button_label_use_font (font_button);
 } 
+
+static void
+gtk_font_button_set_level (GtkFontButton       *button,
+                           GtkFontChooserLevel  level)
+{
+  GtkFontButtonPrivate *priv = button->priv;
+
+  if (priv->level == level)
+    return;
+
+  priv->level = level;
+
+  if (priv->font_dialog)
+    g_object_set (priv->font_dialog, "level", level, NULL);
+
+  g_object_set (button,
+                "show-size", level == GTK_FONT_CHOOSER_LEVEL_FONT,
+                "show-style", level != GTK_FONT_CHOOSER_LEVEL_FAMILY,
+                NULL);
+
+  g_object_notify (G_OBJECT (button), "level");
+}
diff --git a/gtk/gtkfontchooser.c b/gtk/gtkfontchooser.c
index 4502a93..41bb4b4 100644
--- a/gtk/gtkfontchooser.c
+++ b/gtk/gtkfontchooser.c
@@ -106,6 +106,22 @@ gtk_font_chooser_default_init (GtkFontChooserInterface *iface)
                           GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
 
   /**
+   * GtkFontChooser:level:
+   *
+   * The level of granularity to offer for selecting fonts.
+   *
+   * Since: 3.94
+   */
+  g_object_interface_install_property
+     (iface,
+      g_param_spec_enum ("level",
+                         P_("Selection level"),
+                         P_("Whether to select family, face or font"),
+                         GTK_TYPE_FONT_CHOOSER_LEVEL,
+                         GTK_FONT_CHOOSER_LEVEL_FONT,
+                         GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
+
+  /**
    * GtkFontChooser::font-activated:
    * @self: the object which received the signal
    * @fontname: the font name
@@ -481,3 +497,42 @@ gtk_font_chooser_get_font_map (GtkFontChooser *fontchooser)
 
   return fontmap;
 }
+
+/**
+ * gtk_font_chooser_set_level:
+ * @fontchooser: a #GtkFontChooser
+ * @level: the desired level of granularity
+ *
+ * Sets the desired level of granularity for selecting fonts.
+ *
+ * Since: 3.94
+ */
+void
+gtk_font_chooser_set_level (GtkFontChooser      *fontchooser,
+                            GtkFontChooserLevel  level)
+{
+  g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
+
+  g_object_set (fontchooser, "level", level, NULL);
+}
+
+/**
+ * gtk_font_chooser_get_level:
+ * @fontchooser: a #GtkFontChooser
+ *
+ * Returns the current level of granularity for selecting fonts.
+ *
+ * Returns: the current granularity level
+ * Since: 3.94
+ */
+GtkFontChooserLevel
+gtk_font_chooser_get_level (GtkFontChooser *fontchooser)
+{
+  GtkFontChooserLevel level;
+
+  g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), 0);
+
+  g_object_get (fontchooser, "level", &level, NULL);
+
+  return level;
+}
diff --git a/gtk/gtkfontchooser.h b/gtk/gtkfontchooser.h
index 8ca690f..b60d92a 100644
--- a/gtk/gtkfontchooser.h
+++ b/gtk/gtkfontchooser.h
@@ -44,6 +44,24 @@ typedef gboolean (*GtkFontFilterFunc) (const PangoFontFamily *family,
                                        const PangoFontFace   *face,
                                        gpointer               data);
 
+/**
+ * GtkFontChooserLevel:
+ * @GTK_FONT_CHOOSER_LEVEL_FONT: Select an individual
+ *    font, including a size. An example would be: "Arial Bold 11"
+ * @GTK_FONT_CHOOSER_LEVEL_FACE: Select a font face,
+ *    without a size. An example would be: "Arial Bold"
+ * @GTK_FONT_CHOOSER_LEVEL_FAMILY: Select a font family, without
+ *    specifying the face. An example would be: "Arial"
+ *
+ * This enumeration specifies the granularity of font selection
+ * that is desired in a font chooser.
+ */
+typedef enum {
+  GTK_FONT_CHOOSER_LEVEL_FONT,
+  GTK_FONT_CHOOSER_LEVEL_FACE,
+  GTK_FONT_CHOOSER_LEVEL_FAMILY
+} GtkFontChooserLevel;
+
 #define GTK_TYPE_FONT_CHOOSER                  (gtk_font_chooser_get_type ())
 #define GTK_FONT_CHOOSER(obj)                  (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_FONT_CHOOSER, 
GtkFontChooser))
 #define GTK_IS_FONT_CHOOSER(obj)               (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_FONT_CHOOSER))
@@ -122,6 +140,12 @@ void             gtk_font_chooser_set_font_map             (GtkFontChooser   *fo
                                                             PangoFontMap     *fontmap);
 GDK_AVAILABLE_IN_3_18
 PangoFontMap *   gtk_font_chooser_get_font_map             (GtkFontChooser   *fontchooser);
+GDK_AVAILABLE_IN_3_94
+void             gtk_font_chooser_set_level                (GtkFontChooser   *fontchooser,
+                                                            GtkFontChooserLevel level);
+GDK_AVAILABLE_IN_3_94
+GtkFontChooserLevel
+                 gtk_font_chooser_get_level                (GtkFontChooser   *fontchooser);
 
 G_END_DECLS
 
diff --git a/gtk/gtkfontchooserutils.c b/gtk/gtkfontchooserutils.c
index 36ebd8b..09469bd 100644
--- a/gtk/gtkfontchooserutils.c
+++ b/gtk/gtkfontchooserutils.c
@@ -134,6 +134,9 @@ _gtk_font_chooser_install_properties (GObjectClass *klass)
   g_object_class_override_property (klass,
                                     GTK_FONT_CHOOSER_PROP_SHOW_PREVIEW_ENTRY,
                                     "show-preview-entry");
+  g_object_class_override_property (klass,
+                                    GTK_FONT_CHOOSER_PROP_LEVEL,
+                                    "level");
 }
 
 /**
diff --git a/gtk/gtkfontchooserutils.h b/gtk/gtkfontchooserutils.h
index 7c92a64..8d4af1b 100644
--- a/gtk/gtkfontchooserutils.h
+++ b/gtk/gtkfontchooserutils.h
@@ -37,6 +37,7 @@ typedef enum {
   GTK_FONT_CHOOSER_PROP_FONT_DESC,
   GTK_FONT_CHOOSER_PROP_PREVIEW_TEXT,
   GTK_FONT_CHOOSER_PROP_SHOW_PREVIEW_ENTRY,
+  GTK_FONT_CHOOSER_PROP_LEVEL,
   GTK_FONT_CHOOSER_PROP_LAST
 } GtkFontChooserProp;
 
diff --git a/gtk/gtkfontchooserwidget.c b/gtk/gtkfontchooserwidget.c
index 2ef3b81..8a14777 100644
--- a/gtk/gtkfontchooserwidget.c
+++ b/gtk/gtkfontchooserwidget.c
@@ -85,7 +85,6 @@
  * Since: 3.2
  */
 
-
 struct _GtkFontChooserWidgetPrivate
 {
   GtkWidget    *grid;
@@ -102,6 +101,7 @@ struct _GtkFontChooserWidgetPrivate
   gchar           *preview_text;
   gboolean         show_preview_entry;
 
+  GtkWidget *size_label;
   GtkWidget *size_spin;
   GtkWidget *size_slider;
 
@@ -120,6 +120,8 @@ struct _GtkFontChooserWidgetPrivate
 
   GHashTable *axes;
   gboolean updating_variations;
+
+  GtkFontChooserLevel level;
 };
 
 /* Keep in line with GtkTreeStore defined in gtkfontchooserwidget.ui */
@@ -179,6 +181,9 @@ static void     gtk_font_chooser_widget_cell_data_func         (GtkTreeViewColum
                                                                GtkTreeModel      *tree_model,
                                                                GtkTreeIter       *iter,
                                                                gpointer           user_data);
+static void                gtk_font_chooser_widget_set_level (GtkFontChooserWidget *fontchooser,
+                                                              GtkFontChooserLevel   level);
+static GtkFontChooserLevel gtk_font_chooser_widget_get_level (GtkFontChooserWidget *fontchooser);
 
 
 static void gtk_font_chooser_widget_iface_init (GtkFontChooserIface *iface);
@@ -269,6 +274,9 @@ gtk_font_chooser_widget_set_property (GObject         *object,
     case GTK_FONT_CHOOSER_PROP_SHOW_PREVIEW_ENTRY:
       gtk_font_chooser_widget_set_show_preview_entry (fontchooser, g_value_get_boolean (value));
       break;
+    case GTK_FONT_CHOOSER_PROP_LEVEL:
+      gtk_font_chooser_widget_set_level (fontchooser, g_value_get_enum (value));
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -297,6 +305,9 @@ gtk_font_chooser_widget_get_property (GObject         *object,
     case GTK_FONT_CHOOSER_PROP_SHOW_PREVIEW_ENTRY:
       g_value_set_boolean (value, gtk_font_chooser_widget_get_show_preview_entry (fontchooser));
       break;
+    case GTK_FONT_CHOOSER_PROP_LEVEL:
+      g_value_set_enum (value, gtk_font_chooser_widget_get_level (fontchooser));
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -662,6 +673,7 @@ gtk_font_chooser_widget_class_init (GtkFontChooserWidgetClass *klass)
   gtk_widget_class_bind_template_child_private (widget_class, GtkFontChooserWidget, model);
   gtk_widget_class_bind_template_child_private (widget_class, GtkFontChooserWidget, filter_model);
   gtk_widget_class_bind_template_child_private (widget_class, GtkFontChooserWidget, preview);
+  gtk_widget_class_bind_template_child_private (widget_class, GtkFontChooserWidget, size_label);
   gtk_widget_class_bind_template_child_private (widget_class, GtkFontChooserWidget, size_spin);
   gtk_widget_class_bind_template_child_private (widget_class, GtkFontChooserWidget, size_slider);
   gtk_widget_class_bind_template_child_private (widget_class, GtkFontChooserWidget, grid);
@@ -810,7 +822,6 @@ gtk_font_chooser_widget_load_fonts (GtkFontChooserWidget *fontchooser,
   GtkListStore *list_store;
   gint n_families, i;
   PangoFontFamily **families;
-  gchar *family_and_face;
   guint fontconfig_timestamp;
   gboolean need_reload;
   PangoFontMap *font_map;
@@ -859,21 +870,29 @@ gtk_font_chooser_widget_load_fonts (GtkFontChooserWidget *fontchooser,
         {
           GtkDelayedFontDescription *desc;
           const gchar *face_name;
+          char *title;
 
           face_name = pango_font_face_get_face_name (faces[j]);
 
-          family_and_face = g_strconcat (fam_name, " ", face_name, NULL);
+          if (priv->level == GTK_FONT_CHOOSER_LEVEL_FAMILY)
+            title = g_strdup (fam_name);
+          else
+            title = g_strconcat (fam_name, " ", face_name, NULL);
+
           desc = gtk_delayed_font_description_new (faces[j]);
 
           gtk_list_store_insert_with_values (list_store, &iter, -1,
                                              FAMILY_COLUMN, families[i],
                                              FACE_COLUMN, faces[j],
                                              FONT_DESC_COLUMN, desc,
-                                             PREVIEW_TITLE_COLUMN, family_and_face,
+                                             PREVIEW_TITLE_COLUMN, title,
                                              -1);
 
-          g_free (family_and_face);
+          g_free (title);
           gtk_delayed_font_description_unref (desc);
+
+          if (priv->level == GTK_FONT_CHOOSER_LEVEL_FAMILY)
+            break;
         }
 
       g_free (faces);
@@ -1639,6 +1658,43 @@ gtk_font_chooser_widget_set_filter_func (GtkFontChooser  *chooser,
 }
 
 static void
+gtk_font_chooser_widget_set_level (GtkFontChooserWidget *fontchooser,
+                                   GtkFontChooserLevel   level)
+{
+  GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
+
+  if (priv->level == level)
+    return;
+
+  priv->level = level;
+
+  if (level == GTK_FONT_CHOOSER_LEVEL_FONT)
+    {
+      gtk_widget_show (priv->size_label);
+      gtk_widget_show (priv->size_slider);
+      gtk_widget_show (priv->size_spin);
+    }
+  else
+   {
+      gtk_widget_hide (priv->size_label);
+      gtk_widget_hide (priv->size_slider);
+      gtk_widget_hide (priv->size_spin);
+   }
+
+  gtk_font_chooser_widget_load_fonts (fontchooser, TRUE);
+
+  g_object_notify (G_OBJECT (fontchooser), "level");
+}
+
+static GtkFontChooserLevel
+gtk_font_chooser_widget_get_level (GtkFontChooserWidget *fontchooser)
+{
+  GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
+
+  return priv->level;
+}
+
+static void
 gtk_font_chooser_widget_iface_init (GtkFontChooserIface *iface)
 {
   iface->get_font_family = gtk_font_chooser_widget_get_family;
diff --git a/gtk/ui/gtkfontchooserwidget.ui b/gtk/ui/gtkfontchooserwidget.ui
index fce6278..d74962b 100644
--- a/gtk/ui/gtkfontchooserwidget.ui
+++ b/gtk/ui/gtkfontchooserwidget.ui
@@ -118,7 +118,7 @@
                   </packing>
                 </child>
                 <child>
-                  <object class="GtkLabel">
+                  <object class="GtkLabel" id="size_label">
                     <property name="visible">1</property>
                     <property name="label" translatable="yes">Size</property>
                     <property name="xalign">0</property>


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