2 * Copyright (C) 1987-2008 Sun Microsystems, Inc. All Rights Reserved.
3 * Copyright (C) 2008-2012 Robert Ancell
5 * This program is free software: you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation, either version 2 of the License, or (at your option) any later
8 * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
12 public class GCalctool
: Gtk
.Application
14 private Settings settings
;
15 private MathWindow window
;
16 private MathPreferencesDialog preferences_dialog
;
18 private const ActionEntry
[] app_entries
=
20 { "copy", copy_cb
, null, null, null },
21 { "paste", paste_cb
, null, null, null },
22 { "undo", undo_cb
, null, null, null },
23 { "redo", redo_cb
, null, null, null },
24 { "mode", mode_changed_cb
, "s", "\"basic\"", null },
25 { "preferences", show_preferences_cb
, null, null, null },
26 { "help", help_cb
, null, null, null },
27 { "about", about_cb
, null, null, null },
28 { "quit", quit_cb
, null, null, null },
33 Object (flags
: ApplicationFlags
.NON_UNIQUE
);
36 protected override void startup ()
40 settings
= new
Settings ("org.gnome.gcalctool");
41 var accuracy
= settings
.get_int ("accuracy");
42 var word_size
= settings
.get_int ("word-size");
43 var number_base
= settings
.get_int ("base");
44 var show_tsep
= settings
.get_boolean ("show-thousands");
45 var show_zeroes
= settings
.get_boolean ("show-zeroes");
46 var number_format
= (DisplayFormat
) settings
.get_enum ("number-format");
47 var angle_units
= (AngleUnit
) settings
.get_enum ("angle-units");
48 var button_mode
= (ButtonMode
) settings
.get_enum ("button-mode");
49 var source_currency
= settings
.get_string ("source-currency");
50 var target_currency
= settings
.get_string ("target-currency");
51 var source_units
= settings
.get_string ("source-units");
52 var target_units
= settings
.get_string ("target-units");
54 var equation
= new
MathEquation ();
55 equation
.accuracy
= accuracy
;
56 equation
.word_size
= word_size
;
57 equation
.show_thousands_separators
= show_tsep
;
58 equation
.show_trailing_zeroes
= show_zeroes
;
59 equation
.number_format
= number_format
;
60 equation
.angle_units
= angle_units
;
61 equation
.source_currency
= source_currency
;
62 equation
.target_currency
= target_currency
;
63 equation
.source_units
= source_units
;
64 equation
.target_units
= target_units
;
66 add_action_entries (app_entries
, this
);
68 window
= new
MathWindow (this
, equation
);
69 var buttons
= window
.buttons
;
70 buttons
.programming_base
= number_base
;
71 buttons
.mode
= button_mode
; // FIXME: We load the basic buttons even if we immediately switch to the next type
72 buttons
.notify
["mode"].connect ((pspec
) => { mode_cb (); });
75 var menu
= new
Menu ();
77 var section
= new
Menu ();
78 section
.append (_("Basic"), "app.mode::basic");
79 section
.append (_("Advanced"), "app.mode::advanced");
80 section
.append (_("Financial"), "app.mode::financial");
81 section
.append (_("Programming"), "app.mode::programming");
82 menu
.append_section (_("Mode"), section
);
84 section
= new
Menu ();
85 section
.append (_("Preferences"), "app.preferences");
86 menu
.append_section (null, section
);
88 section
= new
Menu ();
89 section
.append (_("About Calculator"), "app.about");
90 section
.append (_("Help"), "app.help");
91 section
.append (_("Quit"), "app.quit");
92 menu
.append_section (null, section
);
96 add_accelerator ("<control>Q", "app.quit", null);
97 add_accelerator ("F1", "app.help", null);
98 add_accelerator ("<control>C", "app.copy", null);
99 add_accelerator ("<control>V", "app.paste", null);
100 add_accelerator ("<control>Z", "app.undo", null);
101 add_accelerator ("<control><shift>Z", "app.redo", null);
104 protected override void activate ()
111 protected override void shutdown ()
115 var equation
= window
.equation
;
116 var buttons
= window
.buttons
;
118 settings
.set_enum ("button-mode", buttons
.mode
);
119 settings
.set_int ("accuracy", equation
.accuracy
);
120 settings
.set_int ("word-size", equation
.word_size
);
121 settings
.set_boolean ("show-thousands", equation
.show_thousands_separators
);
122 settings
.set_boolean ("show-zeroes", equation
.show_trailing_zeroes
);
123 settings
.set_enum ("number-format", equation
.number_format
);
124 settings
.set_enum ("angle-units", equation
.angle_units
);
125 settings
.set_string ("source-currency", equation
.source_currency
);
126 settings
.set_string ("target-currency", equation
.target_currency
);
127 settings
.set_string ("source-units", equation
.source_units
);
128 settings
.set_string ("target-units", equation
.target_units
);
129 settings
.set_int ("base", buttons
.programming_base
);
132 private static void solve (string equation
)
134 var e
= new
SolveEquation (equation
);
137 e
.angle_units
= AngleUnit
.DEGREES
;
140 var result
= e
.parse (out error
);
143 var serializer
= new
Serializer (DisplayFormat
.AUTOMATIC
, 10, 9);
144 stdout
.printf ("%s\n", serializer
.to_string (result
));
147 else if (error
== ErrorCode
.MP
)
149 stderr
.printf ("Error: %s\n", mp_get_error ());
150 Posix
.exit (Posix
.EXIT_FAILURE
);
154 stderr
.printf ("Error: %s\n", mp_error_code_to_string (error
));
155 Posix
.exit (Posix
.EXIT_FAILURE
);
159 private static void usage (string progname
, bool show_application
, bool show_gtk
)
161 stderr
.printf (/* Description on how to use gcalctool displayed on command-line */
162 _("Usage:\n %s — Perform mathematical calculations"), progname
);
164 stderr
.printf ("\n\n");
166 stderr
.printf (/* Description on gcalctool command-line help options displayed on command-line */
167 _("Help Options:\n -v, --version Show release version\n -h, -?, --help Show help options\n --help-all Show all help options\n --help-gtk Show GTK+ options"));
168 stderr
.printf ("\n\n");
172 stderr
.printf (/* Description on gcalctool command-line GTK+ options displayed on command-line */
173 _("GTK+ Options:\n --class=CLASS Program class as used by the window manager\n --name=NAME Program name as used by the window manager\n --screen=SCREEN X screen to use\n --sync Make X calls synchronous\n --gtk-module=MODULES Load additional GTK+ modules\n --g-fatal-warnings Make all warnings fatal"));
174 stderr
.printf ("\n\n");
177 if (show_application
)
179 stderr
.printf (/* Description on gcalctool application options displayed on command-line */
180 _("Application Options:\n -s, --solve <equation> Solve the given equation"));
181 stderr
.printf ("\n\n");
185 private static void get_options (string[] args
)
187 var progname
= Path
.get_basename (args
[0]);
189 for (var i
= 1; i
< args
.length
; i
++)
193 if (arg
== "-v" || arg
== "--version")
195 /* NOTE: Is not translated so can be easily parsed */
196 stderr
.printf ("%1$s %2$s\n", progname
, VERSION
);
197 Posix
.exit (Posix
.EXIT_SUCCESS
);
199 else if (arg
== "-h" || arg
== "-?" || arg
== "--help")
201 usage (progname
, true, false);
202 Posix
.exit (Posix
.EXIT_SUCCESS
);
204 else if (arg
== "--help-all")
206 usage (progname
, true, true);
207 Posix
.exit (Posix
.EXIT_SUCCESS
);
209 else if (arg
== "--help-gtk")
211 usage (progname
, false, true);
212 Posix
.exit (Posix
.EXIT_SUCCESS
);
214 else if (arg
== "-s" || arg
== "--solve")
217 if (i
>= args
.length
)
219 stderr
.printf (/* Error printed to stderr when user uses --solve argument without an equation */
220 _("Argument --solve requires an equation to solve"));
221 stderr
.printf ("\n");
222 Posix
.exit (Posix
.EXIT_FAILURE
);
229 stderr
.printf (/* Error printed to stderr when user provides an unknown command-line argument */
230 _("Unknown argument '%s'"), arg
);
231 stderr
.printf ("\n");
232 usage (progname
, true, false);
233 Posix
.exit (Posix
.EXIT_FAILURE
);
238 private void mode_cb ()
240 var buttons
= window
.buttons
;
242 switch (buttons
.mode
)
245 case ButtonMode
.BASIC
:
247 //FIXME: Should it revert to decimal mode? equation.number_format = NumberFormat.DECIMAL;
250 case ButtonMode
.ADVANCED
:
254 case ButtonMode
.FINANCIAL
:
258 case ButtonMode
.PROGRAMMING
:
259 state
= "programming";
263 var action
= lookup_action ("mode") as SimpleAction
;
264 action
.set_state (new Variant
.string (state
));
267 private void copy_cb ()
269 window
.equation
.copy ();
272 private void paste_cb ()
274 window
.equation
.paste ();
277 private void undo_cb ()
279 window
.equation
.undo ();
282 private void redo_cb ()
284 window
.equation
.redo ();
287 private void mode_changed_cb (SimpleAction action
, Variant? parameter
)
289 var mode
= ButtonMode
.BASIC
;
290 var mode_str
= parameter
.get_string (null);
291 if (mode_str
== "basic")
292 mode
= ButtonMode
.BASIC
;
293 else if (mode_str
== "advanced")
294 mode
= ButtonMode
.ADVANCED
;
295 else if (mode_str
== "financial")
296 mode
= ButtonMode
.FINANCIAL
;
297 else if (mode_str
== "programming")
298 mode
= ButtonMode
.PROGRAMMING
;
299 window
.buttons
.mode
= mode
;
302 private void show_preferences_cb ()
304 if (preferences_dialog
== null)
306 preferences_dialog
= new
MathPreferencesDialog (window
.equation
);
307 preferences_dialog
.set_transient_for (window
);
309 preferences_dialog
.present ();
312 private void help_cb ()
316 Gtk
.show_uri (window
.get_screen (), "help:gcalctool", Gtk
.get_current_event_time ());
320 /* Translators: Error message displayed when unable to launch help browser */
321 var message
= _("Unable to open help file");
323 var d
= new Gtk
.MessageDialog (window
,
324 Gtk
.DialogFlags
.MODAL
| Gtk
.DialogFlags
.DESTROY_WITH_PARENT
,
325 Gtk
.MessageType
.ERROR
,
326 Gtk
.ButtonsType
.CLOSE
,
328 d
.format_secondary_text ("%s", e
.message
);
334 private void about_cb ()
338 "Rich Burridge <rich.burridge@gmail.com>",
339 "Robert Ancell <robert.ancell@gmail.com>",
340 "Klaus Niederkrüger <kniederk@umpa.ens-lyon.fr>",
341 "Robin Sonefors <ozamosi@flukkost.nu>",
344 string[] documenters
=
350 /* The translator credits. Please translate this with your name (s). */
351 var translator_credits
= _("translator-credits");
353 /* The license this software is under (GPL2+) */
354 var license
= _("Gcalctool is free software; you can redistribute it and/or modify\nit under the terms of the GNU General Public License as published by\nthe Free Software Foundation; either version 2 of the License, or\n(at your option) any later version.\n\nGcalctool is distributed in the hope that it will be useful,\nbut WITHOUT ANY WARRANTY; without even the implied warranty of\nMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\nGNU General Public License for more details.\n\nYou should have received a copy of the GNU General Public License\nalong with Gcalctool; if not, write to the Free Software Foundation, Inc.,\n51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA");
356 Gtk
.show_about_dialog (window
,
358 /* Program name in the about dialog */
362 /* Copyright notice in the about dialog */
363 _("\xc2\xa9 1986–2010 The Gcalctool authors"),
366 /* Short description in the about dialog */
367 _("Calculator with financial and scientific modes."),
369 "documenters", documenters
,
370 "translator_credits", translator_credits
,
371 "logo-icon-name", "accessories-calculator");
374 private void quit_cb ()
379 public static int main (string[] args
)
381 Intl
.setlocale (LocaleCategory
.ALL
, "");
382 Intl
.bindtextdomain (GETTEXT_PACKAGE
, LOCALE_DIR
);
383 Intl
.bind_textdomain_codeset (GETTEXT_PACKAGE
, "UTF-8");
384 Intl
.textdomain (GETTEXT_PACKAGE
);
386 /* Seed random number generator. */
387 var now
= new DateTime
.now_utc ();
388 Random
.set_seed (now
.get_microsecond ());
394 Gtk
.Window
.set_default_icon_name ("accessories-calculator");
396 var app
= new
GCalctool ();
398 return app
.run (args
);
402 private class SolveEquation
: Equation
404 public SolveEquation (string text
)
409 public override Number?
convert (Number x
, string x_units
, string z_units
)
411 return UnitManager
.get_default ().convert_by_symbol (x
, x_units
, z_units
);