2 ** Copyright (c) 2024 rxi
4 ** This library is free software; you can redistribute it and/or modify it
5 ** under the terms of the MIT license. See `microui.c` for details.
11 #define MU_VERSION "2.02"
13 #define MU_COMMANDLIST_SIZE (256 * 1024)
14 #define MU_ROOTLIST_SIZE 32
15 #define MU_CONTAINERSTACK_SIZE 32
16 #define MU_CLIPSTACK_SIZE 32
17 #define MU_IDSTACK_SIZE 32
18 #define MU_LAYOUTSTACK_SIZE 16
19 #define MU_CONTAINERPOOL_SIZE 48
20 #define MU_TREENODEPOOL_SIZE 48
21 #define MU_MAX_WIDTHS 16
23 #define MU_REAL_FMT "%.3g"
24 #define MU_SLIDER_FMT "%.2f"
25 #define MU_MAX_FMT 127
27 #define mu_stack(T, n) struct { int idx; T items[n]; }
28 #define mu_min(a, b) ((a) < (b) ? (a) : (b))
29 #define mu_max(a, b) ((a) > (b) ? (a) : (b))
30 #define mu_clamp(x, a, b) mu_min(b, mu_max(a, x))
73 MU_RES_ACTIVE
= (1 << 0),
74 MU_RES_SUBMIT
= (1 << 1),
75 MU_RES_CHANGE
= (1 << 2)
79 MU_OPT_ALIGNCENTER
= (1 << 0),
80 MU_OPT_ALIGNRIGHT
= (1 << 1),
81 MU_OPT_NOINTERACT
= (1 << 2),
82 MU_OPT_NOFRAME
= (1 << 3),
83 MU_OPT_NORESIZE
= (1 << 4),
84 MU_OPT_NOSCROLL
= (1 << 5),
85 MU_OPT_NOCLOSE
= (1 << 6),
86 MU_OPT_NOTITLE
= (1 << 7),
87 MU_OPT_HOLDFOCUS
= (1 << 8),
88 MU_OPT_AUTOSIZE
= (1 << 9),
89 MU_OPT_POPUP
= (1 << 10),
90 MU_OPT_CLOSED
= (1 << 11),
91 MU_OPT_EXPANDED
= (1 << 12)
95 MU_MOUSE_LEFT
= (1 << 0),
96 MU_MOUSE_RIGHT
= (1 << 1),
97 MU_MOUSE_MIDDLE
= (1 << 2)
101 MU_KEY_SHIFT
= (1 << 0),
102 MU_KEY_CTRL
= (1 << 1),
103 MU_KEY_ALT
= (1 << 2),
104 MU_KEY_BACKSPACE
= (1 << 3),
105 MU_KEY_RETURN
= (1 << 4)
109 typedef struct mu_Context mu_Context
;
110 typedef unsigned mu_Id
;
111 typedef MU_REAL mu_Real
;
112 typedef void* mu_Font
;
114 typedef struct { int x
, y
; } mu_Vec2
;
115 typedef struct { int x
, y
, w
, h
; } mu_Rect
;
116 typedef struct { unsigned char r
, g
, b
, a
; } mu_Color
;
117 typedef struct { mu_Id id
; int last_update
; } mu_PoolItem
;
119 typedef struct { int type
, size
; } mu_BaseCommand
;
120 typedef struct { mu_BaseCommand base
; void *dst
; } mu_JumpCommand
;
121 typedef struct { mu_BaseCommand base
; mu_Rect rect
; } mu_ClipCommand
;
122 typedef struct { mu_BaseCommand base
; mu_Rect rect
; mu_Color color
; } mu_RectCommand
;
123 typedef struct { mu_BaseCommand base
; mu_Font font
; mu_Vec2 pos
; mu_Color color
; char str
[1]; } mu_TextCommand
;
124 typedef struct { mu_BaseCommand base
; mu_Rect rect
; int id
; mu_Color color
; } mu_IconCommand
;
142 int widths
[MU_MAX_WIDTHS
];
151 mu_Command
*head
, *tail
;
154 mu_Vec2 content_size
;
169 mu_Color colors
[MU_COLOR_MAX
];
174 int (*text_width
)(mu_Font font
, const char *str
, int len
);
175 int (*text_height
)(mu_Font font
);
176 void (*draw_frame
)(mu_Context
*ctx
, mu_Rect rect
, int colorid
);
187 mu_Container
*hover_root
;
188 mu_Container
*next_hover_root
;
189 mu_Container
*scroll_target
;
190 char number_edit_buf
[MU_MAX_FMT
];
193 mu_stack(char, MU_COMMANDLIST_SIZE
) command_list
;
194 mu_stack(mu_Container
*, MU_ROOTLIST_SIZE
) root_list
;
195 mu_stack(mu_Container
*, MU_CONTAINERSTACK_SIZE
) container_stack
;
196 mu_stack(mu_Rect
, MU_CLIPSTACK_SIZE
) clip_stack
;
197 mu_stack(mu_Id
, MU_IDSTACK_SIZE
) id_stack
;
198 mu_stack(mu_Layout
, MU_LAYOUTSTACK_SIZE
) layout_stack
;
199 /* retained state pools */
200 mu_PoolItem container_pool
[MU_CONTAINERPOOL_SIZE
];
201 mu_Container containers
[MU_CONTAINERPOOL_SIZE
];
202 mu_PoolItem treenode_pool
[MU_TREENODEPOOL_SIZE
];
205 mu_Vec2 last_mouse_pos
;
207 mu_Vec2 scroll_delta
;
216 mu_Vec2
mu_vec2(int x
, int y
);
217 mu_Rect
mu_rect(int x
, int y
, int w
, int h
);
218 mu_Color
mu_color(int r
, int g
, int b
, int a
);
220 void mu_init(mu_Context
*ctx
);
221 void mu_begin(mu_Context
*ctx
);
222 void mu_end(mu_Context
*ctx
);
223 void mu_set_focus(mu_Context
*ctx
, mu_Id id
);
224 mu_Id
mu_get_id(mu_Context
*ctx
, const void *data
, int size
);
225 void mu_push_id(mu_Context
*ctx
, const void *data
, int size
);
226 void mu_pop_id(mu_Context
*ctx
);
227 void mu_push_clip_rect(mu_Context
*ctx
, mu_Rect rect
);
228 void mu_pop_clip_rect(mu_Context
*ctx
);
229 mu_Rect
mu_get_clip_rect(mu_Context
*ctx
);
230 int mu_check_clip(mu_Context
*ctx
, mu_Rect r
);
231 mu_Container
* mu_get_current_container(mu_Context
*ctx
);
232 mu_Container
* mu_get_container(mu_Context
*ctx
, const char *name
);
233 void mu_bring_to_front(mu_Context
*ctx
, mu_Container
*cnt
);
235 int mu_pool_init(mu_Context
*ctx
, mu_PoolItem
*items
, int len
, mu_Id id
);
236 int mu_pool_get(mu_Context
*ctx
, mu_PoolItem
*items
, int len
, mu_Id id
);
237 void mu_pool_update(mu_Context
*ctx
, mu_PoolItem
*items
, int idx
);
239 void mu_input_mousemove(mu_Context
*ctx
, int x
, int y
);
240 void mu_input_mousedown(mu_Context
*ctx
, int x
, int y
, int btn
);
241 void mu_input_mouseup(mu_Context
*ctx
, int x
, int y
, int btn
);
242 void mu_input_scroll(mu_Context
*ctx
, int x
, int y
);
243 void mu_input_keydown(mu_Context
*ctx
, int key
);
244 void mu_input_keyup(mu_Context
*ctx
, int key
);
245 void mu_input_text(mu_Context
*ctx
, const char *text
);
247 mu_Command
* mu_push_command(mu_Context
*ctx
, int type
, int size
);
248 int mu_next_command(mu_Context
*ctx
, mu_Command
**cmd
);
249 void mu_set_clip(mu_Context
*ctx
, mu_Rect rect
);
250 void mu_draw_rect(mu_Context
*ctx
, mu_Rect rect
, mu_Color color
);
251 void mu_draw_box(mu_Context
*ctx
, mu_Rect rect
, mu_Color color
);
252 void mu_draw_text(mu_Context
*ctx
, mu_Font font
, const char *str
, int len
, mu_Vec2 pos
, mu_Color color
);
253 void mu_draw_icon(mu_Context
*ctx
, int id
, mu_Rect rect
, mu_Color color
);
255 void mu_layout_row(mu_Context
*ctx
, int items
, const int *widths
, int height
);
256 void mu_layout_width(mu_Context
*ctx
, int width
);
257 void mu_layout_height(mu_Context
*ctx
, int height
);
258 void mu_layout_begin_column(mu_Context
*ctx
);
259 void mu_layout_end_column(mu_Context
*ctx
);
260 void mu_layout_set_next(mu_Context
*ctx
, mu_Rect r
, int relative
);
261 mu_Rect
mu_layout_next(mu_Context
*ctx
);
263 void mu_draw_control_frame(mu_Context
*ctx
, mu_Id id
, mu_Rect rect
, int colorid
, int opt
);
264 void mu_draw_control_text(mu_Context
*ctx
, const char *str
, mu_Rect rect
, int colorid
, int opt
);
265 int mu_mouse_over(mu_Context
*ctx
, mu_Rect rect
);
266 void mu_update_control(mu_Context
*ctx
, mu_Id id
, mu_Rect rect
, int opt
);
268 #define mu_button(ctx, label) mu_button_ex(ctx, label, 0, MU_OPT_ALIGNCENTER)
269 #define mu_textbox(ctx, buf, bufsz) mu_textbox_ex(ctx, buf, bufsz, 0)
270 #define mu_slider(ctx, value, lo, hi) mu_slider_ex(ctx, value, lo, hi, 0, MU_SLIDER_FMT, MU_OPT_ALIGNCENTER)
271 #define mu_number(ctx, value, step) mu_number_ex(ctx, value, step, MU_SLIDER_FMT, MU_OPT_ALIGNCENTER)
272 #define mu_header(ctx, label) mu_header_ex(ctx, label, 0)
273 #define mu_begin_treenode(ctx, label) mu_begin_treenode_ex(ctx, label, 0)
274 #define mu_begin_window(ctx, title, rect) mu_begin_window_ex(ctx, title, rect, 0)
275 #define mu_begin_panel(ctx, name) mu_begin_panel_ex(ctx, name, 0)
277 void mu_text(mu_Context
*ctx
, const char *text
);
278 void mu_label(mu_Context
*ctx
, const char *text
);
279 int mu_button_ex(mu_Context
*ctx
, const char *label
, int icon
, int opt
);
280 int mu_checkbox(mu_Context
*ctx
, const char *label
, int *state
);
281 int mu_textbox_raw(mu_Context
*ctx
, char *buf
, int bufsz
, mu_Id id
, mu_Rect r
, int opt
);
282 int mu_textbox_ex(mu_Context
*ctx
, char *buf
, int bufsz
, int opt
);
283 int mu_slider_ex(mu_Context
*ctx
, mu_Real
*value
, mu_Real low
, mu_Real high
, mu_Real step
, const char *fmt
, int opt
);
284 int mu_number_ex(mu_Context
*ctx
, mu_Real
*value
, mu_Real step
, const char *fmt
, int opt
);
285 int mu_header_ex(mu_Context
*ctx
, const char *label
, int opt
);
286 int mu_begin_treenode_ex(mu_Context
*ctx
, const char *label
, int opt
);
287 void mu_end_treenode(mu_Context
*ctx
);
288 int mu_begin_window_ex(mu_Context
*ctx
, const char *title
, mu_Rect rect
, int opt
);
289 void mu_end_window(mu_Context
*ctx
);
290 void mu_open_popup(mu_Context
*ctx
, const char *name
);
291 int mu_begin_popup(mu_Context
*ctx
, const char *name
);
292 void mu_end_popup(mu_Context
*ctx
);
293 void mu_begin_panel_ex(mu_Context
*ctx
, const char *name
, int opt
);
294 void mu_end_panel(mu_Context
*ctx
);