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



commit 05738ca64d9b1997ac67ece6de7cbb4ef71c8abd
Author: Tristan Van Berkom <tristanvb openismus com>
Date:   Thu Mar 21 17:54:16 2013 +0900

    GtkDialog: Define children with a GtkBuilder template
    
    As the first composite widget in GTK+, this patch also
    adds some Makefile mechanics to list the ui files as
    dependencies of the global GTK+ resources, and adds the
    initial test case where composite classes should be tested.

 gtk/Makefile.am       |    5 +++-
 gtk/gtk.gresource.xml |    1 +
 gtk/gtkdialog.c       |   59 ++++++-----------------------------------------
 gtk/gtkdialog.ui      |   30 ++++++++++++++++++++++++
 gtk/tests/Makefile.am |    4 +++
 gtk/tests/templates.c |   61 +++++++++++++++++++++++++++++++++++++++++++++++++
 po/POTFILES.in        |    1 +
 7 files changed, 109 insertions(+), 52 deletions(-)
---
diff --git a/gtk/Makefile.am b/gtk/Makefile.am
index f178460..250f03c 100644
--- a/gtk/Makefile.am
+++ b/gtk/Makefile.am
@@ -1095,6 +1095,9 @@ DND_CURSORS = \
        cursor_dnd_move.png                             \
        cursor_dnd_none.png
 
+COMPOSITE_TEMPLATES = \
+       gtkdialog.ui
+
 #
 # rules to generate built sources
 #
@@ -1139,7 +1142,7 @@ gtktypebuiltins.c: @REBUILD@ $(gtk_public_h_sources) $(deprecated_h_sources) gtk
 gtkresources.h: gtk.gresource.xml
        $(AM_V_GEN) $(GLIB_COMPILE_RESOURCES) $(srcdir)/gtk.gresource.xml \
                --target=$@ --sourcedir=$(srcdir) --c-name _gtk --generate-header --manual-register
-gtkresources.c: gtk.gresource.xml gtk-default.css gtk-win32.css gtk-win32-xp.css gtk-win32-base.css 
gtk-win32-classic.css $(DND_CURSORS)
+gtkresources.c: gtk.gresource.xml gtk-default.css gtk-win32.css gtk-win32-xp.css gtk-win32-base.css 
gtk-win32-classic.css $(DND_CURSORS) $(COMPOSITE_TEMPLATES)
        $(AM_V_GEN) $(GLIB_COMPILE_RESOURCES) $(srcdir)/gtk.gresource.xml \
                --target=$@ --sourcedir=$(srcdir) --c-name _gtk --generate-source --manual-register
 
diff --git a/gtk/gtk.gresource.xml b/gtk/gtk.gresource.xml
index 939d35f..594d3ff 100644
--- a/gtk/gtk.gresource.xml
+++ b/gtk/gtk.gresource.xml
@@ -11,5 +11,6 @@
     <file alias="cursor/dnd-none.png">cursor_dnd_none.png</file>
     <file alias="cursor/dnd-move.png">cursor_dnd_move.png</file>
     <file alias="cursor/dnd-copy.png">cursor_dnd_copy.png</file>
+    <file compressed="true">gtkdialog.ui</file>
   </gresource>
 </gresources>
diff --git a/gtk/gtkdialog.c b/gtk/gtkdialog.c
index b88ca5f..06cff85 100644
--- a/gtk/gtkdialog.c
+++ b/gtk/gtkdialog.c
@@ -195,9 +195,6 @@ static ResponseData * get_response_data          (GtkWidget    *widget,
                                                   gboolean      create);
 
 static void      gtk_dialog_buildable_interface_init     (GtkBuildableIface *iface);
-static GObject * gtk_dialog_buildable_get_internal_child (GtkBuildable  *buildable,
-                                                          GtkBuilder    *builder,
-                                                          const gchar   *childname);
 static gboolean  gtk_dialog_buildable_custom_tag_start   (GtkBuildable  *buildable,
                                                           GtkBuilder    *builder,
                                                           GObject       *child,
@@ -331,8 +328,14 @@ gtk_dialog_class_init (GtkDialogClass *class)
                                                              GTK_PARAM_READABLE));
 
   binding_set = gtk_binding_set_by_class (class);
-
   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Escape, 0, "close", 0);
+
+  /* Bind class to template
+   */
+  gtk_widget_class_set_template_from_resource (widget_class, "/org/gtk/libgtk/gtkdialog.ui");
+  gtk_widget_class_bind_child_internal (widget_class, GtkDialogPrivate, vbox);
+  gtk_widget_class_bind_child_internal (widget_class, GtkDialogPrivate, action_area);
+  gtk_widget_class_bind_callback (widget_class, gtk_dialog_delete_event_handler);
 }
 
 static void
@@ -350,7 +353,6 @@ update_spacings (GtkDialog *dialog)
                         "button-spacing", &button_spacing,
                         "action-area-border", &action_area_border,
                         NULL);
-
   
   gtk_container_set_border_width (GTK_CONTAINER (priv->vbox),
                                   content_area_border);
@@ -368,38 +370,11 @@ update_spacings (GtkDialog *dialog)
 static void
 gtk_dialog_init (GtkDialog *dialog)
 {
-  GtkDialogPrivate *priv;
-
   dialog->priv = G_TYPE_INSTANCE_GET_PRIVATE (dialog,
                                               GTK_TYPE_DIALOG,
                                               GtkDialogPrivate);
-  priv = dialog->priv;
-
-  /* To avoid breaking old code that prevents destroy on delete event
-   * by connecting a handler, we have to have the FIRST signal
-   * connection on the dialog.
-   */
-  g_signal_connect (dialog,
-                    "delete-event",
-                    G_CALLBACK (gtk_dialog_delete_event_handler),
-                    NULL);
 
-  priv->vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
-  gtk_container_add (GTK_CONTAINER (dialog), priv->vbox);
-  gtk_widget_show (priv->vbox);
-
-  priv->action_area = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);
-
-  gtk_button_box_set_layout (GTK_BUTTON_BOX (priv->action_area),
-                             GTK_BUTTONBOX_END);
-
-  gtk_box_pack_end (GTK_BOX (priv->vbox), priv->action_area,
-                    FALSE, TRUE, 0);
-  gtk_widget_show (priv->action_area);
-
-  gtk_window_set_type_hint (GTK_WINDOW (dialog),
-                           GDK_WINDOW_TYPE_HINT_DIALOG);
-  gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER_ON_PARENT);
+  gtk_widget_init_template (GTK_WIDGET (dialog));
 
   update_spacings (dialog);
 }
@@ -410,28 +385,10 @@ static void
 gtk_dialog_buildable_interface_init (GtkBuildableIface *iface)
 {
   parent_buildable_iface = g_type_interface_peek_parent (iface);
-  iface->get_internal_child = gtk_dialog_buildable_get_internal_child;
   iface->custom_tag_start = gtk_dialog_buildable_custom_tag_start;
   iface->custom_finished = gtk_dialog_buildable_custom_finished;
 }
 
-static GObject *
-gtk_dialog_buildable_get_internal_child (GtkBuildable *buildable,
-                                        GtkBuilder   *builder,
-                                        const gchar  *childname)
-{
-  GtkDialogPrivate *priv = GTK_DIALOG (buildable)->priv;
-
-  if (strcmp (childname, "vbox") == 0)
-    return G_OBJECT (priv->vbox);
-  else if (strcmp (childname, "action_area") == 0)
-    return G_OBJECT (priv->action_area);
-
-  return parent_buildable_iface->get_internal_child (buildable,
-                                                     builder,
-                                                     childname);
-}
-
 static gboolean
 gtk_dialog_delete_event_handler (GtkWidget   *widget,
                                  GdkEventAny *event,
diff --git a/gtk/gtkdialog.ui b/gtk/gtkdialog.ui
new file mode 100644
index 0000000..9e81134
--- /dev/null
+++ b/gtk/gtkdialog.ui
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface domain="gtk30">
+  <!-- interface-requires gtk+ 3.10 -->
+  <template class="GtkDialog" parent="GtkWindow">
+    <property name="can_focus">False</property>
+    <property name="window_position">center-on-parent</property>
+    <property name="type_hint">dialog</property>
+    <signal name="delete-event" handler="gtk_dialog_delete_event_handler" swapped="no"/>
+    <child>
+      <object class="GtkBox" id="vbox">
+        <property name="visible">True</property>
+        <property name="can_focus">False</property>
+        <property name="orientation">vertical</property>
+        <child>
+          <object class="GtkButtonBox" id="action_area">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="layout_style">end</property>
+          </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>
+      </object>
+    </child>
+  </template>
+</interface>
diff --git a/gtk/tests/Makefile.am b/gtk/tests/Makefile.am
index 0f2dd78..1043839 100644
--- a/gtk/tests/Makefile.am
+++ b/gtk/tests/Makefile.am
@@ -78,6 +78,10 @@ builder_SOURCES                       = builder.c
 builder_LDADD                   = $(progs_ldadd)
 builder_LDFLAGS                         = -export-dynamic
 
+TEST_PROGS                     += templates
+templates_SOURCES               = templates.c
+templates_LDADD                         = $(progs_ldadd)
+
 if OS_UNIX
 #TEST_PROGS                    += defaultvalue
 #defaultvalue_SOURCES           = defaultvalue.c
diff --git a/gtk/tests/templates.c b/gtk/tests/templates.c
new file mode 100644
index 0000000..199c99c
--- /dev/null
+++ b/gtk/tests/templates.c
@@ -0,0 +1,61 @@
+/* templates.c
+ * Copyright (C) 2013 Openismus GmbH
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Tristan Van Berkom <tristanvb openismus com>
+ */
+#include <gtk/gtk.h>
+
+static void
+test_dialog_basic (void)
+{
+  GtkWidget *dialog;
+
+  dialog = gtk_dialog_new();
+  g_assert (GTK_IS_DIALOG (dialog));
+  gtk_widget_destroy (dialog);
+}
+
+static void
+test_dialog_override_property (void)
+{
+  GtkWidget *dialog;
+
+  dialog = g_object_new (GTK_TYPE_DIALOG,
+                        "type-hint", GDK_WINDOW_TYPE_HINT_UTILITY,
+                        NULL);
+  g_assert (GTK_IS_DIALOG (dialog));
+  g_assert (gtk_window_get_type_hint (GTK_WINDOW (dialog)) == GDK_WINDOW_TYPE_HINT_UTILITY);
+
+  gtk_widget_destroy (dialog);
+}
+
+int
+main (int argc, char **argv)
+{
+  /* initialize test program */
+  gtk_test_init (&argc, &argv);
+
+  /* This environment variable cooperates with gtk_widget_destroy()
+   * to assert that all automated compoenents are properly finalized
+   * when a given composite widget is destroyed.
+   */
+  g_assert (g_setenv ("GTK_WIDGET_ASSERT_COMPONENTS", "1", TRUE));
+
+  g_test_add_func ("/Template/GtkDialog/Basic", test_dialog_basic);
+  g_test_add_func ("/Template/GtkDialog/OverrideProperty", test_dialog_override_property);
+
+  return g_test_run();
+}
diff --git a/po/POTFILES.in b/po/POTFILES.in
index dfb77eb..52d21c7 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -274,3 +274,4 @@ modules/printbackends/file/gtkprintbackendfile.c
 modules/printbackends/lpr/gtkprintbackendlpr.c
 modules/printbackends/papi/gtkprintbackendpapi.c
 modules/printbackends/test/gtkprintbackendtest.c
+[type: gettext/glade]gtk/gtkdialog.ui


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