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
12 #include <glib/gi18n.h>
14 #include <gdk/gdkkeysyms.h>
16 #include "math-window.h"
17 #include "math-preferences.h"
24 struct MathWindowPrivate
27 MathEquation
*equation
;
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_APPLICATION_WINDOW
);
39 math_window_new(GtkApplication
*app
, MathEquation
*equation
)
41 return g_object_new(math_window_get_type(),
43 "equation", equation
, NULL
);
48 math_window_get_menu_bar(MathWindow
*window
)
50 g_return_val_if_fail(window
!= NULL
, NULL
);
51 return window
->priv
->menu_bar
;
56 math_window_get_equation(MathWindow
*window
)
58 g_return_val_if_fail(window
!= NULL
, NULL
);
59 return window
->priv
->equation
;
64 math_window_get_display(MathWindow
*window
)
66 g_return_val_if_fail(window
!= NULL
, NULL
);
67 return window
->priv
->display
;
72 math_window_get_buttons(MathWindow
*window
)
74 g_return_val_if_fail(window
!= NULL
, NULL
);
75 return window
->priv
->buttons
;
80 math_window_critical_error(MathWindow
*window
, const gchar
*title
, const gchar
*contents
)
84 g_return_if_fail(window
!= NULL
);
85 g_return_if_fail(title
!= NULL
);
86 g_return_if_fail(contents
!= NULL
);
88 dialog
= gtk_message_dialog_new(NULL
, 0,
92 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog
),
94 gtk_dialog_add_buttons(GTK_DIALOG(dialog
), GTK_STOCK_QUIT
, GTK_RESPONSE_ACCEPT
, NULL
);
96 gtk_dialog_run(GTK_DIALOG(dialog
));
98 gtk_widget_destroy(GTK_WIDGET(window
));
103 copy_cb(GtkWidget
*widget
, MathWindow
*window
)
105 math_equation_copy(window
->priv
->equation
);
110 paste_cb(GtkWidget
*widget
, MathWindow
*window
)
112 math_equation_paste(window
->priv
->equation
);
117 undo_cb(GtkWidget
*widget
, MathWindow
*window
)
119 math_equation_undo(window
->priv
->equation
);
124 redo_cb(GtkWidget
*widget
, MathWindow
*window
)
126 math_equation_redo(window
->priv
->equation
);
131 mode_changed_cb(GtkWidget
*menu
, MathWindow
*window
)
135 if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menu
)))
138 mode
= GPOINTER_TO_INT(g_object_get_data(G_OBJECT(menu
), "calcmode"));
139 math_buttons_set_mode(window
->priv
->buttons
, mode
);
144 show_preferences_cb(GtkMenuItem
*menu
, MathWindow
*window
)
146 if (!window
->priv
->preferences_dialog
) {
147 window
->priv
->preferences_dialog
= math_preferences_dialog_new(window
->priv
->equation
);
148 gtk_window_set_transient_for(GTK_WINDOW(window
->priv
->preferences_dialog
), GTK_WINDOW(window
));
150 gtk_window_present(GTK_WINDOW(window
->priv
->preferences_dialog
));
155 help_cb(GtkWidget
*widget
, MathWindow
*window
)
158 GError
*error
= NULL
;
160 screen
= gtk_widget_get_screen(GTK_WIDGET(window
));
161 gtk_show_uri(screen
, "help:gcalctool", gtk_get_current_event_time(), &error
);
166 /* Translators: Error message displayed when unable to launch help browser */
167 const char *message
= _("Unable to open help file");
169 d
= gtk_message_dialog_new(GTK_WINDOW (window
),
170 GTK_DIALOG_MODAL
| GTK_DIALOG_DESTROY_WITH_PARENT
,
171 GTK_MESSAGE_ERROR
, GTK_BUTTONS_CLOSE
,
173 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG (d
),
174 "%s", error
->message
);
175 g_signal_connect(d
, "response", G_CALLBACK(gtk_widget_destroy
), NULL
);
176 gtk_window_present(GTK_WINDOW(d
));
184 about_cb(GtkWidget
*widget
, MathWindow
*window
)
186 const gchar
*authors
[] = {
187 "Rich Burridge <rich.burridge@gmail.com>",
188 "Robert Ancell <robert.ancell@gmail.com>",
189 "Klaus Niederkrüger <kniederk@umpa.ens-lyon.fr>",
190 "Robin Sonefors <ozamosi@flukkost.nu>",
193 const gchar
*documenters
[] = {
198 /* The translator credits. Please translate this with your name(s). */
199 const gchar
*translator_credits
= _("translator-credits");
201 /* The license this software is under (GPL2+) */
202 char *license
= _("Gcalctool is free software; you can redistribute it and/or modify\n"
203 "it under the terms of the GNU General Public License as published by\n"
204 "the Free Software Foundation; either version 2 of the License, or\n"
205 "(at your option) any later version.\n"
207 "Gcalctool is distributed in the hope that it will be useful,\n"
208 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
209 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
210 "GNU General Public License for more details.\n"
212 "You should have received a copy of the GNU General Public License\n"
213 "along with Gcalctool; if not, write to the Free Software Foundation, Inc.,\n"
214 "51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA");
216 gtk_show_about_dialog(GTK_WINDOW(window
),
218 /* Program name in the about dialog */
222 /* Copyright notice in the about dialog */
223 _("\xc2\xa9 1986–2010 The Gcalctool authors"),
226 /* Short description in the about dialog */
227 _("Calculator with financial and scientific modes."),
229 "documenters", documenters
,
230 "translator_credits", translator_credits
,
231 "logo-icon-name", "accessories-calculator",
237 quit_cb(GtkWidget
*widget
, MathWindow
*window
)
239 gtk_widget_destroy(GTK_WIDGET(window
));
244 key_press_cb(MathWindow
*window
, GdkEventKey
*event
)
247 g_signal_emit_by_name(window
->priv
->display
, "key-press-event", event
, &result
);
249 if (math_buttons_get_mode (window
->priv
->buttons
) == PROGRAMMING
&& (event
->state
& GDK_CONTROL_MASK
) == GDK_CONTROL_MASK
) {
250 switch(event
->keyval
)
254 math_equation_set_base (window
->priv
->equation
, 2);
258 math_equation_set_base (window
->priv
->equation
, 8);
262 math_equation_set_base (window
->priv
->equation
, 10);
266 math_equation_set_base (window
->priv
->equation
, 16);
276 scroll_changed_cb(GtkAdjustment
*adjustment
, MathWindow
*window
)
278 if (window
->priv
->right_aligned
)
279 gtk_adjustment_set_value(adjustment
, gtk_adjustment_get_upper(adjustment
) - gtk_adjustment_get_page_size(adjustment
));
284 scroll_value_changed_cb(GtkAdjustment
*adjustment
, MathWindow
*window
)
286 if (gtk_adjustment_get_value(adjustment
) == gtk_adjustment_get_upper(adjustment
) - gtk_adjustment_get_page_size(adjustment
))
287 window
->priv
->right_aligned
= TRUE
;
289 window
->priv
->right_aligned
= FALSE
;
294 button_mode_changed_cb(MathButtons
*buttons
, GParamSpec
*spec
, MathWindow
*window
)
298 switch(math_buttons_get_mode(buttons
))
302 menu
= window
->priv
->mode_basic_menu_item
;
303 //FIXME: Should it revert to decimal mode? math_equation_set_number_format(window->priv->equation, DEC);
307 menu
= window
->priv
->mode_advanced_menu_item
;
311 menu
= window
->priv
->mode_financial_menu_item
;
315 menu
= window
->priv
->mode_programming_menu_item
;
318 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu
), TRUE
);
323 add_menu(GtkWidget
*menu_bar
, const gchar
*name
)
325 GtkWidget
*menu_item
, *menu
;
327 menu_item
= gtk_menu_item_new_with_mnemonic(name
);
328 gtk_menu_shell_append(GTK_MENU_SHELL(menu_bar
), menu_item
);
329 gtk_widget_show(menu_item
);
330 menu
= gtk_menu_new();
331 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item
), menu
);
338 add_menu_item(GtkWidget
*menu
, GtkWidget
*menu_item
, GCallback callback
, gpointer callback_data
)
340 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), menu_item
);
341 gtk_widget_show(menu_item
);
343 g_signal_connect(G_OBJECT(menu_item
), "activate", callback
, callback_data
);
349 radio_menu_item_new(GSList
**group
, const gchar
*name
)
351 GtkWidget
*menu_item
;
352 menu_item
= gtk_radio_menu_item_new_with_mnemonic(*group
, name
);
353 *group
= gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(menu_item
));
359 create_menu(MathWindow
*window
)
361 GtkAccelGroup
*accel_group
;
362 GtkWidget
*menu
, *menu_item
;
363 GSList
*group
= NULL
;
365 accel_group
= gtk_accel_group_new();
366 gtk_window_add_accel_group(GTK_WINDOW(window
), accel_group
);
368 /* Calculator menu */
369 #define CALCULATOR_MENU_LABEL _("_Calculator")
371 #define MODE_MENU_LABEL _("_Mode")
372 /* Help menu label */
373 #define HELP_MENU_LABEL _("_Help")
374 /* Basic menu label */
375 #define MODE_BASIC_LABEL _("_Basic")
376 /* Advanced menu label */
377 #define MODE_ADVANCED_LABEL _("_Advanced")
378 /* Financial menu label */
379 #define MODE_FINANCIAL_LABEL _("_Financial")
380 /* Programming menu label */
381 #define MODE_PROGRAMMING_LABEL _("_Programming")
382 /* Help>Contents menu label */
383 #define HELP_CONTENTS_LABEL _("_Contents")
385 menu
= add_menu(window
->priv
->menu_bar
, CALCULATOR_MENU_LABEL
);
386 add_menu_item(menu
, gtk_image_menu_item_new_from_stock(GTK_STOCK_COPY
, accel_group
), G_CALLBACK(copy_cb
), window
);
387 add_menu_item(menu
, gtk_image_menu_item_new_from_stock(GTK_STOCK_PASTE
, accel_group
), G_CALLBACK(paste_cb
), window
);
388 menu_item
= add_menu_item(menu
, gtk_image_menu_item_new_from_stock(GTK_STOCK_UNDO
, accel_group
), G_CALLBACK(undo_cb
), window
);
389 gtk_widget_add_accelerator(menu_item
, "activate", accel_group
, GDK_KEY_z
, GDK_CONTROL_MASK
, GTK_ACCEL_VISIBLE
);
390 menu_item
= add_menu_item(menu
, gtk_image_menu_item_new_from_stock(GTK_STOCK_REDO
, accel_group
), G_CALLBACK(redo_cb
), window
);
391 gtk_widget_add_accelerator(menu_item
, "activate", accel_group
, GDK_KEY_z
, GDK_CONTROL_MASK
| GDK_SHIFT_MASK
, GTK_ACCEL_VISIBLE
);
392 add_menu_item(menu
, gtk_separator_menu_item_new(), NULL
, NULL
);
393 add_menu_item(menu
, gtk_image_menu_item_new_from_stock(GTK_STOCK_PREFERENCES
, accel_group
), G_CALLBACK(show_preferences_cb
), window
);
394 add_menu_item(menu
, gtk_separator_menu_item_new(), NULL
, NULL
);
395 menu_item
= add_menu_item(menu
, gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT
, accel_group
), G_CALLBACK(quit_cb
), window
);
396 gtk_widget_add_accelerator(menu_item
, "activate", accel_group
, GDK_KEY_w
, GDK_CONTROL_MASK
, 0);
398 menu
= add_menu(window
->priv
->menu_bar
, MODE_MENU_LABEL
);
399 window
->priv
->mode_basic_menu_item
= add_menu_item(menu
, radio_menu_item_new(&group
, MODE_BASIC_LABEL
), G_CALLBACK(mode_changed_cb
), window
);
400 g_object_set_data(G_OBJECT(window
->priv
->mode_basic_menu_item
), "calcmode", GINT_TO_POINTER(BASIC
));
401 window
->priv
->mode_advanced_menu_item
= add_menu_item(menu
, radio_menu_item_new(&group
, MODE_ADVANCED_LABEL
), G_CALLBACK(mode_changed_cb
), window
);
402 g_object_set_data(G_OBJECT(window
->priv
->mode_advanced_menu_item
), "calcmode", GINT_TO_POINTER(ADVANCED
));
403 window
->priv
->mode_financial_menu_item
= add_menu_item(menu
, radio_menu_item_new(&group
, MODE_FINANCIAL_LABEL
), G_CALLBACK(mode_changed_cb
), window
);
404 g_object_set_data(G_OBJECT(window
->priv
->mode_financial_menu_item
), "calcmode", GINT_TO_POINTER(FINANCIAL
));
405 window
->priv
->mode_programming_menu_item
= add_menu_item(menu
, radio_menu_item_new(&group
, MODE_PROGRAMMING_LABEL
), G_CALLBACK(mode_changed_cb
), window
);
406 g_object_set_data(G_OBJECT(window
->priv
->mode_programming_menu_item
), "calcmode", GINT_TO_POINTER(PROGRAMMING
));
408 menu
= add_menu(window
->priv
->menu_bar
, HELP_MENU_LABEL
);
409 menu_item
= add_menu_item(menu
, gtk_menu_item_new_with_mnemonic(HELP_CONTENTS_LABEL
), G_CALLBACK(help_cb
), window
);
410 gtk_widget_add_accelerator(menu_item
, "activate", accel_group
, GDK_KEY_F1
, 0, GTK_ACCEL_VISIBLE
);
411 add_menu_item(menu
, gtk_image_menu_item_new_from_stock(GTK_STOCK_ABOUT
, accel_group
), G_CALLBACK(about_cb
), window
);
416 create_gui(MathWindow
*window
)
418 GtkWidget
*main_vbox
, *vbox
;
419 GtkWidget
*scrolled_window
;
421 main_vbox
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 0);
422 gtk_container_add(GTK_CONTAINER(window
), main_vbox
);
423 gtk_widget_show(main_vbox
);
425 window
->priv
->menu_bar
= gtk_menu_bar_new();
426 gtk_box_pack_start(GTK_BOX(main_vbox
), window
->priv
->menu_bar
, FALSE
, FALSE
, 0);
427 gtk_widget_show(window
->priv
->menu_bar
);
431 vbox
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 6);
432 gtk_container_set_border_width(GTK_CONTAINER(vbox
), 6);
433 gtk_box_pack_start(GTK_BOX(main_vbox
), vbox
, TRUE
, TRUE
, 0);
434 gtk_widget_show(vbox
);
436 scrolled_window
= gtk_scrolled_window_new(NULL
, NULL
);
437 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window
), GTK_POLICY_AUTOMATIC
, GTK_POLICY_NEVER
);
438 gtk_box_pack_start(GTK_BOX(vbox
), GTK_WIDGET(scrolled_window
), FALSE
, FALSE
, 0);
439 g_signal_connect(gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(scrolled_window
)), "changed", G_CALLBACK(scroll_changed_cb
), window
);
440 g_signal_connect(gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(scrolled_window
)), "value-changed", G_CALLBACK(scroll_value_changed_cb
), window
);
441 window
->priv
->right_aligned
= TRUE
;
442 gtk_widget_show(scrolled_window
);
444 window
->priv
->display
= math_display_new_with_equation(window
->priv
->equation
);
445 gtk_container_add(GTK_CONTAINER(scrolled_window
), GTK_WIDGET(window
->priv
->display
));
446 gtk_widget_show(GTK_WIDGET(window
->priv
->display
));
448 window
->priv
->buttons
= math_buttons_new(window
->priv
->equation
);
449 g_signal_connect(window
->priv
->buttons
, "notify::mode", G_CALLBACK(button_mode_changed_cb
), window
);
450 button_mode_changed_cb(window
->priv
->buttons
, NULL
, window
);
451 gtk_box_pack_start(GTK_BOX(vbox
), GTK_WIDGET(window
->priv
->buttons
), TRUE
, TRUE
, 0);
452 gtk_widget_show(GTK_WIDGET(window
->priv
->buttons
));
457 math_window_set_property(GObject
*object
,
464 self
= MATH_WINDOW(object
);
468 self
->priv
->equation
= g_value_get_object(value
);
472 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
479 math_window_get_property(GObject
*object
,
486 self
= MATH_WINDOW(object
);
490 g_value_set_object(value
, self
->priv
->equation
);
493 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
500 math_window_class_init(MathWindowClass
*klass
)
502 GObjectClass
*object_class
= G_OBJECT_CLASS(klass
);
504 object_class
->get_property
= math_window_get_property
;
505 object_class
->set_property
= math_window_set_property
;
507 g_type_class_add_private(klass
, sizeof(MathWindowPrivate
));
509 g_object_class_install_property(object_class
,
511 g_param_spec_object("equation",
513 "Equation being calculated",
514 math_equation_get_type(),
515 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT_ONLY
));
520 math_window_init(MathWindow
*window
)
522 window
->priv
= G_TYPE_INSTANCE_GET_PRIVATE(window
, math_window_get_type(), MathWindowPrivate
);
523 gtk_window_set_title(GTK_WINDOW(window
),
524 /* Title of main window */
526 gtk_window_set_icon_name(GTK_WINDOW(window
), "accessories-calculator");
527 gtk_window_set_role(GTK_WINDOW(window
), "gcalctool");
528 gtk_window_set_resizable(GTK_WINDOW(window
), FALSE
);
529 g_signal_connect_after(G_OBJECT(window
), "key-press-event", G_CALLBACK(key_press_cb
), NULL
);