[gnome-calculator] New feature: word size changer in programming mode



commit f145996000797ac98d241bb411658121454eecac
Author: A.M. Rowsell <amrowsell frozenelectronics ca>
Date:   Sat Oct 17 11:42:36 2020 -0400

    New feature: word size changer in programming mode
    
    This feature adds a word size changer GtkMenuButton to
    programming mode. It also changes the preferences menu for the
    same option (word size) to use "bit" instead of "bits".

 src/math-buttons.vala         | 38 +++++++++++++++++++++++++++++++++-
 src/ui/buttons-programming.ui | 48 ++++++++++++++++++++++++++++++++++++++++++-
 src/ui/math-preferences.ui    |  8 ++++----
 3 files changed, 88 insertions(+), 6 deletions(-)
---
diff --git a/src/math-buttons.vala b/src/math-buttons.vala
index 694ec7f1..fc8b6269 100644
--- a/src/math-buttons.vala
+++ b/src/math-buttons.vala
@@ -68,6 +68,7 @@ public class MathButtons : Gtk.Box
 
     private Gtk.ComboBox base_combo;
     private Gtk.Label base_label;
+    private Gtk.Label word_size_label;
     private Gtk.Widget bit_panel;
     private List<Gtk.Button> toggle_bit_buttons;
 
@@ -98,6 +99,7 @@ public class MathButtons : Gtk.Box
         {"factorize",            on_factorize                                },
         {"insert-exponent",      on_insert_exponent                          },
         {"bitshift",             on_bitshift,             "i"                },
+        {"set-word-size",        on_set_word_size,        "i"                },
         {"toggle-bit",           on_toggle_bit,           "i"                },
         {"insert-character",     on_insert_character                         },
         {"insert-numeric-point", on_insert_numeric_point                     },
@@ -119,6 +121,7 @@ public class MathButtons : Gtk.Box
         equation.notify["number-mode"].connect ((pspec) => { number_mode_changed_cb (); });
         equation.notify["angle-units"].connect ((pspec) => { update_bit_panel (); });
         equation.notify["number-format"].connect ((pspec) => { update_bit_panel (); });
+        equation.notify["word-size"].connect ((pspec) => { word_size_changed_cb (); });
         number_mode_changed_cb ();
         update_bit_panel ();
     }
@@ -201,6 +204,13 @@ public class MathButtons : Gtk.Box
         equation.insert_shift (param.get_int32 ());
     }
 
+    private void on_set_word_size (SimpleAction action, Variant? param)
+    {
+        equation.word_size = (param.get_int32 ());
+        string format = ngettext("%d-bit", "%d-bit", param.get_int32 ());
+        word_size_label.set_label(format.printf(param.get_int32 ()));
+    }
+
     private void on_insert_numeric_point (SimpleAction action, Variant? param)
     {
         equation.insert_numeric_point ();
@@ -351,6 +361,9 @@ public class MathButtons : Gtk.Box
         menu_button = builder.get_object ("calc_shift_right_button") as Gtk.MenuButton;
         if (menu_button != null)
             menu_button.menu_model = create_shift_menu (false);
+        menu_button = builder.get_object ("calc_word_size_button") as Gtk.MenuButton;
+        if (menu_button != null)
+            menu_button.menu_model = create_word_size_menu ();
         menu_button = builder.get_object ("calc_memory_button") as Gtk.MenuButton;
         if (menu_button != null)
             menu_button.popover = new MathVariablePopover (equation);
@@ -380,7 +393,7 @@ public class MathButtons : Gtk.Box
                 i++;
             }
             toggle_bit_buttons.reverse ();
-
+            word_size_label = builder.get_object ("word_size_label") as Gtk.Label;
             base_combo = builder.get_object ("base_combo") as Gtk.ComboBox;
             base_combo.changed.connect (base_combobox_changed_cb);
             equation.notify["number-base"].connect ((pspec) => { base_changed_cb (); } );
@@ -472,6 +485,29 @@ public class MathButtons : Gtk.Box
         return shift_menu;
     }
 
+    private Menu create_word_size_menu ()
+    {
+        var word_size_menu = new Menu ();
+        var i = 64;
+        string format = ngettext ("%d-bit", "%d-bit", i);
+        word_size_menu.append(format.printf (i), "cal.set-word-size(%d)".printf (i));
+        i = 32;
+        word_size_menu.append(format.printf (i), "cal.set-word-size(%d)".printf (i));
+        i = 16;
+        word_size_menu.append(format.printf (i), "cal.set-word-size(%d)".printf (i));
+        i = 8;
+        word_size_menu.append(format.printf (i), "cal.set-word-size(%d)".printf (i));
+
+        return word_size_menu;
+    }
+
+    private void word_size_changed_cb ()
+    {
+        var size = equation.word_size;
+        string format = ngettext("%d-bit", "%d-bit", size);
+        word_size_label.set_label(format.printf(size));
+    }
+
     private void on_launch_finc_dialog (SimpleAction action, Variant? param)
     {
         var name = param.get_string ();
diff --git a/src/ui/buttons-programming.ui b/src/ui/buttons-programming.ui
index 51be8bf4..aaaefeb5 100644
--- a/src/ui/buttons-programming.ui
+++ b/src/ui/buttons-programming.ui
@@ -2327,7 +2327,53 @@
           </packing>
         </child>
         <child>
-          <placeholder/>
+          <object class="GtkMenuButton" id="calc_word_size_button">
+            <property name="visible">True</property>
+            <property name="can_focus">True</property>
+            <property name="focus_on_click">False</property>
+            <property name="receives_default">True</property>
+            <property name="tooltip_text" translatable="yes">Change word size</property>
+            <child>
+              <object class="GtkHBox" id="hbox2">
+                <property name="visible">True</property>
+                <property name="can_focus">False</property>
+                <property name="spacing">3</property>
+                <child>
+                  <object class="GtkLabel" id="word_size_label">
+                    <property name="visible">True</property>
+                    <property name="can_focus">False</property>
+                    <property name="label">64-bit</property>
+                  </object>
+                  <packing>
+                    <property name="expand">True</property>
+                    <property name="fill">True</property>
+                    <property name="position">0</property>
+                  </packing>
+                </child>
+                <child>
+                  <object class="GtkArrow" id="arrow1">
+                    <property name="visible">True</property>
+                    <property name="can_focus">False</property>
+                    <property name="arrow_type">down</property>
+                  </object>
+                  <packing>
+                    <property name="expand">False</property>
+                    <property name="fill">True</property>
+                    <property name="position">1</property>
+                  </packing>
+                </child>
+              </object>
+            </child>
+            <child internal-child="accessible">
+              <object class="AtkObject" id="calc_word_size_button-atkobject">
+                <property name="AtkObject::accessible-name" translatable="yes">Word Size</property>
+              </object>
+            </child>
+          </object>
+          <packing>
+            <property name="left_attach">9</property>
+            <property name="top_attach">0</property>
+          </packing>
         </child>
         <style>
           <class name="math-buttons"/>
diff --git a/src/ui/math-preferences.ui b/src/ui/math-preferences.ui
index d60a9d0b..6cf53c0a 100644
--- a/src/ui/math-preferences.ui
+++ b/src/ui/math-preferences.ui
@@ -193,10 +193,10 @@
                 <property name="visible">True</property>
                 <property name="can_focus">False</property>
                 <items>
-                  <item id="8" translatable="yes" comments="Word size combo: 8 bits">8 bits</item>
-                  <item id="16" translatable="yes" comments="Word size combo: 16 bits">16 bits</item>
-                  <item id="32" translatable="yes" comments="Word size combo: 32 bits">32 bits</item>
-                  <item id="64" translatable="yes" comments="Word size combo: 64 bits">64 bits</item>
+                  <item id="8" translatable="yes" comments="Word size combo: 8 bit">8 bit</item>
+                  <item id="16" translatable="yes" comments="Word size combo: 16 bit">16 bit</item>
+                  <item id="32" translatable="yes" comments="Word size combo: 32 bit">32 bit</item>
+                  <item id="64" translatable="yes" comments="Word size combo: 64 bit">64 bit</item>
                 </items>
               </object>
               <packing>


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