[gnome-calculator] Fixed consecutive squaring



commit f287847f32b237ece30381aacbbf4d476ecb7590
Author: Garima Joshi <gjoshi0311 gmail com>
Date:   Fri May 3 13:34:36 2013 +0530

    Fixed consecutive squaring

 src/math-buttons.vala  |    8 ++++++--
 src/math-equation.vala |   26 ++++++++++++++++++++++++++
 2 files changed, 32 insertions(+), 2 deletions(-)
---
diff --git a/src/math-buttons.vala b/src/math-buttons.vala
index f75533a..755334e 100644
--- a/src/math-buttons.vala
+++ b/src/math-buttons.vala
@@ -315,8 +315,6 @@ public class MathButtons : Gtk.Box
         setup_button (builder, "function",           null, _("Additional Functions"));
         /* Tooltip for the exponent button */
         setup_button (builder, "x_pow_y",            "^", _("Exponent [^ or **]"));
-        /* Tooltip for the square button */
-        setup_button (builder, "x_squared",          "²", _("Square [Ctrl+2]"));
         /* Tooltip for the percentage button */
         setup_button (builder, "percentage",         "%", _("Percentage [%]"));
         /* Tooltip for the factorial button */
@@ -427,6 +425,12 @@ public class MathButtons : Gtk.Box
         var button = builder.get_object ("calc_subtract_button") as Gtk.Button;
         if (button != null)
             button.clicked.connect (() => { equation.insert_subtract (); });
+        button = builder.get_object ("calc_x_squared_button") as Gtk.Button;
+        if (button != null)
+        {
+            button.clicked.connect (() => { equation.insert_square (); });
+            button.set_tooltip_text (_("Square [Ctrl+2]"));
+        }
         button = builder.get_object ("calc_undo_button") as Gtk.Button;
         if (button != null)
             button.clicked.connect (() => { equation.undo (); });
diff --git a/src/math-equation.vala b/src/math-equation.vala
index 9edb169..ad39671 100644
--- a/src/math-equation.vala
+++ b/src/math-equation.vala
@@ -792,6 +792,32 @@ public class MathEquation : Gtk.TextBuffer
         insert_at_cursor (text, -1);
     }
 
+    public new void insert_square ()
+    {
+        var space_required = false;
+        Gtk.TextIter iter;
+        get_iter_at_mark (out iter, get_insert ());
+
+        /*if it is not the first character in the buffer*/
+        if (iter.backward_char ())
+        {
+            unichar previous_character = iter.get_char ();
+            if ("⁰¹²³⁴⁵⁶⁷⁸⁹".index_of_char (previous_character) >= 0)
+            {
+                space_required = true;
+            }
+        }
+
+        if (space_required)
+        {
+            insert (" ²");
+        }
+        else
+        {
+            insert ("²");
+        }
+    }
+
     public void insert_digit (uint digit)
     {
         const unichar subscript_digits[] = {'₀', '₁', '₂', '₃', '₄', '₅', '₆', '₇', '₈', '₉'};


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