[gnome-calculator/wip/glade: 2/2] Migrate MathWindow to GtkBuilder and use GtkTemplate to define the UI layout



commit ac0183eef4a0516522b4e56e983ab9e30d78892f
Author: Alberto Ruiz <aruiz gnome org>
Date:   Sun Jan 10 20:03:34 2016 +0000

    Migrate MathWindow to GtkBuilder and use GtkTemplate to define the UI layout

 data/Makefile.am                    |    1 +
 data/gnome-calculator.gresource.xml |    1 +
 data/math-window.ui                 |  132 +++++++++++++++++++++++++++++++++++
 data/menu.ui                        |   29 --------
 gnome-calculator.doap               |    2 +-
 po/POTFILES.in                      |    1 +
 src/math-buttons.vala               |    2 +-
 src/math-window.vala                |   86 +++++------------------
 8 files changed, 156 insertions(+), 98 deletions(-)
---
diff --git a/data/Makefile.am b/data/Makefile.am
index 76fa087..957a555 100644
--- a/data/Makefile.am
+++ b/data/Makefile.am
@@ -19,6 +19,7 @@ EXTRA_DIST = \
        buttons-financial.ui \
        buttons-programming.ui \
        math-converter.ui \
+       math-window.ui \
        menu.ui \
        calculator.css \
        gnome-calculator.gresource.xml \
diff --git a/data/gnome-calculator.gresource.xml b/data/gnome-calculator.gresource.xml
index b68e708..30630d9 100644
--- a/data/gnome-calculator.gresource.xml
+++ b/data/gnome-calculator.gresource.xml
@@ -6,6 +6,7 @@
     <file preprocess="xml-stripblanks">buttons-financial.ui</file>
     <file preprocess="xml-stripblanks">buttons-programming.ui</file>
     <file preprocess="xml-stripblanks">math-converter.ui</file>
+    <file preprocess="xml-stripblanks">math-window.ui</file>
     <file preprocess="xml-stripblanks">menu.ui</file>
     <file>calculator.css</file>
   </gresource>
diff --git a/data/math-window.ui b/data/math-window.ui
new file mode 100644
index 0000000..ec55d93
--- /dev/null
+++ b/data/math-window.ui
@@ -0,0 +1,132 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- Generated with glade 3.19.0 -->
+<interface>
+  <requires lib="gtk+" version="3.16"/>
+  <object class="GtkAdjustment" id="hadjustment">
+    <property name="upper">100</property>
+    <property name="step_increment">1</property>
+    <property name="page_increment">10</property>
+  </object>
+  <object class="GtkAdjustment" id="vadjustment">
+    <property name="upper">100</property>
+    <property name="step_increment">1</property>
+    <property name="page_increment">10</property>
+    <signal name="changed" handler="scroll_changed_cb" swapped="no"/>
+    <signal name="value-changed" handler="scroll_value_changed_cb" swapped="no"/>
+  </object>
+  <menu id="window_menu">
+    <section>
+      <item>
+        <attribute name="label" translatable="yes">Basic Mode</attribute>
+        <attribute name="action">win.mode</attribute>
+        <attribute name="target">basic</attribute>
+      </item>
+      <item>
+        <attribute name="label" translatable="yes">Advanced Mode</attribute>
+        <attribute name="action">win.mode</attribute>
+        <attribute name="target">advanced</attribute>
+      </item>
+      <item>
+        <attribute name="label" translatable="yes">Financial Mode</attribute>
+        <attribute name="action">win.mode</attribute>
+        <attribute name="target">financial</attribute>
+      </item>
+      <item>
+        <attribute name="label" translatable="yes">Programming Mode</attribute>
+        <attribute name="action">win.mode</attribute>
+        <attribute name="target">programming</attribute>
+      </item>
+       <item>
+        <attribute name="label" translatable="yes">Keyboard Mode</attribute>
+        <attribute name="action">win.mode</attribute>
+        <attribute name="target">keyboard</attribute>
+      </item>
+    </section>
+  </menu>
+  <template class="MathWindow" parent="GtkApplicationWindow">
+    <property name="can_focus">False</property>
+    <property name="title" translatable="yes">Calculator</property>
+    <property name="role">gnome-calculator</property>
+    <property name="resizable">False</property>
+    <property name="show_menubar">False</property>
+    <child>
+      <object class="GtkGrid" id="grid">
+        <property name="visible">True</property>
+        <property name="can_focus">False</property>
+        <property name="vexpand">True</property>
+        <property name="border_width">6</property>
+        <property name="orientation">vertical</property>
+        <property name="row_spacing">6</property>
+        <child>
+          <object class="GtkScrolledWindow" id="scrolled_window">
+            <property name="visible">True</property>
+            <property name="can_focus">True</property>
+            <property name="hexpand">True</property>
+            <property name="vexpand">True</property>
+            <property name="hadjustment">hadjustment</property>
+            <property name="vadjustment">vadjustment</property>
+            <property name="hscrollbar_policy">never</property>
+            <property name="shadow_type">in</property>
+            <property name="min_content_height">154</property>
+            <child>
+              <placeholder/>
+            </child>
+          </object>
+          <packing>
+            <property name="left_attach">0</property>
+            <property name="top_attach">0</property>
+          </packing>
+        </child>
+      </object>
+    </child>
+    <child type="titlebar">
+      <object class="GtkHeaderBar" id="headerbar">
+        <property name="visible">True</property>
+        <property name="can_focus">False</property>
+        <property name="show_close_button">True</property>
+        <child type="title">
+          <object class="GtkMenuButton" id="menu_button">
+            <property name="visible">True</property>
+            <property name="can_focus">True</property>
+            <property name="receives_default">True</property>
+            <property name="relief">none</property>
+            <property name="menu_model">window_menu</property>
+            <child>
+              <object class="GtkGrid" id="grid1">
+                <property name="visible">True</property>
+                <property name="can_focus">False</property>
+                <child>
+                  <object class="GtkLabel" id="mode_label">
+                    <property name="visible">True</property>
+                    <property name="can_focus">False</property>
+                    <property name="halign">baseline</property>
+                    <property name="hexpand">True</property>
+                  </object>
+                  <packing>
+                    <property name="left_attach">0</property>
+                    <property name="top_attach">0</property>
+                  </packing>
+                </child>
+                <child>
+                  <object class="GtkArrow" id="arrow">
+                    <property name="visible">True</property>
+                    <property name="can_focus">False</property>
+                    <property name="arrow_type">down</property>
+                  </object>
+                  <packing>
+                    <property name="left_attach">1</property>
+                    <property name="top_attach">0</property>
+                  </packing>
+                </child>
+              </object>
+            </child>
+            <style>
+              <class name="title"/>
+              <class name="text-button"/>
+            </style>
+          </object>
+        </child>
+      </object>
+    </child>
+  </template>
+</interface>
diff --git a/data/menu.ui b/data/menu.ui
index 8a7a501..4bc1f5b 100644
--- a/data/menu.ui
+++ b/data/menu.ui
@@ -25,33 +25,4 @@
       </item>
     </section>
   </menu>
-  <menu id="window-menu">
-    <section>
-      <item>
-        <attribute name="label" translatable="yes">Basic Mode</attribute>
-        <attribute name="action">win.mode</attribute>
-        <attribute name="target">basic</attribute>
-      </item>
-      <item>
-        <attribute name="label" translatable="yes">Advanced Mode</attribute>
-        <attribute name="action">win.mode</attribute>
-        <attribute name="target">advanced</attribute>
-      </item>
-      <item>
-        <attribute name="label" translatable="yes">Financial Mode</attribute>
-        <attribute name="action">win.mode</attribute>
-        <attribute name="target">financial</attribute>
-      </item>
-      <item>
-        <attribute name="label" translatable="yes">Programming Mode</attribute>
-        <attribute name="action">win.mode</attribute>
-        <attribute name="target">programming</attribute>
-      </item>
-       <item>
-        <attribute name="label" translatable="yes">Keyboard Mode</attribute>
-        <attribute name="action">win.mode</attribute>
-        <attribute name="target">keyboard</attribute>
-      </item>
-    </section>
-  </menu>
 </interface>
diff --git a/gnome-calculator.doap b/gnome-calculator.doap
index 90d320e..66de213 100644
--- a/gnome-calculator.doap
+++ b/gnome-calculator.doap
@@ -4,7 +4,7 @@
          xmlns:gnome="http://api.gnome.org/doap-extensions#";
          xmlns="http://usefulinc.com/ns/doap#";>
 
-  <name xml:lang="en">gnome-calculator</name>
+  <name xml:lang="en">GNOME Calculator/name>
   <shortdesc xml:lang="en">Desktop calculator</shortdesc>
   <homepage rdf:resource="https://wiki.gnome.org/Apps/Calculator"; />
   <mailing-list rdf:resource="https://mail.gnome.org/mailman/listinfo/gcalctool-list"; />
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 4124820..f0d867a 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -8,6 +8,7 @@
 data/gnome-calculator.desktop.in
 data/gnome-calculator.appdata.xml.in
 data/math-converter.ui
+data/math-window.ui
 [type: gettext/glade]data/menu.ui
 data/org.gnome.calculator.gschema.xml
 lib/currency.vala
diff --git a/src/math-buttons.vala b/src/math-buttons.vala
index 1207eff..9fd6333 100644
--- a/src/math-buttons.vala
+++ b/src/math-buttons.vala
@@ -94,7 +94,7 @@ public class MathButtons : Gtk.Box
 
     public MathButtons (MathEquation equation)
     {
-        Object (orientation: Gtk.Orientation.VERTICAL);
+        Object (orientation: Gtk.Orientation.VERTICAL, vexpand_set: true);
         spacing = 6;
         show.connect (load_buttons);
         this.equation = equation;
diff --git a/src/math-window.vala b/src/math-window.vala
index c6c32f7..8424b2b 100644
--- a/src/math-window.vala
+++ b/src/math-window.vala
@@ -9,6 +9,7 @@
  * license.
  */
 
+[GtkTemplate (ui = "/org/gnome/calculator/math-window.ui")]
 public class MathWindow : Gtk.ApplicationWindow
 {
     private MathEquation _equation;
@@ -21,11 +22,15 @@ public class MathWindow : Gtk.ApplicationWindow
     public MathButtons buttons { get { return _buttons; } }
     private bool right_aligned;
     private bool remove_buttons;
-    private Gtk.MenuButton menu_button;
 
-    private Gtk.HeaderBar headerbar;
-    private Gtk.Box vbox;
+    [GtkChild]
+    private Gtk.MenuButton menu_button;
+    [GtkChild]
     private Gtk.Label mode_label;
+    [GtkChild]
+    private Gtk.ScrolledWindow scrolled_window;
+    [GtkChild]
+    private Gtk.Grid grid;
 
     private const ActionEntry[] window_entries =
     {
@@ -40,71 +45,14 @@ public class MathWindow : Gtk.ApplicationWindow
     {
         Object (application: app);
         _equation = equation;
-        set_title (/* Title of main window */
-                   _("Calculator"));
+        right_aligned = true;
+
+        add_action_entries (window_entries, this);
 
-        role = "gnome-calculator";
-        resizable = false;
         converter = new MathConverter (_equation);
         converter.set_category (null);
         converter.set_conversion (equation.source_units, equation.target_units);
-        add_action_entries (window_entries, this);
-
-        var builder = new Gtk.Builder ();
-        try
-        {
-            builder.add_from_resource ("/org/gnome/calculator/menu.ui");
-        }
-        catch (Error e)
-        {
-            error ("Error loading menu UI: %s", e.message);
-        }
-
-        mode_label = new Gtk.Label (null);
-        mode_label.show ();
-
-        var arrow = new Gtk.Arrow (Gtk.ArrowType.DOWN, Gtk.ShadowType.NONE);
-        arrow.show ();
-
-        var menu_box = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 6);
-        menu_box.pack_start (mode_label);
-        menu_box.pack_start (arrow);
-        menu_box.show ();
-
-        menu_button = new Gtk.MenuButton ();
-        menu_button.add (menu_box);
-        menu_button.menu_model = (MenuModel) builder.get_object ("window-menu");
-        menu_button.get_style_context ().add_class ("title");
-        menu_button.get_style_context ().add_class ("text-button");
-        menu_button.use_popover = true;
-        menu_button.relief = Gtk.ReliefStyle.NONE;
-        menu_button.show ();
-
-        headerbar = new Gtk.HeaderBar ();
-        headerbar.show_close_button = true;
-        headerbar.custom_title = menu_button;
-        headerbar.show ();
-        set_titlebar (headerbar);
-
-        var main_vbox = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
-        add (main_vbox);
-        main_vbox.show ();
-
-        vbox = new Gtk.Box (Gtk.Orientation.VERTICAL, 6);
-        vbox.border_width = 6;
-        vbox.set_vexpand (true);
-        vbox.pack_start (converter, false, true, 0);
-        main_vbox.pack_start (vbox, true, true, 0);
-        vbox.show ();
-
-        var scrolled_window = new Gtk.ScrolledWindow (null, null);
-        scrolled_window.set_policy (Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.NEVER);
-        scrolled_window.set_shadow_type (Gtk.ShadowType.IN);
-        vbox.pack_start (scrolled_window, false, true, 0);
-        scrolled_window.get_hadjustment ().changed.connect (scroll_changed_cb);
-        scrolled_window.get_hadjustment ().value_changed.connect (scroll_value_changed_cb);
-        right_aligned = true;
-        scrolled_window.show ();
+        grid.add (converter);
 
         _display = new MathDisplay (equation);
         scrolled_window.add (_display);
@@ -112,15 +60,17 @@ public class MathWindow : Gtk.ApplicationWindow
         _display.grabfocus ();
 
         _buttons = new MathButtons (equation);
+        grid.add(buttons);
 
         if (_buttons.mode != ButtonMode.KEYBOARD) /* Checks if the calculator is in Keyboard mode or not */
         {
-            vbox.pack_start (buttons, true, true, 0); /* Packs buttons if not in keyboard mode. */
+            buttons.set_visible (true);
             remove_buttons = false;
             converter.set_visible (false);
         }
         else
         {
+            buttons.set_visible(false);
             remove_buttons = true;
             converter.set_visible (true); /* Unpacks buttons if in keyboard mode */
             resizable = true;
@@ -180,14 +130,14 @@ public class MathWindow : Gtk.ApplicationWindow
 
         if (remove_buttons ==  true && buttons.mode != ButtonMode.KEYBOARD)
         {
-            vbox.pack_start (buttons, true, true, 0); /* Packs buttons when calculator is switched from 
keyboard mode to any other mode */
+            buttons.set_visible (true);
             remove_buttons = false;
             converter.set_visible (false);
             resizable = false;
         }
         else if (remove_buttons == false && buttons.mode == ButtonMode.KEYBOARD)
         {
-            vbox.remove (vbox.get_children ().nth_data (2)); /* Unpacks buttons when switched to keyboard 
mode */
+            buttons.set_visible (false);
             remove_buttons = true;
             converter.set_visible (true); /* Converter above the display window is set to visible. */
             resizable = true;
@@ -238,12 +188,14 @@ public class MathWindow : Gtk.ApplicationWindow
         return result;
     }
 
+    [GtkCallback]
     private void scroll_changed_cb (Gtk.Adjustment adjustment)
     {
         if (right_aligned)
             adjustment.set_value (adjustment.get_upper () - adjustment.get_page_size ());
     }
 
+    [GtkCallback]
     private void scroll_value_changed_cb (Gtk.Adjustment adjustment)
     {
         if (adjustment.get_value () == adjustment.get_upper () - adjustment.get_page_size ())


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