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

Linux Cross Reference
Tina5/tina-tools/tinatool/wdgts/gtk/wdgtsGtk_tw_check.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_check.c,v $
 37  * Date    :  $Date: 2009/03/25 13:37:03 $
 38  * Version :  $Revision: 1.4 $
 39  * CVS Id  :  $Id: wdgtsGtk_tw_check.c,v 1.4 2009/03/25 13:37:03 paul Exp $
 40  *
 41  * Author  :  a.lacey@man.ac.uk, giob@man.ac.uk
 42  *
 43  *********
 44 */
 45 /** 
 46  *  @file  wdgtsGtk_tw_check.c
 47  *  @brief Define and create GTK check button and its associated callbacks.
 48  *
 49  *  Functions to create a GTK check button 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_check.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/tlbase/tlbaseDef.h>
 69 #include <tinatool/tlbase/tlbasePro.h>
 70 #include <tinatool/wdgts/gtk/wdgts_GtkDef.h>
 71 #include <tinatool/wdgts/gtk/wdgtsGtk_tw_command.h>
 72 #include <tinatool/wdgts/gtk/wdgtsGtk_tw_tool.h>
 73 
 74 
 75 /* In Gtk, check and choice lists can have one of two styles.  If style=FALSE, the lists are drawn as 
 76    normal buttons i.e. a large button containing some text.  If style=TRUE, then check lists are drawn
 77    as tick-boxes with labels next to them, and choice lists are drawn as radio buttons i.e. circles
 78    that are empty when not selected, contain a dot when selected, and have text labels next to them.
 79    The default will now be FALSE such that both forms of list look like the lists in xview.  The fuctions
 80    tw_choice_list_style_select and tw_check_list_style_select will allow the behaviour to be changed from 
 81    the higher levels.  Corresponding functions exist in all widget sets but are not implemented.  Note 
 82    that the style is implemented immediately after the button is created, and so any change will affect 
 83    all subsequent lists. 
 84 
 85    Note that the argument to tw_check_list_style_select and tw_choice_list_syle_select is an integer, 
 86    rather than a boolean.  This is for several reasons: there is no Bool type in C prior to C99, and 
 87    so Bool tends to be redefined using preprocessor macros.  The NULL widgets cannot therefore use Bool 
 88    in the arguments list without including headers from tina-libs, which we try to avoid.  Gbooleans 
 89    (gdk) are actually integers, with TRUE and FALSE defined as preprocessor macros.  However, this may 
 90    change in future.  Xview does the same thing, so TINA has historically defined Bool as an integer.  
 91    Therefore, having the argument as an integer and explicitly testing style==0 seems to be the safest
 92    option.  PAB 24/03/2009 */
 93 
 94 static gboolean check_list_style=FALSE;
 95 
 96 
 97 /**
 98  * @brief  Select the style of subsequently created check lists.  
 99  * @param  stlye int style indicator (default=0).
100  * 
101  * In Gtk, style=0 (default) causes check lists to be displayed 
102  * as toggle buttons (large buttons containing a text label): 
103  * style!=0 causes them to be displayed as check buttons (tick-boxes
104  * with text labels next to them).
105  * 
106  */
107 void tw_check_list_style_select(int style)
108 {
109   if(style==0) 
110   {
111     check_list_style = FALSE;
112   }
113   else
114   {
115     check_list_style = TRUE;
116   }
117 }
118 
119 
120 /**
121  * @brief  Invoke check button functions.  
122  * @param  twc  Pointer to Tw_callback associated with this widget.
123  * 
124  * Check button functions depend on which button was selected, and  
125  * this is captured in the Tw_callback data1 field.  However Tina
126  * expects check buttons to be grouped in the Xv style, so a group
127  * bitmask is held in the Tw_callback data1 field.
128  *
129  * A bitwise XOR is used to set or unset the appropriate bits in the
130  * group mask.
131  *
132  */
133 static void check_call(Tw_callback * twc)
134 {
135   int *value = (int *) twc->data1;
136   int *mask  = (int *) twc->data2;
137 
138   *mask = ((*value)^(*mask));
139 
140   (*twc->func) (*mask);
141 }
142 
143 
144 /**
145  * @brief   Replay check button selection from current macro file.  
146  * @param   twc   Pointer to Tw_callback associated with this widget.
147  * @param   args  Pointer to required data, stored in the macro file.
148  * 
149  *  Note that this is missing the code to reset the choice in the GUI.
150  *  GAB 12 Nov 2003
151  */
152 static void check_cmnd(Tw_callback *twc, char *args)
153 {
154   int *value = (int *) twc->data1;
155   int  mask;
156 
157   (void) sscanf(args, "%*s %d", &mask);
158 
159   if(*value & mask)
160     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(twc->widget), (gboolean) TRUE);
161   else
162     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(twc->widget), (gboolean) FALSE);
163 }
164 
165 
166 /**
167  * @brief  Record check button selection in current macro file.  
168  * @param  fp   Pointer to current macro file.
169  * @param  twc  Pointer to Tw_callback associated with this widget.
170  * 
171  * Records check button selection for macro replay facility. 
172  *
173  */
174 static void check_prnt(FILE * fp, Tw_callback * twc)
175 {
176   int *mask  = (int *) twc->data2;
177 
178   (void) fprintf(fp, "%s %d\n", twc->name, *mask);
179 }
180 
181 
182 /**
183  * @brief  Create new GTK check button set and an associated callback struct.  
184  * @param  name  String to be used as group identifier and label.
185  * @param  args...  Vararg list of parameters.
186  * 
187  * Check buttons allow the user to set functionality off or on.  They are used among 
188  * other things to choose the image types (edge, corner, etc.) for display. 
189  *
190  * The parameter list must have the following items, in the order shown:
191  * - string name, 
192  * - pointer to group callback function,
193  * - integer bitmask of default selection(s),
194  * - comma-separated list of strings with button names,
195  * - NULL to terminate button name list.
196  *
197  * Had some fun with this one.  Xv groups check buttons in the same way as radio
198  * buttons - GTK (in line with every sensible GUI toolkit) does not.  But Tina
199  * has been written in an X-centric way, so functions from the Tina tools that use
200  * check buttons expect them to be grouped, and to make use of a bit mask indicating
201  * which buttons in the group are set.
202  *
203  * This is alien to GTK, which simply expects check buttons to link to ON-OFF 
204  * boolean flags.  To synthesize the expected tina functionality, the Tw_callback
205  * of check buttons stores a button group bitmask in data2, and the button value in
206  * data1.  The bitmask is built up step-by-step as the button group is created and
207  * then the callbacks use the group bitmask when required.
208  *
209  * If you want to change this, be warned!!
210  *
211  * Invokes callback for each check button press, via call to tw_panel_callback 
212  * (in wdgtsGtk_tw_command.c).
213  *
214  */
215 void tw_check(char *name, ...)
216 {
217   Tw_callback *twc;
218 
219   GtkWidget *panel= tw_get_panel();
220   GtkWidget *box;
221   GtkWidget *button;
222   GtkWidget *chklabel;
223 
224   va_list    ap;
225   void     (*func)();
226   int       *vvalue;      /** Pointer to stored button value            */
227   int        value;       /** Button value                              */
228   int       *st_mask;     /** Pointer to stored button group value mask */
229   int        mask;        /** Initial button value mask                 */
230   int        i;
231 
232   char  *butname;
233   char  *fullname;
234 
235   /** First argument is widget name */
236   va_start(ap, name);
237   fullname = tw_extend_fullname(tw_get_toolname(), name);
238 
239   /** Second argument is pointer to callback function */
240   func = (void (*)())va_arg(ap, void *);
241 
242   /** Third argument is integer bitmask of default selection(s) */
243   mask = va_arg(ap, int);
244   /** 
245    *  But store a clear mask initially because the bits will be set
246    *  as we go along in the for loop ...
247    */
248   st_mask = (int *)ralloc(sizeof(int));
249   *st_mask = 0;
250 
251   box = gtk_hbox_new (FALSE, 0);
252   gtk_container_add(GTK_CONTAINER(panel), box);
253   gtk_widget_show (box);        
254         
255   chklabel = gtk_label_new(name);
256   gtk_box_pack_start(GTK_BOX(box), chklabel, TRUE, TRUE, 0);
257   gtk_widget_show(chklabel);
258 
259   for (i = 0; (butname = va_arg(ap, char *)) != NULL; i++)
260   {
261     button = gtk_check_button_new_with_label(butname);
262 
263     /* Set the mode of the button PAB 24/03/2009 */
264 
265     gtk_toggle_button_set_mode(GTK_TOGGLE_BUTTON(button), check_list_style);
266 
267     gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);
268     gtk_widget_show(button);
269 
270     /* Set the button's value (in this group) ... */
271     vvalue = (int *)ralloc(sizeof(int));
272     if (i == 0)
273       value = 1;
274     else
275       value *= 2;
276     *vvalue = value;
277     
278     /** Create callback with stored button value and group mask ... */
279     twc = tw_callback_make(fullname, button, 
280                            check_call, check_cmnd, check_prnt, func, 
281                            (void *)vvalue, (void *)st_mask);
282 
283     gtk_signal_connect(GTK_OBJECT(button), "toggled", 
284                        GTK_SIGNAL_FUNC(tw_gtk_callback),
285                        (gpointer)twc);
286 
287     /** Set initial state of each button (also builds up stored mask) ... */
288     if(value & mask)
289       gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), (gboolean) TRUE);
290   }
291 
292   va_end(ap);
293 }
294 
295 

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