1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
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
69
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