~registry/gcalctool/trunk

« back to all changes in this revision

Viewing changes to src/math-preferences.c

  • Committer: Robert Ancell
  • Date: 2012-10-14 03:31:40 UTC
  • Revision ID: git-v1:12ba2c81b0a81bb3ac776d1034a3c41b3173196a
Port to Vala

https://bugzilla.gnome.org/show_bug.cgi?id=640685

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2008-2011 Robert Ancell
3
 
 *
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
 
 */
10
 
 
11
 
#include <glib/gi18n.h>
12
 
#include <gtk/gtk.h>
13
 
 
14
 
#include "math-preferences.h"
15
 
 
16
 
G_DEFINE_TYPE (MathPreferencesDialog, math_preferences, GTK_TYPE_DIALOG);
17
 
 
18
 
enum {
19
 
    PROP_0,
20
 
    PROP_EQUATION
21
 
};
22
 
 
23
 
struct MathPreferencesDialogPrivate
24
 
{
25
 
    MathEquation *equation;
26
 
    GtkBuilder *ui;
27
 
};
28
 
 
29
 
#define UI_DIALOGS_FILE  UI_DIR "/preferences.ui"
30
 
#define GET_WIDGET(ui, name) \
31
 
          GTK_WIDGET(gtk_builder_get_object(ui, name))
32
 
 
33
 
 
34
 
MathPreferencesDialog *
35
 
math_preferences_dialog_new(MathEquation *equation)
36
 
{  
37
 
    return g_object_new(math_preferences_get_type(), "equation", equation, NULL);
38
 
}
39
 
 
40
 
 
41
 
static void
42
 
preferences_response_cb(GtkWidget *widget, gint id)
43
 
{
44
 
    gtk_widget_hide(widget);
45
 
}
46
 
 
47
 
 
48
 
static gboolean
49
 
preferences_dialog_delete_cb(GtkWidget *widget, GdkEvent *event)
50
 
{
51
 
    preferences_response_cb(widget, 0);
52
 
    return TRUE;
53
 
}
54
 
 
55
 
 
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)
60
 
{
61
 
    MpDisplayFormat value;
62
 
    GtkTreeModel *model;
63
 
    GtkTreeIter iter;
64
 
 
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);
69
 
}
70
 
 
71
 
 
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)
76
 
{
77
 
    MPAngleUnit value;
78
 
    GtkTreeModel *model;
79
 
    GtkTreeIter iter;
80
 
 
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);
85
 
}
86
 
 
87
 
 
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)
92
 
{
93
 
    gint value;
94
 
    GtkTreeModel *model;
95
 
    GtkTreeIter iter;
96
 
 
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);
101
 
}
102
 
 
103
 
 
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)
108
 
{
109
 
    gint value = 0;
110
 
 
111
 
    value = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(spin));
112
 
    math_equation_set_accuracy(dialog->priv->equation, value);
113
 
}
114
 
 
115
 
 
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)
120
 
{
121
 
    gboolean value;
122
 
 
123
 
    value = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(check));
124
 
    math_equation_set_show_thousands_separators(dialog->priv->equation, value);
125
 
}
126
 
 
127
 
 
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)
132
 
{
133
 
    gboolean value;
134
 
 
135
 
    value = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(check));
136
 
    math_equation_set_show_trailing_zeroes(dialog->priv->equation, value);
137
 
}
138
 
 
139
 
 
140
 
static void
141
 
set_combo_box_from_int(GtkWidget *combo, int value)
142
 
{
143
 
    GtkTreeModel *model;
144
 
    GtkTreeIter iter;
145
 
    gboolean valid;
146
 
 
147
 
    model = gtk_combo_box_get_model(GTK_COMBO_BOX(combo));
148
 
    valid = gtk_tree_model_get_iter_first(model, &iter);
149
 
 
150
 
    while (valid) {
151
 
        gint v;
152
 
 
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);
157
 
    }
158
 
    if (!valid)
159
 
        valid = gtk_tree_model_get_iter_first(model, &iter);
160
 
 
161
 
    gtk_combo_box_set_active_iter(GTK_COMBO_BOX(combo), &iter);
162
 
}
163
 
 
164
 
 
165
 
static void
166
 
accuracy_cb(MathEquation *equation, GParamSpec *spec, MathPreferencesDialog *dialog)
167
 
{
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));
170
 
}
171
 
 
172
 
 
173
 
static void
174
 
show_thousands_separators_cb(MathEquation *equation, GParamSpec *spec, MathPreferencesDialog *dialog)
175
 
{
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));
178
 
}
179
 
 
180
 
 
181
 
static void
182
 
show_trailing_zeroes_cb(MathEquation *equation, GParamSpec *spec, MathPreferencesDialog *dialog)
183
 
{
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));
186
 
}
187
 
 
188
 
 
189
 
static void
190
 
number_format_cb(MathEquation *equation, GParamSpec *spec, MathPreferencesDialog *dialog)
191
 
{
192
 
    set_combo_box_from_int(GET_WIDGET(dialog->priv->ui, "number_format_combobox"), math_equation_get_number_format(equation));
193
 
}
194
 
 
195
 
 
196
 
static void
197
 
word_size_cb(MathEquation *equation, GParamSpec *spec, MathPreferencesDialog *dialog)
198
 
{
199
 
    set_combo_box_from_int(GET_WIDGET(dialog->priv->ui, "word_size_combobox"), math_equation_get_word_size(equation));
200
 
}
201
 
 
202
 
 
203
 
static void
204
 
angle_unit_cb(MathEquation *equation, GParamSpec *spec, MathPreferencesDialog *dialog)
205
 
{
206
 
    set_combo_box_from_int(GET_WIDGET(dialog->priv->ui, "angle_unit_combobox"), math_equation_get_angle_units(equation));  
207
 
}
208
 
 
209
 
 
210
 
static void
211
 
create_gui(MathPreferencesDialog *dialog)
212
 
{
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 };
221
 
 
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);
228
 
 
229
 
    gtk_window_set_title(GTK_WINDOW(dialog),
230
 
                         /* Title of preferences dialog */
231
 
                         _("Preferences"));
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 */
235
 
                          _("_Close"), 0);
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);
239
 
 
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);
257
 
 
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);
279
 
 
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);
284
 
 
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");
289
 
    if (tokens[0])
290
 
        string = g_strstrip(tokens[0]);
291
 
    else
292
 
        string = "";
293
 
    if (string[0] != '\0')
294
 
        gtk_label_set_text_with_mnemonic(GTK_LABEL(widget), string);
295
 
    else
296
 
        gtk_widget_hide(widget);
297
 
 
298
 
    widget = GET_WIDGET(dialog->priv->ui, "decimal_places_label2");
299
 
    if (tokens[0] && tokens[1])
300
 
        string = g_strstrip(tokens[1]);
301
 
    else
302
 
        string = "";
303
 
    if (string[0] != '\0')
304
 
        gtk_label_set_text_with_mnemonic(GTK_LABEL(widget), string);
305
 
    else
306
 
        gtk_widget_hide(widget);
307
 
 
308
 
    g_strfreev(tokens);
309
 
 
310
 
    gtk_builder_connect_signals(dialog->priv->ui, dialog);
311
 
 
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);
318
 
 
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);
325
 
}
326
 
 
327
 
 
328
 
static void
329
 
math_preferences_set_property(GObject      *object,
330
 
                              guint         prop_id,
331
 
                              const GValue *value,
332
 
                              GParamSpec   *pspec)
333
 
{
334
 
    MathPreferencesDialog *self;
335
 
 
336
 
    self = MATH_PREFERENCES(object);
337
 
 
338
 
    switch (prop_id) {
339
 
    case PROP_EQUATION:
340
 
        self->priv->equation = g_value_get_object(value);
341
 
        create_gui(self);
342
 
        break;
343
 
    default:
344
 
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
345
 
        break;
346
 
    }
347
 
}
348
 
 
349
 
 
350
 
static void
351
 
math_preferences_get_property(GObject    *object,
352
 
                              guint       prop_id,
353
 
                              GValue     *value,
354
 
                              GParamSpec *pspec)
355
 
{
356
 
    MathPreferencesDialog *self;
357
 
 
358
 
    self = MATH_PREFERENCES(object);
359
 
 
360
 
    switch (prop_id) {
361
 
    case PROP_EQUATION:
362
 
        g_value_set_object(value, self->priv->equation);
363
 
        break;
364
 
    default:
365
 
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
366
 
        break;
367
 
    }
368
 
}
369
 
 
370
 
 
371
 
static void
372
 
math_preferences_class_init(MathPreferencesDialogClass *klass)
373
 
{
374
 
    GObjectClass *object_class = G_OBJECT_CLASS(klass);
375
 
 
376
 
    object_class->get_property = math_preferences_get_property;
377
 
    object_class->set_property = math_preferences_set_property;
378
 
 
379
 
    g_type_class_add_private(klass, sizeof(MathPreferencesDialogPrivate));
380
 
 
381
 
    g_object_class_install_property(object_class,
382
 
                                    PROP_EQUATION,
383
 
                                    g_param_spec_object("equation",
384
 
                                                        "equation",
385
 
                                                        "Equation being configured",
386
 
                                                        math_equation_get_type(),
387
 
                                                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
388
 
}
389
 
 
390
 
 
391
 
static void
392
 
math_preferences_init(MathPreferencesDialog *dialog)
393
 
{
394
 
    dialog->priv = G_TYPE_INSTANCE_GET_PRIVATE(dialog, math_preferences_get_type(), MathPreferencesDialogPrivate);
395
 
}