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



commit 26d8f2783a738084d90c1ae38383a3db40e4474d
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 |   76 ++++++++++++++++++---------------------------
 gtk/tests/templates.c     |   11 ++++++
 po/POTFILES.in            |    1 +
 5 files changed, 44 insertions(+), 46 deletions(-)
---
diff --git a/gtk/Makefile.am b/gtk/Makefile.am
index 9cf77ac..abd15da 100644
--- a/gtk/Makefile.am
+++ b/gtk/Makefile.am
@@ -1095,6 +1095,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 e7bd656..db5ce92 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>gtkaboutdialog.ui</file>
+    <file>gtkappchooserdialog.ui</file>
     <file>gtkappchooserwidget.ui</file>
     <file>gtkassistant.ui</file>
     <file>gtkdialog.ui</file>
diff --git a/gtk/gtkappchooserdialog.c b/gtk/gtkappchooserdialog.c
index 00882d5..9e3ff08 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);
 }
@@ -651,6 +613,7 @@ static void
 gtk_app_chooser_dialog_class_init (GtkAppChooserDialogClass *klass)
 {
   GObjectClass *gobject_class;
+  GtkContainerClass *container_class;
   GParamSpec *pspec;
 
   gobject_class = G_OBJECT_CLASS (klass);
@@ -690,6 +653,22 @@ 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);
 
+  /* Define templates */
+  container_class = GTK_CONTAINER_CLASS (klass);
+  gtk_container_class_set_template (container_class,
+                                   GTK_TEMPLATE_SOURCE_RESOURCE,
+                                   "/org/gtk/libgtk/gtkappchooserdialog.ui");
+  gtk_container_class_automate_child (container_class, "label", FALSE, GTK_AUTOMATE_CHILD_PRIVATE,
+                                     G_STRUCT_OFFSET (GtkAppChooserDialogPrivate, label));
+  gtk_container_class_automate_child (container_class, "select", FALSE, GTK_AUTOMATE_CHILD_PRIVATE,
+                                     G_STRUCT_OFFSET (GtkAppChooserDialogPrivate, button));
+  gtk_container_class_automate_child (container_class, "show_more_button", FALSE, GTK_AUTOMATE_CHILD_PRIVATE,
+                                     G_STRUCT_OFFSET (GtkAppChooserDialogPrivate, show_more_button));
+  gtk_container_class_automate_child (container_class, "inner_box", FALSE, GTK_AUTOMATE_CHILD_PRIVATE,
+                                     G_STRUCT_OFFSET (GtkAppChooserDialogPrivate, inner_box));
+  gtk_container_class_declare_callback (container_class,
+                                       "show_more_button_clicked_cb",
+                                       G_CALLBACK (show_more_button_clicked_cb));
 
   g_type_class_add_private (klass, sizeof (GtkAppChooserDialogPrivate));
 }
@@ -700,11 +679,16 @@ gtk_app_chooser_dialog_init (GtkAppChooserDialog *self)
   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GTK_TYPE_APP_CHOOSER_DIALOG,
                                             GtkAppChooserDialogPrivate);
 
+  gtk_container_init_template (GTK_CONTAINER (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/tests/templates.c b/gtk/tests/templates.c
index 5bb96a4..4276011 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)
 {
@@ -145,6 +155,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]