--- a/ui/common/object.c Thu Nov 14 23:22:35 2024 +0100 +++ b/ui/common/object.c Fri Nov 15 21:50:20 2024 +0100 @@ -49,6 +49,34 @@ } } +void ui_object_ref(UiObject *obj) { + obj->ref++; +} + +void ui_object_unref(UiObject *obj) { + // it is possible to have 0 references, in case + // a window was created but ui_show was never called + if(obj->ref == 0 || --obj->ref == 0) { + if(obj->destroy) { + obj->destroy(obj); + } else { + uic_object_destroy(obj); + } + } +} + +void uic_object_destroy(UiObject *obj) { + if(obj->ctx->close_callback) { + UiEvent ev; + ev.window = obj->window; + ev.document = obj->ctx->document; + ev.obj = obj; + ev.eventdata = NULL; + ev.intval = 0; + obj->ctx->close_callback(&ev, obj->ctx->close_data); + } + cxMempoolDestroy(obj->ctx->mp); +} UiObject* uic_object_new(UiObject *toplevel, UIWIDGET widget) { return uic_ctx_object_new(toplevel->ctx, widget);