1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
|
/*
* Copyright (C) 2008-2011 Robert Ancell
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version. See http://www.gnu.org/copyleft/gpl.html the full text of the
* license.
*/
#include <glib/gi18n.h>
#include "math-converter.h"
#include "unit-manager.h"
#include "currency-manager.h"
enum {
CHANGED,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0, };
struct MathConverterPrivate
{
MathEquation *equation;
gchar *category;
GtkWidget *from_combo;
GtkWidget *to_combo;
GtkWidget *result_label;
};
G_DEFINE_TYPE_WITH_PRIVATE (MathConverter, math_converter, GTK_TYPE_BOX);
static void display_changed_cb(MathEquation *equation, GParamSpec *spec, MathConverter *converter);
static void update_from_model(MathConverter *converter);
MathConverter *
math_converter_new(MathEquation *equation)
{
MathConverter *converter = g_object_new(math_converter_get_type(), NULL);
converter->priv->equation = g_object_ref(equation);
g_signal_connect(converter->priv->equation, "notify::display", G_CALLBACK(display_changed_cb), converter);
update_from_model(converter);
return converter;
}
static gboolean
convert_equation(MathConverter *converter, const MPNumber *x, MPNumber *z)
{
GtkTreeIter from_iter, to_iter;
UnitCategory *category = NULL;
Unit *source_unit = NULL, *target_unit = NULL;
gboolean result;
if (!gtk_combo_box_get_active_iter(GTK_COMBO_BOX(converter->priv->from_combo), &from_iter) ||
!gtk_combo_box_get_active_iter(GTK_COMBO_BOX(converter->priv->to_combo), &to_iter))
return FALSE;
gtk_tree_model_get(gtk_combo_box_get_model(GTK_COMBO_BOX(converter->priv->from_combo)), &from_iter, 1, &category, 2, &source_unit, -1);
gtk_tree_model_get(gtk_combo_box_get_model(GTK_COMBO_BOX(converter->priv->to_combo)), &to_iter, 2, &target_unit, -1);
result = unit_category_convert(category, x, source_unit, target_unit, z);
if (category)
g_object_unref(category);
if (source_unit)
g_object_unref(source_unit);
if (target_unit)
g_object_unref(target_unit);
return result;
}
static void
update_result_label(MathConverter *converter)
{
MPNumber x = mp_new();
MPNumber z = mp_new();
gboolean enabled;
if (!converter->priv->result_label)
return;
if (math_equation_get_number(converter->priv->equation, &x))
enabled = convert_equation(converter, &x, &z);
else
enabled = FALSE;
gtk_widget_set_sensitive(converter->priv->result_label, enabled);
if (enabled) {
gchar *source_text, *target_text, *label;
Unit *source_unit, *target_unit;
math_converter_get_conversion(converter, &source_unit, &target_unit);
source_text = unit_format(source_unit, &x);
target_text = unit_format(target_unit, &z);
label = g_strdup_printf("%s = %s", source_text, target_text);
gtk_label_set_text(GTK_LABEL(converter->priv->result_label), label);
g_free(source_text);
g_free(target_text);
g_free(label);
g_object_unref(source_unit);
g_object_unref(target_unit);
}
mp_clear(&x);
mp_clear(&z);
}
static void
display_changed_cb(MathEquation *equation, GParamSpec *spec, MathConverter *converter)
{
update_result_label(converter);
}
static void
update_from_model(MathConverter *converter)
{
GtkTreeStore *from_model;
from_model = gtk_tree_store_new(3, G_TYPE_STRING, G_TYPE_OBJECT, G_TYPE_OBJECT);
if (converter->priv->category == NULL) {
const GList *categories, *iter;
categories = unit_manager_get_categories(unit_manager_get_default());
for (iter = categories; iter; iter = iter->next) {
UnitCategory *category = iter->data;
GtkTreeIter parent;
const GList *unit_iter;
gtk_tree_store_append(from_model, &parent, NULL);
gtk_tree_store_set(from_model, &parent, 0, unit_category_get_display_name(category), 1, category, -1);
for (unit_iter = unit_category_get_units(category); unit_iter; unit_iter = unit_iter->next) {
Unit *unit = unit_iter->data;
GtkTreeIter iter_new_row;
gtk_tree_store_append(from_model, &iter_new_row, &parent);
gtk_tree_store_set(from_model, &iter_new_row, 0, unit_get_display_name(unit), 1, category, 2, unit, -1);
}
}
}
else {
UnitCategory *category;
const GList *unit_iter;
category = unit_manager_get_category(unit_manager_get_default(), converter->priv->category);
for (unit_iter = unit_category_get_units(category); unit_iter; unit_iter = unit_iter->next) {
Unit *unit = unit_iter->data;
GtkTreeIter iter;
gtk_tree_store_append(from_model, &iter, NULL);
gtk_tree_store_set(from_model, &iter, 0, unit_get_display_name(unit), 1, category, 2, unit, -1);
}
}
gtk_combo_box_set_model(GTK_COMBO_BOX(converter->priv->from_combo), GTK_TREE_MODEL(from_model));
}
void
math_converter_set_category(MathConverter *converter, const gchar *category)
{
g_return_if_fail (converter != NULL);
if (category == NULL && converter->priv->category == NULL)
return;
if (category != NULL && converter->priv->category != NULL && strcmp(category, converter->priv->category) == 0)
return;
g_free(converter->priv->category);
converter->priv->category = g_strdup(category);
update_from_model(converter);
}
const gchar *
math_converter_get_category(MathConverter *converter)
{
g_return_val_if_fail (converter != NULL, NULL);
return converter->priv->category;
}
static gboolean
iter_is_unit(GtkTreeModel *model, GtkTreeIter *iter, const Unit *unit)
{
Unit *u;
gtk_tree_model_get(model, iter, 2, &u, -1);
if (!u)
return FALSE;
g_object_unref(u);
if (u == unit)
return TRUE;
return FALSE;
}
static gboolean
set_active_unit(GtkComboBox *combo, GtkTreeIter *iter, Unit *unit)
{
GtkTreeModel *model;
GtkTreeIter child_iter;
model = gtk_combo_box_get_model(combo);
if (iter && iter_is_unit(model, iter, unit)) {
gtk_combo_box_set_active_iter(combo, iter);
return TRUE;
}
if (!gtk_tree_model_iter_children(model, &child_iter, iter))
return FALSE;
do {
if (set_active_unit(combo, &child_iter, unit))
return TRUE;
} while (gtk_tree_model_iter_next(model, &child_iter));
return FALSE;
}
void
math_converter_set_conversion(MathConverter *converter, /*const gchar *category,*/ const gchar *unit_a, const gchar *unit_b)
{
Unit *ua;
Unit *ub;
g_return_if_fail (converter != NULL);
g_return_if_fail (unit_a != NULL);
g_return_if_fail (unit_b != NULL);
ua = unit_manager_get_unit_by_name(unit_manager_get_default(), unit_a);
ub = unit_manager_get_unit_by_name(unit_manager_get_default(), unit_b);
if (!ua || !ub)
{
GtkTreeModel *model;
GtkTreeIter iter;
/* Select the first unit */
model = gtk_combo_box_get_model(GTK_COMBO_BOX(converter->priv->from_combo));
if (gtk_tree_model_get_iter_first(model, &iter)) {
GtkTreeIter child_iter;
while (gtk_tree_model_iter_children(model, &child_iter, &iter))
iter = child_iter;
gtk_combo_box_set_active_iter(GTK_COMBO_BOX(converter->priv->from_combo), &iter);
}
return;
}
set_active_unit(GTK_COMBO_BOX(converter->priv->from_combo), NULL, ua);
set_active_unit(GTK_COMBO_BOX(converter->priv->to_combo), NULL, ub);
}
void
math_converter_get_conversion(MathConverter *converter, Unit **from_unit, Unit **to_unit)
{
GtkTreeIter from_iter, to_iter;
g_return_if_fail (converter != NULL);
g_return_if_fail (from_unit != NULL);
g_return_if_fail (to_unit != NULL);
gtk_combo_box_get_active_iter(GTK_COMBO_BOX(converter->priv->from_combo), &from_iter);
gtk_combo_box_get_active_iter(GTK_COMBO_BOX(converter->priv->to_combo), &to_iter);
gtk_tree_model_get(gtk_combo_box_get_model(GTK_COMBO_BOX(converter->priv->from_combo)), &from_iter, 2, from_unit, -1);
gtk_tree_model_get(gtk_combo_box_get_model(GTK_COMBO_BOX(converter->priv->to_combo)), &to_iter, 2, to_unit, -1);
}
static void
math_converter_class_init(MathConverterClass *klass)
{
signals[CHANGED] =
g_signal_new("changed",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (MathConverterClass, changed),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
}
static void
from_combobox_changed_cb(GtkWidget *combo, MathConverter *converter)
{
GtkTreeModel *model;
GtkTreeIter iter;
UnitCategory *category;
Unit *unit;
const GList *unit_iter;
model = gtk_combo_box_get_model(GTK_COMBO_BOX(combo));
if (!gtk_combo_box_get_active_iter(GTK_COMBO_BOX(combo), &iter))
return;
gtk_tree_model_get(model, &iter, 1, &category, 2, &unit, -1);
/* Set the to combobox to be the list of units can be converted to */
model = GTK_TREE_MODEL(gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_OBJECT, G_TYPE_OBJECT));
for (unit_iter = unit_category_get_units(category); unit_iter; unit_iter = unit_iter->next) {
Unit *u = unit_iter->data;
if (u == unit)
continue;
gtk_list_store_append(GTK_LIST_STORE(model), &iter);
gtk_list_store_set(GTK_LIST_STORE(model), &iter, 0, unit_get_display_name(u), 1, category, 2, u, -1);
}
gtk_combo_box_set_model(GTK_COMBO_BOX(converter->priv->to_combo), model);
/* Select the first possible unit */
gtk_combo_box_set_active(GTK_COMBO_BOX(converter->priv->to_combo), 0);
g_object_unref(category);
g_object_unref(unit);
}
static void
to_combobox_changed_cb(GtkWidget *combo, MathConverter *converter)
{
/* Conversion must have changed */
update_result_label(converter);
g_signal_emit(converter, signals[CHANGED], 0);
}
static void
from_cell_data_func(GtkCellLayout *cell_layout,
GtkCellRenderer *cell,
GtkTreeModel *tree_model,
GtkTreeIter *iter,
gpointer data)
{
g_object_set(cell, "sensitive", !gtk_tree_model_iter_has_child(tree_model, iter), NULL);
}
static void
currency_updated_cb(CurrencyManager *manager, MathConverter *converter)
{
update_result_label(converter);
}
static void
swap_button_clicked_cb(GtkButton *button, MathConverter *converter)
{
Unit *from_unit, *to_unit;
MPNumber x = mp_new();
MPNumber z = mp_new();
if (math_equation_get_number(converter->priv->equation, &x) &&
convert_equation(converter, &x, &z))
math_equation_set_number(converter->priv->equation, &z);
math_converter_get_conversion(converter, &from_unit, &to_unit);
set_active_unit(GTK_COMBO_BOX(converter->priv->from_combo), NULL, to_unit);
set_active_unit(GTK_COMBO_BOX(converter->priv->to_combo), NULL, from_unit);
update_result_label(converter);
g_object_unref(from_unit);
g_object_unref(to_unit);
mp_clear(&x);
mp_clear(&z);
}
static void
math_converter_init(MathConverter *converter)
{
GtkWidget *hbox, *label, *swap_button;
GtkCellRenderer *renderer;
converter->priv = math_converter_get_instance_private (converter);
gtk_box_set_spacing(GTK_BOX(converter), 6);
gtk_orientable_set_orientation (GTK_ORIENTABLE (converter),
GTK_ORIENTATION_HORIZONTAL);
hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
gtk_widget_show(hbox);
gtk_box_pack_start(GTK_BOX(converter), hbox, FALSE, TRUE, 0);
converter->priv->from_combo = gtk_combo_box_new ();
renderer = gtk_cell_renderer_text_new();
gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(converter->priv->from_combo), renderer, TRUE);
gtk_cell_layout_add_attribute(GTK_CELL_LAYOUT(converter->priv->from_combo), renderer, "text", 0);
gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(converter->priv->from_combo),
renderer,
from_cell_data_func,
NULL, NULL);
g_signal_connect(converter->priv->from_combo, "changed", G_CALLBACK(from_combobox_changed_cb), converter);
gtk_widget_show(converter->priv->from_combo);
gtk_box_pack_start(GTK_BOX(hbox), converter->priv->from_combo, FALSE, TRUE, 0);
label = gtk_label_new(/* Label that is displayed between the two conversion combo boxes, e.g. "[degrees] in [radians]" */
_(" in "));
gtk_widget_show(label);
gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, TRUE, 5);
converter->priv->to_combo = gtk_combo_box_new();
renderer = gtk_cell_renderer_text_new();
gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(converter->priv->to_combo), renderer, TRUE);
gtk_cell_layout_add_attribute(GTK_CELL_LAYOUT(converter->priv->to_combo), renderer, "text", 0);
g_signal_connect(converter->priv->to_combo, "changed", G_CALLBACK(to_combobox_changed_cb), converter);
gtk_widget_show(converter->priv->to_combo);
gtk_box_pack_start(GTK_BOX(hbox), converter->priv->to_combo, FALSE, TRUE, 0);
swap_button = gtk_button_new_with_label ("⇆");
gtk_widget_set_tooltip_text (swap_button,
/* Tooltip for swap conversion button */
_("Switch conversion units"));
gtk_button_set_relief (GTK_BUTTON (swap_button), GTK_RELIEF_NONE);
g_signal_connect (swap_button, "clicked", G_CALLBACK (swap_button_clicked_cb), converter);
gtk_widget_show(swap_button);
gtk_box_pack_start(GTK_BOX(hbox), swap_button, FALSE, TRUE, 0);
converter->priv->result_label = gtk_label_new("");
gtk_widget_set_halign (converter->priv->result_label, GTK_ALIGN_END);
gtk_widget_set_sensitive(converter->priv->result_label, FALSE);
gtk_widget_show(converter->priv->result_label);
gtk_box_pack_start(GTK_BOX(converter), converter->priv->result_label, TRUE, TRUE, 0);
g_signal_connect(currency_manager_get_default(), "updated", G_CALLBACK(currency_updated_cb), converter);
}
|