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_WINDOW
);
39 math_window_new(MathEquation
*equation
)
41 return g_object_new(math_window_get_type(), "equation", equation
, NULL
);
46 math_window_get_menu_bar(MathWindow
*window
)
48 return window
->priv
->menu_bar
;
53 math_window_get_equation(MathWindow
*window
)
55 return window
->priv
->equation
;
60 math_window_get_display(MathWindow
*window
)
62 return window
->priv
->display
;
67 math_window_get_buttons(MathWindow
*window
)
69 return window
->priv
->buttons
;
74 math_window_critical_error(MathWindow
*window
, const gchar
*title
, const gchar
*contents
)
78 dialog
= gtk_message_dialog_new(NULL
, 0,
82 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog
),
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
));
93 copy_cb(GtkWidget
*widget
, MathWindow
*window
)
95 math_equation_copy(window
->priv
->equation
);
100 paste_cb(GtkWidget
*widget
, MathWindow
*window
)
102 math_equation_paste(window
->priv
->equation
);
107 undo_cb(GtkWidget
*widget
, MathWindow
*window
)
109 math_equation_undo(window
->priv
->equation
);
114 redo_cb(GtkWidget
*widget
, MathWindow
*window
)
116 math_equation_redo(window
->priv
->equation
);
121 mode_changed_cb(GtkWidget
*menu
, MathWindow
*window
)
125 if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menu
)))
128 mode
= GPOINTER_TO_INT(g_object_get_data(G_OBJECT(menu
), "calcmode"));
129 math_buttons_set_mode(window
->priv
->buttons
, mode
);
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
));
145 help_cb(GtkWidget
*widget
, MathWindow
*window
)
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
);
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
,
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
));
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>",
183 const gchar
*documenters
[] = {
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"
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"
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
),
208 /* Program name in the about dialog */
212 /* Copyright notice in the about dialog */
213 _("\xc2\xa9 1986–2010 The Gcalctool authors"),
216 /* Short description in the about dialog */
217 _("Calculator with financial and scientific modes."),
219 "documenters", documenters
,
220 "translator_credits", translator_credits
,
221 "logo-icon-name", "accessories-calculator",
227 quit_cb(GtkWidget
*widget
, MathWindow
*window
)
229 gtk_widget_destroy(GTK_WIDGET(window
));
234 key_press_cb(MathWindow
*window
, GdkEventKey
*event
)
237 g_signal_emit_by_name(window
->priv
->display
, "key-press-event", event
, &result
);
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
));
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
;
256 window
->priv
->right_aligned
= FALSE
;
261 button_mode_changed_cb(MathButtons
*buttons
, GParamSpec
*spec
, MathWindow
*window
)
265 switch(math_buttons_get_mode(buttons
))
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);
274 menu
= window
->priv
->mode_advanced_menu_item
;
278 menu
= window
->priv
->mode_financial_menu_item
;
282 menu
= window
->priv
->mode_programming_menu_item
;
285 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu
), TRUE
);
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
);
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
);
310 g_signal_connect(G_OBJECT(menu_item
), "activate", callback
, callback_data
);
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
));
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")
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
);
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
);
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
));
424 math_window_set_property(GObject
*object
,
431 self
= MATH_WINDOW(object
);
435 self
->priv
->equation
= g_value_get_object(value
);
439 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
446 math_window_get_property(GObject
*object
,
453 self
= MATH_WINDOW(object
);
457 g_value_set_object(value
, self
->priv
->equation
);
460 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
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
,
478 g_param_spec_object("equation",
480 "Equation being calculated",
481 math_equation_get_type(),
482 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT_ONLY
));
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 */
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
);