Support entering super/subscript with numeric keypad (Bug #634904)
[gcalctool.git] / src / gcalctool.c
blob3c03670beb95fadea66f3e5b4c4c60ed0f33f2c0
1 /*
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
9 * license.
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <locale.h>
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;
26 static void
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);
34 static int
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);
41 static void
42 solve(const char *equation)
44 MPEquationOptions options;
45 MPErrorCode error;
46 MPNumber result;
47 char *result_str;
49 memset(&options, 0, sizeof(options));
50 options.base = 10;
51 options.wordlen = 32;
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());
58 exit(1);
60 else if(error != 0) {
61 fprintf(stderr, "Error: %s\n", mp_error_code_to_string(error));
62 exit(1);
64 else {
65 result_str = mp_serializer_to_string(mp_serializer_new(MP_DISPLAY_FORMAT_AUTOMATIC, 10, 9), &result);
66 printf("%s\n", result_str);
67 exit(0);
72 static void
73 usage(const gchar *progname, gboolean show_application, gboolean show_gtk)
75 fprintf(stderr,
76 /* Description on how to use gcalctool displayed on command-line */
77 _("Usage:\n"
78 " %s — Perform mathematical calculations"), progname);
80 fprintf(stderr,
81 "\n\n");
83 fprintf(stderr,
84 /* Description on gcalctool command-line help options displayed on command-line */
85 _("Help Options:\n"
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"));
90 fprintf(stderr,
91 "\n\n");
93 if (show_gtk) {
94 fprintf(stderr,
95 /* Description on gcalctool command-line GTK+ options displayed on command-line */
96 _("GTK+ Options:\n"
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"));
103 fprintf(stderr,
104 "\n\n");
107 if (show_application) {
108 fprintf(stderr,
109 /* Description on gcalctool application options displayed on command-line */
110 _("Application Options:\n"
111 " -s, --solve <equation> Solve the given equation"));
112 fprintf(stderr,
113 "\n\n");
118 static void
119 get_options(int argc, char *argv[])
121 int i;
122 char *progname, *arg;
124 progname = g_path_get_basename(argv[0]);
126 for (i = 1; i < argc; i++) {
127 arg = argv[i];
129 if (strcmp(arg, "-v") == 0 ||
130 strcmp(arg, "--version") == 0) {
131 version(progname);
132 exit(0);
134 else if (strcmp(arg, "-h") == 0 ||
135 strcmp(arg, "-?") == 0 ||
136 strcmp(arg, "--help") == 0) {
137 usage(progname, TRUE, FALSE);
138 exit(0);
140 else if (strcmp(arg, "--help-all") == 0) {
141 usage(progname, TRUE, TRUE);
142 exit(0);
144 else if (strcmp(arg, "--help-gtk") == 0) {
145 usage(progname, FALSE, TRUE);
146 exit(0);
148 else if (strcmp(arg, "-s") == 0 ||
149 strcmp(arg, "--solve") == 0) {
150 i++;
151 if (i >= argc) {
152 fprintf(stderr,
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");
156 exit(1);
158 else
159 solve(argv[i]);
161 else {
162 fprintf(stderr,
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);
167 exit(1);
173 static void
174 quit_cb(MathWindow *window)
176 MathEquation *equation;
177 MathButtons *buttons;
179 equation = math_window_get_equation(window);
180 buttons = math_window_get_buttons(window);
182 g_settings_set_int(settings, "accuracy", math_equation_get_accuracy(equation));
183 g_settings_set_int(settings, "word-size", math_equation_get_word_size(equation));
184 g_settings_set_int(settings, "base", math_buttons_get_programming_base(buttons));
185 g_settings_set_boolean(settings, "show-thousands", math_equation_get_show_thousands_separators(equation));
186 g_settings_set_boolean(settings, "show-zeroes", math_equation_get_show_trailing_zeroes(equation));
187 g_settings_set_enum(settings, "number-format", math_equation_get_number_format(equation));
188 g_settings_set_enum(settings, "angle-units", math_equation_get_angle_units(equation));
189 g_settings_set_enum(settings, "button-mode", math_buttons_get_mode(buttons));
190 g_settings_set_string(settings, "source-currency", math_equation_get_source_currency(equation));
191 g_settings_set_string(settings, "target-currency", math_equation_get_target_currency(equation));
192 g_settings_set_string(settings, "source-units", math_equation_get_source_units(equation));
193 g_settings_set_string(settings, "target-units", math_equation_get_target_units(equation));
194 g_settings_sync();
196 gtk_main_quit();
201 main(int argc, char **argv)
203 MathEquation *equation;
204 int accuracy = 9, word_size = 64, base = 10;
205 gboolean show_tsep = FALSE, show_zeroes = FALSE;
206 MpDisplayFormat number_format;
207 MPAngleUnit angle_units;
208 ButtonMode button_mode;
209 gchar *source_currency, *target_currency;
210 gchar *source_units, *target_units;
212 g_type_init();
214 setlocale(LC_ALL, "");
215 bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
216 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
217 textdomain(GETTEXT_PACKAGE);
219 /* Seed random number generator. */
220 srand48((long) time((time_t *) 0));
222 get_options(argc, argv);
224 settings = g_settings_new ("org.gnome.gcalctool");
225 accuracy = g_settings_get_int(settings, "accuracy");
226 word_size = g_settings_get_int(settings, "word-size");
227 base = g_settings_get_int(settings, "base");
228 show_tsep = g_settings_get_boolean(settings, "show-thousands");
229 show_zeroes = g_settings_get_boolean(settings, "show-zeroes");
230 number_format = g_settings_get_enum(settings, "number-format");
231 angle_units = g_settings_get_enum(settings, "angle-units");
232 button_mode = g_settings_get_enum(settings, "button-mode");
233 source_currency = g_settings_get_string(settings, "source-currency");
234 target_currency = g_settings_get_string(settings, "target-currency");
235 source_units = g_settings_get_string(settings, "source-units");
236 target_units = g_settings_get_string(settings, "target-units");
238 equation = math_equation_new();
239 math_equation_set_accuracy(equation, accuracy);
240 math_equation_set_word_size(equation, word_size);
241 math_equation_set_show_thousands_separators(equation, show_tsep);
242 math_equation_set_show_trailing_zeroes(equation, show_zeroes);
243 math_equation_set_number_format(equation, number_format);
244 math_equation_set_angle_units(equation, angle_units);
245 math_equation_set_source_currency(equation, source_currency);
246 math_equation_set_target_currency(equation, target_currency);
247 math_equation_set_source_units(equation, source_units);
248 math_equation_set_target_units(equation, target_units);
249 g_free(source_currency);
250 g_free(target_currency);
251 g_free(source_units);
252 g_free(target_units);
254 gtk_init(&argc, &argv);
256 window = math_window_new(equation);
257 g_signal_connect(G_OBJECT(window), "quit", G_CALLBACK(quit_cb), NULL);
258 math_buttons_set_programming_base(math_window_get_buttons(window), base);
259 math_buttons_set_mode(math_window_get_buttons(window), button_mode); // FIXME: We load the basic buttons even if we immediately switch to the next type
261 gtk_widget_show(GTK_WIDGET(window));
262 gtk_main();
264 return(0);