1 /********************************************************************************
2 * *
3 * D a t a T a r g e t *
4 * *
5 *********************************************************************************
6 * Copyright (C) 1997,2005 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU Lesser General Public *
10 * License as published by the Free Software Foundation; either *
11 * version 2.1 of the License, or (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16 * Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with this library; if not, write to the Free Software *
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
21 *********************************************************************************
22 * $Id: FXDataTarget.cpp,v 1.33 2005/01/16 16:06:06 fox Exp $ *
23 ********************************************************************************/
24 #include "xincs.h"
25 #include "fxver.h"
26 #include "fxdefs.h"
27 #include "fxkeys.h"
28 #include "FXHash.h"
29 #include "FXThread.h"
30 #include "FXStream.h"
31 #include "FXString.h"
32 #include "FXSize.h"
33 #include "FXPoint.h"
34 #include "FXRectangle.h"
35 #include "FXSettings.h"
36 #include "FXRegistry.h"
37 #include "FXApp.h"
38 #include "FXWindow.h"
39 #include "FXDataTarget.h"
40
41
42 /*
43 Notes:
44 - DataTarget connects GUI to basic values such as flags (FXbool), integral
45 or real numbers, and strings (FXString).
46 - Values in the application program may get updated from the GUI, and
47 vice-versa GUI gets updated when the program has changed a value as well.
48 - Would be nice to set value from message ID also...
49 - When the sender of onCmdValue does not understand the ID_GETXXXXVALUE message,
50 the data target keeps the same value as before.
51 - Catch SEL_CHANGED when we have expunged this from FXTextField.
52 - DT_VOID, i.e. unconnected FXDataTarget maybe it should grey out corresponding
53 widgets.
54 - Need to add ID_GETLONGVALUE/ID_SETLONGVALUE message handlers some day.
55 */
56
57 using namespace FX;
58
59 /*******************************************************************************/
60
61 namespace FX {
62
63 // Map
64 FXDEFMAP(FXDataTarget) FXDataTargetMap[]={
65 FXMAPFUNC(SEL_COMMAND,FXDataTarget::ID_VALUE,FXDataTarget::onCmdValue),
66 FXMAPFUNC(SEL_CHANGED,FXDataTarget::ID_VALUE,FXDataTarget::onCmdValue),
67 FXMAPFUNC(SEL_UPDATE,FXDataTarget::ID_VALUE,FXDataTarget::onUpdValue),
68 FXMAPFUNCS(SEL_COMMAND,FXDataTarget::ID_OPTION-10001,FXDataTarget::ID_OPTION+10000,FXDataTarget::onCmdOption),
69 FXMAPFUNCS(SEL_UPDATE,FXDataTarget::ID_OPTION-10001,FXDataTarget::ID_OPTION+10000,FXDataTarget::onUpdOption),
70 };
71
72
73 // Object implementation
FXIMPLEMENT(FXDataTarget,FXObject,FXDataTargetMap,ARRAYNUMBER (FXDataTargetMap))74 FXIMPLEMENT(FXDataTarget,FXObject,FXDataTargetMap,ARRAYNUMBER(FXDataTargetMap))
75
76
77 // Value changed from widget
78 long FXDataTarget::onCmdValue(FXObject* sender,FXSelector sel,void*){
79 FXdouble d;
80 FXint i;
81 switch(type){
82 case DT_CHAR:
83 i=*((FXchar*)data);
84 sender->handle(this,FXSEL(SEL_COMMAND,FXWindow::ID_GETINTVALUE),(void*)&i);
85 *((FXchar*)data)=i;
86 break;
87 case DT_UCHAR:
88 i=*((FXuchar*)data);
89 sender->handle(this,FXSEL(SEL_COMMAND,FXWindow::ID_GETINTVALUE),(void*)&i);
90 *((FXuchar*)data)=i;
91 break;
92 case DT_SHORT:
93 i=*((FXshort*)data);
94 sender->handle(this,FXSEL(SEL_COMMAND,FXWindow::ID_GETINTVALUE),(void*)&i);
95 *((FXshort*)data)=i;
96 break;
97 case DT_USHORT:
98 i=*((FXushort*)data);
99 sender->handle(this,FXSEL(SEL_COMMAND,FXWindow::ID_GETINTVALUE),(void*)&i);
100 *((FXushort*)data)=i;
101 break;
102 case DT_INT:
103 sender->handle(this,FXSEL(SEL_COMMAND,FXWindow::ID_GETINTVALUE),data);
104 break;
105 case DT_UINT:
106 sender->handle(this,FXSEL(SEL_COMMAND,FXWindow::ID_GETINTVALUE),data);
107 break;
108 case DT_LONG:
109 i=(FXint) *((FXlong*)data);
110 sender->handle(this,FXSEL(SEL_COMMAND,FXWindow::ID_GETINTVALUE),(void*)&i);
111 *((FXlong*)data)=i;
112 break;
113 case DT_ULONG:
114 i=(FXint) *((FXulong*)data);
115 sender->handle(this,FXSEL(SEL_COMMAND,FXWindow::ID_GETINTVALUE),(void*)&i);
116 *((FXulong*)data)=i;
117 break;
118 case DT_FLOAT:
119 d=*((FXfloat*)data);
120 sender->handle(this,FXSEL(SEL_COMMAND,FXWindow::ID_GETREALVALUE),(void*)&d);
121 *((FXfloat*)data)=(FXfloat)d;
122 break;
123 case DT_DOUBLE:
124 sender->handle(this,FXSEL(SEL_COMMAND,FXWindow::ID_GETREALVALUE),data);
125 break;
126 case DT_STRING:
127 sender->handle(this,FXSEL(SEL_COMMAND,FXWindow::ID_GETSTRINGVALUE),data);
128 break;
129 }
130 if(target){
131 target->handle(this,FXSEL(FXSELTYPE(sel),message),data);
132 }
133 return 1;
134 }
135
136
137 // Widget changed from value
onUpdValue(FXObject * sender,FXSelector,void *)138 long FXDataTarget::onUpdValue(FXObject* sender,FXSelector,void*){
139 FXdouble d;
140 FXint i;
141 switch(type){
142 case DT_CHAR:
143 i=*((FXchar*)data);
144 sender->handle(this,FXSEL(SEL_COMMAND,FXWindow::ID_SETINTVALUE),(void*)&i);
145 break;
146 case DT_UCHAR:
147 i=*((FXuchar*)data);
148 sender->handle(this,FXSEL(SEL_COMMAND,FXWindow::ID_SETINTVALUE),(void*)&i);
149 break;
150 case DT_SHORT:
151 i=*((FXshort*)data);
152 sender->handle(this,FXSEL(SEL_COMMAND,FXWindow::ID_SETINTVALUE),(void*)&i);
153 break;
154 case DT_USHORT:
155 i=*((FXushort*)data);
156 sender->handle(this,FXSEL(SEL_COMMAND,FXWindow::ID_SETINTVALUE),(void*)&i);
157 break;
158 case DT_INT:
159 sender->handle(this,FXSEL(SEL_COMMAND,FXWindow::ID_SETINTVALUE),data);
160 break;
161 case DT_UINT:
162 sender->handle(this,FXSEL(SEL_COMMAND,FXWindow::ID_SETINTVALUE),data);
163 break;
164 case DT_LONG:
165 i=(FXint) *((FXlong*)data);
166 sender->handle(this,FXSEL(SEL_COMMAND,FXWindow::ID_SETINTVALUE),(void*)&i);
167 break;
168 case DT_ULONG:
169 i=(FXint) *((FXulong*)data);
170 sender->handle(this,FXSEL(SEL_COMMAND,FXWindow::ID_SETINTVALUE),(void*)&i);
171 break;
172 case DT_FLOAT:
173 d=*((FXfloat*)data);
174 sender->handle(this,FXSEL(SEL_COMMAND,FXWindow::ID_SETREALVALUE),(void*)&d);
175 break;
176 case DT_DOUBLE:
177 sender->handle(this,FXSEL(SEL_COMMAND,FXWindow::ID_SETREALVALUE),data);
178 break;
179 case DT_STRING:
180 sender->handle(this,FXSEL(SEL_COMMAND,FXWindow::ID_SETSTRINGVALUE),data);
181 break;
182 }
183 return 1;
184 }
185
186
187 // Value set from message id
onCmdOption(FXObject *,FXSelector sel,void *)188 long FXDataTarget::onCmdOption(FXObject*,FXSelector sel,void*){
189 FXint num=((FXint)FXSELID(sel))-ID_OPTION;
190 switch(type){
191 case DT_CHAR:
192 *((FXchar*)data)=num;
193 break;
194 case DT_UCHAR:
195 *((FXuchar*)data)=num;
196 break;
197 case DT_SHORT:
198 *((FXshort*)data)=num;
199 break;
200 case DT_USHORT:
201 *((FXushort*)data)=num;
202 break;
203 case DT_INT:
204 *((FXint*)data)=num;
205 break;
206 case DT_UINT:
207 *((FXuint*)data)=num;
208 break;
209 case DT_LONG:
210 *((FXlong*)data)=num;
211 break;
212 case DT_ULONG:
213 *((FXulong*)data)=num;
214 break;
215 case DT_FLOAT:
216 *((FXfloat*)data)=(FXfloat)num;
217 break;
218 case DT_DOUBLE:
219 *((FXdouble*)data)=num;
220 break;
221 }
222 if(target){
223 target->handle(this,FXSEL(FXSELTYPE(sel),message),data);
224 }
225 return 1;
226 }
227
228
229 // Check widget whose message id matches
onUpdOption(FXObject * sender,FXSelector sel,void *)230 long FXDataTarget::onUpdOption(FXObject* sender,FXSelector sel,void*){
231 FXint num=((FXint)FXSELID(sel))-ID_OPTION;
232 FXint i=0;
233 switch(type){
234 case DT_CHAR:
235 i=*((FXchar*)data);
236 break;
237 case DT_UCHAR:
238 i=*((FXuchar*)data);
239 break;
240 case DT_SHORT:
241 i=*((FXshort*)data);
242 break;
243 case DT_USHORT:
244 i=*((FXushort*)data);
245 break;
246 case DT_INT:
247 i=*((FXint*)data);
248 break;
249 case DT_UINT:
250 i=*((FXuint*)data);
251 break;
252 case DT_LONG:
253 i=(FXint) *((FXlong*)data);
254 break;
255 case DT_ULONG:
256 i=(FXint) *((FXulong*)data);
257 break;
258 case DT_FLOAT:
259 i=(FXint) *((FXfloat*)data);
260 break;
261 case DT_DOUBLE:
262 i=(FXint) *((FXdouble*)data);
263 break;
264 }
265 if(i==num){
266 sender->handle(this,FXSEL(SEL_COMMAND,FXWindow::ID_CHECK),NULL);
267 }
268 else{
269 sender->handle(this,FXSEL(SEL_COMMAND,FXWindow::ID_UNCHECK),NULL);
270 }
271 return 1;
272 }
273
274
275 /// Destroy
~FXDataTarget()276 FXDataTarget::~FXDataTarget(){
277 target=(FXObject*)-1L;
278 data=(void*)-1L;
279 }
280
281 }
282
283