Browse code

Add library init/free, widgetclass register/unregister

Dario Rodriguez authored on 27/03/2022 14:42:39
Showing 2 changed files
... ...
@@ -7,4 +7,206 @@
7 7
  * Please see the file LICENSE.txt in the source's root directory.
8 8
  */
9 9
 
10
+#include <stdio.h>
11
+#include <stdlib.h>
12
+#include <unistd.h>
13
+#include <string.h>
10 14
 #include "widgetlib.h"
15
+
16
+#define BLOCKWICLASS 16
17
+
18
+wi_t *
19
+wi_init(void)
20
+{
21
+        wi_t *wi;
22
+        if((wi=malloc(sizeof(wi_t)))==NULL)
23
+                return(NULL); /* insuf. mem. */
24
+        memset(wi,0,sizeof(wi_t));
25
+        return(wi);
26
+}
27
+
28
+void
29
+wi_free(wi_t *wi)
30
+{
31
+        int i;
32
+        if(wi==NULL)
33
+                return; /* nothing to do */
34
+        for(i=wi->usedclasses-1;i>=0 && i<wi->usedclasses;i--)
35
+                wi_class_unregister(wi,wi->classes[i].classname);
36
+        if(wi->classes!=NULL)
37
+                free(wi->classes),wi->classes=NULL,wi->sizeclasses=wi->usedclasses=0;
38
+        free(wi),wi=NULL;
39
+        return;
40
+}
41
+
42
+int
43
+wi_class_register(wi_t *wi, char *classname, int (*classcallback)(void *wi, wiclass_t *cl, widget_t *widget, wiclassop_t *op))
44
+{
45
+        wiclass_t *cl;
46
+        wiclassop_t clop;
47
+        if(wi==NULL || classname==NULL || classcallback==NULL)
48
+                return(-1); /* sanity check error */
49
+        wi_class_unregister(wi,classname);
50
+        if(wi->sizeclasses==wi->usedclasses) {
51
+                wiclass_t *newclasses;
52
+                int newsize;
53
+                newsize=wi->sizeclasses+BLOCKWICLASS;
54
+                if((newclasses=realloc(wi->classes,newsize*sizeof(wiclass_t)))==NULL)
55
+                        return(-1); /* insuf. mem. */
56
+                wi->classes=newclasses;
57
+                memset(newclasses+wi->sizeclasses,0,sizeof(wiclass_t)*(newsize-wi->sizeclasses));
58
+                wi->sizeclasses=newsize;
59
+        }
60
+        cl=wi->classes+wi->usedclasses;
61
+        memset(cl,0,sizeof(wiclass_t));
62
+        memset(&clop,0,sizeof(clop));
63
+        clop.op=classop_initclass;
64
+        cl->classcallback=classcallback;
65
+        if((cl->classname=strdup(classname))==NULL || classcallback(wi,cl,NULL,&clop)!=0) {
66
+                if(cl->classname!=NULL)
67
+                        free(cl->classname),cl->classname=NULL;
68
+                if(cl->classglobaldata!=NULL)
69
+                        free(cl->classglobaldata),cl->classglobaldata=NULL,cl->sizeclassglobaldata=0;
70
+                cl->classcallback=NULL;
71
+                return(-1); /* insuf. mem. */
72
+        }
73
+        wi->usedclasses++;
74
+        return(0);
75
+}
76
+
77
+int
78
+wi_class_unregister(wi_t *wi, char *classname)
79
+{
80
+        int i;
81
+        wiclass_t *cl;
82
+        wiclassop_t clop;
83
+        if(wi==NULL || classname==NULL)
84
+                return(-1); /* sanity check failed */
85
+        /* search for class starting from last (as the usual user of this function is wi_free(), and it frees classes starting from the last one) */
86
+        for(i=wi->usedclasses-1;i>=0;i--) {
87
+                if(strcmp(wi->classes[i].classname,classname)==0)
88
+                        break;
89
+        }
90
+        if(i<0)
91
+                return(-1); /* unknown classname */
92
+        cl=wi->classes+i;
93
+        memset(&clop,0,sizeof(clop));
94
+        clop.op=classop_freeclass;
95
+        cl->classcallback(wi,cl,NULL,&clop);
96
+        if(cl->classname!=NULL)
97
+                free(cl->classname),cl->classname=NULL;
98
+        if(cl->classglobaldata!=NULL)
99
+                free(cl->classglobaldata),cl->classglobaldata=NULL,cl->sizeclassglobaldata=0;
100
+        cl->classcallback=NULL;
101
+        memmove(wi->classes+i,wi->classes+i+1,(wi->usedclasses-(i+1))*sizeof(wiclass_t));
102
+        memset(wi->classes+(wi->usedclasses-1),0,sizeof(wiclass_t));
103
+        wi->usedclasses--;
104
+        return(0);
105
+}
106
+
107
+int
108
+wi_toplevel_set(wi_t *wi, char *name)
109
+{
110
+#warning TODO
111
+        return(-1);
112
+}
113
+
114
+int
115
+wi_toplevel_destroy(wi_t *wi, char *name)
116
+{
117
+#warning TODO
118
+        return(-1);
119
+}
120
+
121
+char *
122
+wi_add(wi_t *wi, char *debugfile, int debugline, char *class, char *path, char *format,...)
123
+{
124
+#warning TODO
125
+        return(NULL);
126
+}
127
+
128
+char *
129
+wi_config(wi_t *wi, char *debugfile, int debugline, char *path, char *format,...)
130
+{
131
+#warning TODO
132
+        return(NULL);
133
+}
134
+
135
+int
136
+wi_pack(wi_t *wi, char *debugfile, int debugline, char *path, char *format,...)
137
+{
138
+#warning TODO
139
+        return(-1);
140
+}
141
+
142
+int
143
+wi_destroy(wi_t *wi, char *path)
144
+{
145
+#warning TODO
146
+        return(-1);
147
+}
148
+
149
+int
150
+wi_alias_set(wi_t *wi, char *alias, char *path)
151
+{
152
+#warning TODO
153
+        return(-1);
154
+}
155
+
156
+int
157
+wi_alias_unset(wi_t *wi, char *alias)
158
+{
159
+#warning TODO
160
+        return(-1);
161
+}
162
+
163
+int
164
+wi_tree_move(wi_t *wi, char *path, char *newparent)
165
+{
166
+#warning TODO
167
+        return(-1);
168
+}
169
+
170
+int
171
+wi_var_updated(wi_t *wi, char *vardata, int sizevardata)
172
+{
173
+#warning TODO
174
+        return(-1);
175
+}
176
+
177
+int wi_var_destroy(wi_t *wi, char *vardata, int sizevardata)
178
+{
179
+#warning TODO
180
+        return(-1);
181
+}
182
+
183
+int
184
+wi_font_load(wi_t *wi, char *filename, char *fontname)
185
+{
186
+#warning TODO
187
+        return(-1);
188
+}
189
+
190
+/* default widgets */
191
+int
192
+wi_classcallback_frame(void *wi, wiclass_t *cl, widget_t *widget, wiclassop_t *op)
193
+{
194
+#warning TODO
195
+        return(-1);
196
+}
197
+
198
+int
199
+wi_classcallback_label(void *wi, wiclass_t *cl, widget_t *widget, wiclassop_t *op)
200
+{
201
+#warning TODO
202
+        return(-1);
203
+}
204
+
205
+int
206
+wi_classcallback_image(void *wi, wiclass_t *cl, widget_t *widget, wiclassop_t *op)
207
+{
208
+#warning TODO
209
+        return(-1);
210
+}
211
+
212
+
... ...
@@ -157,7 +157,7 @@ typedef struct wiclass_t {
157 157
         char *classname;
158 158
         int sizeclassglobaldata;
159 159
         char *classglobaldata;
160
-        void (*classcallback)(void *wi, struct wiclass_t *cl, widget_t *widget, wiclassop_t *op);
160
+        int (*classcallback)(void *wi, struct wiclass_t *cl, widget_t *widget, wiclassop_t *op);
161 161
 } wiclass_t;
162 162
 
163 163
 typedef struct wifontinstance_t {
... ...
@@ -194,10 +194,10 @@ typedef struct wi_t {
194 194
         wifont_t *fonts;
195 195
 } wi_t;
196 196
 
197
-wi_t wi_init(void);
197
+wi_t *wi_init(void);
198 198
 void wi_free(wi_t *wi);
199 199
 
200
-int wi_class_register(wi_t *wi, char *classname, void (*classcallback)(void *wi, wiclass_t *cl, widget_t *widget, wiclassop_t *op));
200
+int wi_class_register(wi_t *wi, char *classname, int (*classcallback)(void *wi, wiclass_t *cl, widget_t *widget, wiclassop_t *op));
201 201
 int wi_class_unregister(wi_t *wi, char *classname);
202 202
 
203 203
 int wi_toplevel_set(wi_t *wi, char *name);
... ...
@@ -208,9 +208,9 @@ char *wi_add(wi_t *wi, char *debugfile, int debugline, char *class, char *path,
208 208
 char *wi_config(wi_t *wi, char *debugfile, int debugline, char *path, char *format,...);
209 209
 int wi_pack(wi_t *wi, char *debugfile, int debugline, char *path, char *format,...);
210 210
 #else
211
-  char *wi_add(wi_t *wi, char *debugfile, int debugline, char *class, char *path, char *format,...) __attribute__ ((format (printf,6,7));
212
-  char *wi_config(wi_t *wi, char *debugfile, int debugline, char *path, char *format,...) __attribute__ ((format (printf,5,6));
213
-  int wi_pack(wi_t *wi, char *debugfile, int debugline, char *path, char *format,...) __attribute__ ((format (printf,5,6));
211
+  char *wi_add(wi_t *wi, char *debugfile, int debugline, char *class, char *path, char *format,...) __attribute__ ((format (printf,6,7)));
212
+  char *wi_config(wi_t *wi, char *debugfile, int debugline, char *path, char *format,...) __attribute__ ((format (printf,5,6)));
213
+  int wi_pack(wi_t *wi, char *debugfile, int debugline, char *path, char *format,...) __attribute__ ((format (printf,5,6)));
214 214
 #endif
215 215
 int wi_destroy(wi_t *wi, char *path);
216 216
 
... ...
@@ -225,8 +225,8 @@ int wi_var_destroy(wi_t *wi, char *vardata, int sizevardata);
225 225
 int wi_font_load(wi_t *wi, char *filename, char *fontname);
226 226
 
227 227
 /* default widgets */
228
-void wi_classcallback_frame(void *wi, wiclass_t *cl, widget_t *widget, wiclassop_t *op);
229
-void wi_classcallback_label(void *wi, wiclass_t *cl, widget_t *widget, wiclassop_t *op);
230
-void wi_classcallback_image(void *wi, wiclass_t *cl, widget_t *widget, wiclassop_t *op);
228
+int wi_classcallback_frame(void *wi, wiclass_t *cl, widget_t *widget, wiclassop_t *op);
229
+int wi_classcallback_label(void *wi, wiclass_t *cl, widget_t *widget, wiclassop_t *op);
230
+int wi_classcallback_image(void *wi, wiclass_t *cl, widget_t *widget, wiclassop_t *op);
231 231
 #endif
232 232