Sun, 07 Dec 2025 15:50:20 +0100
rename tree.h to list.h
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 2017 Olaf Wintermann. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdarg.h> #include <cx/list.h> #include <cx/array_list.h> #include "../ui/list.h" #include "types.h" #include "context.h" #include "../ui/image.h" static ui_list_init_func default_list_init; static void *default_list_init_userdata; static ui_list_destroy_func default_list_destroy; static void *default_list_destroy_userdata; UiObserver* ui_observer_new(ui_callback f, void *data) { UiObserver *observer = malloc(sizeof(UiObserver)); observer->callback = f; observer->data = data; observer->next = NULL; return observer; } UiObserver* ui_obsvlist_add(UiObserver *list, UiObserver *observer) { if(!list) { return observer; } else { UiObserver *l = list; while(l->next) { l = l->next; } l->next = observer; return list; } } UiObserver* ui_add_observer(UiObserver *list, ui_callback f, void *data) { UiObserver *observer = ui_observer_new(f, data); return ui_obsvlist_add(list, observer); } void ui_notify(UiObserver *observer, void *data) { ui_notify_except(observer, NULL, data); } void ui_notify_except(UiObserver *observer, UiObserver *exc, void *data) { UiEvent evt; evt.obj = NULL; evt.window = NULL; evt.document = NULL; evt.eventdata = data; evt.eventdatatype = UI_EVENT_DATA_POINTER; evt.intval = 0; while(observer) { if(observer != exc) { observer->callback(&evt, observer->data); } observer = observer->next; } } void ui_notify_evt(UiObserver *observer, UiEvent *event) { while(observer) { observer->callback(event, observer->data); observer = observer->next; } } /* --------------------------- UiList --------------------------- */ void uic_ucx_list_init(UiContext *ctx, UiList *list, void *unused) { list->data = cxArrayListCreate(ctx->mp->allocator, NULL, CX_STORE_POINTERS, 32); list->first = ui_list_first; list->next = ui_list_next; list->get = ui_list_get; list->count = ui_list_count; } void uic_ucx_list_destroy(UiContext *ctx, UiList *list, void *unused) { cxListFree(list->data); ui_free(ctx, list); } UiList* ui_list_new(UiContext *ctx, const char *name) { return ui_list_new2(ctx, name, default_list_init ? default_list_init : uic_ucx_list_init, default_list_init_userdata); } UiList* ui_list_new2(UiContext *ctx, const char *name, ui_list_init_func listinit, void *userdata) { UiList *list = cxMalloc(ctx->mp->allocator, sizeof(UiList)); memset(list, 0, sizeof(UiList)); listinit(ctx, list, userdata); if(name) { uic_reg_var(ctx, name, UI_VAR_LIST, list); } return list; } void ui_list_free(UiContext *ctx, UiList *list) { if(!default_list_destroy) { uic_ucx_list_destroy(ctx, list, NULL); } else { default_list_destroy(ctx, list, default_list_destroy_userdata); } } void* ui_list_first(UiList *list) { list->iter = (void*)(intptr_t)0; return cxListAt(list->data, 0); } void* ui_list_next(UiList *list) { intptr_t iter = (intptr_t)list->iter; iter++; void *elm = cxListAt(list->data, iter); if(elm) { list->iter = (void*)iter; } return elm; } void* ui_list_get(UiList *list, int i) { return cxListAt(list->data, i); } int ui_list_count(UiList *list) { return cxListSize(list->data); } void ui_list_append(UiList *list, void *data) { cxListAdd(list->data, data); } void ui_list_prepend(UiList *list, void *data) { cxListInsert(list->data, 0, data); } void ui_list_remove(UiList *list, int i) { cxListRemove(list->data, i); } void ui_list_clear(UiList *list) { cxListClear(list->data); } void ui_list_update(UiList *list) { if(list->update) { list->update(list, -1); } } void ui_list_update_row(UiList *list, int row) { if(list->update) { list->update(list, row); } } UiListSelection ui_list_get_selection(UiList *list) { if(list->getselection) { return list->getselection(list); } else { return (UiListSelection){0, NULL}; } } void ui_list_addobsv(UiList *list, ui_callback f, void *data) { list->observers = ui_add_observer(list->observers, f, data); } void ui_list_notify(UiList *list) { ui_notify(list->observers, list); } typedef struct { int type; char *name; } UiColumn; UiModel* ui_model(UiContext *ctx, ...) { UiModel *info = ui_calloc(ctx, 1, sizeof(UiModel)); info->ctx = ctx; va_list ap; va_start(ap, ctx); CxList *cols = cxArrayListCreate(cxDefaultAllocator, NULL, sizeof(UiColumn), 32); int type; while((type = va_arg(ap, int)) != -1) { char *name = va_arg(ap, char*); UiColumn column; column.type = type; column.name = name; cxListAdd(cols, &column); } va_end(ap); size_t len = cxListSize(cols); info->alloc = len; info->columns = len; info->types = ui_calloc(ctx, len, sizeof(UiModelType)); info->titles = ui_calloc(ctx, len, sizeof(char*)); info->columnsize = ui_calloc(ctx, len, sizeof(int)); int i = 0; CxIterator iter = cxListIterator(cols); cx_foreach(UiColumn*, c, iter) { info->types[i] = c->type; info->titles[i] = ui_strdup(ctx, c->name); i++; } cxListFree(cols); return info; } #define UI_MODEL_DEFAULT_ALLOC_SIZE 16 static void model_notify_observer(UiModel *model, int insert, int delete) { UiModelChangeObserver *obs = model->observer; while(obs) { obs->update(model, obs->userdata, insert, delete); obs = obs->next; } } UiModel* ui_model_new(UiContext *ctx) { UiModel *info = ui_calloc(ctx, 1, sizeof(UiModel)); info->ctx = ctx; info->alloc = UI_MODEL_DEFAULT_ALLOC_SIZE; info->types = ui_calloc(ctx, UI_MODEL_DEFAULT_ALLOC_SIZE, sizeof(UiModelType)); info->titles = ui_calloc(ctx, UI_MODEL_DEFAULT_ALLOC_SIZE, sizeof(char*)); info->columnsize = ui_calloc(ctx, UI_MODEL_DEFAULT_ALLOC_SIZE, sizeof(int)); return info; } void ui_model_add_column(UiModel *model, UiModelType type, const char *title, int width) { UiContext *ctx = model->ctx; if(model->columns >= model->alloc) { model->alloc += UI_MODEL_DEFAULT_ALLOC_SIZE; model->types = ui_realloc(ctx, model->types, model->alloc * sizeof(UiModelType)); model->titles = ui_realloc(ctx, model->titles, model->alloc * sizeof(char*)); model->columnsize = ui_realloc(ctx, model->columnsize, model->alloc * sizeof(int)); } int index = model->columns; model->types[index] = type; model->titles[index] = ui_strdup(ctx, title); model->columnsize[index] = width; model_notify_observer(model, index, -1); model->columns++; } UiModel* ui_model_copy(UiContext *ctx, UiModel* model) { const CxAllocator* a = ctx ? ctx->allocator : cxDefaultAllocator; UiModel* newmodel = cxMalloc(a, sizeof(UiModel)); newmodel->ctx = ctx; *newmodel = *model; newmodel->types = cxCalloc(a, model->columns, sizeof(UiModelType)); memcpy(newmodel->types, model->types, model->columns); newmodel->titles = cxCalloc(a, model->columns, sizeof(char*)); for (int i = 0; i < model->columns; i++) { newmodel->titles[i] = model->titles[i] ? cx_strdup_a(a, cx_str(model->titles[i])).ptr : NULL; } newmodel->columnsize = cxCalloc(a, model->columns, sizeof(int)); memcpy(newmodel->columnsize, model->columnsize, model->columns*sizeof(int)); return newmodel; } void ui_model_ref(UiModel *model) { model->ref++; } void ui_model_unref(UiModel *model) { if(--model->ref == 0) { ui_model_free(model); } } void ui_model_add_observer(UiModel *model, ui_model_update_func update, void *data) { UiModelChangeObserver *observer = ui_malloc(model->ctx, sizeof(UiModelChangeObserver)); observer->update = update; observer->userdata = data; observer->next = NULL; if(model->observer) { UiModelChangeObserver *last = model->observer; while(last->next) { last = last->next; } last->next = observer; } else { model->observer = observer; } } void ui_model_remove_observer(UiModel *model, void *data) { if(model->observer) { UiModelChangeObserver *obs = model->observer; UiModelChangeObserver *prev = NULL; while(obs) { if(obs->userdata == data) { // remove if(prev) { prev->next = obs->next; } else { model->observer = obs->next; } // free ui_free(model->ctx, obs); break; } prev = obs; obs = obs->next; } } } void ui_model_free(UiModel *mi) { UiContext *ctx = mi->ctx; const CxAllocator* a = ctx->allocator; for(int i=0;i<mi->columns;i++) { ui_free(ctx, mi->titles[i]); } UiModelChangeObserver *obs = mi->observer; while(obs) { UiModelChangeObserver *n = obs->next; cxFree(a, obs); obs = n; } cxFree(a, mi->types); cxFree(a, mi->titles); cxFree(a, mi->columnsize); cxFree(a, mi); } // types // public functions UiInteger* ui_int_new(UiContext *ctx, const char *name) { UiInteger *i = ui_malloc(ctx, sizeof(UiInteger)); memset(i, 0, sizeof(UiInteger)); if(name) { uic_reg_var(ctx, name, UI_VAR_INTEGER, i); } return i; } UiDouble* ui_double_new(UiContext *ctx, const char *name) { UiDouble *d = ui_malloc(ctx, sizeof(UiDouble)); memset(d, 0, sizeof(UiDouble)); if(name) { uic_reg_var(ctx, name, UI_VAR_DOUBLE, d); } return d; } static void string_destroy(UiString *s) { if(s->value.free && s->value.ptr) { s->value.free(s->value.ptr); } } UiString* ui_string_new(UiContext *ctx, const char *name) { UiString *s = ui_malloc(ctx, sizeof(UiString)); memset(s, 0, sizeof(UiString)); ui_set_destructor(s, (ui_destructor_func)string_destroy); if(name) { uic_reg_var(ctx, name, UI_VAR_STRING, s); } return s; } static void text_destroy(UiText *t) { if(t->destroy) { t->destroy(t); } } UiText* ui_text_new(UiContext *ctx, const char *name) { UiText *t = ui_malloc(ctx, sizeof(UiText)); memset(t, 0, sizeof(UiText)); ui_set_destructor(t, (ui_destructor_func)text_destroy); if(name) { uic_reg_var(ctx, name, UI_VAR_TEXT, t); } return t; } UiRange* ui_range_new(UiContext *ctx, const char *name) { UiRange *r = ui_malloc(ctx, sizeof(UiRange)); memset(r, 0, sizeof(UiRange)); if(name) { uic_reg_var(ctx, name, UI_VAR_RANGE, r); } return r; } static void generic_destroy(UiGeneric *g) { if(g->destroy) { g->destroy(g); } } UiGeneric* ui_generic_new(UiContext *ctx, const char *name) { UiGeneric *g = ui_malloc(ctx, sizeof(UiGeneric)); memset(g, 0, sizeof(UiGeneric)); ui_set_destructor(g, (ui_destructor_func)generic_destroy); if(name) { uic_reg_var(ctx, name, UI_VAR_GENERIC, g); } return g; } void ui_int_set(UiInteger* i, int64_t value) { if (i) { if (i->set) { ui_setop_enable(TRUE); i->set(i, value); ui_setop_enable(FALSE); } else { i->value = value; } } } int64_t ui_int_get(UiInteger* i) { if (i) { return i->get ? i->get(i) : i->value; } else { return 0; } } void ui_double_set(UiDouble* d, double value) { if (d) { if (d->set) { ui_setop_enable(TRUE); d->set(d, value); ui_setop_enable(FALSE); } else { d->value = value; } } } double ui_double_get(UiDouble* d) { if (d) { return d->get ? d->get(d) : d->value; } else { return 0; } } void ui_string_set(UiString* s, const char* value) { if (s) { if (s->set) { ui_setop_enable(TRUE); s->set(s, value); ui_setop_enable(FALSE); } else { if(s->value.free) { s->value.free(s->value.ptr); } if (value) { s->value.ptr = strdup(value); s->value.free = free; } else { s->value.ptr = NULL; s->value.free = NULL; } } } } char* ui_string_get(UiString* s) { if (s) { return s->get ? s->get(s) : s->value.ptr; } else { return NULL; } } void ui_text_set(UiText* s, const char* value) { if (s) { if (s->set) { ui_setop_enable(TRUE); s->set(s, value); ui_setop_enable(FALSE); } else { if(s->value.free) { s->value.free(s->value.ptr); } if (value) { s->value.ptr = strdup(value); s->value.free = free; } else { s->value.ptr = NULL; s->value.free = NULL; } } } } char* ui_text_get(UiText* s) { if (s) { return s->get ? s->get(s) : s->value.ptr; } else { return NULL; } } void ui_range_set(UiRange *r, double value) { if (r) { if (r->set) { r->set(r, value); } else { r->value = value; } } } void ui_range_set_range(UiRange *r, double min, double max) { if (r) { if (r->setrange) { r->setrange(r, min, max); } else { r->min = min; r->max = max; } } } void ui_range_set_extent(UiRange *r, double extent) { if (r) { if (r->setextent) { r->setextent(r, extent); } else { r->extent = extent; } } } double ui_range_get(UiRange *r) { if (r) { return r->get ? r->get(r) : r->value; } else { return 0; } } double ui_range_get_min(UiRange *r) { if (r) { return r->min; } else { return 0; } } double ui_range_get_max(UiRange *r) { if (r) { return r->max; } else { return 0; } } double ui_range_get_extent(UiRange *r) { if (r) { return r->extent; } else { return 0; } } void ui_generic_set_image(UiGeneric *g, void *img) { if(g->set) { g->set(g, img, UI_IMAGE_OBJECT_TYPE); } else { if(g->value) { ui_image_unref(g->value); } ui_image_ref(img); g->value = img; g->type = UI_IMAGE_OBJECT_TYPE; } } void* ui_generic_get_image(UiGeneric *g) { if(g->type) { if(!strcmp(g->type, UI_IMAGE_OBJECT_TYPE)) { return g->value; } else { return NULL; } } return g->value; } // private functions void uic_int_copy(UiInteger *from, UiInteger *to) { to->get = from->get; to->set = from->set; to->obj = from->obj; } void uic_double_copy(UiDouble *from, UiDouble *to) { to->get = from->get; to->set = from->set; to->obj = from->obj; } void uic_string_copy(UiString *from, UiString *to) { to->get = from->get; to->set = from->set; to->obj = from->obj; } void uic_text_copy(UiText *from, UiText *to) { to->get = from->get; to->set = from->set; to->getsubstr = from->getsubstr; to->insert = from->insert; to->setposition = from->setposition; to->position = from->position; to->selection = from->selection; to->length = from->length; to->remove = from->remove; to->restore = from->restore; to->save = from->save; to->destroy = from->destroy; to->obj = from->obj; // do not copy the undo manager, data1, data2 } void uic_range_copy(UiRange *from, UiRange *to) { to->get = from->get; to->set = from->set; to->setrange = from->setrange; to->setextent = from->setextent; to->obj = from->obj; } void uic_list_copy(UiList *from, UiList *to) { to->update = from->update; to->getselection = from->getselection; to->setselection = from->setselection; to->obj = from->obj; } void uic_generic_copy(UiGeneric *from, UiGeneric *to) { to->get = from->get; to->get_type = from->get_type; to->set = from->set; to->obj = from->obj; } void uic_int_save(UiInteger *i) { if(!i->obj) return; i->value = i->get(i); } void uic_double_save(UiDouble *d) { if(!d->obj) return; d->value = d->get(d); } void uic_string_save(UiString *s) { if(!s->obj) return; s->get(s); } void uic_text_save(UiText *t) { if(!t->obj) return; t->save(t); t->position(t); } void uic_range_save(UiRange *r) { if(!r->obj) return; r->get(r); } void uic_generic_save(UiGeneric *g) { if(!g->obj) return; g->get(g); } void uic_int_unbind(UiInteger *i) { i->get = NULL; i->set = NULL; i->obj = NULL; } void uic_double_unbind(UiDouble *d) { d->get = NULL; d->set = NULL; d->obj = NULL; } void uic_string_unbind(UiString *s) { s->get = NULL; s->set = NULL; s->obj = NULL; } void uic_text_unbind(UiText *t) { t->set = NULL; t->get = NULL; t->getsubstr = NULL; t->insert = NULL; t->setposition = NULL; t->position = NULL; t->selection = NULL; t->length = NULL; t->remove = NULL; t->obj = NULL; } void uic_range_unbind(UiRange *r) { r->get = NULL; r->set = NULL; r->setextent = NULL; r->setrange = NULL; r->obj = NULL; } void uic_list_unbind(UiList *l) { l->update = NULL; l->getselection = NULL; l->setselection = NULL; l->obj = NULL; } void uic_generic_unbind(UiGeneric *g) { g->get = NULL; g->get_type = NULL; g->set = NULL; g->obj = NULL; } UIEXPORT UiListSelection ui_list_getselection(UiList *list) { if (list->getselection) { return list->getselection(list); } return (UiListSelection){ 0, NULL }; } UIEXPORT void ui_list_setselection(UiList *list, int index) { if (list->setselection) { UiListSelection sel = { 0, NULL }; if(index >= 0) { sel.count = 1; sel.rows = &index; } list->setselection(list, sel); } } UIEXPORT void ui_listselection_free(UiListSelection selection) { free(selection.rows); } UIEXPORT UiStr ui_str(char *cstr) { return (UiStr) { cstr, NULL }; } UIEXPORT UiStr ui_str_free(char *str, void (*freefunc)(void *v)) { return (UiStr) { str, freefunc }; } UIEXPORT UiFileList ui_filelist_copy(UiFileList list) { char **newlist = calloc(sizeof(char*), list.nfiles); for (int i = 0; i < list.nfiles; i++) { newlist[i] = strdup(list.files[i]); } return (UiFileList) { newlist, list.nfiles }; } UIEXPORT void ui_filelist_free(UiFileList list) { for (int i = 0; i < list.nfiles; i++) { free(list.files[i]); } free(list.files); } typedef struct UiObserverDestructor { UiList *list; UiObserver *observer; } UiObserverDestructor; static void observer_destructor(UiObserverDestructor *destr) { UiObserver *remove_obs = destr->observer; UiObserver *obs = destr->list->observers; UiObserver *prev = NULL; while(obs) { if(obs == remove_obs) { if(prev) { prev->next = obs->next; } else { destr->list->observers = obs->next; } break; } prev = obs; obs = obs->next; } free(remove_obs); } void uic_list_register_observer_destructor(UiContext *ctx, UiList *list, UiObserver *observer) { CxMempool *mp = ctx->mp; UiObserverDestructor *destr = cxMalloc(mp->allocator, sizeof(UiObserverDestructor)); destr->list = list; destr->observer = observer; cxMempoolSetDestructor(destr, (cx_destructor_func)observer_destructor); } static int ui_set_op = 0; static int ui_onchange_events_enabled = TRUE; void ui_setop_enable(int set) { ui_set_op = set; } int ui_get_setop(void) { return ui_set_op; } void ui_onchange_events_enable(UiBool enable) { ui_onchange_events_enabled = enable; } UiBool ui_onchange_events_is_enabled(void) { return ui_onchange_events_enabled; } /* ---------------- List initializers and wrapper functions ---------------- */ void ui_global_list_initializer(ui_list_init_func func, void *userdata) { default_list_init = func; default_list_init_userdata = userdata; } void ui_global_list_destructor(ui_list_destroy_func func, void *userdata) { default_list_destroy = func; default_list_destroy_userdata = userdata; } void ui_list_class_set_first(UiList *list, void*(*first)(UiList *list)) { list->first = first; } void ui_list_class_set_next(UiList *list, void*(*next)(UiList *list)) { list->next = next; } void ui_list_class_set_get(UiList *list, void*(*get)(UiList *list, int i)) { list->get = get; } void ui_list_class_set_count(UiList *list, int(*count)(UiList *list)) { list->count = count; } void ui_list_class_set_data(UiList *list, void *data) { list->data = data; } void ui_list_class_set_iter(UiList *list, void *iter) { list->iter = iter; }