1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2007, 2010, 2011, 2015 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include <gdk/gdkkeysyms.h>
20 #include "psppire-keypad.h"
28 static void psppire_keypad_class_init (PsppireKeypadClass *klass);
29 static void psppire_keypad_init (PsppireKeypad *kp);
31 static guint keypad_signals [n_SIGNALS] = { 0 };
34 psppire_keypad_get_type (void)
36 static GType kp_type = 0;
40 static const GTypeInfo kp_info =
42 sizeof (PsppireKeypadClass),
44 NULL, /* base_finalize */
45 (GClassInitFunc) psppire_keypad_class_init,
46 NULL, /* class_finalize */
47 NULL, /* class_data */
48 sizeof (PsppireKeypad),
50 (GInstanceInitFunc) psppire_keypad_init,
53 kp_type = g_type_register_static (GTK_TYPE_EVENT_BOX, "PsppireKeypad",
60 static GObjectClass * parent_class = NULL;
63 psppire_keypad_dispose (GObject *obj)
65 PsppireKeypad *kp = (PsppireKeypad *)obj;
67 if (kp->dispose_has_run)
70 /* Make sure dispose does not run twice. */
71 kp->dispose_has_run = TRUE;
73 g_hash_table_unref (kp->frag_table);
75 /* Chain up to the parent class */
76 G_OBJECT_CLASS (parent_class)->dispose (obj);
80 psppire_keypad_finalize (GObject *obj)
82 /* Chain up to the parent class */
83 G_OBJECT_CLASS (parent_class)->finalize (obj);
87 psppire_keypad_class_init (PsppireKeypadClass *klass)
89 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
91 parent_class = g_type_class_peek_parent (klass);
93 gobject_class->dispose = psppire_keypad_dispose;
94 gobject_class->finalize = psppire_keypad_finalize;
96 keypad_signals[INSERT_SYNTAX] = g_signal_new ("insert-syntax",
97 G_TYPE_FROM_CLASS (klass),
98 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
99 G_STRUCT_OFFSET (PsppireKeypadClass,
103 g_cclosure_marshal_VOID__STRING,
107 keypad_signals[ERASE] = g_signal_new ("erase",
108 G_TYPE_FROM_CLASS (klass),
109 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
110 G_STRUCT_OFFSET (PsppireKeypadClass,
114 g_cclosure_marshal_VOID__VOID,
120 These are the strings that will be arguments to
122 The order of these must correspond
123 to the order of the button declarations
125 static const char * const keypad_insert_text[] = {
126 "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
127 ".", "+", "-", "*", "**", "/", "=", "<>", "<", "<=",
128 ">", ">=", "&", "|", "~", "()", NULL
132 /* Callback for any button click.
133 Emits the "insert-syntax" signal for the keypad,
134 with the string corresponding to the clicked button.
137 button_click (GtkButton *b, PsppireKeypad *kp)
139 const gchar *s = g_hash_table_lookup (kp->frag_table, b);
143 g_signal_emit (kp, keypad_signals [INSERT_SYNTAX], 0, s);
145 g_signal_emit (kp, keypad_signals [ERASE], 0);
148 static const gint cols = 6;
149 static const gint rows = 5;
153 /* Add BUTTON to KP. The top-left corner at X1,Y1, the
154 botton-right corner at X2,Y2 */
156 add_button (PsppireKeypad *kp, GtkWidget **button,
160 g_object_set (G_OBJECT (*button), "focus-on-click", FALSE, NULL);
162 gtk_grid_attach (GTK_GRID(kp->table), *button, x1, y1, x2 - x1, y2 - y1);
164 gtk_widget_set_size_request (*button,
165 30 * rows / (float) cols,
166 30 * cols / (float) rows);
168 g_hash_table_insert (kp->frag_table, *button,
169 (void *) keypad_insert_text[(button - &kp->digit[0])]);
171 g_signal_connect (*button, "clicked",
172 G_CALLBACK (button_click), kp);
174 gtk_widget_show (*button);
178 /* Return a new button with CODE as the unicode character for its label */
179 static inline GtkWidget *
180 button_new_from_unicode (gunichar code)
182 char s[6] = {0,0,0,0,0,0};
184 g_unichar_to_utf8 (code, s);
186 return gtk_button_new_with_label (s);
190 /* Callback which occurs when the mouse enters the widget.
191 It sets or unsets the focus.
194 enter_leave_notify (GtkWidget *widget,
195 GdkEventCrossing *event,
198 /* Do nothing if we're just moving between the widget and
200 if (event->detail == GDK_NOTIFY_INFERIOR)
203 if (event->type == GDK_ENTER_NOTIFY)
204 gtk_widget_grab_focus (widget);
210 key_release_callback (GtkWidget *widget,
214 if (! gtk_widget_has_focus (widget))
217 switch (event->keyval)
220 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "(");
223 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, ")");
226 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, ">");
229 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "<");
231 case GDK_KEY_KP_Equal :
233 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "=");
235 case GDK_KEY_KP_Multiply :
237 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "*");
239 case GDK_KEY_KP_Add :
241 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "+");
243 case GDK_KEY_KP_Subtract :
245 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "-");
247 case GDK_KEY_KP_Decimal :
249 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, ".");
251 case GDK_KEY_KP_Divide :
253 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "/");
257 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "0");
261 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "1");
265 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "2");
269 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "3");
273 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "4");
277 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "5");
281 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "6");
285 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "7");
289 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "8");
293 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "9");
304 psppire_keypad_init (PsppireKeypad *kp)
307 const int digit_voffset = 0;
308 const int digit_hoffset = 3;
310 gtk_widget_set_can_focus (GTK_WIDGET (kp), TRUE);
312 kp->dispose_has_run = FALSE;
314 g_signal_connect (kp, "enter-notify-event", G_CALLBACK (enter_leave_notify),
317 g_signal_connect (kp, "leave-notify-event", G_CALLBACK (enter_leave_notify),
320 g_signal_connect (kp, "key-release-event", G_CALLBACK (key_release_callback),
323 kp->frag_table = g_hash_table_new (g_direct_hash, g_direct_equal);
325 kp->table = gtk_grid_new ();
327 /* Buttons for the digits */
328 for (i = 0; i < 10; i++)
332 g_snprintf (buf, 5, "%d", i);
333 kp->digit[i] = gtk_button_new_with_label (buf);
336 add_button (kp, &kp->digit[i],
337 digit_hoffset + 0, digit_hoffset + 2,
338 digit_voffset + 3, digit_voffset + 4);
340 add_button (kp, &kp->digit[i],
341 digit_hoffset + j % 3, digit_hoffset + j % 3 + 1,
342 digit_voffset + 2 - (j / 3),
343 digit_voffset + 2 - (j / 3) + 1);
346 /* ... all the other buttons */
348 kp->dot = button_new_from_unicode (0xB7); /* MIDDLE DOT */
349 add_button (kp, &kp->dot, digit_hoffset + 2,
354 kp->plus = gtk_button_new_with_label ("+");
355 add_button (kp, &kp->plus, 0, 1,
358 kp->minus = button_new_from_unicode (0x2212); /* MINUS SIGN */
359 add_button (kp, &kp->minus, 0, 1,
362 kp->star = button_new_from_unicode (0xD7); /* MULTIPLICATION SIGN */
363 add_button (kp, &kp->star, 0, 1,
366 kp->slash = button_new_from_unicode (0xF7); /* DIVISION SIGN */
367 add_button (kp, &kp->slash, 0, 1,
373 g_markup_printf_escaped ("<span style=\"italic\">x<sup>y</sup></span>");
375 label = gtk_label_new ("**");
377 gtk_label_set_markup (GTK_LABEL (label), markup);
380 kp->star_star = gtk_button_new ();
381 gtk_container_add (GTK_CONTAINER (kp->star_star), label);
383 gtk_widget_show (label);
385 add_button (kp, &kp->star_star,
391 kp->gt = button_new_from_unicode (0x3E); /* GREATER-THAN SIGN*/
392 add_button (kp, &kp->gt, 2, 3,
395 kp->lt = button_new_from_unicode (0x3C); /* LESS-THAN SIGN*/
396 add_button (kp, &kp->lt, 1, 2,
399 kp->ge = button_new_from_unicode (0x2265); /* GREATER-THAN OR EQUAL */
400 add_button (kp, &kp->ge, 2, 3,
403 kp->le = button_new_from_unicode (0x2264); /* LESS-THAN OR EQUAL */
404 add_button (kp, &kp->le, 1, 2,
407 kp->neq = button_new_from_unicode (0x2260); /* NOT EQUAL */
408 add_button (kp, &kp->neq, 2, 3,
411 kp->eq = gtk_button_new_with_label ("=");
412 add_button (kp, &kp->eq, 1, 2,
415 kp->parentheses = gtk_button_new_with_label ("()");
416 add_button (kp, &kp->parentheses, 2, 3,
420 kp->delete = gtk_button_new_with_label ("Delete");
421 add_button (kp, &kp->delete, 3, 6,
426 kp->and = button_new_from_unicode (0x2227); /* LOGICAL AND */
427 add_button (kp, &kp->and, 1, 2,
431 kp->or = button_new_from_unicode (0x2228); /* LOGICAL OR */
432 add_button (kp, &kp->or, 2, 3,
436 kp->not = button_new_from_unicode (0xAC); /* NOT SIGN */
437 add_button (kp, &kp->not, 1, 2,
442 g_object_set (G_OBJECT (kp->table), "row-spacing", 5, NULL);
443 g_object_set (G_OBJECT (kp->table), "column-spacing", 5, NULL);
445 gtk_container_add (GTK_CONTAINER (kp), kp->table);
446 gtk_widget_show (kp->table);
448 gtk_widget_add_events (GTK_WIDGET (kp),
449 GDK_KEY_RELEASE_MASK |
450 GDK_LEAVE_NOTIFY_MASK |
451 GDK_ENTER_NOTIFY_MASK |
452 GDK_FOCUS_CHANGE_MASK);
458 psppire_keypad_new (void)
460 return GTK_WIDGET (g_object_new (psppire_keypad_get_type (), NULL));