Updated Latvian translation and added Latvian translation for help by Viesturs Ružāns...
[gcalctool.git] / src / math-preferences.c
blobb10fe811ab61e90604e85bd8eb014ef8d7739703
1 /*
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
8 * license.
9 */
11 #include <glib/gi18n.h>
12 #include <gtk/gtk.h>
14 #include "math-preferences.h"
16 G_DEFINE_TYPE (MathPreferencesDialog, math_preferences, GTK_TYPE_DIALOG);
18 enum {
19 PROP_0,
20 PROP_EQUATION
23 struct MathPreferencesDialogPrivate
25 MathEquation *equation;
26 GtkBuilder *ui;
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);
41 static void
42 preferences_response_cb(GtkWidget *widget, gint id)
44 gtk_widget_hide(widget);
48 static gboolean
49 preferences_dialog_delete_cb(GtkWidget *widget, GdkEvent *event)
51 preferences_response_cb(widget, 0);
52 return TRUE;
56 void number_format_combobox_changed_cb(GtkWidget *combo, MathPreferencesDialog *dialog);
57 G_MODULE_EXPORT
58 void
59 number_format_combobox_changed_cb(GtkWidget *combo, MathPreferencesDialog *dialog)
61 MpDisplayFormat value;
62 GtkTreeModel *model;
63 GtkTreeIter iter;
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);
73 G_MODULE_EXPORT
74 void
75 angle_unit_combobox_changed_cb(GtkWidget *combo, MathPreferencesDialog *dialog)
77 MPAngleUnit value;
78 GtkTreeModel *model;
79 GtkTreeIter iter;
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);
89 G_MODULE_EXPORT
90 void
91 word_size_combobox_changed_cb(GtkWidget *combo, MathPreferencesDialog *dialog)
93 gint value;
94 GtkTreeModel *model;
95 GtkTreeIter iter;
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);
105 G_MODULE_EXPORT
106 void
107 decimal_places_spin_change_value_cb(GtkWidget *spin, MathPreferencesDialog *dialog)
109 gint value = 0;
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);
117 G_MODULE_EXPORT
118 void
119 thousands_separator_check_toggled_cb(GtkWidget *check, MathPreferencesDialog *dialog)
121 gboolean value;
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);
129 G_MODULE_EXPORT
130 void
131 trailing_zeroes_check_toggled_cb(GtkWidget *check, MathPreferencesDialog *dialog)
133 gboolean value;
135 value = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(check));
136 math_equation_set_show_trailing_zeroes(dialog->priv->equation, value);
140 static void
141 set_combo_box_from_int(GtkWidget *combo, int value)
143 GtkTreeModel *model;
144 GtkTreeIter iter;
145 gboolean valid;
147 model = gtk_combo_box_get_model(GTK_COMBO_BOX(combo));
148 valid = gtk_tree_model_get_iter_first(model, &iter);
150 while (valid) {
151 gint v;
153 gtk_tree_model_get(model, &iter, 1, &v, -1);
154 if (v == value)
155 break;
156 valid = gtk_tree_model_iter_next(model, &iter);
158 if (!valid)
159 valid = gtk_tree_model_get_iter_first(model, &iter);
161 gtk_combo_box_set_active_iter(GTK_COMBO_BOX(combo), &iter);
165 static void
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));
173 static void
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));
181 static void
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));
189 static void
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));
196 static void
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));
203 static void
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));
210 static void
211 create_gui(MathPreferencesDialog *dialog)
213 GtkWidget *widget;
214 GtkTreeModel *model;
215 GtkTreeIter iter;
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);
225 if (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 */
231 _("Preferences"));
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 */
236 _("_Close"), 0);
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");
290 if (tokens[0])
291 string = g_strstrip(tokens[0]);
292 else
293 string = "";
294 if (string[0] != '\0')
295 gtk_label_set_text_with_mnemonic(GTK_LABEL(widget), string);
296 else
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]);
302 else
303 string = "";
304 if (string[0] != '\0')
305 gtk_label_set_text_with_mnemonic(GTK_LABEL(widget), string);
306 else
307 gtk_widget_hide(widget);
309 g_strfreev(tokens);
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);
329 static void
330 math_preferences_set_property(GObject *object,
331 guint prop_id,
332 const GValue *value,
333 GParamSpec *pspec)
335 MathPreferencesDialog *self;
337 self = MATH_PREFERENCES(object);
339 switch (prop_id) {
340 case PROP_EQUATION:
341 self->priv->equation = g_value_get_object(value);
342 create_gui(self);
343 break;
344 default:
345 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
346 break;
351 static void
352 math_preferences_get_property(GObject *object,
353 guint prop_id,
354 GValue *value,
355 GParamSpec *pspec)
357 MathPreferencesDialog *self;
359 self = MATH_PREFERENCES(object);
361 switch (prop_id) {
362 case PROP_EQUATION:
363 g_value_set_object(value, self->priv->equation);
364 break;
365 default:
366 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
367 break;
372 static void
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,
383 PROP_EQUATION,
384 g_param_spec_object("equation",
385 "equation",
386 "Equation being configured",
387 math_equation_get_type(),
388 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
392 static void
393 math_preferences_init(MathPreferencesDialog *dialog)
395 dialog->priv = G_TYPE_INSTANCE_GET_PRIVATE(dialog, math_preferences_get_type(), MathPreferencesDialogPrivate);