[gtk+/composite-templates-new: 19/28] GtkColorChooserDialog: Define children with a GtkBuilder template



commit 8c725231d3af2328d5f622bc420c1a3790b0e4a5
Author: Tristan Van Berkom <tristanvb openismus com>
Date:   Sun Mar 24 00:41:07 2013 +0900

    GtkColorChooserDialog: Define children with a GtkBuilder template

 gtk/Makefile.am              |    1 +
 gtk/gtk.gresource.xml        |    1 +
 gtk/gtkcolorchooserdialog.c  |   68 ++++++-----------------------------
 gtk/gtkcolorchooserdialog.ui |   82 ++++++++++++++++++++++++++++++++++++++++++
 gtk/tests/templates.c        |   11 ++++++
 po/POTFILES.in               |    1 +
 6 files changed, 107 insertions(+), 57 deletions(-)
---
diff --git a/gtk/Makefile.am b/gtk/Makefile.am
index b644c8d..403995d 100644
--- a/gtk/Makefile.am
+++ b/gtk/Makefile.am
@@ -1099,6 +1099,7 @@ COMPOSITE_TEMPLATES =                     \
        gtkappchooserdialog.ui          \
        gtkappchooserwidget.ui          \
        gtkassistant.ui                 \
+       gtkcolorchooserdialog.ui        \
        gtkdialog.ui                    \
        gtkinfobar.ui                   \
        gtklockbutton.ui                \
diff --git a/gtk/gtk.gresource.xml b/gtk/gtk.gresource.xml
index db5ce92..6e65b4b 100644
--- a/gtk/gtk.gresource.xml
+++ b/gtk/gtk.gresource.xml
@@ -15,6 +15,7 @@
     <file>gtkappchooserdialog.ui</file>
     <file>gtkappchooserwidget.ui</file>
     <file>gtkassistant.ui</file>
+    <file>gtkcolorchooserdialog.ui</file>
     <file>gtkdialog.ui</file>
     <file>gtkinfobar.ui</file>
     <file>gtklockbutton.ui</file>
diff --git a/gtk/gtkcolorchooserdialog.c b/gtk/gtkcolorchooserdialog.c
index fcde97f..61c3860 100644
--- a/gtk/gtkcolorchooserdialog.c
+++ b/gtk/gtkcolorchooserdialog.c
@@ -101,67 +101,11 @@ selected_cb (GtkButton *button,
 static void
 gtk_color_chooser_dialog_init (GtkColorChooserDialog *cc)
 {
-  GtkColorChooserDialogPrivate *priv;
-  GtkDialog *dialog = GTK_DIALOG (cc);
-  GtkWidget *action_area, *content_area;
-
   cc->priv = G_TYPE_INSTANCE_GET_PRIVATE (cc,
                                           GTK_TYPE_COLOR_CHOOSER_DIALOG,
                                           GtkColorChooserDialogPrivate);
-  priv = cc->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 (cc), FALSE);
-
-  /* Create the content area */
-  priv->chooser = gtk_color_chooser_widget_new ();
-  gtk_container_set_border_width (GTK_CONTAINER (priv->chooser), 5);
-  gtk_widget_show (priv->chooser);
-  gtk_box_pack_start (GTK_BOX (content_area),
-                      priv->chooser, TRUE, TRUE, 0);
 
-  g_signal_connect (priv->chooser, "notify::rgba",
-                    G_CALLBACK (propagate_notify), cc);
-
-  g_signal_connect (priv->chooser, "notify::show-editor",
-                    G_CALLBACK (propagate_notify), cc);
-
-  g_signal_connect (priv->chooser, "color-activated",
-                    G_CALLBACK (color_activated_cb), cc);
-
-  /* Create the action area */
-  priv->cancel_button = gtk_dialog_add_button (dialog,
-                                               GTK_STOCK_CANCEL,
-                                               GTK_RESPONSE_CANCEL);
-
-  /* We emit the response for the Select button manually,
-   * since we want to save the color first
-   */
-  priv->select_button = gtk_button_new_from_stock (_("_Select"));
-  g_signal_connect (priv->select_button, "clicked",
-                    G_CALLBACK (selected_cb), dialog);
-  gtk_widget_set_can_default (priv->select_button, TRUE);
-  gtk_widget_show (priv->select_button);
-  gtk_dialog_add_action_widget (dialog, priv->select_button, GTK_RESPONSE_OK);
-  gtk_widget_grab_default (priv->select_button);
-
-  gtk_dialog_set_alternative_button_order (dialog,
-                                           GTK_RESPONSE_OK,
-                                           GTK_RESPONSE_CANCEL,
-                                           -1);
-
-  gtk_window_set_title (GTK_WINDOW (cc), _("Select a Color"));
-
-  gtk_widget_pop_composite_child ();
+  gtk_widget_init_template (GTK_WIDGET (cc));
 }
 
 static void
@@ -256,6 +200,16 @@ gtk_color_chooser_dialog_class_init (GtkColorChooserDialogClass *class)
       g_param_spec_boolean ("show-editor", P_("Show editor"), P_("Show editor"),
                             FALSE, GTK_PARAM_READWRITE));
 
+  /* Bind class to template
+   */
+  gtk_widget_class_set_template_from_resource (widget_class,
+                                              "/org/gtk/libgtk/gtkcolorchooserdialog.ui");
+  gtk_widget_class_bind_child (widget_class, GtkColorChooserDialogPrivate, chooser);
+  gtk_widget_class_bind_child (widget_class, GtkColorChooserDialogPrivate, cancel_button);
+  gtk_widget_class_bind_child (widget_class, GtkColorChooserDialogPrivate, select_button);
+  gtk_widget_class_bind_callback (widget_class, selected_cb);
+  gtk_widget_class_bind_callback (widget_class, propagate_notify);
+  gtk_widget_class_bind_callback (widget_class, color_activated_cb);
 
   g_type_class_add_private (class, sizeof (GtkColorChooserDialogPrivate));
 }
diff --git a/gtk/gtkcolorchooserdialog.ui b/gtk/gtkcolorchooserdialog.ui
new file mode 100644
index 0000000..c722c34
--- /dev/null
+++ b/gtk/gtkcolorchooserdialog.ui
@@ -0,0 +1,82 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface domain="gtk30">
+  <!-- interface-requires gtk+ 3.10 -->
+  <template class="GtkColorChooserDialog" parent="GtkDialog">
+    <property name="can_focus">False</property>
+    <property name="border_width">5</property>
+    <property name="title" translatable="yes">Select a Color</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="can_default">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>
+                <signal name="clicked" handler="selected_cb" swapped="no"/>
+              </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="GtkColorChooserWidget" id="chooser">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="border_width">5</property>
+            <property name="orientation">vertical</property>
+            <property name="rgba">rgb(255,255,255)</property>
+            <signal name="color-activated" handler="color_activated_cb" swapped="no"/>
+            <signal name="notify::rgba" handler="propagate_notify" swapped="no"/>
+            <signal name="notify::show-editor" handler="propagate_notify" 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 ea7b306..a76e20a 100644
--- a/gtk/tests/templates.c
+++ b/gtk/tests/templates.c
@@ -139,6 +139,16 @@ test_app_chooser_dialog_basic (void)
   gtk_widget_destroy (widget);
 }
 
+static void
+test_color_chooser_dialog_basic (void)
+{
+  GtkWidget *widget;
+
+  widget = gtk_color_chooser_dialog_new (NULL, NULL);
+  g_assert (GTK_IS_COLOR_CHOOSER_DIALOG (widget));
+  gtk_widget_destroy (widget);
+}
+
 int
 main (int argc, char **argv)
 {
@@ -162,6 +172,7 @@ main (int argc, char **argv)
   g_test_add_func ("/Template/GtkStatusBar/Basic", test_statusbar_basic);
   g_test_add_func ("/Template/GtkAppChooserWidget/Basic", test_app_chooser_widget_basic);
   g_test_add_func ("/Template/GtkAppChooserDialog/Basic", test_app_chooser_dialog_basic);
+  g_test_add_func ("/Template/GtkColorChooserDialog/Basic", test_color_chooser_dialog_basic);
 
   return g_test_run();
 }
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 5755ba5..3455a40 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -278,6 +278,7 @@ modules/printbackends/test/gtkprintbackendtest.c
 [type: gettext/glade]gtk/gtkappchooserdialog.ui
 [type: gettext/glade]gtk/gtkappchooserwidget.ui
 [type: gettext/glade]gtk/gtkassistant.ui
+[type: gettext/glade]gtk/gtkcolorchooserdialog.ui
 [type: gettext/glade]gtk/gtkdialog.ui
 [type: gettext/glade]gtk/gtkinfobar.ui
 [type: gettext/glade]gtk/gtklockbutton.ui


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