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

Linux Cross Reference
Tina4/src/TCLwidgets/tw_choice.c

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

  1 /*
  2   tw_choice.c
  3 */
  4 
  5 #include <stdio.h>
  6 #include <tina/sys.h>
  7 #include <tcl.h>
  8 #include "tclWt.h"
  9 #include "tclWtPro.h"
 10 
 11 
 12 static void choice_call(Tw_callback * twc)
 13 {
 14         int value = *((int *)twc->data1);
 15 
 16         (*twc->func) (value);
 17 }
 18 
 19 
 20 static char *ttcl_writetrace_choice(ClientData cdata, Tcl_Interp *interp,
 21                                                         char *name1, char *name2, int flags)
 22 {
 23         static Bool      mask = false;
 24         char *string_alloc_append();
 25         List         *twlist = (List *)cdata;
 26         Tw_callback  *twc, *twc2; 
 27         int           set;
 28         char         *tclvalue, *butname = NULL, *oldname;
 29 
 30   if (!name2 || !twlist)
 31                 return ("choice update failed");
 32 
 33         if (mask)
 34                 return (NULL);
 35 
 36         mask = true;
 37 
 38   tclvalue = Tcl_GetVar2(interp, name1, name2, flags);
 39 
 40         /*
 41                  would be nice to know the previously value but turning 'off'
 42                  will have to do
 43         */
 44         if (Tcl_GetBoolean(interp, tclvalue, &set) != TCL_OK)
 45         {
 46                 Tcl_SetVar2(interp, name1, name2, "off", flags);
 47                 mask = false;
 48                 return ("positive boolean argument required, element turned off");
 49         }
 50 
 51         if (!set)
 52         {
 53                 mask = false;
 54                 return ("cannot unset choice");
 55         }
 56 
 57         /* 
 58                  simulataneously find the new button by name and the last 
 59                  button as the last one set 'on'
 60                  (tclvalue can be reused as tcl controls gc for its assigned
 61                  variables)
 62   */
 63         set = 0;
 64         while(twlist) 
 65         {
 66                 if (!butname && ((twc = (Tw_callback *)(twlist->to)) != NULL))
 67                 {
 68                         butname = (char *)(twc->data2);
 69                         if (strcmp(name2, butname)) 
 70                                 butname = NULL;
 71                 }
 72                 if (!set && ((twc2 = (Tw_callback *)(twlist->to)) != NULL))
 73                 {
 74                         oldname = (char *)(twc2->data2);
 75                         tclvalue = Tcl_GetVar2(interp, name1, oldname, flags);
 76                         Tcl_GetBoolean(interp, tclvalue, &set);
 77                 }
 78                 twlist = twlist->next;
 79         }
 80 
 81         if (!twc || !butname)
 82         {
 83                 mask = false;
 84                 return ("element unknown");
 85         }
 86 
 87         if (!twc2 || !set)
 88         {
 89                 mask = false;
 90                 return ("choice update failed");
 91         }
 92 
 93         choice_call(twc);
 94         Tcl_SetVar2(interp, name1, oldname, "off", flags);
 95 
 96         mask = false;
 97 
 98   return NULL;
 99 }
100 
101 
102 
103 static void choice_cmnd(Tw_callback * twc, char *args)
104 {
105         int value;
106 
107         (void) sscanf(args, "%*s %d", &value);
108         (*twc->func) (value);
109 }
110 
111 
112 static void choice_prnt(FILE * fp, Tw_callback * twc)
113 {
114         int value = (int) twc->data1;
115 
116         (void) fprintf(fp, "%s %d %d\n", twc->name, value);
117 }
118 
119 
120 void    *tw_choice(char *name, ...)
121 {
122         Tcl_Interp *interp;
123         Tw_callback *twc;
124         List *twlist = NULL;
125         va_list ap;
126         void (*func) ();
127         int *vparam, value, i;
128         char *string_alloc_append(), *string_copy();
129         char *tclname, *butname, *element;
130         char *fullname = tw_extend_fullname((char *)tw_get_toolname(), name);
131 
132         if ((interp = ttcl_get_interp()) == NULL)
133                 return;
134 
135         tclname = ttcl_mkshellname(fullname, TTCLCHOICE);
136 
137         va_start(ap, name);
138         func = (void (*)()) va_arg(ap, void *);
139         value = va_arg(ap, int);
140 
141         for (i = 0; (butname = va_arg(ap, char *)) != NULL; i++)
142         {
143                 vparam = (int *) ralloc(sizeof(int));
144                 *vparam = i;
145                 twc = tw_callback_make(fullname, NULL, choice_call, choice_cmnd, 
146                                 choice_prnt, func, (void *) vparam, string_copy(butname));
147                 twlist = ref_addtostart(twlist, twc, 0);
148                 element = string_alloc_append(tclname, "(", butname, ")", NULL);
149                 if (i == value)
150                         Tcl_SetVar(interp, element, "on", 0); 
151                 else
152                         Tcl_SetVar(interp, element, "off", 0); 
153                 rfree(element);
154         }
155 
156         twlist = list_reverse(twlist);
157         va_end(ap);
158 
159         Tcl_TraceVar(interp, tclname, TCL_TRACE_WRITES, ttcl_writetrace_choice, 
160                         (ClientData)twlist);
161         rfree(tclname);
162 
163         (*func) (value);
164 
165         return (twlist);
166 }
167 
168 
169 
170 void tw_choice_reset(Tw_callback * twc, int value)
171 {
172 }
173 

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