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 <gdk/gdkkeysyms-compat.h>
21 #include "psppire-keypad.h"
29 static void psppire_keypad_class_init (PsppireKeypadClass *klass);
30 static void psppire_keypad_init (PsppireKeypad *kp);
32 static guint keypad_signals [n_SIGNALS] = { 0 };
35 psppire_keypad_get_type (void)
37 static GType kp_type = 0;
41 static const GTypeInfo kp_info =
43 sizeof (PsppireKeypadClass),
45 NULL, /* base_finalize */
46 (GClassInitFunc) psppire_keypad_class_init,
47 NULL, /* class_finalize */
48 NULL, /* class_data */
49 sizeof (PsppireKeypad),
51 (GInstanceInitFunc) psppire_keypad_init,
54 kp_type = g_type_register_static (GTK_TYPE_EVENT_BOX, "PsppireKeypad",
61 static GObjectClass * parent_class = NULL;
64 psppire_keypad_dispose (GObject *obj)
66 PsppireKeypad *kp = (PsppireKeypad *)obj;
68 if (kp->dispose_has_run)
71 /* Make sure dispose does not run twice. */
72 kp->dispose_has_run = TRUE;
74 g_hash_table_unref (kp->frag_table);
76 /* Chain up to the parent class */
77 G_OBJECT_CLASS (parent_class)->dispose (obj);
81 psppire_keypad_finalize (GObject *obj)
83 /* Chain up to the parent class */
84 G_OBJECT_CLASS (parent_class)->finalize (obj);
88 psppire_keypad_class_init (PsppireKeypadClass *klass)
90 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
92 parent_class = g_type_class_peek_parent (klass);
94 gobject_class->dispose = psppire_keypad_dispose;
95 gobject_class->finalize = psppire_keypad_finalize;
97 keypad_signals[INSERT_SYNTAX] = g_signal_new ("insert-syntax",
98 G_TYPE_FROM_CLASS (klass),
99 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
100 G_STRUCT_OFFSET (PsppireKeypadClass,
104 g_cclosure_marshal_VOID__STRING,
108 keypad_signals[ERASE] = g_signal_new ("erase",
109 G_TYPE_FROM_CLASS (klass),
110 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
111 G_STRUCT_OFFSET (PsppireKeypadClass,
115 g_cclosure_marshal_VOID__VOID,
121 These are the strings that will be arguments to
123 The order of these must correspond
124 to the order of the button declarations
126 static const char * const keypad_insert_text[] = {
127 "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
128 ".", "+", "-", "*", "**", "/", "=", "<>", "<", "<=",
129 ">", ">=", "&", "|", "~", "()", NULL
133 /* Callback for any button click.
134 Emits the "insert-syntax" signal for the keypad,
135 with the string corresponding to the clicked button.
138 button_click (GtkButton *b, PsppireKeypad *kp)
140 const gchar *s = g_hash_table_lookup (kp->frag_table, b);
144 g_signal_emit (kp, keypad_signals [INSERT_SYNTAX], 0, s);
146 g_signal_emit (kp, keypad_signals [ERASE], 0);
149 static const gint cols = 6;
150 static const gint rows = 5;
154 /* Add BUTTON to KP. The top-left corner at X1,Y1, the
155 botton-right corner at X2,Y2 */
157 add_button (PsppireKeypad *kp, GtkWidget **button,
161 g_object_set (G_OBJECT (*button), "focus-on-click", FALSE, NULL);
163 gtk_table_attach_defaults (GTK_TABLE (kp->table),
168 gtk_widget_set_size_request (*button,
169 30 * rows / (float) cols,
170 30 * cols / (float) rows);
172 g_hash_table_insert (kp->frag_table, *button,
173 (void *) keypad_insert_text[(button - &kp->digit[0])] );
175 g_signal_connect (*button, "clicked",
176 G_CALLBACK (button_click), kp);
178 gtk_widget_show (*button);
182 /* Return a new button with CODE as the unicode character for its label */
183 static inline GtkWidget *
184 button_new_from_unicode (gunichar code)
186 char s[6] = {0,0,0,0,0,0};
188 g_unichar_to_utf8 (code, s);
190 return gtk_button_new_with_label (s);
194 /* Callback which occurs when the mouse enters the widget.
195 It sets or unsets the focus.
198 enter_leave_notify (GtkWidget *widget,
199 GdkEventCrossing *event,
202 /* Do nothing if we're just moving between the widget and
204 if (event->detail == GDK_NOTIFY_INFERIOR)
207 if (event->type == GDK_ENTER_NOTIFY)
208 gtk_widget_grab_focus (widget);
214 key_release_callback (GtkWidget *widget,
218 if ( ! gtk_widget_has_focus (widget))
221 switch (event->keyval)
224 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "(");
227 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, ")");
230 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, ">");
233 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "<");
237 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "=");
239 case GDK_KP_Multiply :
241 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "*");
245 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "+");
247 case GDK_KP_Subtract :
249 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "-");
251 case GDK_KP_Decimal :
253 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, ".");
257 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "/");
261 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "0");
265 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "1");
269 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "2");
273 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "3");
277 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "4");
281 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "5");
285 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "6");
289 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "7");
293 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "8");
297 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "9");
308 psppire_keypad_init (PsppireKeypad *kp)
311 const int digit_voffset = 0;
312 const int digit_hoffset = 3;
314 gtk_widget_set_can_focus (GTK_WIDGET (kp), TRUE);
316 kp->dispose_has_run = FALSE;
318 g_signal_connect (kp, "enter-notify-event", G_CALLBACK (enter_leave_notify),
321 g_signal_connect (kp, "leave-notify-event", G_CALLBACK (enter_leave_notify),
324 g_signal_connect (kp, "key-release-event", G_CALLBACK (key_release_callback),
327 kp->frag_table = g_hash_table_new (g_direct_hash, g_direct_equal);
329 kp->table = gtk_table_new (rows, cols, TRUE);
331 /* Buttons for the digits */
332 for (i = 0; i < 10; i++)
336 g_snprintf (buf, 5, "%d", i);
337 kp->digit[i] = gtk_button_new_with_label (buf);
340 add_button (kp, &kp->digit[i],
341 digit_hoffset + 0, digit_hoffset + 2,
342 digit_voffset + 3, digit_voffset + 4);
344 add_button (kp, &kp->digit[i],
345 digit_hoffset + j % 3, digit_hoffset + j % 3 + 1,
346 digit_voffset + 2 - (j / 3),
347 digit_voffset + 2 - (j / 3) + 1);
350 /* ... all the other buttons */
352 kp->dot = button_new_from_unicode (0xB7); /* MIDDLE DOT */
353 add_button (kp, &kp->dot, digit_hoffset + 2,
358 kp->plus = gtk_button_new_with_label ("+");
359 add_button (kp, &kp->plus, 0, 1,
362 kp->minus = button_new_from_unicode (0x2212); /* MINUS SIGN */
363 add_button (kp, &kp->minus, 0, 1,
366 kp->star = button_new_from_unicode (0xD7); /* MULTIPLICATION SIGN */
367 add_button (kp, &kp->star, 0, 1,
370 kp->slash = button_new_from_unicode (0xF7); /* DIVISION SIGN */
371 add_button (kp, &kp->slash, 0, 1,
377 g_markup_printf_escaped ("<span style=\"italic\">x<sup>y</sup></span>");
379 label = gtk_label_new ("**");
381 gtk_label_set_markup (GTK_LABEL (label), markup);
384 kp->star_star = gtk_button_new ();
385 gtk_container_add (GTK_CONTAINER (kp->star_star), label);
387 gtk_widget_show (label);
389 add_button (kp, &kp->star_star,
395 kp->gt = button_new_from_unicode (0x3E); /* GREATER-THAN SIGN*/
396 add_button (kp, &kp->gt, 2, 3,
399 kp->lt = button_new_from_unicode (0x3C); /* LESS-THAN SIGN*/
400 add_button (kp, &kp->lt, 1, 2,
403 kp->ge = button_new_from_unicode (0x2265); /* GREATER-THAN OR EQUAL */
404 add_button (kp, &kp->ge, 2, 3,
407 kp->le = button_new_from_unicode (0x2264); /* LESS-THAN OR EQUAL */
408 add_button (kp, &kp->le, 1, 2,
411 kp->neq = button_new_from_unicode (0x2260); /* NOT EQUAL */
412 add_button (kp, &kp->neq, 2, 3,
415 kp->eq = gtk_button_new_with_label ("=");
416 add_button (kp, &kp->eq, 1, 2,
419 kp->parentheses = gtk_button_new_with_label ("()");
420 add_button (kp, &kp->parentheses, 2, 3,
424 kp->delete = gtk_button_new_with_label ("Delete");
425 add_button (kp, &kp->delete, 3, 6,
430 kp->and = button_new_from_unicode (0x2227); /* LOGICAL AND */
431 add_button (kp, &kp->and, 1, 2,
435 kp->or = button_new_from_unicode (0x2228); /* LOGICAL OR */
436 add_button (kp, &kp->or, 2, 3,
440 kp->not = button_new_from_unicode (0xAC); /* NOT SIGN */
441 add_button (kp, &kp->not, 1, 2,
446 g_object_set (G_OBJECT (kp->table), "row-spacing", 5, NULL);
447 g_object_set (G_OBJECT (kp->table), "column-spacing", 5, NULL);
449 gtk_container_add (GTK_CONTAINER (kp), kp->table);
450 gtk_widget_show (kp->table);
452 gtk_widget_add_events (GTK_WIDGET (kp),
453 GDK_KEY_RELEASE_MASK |
454 GDK_LEAVE_NOTIFY_MASK |
455 GDK_ENTER_NOTIFY_MASK |
456 GDK_FOCUS_CHANGE_MASK);
462 psppire_keypad_new (void)
464 return GTK_WIDGET (g_object_new (psppire_keypad_get_type (), NULL));