ui/common/types.c

changeset 964
c563220d9aea
parent 906
edfdf9776da9
child 965
5d4419042d9b
equal deleted inserted replaced
963:90b349cdd47f 964:c563220d9aea
210 char *name; 210 char *name;
211 } UiColumn; 211 } UiColumn;
212 212
213 UiModel* ui_model(UiContext *ctx, ...) { 213 UiModel* ui_model(UiContext *ctx, ...) {
214 UiModel *info = ui_calloc(ctx, 1, sizeof(UiModel)); 214 UiModel *info = ui_calloc(ctx, 1, sizeof(UiModel));
215 info->ctx = ctx;
215 216
216 va_list ap; 217 va_list ap;
217 va_start(ap, ctx); 218 va_start(ap, ctx);
218 219
219 CxList *cols = cxArrayListCreate(cxDefaultAllocator, NULL, sizeof(UiColumn), 32); 220 CxList *cols = cxArrayListCreate(cxDefaultAllocator, NULL, sizeof(UiColumn), 32);
251 252
252 #define UI_MODEL_DEFAULT_ALLOC_SIZE 16 253 #define UI_MODEL_DEFAULT_ALLOC_SIZE 16
253 254
254 UiModel* ui_model_new(UiContext *ctx) { 255 UiModel* ui_model_new(UiContext *ctx) {
255 UiModel *info = ui_calloc(ctx, 1, sizeof(UiModel)); 256 UiModel *info = ui_calloc(ctx, 1, sizeof(UiModel));
257 info->ctx = ctx;
256 info->alloc = UI_MODEL_DEFAULT_ALLOC_SIZE; 258 info->alloc = UI_MODEL_DEFAULT_ALLOC_SIZE;
257 info->types = ui_calloc(ctx, UI_MODEL_DEFAULT_ALLOC_SIZE, sizeof(UiModelType)); 259 info->types = ui_calloc(ctx, UI_MODEL_DEFAULT_ALLOC_SIZE, sizeof(UiModelType));
258 info->titles = ui_calloc(ctx, UI_MODEL_DEFAULT_ALLOC_SIZE, sizeof(char*)); 260 info->titles = ui_calloc(ctx, UI_MODEL_DEFAULT_ALLOC_SIZE, sizeof(char*));
259 info->columnsize = ui_calloc(ctx, UI_MODEL_DEFAULT_ALLOC_SIZE, sizeof(int)); 261 info->columnsize = ui_calloc(ctx, UI_MODEL_DEFAULT_ALLOC_SIZE, sizeof(int));
260 return info; 262 return info;
275 277
276 UiModel* ui_model_copy(UiContext *ctx, UiModel* model) { 278 UiModel* ui_model_copy(UiContext *ctx, UiModel* model) {
277 const CxAllocator* a = ctx ? ctx->allocator : cxDefaultAllocator; 279 const CxAllocator* a = ctx ? ctx->allocator : cxDefaultAllocator;
278 280
279 UiModel* newmodel = cxMalloc(a, sizeof(UiModel)); 281 UiModel* newmodel = cxMalloc(a, sizeof(UiModel));
282 newmodel->ctx = ctx;
280 *newmodel = *model; 283 *newmodel = *model;
281 284
282 newmodel->types = cxCalloc(a, model->columns, sizeof(UiModelType)); 285 newmodel->types = cxCalloc(a, model->columns, sizeof(UiModelType));
283 memcpy(newmodel->types, model->types, model->columns); 286 memcpy(newmodel->types, model->types, model->columns);
284 287
290 memcpy(newmodel->columnsize, model->columnsize, model->columns*sizeof(int)); 293 memcpy(newmodel->columnsize, model->columnsize, model->columns*sizeof(int));
291 294
292 return newmodel; 295 return newmodel;
293 } 296 }
294 297
295 void ui_model_free(UiContext *ctx, UiModel *mi) { 298 void ui_model_ref(UiModel *model) {
296 const CxAllocator* a = ctx ? ctx->allocator : cxDefaultAllocator; 299 model->ref++;
300 }
301
302 void ui_model_unref(UiModel *model) {
303 if(--model->ref == 0) {
304 ui_model_free(model);
305 }
306 }
307
308 void ui_model_free(UiModel *mi) {
309 UiContext *ctx = mi->ctx;
310 const CxAllocator* a = ctx->allocator;
297 for(int i=0;i<mi->columns;i++) { 311 for(int i=0;i<mi->columns;i++) {
298 ui_free(ctx, mi->titles[i]); 312 ui_free(ctx, mi->titles[i]);
299 } 313 }
300 cxFree(a, mi->types); 314 cxFree(a, mi->types);
301 cxFree(a, mi->titles); 315 cxFree(a, mi->titles);
733 list->setselection(list, sel); 747 list->setselection(list, sel);
734 } 748 }
735 } 749 }
736 750
737 UIEXPORT void ui_listselection_free(UiListSelection selection) { 751 UIEXPORT void ui_listselection_free(UiListSelection selection) {
738 if (selection.rows) { 752 free(selection.rows);
739 free(selection.rows);
740 }
741 } 753 }
742 754
743 UIEXPORT UiStr ui_str(char *cstr) { 755 UIEXPORT UiStr ui_str(char *cstr) {
744 return (UiStr) { cstr, NULL }; 756 return (UiStr) { cstr, NULL };
745 } 757 }

mercurial