1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2007 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/>. */
18 #include <gtk/gtksignal.h>
19 #include <gtk/gtktable.h>
20 #include <gtk/gtkbutton.h>
21 #include <gtk/gtklabel.h>
22 #include <gdk/gdkkeysyms.h>
23 #include "psppire-keypad.h"
31 static void psppire_keypad_class_init (PsppireKeypadClass *klass);
32 static void psppire_keypad_init (PsppireKeypad *kp);
34 static guint keypad_signals [n_SIGNALS] = { 0 };
37 psppire_keypad_get_type (void)
39 static GType kp_type = 0;
43 static const GTypeInfo kp_info =
45 sizeof (PsppireKeypadClass),
47 NULL, /* base_finalize */
48 (GClassInitFunc) psppire_keypad_class_init,
49 NULL, /* class_finalize */
50 NULL, /* class_data */
51 sizeof (PsppireKeypad),
53 (GInstanceInitFunc) psppire_keypad_init,
56 kp_type = g_type_register_static (GTK_TYPE_EVENT_BOX, "PsppireKeypad",
63 static GObjectClass * parent_class = NULL;
66 psppire_keypad_dispose (GObject *obj)
68 PsppireKeypad *kp = (PsppireKeypad *)obj;
70 if (kp->dispose_has_run)
73 /* Make sure dispose does not run twice. */
74 kp->dispose_has_run = TRUE;
76 g_hash_table_unref (kp->frag_table);
78 /* Chain up to the parent class */
79 G_OBJECT_CLASS (parent_class)->dispose (obj);
83 psppire_keypad_finalize (GObject *obj)
85 /* Chain up to the parent class */
86 G_OBJECT_CLASS (parent_class)->finalize (obj);
90 psppire_keypad_class_init (PsppireKeypadClass *klass)
92 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
94 parent_class = g_type_class_peek_parent (klass);
96 gobject_class->dispose = psppire_keypad_dispose;
97 gobject_class->finalize = psppire_keypad_finalize;
99 keypad_signals[INSERT_SYNTAX] = g_signal_new ("insert-syntax",
100 G_TYPE_FROM_CLASS (klass),
101 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
102 G_STRUCT_OFFSET (PsppireKeypadClass,
106 g_cclosure_marshal_VOID__STRING,
110 keypad_signals[ERASE] = g_signal_new ("erase",
111 G_TYPE_FROM_CLASS (klass),
112 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
113 G_STRUCT_OFFSET (PsppireKeypadClass,
117 g_cclosure_marshal_VOID__VOID,
123 These are the strings that will be arguments to
125 The order of these must correspond
126 to the order of the button declarations
128 static const char * const keypad_insert_text[] = {
129 "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
130 ".", "+", "-", "*", "**", "/", "=", "<>", "<", "<=",
131 ">", ">=", "&", "|", "~", "()", NULL
135 /* Callback for any button click.
136 Emits the "insert-syntax" signal for the keypad,
137 with the string corresponding to the clicked button.
140 button_click (GtkButton *b, PsppireKeypad *kp)
142 const gchar *s = g_hash_table_lookup (kp->frag_table, b);
146 g_signal_emit (kp, keypad_signals [INSERT_SYNTAX], 0, s);
148 g_signal_emit (kp, keypad_signals [ERASE], 0);
151 static const gint cols = 6;
152 static const gint rows = 5;
156 /* Add BUTTON to KP. The top-left corner at X1,Y1, the
157 botton-right corner at X2,Y2 */
159 add_button (PsppireKeypad *kp, GtkWidget **button,
163 g_object_set (G_OBJECT (*button), "focus-on-click", FALSE, NULL);
165 gtk_table_attach_defaults (GTK_TABLE (kp->table),
170 gtk_widget_set_size_request (*button,
171 30 * rows / (float) cols,
172 30 * cols / (float) rows);
174 g_hash_table_insert (kp->frag_table, *button,
175 (void *) keypad_insert_text[(button - &kp->digit[0])] );
177 g_signal_connect (*button, "clicked",
178 G_CALLBACK (button_click), kp);
180 gtk_widget_show (*button);
184 /* Return a new button with CODE as the unicode character for its label */
185 static inline GtkWidget *
186 button_new_from_unicode (gunichar code)
188 char s[6] = {0,0,0,0,0,0};
190 g_unichar_to_utf8 (code, s);
192 return gtk_button_new_with_label (s);
196 /* Callback which occurs when the mouse enters the widget.
197 It sets or unsets the focus.
200 enter_leave_notify (GtkWidget *widget,
201 GdkEventCrossing *event,
204 /* Do nothing if we're just moving between the widget and
206 if (event->detail == GDK_NOTIFY_INFERIOR)
209 if (event->type == GDK_ENTER_NOTIFY)
210 gtk_widget_grab_focus (widget);
212 if (event->type == GDK_LEAVE_NOTIFY)
213 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_FOCUS);
219 key_release_callback (GtkWidget *widget,
223 if ( ! (GTK_WIDGET_FLAGS (widget) & GTK_HAS_FOCUS) )
226 switch (event->keyval)
229 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "(");
232 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, ")");
235 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, ">");
238 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "<");
242 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "=");
244 case GDK_KP_Multiply :
246 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "*");
250 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "+");
252 case GDK_KP_Subtract :
254 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "-");
256 case GDK_KP_Decimal :
258 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, ".");
262 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "/");
266 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "0");
270 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "1");
274 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "2");
278 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "3");
282 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "4");
286 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "5");
290 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "6");
294 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "7");
298 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "8");
302 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "9");
313 psppire_keypad_init (PsppireKeypad *kp)
316 const int digit_voffset = 0;
317 const int digit_hoffset = 3;
319 GTK_WIDGET_SET_FLAGS (kp, GTK_CAN_FOCUS);
320 GTK_WIDGET_UNSET_FLAGS (kp, GTK_HAS_FOCUS);
322 kp->dispose_has_run = FALSE;
324 g_signal_connect (kp, "enter-notify-event", G_CALLBACK (enter_leave_notify),
327 g_signal_connect (kp, "leave-notify-event", G_CALLBACK (enter_leave_notify),
330 g_signal_connect (kp, "key-release-event", G_CALLBACK (key_release_callback),
333 kp->frag_table = g_hash_table_new (g_direct_hash, g_direct_equal);
335 kp->table = gtk_table_new (rows, cols, TRUE);
337 /* Buttons for the digits */
338 for (i = 0; i < 10; i++)
342 g_snprintf (buf, 5, "%d", i);
343 kp->digit[i] = gtk_button_new_with_label (buf);
346 add_button (kp, &kp->digit[i],
347 digit_hoffset + 0, digit_hoffset + 2,
348 digit_voffset + 3, digit_voffset + 4);
350 add_button (kp, &kp->digit[i],
351 digit_hoffset + j % 3, digit_hoffset + j % 3 + 1,
352 digit_voffset + 2 - (j / 3),
353 digit_voffset + 2 - (j / 3) + 1);
356 /* ... all the other buttons */
358 kp->dot = button_new_from_unicode (0xB7); /* MIDDLE DOT */
359 add_button (kp, &kp->dot, digit_hoffset + 2,
364 kp->plus = gtk_button_new_with_label ("+");
365 add_button (kp, &kp->plus, 0, 1,
368 kp->minus = button_new_from_unicode (0x2212); /* MINUS SIGN */
369 add_button (kp, &kp->minus, 0, 1,
372 kp->star = button_new_from_unicode (0xD7); /* MULTIPLICATION SIGN */
373 add_button (kp, &kp->star, 0, 1,
376 kp->slash = button_new_from_unicode (0xF7); /* DIVISION SIGN */
377 add_button (kp, &kp->slash, 0, 1,
383 g_markup_printf_escaped ("<span style=\"italic\">x<sup>y</sup></span>");
385 label = gtk_label_new ("**");
387 gtk_label_set_markup (GTK_LABEL (label), markup);
390 kp->star_star = gtk_button_new ();
391 gtk_container_add (GTK_CONTAINER (kp->star_star), label);
393 gtk_widget_show (label);
395 add_button (kp, &kp->star_star,
401 kp->gt = button_new_from_unicode (0x3E); /* GREATER-THAN SIGN*/
402 add_button (kp, &kp->gt, 2, 3,
405 kp->lt = button_new_from_unicode (0x3C); /* LESS-THAN SIGN*/
406 add_button (kp, &kp->lt, 1, 2,
409 kp->ge = button_new_from_unicode (0x2265); /* GREATER-THAN OR EQUAL */
410 add_button (kp, &kp->ge, 2, 3,
413 kp->le = button_new_from_unicode (0x2264); /* LESS-THAN OR EQUAL */
414 add_button (kp, &kp->le, 1, 2,
417 kp->neq = button_new_from_unicode (0x2260); /* NOT EQUAL */
418 add_button (kp, &kp->neq, 2, 3,
421 kp->eq = gtk_button_new_with_label ("=");
422 add_button (kp, &kp->eq, 1, 2,
425 kp->parentheses = gtk_button_new_with_label ("()");
426 add_button (kp, &kp->parentheses, 2, 3,
430 kp->delete = gtk_button_new_with_label ("Delete");
431 add_button (kp, &kp->delete, 3, 6,
436 kp->and = button_new_from_unicode (0x2227); /* LOGICAL AND */
437 add_button (kp, &kp->and, 1, 2,
441 kp->or = button_new_from_unicode (0x2228); /* LOGICAL OR */
442 add_button (kp, &kp->or, 2, 3,
446 kp->not = button_new_from_unicode (0xAC); /* NOT SIGN */
447 add_button (kp, &kp->not, 1, 2,
452 g_object_set (G_OBJECT (kp->table), "row-spacing", 5, NULL);
453 g_object_set (G_OBJECT (kp->table), "column-spacing", 5, NULL);
455 gtk_container_add (GTK_CONTAINER (kp), kp->table);
456 gtk_widget_show (kp->table);
458 gtk_widget_add_events (GTK_WIDGET (kp),
459 GDK_KEY_RELEASE_MASK |
460 GDK_LEAVE_NOTIFY_MASK |
461 GDK_ENTER_NOTIFY_MASK |
462 GDK_FOCUS_CHANGE_MASK);
468 psppire_keypad_new (void)
470 return GTK_WIDGET (g_object_new (psppire_keypad_get_type (), NULL));