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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
|
/*
* 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 <string.h>
#include <glib/gi18n.h>
#include <gdk/gdkkeysyms.h>
#include "math-display.h"
enum {
PROP_0,
PROP_EQUATION
};
struct MathDisplayPrivate
{
/* Equation being displayed */
MathEquation *equation;
/* Display widget */
GtkWidget *text_view;
/* Buffer that shows errors etc */
GtkTextBuffer *info_buffer;
/* Spinner widget that shows if we're calculating a response */
GtkWidget *spinner;
};
G_DEFINE_TYPE (MathDisplay, math_display, GTK_TYPE_VIEWPORT);
#define GET_WIDGET(ui, name) GTK_WIDGET(gtk_builder_get_object(ui, name))
MathDisplay *
math_display_new()
{
return g_object_new(math_display_get_type(), "equation", math_equation_new(), NULL);
}
MathDisplay *
math_display_new_with_equation(MathEquation *equation)
{
return g_object_new(math_display_get_type(), "equation", equation, NULL);
}
MathEquation *
math_display_get_equation(MathDisplay *display)
{
return display->priv->equation;
}
static gboolean
display_key_press_cb(GtkWidget *widget, GdkEventKey *event, MathDisplay *display)
{
int state;
guint32 c;
guint new_keyval = 0;
/* Treat keypad keys as numbers even when numlock is off */
switch(event->keyval)
{
case GDK_KEY_KP_Delete: /* Period without numlock */
new_keyval = GDK_KEY_KP_Decimal;
break;
case GDK_KEY_KP_Insert:
new_keyval = GDK_KEY_0;
break;
case GDK_KEY_KP_End:
new_keyval = GDK_KEY_1;
break;
case GDK_KEY_KP_Down:
new_keyval = GDK_KEY_2;
break;
case GDK_KEY_KP_Page_Down:
new_keyval = GDK_KEY_3;
break;
case GDK_KEY_KP_Left:
new_keyval = GDK_KEY_4;
break;
case GDK_KEY_KP_Begin: /* This is apparently what "5" does when numlock is off. */
new_keyval = GDK_KEY_5;
break;
case GDK_KEY_KP_Right:
new_keyval = GDK_KEY_6;
break;
case GDK_KEY_KP_Home:
new_keyval = GDK_KEY_7;
break;
case GDK_KEY_KP_Up:
new_keyval = GDK_KEY_8;
break;
case GDK_KEY_KP_Page_Up:
new_keyval = GDK_KEY_9;
break;
}
if (new_keyval) {
gboolean result;
GdkEvent *new_event;
new_event = gdk_event_copy((GdkEvent *)event);
((GdkEventKey *)new_event)->keyval = new_keyval;
g_signal_emit_by_name(widget, "key-press-event", new_event, &result);
gdk_event_free(new_event);
return result;
}
state = event->state & (GDK_CONTROL_MASK | GDK_MOD1_MASK);
c = gdk_keyval_to_unicode(event->keyval);
/* Solve on enter */
if (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_KP_Enter) {
math_equation_solve(display->priv->equation);
return TRUE;
}
/* Clear on escape */
if ((event->keyval == GDK_KEY_Escape && state == 0) ||
(event->keyval == GDK_KEY_BackSpace && state == GDK_CONTROL_MASK) ||
(event->keyval == GDK_KEY_Delete && state == GDK_SHIFT_MASK)) {
math_equation_clear(display->priv->equation);
return TRUE;
}
/* Numeric keypad will often insert '.' regardless of locale */
if (event->keyval == GDK_KEY_KP_Decimal) {
math_equation_insert_numeric_point(display->priv->equation);
return TRUE;
}
/* Substitute */
if (state == 0) {
if (c == '*') {
math_equation_insert(display->priv->equation, "×");
return TRUE;
}
if (c == '/') {
math_equation_insert(display->priv->equation, "÷");
return TRUE;
}
if (c == '-') {
math_equation_insert_subtract(display->priv->equation);
return TRUE;
}
}
/* Shortcuts */
if (state == GDK_CONTROL_MASK) {
switch(event->keyval)
{
case GDK_KEY_bracketleft:
math_equation_insert(display->priv->equation, "⌈");
return TRUE;
case GDK_KEY_bracketright:
math_equation_insert(display->priv->equation, "⌉");
return TRUE;
case GDK_KEY_e:
math_equation_insert_exponent(display->priv->equation);
return TRUE;
case GDK_KEY_f:
math_equation_factorize(display->priv->equation);
return TRUE;
case GDK_KEY_i:
math_equation_insert(display->priv->equation, "⁻¹");
return TRUE;
case GDK_KEY_p:
math_equation_insert(display->priv->equation, "π");
return TRUE;
case GDK_KEY_r:
math_equation_insert(display->priv->equation, "√");
return TRUE;
case GDK_KEY_u:
math_equation_insert(display->priv->equation, "µ");
return TRUE;
case GDK_KEY_minus:
math_equation_insert(display->priv->equation, "⁻");
return TRUE;
case GDK_KEY_apostrophe:
math_equation_insert(display->priv->equation, "°");
return TRUE;
}
}
if (state == GDK_MOD1_MASK) {
switch(event->keyval)
{
case GDK_KEY_bracketleft:
math_equation_insert(display->priv->equation, "⌊");
return TRUE;
case GDK_KEY_bracketright:
math_equation_insert(display->priv->equation, "⌋");
return TRUE;
}
}
if (state == GDK_CONTROL_MASK || math_equation_get_number_mode(display->priv->equation) == SUPERSCRIPT) {
switch(event->keyval)
{
case GDK_KEY_0:
case GDK_KEY_KP_0:
math_equation_insert(display->priv->equation, "⁰");
return TRUE;
case GDK_KEY_1:
case GDK_KEY_KP_1:
math_equation_insert(display->priv->equation, "¹");
return TRUE;
case GDK_KEY_2:
case GDK_KEY_KP_2:
math_equation_insert(display->priv->equation, "²");
return TRUE;
case GDK_KEY_3:
case GDK_KEY_KP_3:
math_equation_insert(display->priv->equation, "³");
return TRUE;
case GDK_KEY_4:
case GDK_KEY_KP_4:
math_equation_insert(display->priv->equation, "⁴");
return TRUE;
case GDK_KEY_5:
case GDK_KEY_KP_5:
math_equation_insert(display->priv->equation, "⁵");
return TRUE;
case GDK_KEY_6:
case GDK_KEY_KP_6:
math_equation_insert(display->priv->equation, "⁶");
return TRUE;
case GDK_KEY_7:
case GDK_KEY_KP_7:
math_equation_insert(display->priv->equation, "⁷");
return TRUE;
case GDK_KEY_8:
case GDK_KEY_KP_8:
math_equation_insert(display->priv->equation, "⁸");
return TRUE;
case GDK_KEY_9:
case GDK_KEY_KP_9:
math_equation_insert(display->priv->equation, "⁹");
return TRUE;
}
}
else if (state == GDK_MOD1_MASK || math_equation_get_number_mode(display->priv->equation) == SUBSCRIPT) {
switch(event->keyval)
{
case GDK_KEY_0:
case GDK_KEY_KP_0:
math_equation_insert(display->priv->equation, "₀");
return TRUE;
case GDK_KEY_1:
case GDK_KEY_KP_1:
math_equation_insert(display->priv->equation, "₁");
return TRUE;
case GDK_KEY_2:
case GDK_KEY_KP_2:
math_equation_insert(display->priv->equation, "₂");
return TRUE;
case GDK_KEY_3:
case GDK_KEY_KP_3:
math_equation_insert(display->priv->equation, "₃");
return TRUE;
case GDK_KEY_4:
case GDK_KEY_KP_4:
math_equation_insert(display->priv->equation, "₄");
return TRUE;
case GDK_KEY_5:
case GDK_KEY_KP_5:
math_equation_insert(display->priv->equation, "₅");
return TRUE;
case GDK_KEY_6:
case GDK_KEY_KP_6:
math_equation_insert(display->priv->equation, "₆");
return TRUE;
case GDK_KEY_7:
case GDK_KEY_KP_7:
math_equation_insert(display->priv->equation, "₇");
return TRUE;
case GDK_KEY_8:
case GDK_KEY_KP_8:
math_equation_insert(display->priv->equation, "₈");
return TRUE;
case GDK_KEY_9:
case GDK_KEY_KP_9:
math_equation_insert(display->priv->equation, "₉");
return TRUE;
}
}
return FALSE;
}
static gboolean
key_press_cb(MathDisplay *display, GdkEventKey *event)
{
gboolean result;
g_signal_emit_by_name(display->priv->text_view, "key-press-event", event, &result);
return result;
}
static void
status_changed_cb(MathEquation *equation, GParamSpec *spec, MathDisplay *display)
{
gtk_text_buffer_set_text(display->priv->info_buffer, math_equation_get_status(equation), -1);
if (math_equation_in_solve(equation) && !gtk_widget_get_visible(display->priv->spinner)) {
gtk_widget_show(display->priv->spinner);
gtk_spinner_start(GTK_SPINNER(display->priv->spinner));
}
else if (!math_equation_in_solve(equation) && gtk_widget_get_visible(display->priv->spinner)) {
gtk_widget_hide(display->priv->spinner);
gtk_spinner_stop(GTK_SPINNER(display->priv->spinner));
}
}
static void
create_gui(MathDisplay *display)
{
GtkWidget *info_view, *info_box, *main_box;
PangoFontDescription *font_desc;
int i;
GtkStyle *style;
#if GTK_CHECK_VERSION (3, 2, 0)
main_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
#else
main_box = gtk_vbox_new(FALSE, 0);
#endif
gtk_container_add(GTK_CONTAINER(display), main_box);
g_signal_connect(display, "key-press-event", G_CALLBACK(key_press_cb), display);
display->priv->text_view = gtk_text_view_new_with_buffer(GTK_TEXT_BUFFER(display->priv->equation));
gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(display->priv->text_view), GTK_WRAP_WORD);
gtk_text_view_set_accepts_tab(GTK_TEXT_VIEW(display->priv->text_view), FALSE);
gtk_text_view_set_pixels_above_lines(GTK_TEXT_VIEW(display->priv->text_view), 8);
gtk_text_view_set_pixels_below_lines(GTK_TEXT_VIEW(display->priv->text_view), 2);
/* TEMP: Disabled for now as GTK+ doesn't properly render a right aligned right margin, see bug #482688 */
/*gtk_text_view_set_right_margin(GTK_TEXT_VIEW(display->priv->text_view), 6);*/
gtk_text_view_set_justification(GTK_TEXT_VIEW(display->priv->text_view), GTK_JUSTIFY_RIGHT);
gtk_widget_ensure_style(display->priv->text_view);
font_desc = pango_font_description_copy(gtk_widget_get_style(display->priv->text_view)->font_desc);
pango_font_description_set_size(font_desc, 16 * PANGO_SCALE);
gtk_widget_modify_font(display->priv->text_view, font_desc);
pango_font_description_free(font_desc);
gtk_widget_set_name(display->priv->text_view, "displayitem");
atk_object_set_role(gtk_widget_get_accessible(display->priv->text_view), ATK_ROLE_EDITBAR);
//FIXME:<property name="AtkObject::accessible-description" translatable="yes" comments="Accessible description for the area in which results are displayed">Result Region</property>
g_signal_connect(display->priv->text_view, "key-press-event", G_CALLBACK(display_key_press_cb), display);
gtk_box_pack_start(GTK_BOX(main_box), display->priv->text_view, TRUE, TRUE, 0);
#if GTK_CHECK_VERSION (3, 2, 0)
info_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6);
#else
info_box = gtk_hbox_new(FALSE, 6);
#endif
gtk_box_pack_start(GTK_BOX(main_box), info_box, FALSE, TRUE, 0);
info_view = gtk_text_view_new();
gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(info_view), GTK_WRAP_WORD);
gtk_widget_set_can_focus(info_view, TRUE); // FIXME: This should be FALSE but it locks the cursor inside the main view for some reason
gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(info_view), FALSE); // FIXME: Just here so when incorrectly gets focus doesn't look editable
gtk_text_view_set_editable(GTK_TEXT_VIEW(info_view), FALSE);
gtk_text_view_set_justification(GTK_TEXT_VIEW(info_view), GTK_JUSTIFY_RIGHT);
/* TEMP: Disabled for now as GTK+ doesn't properly render a right aligned right margin, see bug #482688 */
/*gtk_text_view_set_right_margin(GTK_TEXT_VIEW(info_view), 6);*/
gtk_box_pack_start(GTK_BOX(info_box), info_view, TRUE, TRUE, 0);
display->priv->info_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(info_view));
display->priv->spinner = gtk_spinner_new();
gtk_box_pack_end(GTK_BOX(info_box), display->priv->spinner, FALSE, FALSE, 0);
style = gtk_widget_get_style(info_view);
for (i = 0; i < 5; i++) {
gtk_widget_modify_bg(GTK_WIDGET(display), i, &style->base[i]);
}
gtk_widget_show(info_box);
gtk_widget_show(info_view);
gtk_widget_show(display->priv->text_view);
gtk_widget_show(main_box);
g_signal_connect(display->priv->equation, "notify::status", G_CALLBACK(status_changed_cb), display);
status_changed_cb(display->priv->equation, NULL, display);
}
static void
math_display_set_property(GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
MathDisplay *self;
self = MATH_DISPLAY(object);
switch (prop_id) {
case PROP_EQUATION:
self->priv->equation = g_value_get_object(value);
create_gui(self);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void
math_display_get_property(GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
MathDisplay *self;
self = MATH_DISPLAY(object);
switch (prop_id) {
case PROP_EQUATION:
g_value_set_object(value, self->priv->equation);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void
math_display_class_init(MathDisplayClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS(klass);
object_class->get_property = math_display_get_property;
object_class->set_property = math_display_set_property;
g_type_class_add_private(klass, sizeof(MathDisplayPrivate));
g_object_class_install_property(object_class,
PROP_EQUATION,
g_param_spec_object("equation",
"equation",
"Equation being displayed",
math_equation_get_type(),
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
}
static void
math_display_init(MathDisplay *display)
{
display->priv = G_TYPE_INSTANCE_GET_PRIVATE(display, math_display_get_type(), MathDisplayPrivate);
}
|