Browse code

Add library init/free, widgetclass register/unregister

Dario Rodriguez authored on 27/03/2022 14:42:39
Showing 1 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
+
Browse code

Put this work under the terms of the zlib license

Dario Rodriguez authored on 27/03/2022 11:06:21
Showing 1 changed files
... ...
@@ -4,7 +4,7 @@
4 4
  * UI toolkit for C modelled after Tk
5 5
  *
6 6
  * Author: Dario Rodriguez antartica@whereismybit.com
7
- * This program in licensed under the terms of the MIT/X license.
7
+ * Please see the file LICENSE.txt in the source's root directory.
8 8
  */
9 9
 
10 10
 #include "widgetlib.h"
Browse code

API skeleton (structs and initial function prototypes)

Dario Rodriguez authored on 22/03/2022 19:08:40
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,10 @@
1
+/*
2
+ * widgetlib.c
3
+ *
4
+ * UI toolkit for C modelled after Tk
5
+ *
6
+ * Author: Dario Rodriguez antartica@whereismybit.com
7
+ * This program in licensed under the terms of the MIT/X license.
8
+ */
9
+
10
+#include "widgetlib.h"