ui/common/context.c

changeset 117
b174e721663e
parent 115
e57ca2747782
equal deleted inserted replaced
116:376ef91111f6 117:b174e721663e
57 UiContext* uic_context(UiObject *toplevel, CxMempool *mp) { 57 UiContext* uic_context(UiObject *toplevel, CxMempool *mp) {
58 UiContext *ctx = cxMalloc(mp->allocator, sizeof(UiContext)); 58 UiContext *ctx = cxMalloc(mp->allocator, sizeof(UiContext));
59 memset(ctx, 0, sizeof(UiContext)); 59 memset(ctx, 0, sizeof(UiContext));
60 ctx->mp = mp; 60 ctx->mp = mp;
61 ctx->allocator = mp->allocator; 61 ctx->allocator = mp->allocator;
62 ctx->destroy_handler = cxArrayListCreate(ctx->allocator, NULL, sizeof(UiDestroyHandler), 16); 62 ctx->destroy_handler = cxArrayListCreate(ctx->allocator, sizeof(UiDestroyHandler), 16);
63 ctx->obj = toplevel; 63 ctx->obj = toplevel;
64 ctx->vars = cxHashMapCreate(mp->allocator, CX_STORE_POINTERS, 16); 64 ctx->vars = cxHashMapCreate(mp->allocator, CX_STORE_POINTERS, 16);
65 65
66 ctx->documents = cxLinkedListCreate(mp->allocator, cx_cmp_ptr, CX_STORE_POINTERS); 66 ctx->documents = cxLinkedListCreate(mp->allocator, CX_STORE_POINTERS);
67 ctx->state_widgets = cxLinkedListCreate(mp->allocator, cx_cmp_ptr, sizeof(UiStateWidget)); 67 ctx->state_widgets = cxLinkedListCreate(mp->allocator, sizeof(UiStateWidget));
68 ctx->states = cxArrayListCreate(mp->allocator, cx_cmp_int, sizeof(int), 32); 68 ctx->states = cxArrayListCreate(mp->allocator, sizeof(int), 32);
69 cxSetCompareFunc(ctx->states, cx_cmp_int);
69 70
70 ctx->attach_document = uic_context_attach_document; 71 ctx->attach_document = uic_context_attach_document;
71 ctx->detach_document2 = uic_context_detach_document; 72 ctx->detach_document2 = uic_context_detach_document;
72 73
73 #if UI_GTK2 || UI_GTK3 74 #if UI_GTK2 || UI_GTK3
171 docctx->parent = NULL; 172 docctx->parent = NULL;
172 } 173 }
173 174
174 void uic_context_detach_all(UiContext *ctx) { 175 void uic_context_detach_all(UiContext *ctx) {
175 // copy list 176 // copy list
176 CxList *ls = cxLinkedListCreate(cxDefaultAllocator, NULL, CX_STORE_POINTERS); 177 CxList *ls = cxLinkedListCreate(cxDefaultAllocator, CX_STORE_POINTERS);
177 CxIterator i = cxListIterator(ctx->documents); 178 CxIterator i = cxListIterator(ctx->documents);
178 cx_foreach(void *, doc, i) { 179 cx_foreach(void *, doc, i) {
179 cxListAdd(ls, doc); 180 cxListAdd(ls, doc);
180 } 181 }
181 182
588 void ui_widget_set_states(UiContext *ctx, UIWIDGET widget, ui_enablefunc enable, ...) { 589 void ui_widget_set_states(UiContext *ctx, UIWIDGET widget, ui_enablefunc enable, ...) {
589 if(enable == NULL) { 590 if(enable == NULL) {
590 enable = (ui_enablefunc)ui_set_enabled; 591 enable = (ui_enablefunc)ui_set_enabled;
591 } 592 }
592 // get states 593 // get states
593 CxList *states = cxArrayListCreate(cxDefaultAllocator, NULL, sizeof(int), 16); 594 CxList *states = cxArrayListCreate(cxDefaultAllocator, sizeof(int), 16);
594 va_list ap; 595 va_list ap;
595 va_start(ap, enable); 596 va_start(ap, enable);
596 int state; 597 int state;
597 while((state = va_arg(ap, int)) != -1) { 598 while((state = va_arg(ap, int)) != -1) {
598 cxListAdd(states, &state); 599 cxListAdd(states, &state);
606 607
607 void ui_widget_set_states2(UiContext *ctx, UIWIDGET widget, ui_enablefunc enable, const int *states, int nstates) { 608 void ui_widget_set_states2(UiContext *ctx, UIWIDGET widget, ui_enablefunc enable, const int *states, int nstates) {
608 if(enable == NULL) { 609 if(enable == NULL) {
609 enable = (ui_enablefunc)ui_set_enabled; 610 enable = (ui_enablefunc)ui_set_enabled;
610 } 611 }
611 CxList *ls = cxArrayListCreate(cxDefaultAllocator, NULL, sizeof(int), nstates); 612 CxList *ls = cxArrayListCreate(cxDefaultAllocator, sizeof(int), nstates);
612 for(int i=0;i<nstates;i++) { 613 for(int i=0;i<nstates;i++) {
613 cxListAdd(ls, states+i); 614 cxListAdd(ls, states+i);
614 } 615 }
615 uic_add_state_widget(ctx, widget, enable, ls); 616 uic_add_state_widget(ctx, widget, enable, ls);
616 cxListFree(ls); 617 cxListFree(ls);
692 693
693 void ui_set_destructor(void *mem, ui_destructor_func destr) { 694 void ui_set_destructor(void *mem, ui_destructor_func destr) {
694 cxMempoolSetDestructor(mem, (cx_destructor_func)destr); 695 cxMempoolSetDestructor(mem, (cx_destructor_func)destr);
695 } 696 }
696 697
698 void ui_var_set_int(UiContext *ctx, const char *name, int64_t value) {
699 UiInteger *i = ui_get_int_var(ctx, name);
700 if(i) {
701 ui_set(i, value);
702 }
703 }
704
705 int64_t ui_var_get_int(UiContext *ctx, const char *name) {
706 UiInteger *i = ui_get_int_var(ctx, name);
707 if(i) {
708 return ui_get(i);
709 }
710 return 0;
711 }
712
713 void ui_var_set_double(UiContext *ctx, const char *name, double value) {
714 UiDouble *d = ui_get_double_var(ctx, name);
715 if(d) {
716 ui_set(d, value);
717 }
718 }
719
720 double ui_var_get_double(UiContext *ctx, const char *name) {
721 UiDouble *d = ui_get_double_var(ctx, name);
722 if(d) {
723 return ui_get(d);
724 }
725 return 0;
726 }
727
728 void ui_var_set_string(UiContext *ctx, const char *name, char *value) {
729 UiString *s = ui_get_string_var(ctx, name);
730 if(s) {
731 ui_set(s, value);
732 }
733 }
734
735 char* ui_var_get_string(UiContext *ctx, const char *name) {
736 UiString *s = ui_get_string_var(ctx, name);
737 if(s) {
738 return ui_get(s);
739 }
740 return NULL;
741 }
742
743 UIEXPORT void ui_var_add_observer(UiContext *ctx, const char *varname, ui_callback f, void *data) {
744 UiVar *var = uic_get_var(ctx, varname);
745 if(!var) {
746 return;
747 }
748
749 switch(var->type) {
750 case UI_VAR_INTEGER: {
751 UiInteger *v = var->value;
752 v->observers = ui_add_observer(v->observers, f, data);
753 break;
754 }
755 case UI_VAR_DOUBLE: {
756 UiDouble *v = var->value;
757 v->observers = ui_add_observer(v->observers, f, data);
758 break;
759 }
760 case UI_VAR_RANGE: {
761 UiRange *v = var->value;
762 v->observers = ui_add_observer(v->observers, f, data);
763 break;
764 }
765 case UI_VAR_STRING: {
766 UiString *v = var->value;
767 v->observers = ui_add_observer(v->observers, f, data);
768 break;
769 }
770 case UI_VAR_TEXT: {
771 UiText *v = var->value;
772 v->observers = ui_add_observer(v->observers, f, data);
773 break;
774 }
775 case UI_VAR_LIST: {
776 UiList *v = var->value;
777 v->observers = ui_add_observer(v->observers, f, data);
778 break;
779 }
780 }
781 }
782
783 void ui_int_add_observer(UiInteger *i, ui_callback f, void *data) {
784 i->observers = ui_add_observer(i->observers, f, data);
785 }
786
787 void ui_double_add_observer(UiDouble *d, ui_callback f, void *data) {
788 d->observers = ui_add_observer(d->observers, f, data);
789 }
790
791 void ui_range_add_observer(UiRange *r, ui_callback f, void *data) {
792 r->observers = ui_add_observer(r->observers, f, data);
793 }
794
795 void ui_string_add_observer(UiString *s, ui_callback f, void *data) {
796 s->observers = ui_add_observer(s->observers, f, data);
797 }
798
799 void ui_text_add_observer(UiText *t, ui_callback f, void *data) {
800 t->observers = ui_add_observer(t->observers, f, data);
801 }
802
803 void ui_list_add_observer(UiList *l, ui_callback f, void *data) {
804 l->observers = ui_add_observer(l->observers, f, data);
805 }
806
697 UiInteger* ui_get_int_var(UiContext *ctx, const char *name) { 807 UiInteger* ui_get_int_var(UiContext *ctx, const char *name) {
698 UiVar *var = uic_get_var_t(ctx, name, UI_VAR_INTEGER); 808 UiVar *var = uic_get_var_t(ctx, name, UI_VAR_INTEGER);
699 return var ? var->value : NULL; 809 return var ? var->value : NULL;
700 } 810 }
701 811
717 UiRange* ui_get_range_var(UiContext *ctx, const char *name) { 827 UiRange* ui_get_range_var(UiContext *ctx, const char *name) {
718 UiVar *var = uic_get_var_t(ctx, name, UI_VAR_RANGE); 828 UiVar *var = uic_get_var_t(ctx, name, UI_VAR_RANGE);
719 return var ? var->value : NULL; 829 return var ? var->value : NULL;
720 } 830 }
721 831
832 UIEXPORT UiList* ui_get_list_var(UiContext *ctx, const char *name) {
833 UiVar *var = uic_get_var_t(ctx, name, UI_VAR_LIST);
834 return var ? var->value : NULL;
835 }
836
722 UiGeneric* ui_get_generic_var(UiContext *ctx, const char *name) { 837 UiGeneric* ui_get_generic_var(UiContext *ctx, const char *name) {
723 UiVar *var = uic_get_var_t(ctx, name, UI_VAR_GENERIC); 838 UiVar *var = uic_get_var_t(ctx, name, UI_VAR_GENERIC);
724 return var ? var->value : NULL; 839 return var ? var->value : NULL;
725 } 840 }

mercurial