1 /**********
2 *
3 * Copyright (c) 2003, Division of Imaging Science and Biomedical Engineering,
4 * University of Manchester, UK. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without modification,
7 * are permitted provided that the following conditions are met:
8 *
9 * . Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * . Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * . Neither the name of the University of Manchester nor the names of its
17 * contributors may be used to endorse or promote products derived from this
18 * software without specific prior written permission.
19 *
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 *
33 **********
34 *
35 * Program : TINA
36 * File :
37 * Date :
38 * Version :
39 * CVS Id :
40 *
41 * Author : paul.bromiley@manchester.ac.uk
42 *
43 *********
44 */
45 /**
46 * @file wdgtsGtk_tw_keyvalue.c
47 * @brief Define and create GTK text entry field and its associated callbacks.
48 *
49 * Adapted from wdgtsGtk_tw_svalue.c. Functions to create a special version of
50 * the GTK text entry field and to define its use by linking it into the TINA
51 * callback structure using a TW_callback struct. Also defines the callback
52 * functions invoked when the field is used. The functions in this file differ
53 * from those in wdgtsGtk_tw_svalue.c in that:
54 *
55 * - direct entry into the field using the keyboard is not allowed;
56 * - the field can have keboard focus.
57 *
58 * The aim here is that, when the text entry field has keyboard focus (e.g. the
59 * mouse pointer is within the field) and a key is pressed, the keyval is
60 * translated into a string describing the key (using gdk_keyval_name) and the
61 * name is displayed in the text entry field. The callback function is then
62 * called with the keyval, allowing construction of dialog boxes that set the
63 * keyboard controls used in Tv_tools.
64 *
65 */
66
67 #include "wdgtsGtk_tw_keyvalue.h"
68
69 #if HAVE_CONFIG_H
70 # include <config.h>
71 #endif
72
73 #include <stdio.h>
74 #include <sys/param.h>
75 #include <gtk/gtk.h>
76
77 #include <tina/sys/sysDef.h>
78 #include <tina/sys/sysPro.h>
79
80 #include <tinatool/wdgts/gtk2/wdgts_GtkDef.h>
81 #include <tinatool/wdgts/gtk2/wdgtsGtk_tw_command.h>
82 #include <tinatool/wdgts/gtk2/wdgtsGtk_tw_tool.h>
83
84
85 /**
86 * @brief Copy key from entry widget to stored callback data field.
87 * @param twc Pointer to Tw_callback associated with this integer entry widget.
88 *
89 * Callback function for key entry widgets. Each of these has an associated
90 * Tw_callback, and this function uses a setter function stored in the func field
91 * of the Tw_callback to copy the current widget keycode to the desired variable.
92 *
93 * Note that this callback is not invoked directly by the widget, but via a call
94 * to tw_gtk_callback_local (below).
95 *
96 */
97 static void keyvalue_call(Tw_callback * twc)
98 {
99 void (*set)() = (void (*) ()) twc->func;
100 char *pstring=NULL, string[MAXPATHLEN];
101 int *pkeycode=NULL;
102
103 pkeycode = (int *)(twc->data2);
104 (*set) (*pkeycode);
105
106 /* Since we are hijacking the normal data entry method, we have to explicitly
107 * reset the display.
108 */
109
110 pstring = gdk_keyval_name(*pkeycode);
111 if(pstring!=NULL)
112 {
113 (void) sprintf(string, "%s", pstring);
114 }
115 else
116 {
117 (void) sprintf(string, "NULL");
118 }
119 gtk_entry_set_text(GTK_ENTRY(twc->widget), string);
120 }
121
122
123 /**
124 * @brief Replay key entry from current macro file.
125 * @param twc Pointer to Tw_callback associated with this widget.
126 * @param args Pointer to required data, stored in the macro file.
127 *
128 */
129 static void keyvalue_cmnd(Tw_callback * twc, char *args)
130 {
131 void (*set)() = (void (*) ()) twc->func;
132 char *pstring, string[MAXPATHLEN];
133 int keycode;
134
135 (void) sscanf(args, "%*s %d", &keycode);
136 pstring = gdk_keyval_name(keycode);
137
138 if(pstring!=NULL)
139 {
140 (void) sprintf(string, "%s", pstring);
141 (*set) (keycode);
142 }
143 else
144 {
145 (void) sprintf(string, "NULL");
146 }
147
148 gtk_entry_set_text(GTK_ENTRY(twc->widget), string);
149 }
150
151
152 /**
153 * @brief Record key entry in current macro file.
154 * @param fp Pointer to current macro file.
155 * @param twc Pointer to Tw_callback associated with this widget.
156 *
157 */
158 static void keyvalue_prnt(FILE * fp, Tw_callback * twc)
159 {
160 int (*get) () = (int (*) ()) twc->data1;
161 (void) fprintf(fp, "%s %d\n", twc->name, (*get)());
162 }
163
164
165 /**
166 * @brief Handle keyboard events in key entry field.
167 * @param widget Gtk pointer to the key entry widget.
168 * @param event Desription of the event.
169 * @param data TINA pointer to the key entry widget callback
170 * @return gboolean Should always be TRUE: see inline comment.
171 *
172 * This function is a local copy of tw_gtk_callback that writes the keyval for the
173 * keyboard event into the relevant part of the Tw_callback structure, and then
174 * calls tw_callback so that the key entry widget is still registered with the
175 * standard TINA callback system.
176 *
177 */
178 static gboolean tw_gtk_callback_local(GtkWidget *widget, GdkEventKey *event, gpointer data)
179 {
180 Tw_callback *twc = (Tw_callback *) data;
181 int *pkeycode=NULL, keycode;
182
183 /* The gtk keycode corresponds to an entry in the list in gphx_GdkDef.h, which defines
184 * a corresponding name for TINA. If another widget set is added in future, then a
185 * similar list must be implemented for that widget set. PAB 26/03/2010.
186 */
187
188 keycode = event->keyval;
189 pkeycode = (int *)(twc->data2);
190 *pkeycode = keycode;
191 tw_callback(twc);
192
193 /* Returning TRUE from this function prevents the inheritance of pre-defined Gtk
194 * behaviours for this widget/event combination, and thus prevents e.g. focus
195 * movement when the arrow keys are pressed.
196 */
197
198 return TRUE;
199 }
200
201
202 /**
203 * @brief Create new key entry field and its associated callback struct.
204 * @param name String to be used as entry field identifier and label.
205 * @param get() Pointer to getter function for key entered in the field.
206 * @param set() Pointer to setter function for key entered in the field.
207 * @param nchars Maximum number of characters for input field.
208 * @return Tw_callback Pointer to new Tw_callback associated with this entry field.
209 *
210 * Creates new hbox for key entry field, and packs field and label in it.
211 *
212 * Invokes callback for each keypress, via call to tw_gtk_callback_local
213 * (above).
214 *
215 */
216 Tw_callback *tw_keyvalue(char *name, int (*get)(), void (*set)(), int nchars)
217 {
218 GtkWidget *panel= tw_get_panel();
219 GtkWidget *box;
220 GtkWidget *stxlabel;
221 GtkWidget *stxentry;
222 int *keycode = (int *)ralloc(sizeof(int));
223 Tw_callback *twc;
224 char *fullname = tw_extend_fullname(tw_get_toolname(), name);
225 char *pstring, string[MAXPATHLEN];
226
227 box = gtk_hbox_new(FALSE, 8);
228 gtk_container_add(GTK_CONTAINER(panel), box);
229 gtk_widget_show(box);
230
231 stxlabel = gtk_label_new(name);
232 stxentry = gtk_entry_new();
233
234 /* Now we have to deny the user the abilty to write to the text entry field, and
235 * give it the ability to receive general keyboard input */
236
237 gtk_editable_set_editable(GTK_EDITABLE(stxentry), FALSE);
238
239 gtk_widget_set_usize(stxentry, (10+6*nchars), 22);
240 gtk_box_pack_start(GTK_BOX(box), stxlabel, FALSE, FALSE, 0);
241 gtk_box_pack_start(GTK_BOX(box), stxentry, FALSE, FALSE, 0);
242 gtk_widget_show(stxlabel);
243 gtk_widget_show(stxentry);
244
245 *keycode = (get) ();
246 pstring = gdk_keyval_name(*keycode);
247
248 if(pstring!=NULL)
249 {
250 (void) sprintf(string, "%s", pstring);
251 }
252 else
253 {
254 (void) sprintf(string, "NULL");
255 }
256 gtk_entry_set_text(GTK_ENTRY(stxentry), string);
257
258 /* I have shuffled the order of the functions in the Tw_callback around compared
259 * to that used in svalue, in order to make space for a field to hold the keyval.
260 */
261
262 twc = tw_callback_make(fullname, stxentry,
263 keyvalue_call, keyvalue_cmnd, keyvalue_prnt, (void *) set,
264 (void *) get, (void *) keycode);
265
266 /* Invoke callback for GTK entry "changed" event (i.e. for each keypress) */
267
268
269 /* We cannot call the tw_gtk_callback function, as the prototype for the
270 * key_press_event signal is different to that for the other gtk signals
271 * used in TINA, and we also need to grab the GdkEventKey in order to get
272 * the keyval. The tw_gtk_callback_local function is called instead,
273 * getting the keyval but still calling tw_callback in order to link this
274 * widget into the TINA callback system.
275 */
276
277 gtk_signal_connect(GTK_OBJECT(stxentry), "key_press_event",
278 GTK_SIGNAL_FUNC(tw_gtk_callback_local),
279 (gpointer) twc);
280
281 return (twc);
282 }
283
284
285 /**
286 * @brief Reset key entry field when value has been altered in the program.
287 * @param twc Pointer to Tw_callback associated with this text entry widget.
288 *
289 *
290 */
291 void tw_keyvalue_reset(Tw_callback * twc)
292 {
293 int (*get)();
294 char *pstring=NULL;
295 char string[MAXPATHLEN];
296 int keycode;
297
298 keycode = (get) ();
299 pstring = gdk_keyval_name(keycode);
300
301 if(pstring!=NULL)
302 {
303 (void) sprintf(string, "%s", pstring);
304 }
305 else
306 {
307 (void) sprintf(string, "NULL");
308 }
309 gtk_entry_set_text(GTK_ENTRY(twc->widget), string);
310 }
311
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.