ui/common/types.c

changeset 728
011d236c4a05
parent 693
3e1cd004da0a
child 737
ac7f0869e8ae
equal deleted inserted replaced
727:de40bc4811b1 728:011d236c4a05
218 } 218 }
219 219
220 va_end(ap); 220 va_end(ap);
221 221
222 size_t len = cxListSize(cols); 222 size_t len = cxListSize(cols);
223 info->alloc = len;
223 info->columns = len; 224 info->columns = len;
224 info->types = ui_calloc(ctx, len, sizeof(UiModelType)); 225 info->types = ui_calloc(ctx, len, sizeof(UiModelType));
225 info->titles = ui_calloc(ctx, len, sizeof(char*)); 226 info->titles = ui_calloc(ctx, len, sizeof(char*));
226 info->columnsize = ui_calloc(ctx, len, sizeof(int)); 227 info->columnsize = ui_calloc(ctx, len, sizeof(int));
227 228
228 int i = 0; 229 int i = 0;
229 CxIterator iter = cxListIterator(cols); 230 CxIterator iter = cxListIterator(cols);
230 cx_foreach(UiColumn*, c, iter) { 231 cx_foreach(UiColumn*, c, iter) {
231 info->types[i] = c->type; 232 info->types[i] = c->type;
232 info->titles[i] = c->name; 233 info->titles[i] = ui_strdup(ctx, c->name);
233 i++; 234 i++;
234 } 235 }
235 cxListFree(cols); 236 cxListFree(cols);
236 237
237 return info; 238 return info;
239 }
240
241 #define UI_MODEL_DEFAULT_ALLOC_SIZE 16
242
243 UiModel* ui_model_new(UiContext *ctx) {
244 UiModel *info = ui_calloc(ctx, 1, sizeof(UiModel));
245 info->alloc = UI_MODEL_DEFAULT_ALLOC_SIZE;
246 info->types = ui_calloc(ctx, UI_MODEL_DEFAULT_ALLOC_SIZE, sizeof(UiModelType));
247 info->titles = ui_calloc(ctx, UI_MODEL_DEFAULT_ALLOC_SIZE, sizeof(char*));
248 info->columnsize = ui_calloc(ctx, UI_MODEL_DEFAULT_ALLOC_SIZE, sizeof(int));
249 return info;
250 }
251
252 void ui_model_add_column(UiContext *ctx, UiModel *model, UiModelType type, const char *title, int width) {
253 if(model->columns >= model->alloc) {
254 model->alloc += UI_MODEL_DEFAULT_ALLOC_SIZE;
255 model->types = ui_realloc(ctx, model->types, model->alloc * sizeof(UiModelType));
256 model->titles = ui_realloc(ctx, model->titles, model->alloc * sizeof(char*));
257 model->columnsize = ui_realloc(ctx, model->columnsize, model->alloc * sizeof(int));
258 }
259 model->types[model->columns] = type;
260 model->titles[model->columns] = ui_strdup(ctx, title);
261 model->columnsize[model->columns] = width;
262 model->columns++;
238 } 263 }
239 264
240 UiModel* ui_model_copy(UiContext *ctx, UiModel* model) { 265 UiModel* ui_model_copy(UiContext *ctx, UiModel* model) {
241 const CxAllocator* a = ctx ? ctx->allocator : cxDefaultAllocator; 266 const CxAllocator* a = ctx ? ctx->allocator : cxDefaultAllocator;
242 267
256 return newmodel; 281 return newmodel;
257 } 282 }
258 283
259 void ui_model_free(UiContext *ctx, UiModel *mi) { 284 void ui_model_free(UiContext *ctx, UiModel *mi) {
260 const CxAllocator* a = ctx ? ctx->allocator : cxDefaultAllocator; 285 const CxAllocator* a = ctx ? ctx->allocator : cxDefaultAllocator;
286 for(int i=0;i<mi->columns;i++) {
287 ui_free(ctx, mi->titles[i]);
288 }
261 cxFree(a, mi->types); 289 cxFree(a, mi->types);
262 cxFree(a, mi->titles); 290 cxFree(a, mi->titles);
263 cxFree(a, mi->columnsize); 291 cxFree(a, mi->columnsize);
264 cxFree(a, mi); 292 cxFree(a, mi);
265 } 293 }

mercurial