[devhelp/wip/wintemplate: 1/8] window: use a template to define the UI



commit 0f3e685858a263971e3919c53663a1d102d3869d
Author: Ignacio Casal Quinteiro <ignacio casal nice-software com>
Date:   Tue Jul 23 17:20:07 2013 +0200

    window: use a template to define the UI

 src/devhelp.ui       |   40 ------------
 src/dh-window.c      |  123 +++++++++--------------------------
 src/dh-window.ui     |  172 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/dh.gresource.xml |    3 +-
 4 files changed, 206 insertions(+), 132 deletions(-)
---
diff --git a/src/devhelp.ui b/src/devhelp.ui
index c8ad686..5a036df 100644
--- a/src/devhelp.ui
+++ b/src/devhelp.ui
@@ -50,46 +50,6 @@
     </section>
   </menu>
 
-  <menu id="window-menu">
-    <section>
-      <item>
-       <attribute name="label" translatable="yes">New _Tab</attribute>
-       <attribute name="action">win.new-tab</attribute>
-       <attribute name="accel">&lt;Primary&gt;t</attribute>
-       <attribute name="use-underline">True</attribute>
-      </item>
-    </section>
-    <section>
-      <item>
-       <attribute name="label" translatable="yes">_Print</attribute>
-       <attribute name="action">win.print</attribute>
-       <attribute name="accel">&lt;Primary&gt;p</attribute>
-      </item>
-      <item>
-       <attribute name="label" translatable="yes">_Find</attribute>
-       <attribute name="action">win.find</attribute>
-       <attribute name="accel">&lt;Primary&gt;f</attribute>
-      </item>
-    </section>
-    <section>
-      <item>
-       <attribute name="label" translatable="yes">_Larger Text</attribute>
-       <attribute name="action">win.zoom-in</attribute>
-       <attribute name="accel">&lt;Primary&gt;plus</attribute>
-      </item>
-      <item>
-       <attribute name="label" translatable="yes">S_maller Text</attribute>
-       <attribute name="action">win.zoom-out</attribute>
-       <attribute name="accel">&lt;Primary&gt;minus</attribute>
-      </item>
-      <item>
-       <attribute name="label" translatable="yes">_Normal Size</attribute>
-       <attribute name="action">win.zoom-default</attribute>
-       <attribute name="accel">&lt;Primary&gt;0</attribute>
-      </item>
-    </section>
-  </menu>
-
   <object class="GtkListStore" id="bookshelf_store">
     <columns>
       <!-- column-name enabled -->
diff --git a/src/dh-window.c b/src/dh-window.c
index 3b7bc76..2969102 100644
--- a/src/dh-window.c
+++ b/src/dh-window.c
@@ -40,17 +40,18 @@
 #define TAB_WIDTH_N_CHARS 15
 
 struct _DhWindowPriv {
-        GtkWidget      *main_box;
         GtkWidget      *hpaned;
         GtkWidget      *sidebar;
         GtkWidget      *notebook;
         GtkWidget      *header_bar;
-
-        GtkWidget      *vbox;
+        GtkWidget      *back_button;
+        GtkImage       *back_button_image;
+        GtkWidget      *forward_button;
+        GtkImage       *forward_button_image;
+        GtkWidget      *grid_sidebar;
+        GtkWidget      *grid_documents;
         GtkWidget      *findbar;
 
-        GtkBuilder     *builder;
-
         DhLink         *selected_search_link;
         guint           find_source_id;
         DhSettings     *settings;
@@ -503,12 +504,11 @@ dh_window_init (DhWindow *window)
         GtkAccelGroup *accel_group;
         GClosure      *closure;
         gint           i;
-        GError        *error = NULL;
 
         priv = GET_PRIVATE (window);
         window->priv = priv;
 
-        gtk_window_set_hide_titlebar_when_maximized (GTK_WINDOW (window), TRUE);
+        gtk_widget_init_template (GTK_WIDGET (window));
 
         priv->selected_search_link = NULL;
 
@@ -519,18 +519,6 @@ dh_window_init (DhWindow *window)
                                                    G_CALLBACK (settings_fonts_changed_cb),
                                                    window);
 
-        /* Setup builder */
-        priv->builder = gtk_builder_new ();
-        if (!gtk_builder_add_from_resource (priv->builder, "/org/gnome/devhelp/devhelp.ui", &error)) {
-                g_error ("Cannot add resource to builder: %s", error ? error->message : "unknown error");
-                g_clear_error (&error);
-        }
-
-        priv->main_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
-        gtk_widget_show (priv->main_box);
-
-        gtk_container_add (GTK_CONTAINER (window), priv->main_box);
-
         g_signal_connect (window,
                           "open-link",
                           G_CALLBACK (window_open_link_cb),
@@ -566,7 +554,6 @@ dispose (GObject *object)
         }
 
         g_clear_object (&self->priv->settings);
-        g_clear_object (&self->priv->builder);
 
        /* Chain up to the parent class */
        G_OBJECT_CLASS (dh_window_parent_class)->dispose (object);
@@ -576,7 +563,8 @@ static void
 dh_window_class_init (DhWindowClass *klass)
 {
         GObjectClass *object_class = G_OBJECT_CLASS (klass);
-        g_type_class_add_private (klass, sizeof (DhWindowPriv));
+        GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+
         object_class->dispose = dispose;
 
         signals[OPEN_LINK] =
@@ -600,6 +588,21 @@ dh_window_class_init (DhWindowClass *klass)
                              "}\n"
                              "widget \"*.devhelp-tab-close-button\" "
                              "style \"devhelp-tab-close-button-style\"");
+
+        /* Bind class to template */
+        gtk_widget_class_set_template_from_resource (widget_class,
+                                                     "/org/gnome/devhelp/dh-window.ui");
+        gtk_widget_class_bind_child (widget_class, DhWindowPriv, header_bar);
+        gtk_widget_class_bind_child (widget_class, DhWindowPriv, back_button);
+        gtk_widget_class_bind_child (widget_class, DhWindowPriv, back_button_image);
+        gtk_widget_class_bind_child (widget_class, DhWindowPriv, forward_button);
+        gtk_widget_class_bind_child (widget_class, DhWindowPriv, forward_button_image);
+        gtk_widget_class_bind_child (widget_class, DhWindowPriv, hpaned);
+        gtk_widget_class_bind_child (widget_class, DhWindowPriv, grid_sidebar);
+        gtk_widget_class_bind_child (widget_class, DhWindowPriv, grid_documents);
+        gtk_widget_class_bind_child (widget_class, DhWindowPriv, notebook);
+
+        g_type_class_add_private (klass, sizeof (DhWindowPriv));
 }
 
 static void
@@ -651,21 +654,11 @@ window_populate (DhWindow *window)
 {
         DhWindowPriv  *priv;
         DhBookManager *book_manager;
-        GtkWidget     *back_button;
-        GtkWidget     *back_image;
-        GtkWidget     *forward_button;
-        GtkWidget     *forward_image;
-        GtkWidget     *box;
-        GtkWidget     *menu_button;
-        GtkWidget     *menu_image;
-        GObject       *menu;
         const char *prev_icon, *next_icon;
 
         priv = window->priv;
         book_manager = dh_app_peek_book_manager (DH_APP (gtk_window_get_application (GTK_WINDOW (window))));
 
-        priv->header_bar = gtk_header_bar_new ();
-
         if (gtk_widget_get_direction (GTK_WIDGET (window)) == GTK_TEXT_DIR_RTL) {
                 prev_icon = "go-previous-rtl-symbolic";
                 next_icon = "go-next-rtl-symbolic";
@@ -674,69 +667,21 @@ window_populate (DhWindow *window)
                 next_icon = "go-next-symbolic";
         }
 
-        back_button = gtk_button_new ();
-        back_image = gtk_image_new_from_icon_name (prev_icon, GTK_ICON_SIZE_MENU);
-        gtk_button_set_image (GTK_BUTTON (back_button), back_image);
-        gtk_widget_set_tooltip_text (back_button, _("Back"));
-        gtk_actionable_set_action_name (GTK_ACTIONABLE (back_button), "win.go-back");
-        gtk_widget_set_valign (back_button, GTK_ALIGN_CENTER);
-        gtk_style_context_add_class (gtk_widget_get_style_context (back_button),
-                                    "image-button");
-
-        forward_button = gtk_button_new ();
-        forward_image = gtk_image_new_from_icon_name (next_icon, GTK_ICON_SIZE_MENU);
-        gtk_button_set_image (GTK_BUTTON (forward_button), forward_image);
-        gtk_widget_set_tooltip_text (forward_button, _("Forward"));
-        gtk_actionable_set_action_name (GTK_ACTIONABLE (forward_button), "win.go-forward");
-        gtk_widget_set_valign (forward_button, GTK_ALIGN_CENTER);
-        gtk_style_context_add_class (gtk_widget_get_style_context (forward_button),
-                                    "image-button");
-
-        box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
-        gtk_style_context_add_class (gtk_widget_get_style_context (box), "linked");
-        gtk_box_pack_start (GTK_BOX (box), back_button, FALSE, FALSE, 0);
-        gtk_box_pack_start (GTK_BOX (box), forward_button, FALSE, FALSE, 0);
-        gtk_header_bar_pack_start (GTK_HEADER_BAR (priv->header_bar), box);
-
-        menu_button = gtk_menu_button_new ();
-        menu_image = gtk_image_new_from_icon_name ("emblem-system-symbolic", GTK_ICON_SIZE_MENU);
-        gtk_button_set_image (GTK_BUTTON (menu_button), menu_image);
-        gtk_actionable_set_action_name (GTK_ACTIONABLE (menu_button), "win.gear-menu");
-        gtk_widget_set_valign (menu_button, GTK_ALIGN_CENTER);
-        gtk_style_context_add_class (gtk_widget_get_style_context (menu_button),
-                                    "image-button");
-
-        gtk_header_bar_pack_end (GTK_HEADER_BAR (priv->header_bar), menu_button);
-
-        menu = gtk_builder_get_object (priv->builder, "window-menu");
-        gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (menu_button), G_MENU_MODEL (menu));
-
-        /* Add toolbar to main box */
-        gtk_box_pack_start (GTK_BOX (priv->main_box), priv->header_bar,
-                            FALSE, FALSE, 0);
-
-        priv->hpaned = gtk_paned_new (GTK_ORIENTATION_HORIZONTAL);
-        gtk_box_pack_start (GTK_BOX (priv->main_box), priv->hpaned, TRUE, TRUE, 0);
+        gtk_image_set_from_icon_name (priv->back_button_image, prev_icon, GTK_ICON_SIZE_MENU);
+        gtk_image_set_from_icon_name (priv->forward_button_image, next_icon, GTK_ICON_SIZE_MENU);
 
         /* Sidebar */
         priv->sidebar = dh_sidebar_new (book_manager);
-        gtk_paned_add1 (GTK_PANED (priv->hpaned), priv->sidebar);
+        gtk_widget_set_vexpand (priv->sidebar, TRUE);
+        gtk_widget_set_hexpand (priv->sidebar, TRUE);
+        gtk_widget_show (priv->sidebar);
+        gtk_container_add (GTK_CONTAINER (priv->grid_sidebar), priv->sidebar);
         g_signal_connect (priv->sidebar,
                           "link-selected",
                           G_CALLBACK (window_search_link_selected_cb),
                           window);
 
-        /* Document view */
-        priv->vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
-        gtk_paned_add2 (GTK_PANED (priv->hpaned), priv->vbox);
-
         /* HTML tabs notebook. */
-        priv->notebook = gtk_notebook_new ();
-        gtk_container_set_border_width (GTK_CONTAINER (priv->notebook), 0);
-        gtk_notebook_set_show_border (GTK_NOTEBOOK (priv->notebook), FALSE);
-        gtk_notebook_set_scrollable (GTK_NOTEBOOK (priv->notebook), TRUE);
-        gtk_box_pack_start (GTK_BOX (priv->vbox), priv->notebook, TRUE, TRUE, 0);
-
         g_signal_connect (priv->notebook,
                           "switch-page",
                           G_CALLBACK (window_web_view_switch_page_cb),
@@ -749,7 +694,7 @@ window_populate (DhWindow *window)
         /* Create findbar */
         priv->findbar = egg_find_bar_new ();
         gtk_widget_set_no_show_all (priv->findbar, TRUE);
-        gtk_box_pack_start (GTK_BOX (priv->vbox), priv->findbar, FALSE, FALSE, 0);
+        gtk_container_add (GTK_CONTAINER (priv->grid_documents), priv->findbar);
 
         g_signal_connect (priv->findbar,
                           "notify::search-string",
@@ -772,8 +717,6 @@ window_populate (DhWindow *window)
                           G_CALLBACK (window_findbar_close_cb),
                           window);
 
-        gtk_widget_show_all (priv->hpaned);
-
         /* Focus search in sidebar by default */
         dh_sidebar_set_search_focus (DH_SIDEBAR (priv->sidebar));
 
@@ -1393,11 +1336,9 @@ dh_window_new (DhApp *application)
         DhWindow     *window;
         DhWindowPriv *priv;
 
-        window = g_object_new (DH_TYPE_WINDOW, NULL);
+        window = g_object_new (DH_TYPE_WINDOW, "application", application, NULL);
         priv = window->priv;
 
-        gtk_window_set_application (GTK_WINDOW (window), GTK_APPLICATION (application));
-
         window_populate (window);
 
         gtk_window_set_icon_name (GTK_WINDOW (window), "devhelp");
diff --git a/src/dh-window.ui b/src/dh-window.ui
new file mode 100644
index 0000000..8890076
--- /dev/null
+++ b/src/dh-window.ui
@@ -0,0 +1,172 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <!-- interface-requires gtk+ 3.8 -->
+  <menu id="gear-menu">
+    <section>
+      <item>
+        <attribute name="label" translatable="yes">New _Tab</attribute>
+        <attribute name="action">win.new-tab</attribute>
+        <attribute name="accel">&lt;Primary&gt;t</attribute>
+        <attribute name="use-underline">True</attribute>
+      </item>
+    </section>
+    <section>
+      <item>
+        <attribute name="label" translatable="yes">_Print</attribute>
+        <attribute name="action">win.print</attribute>
+        <attribute name="accel">&lt;Primary&gt;p</attribute>
+      </item>
+      <item>
+        <attribute name="label" translatable="yes">_Find</attribute>
+        <attribute name="action">win.find</attribute>
+        <attribute name="accel">&lt;Primary&gt;f</attribute>
+      </item>
+    </section>
+    <section>
+      <item>
+        <attribute name="label" translatable="yes">_Larger Text</attribute>
+        <attribute name="action">win.zoom-in</attribute>
+        <attribute name="accel">&lt;Primary&gt;plus</attribute>
+      </item>
+      <item>
+        <attribute name="label" translatable="yes">S_maller Text</attribute>
+        <attribute name="action">win.zoom-out</attribute>
+        <attribute name="accel">&lt;Primary&gt;minus</attribute>
+      </item>
+      <item>
+        <attribute name="label" translatable="yes">_Normal Size</attribute>
+        <attribute name="action">win.zoom-default</attribute>
+        <attribute name="accel">&lt;Primary&gt;0</attribute>
+      </item>
+    </section>
+  </menu>
+  <template class="DhWindow" parent="GtkApplicationWindow">
+    <property name="can_focus">False</property>
+    <property name="has_focus">False</property>
+    <property name="is_focus">False</property>
+    <child type="titlebar">
+      <object class="GtkHeaderBar" id="header_bar">
+        <property name="visible">True</property>
+        <property name="can_focus">False</property>
+        <property name="vexpand">False</property>
+        <child>
+          <object class="GtkBox" id="box">
+            <property name="visible">True</property>
+            <property name="valign">center</property>
+            <property name="can_focus">False</property>
+            <style>
+              <class name="linked"/>
+            </style>
+            <child>
+              <object class="GtkButton" id="back_button">
+                <property name="visible">True</property>
+                <property name="valign">center</property>
+                <property name="can_focus">False</property>
+                <property name="tooltip_text" translatable="yes">Back</property>
+                <property name="action_name">win.go-back</property>
+                <style>
+                  <class name="image-button"/>
+                </style>
+                <child>
+                  <object class="GtkImage" id="back_button_image">
+                    <property name="visible">True</property>
+                    <property name="icon_size">1</property>
+                  </object>
+                </child>
+              </object>
+            </child>
+            <child>
+              <object class="GtkButton" id="forward_button">
+                <property name="visible">True</property>
+                <property name="valign">center</property>
+                <property name="can_focus">False</property>
+                <property name="tooltip_text" translatable="yes">Forward</property>
+                <property name="action_name">win.go-forward</property>
+                <style>
+                  <class name="image-button"/>
+                </style>
+                <child>
+                  <object class="GtkImage" id="forward_button_image">
+                    <property name="visible">True</property>
+                    <property name="icon_size">1</property>
+                  </object>
+                </child>
+              </object>
+            </child>
+          </object>
+          <packing>
+            <property name="pack_type">start</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkMenuButton" id="gear_menu">
+            <property name="visible">True</property>
+            <property name="valign">center</property>
+            <property name="can_focus">False</property>
+            <property name="action_name">win.gear-menu</property>
+            <property name="menu_model">gear-menu</property>
+            <style>
+              <class name="image-button"/>
+            </style>
+            <child>
+              <object class="GtkImage" id="gear_image">
+                <property name="visible">True</property>
+                <property name="icon_size">1</property>
+                <property name="icon_name">emblem-system-symbolic</property>
+              </object>
+            </child>
+          </object>
+          <packing>
+            <property name="pack_type">end</property>
+          </packing>
+        </child>
+      </object>
+    </child>
+    <child>
+      <object class="GtkGrid" id="grid">
+        <property name="visible">True</property>
+        <property name="can_focus">False</property>
+        <property name="has_focus">False</property>
+        <property name="is_focus">False</property>
+        <child>
+          <object class="GtkPaned" id="hpaned">
+            <property name="visible">True</property>
+            <property name="can_focus">True</property>
+            <property name="has_focus">False</property>
+            <property name="is_focus">False</property>
+            <child>
+              <object class="GtkGrid" id="grid_sidebar">
+                <property name="visible">True</property>
+              </object>
+            </child>
+            <child>
+              <object class="GtkGrid" id="grid_documents">
+                <property name="visible">True</property>
+                <property name="orientation">vertical</property>
+                <child>
+                  <object class="GtkNotebook" id="notebook">
+                    <property name="visible">True</property>
+                    <property name="border_width">0</property>
+                    <property name="show_border">False</property>
+                    <property name="scrollable">False</property>
+                    <property name="hexpand">True</property>
+                    <property name="vexpand">True</property>
+                  </object>
+                </child>
+              </object>
+            </child>
+          </object>
+          <packing>
+            <property name="left_attach">0</property>
+            <property name="top_attach">1</property>
+            <property name="width">1</property>
+            <property name="height">1</property>
+          </packing>
+        </child>
+        <child>
+          <placeholder/>
+        </child>
+      </object>
+    </child>
+  </template>
+</interface>
diff --git a/src/dh.gresource.xml b/src/dh.gresource.xml
index 6d6ea52..1893e08 100644
--- a/src/dh.gresource.xml
+++ b/src/dh.gresource.xml
@@ -2,5 +2,6 @@
 <gresources>
   <gresource prefix="/org/gnome/devhelp">
     <file>devhelp.ui</file>
+    <file preprocess="xml-stripblanks">dh-window.ui</file>
   </gresource>
-</gresources>
\ No newline at end of file
+</gresources>


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