30 #include <stdlib.h> |
30 #include <stdlib.h> |
31 #include <string.h> |
31 #include <string.h> |
32 |
32 |
33 #include "document.h" |
33 #include "document.h" |
34 |
34 |
35 static UcxMap *documents; |
35 #include <cx/hash_map.h> |
|
36 #include <cx/mempool.h> |
|
37 |
|
38 |
|
39 static CxMap *documents; |
36 |
40 |
37 void uic_docmgr_init() { |
41 void uic_docmgr_init() { |
38 documents = ucx_map_new(32); |
42 if (!documents) { |
|
43 documents = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 32); |
|
44 } |
39 } |
45 } |
40 |
46 |
41 void ui_set_document(UiObject *obj, void *document) { |
47 void ui_set_document(UiObject *obj, void *document) { |
42 uic_context_detach_all(obj->ctx); |
48 uic_context_detach_all(obj->ctx); |
43 obj->ctx->attach_document(obj->ctx, document); |
49 obj->ctx->attach_document(obj->ctx, document); |
75 // TODO |
81 // TODO |
76 return NULL; |
82 return NULL; |
77 } |
83 } |
78 |
84 |
79 void* ui_document_new(size_t size) { |
85 void* ui_document_new(size_t size) { |
80 UcxMempool *mp = ucx_mempool_new(256); |
86 CxMempool *mp = cxMempoolCreate(256, NULL); |
81 UiContext *ctx = ucx_mempool_calloc(mp, 1, sizeof(UiContext)); |
87 const CxAllocator *a = mp->allocator; |
|
88 UiContext *ctx = cxCalloc(a, 1, sizeof(UiContext)); |
|
89 ctx->mp = mp; |
82 ctx->attach_document = uic_context_attach_document; |
90 ctx->attach_document = uic_context_attach_document; |
83 ctx->detach_document2 = uic_context_detach_document2; |
91 ctx->detach_document2 = uic_context_detach_document2; |
84 ctx->mempool = mp; |
92 ctx->allocator = a; |
85 ctx->vars = ucx_map_new_a(mp->allocator, 16); |
93 ctx->vars = cxHashMapCreate(a, CX_STORE_POINTERS, 16); |
86 |
94 |
87 void *document = ucx_mempool_calloc(mp, 1, size); |
95 void *document = cxCalloc(a, size, 1); |
88 ucx_map_put(documents, ucx_key(&document, sizeof(void*)), ctx); |
96 cxMapPut(documents, cx_hash_key(&document, sizeof(void*)), ctx); |
89 return document; |
97 return document; |
90 } |
98 } |
91 |
99 |
92 void ui_document_destroy(void *doc) { |
100 void ui_document_destroy(void *doc) { |
93 // TODO |
101 UiContext *ctx = ui_document_context(doc); |
|
102 if(ctx) { |
|
103 UiEvent ev; |
|
104 ev.window = NULL; |
|
105 ev.document = doc; |
|
106 ev.obj = NULL; |
|
107 ev.eventdata = NULL; |
|
108 ev.intval = 0; |
|
109 |
|
110 if(ctx->close_callback) { |
|
111 ctx->close_callback(&ev, ctx->close_data); |
|
112 } |
|
113 cxMempoolDestroy(ctx->mp); |
|
114 } |
94 } |
115 } |
95 |
116 |
96 UiContext* ui_document_context(void *doc) { |
117 UiContext* ui_document_context(void *doc) { |
97 if(doc) { |
118 if(doc) { |
98 return ucx_map_get(documents, ucx_key(&doc, sizeof(void*))); |
119 return cxMapGet(documents, cx_hash_key(&doc, sizeof(void*))); |
99 } else { |
120 } else { |
100 return NULL; |
121 return NULL; |
101 } |
122 } |
102 } |
123 } |