[gtk+/composite-templates-new: 22/32] GtkFontChooserDialog: Define children with a GtkBuilder template



commit e42059e574e1924a6ed380e786e67c18fb46efca
Author: Tristan Van Berkom <tristanvb openismus com>
Date:   Tue Mar 26 19:13:12 2013 +0900

    GtkFontChooserDialog: Define children with a GtkBuilder template

 gtk/Makefile.am             |    1 +
 gtk/gtk.gresource.xml       |    1 +
 gtk/gtkfontchooserdialog.c  |   66 +++++++++++-------------------------
 gtk/gtkfontchooserdialog.ui |   78 +++++++++++++++++++++++++++++++++++++++++++
 gtk/tests/templates.c       |   11 ++++++
 5 files changed, 112 insertions(+), 45 deletions(-)
---
diff --git a/gtk/Makefile.am b/gtk/Makefile.am
index 7e5b0d2..426c4d7 100644
--- a/gtk/Makefile.am
+++ b/gtk/Makefile.am
@@ -1104,6 +1104,7 @@ COMPOSITE_TEMPLATES =                     \
        gtkdialog.ui                    \
        gtkfilechooserdefault.ui        \
        gtkfilechooserdialog.ui         \
+       gtkfontchooserdialog.ui         \
        gtkfontchooserwidget.ui         \
        gtkinfobar.ui                   \
        gtklockbutton.ui                \
diff --git a/gtk/gtk.gresource.xml b/gtk/gtk.gresource.xml
index 9ca8d7a..43f0af0 100644
--- a/gtk/gtk.gresource.xml
+++ b/gtk/gtk.gresource.xml
@@ -19,6 +19,7 @@
     <file compressed="true">gtkdialog.ui</file>
     <file compressed="true">gtkfilechooserdefault.ui</file>
     <file compressed="true">gtkfilechooserdialog.ui</file>
+    <file compressed="true">gtkfontchooserdialog.ui</file>
     <file compressed="true">gtkfontchooserwidget.ui</file>
     <file compressed="true">gtkinfobar.ui</file>
     <file compressed="true">gtklockbutton.ui</file>
diff --git a/gtk/gtkfontchooserdialog.c b/gtk/gtkfontchooserdialog.c
index 6724440..d1209de 100644
--- a/gtk/gtkfontchooserdialog.c
+++ b/gtk/gtkfontchooserdialog.c
@@ -110,80 +110,56 @@ gtk_font_chooser_dialog_get_property (GObject      *object,
 }
 
 static void
+font_activated_cb (GtkFontChooser *fontchooser,
+                   const gchar    *fontname,
+                   gpointer        user_data)
+{
+  GtkDialog *dialog = user_data;
+
+  gtk_dialog_response (dialog, GTK_RESPONSE_OK);
+}
+
+static void
 gtk_font_chooser_dialog_class_init (GtkFontChooserDialogClass *klass)
 {
   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
 
   gobject_class->get_property = gtk_font_chooser_dialog_get_property;
   gobject_class->set_property = gtk_font_chooser_dialog_set_property;
 
   _gtk_font_chooser_install_properties (gobject_class);
 
-  g_type_class_add_private (klass, sizeof (GtkFontChooserDialogPrivate));
-}
+  /* Bind class to template
+   */
+  gtk_widget_class_set_template_from_resource (widget_class,
+                                              "/org/gtk/libgtk/gtkfontchooserdialog.ui");
 
-static void
-font_activated_cb (GtkFontChooser *fontchooser,
-                   const gchar    *fontname,
-                   gpointer        user_data)
-{
-  GtkDialog *dialog = user_data;
+  gtk_widget_class_bind_child (widget_class, GtkFontChooserDialogPrivate, fontchooser);
+  gtk_widget_class_bind_child (widget_class, GtkFontChooserDialogPrivate, select_button);
+  gtk_widget_class_bind_child (widget_class, GtkFontChooserDialogPrivate, cancel_button);
+  gtk_widget_class_bind_callback (widget_class, font_activated_cb);
 
-  gtk_dialog_response (dialog, GTK_RESPONSE_OK);
+  g_type_class_add_private (klass, sizeof (GtkFontChooserDialogPrivate));
 }
 
 static void
 gtk_font_chooser_dialog_init (GtkFontChooserDialog *fontchooserdiag)
 {
   GtkFontChooserDialogPrivate *priv;
-  GtkDialog *dialog = GTK_DIALOG (fontchooserdiag);
-  GtkWidget *action_area, *content_area;
 
   fontchooserdiag->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontchooserdiag,
                                                        GTK_TYPE_FONT_CHOOSER_DIALOG,
                                                        GtkFontChooserDialogPrivate);
   priv = fontchooserdiag->priv;
 
-  content_area = gtk_dialog_get_content_area (dialog);
-  action_area = gtk_dialog_get_action_area (dialog);
-
-  gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
-  gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
-  gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
-  gtk_box_set_spacing (GTK_BOX (action_area), 6);
-
-  gtk_widget_push_composite_child ();
-
-  gtk_window_set_resizable (GTK_WINDOW (fontchooserdiag), TRUE);
-
-  /* Create the content area */
-  priv->fontchooser = gtk_font_chooser_widget_new ();
-  gtk_container_set_border_width (GTK_CONTAINER (priv->fontchooser), 5);
-  gtk_widget_show (priv->fontchooser);
-  gtk_box_pack_start (GTK_BOX (content_area),
-                      priv->fontchooser, TRUE, TRUE, 0);
-
-  g_signal_connect (priv->fontchooser, "font-activated",
-                    G_CALLBACK (font_activated_cb), dialog);
-
-  /* Create the action area */
-  priv->cancel_button = gtk_dialog_add_button (dialog,
-                                               GTK_STOCK_CANCEL,
-                                               GTK_RESPONSE_CANCEL);
-  priv->select_button = gtk_dialog_add_button (dialog,
-                                               _("_Select"),
-                                               GTK_RESPONSE_OK);
-  gtk_widget_grab_default (priv->select_button);
+  gtk_widget_init_template (GTK_WIDGET (fontchooserdiag));
 
   gtk_dialog_set_alternative_button_order (GTK_DIALOG (fontchooserdiag),
                                            GTK_RESPONSE_OK,
                                            GTK_RESPONSE_CANCEL,
                                            -1);
 
-  gtk_window_set_title (GTK_WINDOW (fontchooserdiag), _("Font Selection"));
-
-  gtk_widget_pop_composite_child ();
-
   _gtk_font_chooser_set_delegate (GTK_FONT_CHOOSER (fontchooserdiag),
                                   GTK_FONT_CHOOSER (priv->fontchooser));
 }
diff --git a/gtk/gtkfontchooserdialog.ui b/gtk/gtkfontchooserdialog.ui
new file mode 100644
index 0000000..8878052
--- /dev/null
+++ b/gtk/gtkfontchooserdialog.ui
@@ -0,0 +1,78 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface domain="gtk30">
+  <!-- interface-requires gtk+ 3.10 -->
+  <template class="GtkFontChooserDialog" parent="GtkDialog">
+    <property name="can_focus">False</property>
+    <property name="border_width">5</property>
+    <property name="title" translatable="yes">Font Selection</property>
+    <property name="type_hint">dialog</property>
+    <child internal-child="vbox">
+      <object class="GtkBox" id="dialog-vbox1">
+        <property name="can_focus">False</property>
+        <property name="orientation">vertical</property>
+        <property name="spacing">2</property>
+        <child internal-child="action_area">
+          <object class="GtkButtonBox" id="dialog-action_area1">
+            <property name="can_focus">False</property>
+            <property name="layout_style">end</property>
+            <child>
+              <object class="GtkButton" id="cancel_button">
+                <property name="label">gtk-cancel</property>
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="receives_default">True</property>
+                <property name="use_stock">True</property>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="fill">True</property>
+                <property name="position">0</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkButton" id="select_button">
+                <property name="label" translatable="yes">_Select</property>
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="can_default">True</property>
+                <property name="has_default">True</property>
+                <property name="receives_default">True</property>
+                <property name="use_underline">True</property>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="fill">True</property>
+                <property name="position">1</property>
+              </packing>
+            </child>
+          </object>
+          <packing>
+            <property name="expand">False</property>
+            <property name="fill">True</property>
+            <property name="pack_type">end</property>
+            <property name="position">0</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkFontChooserWidget" id="fontchooser">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="border_width">5</property>
+            <property name="orientation">vertical</property>
+            <property name="spacing">6</property>
+            <signal name="font-activated" handler="font_activated_cb" swapped="no"/>
+          </object>
+          <packing>
+            <property name="expand">True</property>
+            <property name="fill">True</property>
+            <property name="position">1</property>
+          </packing>
+        </child>
+      </object>
+    </child>
+    <action-widgets>
+      <action-widget response="-6">cancel_button</action-widget>
+      <action-widget response="-5">select_button</action-widget>
+    </action-widgets>
+  </template>
+</interface>
diff --git a/gtk/tests/templates.c b/gtk/tests/templates.c
index 6c61bda..cdbaa2e 100644
--- a/gtk/tests/templates.c
+++ b/gtk/tests/templates.c
@@ -234,6 +234,16 @@ test_font_chooser_widget_basic (void)
   gtk_widget_destroy (widget);
 }
 
+static void
+test_font_chooser_dialog_basic (void)
+{
+  GtkWidget *widget;
+
+  widget = gtk_font_chooser_dialog_new ("Choose a font !", NULL);
+  g_assert (GTK_IS_FONT_CHOOSER_DIALOG (widget));
+  gtk_widget_destroy (widget);
+}
+
 int
 main (int argc, char **argv)
 {
@@ -261,6 +271,7 @@ main (int argc, char **argv)
   g_test_add_func ("/Template/GtkFileChooserWidget/Basic", test_file_chooser_widget_basic);
   g_test_add_func ("/Template/GtkFileChooserDialog/Basic", test_file_chooser_dialog_basic);
   g_test_add_func ("/Template/GtkFontChooserWidget/Basic", test_font_chooser_widget_basic);
+  g_test_add_func ("/Template/GtkFontChooserDialog/Basic", test_font_chooser_dialog_basic);
 
   return g_test_run();
 }


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