ui/common/types.c

branch
newapi
changeset 174
0358f1d9c506
parent 167
161511838ea6
child 215
1bd5534c395d
equal deleted inserted replaced
173:809581724cc7 174:0358f1d9c506
29 #include <stdio.h> 29 #include <stdio.h>
30 #include <stdlib.h> 30 #include <stdlib.h>
31 #include <string.h> 31 #include <string.h>
32 #include <stdarg.h> 32 #include <stdarg.h>
33 33
34 #include <ucx/list.h> 34 #include <cx/list.h>
35 #include <cx/array_list.h>
35 #include "../ui/tree.h" 36 #include "../ui/tree.h"
36 #include "types.h" 37 #include "types.h"
37 #include "context.h" 38 #include "context.h"
38 39
39 UiObserver* ui_observer_new(ui_callback f, void *data) { 40 UiObserver* ui_observer_new(ui_callback f, void *data) {
97 list->next = ui_list_next; 98 list->next = ui_list_next;
98 list->get = ui_list_get; 99 list->get = ui_list_get;
99 list->count = ui_list_count; 100 list->count = ui_list_count;
100 list->observers = NULL; 101 list->observers = NULL;
101 102
102 list->data = NULL; 103 list->data = cxArrayListCreate(cxDefaultAllocator, NULL, CX_STORE_POINTERS, 32);
103 list->iter = NULL; 104 list->iter = NULL;
104 105
105 list->update = NULL; 106 list->update = NULL;
106 list->obj = NULL; 107 list->obj = NULL;
107 108
111 112
112 return list; 113 return list;
113 } 114 }
114 115
115 void ui_list_free(UiList *list) { 116 void ui_list_free(UiList *list) {
116 ucx_list_free(list->data); 117 cxListDestroy(list->data);
117 free(list); 118 free(list);
118 } 119 }
119 120
120 void* ui_list_first(UiList *list) { 121 void* ui_list_first(UiList *list) {
121 UcxList *elm = list->data; 122 list->iter = (void*)(intptr_t)0;
122 list->iter = elm; 123 return cxListAt(list->data, 0);
123 return elm ? elm->data : NULL;
124 } 124 }
125 125
126 void* ui_list_next(UiList *list) { 126 void* ui_list_next(UiList *list) {
127 UcxList *elm = list->iter; 127 intptr_t iter = (intptr_t)list->iter;
128 iter++;
129 void *elm = cxListAt(list->data, iter);
128 if(elm) { 130 if(elm) {
129 elm = elm->next; 131 list->iter = (void*)iter;
130 if(elm) { 132 }
131 list->iter = elm; 133 return elm;
132 return elm->data;
133 }
134 }
135 return NULL;
136 } 134 }
137 135
138 void* ui_list_get(UiList *list, int i) { 136 void* ui_list_get(UiList *list, int i) {
139 UcxList *elm = ucx_list_get(list->data, i); 137 return cxListAt(list->data, i);
140 if(elm) {
141 list->iter = elm;
142 return elm->data;
143 } else {
144 return NULL;
145 }
146 } 138 }
147 139
148 int ui_list_count(UiList *list) { 140 int ui_list_count(UiList *list) {
149 UcxList *elm = list->data; 141 return ((CxList*)list->data)->size;
150 return (int)ucx_list_size(elm);
151 } 142 }
152 143
153 void ui_list_append(UiList *list, void *data) { 144 void ui_list_append(UiList *list, void *data) {
154 list->data = ucx_list_append(list->data, data); 145 cxListAdd(list->data, data);
155 } 146 }
156 147
157 void ui_list_prepend(UiList *list, void *data) { 148 void ui_list_prepend(UiList *list, void *data) {
158 list->data = ucx_list_prepend(list->data, data); 149 cxListInsert(list->data, 0, data);
159 } 150 }
160 151
161 void ui_list_clear(UiList *list) { 152 void ui_list_clear(UiList *list) {
162 ucx_list_free(list->data); 153 cxListClear(list->data);
163 list->data = NULL;
164 } 154 }
165 155
166 void ui_list_addobsv(UiList *list, ui_callback f, void *data) { 156 void ui_list_addobsv(UiList *list, ui_callback f, void *data) {
167 list->observers = ui_add_observer(list->observers, f, data); 157 list->observers = ui_add_observer(list->observers, f, data);
168 } 158 }
181 UiModel *info = ui_calloc(ctx, 1, sizeof(UiModel)); 171 UiModel *info = ui_calloc(ctx, 1, sizeof(UiModel));
182 172
183 va_list ap; 173 va_list ap;
184 va_start(ap, ctx); 174 va_start(ap, ctx);
185 175
186 UcxList *cols = NULL; 176 CxList *cols = cxArrayListCreate(cxDefaultAllocator, NULL, sizeof(UiColumn), 32);
187 int type; 177 int type;
188 while((type = va_arg(ap, int)) != -1) { 178 while((type = va_arg(ap, int)) != -1) {
189 char *name = va_arg(ap, char*); 179 char *name = va_arg(ap, char*);
190 180
191 UiColumn *column = malloc(sizeof(UiColumn)); 181 UiColumn column;
192 column->type = type; 182 column.type = type;
193 column->name = name; 183 column.name = name;
194 184
195 cols = ucx_list_append(cols, column); 185 cxListAdd(cols, &column);
196 } 186 }
197 187
198 va_end(ap); 188 va_end(ap);
199 189
200 size_t len = ucx_list_size(cols); 190 size_t len = cols->size;
201 info->columns = len; 191 info->columns = len;
202 info->types = ui_calloc(ctx, len, sizeof(UiModelType)); 192 info->types = ui_calloc(ctx, len, sizeof(UiModelType));
203 info->titles = ui_calloc(ctx, len, sizeof(char*)); 193 info->titles = ui_calloc(ctx, len, sizeof(char*));
204 194
205 int i = 0; 195 int i = 0;
206 UCX_FOREACH(elm, cols) { 196 CxIterator iter = cxListIterator(cols);
207 UiColumn *c = elm->data; 197 cx_foreach(UiColumn*, c, iter) {
208 info->types[i] = c->type; 198 info->types[i] = c->type;
209 info->titles[i] = c->name; 199 info->titles[i] = c->name;
210 free(c);
211 i++; 200 i++;
212 } 201 }
213 ucx_list_free(cols); 202 cxListDestroy(cols);
214 203
215 return info; 204 return info;
216 } 205 }
217 206
218 void ui_model_free(UiContext *ctx, UiModel *mi) { 207 void ui_model_free(UiContext *ctx, UiModel *mi) {
219 ucx_mempool_free(ctx->mempool, mi->types); 208 cxFree(ctx->allocator, mi->types);
220 ucx_mempool_free(ctx->mempool, mi->titles); 209 cxFree(ctx->allocator, mi->titles);
221 ucx_mempool_free(ctx->mempool, mi); 210 cxFree(ctx->allocator, mi);
222 } 211 }
223 212
224 // types 213 // types
225 214
226 // public functions 215 // public functions

mercurial