[gcalctool] Add a switch units button. Patch by Jonh Wendell, with some changes by Robert Ancell. Closes #63319



commit 9ee104fade0eba634dc46ca324cf7f1f231093cd
Author: Jonh Wendell <jwendell gnome org>
Date:   Wed May 25 10:14:10 2011 +1000

    Add a switch units button.  Patch by Jonh Wendell, with some changes by Robert Ancell.
    Closes #633193.

 NEWS                 |    1 +
 src/math-converter.c |   86 ++++++++++++++++++++++++++++++++++++++-----------
 2 files changed, 67 insertions(+), 20 deletions(-)
---
diff --git a/NEWS b/NEWS
index 59db649..323aa88 100644
--- a/NEWS
+++ b/NEWS
@@ -17,6 +17,7 @@ Overview of changes in gcalctool 6.1.0
     * Make automatic exponentiation less aggressive
     * Fix thousands separator being on front of three digit numbers
     * Don't clear display when entering digit inside result
+    * Add a swap units button (Jonh Wendell, Bug #633193)
 
 Overview of changes in gcalctool 6.0.0
 
diff --git a/src/math-converter.c b/src/math-converter.c
index 46e3601..2f543fe 100644
--- a/src/math-converter.c
+++ b/src/math-converter.c
@@ -50,49 +50,67 @@ math_converter_new(MathEquation *equation)
 }
 
 
-static void
-update_result_label(MathConverter *converter)
+static gboolean
+convert_equation(MathConverter *converter, const MPNumber *x, MPNumber *z)
 {
     GtkTreeIter from_iter, to_iter;
     UnitCategory *category = NULL;
     Unit *source_unit = NULL, *target_unit = NULL;
+    gboolean result;
+
+    if (!gtk_combo_box_get_active_iter(GTK_COMBO_BOX(converter->priv->from_combo), &from_iter) ||
+        !gtk_combo_box_get_active_iter(GTK_COMBO_BOX(converter->priv->to_combo), &to_iter))
+        return FALSE;
+
+    gtk_tree_model_get(gtk_combo_box_get_model(GTK_COMBO_BOX(converter->priv->from_combo)), &from_iter, 1, &category, 2, &source_unit, -1);
+    gtk_tree_model_get(gtk_combo_box_get_model(GTK_COMBO_BOX(converter->priv->to_combo)), &to_iter, 2, &target_unit, -1);
+
+    result = unit_category_convert(category, x, source_unit, target_unit, z);
+
+    if (category)
+        g_object_unref(category);
+    if (source_unit)
+        g_object_unref(source_unit);
+    if (target_unit)
+        g_object_unref(target_unit);
+
+    return result;
+}
+
+
+static void
+update_result_label(MathConverter *converter)
+{
     MPNumber x, z;
     gboolean enabled;
 
     if (!converter->priv->result_label)
         return;
 
-    enabled = math_equation_get_number(converter->priv->equation, &x);
-
-    if (enabled &&
-        gtk_combo_box_get_active_iter(GTK_COMBO_BOX(converter->priv->from_combo), &from_iter) &&
-        gtk_combo_box_get_active_iter(GTK_COMBO_BOX(converter->priv->to_combo), &to_iter)) {
-        gtk_tree_model_get(gtk_combo_box_get_model(GTK_COMBO_BOX(converter->priv->from_combo)), &from_iter, 1, &category, 2, &source_unit, -1);
-        gtk_tree_model_get(gtk_combo_box_get_model(GTK_COMBO_BOX(converter->priv->to_combo)), &to_iter, 2, &target_unit, -1);
-        if (!unit_category_convert(category, &x, source_unit, target_unit, &z))
-            enabled = FALSE;
-    }
+    if (math_equation_get_number(converter->priv->equation, &x))
+        enabled = convert_equation(converter, &x, &z);
     else
-       enabled = FALSE;
+        enabled = FALSE;
 
     gtk_widget_set_sensitive(converter->priv->result_label, enabled);
     if (enabled) {
         gchar *source_text, *target_text, *label;
+        Unit *source_unit, *target_unit;
+
+        math_converter_get_conversion(converter, &source_unit, &target_unit);
+
         source_text = unit_format(source_unit, &x);
         target_text = unit_format(target_unit, &z);
         label = g_strdup_printf("%s = %s", source_text, target_text);
         gtk_label_set_text(GTK_LABEL(converter->priv->result_label), label);
+
         g_free(source_text);
         g_free(target_text);
         g_free(label);
-    }
 
-    if (category)
-        g_object_unref(category);
-    if (source_unit)
         g_object_unref(source_unit);
-    if (target_unit)
         g_object_unref(target_unit);
+    }
 }
 
 
@@ -343,11 +361,30 @@ currency_updated_cb(CurrencyManager *manager, MathConverter *converter)
     update_result_label(converter);
 }
 
+static void
+swap_button_clicked_cb(GtkButton *button, MathConverter *converter)
+{
+    Unit *from_unit, *to_unit;
+    MPNumber x, z;
+
+    if (math_equation_get_number(converter->priv->equation, &x) &&
+        convert_equation(converter, &x, &z))
+        math_equation_set_number(converter->priv->equation, &z);
+
+    math_converter_get_conversion(converter, &from_unit, &to_unit);
+    set_active_unit(GTK_COMBO_BOX(converter->priv->from_combo), NULL, to_unit);
+    set_active_unit(GTK_COMBO_BOX(converter->priv->to_combo), NULL, from_unit);
+
+    update_result_label(converter);
+
+    g_object_unref(from_unit);
+    g_object_unref(to_unit);
+}
 
 static void
 math_converter_init(MathConverter *converter)
 {
-    GtkWidget *hbox, *label;
+    GtkWidget *hbox, *label, *swap_button;
     GtkCellRenderer *renderer;
 
     converter->priv = G_TYPE_INSTANCE_GET_PRIVATE(converter, math_converter_get_type(), MathConverterPrivate);
@@ -374,7 +411,7 @@ math_converter_init(MathConverter *converter)
     label = gtk_label_new(/* Label that is displayed between the two conversion combo boxes, e.g. "[degrees] in [radians]" */
                           _(" in "));
     gtk_widget_show(label);
-    gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, TRUE, 0);
+    gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, TRUE, 5);
 
     converter->priv->to_combo = gtk_combo_box_new();
     renderer = gtk_cell_renderer_text_new();
@@ -384,6 +421,15 @@ math_converter_init(MathConverter *converter)
     gtk_widget_show(converter->priv->to_combo);
     gtk_box_pack_start(GTK_BOX(hbox), converter->priv->to_combo, FALSE, TRUE, 0);
 
+    swap_button = gtk_button_new_with_label ("â??");
+    gtk_widget_set_tooltip_text (swap_button,
+                                 /* Tooltip for swap conversion button */
+                                 _("Switch conversion units"));
+    gtk_button_set_relief (GTK_BUTTON (swap_button), GTK_RELIEF_NONE);
+    g_signal_connect (swap_button, "clicked", G_CALLBACK (swap_button_clicked_cb), converter);
+    gtk_widget_show(swap_button);
+    gtk_box_pack_start(GTK_BOX(hbox), swap_button, FALSE, TRUE, 0);
+
     converter->priv->result_label = gtk_label_new("");
     gtk_misc_set_alignment(GTK_MISC(converter->priv->result_label), 1.0, 0.5);
     gtk_widget_set_sensitive(converter->priv->result_label, FALSE);



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