1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2007, 2010, 2011 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_table_attach_defaults (GTK_TABLE (kp->table),
167 gtk_widget_set_size_request (*button,
168 30 * rows / (float) cols,
169 30 * cols / (float) rows);
171 g_hash_table_insert (kp->frag_table, *button,
172 (void *) keypad_insert_text[(button - &kp->digit[0])] );
174 g_signal_connect (*button, "clicked",
175 G_CALLBACK (button_click), kp);
177 gtk_widget_show (*button);
181 /* Return a new button with CODE as the unicode character for its label */
182 static inline GtkWidget *
183 button_new_from_unicode (gunichar code)
185 char s[6] = {0,0,0,0,0,0};
187 g_unichar_to_utf8 (code, s);
189 return gtk_button_new_with_label (s);
193 /* Callback which occurs when the mouse enters the widget.
194 It sets or unsets the focus.
197 enter_leave_notify (GtkWidget *widget,
198 GdkEventCrossing *event,
201 /* Do nothing if we're just moving between the widget and
203 if (event->detail == GDK_NOTIFY_INFERIOR)
206 if (event->type == GDK_ENTER_NOTIFY)
207 gtk_widget_grab_focus (widget);
213 key_release_callback (GtkWidget *widget,
217 if ( ! gtk_widget_has_focus (widget))
220 switch (event->keyval)
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, ">");
232 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "<");
236 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "=");
238 case GDK_KP_Multiply :
240 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "*");
244 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "+");
246 case GDK_KP_Subtract :
248 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "-");
250 case GDK_KP_Decimal :
252 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, ".");
256 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "/");
260 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "0");
264 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "1");
268 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "2");
272 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "3");
276 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "4");
280 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "5");
284 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "6");
288 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "7");
292 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "8");
296 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "9");
307 psppire_keypad_init (PsppireKeypad *kp)
310 const int digit_voffset = 0;
311 const int digit_hoffset = 3;
313 gtk_widget_set_can_focus (GTK_WIDGET (kp), TRUE);
315 kp->dispose_has_run = FALSE;
317 g_signal_connect (kp, "enter-notify-event", G_CALLBACK (enter_leave_notify),
320 g_signal_connect (kp, "leave-notify-event", G_CALLBACK (enter_leave_notify),
323 g_signal_connect (kp, "key-release-event", G_CALLBACK (key_release_callback),
326 kp->frag_table = g_hash_table_new (g_direct_hash, g_direct_equal);
328 kp->table = gtk_table_new (rows, cols, TRUE);
330 /* Buttons for the digits */
331 for (i = 0; i < 10; i++)
335 g_snprintf (buf, 5, "%d", i);
336 kp->digit[i] = gtk_button_new_with_label (buf);
339 add_button (kp, &kp->digit[i],
340 digit_hoffset + 0, digit_hoffset + 2,
341 digit_voffset + 3, digit_voffset + 4);
343 add_button (kp, &kp->digit[i],
344 digit_hoffset + j % 3, digit_hoffset + j % 3 + 1,
345 digit_voffset + 2 - (j / 3),
346 digit_voffset + 2 - (j / 3) + 1);
349 /* ... all the other buttons */
351 kp->dot = button_new_from_unicode (0xB7); /* MIDDLE DOT */
352 add_button (kp, &kp->dot, digit_hoffset + 2,
357 kp->plus = gtk_button_new_with_label ("+");
358 add_button (kp, &kp->plus, 0, 1,
361 kp->minus = button_new_from_unicode (0x2212); /* MINUS SIGN */
362 add_button (kp, &kp->minus, 0, 1,
365 kp->star = button_new_from_unicode (0xD7); /* MULTIPLICATION SIGN */
366 add_button (kp, &kp->star, 0, 1,
369 kp->slash = button_new_from_unicode (0xF7); /* DIVISION SIGN */
370 add_button (kp, &kp->slash, 0, 1,
376 g_markup_printf_escaped ("<span style=\"italic\">x<sup>y</sup></span>");
378 label = gtk_label_new ("**");
380 gtk_label_set_markup (GTK_LABEL (label), markup);
383 kp->star_star = gtk_button_new ();
384 gtk_container_add (GTK_CONTAINER (kp->star_star), label);
386 gtk_widget_show (label);
388 add_button (kp, &kp->star_star,
394 kp->gt = button_new_from_unicode (0x3E); /* GREATER-THAN SIGN*/
395 add_button (kp, &kp->gt, 2, 3,
398 kp->lt = button_new_from_unicode (0x3C); /* LESS-THAN SIGN*/
399 add_button (kp, &kp->lt, 1, 2,
402 kp->ge = button_new_from_unicode (0x2265); /* GREATER-THAN OR EQUAL */
403 add_button (kp, &kp->ge, 2, 3,
406 kp->le = button_new_from_unicode (0x2264); /* LESS-THAN OR EQUAL */
407 add_button (kp, &kp->le, 1, 2,
410 kp->neq = button_new_from_unicode (0x2260); /* NOT EQUAL */
411 add_button (kp, &kp->neq, 2, 3,
414 kp->eq = gtk_button_new_with_label ("=");
415 add_button (kp, &kp->eq, 1, 2,
418 kp->parentheses = gtk_button_new_with_label ("()");
419 add_button (kp, &kp->parentheses, 2, 3,
423 kp->delete = gtk_button_new_with_label ("Delete");
424 add_button (kp, &kp->delete, 3, 6,
429 kp->and = button_new_from_unicode (0x2227); /* LOGICAL AND */
430 add_button (kp, &kp->and, 1, 2,
434 kp->or = button_new_from_unicode (0x2228); /* LOGICAL OR */
435 add_button (kp, &kp->or, 2, 3,
439 kp->not = button_new_from_unicode (0xAC); /* NOT SIGN */
440 add_button (kp, &kp->not, 1, 2,
445 g_object_set (G_OBJECT (kp->table), "row-spacing", 5, NULL);
446 g_object_set (G_OBJECT (kp->table), "column-spacing", 5, NULL);
448 gtk_container_add (GTK_CONTAINER (kp), kp->table);
449 gtk_widget_show (kp->table);
451 gtk_widget_add_events (GTK_WIDGET (kp),
452 GDK_KEY_RELEASE_MASK |
453 GDK_LEAVE_NOTIFY_MASK |
454 GDK_ENTER_NOTIFY_MASK |
455 GDK_FOCUS_CHANGE_MASK);
461 psppire_keypad_new (void)
463 return GTK_WIDGET (g_object_new (psppire_keypad_get_type (), NULL));