PageRenderTime 28ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/gcalctool-6.5.3/src/gcalctool.c

#
C | 358 lines | 274 code | 66 blank | 18 comment | 29 complexity | 50d0d2e497f0e7a3be105e4f4d9065a4 MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. * Copyright (C) 1987-2008 Sun Microsystems, Inc. All Rights Reserved.
  3. * Copyright (C) 2008-2011 Robert Ancell
  4. *
  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.
  10. */
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <locale.h>
  15. #include <glib/gi18n.h>
  16. #include "math-window.h"
  17. #include "mp-equation.h"
  18. #include "unit-manager.h"
  19. static GSettings *settings = NULL;
  20. static MathWindow *window;
  21. static void
  22. version(const gchar *progname)
  23. {
  24. /* NOTE: Is not translated so can be easily parsed */
  25. fprintf(stderr, "%1$s %2$s\n", progname, VERSION);
  26. }
  27. static int
  28. do_convert(const MPNumber *x, const char *x_units, const char *z_units, MPNumber *z, void *data)
  29. {
  30. return unit_manager_convert_by_symbol(unit_manager_get_default(), x, x_units, z_units, z);
  31. }
  32. static void
  33. solve(const char *equation)
  34. {
  35. MPEquationOptions options;
  36. MPErrorCode error;
  37. MPNumber result;
  38. char *result_str;
  39. memset(&options, 0, sizeof(options));
  40. options.base = 10;
  41. options.wordlen = 32;
  42. options.angle_units = MP_DEGREES;
  43. options.convert = do_convert;
  44. error = mp_equation_parse(equation, &options, &result, NULL);
  45. if(error == PARSER_ERR_MP) {
  46. fprintf(stderr, "Error: %s\n", mp_get_error());
  47. exit(1);
  48. }
  49. else if(error != 0) {
  50. fprintf(stderr, "Error: %s\n", mp_error_code_to_string(error));
  51. exit(1);
  52. }
  53. else {
  54. result_str = mp_serializer_to_string(mp_serializer_new(MP_DISPLAY_FORMAT_AUTOMATIC, 10, 9), &result);
  55. printf("%s\n", result_str);
  56. exit(0);
  57. }
  58. }
  59. static void
  60. usage(const gchar *progname, gboolean show_application, gboolean show_gtk)
  61. {
  62. fprintf(stderr,
  63. /* Description on how to use gcalctool displayed on command-line */
  64. _("Usage:\n"
  65. " %s รข&#x20AC;&#x201D; Perform mathematical calculations"), progname);
  66. fprintf(stderr,
  67. "\n\n");
  68. fprintf(stderr,
  69. /* Description on gcalctool command-line help options displayed on command-line */
  70. _("Help Options:\n"
  71. " -v, --version Show release version\n"
  72. " -h, -?, --help Show help options\n"
  73. " --help-all Show all help options\n"
  74. " --help-gtk Show GTK+ options"));
  75. fprintf(stderr,
  76. "\n\n");
  77. if (show_gtk) {
  78. fprintf(stderr,
  79. /* Description on gcalctool command-line GTK+ options displayed on command-line */
  80. _("GTK+ Options:\n"
  81. " --class=CLASS Program class as used by the window manager\n"
  82. " --name=NAME Program name as used by the window manager\n"
  83. " --screen=SCREEN X screen to use\n"
  84. " --sync Make X calls synchronous\n"
  85. " --gtk-module=MODULES Load additional GTK+ modules\n"
  86. " --g-fatal-warnings Make all warnings fatal"));
  87. fprintf(stderr,
  88. "\n\n");
  89. }
  90. if (show_application) {
  91. fprintf(stderr,
  92. /* Description on gcalctool application options displayed on command-line */
  93. _("Application Options:\n"
  94. " -s, --solve <equation> Solve the given equation"));
  95. fprintf(stderr,
  96. "\n\n");
  97. }
  98. }
  99. static void
  100. get_options(int argc, char *argv[])
  101. {
  102. int i;
  103. char *progname, *arg;
  104. progname = g_path_get_basename(argv[0]);
  105. for (i = 1; i < argc; i++) {
  106. arg = argv[i];
  107. if (strcmp(arg, "-v") == 0 ||
  108. strcmp(arg, "--version") == 0) {
  109. version(progname);
  110. exit(0);
  111. }
  112. else if (strcmp(arg, "-h") == 0 ||
  113. strcmp(arg, "-?") == 0 ||
  114. strcmp(arg, "--help") == 0) {
  115. usage(progname, TRUE, FALSE);
  116. exit(0);
  117. }
  118. else if (strcmp(arg, "--help-all") == 0) {
  119. usage(progname, TRUE, TRUE);
  120. exit(0);
  121. }
  122. else if (strcmp(arg, "--help-gtk") == 0) {
  123. usage(progname, FALSE, TRUE);
  124. exit(0);
  125. }
  126. else if (strcmp(arg, "-s") == 0 ||
  127. strcmp(arg, "--solve") == 0) {
  128. i++;
  129. if (i >= argc) {
  130. fprintf(stderr,
  131. /* Error printed to stderr when user uses --solve argument without an equation */
  132. _("Argument --solve requires an equation to solve"));
  133. fprintf(stderr, "\n");
  134. exit(1);
  135. }
  136. else
  137. solve(argv[i]);
  138. }
  139. else {
  140. fprintf(stderr,
  141. /* Error printed to stderr when user provides an unknown command-line argument */
  142. _("Unknown argument '%s'"), arg);
  143. fprintf(stderr, "\n");
  144. usage(progname, TRUE, FALSE);
  145. exit(1);
  146. }
  147. }
  148. }
  149. static void
  150. accuracy_cb(MathEquation *equation, GParamSpec *spec)
  151. {
  152. g_settings_set_int(settings, "accuracy", math_equation_get_accuracy(equation));
  153. }
  154. static void
  155. word_size_cb(MathEquation *equation, GParamSpec *spec)
  156. {
  157. g_settings_set_int(settings, "word-size", math_equation_get_word_size(equation));
  158. }
  159. static void
  160. show_thousands_separators_cb(MathEquation *equation, GParamSpec *spec)
  161. {
  162. g_settings_set_boolean(settings, "show-thousands", math_equation_get_show_thousands_separators(equation));
  163. }
  164. static void
  165. show_trailing_zeroes_cb(MathEquation *equation, GParamSpec *spec)
  166. {
  167. g_settings_set_boolean(settings, "show-zeroes", math_equation_get_show_trailing_zeroes(equation));
  168. }
  169. static void
  170. number_format_cb(MathEquation *equation, GParamSpec *spec)
  171. {
  172. g_settings_set_enum(settings, "number-format", math_equation_get_number_format(equation));
  173. }
  174. static void
  175. angle_unit_cb(MathEquation *equation, GParamSpec *spec)
  176. {
  177. g_settings_set_enum(settings, "angle-units", math_equation_get_angle_units(equation));
  178. }
  179. static void
  180. source_currency_cb(MathEquation *equation, GParamSpec *spec)
  181. {
  182. g_settings_set_string(settings, "source-currency", math_equation_get_source_currency(equation));
  183. }
  184. static void
  185. target_currency_cb(MathEquation *equation, GParamSpec *spec)
  186. {
  187. g_settings_set_string(settings, "target-currency", math_equation_get_target_currency(equation));
  188. }
  189. static void
  190. source_units_cb(MathEquation *equation, GParamSpec *spec)
  191. {
  192. g_settings_set_string(settings, "source-units", math_equation_get_source_units(equation));
  193. }
  194. static void
  195. target_units_cb(MathEquation *equation, GParamSpec *spec)
  196. {
  197. g_settings_set_string(settings, "target-units", math_equation_get_target_units(equation));
  198. }
  199. static void
  200. programming_base_cb(MathButtons *buttons, GParamSpec *spec)
  201. {
  202. g_settings_set_int(settings, "base", math_buttons_get_programming_base(buttons));
  203. }
  204. static void
  205. mode_cb(MathButtons *buttons, GParamSpec *spec)
  206. {
  207. g_settings_set_enum(settings, "button-mode", math_buttons_get_mode(buttons));
  208. }
  209. static void
  210. startup_cb(GApplication *application)
  211. {
  212. MathEquation *equation;
  213. MathButtons *buttons;
  214. int accuracy = 9, word_size = 64, base = 10;
  215. gboolean show_tsep = FALSE, show_zeroes = FALSE;
  216. MpDisplayFormat number_format;
  217. MPAngleUnit angle_units;
  218. ButtonMode button_mode;
  219. gchar *source_currency, *target_currency;
  220. gchar *source_units, *target_units;
  221. settings = g_settings_new ("org.gnome.gcalctool");
  222. accuracy = g_settings_get_int(settings, "accuracy");
  223. word_size = g_settings_get_int(settings, "word-size");
  224. base = g_settings_get_int(settings, "base");
  225. show_tsep = g_settings_get_boolean(settings, "show-thousands");
  226. show_zeroes = g_settings_get_boolean(settings, "show-zeroes");
  227. number_format = g_settings_get_enum(settings, "number-format");
  228. angle_units = g_settings_get_enum(settings, "angle-units");
  229. button_mode = g_settings_get_enum(settings, "button-mode");
  230. source_currency = g_settings_get_string(settings, "source-currency");
  231. target_currency = g_settings_get_string(settings, "target-currency");
  232. source_units = g_settings_get_string(settings, "source-units");
  233. target_units = g_settings_get_string(settings, "target-units");
  234. equation = math_equation_new();
  235. math_equation_set_accuracy(equation, accuracy);
  236. math_equation_set_word_size(equation, word_size);
  237. math_equation_set_show_thousands_separators(equation, show_tsep);
  238. math_equation_set_show_trailing_zeroes(equation, show_zeroes);
  239. math_equation_set_number_format(equation, number_format);
  240. math_equation_set_angle_units(equation, angle_units);
  241. math_equation_set_source_currency(equation, source_currency);
  242. math_equation_set_target_currency(equation, target_currency);
  243. math_equation_set_source_units(equation, source_units);
  244. math_equation_set_target_units(equation, target_units);
  245. g_free(source_currency);
  246. g_free(target_currency);
  247. g_free(source_units);
  248. g_free(target_units);
  249. g_signal_connect(equation, "notify::accuracy", G_CALLBACK(accuracy_cb), NULL);
  250. g_signal_connect(equation, "notify::word-size", G_CALLBACK(word_size_cb), NULL);
  251. g_signal_connect(equation, "notify::show-thousands-separators", G_CALLBACK(show_thousands_separators_cb), NULL);
  252. g_signal_connect(equation, "notify::show-trailing-zeroes", G_CALLBACK(show_trailing_zeroes_cb), NULL);
  253. g_signal_connect(equation, "notify::number-format", G_CALLBACK(number_format_cb), NULL);
  254. g_signal_connect(equation, "notify::angle-units", G_CALLBACK(angle_unit_cb), NULL);
  255. g_signal_connect(equation, "notify::source-currency", G_CALLBACK(source_currency_cb), NULL);
  256. g_signal_connect(equation, "notify::target-currency", G_CALLBACK(target_currency_cb), NULL);
  257. g_signal_connect(equation, "notify::source-units", G_CALLBACK(source_units_cb), NULL);
  258. g_signal_connect(equation, "notify::target-units", G_CALLBACK(target_units_cb), NULL);
  259. window = math_window_new(GTK_APPLICATION(application), equation);
  260. buttons = math_window_get_buttons(window);
  261. math_buttons_set_programming_base(buttons, base);
  262. math_buttons_set_mode(buttons, button_mode); // FIXME: We load the basic buttons even if we immediately switch to the next type
  263. g_signal_connect(buttons, "notify::programming-base", G_CALLBACK(programming_base_cb), NULL);
  264. g_signal_connect(buttons, "notify::mode", G_CALLBACK(mode_cb), NULL);
  265. gtk_widget_show(GTK_WIDGET(window));
  266. }
  267. static void
  268. activate_cb(GApplication *application)
  269. {
  270. gtk_window_present(GTK_WINDOW(window));
  271. }
  272. int
  273. main(int argc, char **argv)
  274. {
  275. GtkApplication *app;
  276. int status;
  277. setlocale(LC_ALL, "");
  278. bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
  279. bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
  280. textdomain(GETTEXT_PACKAGE);
  281. /* Seed random number generator. */
  282. srand48((long) time((time_t *) 0));
  283. g_type_init();
  284. get_options(argc, argv);
  285. gtk_init(&argc, &argv);
  286. gtk_window_set_default_icon_name("accessories-calculator");
  287. app = gtk_application_new("org.gnome.gcalctool", G_APPLICATION_FLAGS_NONE);
  288. g_signal_connect(app, "startup", G_CALLBACK(startup_cb), NULL);
  289. g_signal_connect(app, "activate", G_CALLBACK(activate_cb), NULL);
  290. status = g_application_run(G_APPLICATION(app), argc, argv);
  291. return status;
  292. }