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

Linux Cross Reference
Tina5/tina-tools/tinatool/wdgts/gtk2/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/gtk2/wdgtsGtk_tw_check.c,v $
 37  * Date    :  $Date: 2009/05/13 15:38:15 $
 38  * Version :  $Revision: 1.5 $
 39  * CVS Id  :  $Id: wdgtsGtk_tw_check.c,v 1.5 2009/05/13 15:38:15 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_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/gtk2/wdgts_GtkDef.h>
 71 #include <tinatool/wdgts/gtk2/wdgtsGtk_tw_command.h>
 72 #include <tinatool/wdgts/gtk2/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   GtkWidget *check_frame=NULL;
224 
225   /* Code for changing the colour of the check list buttons PAB 13/05/2009 */
226   /* GdkColor color_red, color_blue; */
227   /* gdk_color_parse("MistyRose", &color_red); */
228   /* gdk_color_parse("lavender", &color_blue); */
229 
230   va_list    ap;
231   void     (*func)();
232   int       *vvalue;      /** Pointer to stored button value            */
233   int        value;       /** Button value                              */
234   int       *st_mask;     /** Pointer to stored button group value mask */
235   int        mask;        /** Initial button value mask                 */
236   int        i;
237 
238   char  *butname;
239   char  *fullname;
240 
241   /** First argument is widget name */
242   va_start(ap, name);
243   fullname = tw_extend_fullname(tw_get_toolname(), name);
244 
245   /** Second argument is pointer to callback function */
246   func = (void (*)())va_arg(ap, void *);
247 
248   /** Third argument is integer bitmask of default selection(s) */
249   mask = va_arg(ap, int);
250   /** 
251    *  But store a clear mask initially because the bits will be set
252    *  as we go along in the for loop ...
253    */
254   st_mask = (int *)ralloc(sizeof(int));
255   *st_mask = 0;
256 
257   /* Make a frame to hold the whole list, so that it is distinct from other buttons
258      on the interface. PAB 25/03/2009 */
259 
260   check_frame = gtk_frame_new(name);
261   gtk_container_set_border_width(GTK_CONTAINER(check_frame), 0);
262 
263   box = gtk_hbox_new (FALSE, 0);
264   gtk_container_add(GTK_CONTAINER(check_frame), box);
265   gtk_widget_show (box);
266 
267   /*
268   chklabel = gtk_label_new(name);
269   gtk_box_pack_start(GTK_BOX(box), chklabel, TRUE, TRUE, 0);
270   gtk_widget_show(chklabel);
271   */
272 
273   for (i = 0; (butname = va_arg(ap, char *)) != NULL; i++)
274   {
275     button = gtk_check_button_new_with_label(butname);
276 
277     /* Set the mode of the button PAB 24/03/2009 */
278 
279     gtk_toggle_button_set_mode(GTK_TOGGLE_BUTTON(button), check_list_style);
280 
281     /* Code for changing the colour of the check list buttons PAB 13/05/2009 */
282     /* gtk_widget_modify_bg(GTK_WIDGET(button), GTK_STATE_NORMAL, &color_red); */
283     /* gtk_widget_modify_bg(GTK_WIDGET(button), GTK_STATE_ACTIVE, &color_blue); */
284 
285     gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);
286     gtk_widget_show(button);
287 
288     /* Set the button's value (in this group) ... */
289     vvalue = (int *)ralloc(sizeof(int));
290     if (i == 0)
291       value = 1;
292     else
293       value *= 2;
294     *vvalue = value;
295 
296     /** Create callback with stored button value and group mask ... */
297     twc = tw_callback_make(fullname, button, 
298                            check_call, check_cmnd, check_prnt, func, 
299                            (void *)vvalue, (void *)st_mask);
300 
301     gtk_signal_connect(GTK_OBJECT(button), "toggled", 
302                        GTK_SIGNAL_FUNC(tw_gtk_callback),
303                        (gpointer)twc);
304 
305     /** Set initial state of each button (also builds up stored mask) ... */
306     if(value & mask)
307       gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), (gboolean) TRUE);
308   }
309 
310   /* Only display the containing widget after the buttons have been created: then we
311      know how many there are, and so can set the way in which the list spans the panel 
312      accordingly PAB 07/05/2009 
313   */
314 
315   if(i>2)
316   {
317     gtk_container_add(GTK_CONTAINER(panel), check_frame);
318   }
319   else
320   {
321     gtk_box_pack_start(GTK_BOX(panel), check_frame, FALSE, FALSE, 0);
322   }
323   gtk_widget_show(check_frame);
324 
325   va_end(ap);
326 }
327 

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