Nuklear
Minimal-state, immediate-mode graphical user interface toolkit written in ANSI C.
Loading...
Searching...
No Matches
nuklear_context.c
1#include "nuklear.h"
2#include "nuklear_internal.h"
3
4/* ==============================================================
5 *
6 * CONTEXT
7 *
8 * ===============================================================*/
9NK_INTERN void
10nk_setup(struct nk_context *ctx, const struct nk_user_font *font)
11{
12 NK_ASSERT(ctx);
13 if (!ctx) return;
14 nk_zero_struct(*ctx);
15 nk_style_default(ctx);
16 ctx->seq = 1;
17 if (font) ctx->style.font = font;
18#ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
19 nk_draw_list_init(&ctx->draw_list);
20#endif
21}
22#ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
23NK_API nk_bool
24nk_init_default(struct nk_context *ctx, const struct nk_user_font *font)
25{
26 struct nk_allocator alloc;
27 alloc.userdata.ptr = 0;
28 alloc.alloc = nk_malloc;
29 alloc.free = nk_mfree;
30 return nk_init(ctx, &alloc, font);
31}
32#endif
33NK_API nk_bool
34nk_init_fixed(struct nk_context *ctx, void *memory, nk_size size,
35 const struct nk_user_font *font)
36{
37 NK_ASSERT(memory);
38 if (!memory) return 0;
39 nk_setup(ctx, font);
40 nk_buffer_init_fixed(&ctx->memory, memory, size);
41 ctx->use_pool = nk_false;
42 return 1;
43}
44NK_API nk_bool
45nk_init_custom(struct nk_context *ctx, struct nk_buffer *cmds,
46 struct nk_buffer *pool, const struct nk_user_font *font)
47{
48 NK_ASSERT(cmds);
49 NK_ASSERT(pool);
50 if (!cmds || !pool) return 0;
51
52 nk_setup(ctx, font);
53 ctx->memory = *cmds;
54 if (pool->type == NK_BUFFER_FIXED) {
55 /* take memory from buffer and alloc fixed pool */
56 nk_pool_init_fixed(&ctx->pool, pool->memory.ptr, pool->memory.size);
57 } else {
58 /* create dynamic pool from buffer allocator */
59 struct nk_allocator *alloc = &pool->pool;
60 nk_pool_init(&ctx->pool, alloc, NK_POOL_DEFAULT_CAPACITY);
61 }
62 ctx->use_pool = nk_true;
63 return 1;
64}
65NK_API nk_bool
66nk_init(struct nk_context *ctx, const struct nk_allocator *alloc,
67 const struct nk_user_font *font)
68{
69 NK_ASSERT(alloc);
70 if (!alloc) return 0;
71 nk_setup(ctx, font);
72 nk_buffer_init(&ctx->memory, alloc, NK_DEFAULT_COMMAND_BUFFER_SIZE);
73 nk_pool_init(&ctx->pool, alloc, NK_POOL_DEFAULT_CAPACITY);
74 ctx->use_pool = nk_true;
75 return 1;
76}
77#ifdef NK_INCLUDE_COMMAND_USERDATA
78NK_API void
79nk_set_user_data(struct nk_context *ctx, nk_handle handle)
80{
81 if (!ctx) return;
82 ctx->userdata = handle;
83 if (ctx->current)
84 ctx->current->buffer.userdata = handle;
85}
86#endif
87NK_API void
88nk_set_display_size(struct nk_context *ctx, float width, float height)
89{
90 NK_ASSERT(ctx);
91 if (!ctx) return;
92 if (width < 0) width = 0;
93 if (height < 0) height = 0;
94 ctx->display_bounds.x = 0;
95 ctx->display_bounds.y = 0;
96 ctx->display_bounds.w = width;
97 ctx->display_bounds.h = height;
98}
99NK_API void
100nk_set_display_bounds(struct nk_context *ctx, struct nk_rect bounds)
101{
102 NK_ASSERT(ctx);
103 if (!ctx) return;
104 if (bounds.w < 0) bounds.w = 0;
105 if (bounds.h < 0) bounds.h = 0;
106 ctx->display_bounds = bounds;
107}
108NK_API void
109nk_free(struct nk_context *ctx)
110{
111 NK_ASSERT(ctx);
112 if (!ctx) return;
113 nk_buffer_free(&ctx->memory);
114 if (ctx->use_pool)
115 nk_pool_free(&ctx->pool);
116
117 nk_zero(&ctx->input, sizeof(ctx->input));
118 nk_zero(&ctx->style, sizeof(ctx->style));
119 nk_zero(&ctx->memory, sizeof(ctx->memory));
120
121 ctx->seq = 0;
122 ctx->build = 0;
123 ctx->begin = 0;
124 ctx->end = 0;
125 ctx->active = 0;
126 ctx->current = 0;
127 ctx->freelist = 0;
128 ctx->count = 0;
129}
130NK_API void
131nk_clear(struct nk_context *ctx)
132{
133 struct nk_window *iter;
134 struct nk_window *next;
135 NK_ASSERT(ctx);
136
137 if (!ctx) return;
138 if (ctx->use_pool)
139 nk_buffer_clear(&ctx->memory);
140 else nk_buffer_reset(&ctx->memory, NK_BUFFER_FRONT);
141
142 ctx->build = 0;
143 ctx->memory.calls = 0;
144 ctx->last_widget_state = 0;
145 ctx->style.cursor_active = ctx->style.cursors[NK_CURSOR_ARROW];
146 NK_MEMSET(&ctx->overlay, 0, sizeof(ctx->overlay));
147
148 /* garbage collector */
149 iter = ctx->begin;
150 while (iter) {
151 /* make sure valid minimized windows do not get removed */
152 if ((iter->flags & NK_WINDOW_MINIMIZED) &&
153 !(iter->flags & NK_WINDOW_CLOSED) &&
154 iter->seq == ctx->seq) {
155 iter = iter->next;
156 continue;
157 }
158 /* remove hotness from hidden or closed windows*/
159 if (((iter->flags & NK_WINDOW_HIDDEN) ||
160 (iter->flags & NK_WINDOW_CLOSED)) &&
161 iter == ctx->active) {
162 ctx->active = iter->prev;
163 ctx->end = iter->prev;
164 if (!ctx->end)
165 ctx->begin = 0;
166 if (ctx->active)
167 ctx->active->flags &= ~(unsigned)NK_WINDOW_ROM;
168 }
169 /* free unused popup windows */
170 if (iter->popup.win && iter->popup.win->seq != ctx->seq) {
171 nk_free_window(ctx, iter->popup.win);
172 iter->popup.win = 0;
173 }
174 /* remove unused window state tables */
175 {struct nk_table *n, *it = iter->tables;
176 while (it) {
177 n = it->next;
178 if (it->seq != ctx->seq) {
179 nk_remove_table(iter, it);
180 nk_zero(it, sizeof(union nk_page_data));
181 nk_free_table(ctx, it);
182 if (it == iter->tables)
183 iter->tables = n;
184 } it = n;
185 }}
186 /* window itself is not used anymore so free */
187 if (iter->seq != ctx->seq || iter->flags & NK_WINDOW_CLOSED) {
188 next = iter->next;
189 nk_remove_window(ctx, iter);
190 nk_free_window(ctx, iter);
191 iter = next;
192 } else iter = iter->next;
193 }
194 ctx->seq++;
195}
196NK_LIB void
197nk_start_buffer(struct nk_context *ctx, struct nk_command_buffer *buffer)
198{
199 NK_ASSERT(ctx);
200 NK_ASSERT(buffer);
201 if (!ctx || !buffer) return;
202 buffer->begin = ctx->memory.allocated;
203 buffer->end = buffer->begin;
204 buffer->last = buffer->begin;
205 buffer->clip = nk_null_rect;
206}
207NK_LIB void
208nk_start(struct nk_context *ctx, struct nk_window *win)
209{
210 NK_ASSERT(ctx);
211 NK_ASSERT(win);
212 nk_start_buffer(ctx, &win->buffer);
213}
214NK_LIB void
215nk_start_popup(struct nk_context *ctx, struct nk_window *win)
216{
217 struct nk_popup_buffer *buf;
218 NK_ASSERT(ctx);
219 NK_ASSERT(win);
220 if (!ctx || !win) return;
221
222 /* save buffer fill state for popup */
223 buf = &win->popup.buf;
224 buf->begin = win->buffer.end;
225 buf->end = win->buffer.end;
226 buf->parent = win->buffer.last;
227 buf->last = buf->begin;
228 buf->active = nk_true;
229}
230NK_LIB void
231nk_finish_popup(struct nk_context *ctx, struct nk_window *win)
232{
233 struct nk_popup_buffer *buf;
234 NK_ASSERT(ctx);
235 NK_ASSERT(win);
236 if (!ctx || !win) return;
237
238 buf = &win->popup.buf;
239 buf->last = win->buffer.last;
240 buf->end = win->buffer.end;
241}
242NK_LIB void
243nk_finish_buffer(struct nk_context *ctx, struct nk_command_buffer *buffer)
244{
245 NK_ASSERT(ctx);
246 NK_ASSERT(buffer);
247 if (!ctx || !buffer) return;
248 buffer->end = ctx->memory.allocated;
249}
250NK_LIB void
251nk_finish(struct nk_context *ctx, struct nk_window *win)
252{
253 struct nk_popup_buffer *buf;
254 struct nk_command *parent_last;
255 void *memory;
256
257 NK_ASSERT(ctx);
258 NK_ASSERT(win);
259 if (!ctx || !win) return;
260 nk_finish_buffer(ctx, &win->buffer);
261 if (!win->popup.buf.active) return;
262
263 buf = &win->popup.buf;
264 memory = ctx->memory.memory.ptr;
265 parent_last = nk_ptr_add(struct nk_command, memory, buf->parent);
266 parent_last->next = buf->end;
267}
268NK_LIB void
269nk_build(struct nk_context *ctx)
270{
271 struct nk_window *it = 0;
272 struct nk_command *cmd = 0;
273 nk_byte *buffer = 0;
274
275 /* draw cursor overlay */
276 if (!ctx->style.cursor_active)
277 ctx->style.cursor_active = ctx->style.cursors[NK_CURSOR_ARROW];
278 if (ctx->style.cursor_active && !ctx->input.mouse.grabbed && ctx->style.cursor_visible) {
279 struct nk_rect mouse_bounds;
280 const struct nk_cursor *cursor = ctx->style.cursor_active;
281 nk_command_buffer_init(&ctx->overlay, &ctx->memory, NK_CLIPPING_OFF);
282 nk_start_buffer(ctx, &ctx->overlay);
283
284 mouse_bounds.x = ctx->input.mouse.pos.x - cursor->offset.x;
285 mouse_bounds.y = ctx->input.mouse.pos.y - cursor->offset.y;
286 mouse_bounds.w = cursor->size.x;
287 mouse_bounds.h = cursor->size.y;
288
289 nk_draw_image(&ctx->overlay, mouse_bounds, &cursor->img, nk_white);
290 nk_finish_buffer(ctx, &ctx->overlay);
291 }
292 /* build one big draw command list out of all window buffers */
293 it = ctx->begin;
294 buffer = (nk_byte*)ctx->memory.memory.ptr;
295 while (it != 0) {
296 struct nk_window *next = it->next;
297 if (it->buffer.last == it->buffer.begin || (it->flags & NK_WINDOW_HIDDEN)||
298 it->seq != ctx->seq)
299 goto cont;
300
301 cmd = nk_ptr_add(struct nk_command, buffer, it->buffer.last);
302 while (next && ((next->buffer.last == next->buffer.begin) ||
303 (next->flags & NK_WINDOW_HIDDEN) || next->seq != ctx->seq))
304 next = next->next; /* skip empty command buffers */
305
306 if (next) cmd->next = next->buffer.begin;
307 cont: it = next;
308 }
309 /* append all popup draw commands into lists */
310 it = ctx->begin;
311 while (it != 0) {
312 struct nk_window *next = it->next;
313 struct nk_popup_buffer *buf;
314 if (!it->popup.buf.active)
315 goto skip;
316
317 buf = &it->popup.buf;
318 cmd->next = buf->begin;
319 cmd = nk_ptr_add(struct nk_command, buffer, buf->last);
320 buf->active = nk_false;
321 skip: it = next;
322 }
323 if (cmd) {
324 /* append overlay commands */
325 if (ctx->overlay.end != ctx->overlay.begin)
326 cmd->next = ctx->overlay.begin;
327 else cmd->next = ctx->memory.allocated;
328 }
329}
330NK_API const struct nk_command*
331nk__begin(struct nk_context *ctx)
332{
333 struct nk_window *iter;
334 nk_byte *buffer;
335 NK_ASSERT(ctx);
336 if (!ctx) return 0;
337 if (!ctx->count) return 0;
338
339 buffer = (nk_byte*)ctx->memory.memory.ptr;
340 if (!ctx->build) {
341 nk_build(ctx);
342 ctx->build = nk_true;
343 }
344 iter = ctx->begin;
345 while (iter && ((iter->buffer.begin == iter->buffer.end) ||
346 (iter->flags & NK_WINDOW_HIDDEN) || iter->seq != ctx->seq))
347 iter = iter->next;
348 if (!iter) return 0;
349 return nk_ptr_add_const(struct nk_command, buffer, iter->buffer.begin);
350}
351
352NK_API const struct nk_command*
353nk__next(struct nk_context *ctx, const struct nk_command *cmd)
354{
355 nk_byte *buffer;
356 const struct nk_command *next;
357 NK_ASSERT(ctx);
358 if (!ctx || !cmd || !ctx->count) return 0;
359 if (cmd->next >= ctx->memory.allocated) return 0;
360 buffer = (nk_byte*)ctx->memory.memory.ptr;
361 next = nk_ptr_add_const(struct nk_command, buffer, cmd->next);
362 return next;
363}
364
365
struct nk_allocator pool
!< buffer marker to free a buffer to a certain offset
Definition nuklear.h:4301
struct nk_memory memory
!< memory management type
Definition nuklear.h:4303
enum nk_allocation_type type
!< allocator callback for dynamic buffers
Definition nuklear.h:4302
nk_size allocated
!< growing factor for dynamic memory management
Definition nuklear.h:4305
nk_size calls
!< totally consumed memory given that enough memory is present
Definition nuklear.h:4307
command base and header of every command inside the buffer
Definition nuklear.h:4577
int build
windows
Definition nuklear.h:5875
struct nk_rect display_bounds
surface nuklear is drawn into, in widget/mouse space.
Definition nuklear.h:5855
struct nk_command_buffer overlay
draw buffer used for overlay drawing operation like cursor
Definition nuklear.h:5872