Added support for cross-compiling to mingw (#5)
[full-beans.git] / microui.h
blob6e18639d5929f43a1bf355507e04b5e573a5a9d0
1 /*
2 ** Copyright (c) 2024 rxi
3 **
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.
6 */
8 #ifndef MICROUI_H
9 #define MICROUI_H
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
22 #define MU_REAL float
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))
32 enum {
33 MU_CLIP_PART = 1,
34 MU_CLIP_ALL
37 enum {
38 MU_COMMAND_JUMP = 1,
39 MU_COMMAND_CLIP,
40 MU_COMMAND_RECT,
41 MU_COMMAND_TEXT,
42 MU_COMMAND_ICON,
43 MU_COMMAND_MAX
46 enum {
47 MU_COLOR_TEXT,
48 MU_COLOR_BORDER,
49 MU_COLOR_WINDOWBG,
50 MU_COLOR_TITLEBG,
51 MU_COLOR_TITLETEXT,
52 MU_COLOR_PANELBG,
53 MU_COLOR_BUTTON,
54 MU_COLOR_BUTTONHOVER,
55 MU_COLOR_BUTTONFOCUS,
56 MU_COLOR_BASE,
57 MU_COLOR_BASEHOVER,
58 MU_COLOR_BASEFOCUS,
59 MU_COLOR_SCROLLBASE,
60 MU_COLOR_SCROLLTHUMB,
61 MU_COLOR_MAX
64 enum {
65 MU_ICON_CLOSE = 1,
66 MU_ICON_CHECK,
67 MU_ICON_COLLAPSED,
68 MU_ICON_EXPANDED,
69 MU_ICON_MAX
72 enum {
73 MU_RES_ACTIVE = (1 << 0),
74 MU_RES_SUBMIT = (1 << 1),
75 MU_RES_CHANGE = (1 << 2)
78 enum {
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)
94 enum {
95 MU_MOUSE_LEFT = (1 << 0),
96 MU_MOUSE_RIGHT = (1 << 1),
97 MU_MOUSE_MIDDLE = (1 << 2)
100 enum {
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;
126 typedef union {
127 int type;
128 mu_BaseCommand base;
129 mu_JumpCommand jump;
130 mu_ClipCommand clip;
131 mu_RectCommand rect;
132 mu_TextCommand text;
133 mu_IconCommand icon;
134 } mu_Command;
136 typedef struct {
137 mu_Rect body;
138 mu_Rect next;
139 mu_Vec2 position;
140 mu_Vec2 size;
141 mu_Vec2 max;
142 int widths[MU_MAX_WIDTHS];
143 int items;
144 int item_index;
145 int next_row;
146 int next_type;
147 int indent;
148 } mu_Layout;
150 typedef struct {
151 mu_Command *head, *tail;
152 mu_Rect rect;
153 mu_Rect body;
154 mu_Vec2 content_size;
155 mu_Vec2 scroll;
156 int zindex;
157 int open;
158 } mu_Container;
160 typedef struct {
161 mu_Font font;
162 mu_Vec2 size;
163 int padding;
164 int spacing;
165 int indent;
166 int title_height;
167 int scrollbar_size;
168 int thumb_size;
169 mu_Color colors[MU_COLOR_MAX];
170 } mu_Style;
172 struct mu_Context {
173 /* callbacks */
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);
177 /* core state */
178 mu_Style _style;
179 mu_Style *style;
180 mu_Id hover;
181 mu_Id focus;
182 mu_Id last_id;
183 mu_Rect last_rect;
184 int last_zindex;
185 int updated_focus;
186 int frame;
187 mu_Container *hover_root;
188 mu_Container *next_hover_root;
189 mu_Container *scroll_target;
190 char number_edit_buf[MU_MAX_FMT];
191 mu_Id number_edit;
192 /* stacks */
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];
203 /* input state */
204 mu_Vec2 mouse_pos;
205 mu_Vec2 last_mouse_pos;
206 mu_Vec2 mouse_delta;
207 mu_Vec2 scroll_delta;
208 int mouse_down;
209 int mouse_pressed;
210 int key_down;
211 int key_pressed;
212 char input_text[32];
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);
296 #endif