Fix the conversion of nautical miles
[gcalctool.git] / src / math-window.c
blob7021d985754bf0b74573f049fceeebff5beb6b22
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 <glib/gi18n.h>
13 #include <gtk/gtk.h>
14 #include <gdk/gdkkeysyms.h>
16 #include "math-window.h"
17 #include "math-preferences.h"
19 enum {
20 PROP_0,
21 PROP_EQUATION
24 struct MathWindowPrivate
26 GtkWidget *menu_bar;
27 MathEquation *equation;
28 MathDisplay *display;
29 MathButtons *buttons;
30 MathPreferencesDialog *preferences_dialog;
31 gboolean right_aligned;
32 GtkWidget *mode_basic_menu_item, *mode_advanced_menu_item, *mode_financial_menu_item, *mode_programming_menu_item;
35 G_DEFINE_TYPE (MathWindow, math_window, GTK_TYPE_WINDOW);
38 MathWindow *
39 math_window_new(MathEquation *equation)
41 return g_object_new(math_window_get_type(), "equation", equation, NULL);
45 GtkWidget *
46 math_window_get_menu_bar(MathWindow *window)
48 return window->priv->menu_bar;
52 MathEquation *
53 math_window_get_equation(MathWindow *window)
55 return window->priv->equation;
59 MathDisplay *
60 math_window_get_display(MathWindow *window)
62 return window->priv->display;
66 MathButtons *
67 math_window_get_buttons(MathWindow *window)
69 return window->priv->buttons;
73 void
74 math_window_critical_error(MathWindow *window, const gchar *title, const gchar *contents)
76 GtkWidget *dialog;
78 dialog = gtk_message_dialog_new(NULL, 0,
79 GTK_MESSAGE_ERROR,
80 GTK_BUTTONS_NONE,
81 "%s", title);
82 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
83 "%s", contents);
84 gtk_dialog_add_buttons(GTK_DIALOG(dialog), GTK_STOCK_QUIT, GTK_RESPONSE_ACCEPT, NULL);
86 gtk_dialog_run(GTK_DIALOG(dialog));
88 gtk_widget_destroy(GTK_WIDGET(window));
92 static void
93 copy_cb(GtkWidget *widget, MathWindow *window)
95 math_equation_copy(window->priv->equation);
99 static void
100 paste_cb(GtkWidget *widget, MathWindow *window)
102 math_equation_paste(window->priv->equation);
106 static void
107 undo_cb(GtkWidget *widget, MathWindow *window)
109 math_equation_undo(window->priv->equation);
113 static void
114 redo_cb(GtkWidget *widget, MathWindow *window)
116 math_equation_redo(window->priv->equation);
120 static void
121 mode_changed_cb(GtkWidget *menu, MathWindow *window)
123 int mode;
125 if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menu)))
126 return;
128 mode = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(menu), "calcmode"));
129 math_buttons_set_mode(window->priv->buttons, mode);
133 static void
134 show_preferences_cb(GtkMenuItem *menu, MathWindow *window)
136 if (!window->priv->preferences_dialog) {
137 window->priv->preferences_dialog = math_preferences_dialog_new(window->priv->equation);
138 gtk_window_set_transient_for(GTK_WINDOW(window->priv->preferences_dialog), GTK_WINDOW(window));
140 gtk_window_present(GTK_WINDOW(window->priv->preferences_dialog));
144 static void
145 help_cb(GtkWidget *widget, MathWindow *window)
147 GdkScreen *screen;
148 GError *error = NULL;
150 screen = gtk_widget_get_screen(GTK_WIDGET(window));
151 gtk_show_uri(screen, "ghelp:gcalctool", gtk_get_current_event_time(), &error);
153 if (error != NULL)
155 GtkWidget *d;
156 /* Translators: Error message displayed when unable to launch help browser */
157 const char *message = _("Unable to open help file");
159 d = gtk_message_dialog_new(GTK_WINDOW (window),
160 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
161 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
162 "%s", message);
163 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG (d),
164 "%s", error->message);
165 g_signal_connect(d, "response", G_CALLBACK(gtk_widget_destroy), NULL);
166 gtk_window_present(GTK_WINDOW(d));
168 g_error_free(error);
173 static void
174 about_cb(GtkWidget *widget, MathWindow *window)
176 const gchar *authors[] = {
177 "Rich Burridge <rich.burridge@gmail.com>",
178 "Robert Ancell <robert.ancell@gmail.com>",
179 "Klaus Niederkrüger <kniederk@umpa.ens-lyon.fr>",
180 "Robin Sonefors <ozamosi@flukkost.nu>",
181 NULL
183 const gchar *documenters[] = {
184 "Sun Microsystems",
185 NULL
188 /* The translator credits. Please translate this with your name(s). */
189 const gchar *translator_credits = _("translator-credits");
191 /* The license this software is under (GPL2+) */
192 char *license = _("Gcalctool is free software; you can redistribute it and/or modify\n"
193 "it under the terms of the GNU General Public License as published by\n"
194 "the Free Software Foundation; either version 2 of the License, or\n"
195 "(at your option) any later version.\n"
196 "\n"
197 "Gcalctool is distributed in the hope that it will be useful,\n"
198 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
199 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
200 "GNU General Public License for more details.\n"
201 "\n"
202 "You should have received a copy of the GNU General Public License\n"
203 "along with Gcalctool; if not, write to the Free Software Foundation, Inc.,\n"
204 "51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA");
206 gtk_show_about_dialog(GTK_WINDOW(window),
207 "name",
208 /* Program name in the about dialog */
209 _("Gcalctool"),
210 "version", VERSION,
211 "copyright",
212 /* Copyright notice in the about dialog */
213 _("\xc2\xa9 1986–2010 The Gcalctool authors"),
214 "license", license,
215 "comments",
216 /* Short description in the about dialog */
217 _("Calculator with financial and scientific modes."),
218 "authors", authors,
219 "documenters", documenters,
220 "translator_credits", translator_credits,
221 "logo-icon-name", "accessories-calculator",
222 NULL);
226 static void
227 quit_cb(GtkWidget *widget, MathWindow *window)
229 gtk_widget_destroy(GTK_WIDGET(window));
233 static gboolean
234 key_press_cb(MathWindow *window, GdkEventKey *event)
236 gboolean result;
237 g_signal_emit_by_name(window->priv->display, "key-press-event", event, &result);
238 return result;
242 static void
243 scroll_changed_cb(GtkAdjustment *adjustment, MathWindow *window)
245 if (window->priv->right_aligned)
246 gtk_adjustment_set_value(adjustment, gtk_adjustment_get_upper(adjustment) - gtk_adjustment_get_page_size(adjustment));
250 static void
251 scroll_value_changed_cb(GtkAdjustment *adjustment, MathWindow *window)
253 if (gtk_adjustment_get_value(adjustment) == gtk_adjustment_get_upper(adjustment) - gtk_adjustment_get_page_size(adjustment))
254 window->priv->right_aligned = TRUE;
255 else
256 window->priv->right_aligned = FALSE;
260 static void
261 button_mode_changed_cb(MathButtons *buttons, GParamSpec *spec, MathWindow *window)
263 GtkWidget *menu;
265 switch(math_buttons_get_mode(buttons))
267 default:
268 case BASIC:
269 menu = window->priv->mode_basic_menu_item;
270 //FIXME: Should it revert to decimal mode? math_equation_set_number_format(window->priv->equation, DEC);
271 break;
273 case ADVANCED:
274 menu = window->priv->mode_advanced_menu_item;
275 break;
277 case FINANCIAL:
278 menu = window->priv->mode_financial_menu_item;
279 break;
281 case PROGRAMMING:
282 menu = window->priv->mode_programming_menu_item;
283 break;
285 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu), TRUE);
289 static GtkWidget *
290 add_menu(GtkWidget *menu_bar, const gchar *name)
292 GtkWidget *menu_item, *menu;
294 menu_item = gtk_menu_item_new_with_mnemonic(name);
295 gtk_menu_shell_append(GTK_MENU_SHELL(menu_bar), menu_item);
296 gtk_widget_show(menu_item);
297 menu = gtk_menu_new();
298 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item), menu);
300 return menu;
304 static GtkWidget *
305 add_menu_item(GtkWidget *menu, GtkWidget *menu_item, GCallback callback, gpointer callback_data)
307 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item);
308 gtk_widget_show(menu_item);
309 if (callback)
310 g_signal_connect(G_OBJECT(menu_item), "activate", callback, callback_data);
311 return menu_item;
315 static GtkWidget *
316 radio_menu_item_new(GSList **group, const gchar *name)
318 GtkWidget *menu_item;
319 menu_item = gtk_radio_menu_item_new_with_mnemonic(*group, name);
320 *group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(menu_item));
321 return menu_item;
325 static void
326 create_menu(MathWindow *window)
328 GtkAccelGroup *accel_group;
329 GtkWidget *menu, *menu_item;
330 GSList *group = NULL;
332 accel_group = gtk_accel_group_new();
333 gtk_window_add_accel_group(GTK_WINDOW(window), accel_group);
335 /* Calculator menu */
336 #define CALCULATOR_MENU_LABEL _("_Calculator")
337 /* Mode menu */
338 #define MODE_MENU_LABEL _("_Mode")
339 /* Help menu label */
340 #define HELP_MENU_LABEL _("_Help")
341 /* Basic menu label */
342 #define MODE_BASIC_LABEL _("_Basic")
343 /* Advanced menu label */
344 #define MODE_ADVANCED_LABEL _("_Advanced")
345 /* Financial menu label */
346 #define MODE_FINANCIAL_LABEL _("_Financial")
347 /* Programming menu label */
348 #define MODE_PROGRAMMING_LABEL _("_Programming")
349 /* Help>Contents menu label */
350 #define HELP_CONTENTS_LABEL _("_Contents")
352 menu = add_menu(window->priv->menu_bar, CALCULATOR_MENU_LABEL);
353 add_menu_item(menu, gtk_image_menu_item_new_from_stock(GTK_STOCK_COPY, accel_group), G_CALLBACK(copy_cb), window);
354 add_menu_item(menu, gtk_image_menu_item_new_from_stock(GTK_STOCK_PASTE, accel_group), G_CALLBACK(paste_cb), window);
355 menu_item = add_menu_item(menu, gtk_image_menu_item_new_from_stock(GTK_STOCK_UNDO, accel_group), G_CALLBACK(undo_cb), window);
356 gtk_widget_add_accelerator(menu_item, "activate", accel_group, GDK_KEY_z, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
357 menu_item = add_menu_item(menu, gtk_image_menu_item_new_from_stock(GTK_STOCK_REDO, accel_group), G_CALLBACK(redo_cb), window);
358 gtk_widget_add_accelerator(menu_item, "activate", accel_group, GDK_KEY_z, GDK_CONTROL_MASK | GDK_SHIFT_MASK, GTK_ACCEL_VISIBLE);
359 add_menu_item(menu, gtk_separator_menu_item_new(), NULL, NULL);
360 add_menu_item(menu, gtk_image_menu_item_new_from_stock(GTK_STOCK_PREFERENCES, accel_group), G_CALLBACK(show_preferences_cb), window);
361 add_menu_item(menu, gtk_separator_menu_item_new(), NULL, NULL);
362 menu_item = add_menu_item(menu, gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT, accel_group), G_CALLBACK(quit_cb), window);
363 gtk_widget_add_accelerator(menu_item, "activate", accel_group, GDK_KEY_w, GDK_CONTROL_MASK, 0);
365 menu = add_menu(window->priv->menu_bar, MODE_MENU_LABEL);
366 window->priv->mode_basic_menu_item = add_menu_item(menu, radio_menu_item_new(&group, MODE_BASIC_LABEL), G_CALLBACK(mode_changed_cb), window);
367 g_object_set_data(G_OBJECT(window->priv->mode_basic_menu_item), "calcmode", GINT_TO_POINTER(BASIC));
368 window->priv->mode_advanced_menu_item = add_menu_item(menu, radio_menu_item_new(&group, MODE_ADVANCED_LABEL), G_CALLBACK(mode_changed_cb), window);
369 g_object_set_data(G_OBJECT(window->priv->mode_advanced_menu_item), "calcmode", GINT_TO_POINTER(ADVANCED));
370 window->priv->mode_financial_menu_item = add_menu_item(menu, radio_menu_item_new(&group, MODE_FINANCIAL_LABEL), G_CALLBACK(mode_changed_cb), window);
371 g_object_set_data(G_OBJECT(window->priv->mode_financial_menu_item), "calcmode", GINT_TO_POINTER(FINANCIAL));
372 window->priv->mode_programming_menu_item = add_menu_item(menu, radio_menu_item_new(&group, MODE_PROGRAMMING_LABEL), G_CALLBACK(mode_changed_cb), window);
373 g_object_set_data(G_OBJECT(window->priv->mode_programming_menu_item), "calcmode", GINT_TO_POINTER(PROGRAMMING));
375 menu = add_menu(window->priv->menu_bar, HELP_MENU_LABEL);
376 menu_item = add_menu_item(menu, gtk_menu_item_new_with_mnemonic(HELP_CONTENTS_LABEL), G_CALLBACK(help_cb), window);
377 gtk_widget_add_accelerator(menu_item, "activate", accel_group, GDK_KEY_F1, 0, GTK_ACCEL_VISIBLE);
378 add_menu_item(menu, gtk_image_menu_item_new_from_stock(GTK_STOCK_ABOUT, accel_group), G_CALLBACK(about_cb), window);
382 static void
383 create_gui(MathWindow *window)
385 GtkWidget *main_vbox, *vbox;
386 GtkWidget *scrolled_window;
388 main_vbox = gtk_vbox_new(FALSE, 0);
389 gtk_container_add(GTK_CONTAINER(window), main_vbox);
390 gtk_widget_show(main_vbox);
392 window->priv->menu_bar = gtk_menu_bar_new();
393 gtk_box_pack_start(GTK_BOX(main_vbox), window->priv->menu_bar, FALSE, FALSE, 0);
394 gtk_widget_show(window->priv->menu_bar);
396 create_menu(window);
398 vbox = gtk_vbox_new(FALSE, 6);
399 gtk_container_set_border_width(GTK_CONTAINER(vbox), 6);
400 gtk_box_pack_start(GTK_BOX(main_vbox), vbox, TRUE, TRUE, 0);
401 gtk_widget_show(vbox);
403 scrolled_window = gtk_scrolled_window_new(NULL, NULL);
404 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_NEVER);
405 gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(scrolled_window), FALSE, FALSE, 0);
406 g_signal_connect(gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(scrolled_window)), "changed", G_CALLBACK(scroll_changed_cb), window);
407 g_signal_connect(gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(scrolled_window)), "value-changed", G_CALLBACK(scroll_value_changed_cb), window);
408 window->priv->right_aligned = TRUE;
409 gtk_widget_show(scrolled_window);
411 window->priv->display = math_display_new_with_equation(window->priv->equation);
412 gtk_container_add(GTK_CONTAINER(scrolled_window), GTK_WIDGET(window->priv->display));
413 gtk_widget_show(GTK_WIDGET(window->priv->display));
415 window->priv->buttons = math_buttons_new(window->priv->equation);
416 g_signal_connect(window->priv->buttons, "notify::mode", G_CALLBACK(button_mode_changed_cb), window);
417 button_mode_changed_cb(window->priv->buttons, NULL, window);
418 gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(window->priv->buttons), TRUE, TRUE, 0);
419 gtk_widget_show(GTK_WIDGET(window->priv->buttons));
423 static void
424 math_window_set_property(GObject *object,
425 guint prop_id,
426 const GValue *value,
427 GParamSpec *pspec)
429 MathWindow *self;
431 self = MATH_WINDOW(object);
433 switch (prop_id) {
434 case PROP_EQUATION:
435 self->priv->equation = g_value_get_object(value);
436 create_gui(self);
437 break;
438 default:
439 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
440 break;
445 static void
446 math_window_get_property(GObject *object,
447 guint prop_id,
448 GValue *value,
449 GParamSpec *pspec)
451 MathWindow *self;
453 self = MATH_WINDOW(object);
455 switch (prop_id) {
456 case PROP_EQUATION:
457 g_value_set_object(value, self->priv->equation);
458 break;
459 default:
460 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
461 break;
466 static void
467 math_window_class_init(MathWindowClass *klass)
469 GObjectClass *object_class = G_OBJECT_CLASS(klass);
471 object_class->get_property = math_window_get_property;
472 object_class->set_property = math_window_set_property;
474 g_type_class_add_private(klass, sizeof(MathWindowPrivate));
476 g_object_class_install_property(object_class,
477 PROP_EQUATION,
478 g_param_spec_object("equation",
479 "equation",
480 "Equation being calculated",
481 math_equation_get_type(),
482 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
486 static void
487 math_window_init(MathWindow *window)
489 window->priv = G_TYPE_INSTANCE_GET_PRIVATE(window, math_window_get_type(), MathWindowPrivate);
490 gtk_window_set_title(GTK_WINDOW(window),
491 /* Title of main window */
492 _("Calculator"));
493 gtk_window_set_icon_name(GTK_WINDOW(window), "accessories-calculator");
494 gtk_window_set_role(GTK_WINDOW(window), "gcalctool");
495 gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
496 g_signal_connect_after(G_OBJECT(window), "key-press-event", G_CALLBACK(key_press_cb), NULL);