~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina5/tina-tools/tinatool/wdgts/gtk/wdgtsGtk_tw_choice.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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    :  $Source: /home/tina/cvs/tina-tools/tinatool/wdgts/gtk/wdgtsGtk_tw_choice.c,v $
 37  * Date    :  $Date: 2009/03/25 13:37:03 $
 38  * Version :  $Revision: 1.9 $
 39  * CVS Id  :  $Id: wdgtsGtk_tw_choice.c,v 1.9 2009/03/25 13:37:03 paul Exp $
 40  *
 41  * Author  :  a.lacey@man.ac.uk, giob@man.ac.uk, paul.bromiley@manchester.ac.uk
 42  *
 43  *********
 44 */
 45 /** 
 46  *  @file  wdgtsGtk_tw_choice.c
 47  *  @brief Define and create GTK radio button and its associated callbacks.
 48  *
 49  *  Functions to create a GTK radio button group and to define its use by linking it into  
 50  *  the TINA callback system using a Tw_callback struct.  Also defines the callback functions
 51  *  invoked when the buttons are used. 
 52  *
 53  *  Note that the functions in this file are duplicated for each widget set 
 54  *  (xv, GTK, xm, etc ...).
 55  *
 56  */
 57 
 58 #include "wdgtsGtk_tw_choice.h"
 59 
 60 #if HAVE_CONFIG_H
 61 #   include <config.h>
 62 #endif
 63 
 64 #include <stdio.h>
 65 #include <gtk/gtk.h>
 66 #include <tina/sys/sysDef.h>
 67 #include <tina/sys/sysPro.h>
 68 #include <tinatool/wdgts/gtk/wdgtsGtk_tw_command.h>
 69 #include <tinatool/wdgts/gtk/wdgtsGtk_tw_tool.h>
 70 #include <tinatool/wdgts/gtk/wdgts_GtkDef.h>
 71 
 72 #define MAXBUTTON 12
 73 
 74 /* There seems to be no way to stop the callback being called when the state of a gtk toggle button in 
 75    a choice list is reset.  This causes problems with macros e.g. in imcalc, where the resetting of the
 76    choice list is recorded as an actual cast of the image type.  The gboolean reset records whether the
 77    call originated in the reset function, and prevents the button state change from being added to the
 78    list of button pressed in a macro if is did. PAB 7-12-2007 */
 79 
 80 static gboolean reset=FALSE;
 81 
 82 
 83 /* In Gtk, check and choice lists can have one of two styles.  If style=FALSE, the lists are drawn as 
 84    normal buttons i.e. a large button containing some text.  If style=TRUE, then check lists are drawn
 85    as tick-boxes with labels next to them, and choice lists are drawn as radio buttons i.e. circles
 86    that are empty when not selected, contain a dot when selected, and have text labels next to them.
 87    The default will now be FALSE such that both forms of list look like the lists in xview.  The fuctions
 88    tw_choice_list_style_select and tw_check_list_style_select will allow the behaviour to be changed from 
 89    the higher levels.  Corresponding functions exist in all widget sets but are not implemented.  Note 
 90    that the style is implemented immediately after the button is created, and so any change will affect 
 91    all subsequent lists. 
 92 
 93    Note that the argument to tw_check_list_style_select and tw_choice_list_syle_select is an integer, 
 94    rather than a boolean.  This is for several reasons: there is no Bool type in C prior to C99, and 
 95    so Bool tends to be redefined using preprocessor macros.  The NULL widgets cannot therefore use Bool 
 96    in the arguments list without including headers from tina-libs, which we try to avoid.  Gbooleans 
 97    (gdk) are actually integers, with TRUE and FALSE defined as preprocessor macros.  However, this may 
 98    change in future.  Xview does the same thing, so TINA has historically defined Bool as an integer.  
 99    Therefore, having the argument as an integer and explicitly testing style==0 seems to be the safest
100    option.  PAB 24/03/2009 */
101 
102 static gboolean choice_list_style=FALSE;
103 
104 
105 /**
106  * @brief  Select the style of subsequently created choice lists.  
107  * @param  stlye Boolean style indicator (default=FALSE).
108  * 
109  * In Gtk, style=0 (default) causes choice lists to be displayed 
110  * as toggle buttons (large buttons containing a text label): 
111  * style!=0 causes them to be displayed as radio buttons (circles
112  * with text labels next to them).
113  * 
114  */
115 void tw_choice_list_style_select(int style)
116 {
117   if(style==0)
118   {
119     choice_list_style = FALSE;
120   }
121   else
122   {
123     choice_list_style = TRUE;
124   }
125 }
126 
127 
128 /**
129  * @brief  Invoke radio button functions.  
130  * @param  twc  Pointer to Tw_callback associated with this widget.
131  * 
132  * Radio button functions depend on which button was selected, and  
133  * this is captured in the Tw_callback data1 field.
134  *
135  */
136 static void choice_call(Tw_callback * twc)
137 {
138   int *value = (int *) twc->data1;
139 
140  if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(twc->widget)) == TRUE) 
141      (*twc->func) (*value);
142 }
143 
144 
145 /**
146  * @brief   Replay radio button selection from current macro file.  
147  * @param   twc   Pointer to Tw_callback associated with this widget.
148  * @param   args  Pointer to required data, stored in the macro file.
149  */
150 static void choice_cmnd(Tw_callback *twc, char *args)
151 {
152   int  value;
153   int *twc_value = (int *) twc->data1;
154 
155   (void) sscanf(args, "%*s %d", &value);
156 
157   if (value == *twc_value)
158   {
159      gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(twc->widget), (gboolean) TRUE);
160      (*twc->func)(value);
161   }
162 }
163 
164 
165 /**
166  * @brief  Record radio button selection in current macro file.  
167  * @param  fp   Pointer to current macro file.
168  * @param  twc  Pointer to Tw_callback associated with this widget.
169  * 
170  * Records radio button selection for macro replay facility. 
171  *
172  */
173 static void choice_prnt(FILE * fp, Tw_callback * twc)
174 {
175   int *value = (int *) twc->data1;
176 
177   if(reset==FALSE)(void) fprintf(fp, "%s %d \n", twc->name, *value);
178 }
179 
180 
181 /*
182          multiple choice radio buttons
183 
184          - extracts parameters from variable argument list
185          - creates a container
186          - adds label to container
187          - creates first toggle and binds to toggle group
188          - adds remaining buttons to group
189          - ends container
190          - executes callback with default parameter
191 
192 */
193 /**
194  * @brief  Create new GTK radio button group and its associated callback struct.  
195  * @param  name  String to be used as group identifier and label.
196  * @param  args...  Vararg list of parameters.
197  * 
198  * Radio buttons allow the user to select one button from a group, and all others
199  * are automatically deselected.  They are used among other things to choose a file
200  * type for input or ouptut. 
201  *
202  * The parameter list must have the following items, in the order shown:
203  * string name, 
204  * pointer to callback function,
205  * integer identifier of default selection,
206  * comma-separated list of strings with button names,
207  * NULL to terminate button list. 
208  *
209  * Invokes callback for each radio button press, via call to tw_panel_callback 
210  * (in wdgtsGtk_tw_command.c).
211  *
212  */
213 Tw_callback *tw_choice(char *name, ...)
214 {
215   Tw_callback *twc;
216 
217   GtkWidget   *panel = tw_get_panel();
218   GtkWidget   *box;
219   GtkWidget   *button;
220   GtkWidget   **buttons=NULL;
221   GtkWidget   *chclabel;
222   GSList      *group;
223 
224   va_list      ap;
225   void       (*func)();
226   int         *vparam;
227   int          value;
228   int          i;
229 
230   char        *butname;
231   char        *fullname;
232 
233   /* First argument is widget name */
234   va_start(ap, name);
235   fullname = tw_extend_fullname(tw_get_toolname(), name);
236 
237   /* Second argument is pointer to callback function */
238   func = (void (*)())va_arg(ap, void *);
239 
240   /* Third argument is integer identifier of default selection */
241   value   = va_arg(ap, int);
242   vparam  = (int *)ralloc(sizeof(int));
243   buttons =   (void *)pvector_alloc(0,MAXBUTTON);
244   *vparam = value; 
245 
246   box = gtk_hbox_new(FALSE, 0);
247   gtk_container_add(GTK_CONTAINER(panel), box);
248   gtk_widget_show(box);  
249 
250   chclabel = gtk_label_new(name);
251   gtk_box_pack_start(GTK_BOX(box), chclabel, TRUE, TRUE, 0);
252   gtk_widget_show(chclabel);
253 
254   /*
255    * GTK radio buttons must have a group, and this is initialised by
256    * creating the first button in a NULL group.
257    */
258   butname = va_arg(ap, char *);
259   button = gtk_radio_button_new_with_label(NULL, butname);
260 
261   /* Set the mode of the button PAB 24/03/2009 */
262 
263   gtk_toggle_button_set_mode(GTK_TOGGLE_BUTTON(button), choice_list_style);
264 
265   gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);
266   gtk_widget_show(button);
267 
268   twc = tw_callback_make(fullname, button, 
269                          choice_call, choice_cmnd, choice_prnt, func, 
270                          (void *)vparam, (void *) buttons);
271   buttons[0] = button;
272 
273   gtk_signal_connect(GTK_OBJECT(button), "clicked", 
274                      GTK_SIGNAL_FUNC(tw_gtk_callback),
275                      (gpointer)twc);
276 
277   if (value == 0)
278     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), (gboolean) TRUE);
279 
280   for (i = 1; (butname = va_arg(ap, char *)) != NULL; i++)
281   {
282     if (i>11) 
283     {
284         format("too many buttons in tw_choice() \n");
285         va_end(ap);
286         return(twc);
287     }
288     group  = gtk_radio_button_group(GTK_RADIO_BUTTON(button));
289     button = gtk_radio_button_new_with_label(group, butname);
290 
291     /* Set the mode of the button PAB 24/03/2009 */
292 
293     gtk_toggle_button_set_mode(GTK_TOGGLE_BUTTON(button), choice_list_style);
294 
295     gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);
296     gtk_widget_show(button);
297 
298     vparam = (int *)ralloc(sizeof(int));
299     *vparam = i;
300 
301     twc = tw_callback_make(fullname, button, 
302                            choice_call, choice_cmnd, choice_prnt, func, 
303                            (void *)vparam, (void *)buttons);
304     buttons[i] = button;
305 
306     gtk_signal_connect(GTK_OBJECT(button), "clicked", 
307                        GTK_SIGNAL_FUNC(tw_gtk_callback),
308                        (gpointer)twc);
309     if (value == i)
310       gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), (gboolean) TRUE);
311 
312   }
313   va_end(ap);
314   (*func)(value); /* Initialise choice: needed for macros to work PAB 7-12-2007 */
315   return(twc);
316 }
317 
318 
319 /**
320  * @brief Reset radio button selection when it has been altered.  
321  * @param twc    Pointer to Tw_callback associated with this text entry widget.
322  * @param value  Pointer to Tw_callback associated with this text entry widget.
323  *
324  */
325 void tw_choice_reset(Tw_callback *twc, int value)
326 {
327   int *twc_value;
328   GtkWidget *button;
329   GtkWidget **buttons;
330 
331   if (twc && twc->data1 && (buttons = twc->data2))
332   {
333     reset=TRUE;
334     button = buttons[value];
335     if (button!=NULL)
336        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), (gboolean) TRUE);
337     reset=FALSE;
338   }
339 }
340 
341 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.