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



commit e73f22dba6b3fb692a2635215065d40bd575925c
Author: Tristan Van Berkom <tristanvb openismus com>
Date:   Sat Mar 23 23:02:33 2013 +0900

    GtkAppChooserDialog: Define children with a GtkBuilder template

 gtk/Makefile.am            |    1 +
 gtk/gtk.gresource.xml      |    1 +
 gtk/gtkappchooserdialog.c  |   70 +++++++++----------------
 gtk/gtkappchooserdialog.ui |  123 ++++++++++++++++++++++++++++++++++++++++++++
 gtk/tests/templates.c      |   11 ++++
 po/POTFILES.in             |    1 +
 6 files changed, 161 insertions(+), 46 deletions(-)
---
diff --git a/gtk/Makefile.am b/gtk/Makefile.am
index b892518..99acbeb 100644
--- a/gtk/Makefile.am
+++ b/gtk/Makefile.am
@@ -1097,6 +1097,7 @@ DND_CURSORS = \
 
 COMPOSITE_TEMPLATES =                  \
        gtkaboutdialog.ui               \
+       gtkappchooserdialog.ui          \
        gtkappchooserwidget.ui          \
        gtkassistant.ui                 \
        gtkdialog.ui                    \
diff --git a/gtk/gtk.gresource.xml b/gtk/gtk.gresource.xml
index d9c7a8d..9361124 100644
--- a/gtk/gtk.gresource.xml
+++ b/gtk/gtk.gresource.xml
@@ -12,6 +12,7 @@
     <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">gtkaboutdialog.ui</file>
+    <file compressed="true">gtkappchooserdialog.ui</file>
     <file compressed="true">gtkappchooserwidget.ui</file>
     <file compressed="true">gtkassistant.ui</file>
     <file compressed="true">gtkdialog.ui</file>
diff --git a/gtk/gtkappchooserdialog.c b/gtk/gtkappchooserdialog.c
index 00882d5..e55eac3 100644
--- a/gtk/gtkappchooserdialog.c
+++ b/gtk/gtkappchooserdialog.c
@@ -68,6 +68,7 @@ struct _GtkAppChooserDialogPrivate {
   GtkWidget *label;
   GtkWidget *button;
   GtkWidget *online_button;
+  GtkWidget *inner_box;
 
   GtkWidget *open_label;
 
@@ -450,35 +451,14 @@ widget_populate_popup_cb (GtkAppChooserWidget *widget,
 }
 
 static void
-build_dialog_ui (GtkAppChooserDialog *self)
+construct_appchooser_widget (GtkAppChooserDialog *self)
 {
-  GtkWidget *vbox;
-  GtkWidget *vbox2;
-  GtkWidget *button, *w;
   GAppInfo *info;
 
-  gtk_container_set_border_width (GTK_CONTAINER (self), 5);
-
-  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
-  gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
-  gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (self))), vbox, TRUE, TRUE, 0);
-  gtk_widget_show (vbox);
-
-  vbox2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
-  gtk_box_pack_start (GTK_BOX (vbox), vbox2, TRUE, TRUE, 0);
-  gtk_widget_show (vbox2);
-
-  self->priv->label = gtk_label_new ("");
-  gtk_widget_set_halign (self->priv->label, GTK_ALIGN_START);
-  gtk_widget_set_valign (self->priv->label, GTK_ALIGN_CENTER);
-  gtk_label_set_line_wrap (GTK_LABEL (self->priv->label), TRUE);
-  gtk_box_pack_start (GTK_BOX (vbox2), self->priv->label,
-                      FALSE, FALSE, 0);
-  gtk_widget_show (self->priv->label);
-
+  /* Need to build the appchooser widget after, because of the content-type construct-only property */
   self->priv->app_chooser_widget =
     gtk_app_chooser_widget_new (self->priv->content_type);
-  gtk_box_pack_start (GTK_BOX (vbox2), self->priv->app_chooser_widget, TRUE, TRUE, 0);
+  gtk_box_pack_start (GTK_BOX (self->priv->inner_box), self->priv->app_chooser_widget, TRUE, TRUE, 0);
   gtk_widget_show (self->priv->app_chooser_widget);
 
   g_signal_connect (self->priv->app_chooser_widget, "application-selected",
@@ -490,32 +470,14 @@ build_dialog_ui (GtkAppChooserDialog *self)
   g_signal_connect (self->priv->app_chooser_widget, "populate-popup",
                     G_CALLBACK (widget_populate_popup_cb), self);
 
-  button = gtk_button_new_with_label (_("Show other applications"));
-  self->priv->show_more_button = button;
-  w = gtk_image_new_from_stock (GTK_STOCK_ADD,
-                                GTK_ICON_SIZE_BUTTON);
-  gtk_button_set_image (GTK_BUTTON (button), w);
-  gtk_box_pack_start (GTK_BOX (self->priv->app_chooser_widget), button, FALSE, FALSE, 6);
-  gtk_widget_show_all (button);
-
-  g_signal_connect (button, "clicked",
-                    G_CALLBACK (show_more_button_clicked_cb), self);
-
-  gtk_dialog_add_button (GTK_DIALOG (self),
-                         GTK_STOCK_CANCEL,
-                         GTK_RESPONSE_CANCEL);
-
-  self->priv->button = gtk_dialog_add_button (GTK_DIALOG (self),
-                                              _("_Select"),
-                                              GTK_RESPONSE_OK);
+  /* Add the custom button to the new appchooser */
+  gtk_box_pack_start (GTK_BOX (self->priv->app_chooser_widget),
+                     self->priv->show_more_button, FALSE, FALSE, 6);
 
   info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (self->priv->app_chooser_widget));
   gtk_widget_set_sensitive (self->priv->button, info != NULL);
   if (info)
     g_object_unref (info);
-
-  gtk_dialog_set_default_response (GTK_DIALOG (self),
-                                   GTK_RESPONSE_OK);
 }
 
 static void
@@ -560,7 +522,7 @@ gtk_app_chooser_dialog_constructed (GObject *object)
   if (G_OBJECT_CLASS (gtk_app_chooser_dialog_parent_class)->constructed != NULL)
     G_OBJECT_CLASS (gtk_app_chooser_dialog_parent_class)->constructed (object);
 
-  build_dialog_ui (self);
+  construct_appchooser_widget (self);
   set_dialog_properties (self);
   ensure_online_button (self);
 }
@@ -650,6 +612,7 @@ gtk_app_chooser_dialog_iface_init (GtkAppChooserIface *iface)
 static void
 gtk_app_chooser_dialog_class_init (GtkAppChooserDialogClass *klass)
 {
+  GtkWidgetClass *widget_class;
   GObjectClass *gobject_class;
   GParamSpec *pspec;
 
@@ -690,6 +653,16 @@ gtk_app_chooser_dialog_class_init (GtkAppChooserDialogClass *klass)
                                G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
   g_object_class_install_property (gobject_class, PROP_HEADING, pspec);
 
+  /* Bind class to template
+   */
+  widget_class = GTK_WIDGET_CLASS (klass);
+  gtk_widget_class_set_template_from_resource (widget_class,
+                                              "/org/gtk/libgtk/gtkappchooserdialog.ui");
+  gtk_widget_class_bind_child (widget_class, GtkAppChooserDialogPrivate, label);
+  gtk_widget_class_bind_child (widget_class, GtkAppChooserDialogPrivate, button);
+  gtk_widget_class_bind_child (widget_class, GtkAppChooserDialogPrivate, show_more_button);
+  gtk_widget_class_bind_child (widget_class, GtkAppChooserDialogPrivate, inner_box);
+  gtk_widget_class_bind_callback (widget_class, show_more_button_clicked_cb);
 
   g_type_class_add_private (klass, sizeof (GtkAppChooserDialogPrivate));
 }
@@ -700,11 +673,16 @@ gtk_app_chooser_dialog_init (GtkAppChooserDialog *self)
   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GTK_TYPE_APP_CHOOSER_DIALOG,
                                             GtkAppChooserDialogPrivate);
 
+  gtk_widget_init_template (GTK_WIDGET (self));
+
   /* we can't override the class signal handler here, as it's a RUN_LAST;
    * we want our signal handler instead to be executed before any user code.
    */
   g_signal_connect (self, "response",
                     G_CALLBACK (gtk_app_chooser_dialog_response), NULL);
+
+  gtk_dialog_set_default_response (GTK_DIALOG (self),
+                                   GTK_RESPONSE_OK);
 }
 
 static void
diff --git a/gtk/gtkappchooserdialog.ui b/gtk/gtkappchooserdialog.ui
new file mode 100644
index 0000000..720cde1
--- /dev/null
+++ b/gtk/gtkappchooserdialog.ui
@@ -0,0 +1,123 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface domain="gtk30">
+  <!-- interface-requires gtk+ 3.10 -->
+  <template class="GtkAppChooserDialog" parent="GtkDialog">
+    <property name="can_focus">False</property>
+    <property name="border_width">5</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">
+                <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="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="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="GtkBox" id="main_box">
+            <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">12</property>
+            <child>
+              <object class="GtkBox" id="inner_box">
+                <property name="visible">True</property>
+                <property name="can_focus">False</property>
+                <property name="orientation">vertical</property>
+                <property name="spacing">6</property>
+                <child>
+                  <object class="GtkLabel" id="label">
+                    <property name="visible">True</property>
+                    <property name="can_focus">False</property>
+                    <property name="halign">start</property>
+                    <property name="valign">center</property>
+                    <property name="label" translatable="yes">label</property>
+                    <property name="wrap">True</property>
+                  </object>
+                  <packing>
+                    <property name="expand">False</property>
+                    <property name="fill">False</property>
+                    <property name="position">0</property>
+                  </packing>
+                </child>
+                <child>
+                  <placeholder/>
+                </child>
+              </object>
+              <packing>
+                <property name="expand">True</property>
+                <property name="fill">True</property>
+                <property name="position">0</property>
+              </packing>
+            </child>
+            <child>
+              <placeholder/>
+            </child>
+          </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</action-widget>
+      <action-widget response="-5">button</action-widget>
+    </action-widgets>
+  </template>
+  <object class="GtkImage" id="show_more_image">
+    <property name="visible">True</property>
+    <property name="can_focus">False</property>
+    <property name="stock">gtk-add</property>
+  </object>
+  <object class="GtkButton" id="show_more_button">
+    <property name="label" translatable="yes">Show Other Applications</property>
+    <property name="visible">True</property>
+    <property name="can_focus">True</property>
+    <property name="receives_default">True</property>
+    <property name="image">show_more_image</property>
+    <signal name="clicked" handler="show_more_button_clicked_cb" swapped="no"/>
+  </object>
+</interface>
diff --git a/gtk/tests/templates.c b/gtk/tests/templates.c
index 290037f..ea7b306 100644
--- a/gtk/tests/templates.c
+++ b/gtk/tests/templates.c
@@ -129,6 +129,16 @@ test_app_chooser_widget_basic (void)
   gtk_widget_destroy (widget);
 }
 
+static void
+test_app_chooser_dialog_basic (void)
+{
+  GtkWidget *widget;
+
+  widget = gtk_app_chooser_dialog_new_for_content_type (NULL, 0, "text/plain");
+  g_assert (GTK_IS_APP_CHOOSER_DIALOG (widget));
+  gtk_widget_destroy (widget);
+}
+
 int
 main (int argc, char **argv)
 {
@@ -151,6 +161,7 @@ main (int argc, char **argv)
   g_test_add_func ("/Template/GtkScaleButton/Basic", test_scale_button_basic);
   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);
 
   return g_test_run();
 }
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 8f7d476..5755ba5 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -275,6 +275,7 @@ modules/printbackends/lpr/gtkprintbackendlpr.c
 modules/printbackends/papi/gtkprintbackendpapi.c
 modules/printbackends/test/gtkprintbackendtest.c
 [type: gettext/glade]gtk/gtkaboutdialog.ui
+[type: gettext/glade]gtk/gtkappchooserdialog.ui
 [type: gettext/glade]gtk/gtkappchooserwidget.ui
 [type: gettext/glade]gtk/gtkassistant.ui
 [type: gettext/glade]gtk/gtkdialog.ui


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