ui/common/types.c

changeset 110
c00e968d018b
parent 109
c3dfcb8f0be7
child 113
dde28a806552
equal deleted inserted replaced
109:c3dfcb8f0be7 110:c00e968d018b
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 }
387 char* ui_string_get(UiString* s) { 415 char* ui_string_get(UiString* s) {
388 if (s) { 416 if (s) {
389 return s->get ? s->get(s) : s->value.ptr; 417 return s->get ? s->get(s) : s->value.ptr;
390 } 418 }
391 else { 419 else {
392 return 0; 420 return NULL;
393 } 421 }
394 } 422 }
395 423
396 void ui_text_set(UiText* s, const char* value) { 424 void ui_text_set(UiText* s, const char* value) {
397 if (s) { 425 if (s) {
417 char* ui_text_get(UiText* s) { 445 char* ui_text_get(UiText* s) {
418 if (s) { 446 if (s) {
419 return s->get ? s->get(s) : s->value.ptr; 447 return s->get ? s->get(s) : s->value.ptr;
420 } 448 }
421 else { 449 else {
450 return NULL;
451 }
452 }
453
454 void ui_range_set(UiRange *r, double value) {
455 if (r) {
456 if (r->set) {
457 r->set(r, value);
458 } else {
459 r->value = value;
460 }
461 }
462 }
463
464 void ui_range_set_range(UiRange *r, double min, double max) {
465 if (r) {
466 if (r->setrange) {
467 r->setrange(r, min, max);
468 } else {
469 r->min = min;
470 r->max = max;
471 }
472 }
473 }
474
475 void ui_range_set_extent(UiRange *r, double extent) {
476 if (r) {
477 if (r->setextent) {
478 r->setextent(r, extent);
479 } else {
480 r->extent = extent;
481 }
482 }
483 }
484
485 double ui_range_get(UiRange *r) {
486 if (r) {
487 return r->get ? r->get(r) : r->value;
488 } else {
489 return 0;
490 }
491 }
492
493 double ui_range_get_min(UiRange *r) {
494 if (r) {
495 return r->min;
496 } else {
497 return 0;
498 }
499 }
500
501 double ui_range_get_max(UiRange *r) {
502 if (r) {
503 return r->max;
504 } else {
505 return 0;
506 }
507 }
508
509 double ui_range_get_extent(UiRange *r) {
510 if (r) {
511 return r->extent;
512 } else {
422 return 0; 513 return 0;
423 } 514 }
424 } 515 }
425 516
426 void ui_generic_set_image(UiGeneric *g, void *img) { 517 void ui_generic_set_image(UiGeneric *g, void *img) {
578 r->obj = NULL; 669 r->obj = NULL;
579 } 670 }
580 671
581 void uic_list_unbind(UiList *l) { 672 void uic_list_unbind(UiList *l) {
582 l->update = NULL; 673 l->update = NULL;
674 l->getselection = NULL;
675 l->setselection = NULL;
583 l->obj = NULL; 676 l->obj = NULL;
584 } 677 }
585 678
586 void uic_generic_unbind(UiGeneric *g) { 679 void uic_generic_unbind(UiGeneric *g) {
587 g->get = NULL; 680 g->get = NULL;
597 } 690 }
598 return (UiListSelection){ 0, NULL }; 691 return (UiListSelection){ 0, NULL };
599 } 692 }
600 693
601 UIEXPORT void ui_list_setselection(UiList *list, int index) { 694 UIEXPORT void ui_list_setselection(UiList *list, int index) {
602 if (list->setselection && index >= 0) { 695 if (list->setselection) {
603 UiListSelection sel; 696 UiListSelection sel = { 0, NULL };
604 sel.count = 1; 697 if(index >= 0) {
605 sel.rows = &index; 698 sel.count = 1;
699 sel.rows = &index;
700 }
606 list->setselection(list, sel); 701 list->setselection(list, sel);
607 } 702 }
608 } 703 }
609 704
610 UIEXPORT void ui_listselection_free(UiListSelection selection) { 705 UIEXPORT void ui_listselection_free(UiListSelection selection) {

mercurial