UNIXworkcode

1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 * 4 * Copyright 2017 Olaf Wintermann. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <stdio.h> 30 #include <stdlib.h> 31 32 #include "object.h" 33 #include "context.h" 34 35 #include "../ui/container.h" 36 37 void ui_end(UiObject *obj) { 38 if(!obj->next) { 39 return; 40 } 41 42 UiObject *prev = NULL; 43 while(obj->next) { 44 prev = obj; 45 obj = obj->next; 46 } 47 48 if(prev) { 49 // TODO: free last obj 50 prev->next = NULL; 51 } 52 } 53 54 void ui_end_new(UiObject *obj) { 55 if(!obj->container_end) { 56 return; 57 } 58 UiContainerX *rm = obj->container_end; 59 uic_object_pop_container(obj); 60 ui_free(obj->ctx, rm); 61 } 62 63 void ui_object_ref(UiObject *obj) { 64 obj->ref++; 65 } 66 67 int ui_object_unref(UiObject *obj) { 68 // it is possible to have 0 references, in case 69 // a window was created but ui_show was never called 70 if(obj->ref == 0 || --obj->ref == 0) { 71 if(obj->destroy) { 72 obj->destroy(obj); 73 } 74 uic_object_destroy(obj); 75 return 0; 76 } 77 return 1; 78 } 79 80 void uic_object_destroy(UiObject *obj) { 81 if(obj->ctx->close_callback) { 82 UiEvent ev; 83 ev.window = obj->window; 84 ev.document = obj->ctx->document; 85 ev.obj = obj; 86 ev.eventdata = NULL; 87 ev.intval = 0; 88 obj->ctx->close_callback(&ev, obj->ctx->close_data); 89 } 90 cxMempoolDestroy(obj->ctx->mp); 91 } 92 93 UiObject* uic_object_new(UiObject *toplevel, UIWIDGET widget) { 94 return uic_ctx_object_new(toplevel->ctx, widget); 95 } 96 97 UiObject* uic_ctx_object_new(UiContext *ctx, UIWIDGET widget) { 98 UiObject *newobj = cxCalloc(ctx->allocator, 1, sizeof(UiObject)); 99 newobj->ctx = ctx; 100 newobj->widget = widget; 101 102 return newobj; 103 } 104 105 void uic_obj_add(UiObject *toplevel, UiObject *ctobj) { 106 UiObject *current = uic_current_obj(toplevel); 107 current->next = ctobj; 108 } 109 110 UiObject* uic_current_obj(UiObject *toplevel) { 111 if(!toplevel) { 112 return NULL; 113 } 114 UiObject *obj = toplevel; 115 while(obj->next) { 116 obj = obj->next; 117 } 118 return obj; 119 } 120 121 UiContainer* uic_get_current_container(UiObject *obj) { 122 return uic_current_obj(obj)->container; 123 } 124 125 void uic_object_push_container(UiObject *toplevel, UiContainerX *newcontainer) { 126 newcontainer->prev = toplevel->container_end; 127 if(toplevel->container_end) { 128 toplevel->container_end->next = newcontainer; 129 toplevel->container_end = newcontainer; 130 } else { 131 toplevel->container_begin = newcontainer; 132 toplevel->container_end = newcontainer; 133 } 134 } 135 136 void uic_object_pop_container(UiObject *toplevel) { 137 toplevel->container_end = toplevel->container_end->prev; 138 if(toplevel->container_end) { 139 toplevel->container_end->next = NULL; 140 } else { 141 toplevel->container_begin = NULL; 142 } 143 } 144