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_container_set_border_width(GTK_CONTAINER(dialog), 8);
233
gtk_dialog_add_button(GTK_DIALOG(dialog),
234
/* Label on close button in preferences dialog */
236
g_signal_connect(dialog, "response", G_CALLBACK(preferences_response_cb), NULL);
237
g_signal_connect(dialog, "delete-event", G_CALLBACK(preferences_dialog_delete_cb), NULL);
238
gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), GET_WIDGET(dialog->priv->ui, "preferences_table"), TRUE, TRUE, 0);
240
widget = GET_WIDGET(dialog->priv->ui, "angle_unit_combobox");
241
model = gtk_combo_box_get_model(GTK_COMBO_BOX(widget));
242
gtk_list_store_append(GTK_LIST_STORE(model), &iter);
243
gtk_list_store_set(GTK_LIST_STORE(model), &iter, 0,
244
/* Preferences dialog: Angle unit combo box: Use degrees for trigonometric calculations */
245
_("Degrees"), 1, MP_DEGREES, -1);
246
gtk_list_store_append(GTK_LIST_STORE(model), &iter);
247
gtk_list_store_set(GTK_LIST_STORE(model), &iter, 0,
248
/* Preferences dialog: Angle unit combo box: Use radians for trigonometric calculations */
249
_("Radians"), 1, MP_RADIANS, -1);
250
gtk_list_store_append(GTK_LIST_STORE(model), &iter);
251
gtk_list_store_set(GTK_LIST_STORE(model), &iter, 0,
252
/* Preferences dialog: Angle unit combo box: Use gradians for trigonometric calculations */
253
_("Gradians"), 1, MP_GRADIANS, -1);
254
renderer = gtk_cell_renderer_text_new();
255
gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(widget), renderer, TRUE);
256
gtk_cell_layout_add_attribute(GTK_CELL_LAYOUT(widget), renderer, "text", 0);
258
widget = GET_WIDGET(dialog->priv->ui, "number_format_combobox");
259
model = gtk_combo_box_get_model(GTK_COMBO_BOX(widget));
260
gtk_list_store_append(GTK_LIST_STORE(model), &iter);
261
gtk_list_store_set(GTK_LIST_STORE(model), &iter, 0,
262
/* Number display mode combo: Automatic, e.g. 1234 (or scientific for large number 1.234×10^99) */
263
_("Automatic"), 1, MP_DISPLAY_FORMAT_AUTOMATIC, -1);
264
gtk_list_store_append(GTK_LIST_STORE(model), &iter);
265
gtk_list_store_set(GTK_LIST_STORE(model), &iter, 0,
266
/* Number display mode combo: Fixed, e.g. 1234 */
267
_("Fixed"), 1, MP_DISPLAY_FORMAT_FIXED, -1);
268
gtk_list_store_append(GTK_LIST_STORE(model), &iter);
269
gtk_list_store_set(GTK_LIST_STORE(model), &iter, 0,
270
/* Number display mode combo: Scientific, e.g. 1.234×10^3 */
271
_("Scientific"), 1, MP_DISPLAY_FORMAT_SCIENTIFIC, -1);
272
gtk_list_store_append(GTK_LIST_STORE(model), &iter);
273
gtk_list_store_set(GTK_LIST_STORE(model), &iter, 0,
274
/* Number display mode combo: Engineering, e.g. 1.234k */
275
_("Engineering"), 1, MP_DISPLAY_FORMAT_ENGINEERING, -1);
276
renderer = gtk_cell_renderer_text_new();
277
gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(widget), renderer, TRUE);
278
gtk_cell_layout_add_attribute(GTK_CELL_LAYOUT(widget), renderer, "text", 0);
280
widget = GET_WIDGET(dialog->priv->ui, "word_size_combobox");
281
renderer = gtk_cell_renderer_text_new();
282
gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(widget), renderer, TRUE);
283
gtk_cell_layout_add_attribute(GTK_CELL_LAYOUT(widget), renderer, "text", 0);
285
/* Label used in preferences dialog. The %d is replaced by a spinbutton */
286
string = _("Show %d decimal _places");
287
tokens = g_strsplit(string, "%d", 2);
288
widget = GET_WIDGET(dialog->priv->ui, "decimal_places_label1");
290
string = g_strstrip(tokens[0]);
293
if (string[0] != '\0')
294
gtk_label_set_text_with_mnemonic(GTK_LABEL(widget), string);
296
gtk_widget_hide(widget);
298
widget = GET_WIDGET(dialog->priv->ui, "decimal_places_label2");
299
if (tokens[0] && tokens[1])
300
string = g_strstrip(tokens[1]);
303
if (string[0] != '\0')
304
gtk_label_set_text_with_mnemonic(GTK_LABEL(widget), string);
306
gtk_widget_hide(widget);
310
gtk_builder_connect_signals(dialog->priv->ui, dialog);
312
g_signal_connect(dialog->priv->equation, "notify::accuracy", G_CALLBACK(accuracy_cb), dialog);
313
g_signal_connect(dialog->priv->equation, "notify::show-thousands-separators", G_CALLBACK(show_thousands_separators_cb), dialog);
314
g_signal_connect(dialog->priv->equation, "notify::show-trailing_zeroes", G_CALLBACK(show_trailing_zeroes_cb), dialog);
315
g_signal_connect(dialog->priv->equation, "notify::number-format", G_CALLBACK(number_format_cb), dialog);
316
g_signal_connect(dialog->priv->equation, "notify::word-size", G_CALLBACK(word_size_cb), dialog);
317
g_signal_connect(dialog->priv->equation, "notify::angle-units", G_CALLBACK(angle_unit_cb), dialog);
319
accuracy_cb(dialog->priv->equation, NULL, dialog);
320
show_thousands_separators_cb(dialog->priv->equation, NULL, dialog);
321
show_trailing_zeroes_cb(dialog->priv->equation, NULL, dialog);
322
number_format_cb(dialog->priv->equation, NULL, dialog);
323
word_size_cb(dialog->priv->equation, NULL, dialog);
324
angle_unit_cb(dialog->priv->equation, NULL, dialog);
329
math_preferences_set_property(GObject *object,
334
MathPreferencesDialog *self;
336
self = MATH_PREFERENCES(object);
340
self->priv->equation = g_value_get_object(value);
344
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
351
math_preferences_get_property(GObject *object,
356
MathPreferencesDialog *self;
358
self = MATH_PREFERENCES(object);
362
g_value_set_object(value, self->priv->equation);
365
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
372
math_preferences_class_init(MathPreferencesDialogClass *klass)
374
GObjectClass *object_class = G_OBJECT_CLASS(klass);
376
object_class->get_property = math_preferences_get_property;
377
object_class->set_property = math_preferences_set_property;
379
g_type_class_add_private(klass, sizeof(MathPreferencesDialogPrivate));
381
g_object_class_install_property(object_class,
383
g_param_spec_object("equation",
385
"Equation being configured",
386
math_equation_get_type(),
387
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
392
math_preferences_init(MathPreferencesDialog *dialog)
394
dialog->priv = G_TYPE_INSTANCE_GET_PRIVATE(dialog, math_preferences_get_type(), MathPreferencesDialogPrivate);