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/>. */
17 #include <gtk/gtksignal.h>
18 #include <gtk/gtktable.h>
19 #include <gtk/gtkbutton.h>
20 #include <gtk/gtklabel.h>
21 #include <gdk/gdkkeysyms.h>
22 #include "psppire-keypad.h"
30 static void psppire_keypad_class_init (PsppireKeypadClass *klass);
31 static void psppire_keypad_init (PsppireKeypad *kp);
33 static guint keypad_signals[n_SIGNALS] = { 0 };
36 psppire_keypad_get_type (void)
38 static GType kp_type = 0;
42 static const GTypeInfo kp_info =
44 sizeof (PsppireKeypadClass),
46 NULL, /* base_finalize */
47 (GClassInitFunc) psppire_keypad_class_init,
48 NULL, /* class_finalize */
49 NULL, /* class_data */
50 sizeof (PsppireKeypad),
52 (GInstanceInitFunc) psppire_keypad_init,
55 kp_type = g_type_register_static (GTK_TYPE_EVENT_BOX, "PsppireKeypad",
62 static GObjectClass * parent_class = NULL;
65 psppire_keypad_dispose (GObject *obj)
67 PsppireKeypad *kp = (PsppireKeypad *)obj;
69 if (kp->dispose_has_run)
72 /* Make sure dispose does not run twice. */
73 kp->dispose_has_run = TRUE;
75 g_hash_table_unref (kp->frag_table);
77 /* Chain up to the parent class */
78 G_OBJECT_CLASS (parent_class)->dispose (obj);
82 psppire_keypad_finalize (GObject *obj)
84 /* Chain up to the parent class */
85 G_OBJECT_CLASS (parent_class)->finalize (obj);
89 psppire_keypad_class_init (PsppireKeypadClass *klass)
91 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
93 parent_class = g_type_class_peek_parent (klass);
95 gobject_class->dispose = psppire_keypad_dispose;
96 gobject_class->finalize = psppire_keypad_finalize;
98 keypad_signals[INSERT_SYNTAX] = g_signal_new ("insert-syntax",
99 G_TYPE_FROM_CLASS (klass),
100 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
101 G_STRUCT_OFFSET (PsppireKeypadClass,
105 g_cclosure_marshal_VOID__STRING,
109 keypad_signals[ERASE] = g_signal_new ("erase",
110 G_TYPE_FROM_CLASS (klass),
111 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
112 G_STRUCT_OFFSET (PsppireKeypadClass,
116 g_cclosure_marshal_VOID__VOID,
122 These are the strings that will be arguments to
124 The order of these must correspond
125 to the order of the button declarations
127 static const char *keypad_insert_text[] = {
128 "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
129 ".", "+", "-", "*", "**", "/", "=", "<>", "<", "<=",
130 ">", ">=", "&", "|", "~", "()", NULL
134 /* Callback for any button click.
135 Emits the "insert-syntax" signal for the keypad,
136 with the string corresponding to the clicked button.
139 button_click (GtkButton *b, PsppireKeypad *kp)
141 const gchar *s = g_hash_table_lookup (kp->frag_table, b);
145 g_signal_emit (kp, keypad_signals [INSERT_SYNTAX], 0, s);
147 g_signal_emit (kp, keypad_signals [ERASE], 0);
150 static const gint cols = 6;
151 static const gint rows = 5;
155 /* Add BUTTON to KP. The top-left corner at X1,Y1, the
156 botton-right corner at X2,Y2 */
158 add_button (PsppireKeypad *kp, GtkWidget **button,
162 g_object_set (G_OBJECT (*button), "focus-on-click", FALSE, NULL);
164 gtk_table_attach_defaults (GTK_TABLE (kp->table),
169 gtk_widget_set_size_request (*button,
170 30 * rows / (float) cols,
171 30 * cols / (float) rows);
173 g_hash_table_insert (kp->frag_table, *button,
174 (void *) keypad_insert_text[(button - &kp->digit[0])] );
176 g_signal_connect (*button, "clicked",
177 G_CALLBACK (button_click), kp);
179 gtk_widget_show (*button);
183 /* Return a new button with CODE as the unicode character for its label */
184 static inline GtkWidget *
185 button_new_from_unicode (gunichar code)
187 char s[6] = {0,0,0,0,0,0};
189 g_unichar_to_utf8 (code, s);
191 return gtk_button_new_with_label (s);
195 /* Callback which occurs when the mouse enters the widget.
196 It sets or unsets the focus.
199 enter_leave_notify (GtkWidget *widget,
200 GdkEventCrossing *event,
203 /* Do nothing if we're just moving between the widget and
205 if (event->detail == GDK_NOTIFY_INFERIOR)
208 if (event->type == GDK_ENTER_NOTIFY)
209 gtk_widget_grab_focus (widget);
211 if (event->type == GDK_LEAVE_NOTIFY)
212 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_FOCUS);
218 key_release_callback (GtkWidget *widget,
222 if ( ! (GTK_WIDGET_FLAGS (widget) & GTK_HAS_FOCUS) )
225 switch (event->keyval)
228 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "(");
231 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, ")");
234 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, ">");
237 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "<");
241 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "=");
243 case GDK_KP_Multiply :
245 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "*");
249 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "+");
251 case GDK_KP_Subtract :
253 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "-");
255 case GDK_KP_Decimal :
257 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, ".");
261 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "/");
265 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "0");
269 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "1");
273 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "2");
277 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "3");
281 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "4");
285 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "5");
289 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "6");
293 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "7");
297 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "8");
301 g_signal_emit (widget, keypad_signals [INSERT_SYNTAX], 0, "9");
312 psppire_keypad_init (PsppireKeypad *kp)
315 const int digit_voffset = 0;
316 const int digit_hoffset = 3;
318 GTK_WIDGET_SET_FLAGS (kp, GTK_CAN_FOCUS);
319 GTK_WIDGET_UNSET_FLAGS (kp, GTK_HAS_FOCUS);
321 kp->dispose_has_run = FALSE;
323 g_signal_connect (kp, "enter-notify-event", G_CALLBACK (enter_leave_notify),
326 g_signal_connect (kp, "leave-notify-event", G_CALLBACK (enter_leave_notify),
329 g_signal_connect (kp, "key-release-event", G_CALLBACK (key_release_callback),
332 kp->frag_table = g_hash_table_new (g_direct_hash, g_direct_equal);
334 kp->table = gtk_table_new (rows, cols, TRUE);
336 /* Buttons for the digits */
337 for (i = 0; i < 10; i++)
341 snprintf (buf, 5, "%d", i);
342 kp->digit[i] = gtk_button_new_with_label (buf);
345 add_button (kp, &kp->digit[i],
346 digit_hoffset + 0, digit_hoffset + 2,
347 digit_voffset + 3, digit_voffset + 4);
349 add_button (kp, &kp->digit[i],
350 digit_hoffset + j % 3, digit_hoffset + j % 3 + 1,
351 digit_voffset + 2 - (j / 3),
352 digit_voffset + 2 - (j / 3) + 1);
355 /* ... all the other buttons */
357 kp->dot = button_new_from_unicode (0xB7); /* MIDDLE DOT */
358 add_button (kp, &kp->dot, digit_hoffset + 2,
363 kp->plus = gtk_button_new_with_label ("+");
364 add_button (kp, &kp->plus, 0, 1,
367 kp->minus = button_new_from_unicode (0x2212); /* MINUS SIGN */
368 add_button (kp, &kp->minus, 0, 1,
371 kp->star = button_new_from_unicode (0xD7); /* MULTIPLICATION SIGN */
372 add_button (kp, &kp->star, 0, 1,
375 kp->slash = button_new_from_unicode (0xF7); /* DIVISION SIGN */
376 add_button (kp, &kp->slash, 0, 1,
382 g_markup_printf_escaped ("<span style=\"italic\">x<sup>y</sup></span>");
384 label = gtk_label_new ("**");
386 gtk_label_set_markup (GTK_LABEL (label), markup);
389 kp->star_star = gtk_button_new ();
390 gtk_container_add (GTK_CONTAINER (kp->star_star), label);
392 gtk_widget_show (label);
394 add_button (kp, &kp->star_star,
400 kp->gt = button_new_from_unicode (0x3E); /* GREATER-THAN SIGN*/
401 add_button (kp, &kp->gt, 2, 3,
404 kp->lt = button_new_from_unicode (0x3C); /* LESS-THAN SIGN*/
405 add_button (kp, &kp->lt, 1, 2,
408 kp->ge = button_new_from_unicode (0x2265); /* GREATER-THAN OR EQUAL */
409 add_button (kp, &kp->ge, 2, 3,
412 kp->le = button_new_from_unicode (0x2264); /* LESS-THAN OR EQUAL */
413 add_button (kp, &kp->le, 1, 2,
416 kp->neq = button_new_from_unicode (0x2260); /* NOT EQUAL */
417 add_button (kp, &kp->neq, 2, 3,
420 kp->eq = gtk_button_new_with_label ("=");
421 add_button (kp, &kp->eq, 1, 2,
424 kp->parentheses = gtk_button_new_with_label ("()");
425 add_button (kp, &kp->parentheses, 2, 3,
429 kp->delete = gtk_button_new_with_label ("Delete");
430 add_button (kp, &kp->delete, 3, 6,
435 kp->and = button_new_from_unicode (0x2227); /* LOGICAL AND */
436 add_button (kp, &kp->and, 1, 2,
440 kp->or = button_new_from_unicode (0x2228); /* LOGICAL OR */
441 add_button (kp, &kp->or, 2, 3,
445 kp->not = button_new_from_unicode (0xAC); /* NOT SIGN */
446 add_button (kp, &kp->not, 1, 2,
451 g_object_set (G_OBJECT (kp->table), "row-spacing", 5, NULL);
452 g_object_set (G_OBJECT (kp->table), "column-spacing", 5, NULL);
454 gtk_container_add (GTK_CONTAINER (kp), kp->table);
455 gtk_widget_show (kp->table);
457 gtk_widget_add_events (GTK_WIDGET (kp),
458 GDK_KEY_RELEASE_MASK |
459 GDK_LEAVE_NOTIFY_MASK |
460 GDK_ENTER_NOTIFY_MASK |
461 GDK_FOCUS_CHANGE_MASK);
467 psppire_keypad_new (void)
469 return GTK_WIDGET (g_object_new (psppire_keypad_get_type (), NULL));