2 * Copyright (C) 2008-2011 Robert Ancell
4 * This program is free software: you can redistribute it and/or modify it under
5 * the terms of the GNU General Public License as published by the Free Software
6 * Foundation, either version 2 of the License, or (at your option) any later
7 * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
11 #include <glib/gi18n.h>
14 #include "math-preferences.h"
16 G_DEFINE_TYPE (MathPreferencesDialog
, math_preferences
, GTK_TYPE_DIALOG
);
23 struct MathPreferencesDialogPrivate
25 MathEquation
*equation
;
29 #define UI_DIALOGS_FILE UI_DIR "/preferences.ui"
30 #define GET_WIDGET(ui, name) \
31 GTK_WIDGET(gtk_builder_get_object(ui, name))
34 MathPreferencesDialog
*
35 math_preferences_dialog_new(MathEquation
*equation
)
37 return g_object_new(math_preferences_get_type(), "equation", equation
, NULL
);
42 preferences_response_cb(GtkWidget
*widget
, gint id
)
44 gtk_widget_hide(widget
);
49 preferences_dialog_delete_cb(GtkWidget
*widget
, GdkEvent
*event
)
51 preferences_response_cb(widget
, 0);
56 void number_format_combobox_changed_cb(GtkWidget
*combo
, MathPreferencesDialog
*dialog
);
59 number_format_combobox_changed_cb(GtkWidget
*combo
, MathPreferencesDialog
*dialog
)
61 MpDisplayFormat value
;
65 model
= gtk_combo_box_get_model(GTK_COMBO_BOX(combo
));
66 gtk_combo_box_get_active_iter(GTK_COMBO_BOX(combo
), &iter
);
67 gtk_tree_model_get(model
, &iter
, 1, &value
, -1);
68 math_equation_set_number_format(dialog
->priv
->equation
, value
);
72 void angle_unit_combobox_changed_cb(GtkWidget
*combo
, MathPreferencesDialog
*dialog
);
75 angle_unit_combobox_changed_cb(GtkWidget
*combo
, MathPreferencesDialog
*dialog
)
81 model
= gtk_combo_box_get_model(GTK_COMBO_BOX(combo
));
82 gtk_combo_box_get_active_iter(GTK_COMBO_BOX(combo
), &iter
);
83 gtk_tree_model_get(model
, &iter
, 1, &value
, -1);
84 math_equation_set_angle_units(dialog
->priv
->equation
, value
);
88 void word_size_combobox_changed_cb(GtkWidget
*combo
, MathPreferencesDialog
*dialog
);
91 word_size_combobox_changed_cb(GtkWidget
*combo
, MathPreferencesDialog
*dialog
)
97 model
= gtk_combo_box_get_model(GTK_COMBO_BOX(combo
));
98 gtk_combo_box_get_active_iter(GTK_COMBO_BOX(combo
), &iter
);
99 gtk_tree_model_get(model
, &iter
, 1, &value
, -1);
100 math_equation_set_word_size(dialog
->priv
->equation
, value
);
104 void decimal_places_spin_change_value_cb(GtkWidget
*spin
, MathPreferencesDialog
*dialog
);
107 decimal_places_spin_change_value_cb(GtkWidget
*spin
, MathPreferencesDialog
*dialog
)
111 value
= gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(spin
));
112 math_equation_set_accuracy(dialog
->priv
->equation
, value
);
116 void thousands_separator_check_toggled_cb(GtkWidget
*check
, MathPreferencesDialog
*dialog
);
119 thousands_separator_check_toggled_cb(GtkWidget
*check
, MathPreferencesDialog
*dialog
)
123 value
= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(check
));
124 math_equation_set_show_thousands_separators(dialog
->priv
->equation
, value
);
128 void trailing_zeroes_check_toggled_cb(GtkWidget
*check
, MathPreferencesDialog
*dialog
);
131 trailing_zeroes_check_toggled_cb(GtkWidget
*check
, MathPreferencesDialog
*dialog
)
135 value
= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(check
));
136 math_equation_set_show_trailing_zeroes(dialog
->priv
->equation
, value
);
141 set_combo_box_from_int(GtkWidget
*combo
, int value
)
147 model
= gtk_combo_box_get_model(GTK_COMBO_BOX(combo
));
148 valid
= gtk_tree_model_get_iter_first(model
, &iter
);
153 gtk_tree_model_get(model
, &iter
, 1, &v
, -1);
156 valid
= gtk_tree_model_iter_next(model
, &iter
);
159 valid
= gtk_tree_model_get_iter_first(model
, &iter
);
161 gtk_combo_box_set_active_iter(GTK_COMBO_BOX(combo
), &iter
);
166 accuracy_cb(MathEquation
*equation
, GParamSpec
*spec
, MathPreferencesDialog
*dialog
)
168 gtk_spin_button_set_value(GTK_SPIN_BUTTON(gtk_builder_get_object(dialog
->priv
->ui
, "decimal_places_spin")),
169 math_equation_get_accuracy(equation
));
174 show_thousands_separators_cb(MathEquation
*equation
, GParamSpec
*spec
, MathPreferencesDialog
*dialog
)
176 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(gtk_builder_get_object(dialog
->priv
->ui
, "thousands_separator_check")),
177 math_equation_get_show_thousands_separators(equation
));
182 show_trailing_zeroes_cb(MathEquation
*equation
, GParamSpec
*spec
, MathPreferencesDialog
*dialog
)
184 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(gtk_builder_get_object(dialog
->priv
->ui
, "trailing_zeroes_check")),
185 math_equation_get_show_trailing_zeroes(equation
));
190 number_format_cb(MathEquation
*equation
, GParamSpec
*spec
, MathPreferencesDialog
*dialog
)
192 set_combo_box_from_int(GET_WIDGET(dialog
->priv
->ui
, "number_format_combobox"), math_equation_get_number_format(equation
));
197 word_size_cb(MathEquation
*equation
, GParamSpec
*spec
, MathPreferencesDialog
*dialog
)
199 set_combo_box_from_int(GET_WIDGET(dialog
->priv
->ui
, "word_size_combobox"), math_equation_get_word_size(equation
));
204 angle_unit_cb(MathEquation
*equation
, GParamSpec
*spec
, MathPreferencesDialog
*dialog
)
206 set_combo_box_from_int(GET_WIDGET(dialog
->priv
->ui
, "angle_unit_combobox"), math_equation_get_angle_units(equation
));
211 create_gui(MathPreferencesDialog
*dialog
)
216 GtkCellRenderer
*renderer
;
217 gchar
*string
, **tokens
;
218 GError
*error
= NULL
;
219 static gchar
*objects
[] = { "preferences_table", "angle_unit_model", "number_format_model",
220 "word_size_model", "decimal_places_adjustment", "number_base_model", NULL
};
222 // FIXME: Handle errors
223 dialog
->priv
->ui
= gtk_builder_new();
224 gtk_builder_add_objects_from_file(dialog
->priv
->ui
, UI_DIALOGS_FILE
, objects
, &error
);
226 g_warning("Error loading preferences UI: %s", error
->message
);
227 g_clear_error(&error
);
229 gtk_window_set_title(GTK_WINDOW(dialog
),
230 /* Title of preferences dialog */
232 gtk_window_set_icon_name(GTK_WINDOW(dialog
), "accessories-calculator");
233 gtk_container_set_border_width(GTK_CONTAINER(dialog
), 8);
234 gtk_dialog_add_button(GTK_DIALOG(dialog
),
235 /* Label on close button in preferences dialog */
237 g_signal_connect(dialog
, "response", G_CALLBACK(preferences_response_cb
), NULL
);
238 g_signal_connect(dialog
, "delete-event", G_CALLBACK(preferences_dialog_delete_cb
), NULL
);
239 gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog
))), GET_WIDGET(dialog
->priv
->ui
, "preferences_table"), TRUE
, TRUE
, 0);
241 widget
= GET_WIDGET(dialog
->priv
->ui
, "angle_unit_combobox");
242 model
= gtk_combo_box_get_model(GTK_COMBO_BOX(widget
));
243 gtk_list_store_append(GTK_LIST_STORE(model
), &iter
);
244 gtk_list_store_set(GTK_LIST_STORE(model
), &iter
, 0,
245 /* Preferences dialog: Angle unit combo box: Use degrees for trigonometric calculations */
246 _("Degrees"), 1, MP_DEGREES
, -1);
247 gtk_list_store_append(GTK_LIST_STORE(model
), &iter
);
248 gtk_list_store_set(GTK_LIST_STORE(model
), &iter
, 0,
249 /* Preferences dialog: Angle unit combo box: Use radians for trigonometric calculations */
250 _("Radians"), 1, MP_RADIANS
, -1);
251 gtk_list_store_append(GTK_LIST_STORE(model
), &iter
);
252 gtk_list_store_set(GTK_LIST_STORE(model
), &iter
, 0,
253 /* Preferences dialog: Angle unit combo box: Use gradians for trigonometric calculations */
254 _("Gradians"), 1, MP_GRADIANS
, -1);
255 renderer
= gtk_cell_renderer_text_new();
256 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(widget
), renderer
, TRUE
);
257 gtk_cell_layout_add_attribute(GTK_CELL_LAYOUT(widget
), renderer
, "text", 0);
259 widget
= GET_WIDGET(dialog
->priv
->ui
, "number_format_combobox");
260 model
= gtk_combo_box_get_model(GTK_COMBO_BOX(widget
));
261 gtk_list_store_append(GTK_LIST_STORE(model
), &iter
);
262 gtk_list_store_set(GTK_LIST_STORE(model
), &iter
, 0,
263 /* Number display mode combo: Automatic, e.g. 1234 (or scientific for large number 1.234×10^99) */
264 _("Automatic"), 1, MP_DISPLAY_FORMAT_AUTOMATIC
, -1);
265 gtk_list_store_append(GTK_LIST_STORE(model
), &iter
);
266 gtk_list_store_set(GTK_LIST_STORE(model
), &iter
, 0,
267 /* Number display mode combo: Fixed, e.g. 1234 */
268 _("Fixed"), 1, MP_DISPLAY_FORMAT_FIXED
, -1);
269 gtk_list_store_append(GTK_LIST_STORE(model
), &iter
);
270 gtk_list_store_set(GTK_LIST_STORE(model
), &iter
, 0,
271 /* Number display mode combo: Scientific, e.g. 1.234×10^3 */
272 _("Scientific"), 1, MP_DISPLAY_FORMAT_SCIENTIFIC
, -1);
273 gtk_list_store_append(GTK_LIST_STORE(model
), &iter
);
274 gtk_list_store_set(GTK_LIST_STORE(model
), &iter
, 0,
275 /* Number display mode combo: Engineering, e.g. 1.234k */
276 _("Engineering"), 1, MP_DISPLAY_FORMAT_ENGINEERING
, -1);
277 renderer
= gtk_cell_renderer_text_new();
278 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(widget
), renderer
, TRUE
);
279 gtk_cell_layout_add_attribute(GTK_CELL_LAYOUT(widget
), renderer
, "text", 0);
281 widget
= GET_WIDGET(dialog
->priv
->ui
, "word_size_combobox");
282 renderer
= gtk_cell_renderer_text_new();
283 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(widget
), renderer
, TRUE
);
284 gtk_cell_layout_add_attribute(GTK_CELL_LAYOUT(widget
), renderer
, "text", 0);
286 /* Label used in preferences dialog. The %d is replaced by a spinbutton */
287 string
= _("Show %d decimal _places");
288 tokens
= g_strsplit(string
, "%d", 2);
289 widget
= GET_WIDGET(dialog
->priv
->ui
, "decimal_places_label1");
291 string
= g_strstrip(tokens
[0]);
294 if (string
[0] != '\0')
295 gtk_label_set_text_with_mnemonic(GTK_LABEL(widget
), string
);
297 gtk_widget_hide(widget
);
299 widget
= GET_WIDGET(dialog
->priv
->ui
, "decimal_places_label2");
300 if (tokens
[0] && tokens
[1])
301 string
= g_strstrip(tokens
[1]);
304 if (string
[0] != '\0')
305 gtk_label_set_text_with_mnemonic(GTK_LABEL(widget
), string
);
307 gtk_widget_hide(widget
);
311 gtk_builder_connect_signals(dialog
->priv
->ui
, dialog
);
313 g_signal_connect(dialog
->priv
->equation
, "notify::accuracy", G_CALLBACK(accuracy_cb
), dialog
);
314 g_signal_connect(dialog
->priv
->equation
, "notify::show-thousands-separators", G_CALLBACK(show_thousands_separators_cb
), dialog
);
315 g_signal_connect(dialog
->priv
->equation
, "notify::show-trailing_zeroes", G_CALLBACK(show_trailing_zeroes_cb
), dialog
);
316 g_signal_connect(dialog
->priv
->equation
, "notify::number-format", G_CALLBACK(number_format_cb
), dialog
);
317 g_signal_connect(dialog
->priv
->equation
, "notify::word-size", G_CALLBACK(word_size_cb
), dialog
);
318 g_signal_connect(dialog
->priv
->equation
, "notify::angle-units", G_CALLBACK(angle_unit_cb
), dialog
);
320 accuracy_cb(dialog
->priv
->equation
, NULL
, dialog
);
321 show_thousands_separators_cb(dialog
->priv
->equation
, NULL
, dialog
);
322 show_trailing_zeroes_cb(dialog
->priv
->equation
, NULL
, dialog
);
323 number_format_cb(dialog
->priv
->equation
, NULL
, dialog
);
324 word_size_cb(dialog
->priv
->equation
, NULL
, dialog
);
325 angle_unit_cb(dialog
->priv
->equation
, NULL
, dialog
);
330 math_preferences_set_property(GObject
*object
,
335 MathPreferencesDialog
*self
;
337 self
= MATH_PREFERENCES(object
);
341 self
->priv
->equation
= g_value_get_object(value
);
345 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
352 math_preferences_get_property(GObject
*object
,
357 MathPreferencesDialog
*self
;
359 self
= MATH_PREFERENCES(object
);
363 g_value_set_object(value
, self
->priv
->equation
);
366 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
373 math_preferences_class_init(MathPreferencesDialogClass
*klass
)
375 GObjectClass
*object_class
= G_OBJECT_CLASS(klass
);
377 object_class
->get_property
= math_preferences_get_property
;
378 object_class
->set_property
= math_preferences_set_property
;
380 g_type_class_add_private(klass
, sizeof(MathPreferencesDialogPrivate
));
382 g_object_class_install_property(object_class
,
384 g_param_spec_object("equation",
386 "Equation being configured",
387 math_equation_get_type(),
388 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT_ONLY
));
393 math_preferences_init(MathPreferencesDialog
*dialog
)
395 dialog
->priv
= G_TYPE_INSTANCE_GET_PRIVATE(dialog
, math_preferences_get_type(), MathPreferencesDialogPrivate
);