2 * Copyright (C) 1987-2008 Sun Microsystems, Inc. All Rights Reserved.
3 * Copyright (C) 2008-2011 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
16 #include <glib/gi18n.h>
18 #include "math-window.h"
19 #include "mp-equation.h"
20 #include "unit-manager.h"
22 static GSettings
*settings
= NULL
;
24 static MathWindow
*window
;
27 version(const gchar
*progname
)
29 /* NOTE: Is not translated so can be easily parsed */
30 fprintf(stderr
, "%1$s %2$s\n", progname
, VERSION
);
35 do_convert(const MPNumber
*x
, const char *x_units
, const char *z_units
, MPNumber
*z
, void *data
)
37 return unit_manager_convert_by_symbol(unit_manager_get_default(), x
, x_units
, z_units
, z
);
42 solve(const char *equation
)
44 MPEquationOptions options
;
49 memset(&options
, 0, sizeof(options
));
52 options
.angle_units
= MP_DEGREES
;
53 options
.convert
= do_convert
;
55 error
= mp_equation_parse(equation
, &options
, &result
, NULL
);
56 if(error
== PARSER_ERR_MP
) {
57 fprintf(stderr
, "Error: %s\n", mp_get_error());
61 fprintf(stderr
, "Error: %s\n", mp_error_code_to_string(error
));
65 result_str
= mp_serializer_to_string(mp_serializer_new(MP_DISPLAY_FORMAT_AUTOMATIC
, 10, 9), &result
);
66 printf("%s\n", result_str
);
73 usage(const gchar
*progname
, gboolean show_application
, gboolean show_gtk
)
76 /* Description on how to use gcalctool displayed on command-line */
78 " %s — Perform mathematical calculations"), progname
);
84 /* Description on gcalctool command-line help options displayed on command-line */
86 " -v, --version Show release version\n"
87 " -h, -?, --help Show help options\n"
88 " --help-all Show all help options\n"
89 " --help-gtk Show GTK+ options"));
95 /* Description on gcalctool command-line GTK+ options displayed on command-line */
97 " --class=CLASS Program class as used by the window manager\n"
98 " --name=NAME Program name as used by the window manager\n"
99 " --screen=SCREEN X screen to use\n"
100 " --sync Make X calls synchronous\n"
101 " --gtk-module=MODULES Load additional GTK+ modules\n"
102 " --g-fatal-warnings Make all warnings fatal"));
107 if (show_application
) {
109 /* Description on gcalctool application options displayed on command-line */
110 _("Application Options:\n"
111 " -s, --solve <equation> Solve the given equation"));
119 get_options(int argc
, char *argv
[])
122 char *progname
, *arg
;
124 progname
= g_path_get_basename(argv
[0]);
126 for (i
= 1; i
< argc
; i
++) {
129 if (strcmp(arg
, "-v") == 0 ||
130 strcmp(arg
, "--version") == 0) {
134 else if (strcmp(arg
, "-h") == 0 ||
135 strcmp(arg
, "-?") == 0 ||
136 strcmp(arg
, "--help") == 0) {
137 usage(progname
, TRUE
, FALSE
);
140 else if (strcmp(arg
, "--help-all") == 0) {
141 usage(progname
, TRUE
, TRUE
);
144 else if (strcmp(arg
, "--help-gtk") == 0) {
145 usage(progname
, FALSE
, TRUE
);
148 else if (strcmp(arg
, "-s") == 0 ||
149 strcmp(arg
, "--solve") == 0) {
153 /* Error printed to stderr when user uses --solve argument without an equation */
154 _("Argument --solve requires an equation to solve"));
155 fprintf(stderr
, "\n");
163 /* Error printed to stderr when user provides an unknown command-line argument */
164 _("Unknown argument '%s'"), arg
);
165 fprintf(stderr
, "\n");
166 usage(progname
, TRUE
, FALSE
);
174 accuracy_cb(MathEquation
*equation
, GParamSpec
*spec
)
176 g_settings_set_int(settings
, "accuracy", math_equation_get_accuracy(equation
));
181 word_size_cb(MathEquation
*equation
, GParamSpec
*spec
)
183 g_settings_set_int(settings
, "word-size", math_equation_get_word_size(equation
));
188 show_thousands_separators_cb(MathEquation
*equation
, GParamSpec
*spec
)
190 g_settings_set_boolean(settings
, "show-thousands", math_equation_get_show_thousands_separators(equation
));
195 show_trailing_zeroes_cb(MathEquation
*equation
, GParamSpec
*spec
)
197 g_settings_set_boolean(settings
, "show-zeroes", math_equation_get_show_trailing_zeroes(equation
));
202 number_format_cb(MathEquation
*equation
, GParamSpec
*spec
)
204 g_settings_set_enum(settings
, "number-format", math_equation_get_number_format(equation
));
209 angle_unit_cb(MathEquation
*equation
, GParamSpec
*spec
)
211 g_settings_set_enum(settings
, "angle-units", math_equation_get_angle_units(equation
));
216 source_currency_cb(MathEquation
*equation
, GParamSpec
*spec
)
218 g_settings_set_string(settings
, "source-currency", math_equation_get_source_currency(equation
));
223 target_currency_cb(MathEquation
*equation
, GParamSpec
*spec
)
225 g_settings_set_string(settings
, "target-currency", math_equation_get_target_currency(equation
));
230 source_units_cb(MathEquation
*equation
, GParamSpec
*spec
)
232 g_settings_set_string(settings
, "source-units", math_equation_get_source_units(equation
));
237 target_units_cb(MathEquation
*equation
, GParamSpec
*spec
)
239 g_settings_set_string(settings
, "target-units", math_equation_get_target_units(equation
));
244 programming_base_cb(MathButtons
*buttons
, GParamSpec
*spec
)
246 g_settings_set_int(settings
, "base", math_buttons_get_programming_base(buttons
));
251 mode_cb(MathButtons
*buttons
, GParamSpec
*spec
)
253 g_settings_set_enum(settings
, "button-mode", math_buttons_get_mode(buttons
));
258 quit_cb(MathWindow
*window
)
265 main(int argc
, char **argv
)
267 MathEquation
*equation
;
268 MathButtons
*buttons
;
269 int accuracy
= 9, word_size
= 64, base
= 10;
270 gboolean show_tsep
= FALSE
, show_zeroes
= FALSE
;
271 MpDisplayFormat number_format
;
272 MPAngleUnit angle_units
;
273 ButtonMode button_mode
;
274 gchar
*source_currency
, *target_currency
;
275 gchar
*source_units
, *target_units
;
279 setlocale(LC_ALL
, "");
280 bindtextdomain(GETTEXT_PACKAGE
, LOCALE_DIR
);
281 bind_textdomain_codeset(GETTEXT_PACKAGE
, "UTF-8");
282 textdomain(GETTEXT_PACKAGE
);
284 /* Seed random number generator. */
285 srand48((long) time((time_t *) 0));
287 get_options(argc
, argv
);
289 settings
= g_settings_new ("org.gnome.gcalctool");
290 accuracy
= g_settings_get_int(settings
, "accuracy");
291 word_size
= g_settings_get_int(settings
, "word-size");
292 base
= g_settings_get_int(settings
, "base");
293 show_tsep
= g_settings_get_boolean(settings
, "show-thousands");
294 show_zeroes
= g_settings_get_boolean(settings
, "show-zeroes");
295 number_format
= g_settings_get_enum(settings
, "number-format");
296 angle_units
= g_settings_get_enum(settings
, "angle-units");
297 button_mode
= g_settings_get_enum(settings
, "button-mode");
298 source_currency
= g_settings_get_string(settings
, "source-currency");
299 target_currency
= g_settings_get_string(settings
, "target-currency");
300 source_units
= g_settings_get_string(settings
, "source-units");
301 target_units
= g_settings_get_string(settings
, "target-units");
303 equation
= math_equation_new();
304 math_equation_set_accuracy(equation
, accuracy
);
305 math_equation_set_word_size(equation
, word_size
);
306 math_equation_set_show_thousands_separators(equation
, show_tsep
);
307 math_equation_set_show_trailing_zeroes(equation
, show_zeroes
);
308 math_equation_set_number_format(equation
, number_format
);
309 math_equation_set_angle_units(equation
, angle_units
);
310 math_equation_set_source_currency(equation
, source_currency
);
311 math_equation_set_target_currency(equation
, target_currency
);
312 math_equation_set_source_units(equation
, source_units
);
313 math_equation_set_target_units(equation
, target_units
);
314 g_free(source_currency
);
315 g_free(target_currency
);
316 g_free(source_units
);
317 g_free(target_units
);
319 g_signal_connect(equation
, "notify::accuracy", G_CALLBACK(accuracy_cb
), NULL
);
320 g_signal_connect(equation
, "notify::word-size", G_CALLBACK(word_size_cb
), NULL
);
321 g_signal_connect(equation
, "notify::show-thousands-separators", G_CALLBACK(show_thousands_separators_cb
), NULL
);
322 g_signal_connect(equation
, "notify::show-trailing-zeroes", G_CALLBACK(show_trailing_zeroes_cb
), NULL
);
323 g_signal_connect(equation
, "notify::number-format", G_CALLBACK(number_format_cb
), NULL
);
324 g_signal_connect(equation
, "notify::angle-units", G_CALLBACK(angle_unit_cb
), NULL
);
325 g_signal_connect(equation
, "notify::source-currency", G_CALLBACK(source_currency_cb
), NULL
);
326 g_signal_connect(equation
, "notify::target-currency", G_CALLBACK(target_currency_cb
), NULL
);
327 g_signal_connect(equation
, "notify::source-units", G_CALLBACK(source_units_cb
), NULL
);
328 g_signal_connect(equation
, "notify::target-units", G_CALLBACK(target_units_cb
), NULL
);
330 gtk_init(&argc
, &argv
);
332 window
= math_window_new(equation
);
333 buttons
= math_window_get_buttons(window
);
334 g_signal_connect(G_OBJECT(window
), "quit", G_CALLBACK(quit_cb
), NULL
);
335 math_buttons_set_programming_base(buttons
, base
);
336 math_buttons_set_mode(buttons
, button_mode
); // FIXME: We load the basic buttons even if we immediately switch to the next type
337 g_signal_connect(buttons
, "notify::programming-base", G_CALLBACK(programming_base_cb
), NULL
);
338 g_signal_connect(buttons
, "notify::mode", G_CALLBACK(mode_cb
), NULL
);
340 gtk_widget_show(GTK_WIDGET(window
));