1 /*
2 * Common callbacks for spinbuttons
3 *
4 * Authors:
5 * bulia byak <bulia@users.sourceforge.net>
6 *
7 * Copyright (C) 2003 authors
8 *
9 * Released under GNU GPL, read the file 'COPYING' for more information
10 */
12 #ifdef HAVE_CONFIG_H
13 # include "config.h"
14 #endif
16 #include <gtk/gtk.h>
17 #include <gdk/gdkkeysyms.h>
19 #include "../event-context.h"
21 #include "sp-widget.h"
22 #include "widget-sizes.h"
25 gboolean
26 spinbutton_focus_in (GtkWidget *w, GdkEventKey *event, gpointer data)
27 {
28 gdouble *ini;
30 ini = (gdouble *) gtk_object_get_data (GTK_OBJECT (w), "ini");
31 if (ini) g_free (ini); // free the old value if any
33 // retrieve the value
34 ini = g_new (gdouble, 1);
35 *ini = gtk_spin_button_get_value (GTK_SPIN_BUTTON(w));
37 // remember it
38 gtk_object_set_data (GTK_OBJECT (w), "ini", ini);
40 return FALSE; // I didn't consume the event
41 }
43 void
44 spinbutton_undo (GtkWidget *w)
45 {
46 gdouble *ini = (gdouble *) gtk_object_get_data (GTK_OBJECT (w), "ini");
47 gtk_spin_button_set_value(GTK_SPIN_BUTTON(w), *ini);
48 }
50 void
51 spinbutton_defocus (GtkObject *container)
52 {
53 // defocus spinbuttons by moving focus to the canvas, unless "stay" is on
54 gboolean stay = GPOINTER_TO_INT(gtk_object_get_data (GTK_OBJECT (container), "stay"));
55 if (stay) {
56 gtk_object_set_data (GTK_OBJECT (container), "stay", GINT_TO_POINTER (FALSE));
57 } else {
58 GtkWidget *canvas = (GtkWidget *) gtk_object_get_data (GTK_OBJECT (container), "dtw");
59 if (canvas) {
60 gtk_widget_grab_focus (GTK_WIDGET(canvas));
61 }
62 }
63 }
65 gboolean
66 spinbutton_keypress (GtkWidget *w, GdkEventKey *event, gpointer data)
67 {
68 SPWidget *spw = (SPWidget *) data;
69 gdouble v;
71 switch (get_group0_keyval (event)) {
72 case GDK_Escape: // defocus
73 spinbutton_undo (w);
74 spinbutton_defocus (GTK_OBJECT (spw));
75 return TRUE; // I consumed the event
76 break;
77 case GDK_Return: // defocus
78 case GDK_KP_Enter:
79 spinbutton_defocus (GTK_OBJECT (spw));
80 return TRUE; // I consumed the event
81 break;
82 case GDK_Tab:
83 case GDK_ISO_Left_Tab:
84 // set the flag meaning "do not leave toolbar when changing value"
85 gtk_object_set_data (GTK_OBJECT (spw), "stay", GINT_TO_POINTER(TRUE));
86 return FALSE; // I didn't consume the event
87 break;
89 // The following keys are processed manually because GTK implements them in strange ways
90 // (increments start with double step value and seem to grow as you press the key continuously)
92 case GDK_Up:
93 case GDK_KP_Up:
94 gtk_object_set_data (GTK_OBJECT (spw), "stay", GINT_TO_POINTER(TRUE));
95 v = gtk_spin_button_get_value(GTK_SPIN_BUTTON (w));
96 v += SPIN_STEP;
97 gtk_spin_button_set_value(GTK_SPIN_BUTTON(w), v);
98 return TRUE; // I consumed the event
99 break;
100 case GDK_Down:
101 case GDK_KP_Down:
102 gtk_object_set_data (GTK_OBJECT (spw), "stay", GINT_TO_POINTER(TRUE));
103 v = gtk_spin_button_get_value(GTK_SPIN_BUTTON (w));
104 v -= SPIN_STEP;
105 gtk_spin_button_set_value(GTK_SPIN_BUTTON(w), v);
106 return TRUE; // I consumed the event
107 break;
108 case GDK_Page_Up:
109 case GDK_KP_Page_Up:
110 gtk_object_set_data (GTK_OBJECT (spw), "stay", GINT_TO_POINTER(TRUE));
111 v = gtk_spin_button_get_value(GTK_SPIN_BUTTON (w));
112 v += SPIN_PAGE_STEP;
113 gtk_spin_button_set_value(GTK_SPIN_BUTTON(w), v);
114 return TRUE; // I consumed the event
115 break;
116 case GDK_Page_Down:
117 case GDK_KP_Page_Down:
118 gtk_object_set_data (GTK_OBJECT (spw), "stay", GINT_TO_POINTER(TRUE));
119 v = gtk_spin_button_get_value(GTK_SPIN_BUTTON (w));
120 v -= SPIN_PAGE_STEP;
121 gtk_spin_button_set_value(GTK_SPIN_BUTTON(w), v);
122 return TRUE; // I consumed the event
123 break;
124 case GDK_z:
125 case GDK_Z:
126 gtk_object_set_data (GTK_OBJECT (spw), "stay", GINT_TO_POINTER(TRUE));
127 if (event->state & GDK_CONTROL_MASK) {
128 spinbutton_undo (w);
129 return TRUE; // I consumed the event
130 }
131 break;
132 default:
133 return FALSE;
134 break;
135 }
136 return FALSE; // I didn't consume the event
137 }