44#define ALLEGRO_UNSTABLE 1
57#define LAYOUT_FILE "ex_gui_detach_layout.json"
88 while ((opt = getopt(argc, argv,
"haq:V:")) != -1) {
97 if (!strcmp(
"LOG_NULL", optarg))
99 else if (!strcmp(
"LOG_NOTICE", optarg))
101 else if (!strcmp(
"LOG_INFO", optarg))
103 else if (!strcmp(
"LOG_ERR", optarg))
105 else if (!strcmp(
"LOG_DEBUG", optarg))
115 "Usage: %s [-V LOG_LEVEL] [-a] [-q SECONDS]\n"
116 " -a detach every panel at startup\n"
117 " -q SECONDS quit after SECONDS (0 = until ESC)\n",
145 snprintf(one,
sizeof(one),
"%s%s:%s", detail[0] ?
" " :
"",
panels[i].
name,
147 strncat(detail, one,
sizeof(detail) - strlen(detail) - 1);
197 n_log(
LOG_NOTICE,
"%s closed, use 'Show all panels' to bring it back", p ? p->
name :
"panel");
237static int build_panel(
const char* title,
float x,
float y,
float w,
float h,
int detach_flags,
const char* name) {
239 if (win < 0)
return -1;
255int main(
int argc,
char** argv) {
263 if (!al_init_primitives_addon() || !al_init_font_addon()) {
267 al_install_keyboard();
270 ALLEGRO_EVENT_QUEUE* queue = al_create_event_queue();
276 al_set_new_display_flags(ALLEGRO_WINDOWED | ALLEGRO_RESIZABLE);
279 n_log(
LOG_ERR,
"unable to create a display, is a graphical session available?");
280 al_destroy_event_queue(queue);
283 al_set_window_title(
display,
"Nilorea GUI, detachable windows");
285 ALLEGRO_TIMER*
fps_timer = al_create_timer(1.0 / 60.0);
286 al_register_event_source(queue, al_get_display_event_source(
display));
287 al_register_event_source(queue, al_get_timer_event_source(
fps_timer));
288 al_register_event_source(queue, al_get_keyboard_event_source());
289 al_register_event_source(queue, al_get_mouse_event_source());
291 ALLEGRO_FONT* font = al_create_builtin_font();
296 al_destroy_event_queue(queue);
303 al_destroy_font(font);
306 al_destroy_event_queue(queue);
335 "Close a native window: 'Show all panels' brings it back.",
342 int win =
build_panel(
"Mixer (OS chrome)", 20.0f, 250.0f, 300.0f, 220.0f,
357 int win =
build_panel(
"Notes (N_GUI chrome)", 340.0f, 250.0f, 300.0f, 220.0f,
361 "Copy and paste still reach\n"
362 "the right display.\n\n"
363 "Drag me by my own title bar.");
370 int win =
build_panel(
"Files (scaling)", 660.0f, 250.0f, 280.0f, 220.0f,
389 n_log(
LOG_NOTICE,
"ex_gui_detach: click a panel's button to give it its own OS window. ESC quits.");
393 double started_at = al_get_time();
394 al_flush_event_queue(queue);
398 al_wait_for_event(queue, &ev);
403 if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE && ev.display.source ==
display) {
406 if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
407 if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
409 else if (ev.keyboard.keycode == ALLEGRO_KEY_F5)
411 else if (ev.keyboard.keycode == ALLEGRO_KEY_F9)
415 if (ev.type == ALLEGRO_EVENT_DISPLAY_RESIZE && ev.display.source ==
display) {
416 al_acknowledge_resize(
display);
418 (
float)al_get_display_height(
display));
426 if (ev.type == ALLEGRO_EVENT_TIMER) {
435 if (do_draw && al_is_event_queue_empty(queue)) {
437 al_set_target_backbuffer(
display);
438 al_clear_to_color(al_map_rgb(28, 30, 36));
441 ALLEGRO_TRANSFORM tf;
442 al_identity_transform(&tf);
447 al_use_transform(&tf);
448 al_draw_text(font, al_map_rgb(170, 175, 185), 10.0f, (
float)
HEIGHT - 18.0f, 0,
449 "Detach a panel to give it a real OS window | F5 save layout | F9 load layout | ESC quit");
450 al_identity_transform(&tf);
451 al_use_transform(&tf);
466 al_destroy_font(font);
468 al_destroy_event_queue(queue);
void process_args(int argc, char **argv)
ALLEGRO_TIMER * fps_timer
ALLEGRO_DISPLAY * display
void on_slider_change(int widget_id, double value, void *user_data)
static void on_panel_close(int window_id, void *user_data)
A native window's close button (the window manager's, or N_GUI's own when the panel keeps its chrome)...
static double quit_after
seconds after which the demo quits on its own (0 = run until ESC)
static void on_save_layout(int widget_id, void *user_data)
static void refresh_buttons(void)
keep each panel's button label in sync with what it will do next
static int auto_detach
1 to detach every panel at startup (-a)
static void on_show_all(int widget_id, void *user_data)
static void on_toggle_detach(int widget_id, void *user_data)
the per-panel button: toggle this panel between pop-up and native window
static void on_attach_all(int widget_id, void *user_data)
static DEMO_PANEL * panel_of(int window_id)
find the panel a window id belongs to, or NULL
const char * name
short name used in the status line
static int build_panel(const char *title, float x, float y, float w, float h, int detach_flags, const char *name)
build one demo panel: a few ordinary widgets plus its detach button
static DEMO_PANEL panels[3]
static void on_detach_all(int widget_id, void *user_data)
int detach_flags
options this panel detaches with (N_GUI_DETACH_*)
int btn
id of the panel's own detach / attach button
static void on_load_layout(int widget_id, void *user_data)
static int notes_txt
text area of the "Notes" panel, used by the copy/paste hint
one demo panel: a window plus the button that detaches / attaches it
float gui_offset_x
horizontal letterbox offset for virtual canvas
float gui_offset_y
vertical letterbox offset for virtual canvas
float virtual_w
virtual canvas width (0 = disabled / identity transform)
float gui_scale
computed uniform scale factor for virtual canvas
void n_gui_set_display_size(N_GUI_CTX *ctx, float w, float h)
Set the display (viewport) size for global scrollbar computation.
#define N_GUI_ALIGN_LEFT
left aligned text
int n_gui_add_slider(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, double min_val, double max_val, double initial, int mode, void(*on_change)(int, double, void *), void *user_data)
Add a slider widget.
void n_gui_textarea_set_text(N_GUI_CTX *ctx, int widget_id, const char *text)
set the text content of a textarea widget
int n_gui_save_layout_json(N_GUI_CTX *ctx, const char *filepath)
save the geometry + persistent state of every window to a JSON file
int n_gui_window_detach(N_GUI_CTX *ctx, int window_id, int detach_flags)
Promote a pop-up window to a native OS window.
#define N_GUI_DETACH_SCALE_CONTENT
scale the window's widgets when the native window is resized by the user.
#define N_GUI_SLIDER_PERCENT
slider uses 0-100 percentage
void n_gui_set_display(N_GUI_CTX *ctx, ALLEGRO_DISPLAY *display)
Set the display pointer for clipboard operations (copy/paste).
void n_gui_set_widget_tooltip(N_GUI_CTX *ctx, int widget_id, const char *text)
Set (or clear) the tooltip text of any widget.
void n_gui_label_set_text(N_GUI_CTX *ctx, int widget_id, const char *text)
set the text of a label widget
int n_gui_add_listbox(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, int selection_mode, void(*on_select)(int, int, int, void *), void *user_data)
Add a listbox widget.
int n_gui_process_event(N_GUI_CTX *ctx, ALLEGRO_EVENT event)
Process an allegro event through the GUI system.
int n_gui_add_label(N_GUI_CTX *ctx, int window_id, const char *text, float x, float y, float w, float h, int align)
Add a static text label.
void n_gui_close_window(N_GUI_CTX *ctx, int window_id)
Close (hide) a window.
int n_gui_listbox_add_item(N_GUI_CTX *ctx, int widget_id, const char *text)
add an item to a listbox widget
void n_gui_set_virtual_size(N_GUI_CTX *ctx, float w, float h)
Set the virtual canvas size for resolution-independent scaling.
void n_gui_button_set_label(N_GUI_CTX *ctx, int widget_id, const char *label)
Replace the label text drawn on a button.
#define N_GUI_SELECT_SINGLE
single item selection
#define N_GUI_DETACH_RESIZABLE
the native window can be resized by the user through the OS window manager
int n_gui_window_is_detached(N_GUI_CTX *ctx, int window_id)
Check whether a window currently lives in a native OS window.
void n_gui_slider_set_step(N_GUI_CTX *ctx, int widget_id, double step)
set slider step increment.
int n_gui_add_progressbar(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h)
add a horizontal progress bar (display only); returns the widget id or -1
void n_gui_draw(N_GUI_CTX *ctx)
Draw all visible windows and their widgets.
int n_gui_add_window(N_GUI_CTX *ctx, const char *title, float x, float y, float w, float h)
Add a new pseudo-window to the context.
int n_gui_window_attach(N_GUI_CTX *ctx, int window_id)
Return a detached window to being an in-display pop-up.
void n_gui_draw_detached(N_GUI_CTX *ctx)
Render and flip every detached window, and release the ones whose native window was closed.
N_GUI_CTX * n_gui_new_ctx(ALLEGRO_FONT *default_font)
Create a new GUI context.
void n_gui_window_set_close_callback(N_GUI_CTX *ctx, int window_id, void(*on_close)(int, void *), void *user_data)
Set the close button callback for a window.
#define N_GUI_DETACH_OWN_CHROME
keep N_GUI's own window chrome instead of the window manager's: the native window is created frameles...
#define N_GUI_WIN_BTN_MINIMIZE
enable minimize button on the title bar
void n_gui_destroy_ctx(N_GUI_CTX **ctx)
Destroy a GUI context and all its windows/widgets.
void n_gui_open_window(N_GUI_CTX *ctx, int window_id)
Open (show) a window.
int n_gui_count_detached(N_GUI_CTX *ctx)
Count the windows currently detached into native OS windows.
int n_gui_load_layout_json(N_GUI_CTX *ctx, const char *filepath)
load a JSON window layout file and apply it to matching windows
#define N_GUI_SHAPE_ROUNDED
rounded rectangle shape
void n_gui_window_set_flags(N_GUI_CTX *ctx, int window_id, int flags)
Set feature flags on a window.
#define N_GUI_WIN_BTN_ALL
enable all three title bar buttons (minimize + maximize + close)
int n_gui_add_textarea(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, int multiline, size_t char_limit, void(*on_change)(int, const char *, void *), void *user_data)
Add a text area widget.
int n_gui_add_checkbox(N_GUI_CTX *ctx, int window_id, const char *label, float x, float y, float w, float h, int initial_checked, void(*on_toggle)(int, int, void *), void *user_data)
Add a checkbox widget.
int n_gui_add_button(N_GUI_CTX *ctx, int window_id, const char *label, float x, float y, float w, float h, int shape, void(*on_click)(int, void *), void *user_data)
Add a button widget to a window.
void n_gui_set_event_queue(N_GUI_CTX *ctx, ALLEGRO_EVENT_QUEUE *queue)
Set the event queue used to register native window event sources.
#define N_GUI_WIN_RESIZABLE
enable user-resizable window with a drag handle at bottom-right
The top-level GUI context that holds all windows.
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_DEBUG
debug-level messages
#define LOG_ERR
error conditions
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
#define LOG_NOTICE
normal but significant condition
#define LOG_NULL
no log output
#define LOG_INFO
informational
Common headers and low-level functions & define.
GUI system: buttons, sliders, text areas, checkboxes, scrollbars, dropdown menus, windows.