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

Linux Cross Reference
Tina6/tina-tools/tinatool/wdgts/gtk2/wdgtsGtk_tw_command.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_command.c,v $
 37  * Date    :  $Date: 2009/03/24 12:58:52 $
 38  * Version :  $Revision: 1.3 $
 39  * CVS Id  :  $Id: wdgtsGtk_tw_command.c,v 1.3 2009/03/24 12:58:52 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_command.c
 47  *  @brief Create Tw_callback structs, and define their associated functionality.
 48  *
 49  *  Functions to create Tw_callbacks ....  
 50  *
 51  *  Also function to record and replay macros.  Current macro file is held as a static 
 52  *  file pointer at the scope of this c file.
 53  *
 54  *  Note that the functions in this file are duplicated for each widget set 
 55  *  (xv, GTK, xm, etc ...).
 56  *
 57  */
 58 
 59 #include "wdgtsGtk_tw_command.h"
 60 
 61 #if HAVE_CONFIG_H
 62 #   include <config.h>
 63 #endif
 64 
 65 #include <stdio.h>
 66 #include <string.h>
 67 #include <math.h>
 68 #include <gtk/gtk.h>
 69 #include <tina/sys/sysDef.h>
 70 #include <tina/sys/sysPro.h>
 71 #include <tina/sys/sysLst_dd.h>
 72 #include <tinatool/tlbase/tlbaseDef.h>
 73 #include <tinatool/tlbase/tlbasePro.h>
 74 #include <tinatool/wdgts/gtk2/wdgts_GtkDef.h>
 75 #include <tinatool/wdgts/gtk2/wdgtsGtk_tw_sv_btn.h>
 76 
 77 
 78 static FILE *fp                 = NULL;
 79 static List *fplist             = NULL;
 80 static List *callback_list      = NULL;
 81 static List *callback_list_end;
 82 static List *callfrom_list      = NULL;
 83 static List *callfrom_list_end;
 84 
 85 
 86 /**
 87  * @brief   Open new macro file.  
 88  * @param   savefile  Name of file to open.
 89  * @param   mode      File open mode (r, w, etc.).
 90  */
 91 void tw_open_call_file(char *savefile, char *mode)
 92 {
 93   char  call_file[64];
 94   List *ref_addtostart();
 95   List *link_rm_el();
 96 
 97   (void) string_append(call_file, savefile, ".cls", NULL);
 98   if (fp != NULL)
 99     fplist = ref_addtostart(fplist, fp, 0);
100   if ((fp = fopen(call_file, mode)) == NULL)
101   {
102     fp = fplist->to;
103     fplist = link_rm_el(fplist);
104     error("Cannot open calls file", non_fatal);
105   }
106 }
107 
108 /**
109  * @brief   Write the current widget selction to the current macro file.  
110  * @param   twc   Pointer to Tw_callback struct of the appropriate widget.
111  * 
112  * Note that fp is a static file pointer at the scope of this file.
113  * The print function is defined in the .c file for the appropriate widget.
114  */
115 void tw_write_callfrom(Tw_callback * twc)
116 {
117   if (fp != NULL&&twc->prnt!=NULL)
118     (*twc->prnt) (fp, twc);
119 }
120 
121 /**
122  * @brief   Add new Tw_callback to the static callback list.  
123  * @param   twc   Pointer to Tw_callback to add to list.
124  * 
125  * Note that callback_list and callback_list_end are static List pointers 
126  * at the scope of this .c file. 
127  */
128 static void tw_register_callback(Tw_callback * twc)
129 {
130   List *ref_addtoend();
131 
132   callback_list_end = ref_addtoend(callback_list_end, twc, 0);
133   if (callback_list == NULL)
134     callback_list = callback_list_end;
135 }
136 
137 /**
138  * @brief   Add Tw_callback struct to the static callfrom_list.  
139  * @param   twc   Pointer to Tw_callback to add to list.
140  * 
141  * I don't get this one ... 
142  * GAB 12 Nov 2003
143  */
144 void tw_register_callfrom(Tw_callback *twc)
145 {
146   List *ref_addtoend();
147 
148   callfrom_list_end = ref_addtoend(callfrom_list_end, twc, 0);
149   if (callfrom_list == NULL)
150     callfrom_list = callfrom_list_end;
151 }
152 
153 /**
154  * @brief   Create new Tw_callback associated with a newly created widget.  
155  * @param   name    Full string name of the new widget.
156  * @param   widget  Pointer to the new GtkWidget.
157  * @param   call()  Pointer to standard callback function.
158  * @param   cmnd()  Pointer to macro file command replay function.
159  * @param   prnt()  Pointer to macro file command record function.
160  * @param   func()  .
161  * @param   data1   Void pointer to data associated with the widget.
162  * @param   data2   Void pointer to data associated with the widget.
163  * @return  Tw_callback   The new Tw_callback struct.
164  * 
165  * Tw_callback is a generic structure to allow several callback functions and data items to be 
166  * to be associated with the same widget.  For example, data2 is sometimes used to record the
167  * tool on which the widget was created, as macro files may need to restore this tool.
168  */
169 Tw_callback *tw_callback_make(char *name, 
170                               GtkWidget *widget, 
171                               void (*call)(), void (*cmnd)( ), void (*prnt) (), void (*func) (), 
172                               void *data1, void *data2)
173 {
174   Tw_callback *twc = ts_ralloc(Tw_callback);
175 
176   twc->name = name;
177   twc->widget = widget;
178   twc->call = call;
179   twc->cmnd = cmnd;
180   twc->prnt = prnt;
181   twc->func = func;
182   twc->data1 = data1;
183   twc->data2 = data2;
184 
185   tw_register_callback(twc);
186   return (twc);
187 }
188 
189 
190 /**
191  * @brief   Invoke the callback registered in the Tw_callback struct.  
192  * @param   twc   Pointer to Tw_callback associated with the appropriate widget.
193  * 
194  * The callback function is defined in the .c file for the appropriate widget.
195  * If the user has started macro recording, the widget selection is recorded in the
196  * macro file (also as defined in the widget .c file ...). 
197  */
198 void tw_callback(Tw_callback *twc)
199 {
200 
201   if (twc->call != NULL)
202     (*twc->call)(twc);
203   if (tw_save())
204     tw_write_callfrom(twc);
205 }
206 
207 
208 /**
209  * @brief   Callback function for general GTK widgets.  
210  * @param   widget  Pointer to the GtkWidget associated with the callback.
211  * @param   data    Pointer to the data associated with the GtkWidget.
212  * @return  gint  Standard GTK return flag (not used in Tina but required).
213  * 
214  * Simply passes on callback.  Required only to match GTK callback signature.
215  */
216 gint   tw_gtk_callback(GtkWidget *widget, gpointer data)
217 {
218     Tw_callback *twc = (Tw_callback *) data;
219 
220     tw_callback(twc);
221 }
222 
223 /**
224  * @brief   Close the macro file.  
225  */
226 void tw_close_call_file(void)
227 {
228   List *ptr;
229   List *link_rm_el();
230 
231   if (fp == NULL)
232     return;
233 
234   for (ptr = callfrom_list; ptr != NULL; ptr = ptr->next)
235   {
236     Tw_callback *twc = (Tw_callback *) ptr->to;
237 
238     if (twc->prnt != NULL)
239       (*twc->prnt) (fp, twc);
240   }
241 
242   (void) fclose(fp);
243 
244   if (fplist != NULL)
245   {
246     fp = fplist->to;
247     fplist = link_rm_el(fplist);
248   } 
249   else
250     fp = NULL;
251 }
252 
253 /**
254  * @brief   Run macro command (from macro file).  
255  * @param   name   String name of command (from macro file).
256  * @param   args   String arguments passed to command.
257  */
258 void tw_run_cmnd(char *name, char *args)
259 {
260   List *ptr;
261   char *str1;
262   char *str2;
263   int fail = 0;
264 
265   /** Uses the static callback_list ... */
266   for (ptr = callback_list; ptr != NULL; ptr = ptr->next)
267   {
268     Tw_callback *twc = (Tw_callback *) ptr->to;
269 
270     if (strcmp(twc->name, name) == 0 && twc->cmnd != NULL)
271     {
272       (*twc->cmnd)(twc, args);
273       fail = 1;
274     }
275   }
276 
277   for (ptr = callback_list; fail != 1 && ptr != NULL; ptr = ptr->next)
278   {
279     Tw_callback *twc = (Tw_callback *) ptr->to;
280 
281     str1 = strchr(twc->name, '.');
282     str2 = strchr(name, '.');
283     if (str1 && str2 && strcmp(str1, str2) == 0 && twc->cmnd != NULL)
284       (*twc->cmnd)(twc, args);
285   }
286 }
287 
288 /**
289  * @brief   Read and execute macro file..  
290  * @param   macrofile   String name of macro file to read and execute.
291  * 
292  * Xv Tina uses fp instead of fp_local.  Easily confused with static fp
293  * by idiots like me, so I changed it. 
294  * GAB 14 Nov 2003.
295  */
296 void tw_read_call_file(char *macrofile)
297 {
298   char name[64];
299   char args[65535]; /* Expanded from 256 to allow saving of curve widget data PAB 19/8/1009 */
300   char callback_file[64];
301 
302   FILE *fp_local;    /** As opposed to static file pointer, fp */
303 
304   (void) string_append(callback_file, macrofile, ".cls", NULL);
305   fp_local = fopen(callback_file, "r");
306 
307   if (fp_local == NULL)
308   {
309     format("macro file not found\n");
310     return;
311   }
312 
313   while (freadline(fp_local, args) != EOF)
314   {
315     (void) sscanf(args, "%s", name);
316     tw_run_cmnd(name, args);
317   }
318 
319   (void) fclose(fp_local);
320 }
321 
322 /**
323  * @brief   Replace spaces in string with underscores.  
324  * @param   str   String in which to replace the spaces..
325  * 
326  * Mostly used with tw_extend_fullname(). 
327  */
328 static void replace_spaces(char *str)
329 {
330   int i;
331 
332   for (i = 0; i < strlen(str); i++)
333     if (str[i] == ' ')
334       str[i] = '_';
335 }
336 
337 /**
338  * @brief   Extend widget name to include its full hierarchy.  
339  * @param   name       String with widget's short name.
340  * @param   extension  String with name extension.
341  * @return  char *     String with extended name.
342  */
343 char *tw_extend_fullname(char *name, char *extension)
344 {
345   char   *fullname;
346 
347   if (extension == NULL)
348   {
349     fullname = string_copy(name);
350     replace_spaces(fullname);
351   } 
352   else
353   {
354     fullname = string_alloc_append(name, ".", extension, NULL);
355     replace_spaces(fullname);
356   }
357 
358   return (fullname);
359 }
360 

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