[gcalctool/vala] Fix up financial dialog stuff



commit 0398f401eb8a48ca8ddf2f14b0148422a320f67e
Author: Robert Ancell <robert ancell canonical com>
Date:   Fri Oct 12 20:15:20 2012 +1300

    Fix up financial dialog stuff

 src/math-buttons.vala |  172 +++++++++++++++++++++++++------------------------
 1 files changed, 87 insertions(+), 85 deletions(-)
---
diff --git a/src/math-buttons.vala b/src/math-buttons.vala
index 8dea38d..fb970cc 100644
--- a/src/math-buttons.vala
+++ b/src/math-buttons.vala
@@ -82,21 +82,16 @@ public class MathButtons : Gtk.Box
     private Gtk.Entry character_code_entry;
 
     /* The names of each field in the dialogs for the financial functions */
-    // FIXME
-    /*private const string[,] finc_dialog_fields =
-    {
-        {"ctrm_pint", "ctrm_fv",     "ctrm_pv",    null,         null},
-        {"ddb_cost",  "ddb_life",    "ddb_period", null,         null},
-        {"fv_pmt",    "fv_pint",     "fv_n",       null,         null},
-        {"gpm_cost",  "gpm_margin",  null,         null,         null},
-        {"pmt_prin",  "pmt_pint",    "pmt_n",      null,         null},
-        {"pv_pmt",    "pv_pint",     "pv_n",       null,         null},
-        {"rate_fv",   "rate_pv",     "rate_n",     null,         null},
-        {"sln_cost",  "sln_salvage", "sln_life",   null,         null},
-        {"syd_cost",  "syd_salvage", "syd_life",   "syd_period", null},
-        {"term_pmt",  "term_fv",     "term_pint",  null,         null},
-        {null,        null,          null,         null,         null}
-    };*/
+    private const string[] ctrm_entries =  {"ctrm_pint", "ctrm_fv", "ctrm_pv"};
+    private const string[] ddb_entries = {"ddb_cost", "ddb_life", "ddb_period"};
+    private const string[] fv_entries = {"fv_pmt", "fv_pint", "fv_n"};
+    private const string[] gpm_entries = {"gpm_cost", "gpm_margin"};
+    private const string[] pmt_entries = {"pmt_prin", "pmt_pint", "pmt_n"};
+    private const string[] pv_entries = {"pv_pmt", "pv_pint", "pv_n"};
+    private const string[] rate_entries = {"rate_fv", "rate_pv", "rate_n"};
+    private const string[] sln_entries = {"sln_cost", "sln_salvage", "sln_life"};
+    private const string[] syd_entries = {"syd_cost", "syd_salvage", "syd_life"};
+    private const string[] term_entries = {"term_pmt","term_fv", "term_pint"};
 
     public MathButtons (MathEquation equation)
     {
@@ -115,48 +110,30 @@ public class MathButtons : Gtk.Box
 
     private void load_finc_dialogs ()
     {
-        var dialog = financial_ui.get_object ("ctrm_dialog") as Gtk.Dialog;
-        dialog.set_data<int> ("finc_dialog", FinancialDialog.CTRM_DIALOG);
-        dialog.response.connect (finc_response_cb);
-        dialog = financial_ui.get_object ("ddb_dialog") as Gtk.Dialog;
-        dialog.set_data<int> ("finc_dialog", FinancialDialog.DDB_DIALOG);
-        dialog.response.connect (finc_response_cb);
-        dialog = financial_ui.get_object ("fv_dialog") as Gtk.Dialog;
-        dialog.set_data<int> ("finc_dialog", FinancialDialog.FV_DIALOG);
-        dialog.response.connect (finc_response_cb);
-        dialog = financial_ui.get_object ("gpm_dialog") as Gtk.Dialog;
-        dialog.set_data<int> ("finc_dialog", FinancialDialog.GPM_DIALOG);
-        dialog.response.connect (finc_response_cb);
-        dialog = financial_ui.get_object ("pmt_dialog") as Gtk.Dialog;
-        dialog.set_data<int> ("finc_dialog", FinancialDialog.PMT_DIALOG);
-        dialog.response.connect (finc_response_cb);
-        dialog = financial_ui.get_object ("pv_dialog") as Gtk.Dialog;
-        dialog.set_data<int> ("finc_dialog", FinancialDialog.PV_DIALOG);
-        dialog.response.connect (finc_response_cb);
-        dialog = financial_ui.get_object ("rate_dialog") as Gtk.Dialog;
-        dialog.set_data<int> ("finc_dialog", FinancialDialog.RATE_DIALOG);
-        dialog.response.connect (finc_response_cb);
-        dialog = financial_ui.get_object ("sln_dialog") as Gtk.Dialog;
-        dialog.set_data<int> ("finc_dialog", FinancialDialog.SLN_DIALOG);
-        dialog.response.connect (finc_response_cb);
-        dialog = financial_ui.get_object ("syd_dialog") as Gtk.Dialog;
-        dialog.set_data<int> ("finc_dialog", FinancialDialog.SYD_DIALOG);
-        dialog.response.connect (finc_response_cb);
-        dialog = financial_ui.get_object ("term_dialog") as Gtk.Dialog;
-        dialog.set_data<int> ("finc_dialog", FinancialDialog.TERM_DIALOG);
-        dialog.response.connect (finc_response_cb);
+        load_finc_dialog ("ctrm_dialog", ctrm_entries, FinancialDialog.CTRM_DIALOG);
+        load_finc_dialog ("ddb_dialog", ddb_entries, FinancialDialog.DDB_DIALOG);
+        load_finc_dialog ("fv_dialog", fv_entries, FinancialDialog.FV_DIALOG);
+        load_finc_dialog ("gpm_dialog", gpm_entries, FinancialDialog.GPM_DIALOG);
+        load_finc_dialog ("pmt_dialog", pmt_entries, FinancialDialog.PMT_DIALOG);
+        load_finc_dialog ("pv_dialog", pv_entries, FinancialDialog.PV_DIALOG);
+        load_finc_dialog ("rate_dialog", rate_entries, FinancialDialog.RATE_DIALOG);
+        load_finc_dialog ("sln_dialog", sln_entries, FinancialDialog.SLN_DIALOG);
+        load_finc_dialog ("syd_dialog", syd_entries, FinancialDialog.SYD_DIALOG);
+        load_finc_dialog ("term_dialog", term_entries, FinancialDialog.TERM_DIALOG);
+    }
 
-        // FIXME
-        /*for (var i = 0; finc_dialog_fields[i, 0] != null; i++)
+    private void load_finc_dialog (string name, string[] entry_names, FinancialDialog function)
+    {
+        var dialog = financial_ui.get_object (name) as Gtk.Dialog;
+        dialog.set_data<int> ("finc-function", function);
+        dialog.response.connect (finc_response_cb);
+        for (var i = 0; i < entry_names.length; i++)
         {
-            for (var j = 0; finc_dialog_fields[i, j] != null; j++)
-            {
-                var o = financial_ui.get_object (finc_dialog_fields[i, j]);
-                o.set_data<int> ("finc_field", i);
-                o.set_data<int> ("finc_dialog", j);
-                o.activate.connect (finc_activate_cb);
-            }
-        }*/
+            var entry = financial_ui.get_object (entry_names[i]) as Gtk.Entry;
+            if (i != ctrm_entries.length - 1)
+                entry.set_data<Gtk.Entry> ("next-entry", financial_ui.get_object (entry_names[i+1]) as Gtk.Entry);
+            entry.activate.connect (finc_activate_cb);
+        }
     }
 
     private void update_bit_panel ()
@@ -574,61 +551,61 @@ public class MathButtons : Gtk.Box
             button = builder.get_object ("calc_finc_compounding_term_button") as Gtk.Button;
             if (button != null)
             {
-                button.set_data<string> ("finc_dialog", "ctrm_dialog");
+                button.set_data<string> ("finc-dialog-name", "ctrm_dialog");
                 button.clicked.connect (finc_cb);
             }
             button = builder.get_object ("calc_finc_double_declining_depreciation_button") as Gtk.Button;
             if (button != null)
             {
-                button.set_data<string> ("finc_dialog", "ddb_dialog");
+                button.set_data<string> ("finc-dialog-name", "ddb_dialog");
                 button.clicked.connect (finc_cb);
             }
             button = builder.get_object ("calc_finc_future_value_button") as Gtk.Button;
             if (button != null)
             {
-                button.set_data<string> ("finc_dialog", "fv_dialog");
+                button.set_data<string> ("finc-dialog-name", "fv_dialog");
                 button.clicked.connect (finc_cb);
             }
             button = builder.get_object ("calc_finc_gross_profit_margin_button") as Gtk.Button;
             if (button != null)
             {
-                button.set_data<string> ("finc_dialog", "gpm_dialog");
+                button.set_data<string> ("finc-dialog-name", "gpm_dialog");
                 button.clicked.connect (finc_cb);
             }
             button = builder.get_object ("calc_finc_periodic_payment_button") as Gtk.Button;
             if (button != null)
             {
-                button.set_data<string> ("finc_dialog", "pmt_dialog");
+                button.set_data<string> ("finc-dialog-name", "pmt_dialog");
                 button.clicked.connect (finc_cb);
             }
             button = builder.get_object ("calc_finc_present_value_button") as Gtk.Button;
             if (button != null)
             {
-                button.set_data<string> ("finc_dialog", "pv_dialog");
+                button.set_data<string> ("finc-dialog-name", "pv_dialog");
                 button.clicked.connect (finc_cb);
             }
             button = builder.get_object ("calc_finc_periodic_interest_rate_button") as Gtk.Button;
             if (button != null)
             {
-                button.set_data<string> ("finc_dialog", "rate_dialog");
+                button.set_data<string> ("finc-dialog-name", "rate_dialog");
                 button.clicked.connect (finc_cb);
             }
             button = builder.get_object ("calc_finc_straight_line_depreciation_button") as Gtk.Button;
             if (button != null)
             {
-                button.set_data<string> ("finc_dialog", "sln_dialog");
+                button.set_data<string> ("finc-dialog-name", "sln_dialog");
                 button.clicked.connect (finc_cb);
             }
             button = builder.get_object ("calc_finc_sum_of_the_years_digits_depreciation_button") as Gtk.Button;
             if (button != null)
             {
-                button.set_data<string> ("finc_dialog", "syd_dialog");
+                button.set_data<string> ("finc-dialog-name", "syd_dialog");
                 button.clicked.connect (finc_cb);
             }
             button = builder.get_object ("calc_finc_term_button") as Gtk.Button;
             if (button != null)
             {
-                button.set_data<string> ("finc_dialog", "term_dialog");
+                button.set_data<string> ("finc-dialog-name", "term_dialog");
                 button.clicked.connect (finc_cb);
             }
         }
@@ -863,7 +840,7 @@ public class MathButtons : Gtk.Box
 
     private void finc_cb (Gtk.Widget widget)
     {
-        var name = widget.get_data<string> ("finc_dialog");
+        var name = widget.get_data<string> ("finc-dialog-name");
         var dialog = financial_ui.get_object (name) as Gtk.Dialog;
         dialog.run ();
         dialog.hide ();
@@ -876,11 +853,8 @@ public class MathButtons : Gtk.Box
 
     private void finc_activate_cb (Gtk.Widget widget)
     {
-        var dialog_type = widget.get_data<int> ("finc_dialog");
-        var field = widget.get_data<int> ("finc_field");
-
-        // FIXME
-        /*if (finc_dialog_fields[dialog_type, field+1] == null)
+        var next_entry = widget.get_data<Gtk.Entry> ("next-entry");
+        if (next_entry == null)
         {
             var dialog = widget.get_toplevel () as Gtk.Dialog;
             if (dialog.is_toplevel ())
@@ -890,10 +864,7 @@ public class MathButtons : Gtk.Box
             }
         }
         else
-        {
-            var next_widget = financial_ui.get_object (finc_dialog_fields[dialog_type, field+1]) as Gtk.Widget;
-            next_widget.grab_focus ();
-        }*/
+            next_entry.grab_focus ();
     }
 
     private void finc_response_cb (Gtk.Widget widget, int response_id)
@@ -901,22 +872,53 @@ public class MathButtons : Gtk.Box
         if (response_id != Gtk.ResponseType.OK)
             return;
 
-        var dialog = widget.get_data<int> ("finc_dialog");
+        var function = (FinancialDialog) widget.get_data<int> ("finc-function");
+        var entries = new string[0];
+        switch (function)
+        {
+        case FinancialDialog.CTRM_DIALOG:
+            entries = ctrm_entries;
+            break;
+        case FinancialDialog.DDB_DIALOG:
+            entries = ddb_entries;
+            break;
+        case FinancialDialog.FV_DIALOG:
+            entries = fv_entries;
+            break;
+        case FinancialDialog.GPM_DIALOG:
+            entries = gpm_entries;
+            break;
+        case FinancialDialog.PMT_DIALOG:
+            entries = pmt_entries;
+            break;
+        case FinancialDialog.PV_DIALOG:
+            entries = pv_entries;
+            break;
+        case FinancialDialog.RATE_DIALOG:
+            entries = rate_entries;
+            break;
+        case FinancialDialog.SLN_DIALOG:
+            entries = sln_entries;
+            break;
+        case FinancialDialog.SYD_DIALOG:
+            entries = syd_entries;
+            break;
+        case FinancialDialog.TERM_DIALOG:
+            entries = term_entries;
+            break;
+        }
 
-        Number arg[4];
-        // FIXME
-        /*for (var i = 0; i < 4; i++)
+        Number arg[4] = { null, null, null, null };
+        for (var i = 0; i < entries.length; i++)
         {
-            if (finc_dialog_fields[dialog, i] == null)
-                continue;
-            var entry = financial_ui.get_object (finc_dialog_fields[dialog, i]) as Gtk.Entry;
+            var entry = financial_ui.get_object (entries[i]) as Gtk.Entry;
             arg[i] = mp_set_from_string (entry.get_text ());
             entry.set_text ("0");
         }
-        var first_entry = financial_ui.get_object (finc_dialog_fields[dialog, 0]) as Gtk.Entry;
-        first_entry.grab_focus ();*/
+        var first_entry = financial_ui.get_object (entries[0]) as Gtk.Entry;
+        first_entry.grab_focus ();
 
-        do_finc_expression (equation, (FinancialDialog) dialog, arg[0], arg[1], arg[2], arg[3]);
+        do_finc_expression (equation, function, arg[0], arg[1], arg[2], arg[3]);
     }
 
     private void character_code_dialog_response_cb (Gtk.Widget dialog, int response_id)



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