gwenhywfar  4.7.0beta
widget.c
Go to the documentation of this file.
1 /***************************************************************************
2  begin : Wed Jan 20 2010
3  copyright : (C) 2010 by Martin Preuss
4  email : martin@libchipcard.de
5 
6  ***************************************************************************
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, *
21  * MA 02111-1307 USA *
22  * *
23  ***************************************************************************/
24 
25 
26 #ifdef HAVE_CONFIG_H
27 # include <config.h>
28 #endif
29 
30 #define DISABLE_DEBUGLOG
31 
32 
33 #include "widget_p.h"
34 
35 #include <gwenhywfar/text.h>
36 #include <gwenhywfar/debug.h>
37 #include <gwenhywfar/dialog_be.h>
38 
39 #include <assert.h>
40 #include <ctype.h>
41 
42 
43 
44 GWEN_TREE_FUNCTIONS(GWEN_WIDGET, GWEN_Widget)
46 
47 
48 
49 
51  GWEN_WIDGET *w;
52 
54  w->refCount=1;
57 
58  w->dialog=dlg;
59 
60  return w;
61 }
62 
63 
64 
66  if (w) {
67  assert(w->refCount);
68  if (w->refCount>1) {
69  w->refCount--;
70  }
71  else {
72  int i;
73 
76  free(w->name);
77  for (i=0; i<GWEN_WIDGET_TEXTCOUNT; i++)
78  free(w->text[i]);
79  free(w->iconFile);
80  free(w->imageFile);
81  w->refCount=0;
83  }
84  }
85 }
86 
87 
88 
89 
91  assert(w);
92  assert(w->refCount);
93 
94  return w->dialog;
95 }
96 
97 
98 
100  GWEN_DIALOG *dlg;
101  GWEN_DIALOG *pdlg;
102 
103  assert(w);
104  assert(w->refCount);
105 
106  dlg=w->dialog;
107  if (dlg) {
108  while( (pdlg=GWEN_Dialog_GetParentDialog(dlg)) )
109  dlg=pdlg;
110 
111  return w->dialog;
112  }
113  return NULL;
114 }
115 
116 
117 
118 void *GWEN_Widget_GetImplData(const GWEN_WIDGET *w, int index) {
119  assert(w);
120  assert(w->refCount);
121  if (index<GWEN_WIDGET_IMPLDATACOUNT)
122  return w->impl_data[index];
123  else {
124  DBG_ERROR(GWEN_LOGDOMAIN, "Index out of range");
125  return NULL;
126  }
127 }
128 
129 
130 
131 void GWEN_Widget_SetImplData(GWEN_WIDGET *w, int index, void *ptr) {
132  assert(w);
133  assert(w->refCount);
134  if (index<GWEN_WIDGET_IMPLDATACOUNT)
135  w->impl_data[index]=ptr;
136  else {
137  DBG_ERROR(GWEN_LOGDOMAIN, "Index out of range");
138  }
139 }
140 
141 
142 
143 uint32_t GWEN_Widget_GetFlags(const GWEN_WIDGET *w) {
144  assert(w);
145  assert(w->refCount);
146  return w->flags;
147 }
148 
149 
150 
151 void GWEN_Widget_SetFlags(GWEN_WIDGET *w, uint32_t fl) {
152  assert(w);
153  assert(w->refCount);
154  w->flags=fl;
155 }
156 
157 
158 
159 void GWEN_Widget_AddFlags(GWEN_WIDGET *w, uint32_t fl) {
160  assert(w);
161  assert(w->refCount);
162  w->flags|=fl;
163 }
164 
165 
166 
167 void GWEN_Widget_SubFlags(GWEN_WIDGET *w, uint32_t fl) {
168  assert(w);
169  assert(w->refCount);
170  w->flags&=~fl;
171 }
172 
173 
174 
176  assert(w);
177  assert(w->refCount);
178  return w->wtype;
179 }
180 
181 
182 
184  assert(w);
185  assert(w->refCount);
186  w->wtype=t;
187 }
188 
189 
190 
192  assert(w);
193  assert(w->refCount);
194  return w->columns;
195 }
196 
197 
198 
200  assert(w);
201  assert(w->refCount);
202  w->columns=i;
203 }
204 
205 
206 
208  assert(w);
209  assert(w->refCount);
210  return w->rows;
211 }
212 
213 
214 
216  assert(w);
217  assert(w->refCount);
218  w->rows=i;
219 }
220 
221 
222 
224  assert(w);
225  assert(w->refCount);
226  return w->groupId;
227 }
228 
229 
230 
232  assert(w);
233  assert(w->refCount);
234  w->groupId=i;
235 }
236 
237 
238 
240  assert(w);
241  assert(w->refCount);
242  return w->width;
243 }
244 
245 
246 
248  assert(w);
249  assert(w->refCount);
250  w->width=i;
251 }
252 
253 
254 
256  assert(w);
257  assert(w->refCount);
258  return w->height;
259 }
260 
261 
262 
264  assert(w);
265  assert(w->refCount);
266  w->height=i;
267 }
268 
269 
270 
271 const char *GWEN_Widget_GetText(const GWEN_WIDGET *w, int idx) {
272  assert(w);
273  assert(w->refCount);
274  if (idx<0 || idx>=GWEN_WIDGET_TEXTCOUNT)
275  return NULL;
276  return w->text[idx];
277 }
278 
279 
280 
281 void GWEN_Widget_SetText(GWEN_WIDGET *w, int idx, const char *s) {
282  assert(w);
283  assert(w->refCount);
284 
285  if (idx>=0 && idx<GWEN_WIDGET_TEXTCOUNT) {
286  free(w->text[idx]);
287  if (s) w->text[idx]=strdup(s);
288  else w->text[idx]=NULL;
289  }
290 }
291 
292 
293 
294 const char *GWEN_Widget_GetName(const GWEN_WIDGET *w) {
295  assert(w);
296  assert(w->refCount);
297  return w->name;
298 }
299 
300 
301 
302 void GWEN_Widget_SetName(GWEN_WIDGET *w, const char *s) {
303  assert(w);
304  assert(w->refCount);
305  free(w->name);
306  if (s) w->name=strdup(s);
307  else w->name=NULL;
308 }
309 
310 
311 
313  assert(w);
314  assert(w->refCount);
315  return w->iconFile;
316 }
317 
318 
319 
320 void GWEN_Widget_SetIconFileName(GWEN_WIDGET *w, const char *s) {
321  assert(w);
322  assert(w->refCount);
323  free(w->iconFile);
324  if (s) w->iconFile=strdup(s);
325  else w->iconFile=NULL;
326 }
327 
328 
329 
331  assert(w);
332  assert(w->refCount);
333  return w->imageFile;
334 }
335 
336 
337 
338 void GWEN_Widget_SetImageFileName(GWEN_WIDGET *w, const char *s) {
339  assert(w);
340  assert(w->refCount);
341  free(w->imageFile);
342  if (s) w->imageFile=strdup(s);
343  else w->imageFile=NULL;
344 }
345 
346 
347 
348 
349 
351  if (s && *s) {
352  if (strcasecmp(s, "unknown")==0)
354  else if (strcasecmp(s, "none")==0)
355  return GWEN_Widget_TypeNone;
356  else if (strcasecmp(s, "label")==0)
357  return GWEN_Widget_TypeLabel;
358  else if (strcasecmp(s, "pushButton")==0)
360  else if (strcasecmp(s, "lineEdit")==0)
362  else if (strcasecmp(s, "textEdit")==0)
364  else if (strcasecmp(s, "comboBox")==0)
366  else if (strcasecmp(s, "radioButton")==0)
368  else if (strcasecmp(s, "progressBar")==0)
370  else if (strcasecmp(s, "groupBox")==0)
372  else if (strcasecmp(s, "hSpacer")==0)
374  else if (strcasecmp(s, "vSpacer")==0)
376  else if (strcasecmp(s, "hLayout")==0)
378  else if (strcasecmp(s, "vLayout")==0)
380  else if (strcasecmp(s, "gridLayout")==0)
382  else if (strcasecmp(s, "listBox")==0)
384  else if (strcasecmp(s, "dialog")==0)
385  return GWEN_Widget_TypeDialog;
386  else if (strcasecmp(s, "tabBook")==0)
388  else if (strcasecmp(s, "tabPage")==0)
390  else if (strcasecmp(s, "widgetStack")==0)
392  else if (strcasecmp(s, "checkBox")==0)
394  else if (strcasecmp(s, "scrollArea")==0)
396  else if (strcasecmp(s, "hLine")==0)
397  return GWEN_Widget_TypeHLine;
398  else if (strcasecmp(s, "vLine")==0)
399  return GWEN_Widget_TypeVLine;
400  else if (strcasecmp(s, "textBrowser")==0)
402  else if (strcasecmp(s, "spinBox")==0)
404  else {
405  DBG_ERROR(GWEN_LOGDOMAIN, "Unknown widget type [%s]", s);
406  }
407  }
409 }
410 
411 
412 
414  switch(t) {
415  case GWEN_Widget_TypeNone: return "none";
416  case GWEN_Widget_TypeLabel: return "label";
417  case GWEN_Widget_TypePushButton: return "pushButton";
418  case GWEN_Widget_TypeLineEdit: return "lineEdit";
419  case GWEN_Widget_TypeTextEdit: return "textEdit";
420  case GWEN_Widget_TypeComboBox: return "comboBox";
421  case GWEN_Widget_TypeRadioButton: return "radioButton";
422  case GWEN_Widget_TypeProgressBar: return "progressBar";
423  case GWEN_Widget_TypeGroupBox: return "groupBox";
424  case GWEN_Widget_TypeHSpacer: return "hSpacer";
425  case GWEN_Widget_TypeVSpacer: return "vSpacer";
426  case GWEN_Widget_TypeHLayout: return "hLayout";
427  case GWEN_Widget_TypeVLayout: return "vLayout";
428  case GWEN_Widget_TypeGridLayout: return "gridLayout";
429  case GWEN_Widget_TypeListBox: return "listBox";
430  case GWEN_Widget_TypeDialog: return "dialog";
431  case GWEN_Widget_TypeTabBook: return "tabBook";
432  case GWEN_Widget_TypeTabPage: return "tabPage";
433  case GWEN_Widget_TypeWidgetStack: return "widgetStack";
434  case GWEN_Widget_TypeCheckBox: return "checkBox";
435  case GWEN_Widget_TypeScrollArea: return "scrollArea";
436  case GWEN_Widget_TypeHLine: return "hLine";
437  case GWEN_Widget_TypeVLine: return "vLine";
438  case GWEN_Widget_TypeTextBrowser: return "textBrowser";
439  case GWEN_Widget_TypeSpinBox: return "spinBox";
440  case GWEN_Widget_TypeUnknown: return "unknown";
441  }
442 
443  return "unknown";
444 }
445 
446 
447 
448 uint32_t GWEN_Widget_Flags_fromString(const char *s){
449  uint32_t fl=0;
450 
451  if (s && *s) {
452  char *copy;
453  char *p;
454 
455  copy=strdup(s);
456  p=copy;
457 
458  while(*p) {
459  char *wstart;
460 
461  /* skip blanks */
462  while(*p && isspace(*p))
463  p++;
464  /* save start of word */
465  wstart=p;
466 
467  /* find end of word */
468  while(*p && !(isspace(*p) || *p==','))
469  p++;
470  if (*p)
471  /* set blank or comma to 0, advance pointer */
472  *(p++)=0;
473 
474  /* parse flags */
475  if (strcasecmp(wstart, "fillX")==0)
477  else if (strcasecmp(wstart, "fillY")==0)
479  else if (strcasecmp(wstart, "readOnly")==0)
481  else if (strcasecmp(wstart, "password")==0)
483  else if (strcasecmp(wstart, "default")==0)
485  else if (strcasecmp(wstart, "decorShrinkable")==0)
487  else if (strcasecmp(wstart, "decorStretchable")==0)
489  else if (strcasecmp(wstart, "decorMinimize")==0)
491  else if (strcasecmp(wstart, "decorMaximize")==0)
493  else if (strcasecmp(wstart, "decorClose")==0)
495  else if (strcasecmp(wstart, "decorMenu")==0)
497  else if (strcasecmp(wstart, "fixedWidth")==0)
499  else if (strcasecmp(wstart, "fixedHeight")==0)
501  else if (strcasecmp(wstart, "equalWidth")==0)
503  else if (strcasecmp(wstart, "equalHeight")==0)
505  else if (strcasecmp(wstart, "justifyLeft")==0)
507  else if (strcasecmp(wstart, "justifyRight")==0)
509  else if (strcasecmp(wstart, "justifyTop")==0)
511  else if (strcasecmp(wstart, "justifyBottom")==0)
513  else if (strcasecmp(wstart, "justifyCenterX")==0)
515  else if (strcasecmp(wstart, "justifyCenterY")==0)
517  else if (strcasecmp(wstart, "noWordWrap")==0)
519  }
520  }
521 
522  return fl;
523 }
524 
525 
526 
528  const char *s;
529 
530  s=GWEN_XMLNode_GetProperty(node, "name", NULL);
531  if (s && *s)
532  GWEN_Widget_SetName(w, s);
533 
534  s=GWEN_XMLNode_GetProperty(node, "type", "unknown");
535  if (s && *s) {
536  w->wtype=GWEN_Widget_Type_fromString(s);
537  if (w->wtype==GWEN_Widget_TypeUnknown) {
538  DBG_ERROR(GWEN_LOGDOMAIN, "unknown type in node");
539  GWEN_XMLNode_Dump(node, 2);
540  return GWEN_ERROR_BAD_DATA;
541  }
542  }
543  else {
544  DBG_ERROR(GWEN_LOGDOMAIN, "type property missing in node");
545  return GWEN_ERROR_BAD_DATA;
546  }
547 
548  s=GWEN_XMLNode_GetProperty(node, "flags", NULL);
549  if (s && *s)
550  w->flags=GWEN_Widget_Flags_fromString(s);
551 
552  s=GWEN_XMLNode_GetProperty(node, "columns", NULL);
553  if (s && *s) {
554  if (1!=sscanf(s, "%d", &(w->columns))) {
555  DBG_ERROR(GWEN_LOGDOMAIN, "Value [%s] is not an integer", s);
556  return GWEN_ERROR_BAD_DATA;
557  }
558  }
559 
560  s=GWEN_XMLNode_GetProperty(node, "rows", NULL);
561  if (s && *s) {
562  if (1!=sscanf(s, "%d", &(w->rows))) {
563  DBG_ERROR(GWEN_LOGDOMAIN, "Value [%s] is not an integer", s);
564  return GWEN_ERROR_BAD_DATA;
565  }
566  }
567 
568  s=GWEN_XMLNode_GetProperty(node, "width", NULL);
569  if (s && *s) {
570  if (1!=sscanf(s, "%d", &(w->width))) {
571  DBG_ERROR(GWEN_LOGDOMAIN, "Value [%s] is not an integer", s);
572  return GWEN_ERROR_BAD_DATA;
573  }
574  }
575 
576  s=GWEN_XMLNode_GetProperty(node, "height", NULL);
577  if (s && *s) {
578  if (1!=sscanf(s, "%d", &(w->height))) {
579  DBG_ERROR(GWEN_LOGDOMAIN, "Value [%s] is not an integer", s);
580  return GWEN_ERROR_BAD_DATA;
581  }
582  }
583 
584  s=GWEN_XMLNode_GetProperty(node, "text", NULL);
585  if (s && *s)
587 
588  s=GWEN_XMLNode_GetProperty(node, "icon", NULL);
589  if (s && *s)
591 
592  s=GWEN_XMLNode_GetProperty(node, "image", NULL);
593  if (s && *s)
595 
596  s=GWEN_XMLNode_GetProperty(node, "groupId", NULL);
597  if (s && *s) {
598  if (1!=sscanf(s, "%d", &(w->groupId))) {
599  DBG_ERROR(GWEN_LOGDOMAIN, "Value [%s] is not an integer", s);
600  return GWEN_ERROR_BAD_DATA;
601  }
602  }
603 
604  return 0;
605 }
606 
607 
608 
612 
613  assert(w);
614  assert(w->refCount);
615 
616  of=w->setIntPropertyFn;
617  w->setIntPropertyFn=fn;
618  return of;
619 }
620 
621 
622 
626 
627  assert(w);
628  assert(w->refCount);
629 
630  of=w->getIntPropertyFn;
631  w->getIntPropertyFn=fn;
632  return of;
633 }
634 
635 
636 
640 
641  assert(w);
642  assert(w->refCount);
643 
644  of=w->setCharPropertyFn;
645  w->setCharPropertyFn=fn;
646  return of;
647 }
648 
649 
650 
654 
655  assert(w);
656  assert(w->refCount);
657 
658  of=w->getCharPropertyFn;
659  w->getCharPropertyFn=fn;
660  return of;
661 }
662 
663 
664 
668 
669  assert(w);
670  assert(w->refCount);
671 
672  of=w->addChildGuiWidgetFn;
673  w->addChildGuiWidgetFn=fn;
674  return of;
675 }
676 
677 
678 
681  int index,
682  int value,
683  int doSignal) {
684  assert(w);
685  assert(w->refCount);
686 
687  if (w->setIntPropertyFn)
688  return w->setIntPropertyFn(w, prop, index, value, doSignal);
689  else
691 }
692 
693 
694 
697  int index,
698  int defaultValue) {
699  assert(w);
700  assert(w->refCount);
701 
702  if (w->getIntPropertyFn)
703  return w->getIntPropertyFn(w, prop, index, defaultValue);
704  else
705  return defaultValue;
706 }
707 
708 
709 
712  int index,
713  const char *value,
714  int doSignal) {
715  assert(w);
716  assert(w->refCount);
717 
718  if (w->setCharPropertyFn)
719  return w->setCharPropertyFn(w, prop, index, value, doSignal);
720  else
722 }
723 
724 
725 
728  int index,
729  const char *defaultValue) {
730  assert(w);
731  assert(w->refCount);
732 
733  if (w->getCharPropertyFn)
734  return w->getCharPropertyFn(w, prop, index, defaultValue);
735  else
736  return defaultValue;
737 }
738 
739 
740 
742  assert(w);
743  assert(w->refCount);
744 
745  if (w->addChildGuiWidgetFn)
746  return w->addChildGuiWidgetFn(w, wChild);
747  else
749 }
750 
751 
752 
753 
754