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



commit 6777b53e6b5739a8336d08e22d89ae65ffe733a6
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       |   61 ++++++++-----------------------------------------
 gtk/gtkdialog.ui      |   26 +++++++++++++++++++++
 gtk/tests/Makefile.am |    4 +++
 gtk/tests/templates.c |   55 ++++++++++++++++++++++++++++++++++++++++++++
 po/POTFILES.in        |    1 +
 7 files changed, 101 insertions(+), 52 deletions(-)
---
diff --git a/gtk/Makefile.am b/gtk/Makefile.am
index 03f3e31..da0806b 100644
--- a/gtk/Makefile.am
+++ b/gtk/Makefile.am
@@ -1093,6 +1093,9 @@ DND_CURSORS = \
        cursor_dnd_move.png                             \
        cursor_dnd_none.png
 
+COMPOSITE_TEMPLATES = \
+       gtkdialog.ui
+
 #
 # rules to generate built sources
 #
@@ -1137,7 +1140,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..2704c7e 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>gtkdialog.ui</file>
   </gresource>
 </gresources>
diff --git a/gtk/gtkdialog.c b/gtk/gtkdialog.c
index b88ca5f..752e570 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,
@@ -233,10 +230,12 @@ gtk_dialog_class_init (GtkDialogClass *class)
 {
   GObjectClass *gobject_class;
   GtkWidgetClass *widget_class;
+  GtkContainerClass *container_class;
   GtkBindingSet *binding_set;
 
   gobject_class = G_OBJECT_CLASS (class);
   widget_class = GTK_WIDGET_CLASS (class);
+  container_class = GTK_CONTAINER_CLASS (class);
 
   widget_class->map = gtk_dialog_map;
   widget_class->style_updated = gtk_dialog_style_updated;
@@ -331,8 +330,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_container_class_set_template_from_resource (container_class, "/org/gtk/libgtk/gtkdialog.ui");
+  gtk_container_class_bind_child_internal (container_class, GtkDialogPrivate, vbox);
+  gtk_container_class_bind_child_internal (container_class, GtkDialogPrivate, action_area);
+  gtk_container_class_bind_callback (container_class, gtk_dialog_delete_event_handler);
 }
 
 static void
@@ -350,7 +355,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 +372,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_container_init_template (GTK_CONTAINER (dialog));
 
   update_spacings (dialog);
 }
@@ -410,28 +387,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..5231ea0
--- /dev/null
+++ b/gtk/gtkdialog.ui
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface domain="gtk30">
+  <!-- interface-requires gtk+ 3.10 -->
+  <template class="GtkDialog" parent="GtkWindow">
+    <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="orientation">vertical</property>
+        <child>
+          <object class="GtkButtonBox" id="action_area">
+            <property name="visible">True</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>
+          </packing>
+        </child>
+      </object>
+    </child>
+  </template>
+</interface>
diff --git a/gtk/tests/Makefile.am b/gtk/tests/Makefile.am
index 4fbbdab..d27e23e 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..b23a503
--- /dev/null
+++ b/gtk/tests/templates.c
@@ -0,0 +1,55 @@
+/* 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);
+
+  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]