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

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

~ [ 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.