FEAT: libwidget.c

This commit is contained in:
boreddevnl
2026-03-22 22:07:30 +01:00
parent 4e8ea5acd2
commit 63749b8734
12 changed files with 1318 additions and 909 deletions

2405
src/wm/cmd.c Normal file

File diff suppressed because it is too large Load Diff

31
src/wm/cmd.h Normal file
View File

@@ -0,0 +1,31 @@
// Copyright (c) 2023-2026 Chris (boreddevnl)
// This software is released under the GNU General Public License v3.0. See LICENSE file for details.
// This header needs to maintain in any file it is present in, as per the GPL license terms.
#ifndef CMD_H
#define CMD_H
#include "wm.h"
extern Window win_cmd;
void cmd_init(void);
void cmd_reset(void);
// IO Functions
void cmd_write(const char *str);
void cmd_putchar(char c);
void cmd_write_int(int n);
void cmd_write_hex(uint64_t n);
int cmd_get_cursor_col(void);
void cmd_screen_clear(void);
void cmd_increment_msg_count(void);
void cmd_reset_msg_count(void);
void cmd_handle_resize(Window *win, int w, int h);
void cmd_handle_click(Window *win, int x, int y);
uint32_t cmd_get_config_value(const char *key);
void cmd_set_current_color(uint32_t color);
#endif

View File

@@ -42,6 +42,28 @@ static int dropdown_menu_item_height = 25;
#define FILE_CONTEXT_MENU_HEIGHT 50
#define CONTEXT_MENU_ITEM_HEIGHT 25
static void wm_draw_rect(void *user_data, int x, int y, int w, int h, uint32_t color) {
(void)user_data; draw_rect(x, y, w, h, color);
}
static void wm_draw_rounded_rect_filled(void *user_data, int x, int y, int w, int h, int r, uint32_t color) {
(void)user_data; draw_rounded_rect_filled(x, y, w, h, r, color);
}
static void wm_draw_string(void *user_data, int x, int y, const char *str, uint32_t color) {
(void)user_data; draw_string(x, y, str, color);
}
static int wm_measure_string_width(void *user_data, const char *str) {
(void)user_data;
return font_manager_get_string_width(graphics_get_current_ttf(), str);
}
static widget_context_t wm_widget_ctx = {
.user_data = NULL,
.draw_rect = wm_draw_rect,
.draw_rounded_rect_filled = wm_draw_rounded_rect_filled,
.draw_string = wm_draw_string,
.measure_string_width = wm_measure_string_width,
.mark_dirty = NULL
};
static char clipboard_path[FAT32_MAX_PATH] = "";
static int clipboard_action = 0;
#define FILE_CONTEXT_ITEMS 2
@@ -950,16 +972,15 @@ static void explorer_paint(Window *win) {
draw_string(path_x + 6 + path_label_w + 6, offset_y + 8, state->current_path, COLOR_DARK_TEXT);
int dropdown_btn_x = win->x + win->w - 90;
draw_rounded_rect_filled(dropdown_btn_x, offset_y + 3, 35, 22, 5, COLOR_DARK_PANEL);
draw_string(dropdown_btn_x + 10, offset_y + 8, "...", COLOR_DARK_TEXT);
widget_button_init(&state->btn_dropdown, dropdown_btn_x, offset_y + 3, 35, 22, "...");
widget_button_init(&state->btn_back, win->x + win->w - 40, offset_y + 3, 30, 22, "<");
widget_button_init(&state->btn_up, win->x + win->w - 160, offset_y + 3, 30, 22, "^");
widget_button_init(&state->btn_fwd, win->x + win->w - 125, offset_y + 3, 30, 22, "v");
draw_rounded_rect_filled(win->x + win->w - 40, offset_y + 3, 30, 22, 5, COLOR_DARK_PANEL);
draw_string(win->x + win->w - 32, offset_y + 8, "<", COLOR_DARK_TEXT);
draw_rounded_rect_filled(win->x + win->w - 160, offset_y + 3, 30, 22, 5, COLOR_DARK_PANEL);
draw_string(win->x + win->w - 150, offset_y + 8, "^", COLOR_DARK_TEXT);
draw_rounded_rect_filled(win->x + win->w - 125, offset_y + 3, 30, 22, 5, COLOR_DARK_PANEL);
draw_string(win->x + win->w - 115, offset_y + 8, "v", COLOR_DARK_TEXT);
widget_button_draw(&wm_widget_ctx, &state->btn_dropdown);
widget_button_draw(&wm_widget_ctx, &state->btn_back);
widget_button_draw(&wm_widget_ctx, &state->btn_up);
widget_button_draw(&wm_widget_ctx, &state->btn_fwd);
int content_start_y = offset_y + 30;
@@ -1043,25 +1064,15 @@ static void explorer_paint(Window *win) {
draw_string(dlg_x + 10, dlg_y + 10, "Create New File", COLOR_WHITE);
draw_rounded_rect_filled(dlg_x + 10, dlg_y + 35, 280, 20, 4, COLOR_DARK_BG);
draw_string(dlg_x + 15, dlg_y + 40, state->dialog_input, COLOR_WHITE);
{ int max_w = 265;
ttf_font_t *ttf_ = graphics_get_current_ttf();
int total_w = font_manager_get_string_width(ttf_, state->dialog_input);
int scroll_x = 0;
if (total_w > max_w) scroll_x = total_w - max_w;
char sub_[128]; int k_=0;
for(k_=0; k_<state->dialog_input_cursor && state->dialog_input[k_]; k_++) sub_[k_]=state->dialog_input[k_];
sub_[k_]=0;
int cx_ = font_manager_get_string_width(ttf_, sub_) - scroll_x;
if (cx_ < 0) cx_ = 0;
if (cx_ > max_w) cx_ = max_w;
draw_rect(dlg_x+15+cx_, dlg_y+39, 2, 12, COLOR_WHITE); }
widget_textbox_init(&state->dialog_textbox, dlg_x + 10, dlg_y + 35, 280, 25, state->dialog_input, DIALOG_INPUT_MAX);
state->dialog_textbox.focused = true;
state->dialog_textbox.cursor_pos = state->dialog_input_cursor;
widget_textbox_draw(&wm_widget_ctx, &state->dialog_textbox);
draw_rounded_rect_filled(dlg_x + 50, dlg_y + 65, 80, 25, 6, COLOR_DARK_BORDER);
draw_string(dlg_x + 70, dlg_y + 72, "Create", COLOR_WHITE);
draw_rounded_rect_filled(dlg_x + 170, dlg_y + 65, 80, 25, 6, COLOR_DARK_BORDER);
draw_string(dlg_x + 185, dlg_y + 72, "Cancel", COLOR_WHITE);
widget_button_init(&state->btn_primary, dlg_x + 50, dlg_y + 70, 80, 25, "Create");
widget_button_init(&state->btn_secondary, dlg_x + 170, dlg_y + 70, 80, 25, "Cancel");
widget_button_draw(&wm_widget_ctx, &state->btn_primary);
widget_button_draw(&wm_widget_ctx, &state->btn_secondary);
} else if (state->dialog_state == DIALOG_CREATE_FOLDER) {
int dlg_x = win->x + win->w / 2 - 150;
int dlg_y = win->y + win->h / 2 - 60;
@@ -1070,25 +1081,15 @@ static void explorer_paint(Window *win) {
draw_string(dlg_x + 10, dlg_y + 10, "Create New Folder", COLOR_WHITE);
draw_rounded_rect_filled(dlg_x + 10, dlg_y + 35, 280, 20, 4, COLOR_DARK_BG);
draw_string(dlg_x + 15, dlg_y + 40, state->dialog_input, COLOR_WHITE);
{ int max_w = 265;
ttf_font_t *ttf_ = graphics_get_current_ttf();
int total_w = font_manager_get_string_width(ttf_, state->dialog_input);
int scroll_x = 0;
if (total_w > max_w) scroll_x = total_w - max_w;
char sub_[128]; int k_=0;
for(k_=0; k_<state->dialog_input_cursor && state->dialog_input[k_]; k_++) sub_[k_]=state->dialog_input[k_];
sub_[k_]=0;
int cx_ = font_manager_get_string_width(ttf_, sub_) - scroll_x;
if (cx_ < 0) cx_ = 0;
if (cx_ > max_w) cx_ = max_w;
draw_rect(dlg_x+15+cx_, dlg_y+39, 2, 12, COLOR_WHITE); }
widget_textbox_init(&state->dialog_textbox, dlg_x + 10, dlg_y + 35, 280, 25, state->dialog_input, DIALOG_INPUT_MAX);
state->dialog_textbox.focused = true;
state->dialog_textbox.cursor_pos = state->dialog_input_cursor;
widget_textbox_draw(&wm_widget_ctx, &state->dialog_textbox);
draw_rounded_rect_filled(dlg_x + 50, dlg_y + 65, 80, 25, 6, COLOR_DARK_BORDER);
draw_string(dlg_x + 70, dlg_y + 72, "Create", COLOR_WHITE);
draw_rounded_rect_filled(dlg_x + 170, dlg_y + 65, 80, 25, 6, COLOR_DARK_BORDER);
draw_string(dlg_x + 185, dlg_y + 72, "Cancel", COLOR_WHITE);
widget_button_init(&state->btn_primary, dlg_x + 50, dlg_y + 70, 80, 25, "Create");
widget_button_init(&state->btn_secondary, dlg_x + 170, dlg_y + 70, 80, 25, "Cancel");
widget_button_draw(&wm_widget_ctx, &state->btn_primary);
widget_button_draw(&wm_widget_ctx, &state->btn_secondary);
} else if (state->dialog_state == DIALOG_DELETE_CONFIRM) {
int dlg_x = win->x + win->w / 2 - 150;
int dlg_y = win->y + win->h / 2 - 60;
@@ -1107,8 +1108,14 @@ static void explorer_paint(Window *win) {
}
draw_rounded_rect_filled(dlg_x + 50, dlg_y + 65, 80, 25, 6, 0xFF8B2020);
draw_string(dlg_x + 68, dlg_y + 72, "Delete", COLOR_WHITE);
draw_rounded_rect_filled(dlg_x + 170, dlg_y + 65, 80, 25, 6, COLOR_DARK_BORDER);
draw_string(dlg_x + 185, dlg_y + 72, "Cancel", COLOR_WHITE);
// Use libwidget only for the cancel button, or do we want to use libwidget for the delete button too?
// Let's use libwidget but the delete button needs red styling so maybe just keep it manual or make it secondary.
// Actually wait, I will use libwidget for both and let the text dictate the action, we can't style individual buttons yet.
widget_button_init(&state->btn_primary, dlg_x + 50, dlg_y + 70, 80, 25, "Delete");
widget_button_init(&state->btn_secondary, dlg_x + 170, dlg_y + 70, 80, 25, "Cancel");
widget_button_draw(&wm_widget_ctx, &state->btn_primary);
widget_button_draw(&wm_widget_ctx, &state->btn_secondary);
} else if (state->dialog_state == DIALOG_REPLACE_CONFIRM) {
int dlg_x = win->x + win->w / 2 - 150;
int dlg_y = win->y + win->h / 2 - 60;
@@ -1120,10 +1127,10 @@ static void explorer_paint(Window *win) {
draw_string(dlg_x + 10, dlg_y + 35, "Replace existing file?", 0xFFAAAAAA);
draw_string(dlg_x + 10, dlg_y + 48, "This cannot be undone.", 0xFFAAAAAA);
draw_rounded_rect_filled(dlg_x + 50, dlg_y + 70, 80, 25, 6, COLOR_DARK_BORDER);
draw_string(dlg_x + 63, dlg_y + 77, "Replace", COLOR_WHITE);
draw_rounded_rect_filled(dlg_x + 170, dlg_y + 70, 80, 25, 6, COLOR_DARK_BORDER);
draw_string(dlg_x + 185, dlg_y + 77, "Cancel", COLOR_WHITE);
widget_button_init(&state->btn_primary, dlg_x + 50, dlg_y + 70, 80, 25, "Replace");
widget_button_init(&state->btn_secondary, dlg_x + 170, dlg_y + 70, 80, 25, "Cancel");
widget_button_draw(&wm_widget_ctx, &state->btn_primary);
widget_button_draw(&wm_widget_ctx, &state->btn_secondary);
} else if (state->dialog_state == DIALOG_REPLACE_MOVE_CONFIRM) {
int dlg_x = win->x + win->w / 2 - 150;
int dlg_y = win->y + win->h / 2 - 60;
@@ -1150,10 +1157,10 @@ static void explorer_paint(Window *win) {
draw_string(dlg_x + 10, dlg_y + 35, "Overwrite existing file?", 0xFFAAAAAA);
draw_string(dlg_x + 10, dlg_y + 48, "This cannot be undone.", 0xFFAAAAAA);
draw_rounded_rect_filled(dlg_x + 50, dlg_y + 70, 80, 25, 6, COLOR_DARK_BORDER);
draw_string(dlg_x + 57, dlg_y + 77, "Overwrite", COLOR_WHITE);
draw_rounded_rect_filled(dlg_x + 170, dlg_y + 70, 80, 25, 6, COLOR_DARK_BORDER);
draw_string(dlg_x + 185, dlg_y + 77, "Cancel", COLOR_WHITE);
widget_button_init(&state->btn_primary, dlg_x + 50, dlg_y + 70, 80, 25, "Overwrite");
widget_button_init(&state->btn_secondary, dlg_x + 170, dlg_y + 70, 80, 25, "Cancel");
widget_button_draw(&wm_widget_ctx, &state->btn_primary);
widget_button_draw(&wm_widget_ctx, &state->btn_secondary);
} else if (state->dialog_state == DIALOG_ERROR) {
int dlg_x = win->x + win->w / 2 - 150;
int dlg_y = win->y + win->h / 2 - 60;
@@ -1163,32 +1170,23 @@ static void explorer_paint(Window *win) {
draw_string(dlg_x + 10, dlg_y + 10, "Error", 0xFFFF6B6B);
draw_string(dlg_x + 10, dlg_y + 40, state->dialog_input, 0xFFAAAAAA);
draw_rounded_rect_filled(dlg_x + 110, dlg_y + 70, 80, 25, 6, COLOR_DARK_BORDER);
draw_string(dlg_x + 138, dlg_y + 77, "OK", COLOR_WHITE);
widget_button_init(&state->btn_primary, dlg_x + 110, dlg_y + 70, 80, 25, "OK");
widget_button_draw(&wm_widget_ctx, &state->btn_primary);
} else if (state->dialog_state == DIALOG_RENAME) {
int dlg_x = win->x + win->w / 2 - 150;
int dlg_y = win->y + win->h / 2 - 60;
draw_rounded_rect_filled(dlg_x, dlg_y, 300, 110, 8, COLOR_DARK_PANEL);
draw_string(dlg_x + 10, dlg_y + 10, "Rename", COLOR_WHITE);
draw_rounded_rect_filled(dlg_x + 10, dlg_y + 35, 280, 20, 4, COLOR_DARK_BG);
draw_string(dlg_x + 15, dlg_y + 40, state->dialog_input, COLOR_WHITE);
{ int max_w = 265;
ttf_font_t *ttf_ = graphics_get_current_ttf();
int total_w = font_manager_get_string_width(ttf_, state->dialog_input);
int scroll_x = 0;
if (total_w > max_w) scroll_x = total_w - max_w;
char sub_[128]; int k_=0;
for(k_=0; k_<state->dialog_input_cursor && state->dialog_input[k_]; k_++) sub_[k_]=state->dialog_input[k_];
sub_[k_]=0;
int cx_ = font_manager_get_string_width(ttf_, sub_) - scroll_x;
if (cx_ < 0) cx_ = 0;
if (cx_ > max_w) cx_ = max_w;
draw_rect(dlg_x+15+cx_, dlg_y+39, 2, 12, COLOR_WHITE); }
draw_rounded_rect_filled(dlg_x + 50, dlg_y + 65, 80, 25, 6, COLOR_DARK_BORDER);
draw_string(dlg_x + 68, dlg_y + 72, "Rename", COLOR_WHITE);
draw_rounded_rect_filled(dlg_x + 170, dlg_y + 65, 80, 25, 6, COLOR_DARK_BORDER);
draw_string(dlg_x + 185, dlg_y + 72, "Cancel", COLOR_WHITE);
widget_textbox_init(&state->dialog_textbox, dlg_x + 10, dlg_y + 35, 280, 25, state->dialog_input, DIALOG_INPUT_MAX);
state->dialog_textbox.focused = true;
state->dialog_textbox.cursor_pos = state->dialog_input_cursor;
widget_textbox_draw(&wm_widget_ctx, &state->dialog_textbox);
widget_button_init(&state->btn_primary, dlg_x + 50, dlg_y + 70, 80, 25, "Rename");
widget_button_init(&state->btn_secondary, dlg_x + 170, dlg_y + 70, 80, 25, "Cancel");
widget_button_draw(&wm_widget_ctx, &state->btn_primary);
widget_button_draw(&wm_widget_ctx, &state->btn_secondary);
}
if (state->file_context_menu_visible) {
@@ -1220,6 +1218,9 @@ static void explorer_paint(Window *win) {
}
#define WIDGET_CLICKED(btn, cx, cy) ((cx) >= (btn)->x && (cx) < (btn)->x + (btn)->w && (cy) >= (btn)->y && (cy) < (btn)->y + (btn)->h)
#define TEXTBOX_CLICKED(tb, cx, cy) ((cx) >= (tb)->x && (cx) < (tb)->x + (tb)->w && (cy) >= (tb)->y && (cy) < (tb)->y + (tb)->h)
static void explorer_handle_click(Window *win, int x, int y) {
ExplorerState *state = (ExplorerState*)win->data;
if (state->file_context_menu_visible) {
@@ -1228,106 +1229,90 @@ static void explorer_handle_click(Window *win, int x, int y) {
}
if (state->dialog_state == DIALOG_CREATE_FILE || state->dialog_state == DIALOG_CREATE_FOLDER) {
int dlg_x = win->w / 2 - 150;
int dlg_y = win->h / 2 - 80;
if (x >= dlg_x + 50 && x < dlg_x + 130 &&
y >= dlg_y + 65 && y < dlg_y + 90) {
if (state->dialog_state == DIALOG_CREATE_FILE) {
dialog_confirm_create_file(win);
} else {
dialog_confirm_create_folder(win);
}
if (WIDGET_CLICKED(&state->btn_primary, win->x + x, win->y + y + 20)) {
state->btn_primary.pressed = false;
if (state->dialog_state == DIALOG_CREATE_FILE) dialog_confirm_create_file(win);
else dialog_confirm_create_folder(win);
return;
}
if (x >= dlg_x + 170 && x < dlg_x + 250 &&
y >= dlg_y + 65 && y < dlg_y + 90) {
if (WIDGET_CLICKED(&state->btn_secondary, win->x + x, win->y + y)) {
state->btn_secondary.pressed = false;
dialog_close(win);
return;
}
if (x >= dlg_x + 10 && x < dlg_x + 290 &&
y >= dlg_y + 35 && y < dlg_y + 55) {
state->dialog_input_cursor = (x - dlg_x - 15) / 8;
if (TEXTBOX_CLICKED(&state->dialog_textbox, win->x + x, win->y + y)) {
state->dialog_input_cursor = (win->x + x - state->dialog_textbox.x - 5) / 8;
if (state->dialog_input_cursor > (int)explorer_strlen(state->dialog_input)) {
state->dialog_input_cursor = explorer_strlen(state->dialog_input);
}
if (state->dialog_input_cursor < 0) state->dialog_input_cursor = 0;
return;
}
return;
} else if (state->dialog_state == DIALOG_DELETE_CONFIRM) {
int dlg_x = win->w / 2 - 150;
int dlg_y = win->h / 2 - 80;
if (x >= dlg_x + 50 && x < dlg_x + 130 &&
y >= dlg_y + 65 && y < dlg_y + 90) {
if (WIDGET_CLICKED(&state->btn_primary, win->x + x, win->y + y + 20)) {
state->btn_primary.pressed = false;
dialog_confirm_delete(win);
return;
}
if (x >= dlg_x + 170 && x < dlg_x + 250 &&
y >= dlg_y + 65 && y < dlg_y + 90) {
if (WIDGET_CLICKED(&state->btn_secondary, win->x + x, win->y + y)) {
state->btn_secondary.pressed = false;
dialog_close(win);
return;
}
return;
} else if (state->dialog_state == DIALOG_REPLACE_CONFIRM) {
int dlg_x = win->w / 2 - 150;
int dlg_y = win->h / 2 - 80;
if (x >= dlg_x + 50 && x < dlg_x + 130 && y >= dlg_y + 70 && y < dlg_y + 95) {
if (WIDGET_CLICKED(&state->btn_primary, win->x + x, win->y + y + 20)) {
state->btn_primary.pressed = false;
dialog_confirm_replace(win);
return;
}
if (x >= dlg_x + 170 && x < dlg_x + 250 && y >= dlg_y + 70 && y < dlg_y + 95) {
if (WIDGET_CLICKED(&state->btn_secondary, win->x + x, win->y + y + 20)) {
state->btn_secondary.pressed = false;
dialog_close(win);
return;
}
return;
} else if (state->dialog_state == DIALOG_REPLACE_MOVE_CONFIRM) {
int dlg_x = win->w / 2 - 150;
int dlg_y = win->h / 2 - 80;
if (x >= dlg_x + 50 && x < dlg_x + 130 && y >= dlg_y + 70 && y < dlg_y + 95) {
if (WIDGET_CLICKED(&state->btn_primary, win->x + x, win->y + y + 20)) {
state->btn_primary.pressed = false;
dialog_confirm_replace_move(win);
return;
}
if (x >= dlg_x + 170 && x < dlg_x + 250 && y >= dlg_y + 70 && y < dlg_y + 95) {
if (WIDGET_CLICKED(&state->btn_secondary, win->x + x, win->y + y + 20)) {
state->btn_secondary.pressed = false;
dialog_close(win);
return;
}
return;
} else if (state->dialog_state == DIALOG_CREATE_REPLACE_CONFIRM) {
int dlg_x = win->w / 2 - 150;
int dlg_y = win->h / 2 - 80;
if (x >= dlg_x + 50 && x < dlg_x + 130 && y >= dlg_y + 70 && y < dlg_y + 95) {
if (WIDGET_CLICKED(&state->btn_primary, win->x + x, win->y + y + 20)) {
state->btn_primary.pressed = false;
dialog_force_create_file(win);
return;
}
if (x >= dlg_x + 170 && x < dlg_x + 250 && y >= dlg_y + 70 && y < dlg_y + 95) {
if (WIDGET_CLICKED(&state->btn_secondary, win->x + x, win->y + y + 20)) {
state->btn_secondary.pressed = false;
dialog_close(win);
return;
}
return;
} else if (state->dialog_state == DIALOG_ERROR) {
int dlg_x = win->w / 2 - 150;
int dlg_y = win->h / 2 - 80;
if (x >= dlg_x + 110 && x < dlg_x + 190 && y >= dlg_y + 70 && y < dlg_y + 95) {
if (WIDGET_CLICKED(&state->btn_primary, win->x + x, win->y + y + 20)) {
state->btn_primary.pressed = false;
dialog_close(win);
return;
}
return;
} else if (state->dialog_state == DIALOG_RENAME) {
int dlg_x = win->w / 2 - 150;
int dlg_y = win->h / 2 - 80;
if (x >= dlg_x + 50 && x < dlg_x + 130 && y >= dlg_y + 65 && y < dlg_y + 90) {
if (WIDGET_CLICKED(&state->btn_primary, win->x + x, win->y + y + 20)) {
state->btn_primary.pressed = false;
char new_path[FAT32_MAX_PATH];
explorer_strcpy(new_path, state->current_path);
if (new_path[explorer_strlen(new_path)-1] != '/') explorer_strcat(new_path, "/");
@@ -1338,16 +1323,17 @@ static void explorer_handle_click(Window *win, int x, int y) {
return;
}
if (x >= dlg_x + 170 && x < dlg_x + 250 && y >= dlg_y + 65 && y < dlg_y + 90) {
if (WIDGET_CLICKED(&state->btn_secondary, win->x + x, win->y + y + 20)) {
state->btn_secondary.pressed = false;
dialog_close(win);
return;
}
if (x >= dlg_x + 10 && x < dlg_x + 290 && y >= dlg_y + 35 && y < dlg_y + 55) {
state->dialog_input_cursor = (x - dlg_x - 15) / 8;
if (TEXTBOX_CLICKED(&state->dialog_textbox, win->x + x, win->y + y + 20)) {
state->dialog_input_cursor = (win->x + x - state->dialog_textbox.x - 5) / 8;
if (state->dialog_input_cursor > (int)explorer_strlen(state->dialog_input)) {
state->dialog_input_cursor = explorer_strlen(state->dialog_input);
}
if (state->dialog_input_cursor < 0) state->dialog_input_cursor = 0;
return;
}
return;
@@ -1420,27 +1406,27 @@ static void explorer_handle_click(Window *win, int x, int y) {
return;
}
if (x >= win->w - 90 && x < win->w - 55 &&
y >= button_y && y < button_y + 22) {
if (WIDGET_CLICKED(&state->btn_dropdown, win->x + x, win->y + y + 20)) {
state->btn_dropdown.pressed = false;
dropdown_menu_toggle(win);
state->drive_menu_visible = false;
return;
}
if (x >= win->w - 40 && x < win->w - 10 &&
y >= button_y && y < button_y + 22) {
if (WIDGET_CLICKED(&state->btn_back, win->x + x, win->y + y + 20)) {
state->btn_back.pressed = false;
explorer_navigate_to(win, "..");
return;
}
if (x >= win->w - 160 && x < win->w - 130 &&
y >= button_y && y < button_y + 22) {
if (WIDGET_CLICKED(&state->btn_up, win->x + x, win->y + y + 20)) {
state->btn_up.pressed = false;
if (state->explorer_scroll_row > 0) state->explorer_scroll_row--;
return;
}
if (x >= win->w - 125 && x < win->w - 95 &&
y >= button_y && y < button_y + 22) {
if (WIDGET_CLICKED(&state->btn_fwd, win->x + x, win->y + y + 20)) {
state->btn_fwd.pressed = false;
int total_rows = (state->item_count + EXPLORER_COLS - 1) / EXPLORER_COLS;
if (total_rows == 0) total_rows = 1;
if (state->explorer_scroll_row < total_rows - (EXPLORER_ROWS - 1)) state->explorer_scroll_row++;

View File

@@ -7,6 +7,7 @@
#include "wm.h"
#include "fat32.h"
#include <stddef.h>
#include "libwidget.h"
// External windows references (for opening other apps)
extern Window win_explorer;
@@ -55,6 +56,16 @@ typedef struct {
int file_context_menu_y;
int file_context_menu_item;
// GUI widgets
widget_button_t btn_primary;
widget_button_t btn_secondary;
widget_button_t btn_dropdown;
widget_button_t btn_back;
widget_button_t btn_up;
widget_button_t btn_fwd;
widget_textbox_t dialog_textbox;
} ExplorerState;
void explorer_init(void);

View File

@@ -287,28 +287,39 @@ static int isqrt(int n) {
void draw_rounded_rect(int x, int y, int w, int h, int radius, uint32_t color) {
if (radius > w / 2) radius = w / 2;
if (radius > h / 2) radius = h / 2;
if (radius < 1) radius = 1;
if (radius < 1) {
// Draw a simple rect outline if no radius
draw_rect(x, y, w, 1, color);
draw_rect(x, y + h - 1, w, 1, color);
draw_rect(x, y + 1, 1, h - 2, color);
draw_rect(x + w - 1, y + 1, 1, h - 2, color);
return;
}
// Draw top and bottom edges
// Draw top and bottom straight edges
draw_rect(x + radius, y, w - 2*radius, 1, color);
draw_rect(x + radius, y + h - 1, w - 2*radius, 1, color);
// Draw left and right edges
// Draw left and right straight edges
draw_rect(x, y + radius, 1, h - 2*radius, color);
draw_rect(x + w - 1, y + radius, 1, h - 2*radius, color);
// Draw corner circles using integer approximation
for (int i = 0; i < radius; i++) {
int j = isqrt(radius*radius - i*i);
// Draw four corner arcs
for (int dy = 0; dy < radius; dy++) {
int y_dist = radius - 1 - dy;
int dx = isqrt(radius*radius - y_dist*y_dist);
int next_dx = (dy < radius - 1) ? isqrt(radius*radius - (y_dist - 1)*(y_dist - 1)) : radius;
// Top-left corner
put_pixel(x + radius - i - 1, y + radius - j, color);
// Top-right corner
put_pixel(x + w - radius + i, y + radius - j, color);
// Bottom-left corner
put_pixel(x + radius - i - 1, y + h - radius + j - 1, color);
// Bottom-right corner
put_pixel(x + w - radius + i, y + h - radius + j - 1, color);
for (int i = dx; i < next_dx && i <= radius; i++) {
// Top-left
put_pixel(x + radius - 1 - i, y + dy, color);
// Top-right
put_pixel(x + w - radius + i, y + dy, color);
// Bottom-left
put_pixel(x + radius - 1 - i, y + h - 1 - dy, color);
// Bottom-right
put_pixel(x + w - radius + i, y + h - 1 - dy, color);
}
}
}
@@ -316,25 +327,21 @@ void draw_rounded_rect(int x, int y, int w, int h, int radius, uint32_t color) {
void draw_rounded_rect_filled(int x, int y, int w, int h, int radius, uint32_t color) {
if (radius > w / 2) radius = w / 2;
if (radius > h / 2) radius = h / 2;
if (radius < 1) radius = 1;
if (radius < 1) {
draw_rect(x, y, w, h, color);
return;
}
// Draw main rectangle body (center part without corners)
draw_rect(x + radius, y, w - 2*radius, h, color);
draw_rect(x, y + radius, radius, h - 2*radius, color);
draw_rect(x + w - radius, y + radius, radius, h - 2*radius, color);
// Draw main rectangle body
draw_rect(x, y + radius, w, h - 2*radius, color);
// Draw rounded top and bottom caps
for (int dy = 0; dy < radius; dy++) {
int dx_top = isqrt(radius*radius - (radius - dy) * (radius - dy));
int y_dist = radius - 1 - dy;
int dx = isqrt(radius*radius - y_dist*y_dist);
int dx_bottom = isqrt(radius*radius - dy*dy);
draw_rect(x + radius - dx_top, y + dy, dx_top, 1, color);
draw_rect(x + w - radius, y + dy, dx_top, 1, color);
draw_rect(x + radius - dx_bottom, y + h - radius + dy, dx_bottom, 1, color);
draw_rect(x + w - radius, y + h - radius + dy, dx_bottom, 1, color);
draw_rect(x + radius - dx, y + dy, w - 2*radius + 2*dx, 1, color);
draw_rect(x + radius - dx, y + h - 1 - dy, w - 2*radius + 2*dx, 1, color);
}
}

380
src/wm/libwidget.c Normal file
View File

@@ -0,0 +1,380 @@
#include "libwidget.h"
#include <stddef.h>
#define COLOR_GRAY 0xFFC0C0C0
#define COLOR_LTGRAY 0xFFDFDFDF
#define COLOR_DKGRAY 0xFF808080
#define COLOR_WHITE 0xFFFFFFFF
#define COLOR_BLACK 0xFF000000
#define COLOR_SCROLLBAR_BG 0xFF2A2A2A
#define COLOR_SCROLLBAR_THUMB 0xFF505050
#define COLOR_SCROLLBAR_THUMB_HOVER 0xFF707070
#define COLOR_SCROLLBAR_THUMB_DRAG 0xFF909090
static size_t string_len(const char *str) {
size_t l = 0;
while(str && str[l]) l++;
return l;
}
#define MAC_BTN_BORDER 0xFF4A4A4C
#define MAC_BTN_BG_NORMAL 0xFF353537
#define MAC_BTN_BG_HOVER 0xFF454547
#define MAC_BTN_BG_PRESSED 0xFF555557
// --- Button Implementation ---
void widget_button_init(widget_button_t *btn, int x, int y, int w, int h, const char *text) {
btn->x = x;
btn->y = y;
btn->w = w;
btn->h = h;
btn->text = text;
btn->pressed = false;
btn->hovered = false;
btn->on_click = NULL;
}
void widget_button_draw(widget_context_t *ctx, widget_button_t *btn) {
uint32_t bg_color = MAC_BTN_BG_NORMAL;
if (btn->pressed) {
bg_color = MAC_BTN_BG_PRESSED;
} else if (btn->hovered) {
bg_color = MAC_BTN_BG_HOVER;
}
if (ctx->draw_rounded_rect_filled) {
ctx->draw_rounded_rect_filled(ctx->user_data, btn->x, btn->y, btn->w, btn->h, 6, MAC_BTN_BORDER);
ctx->draw_rounded_rect_filled(ctx->user_data, btn->x + 1, btn->y + 1, btn->w - 2, btn->h - 2, 5, bg_color);
} else if (ctx->draw_rect) {
ctx->draw_rect(ctx->user_data, btn->x, btn->y, btn->w, btn->h, MAC_BTN_BORDER);
ctx->draw_rect(ctx->user_data, btn->x + 1, btn->y + 1, btn->w - 2, btn->h - 2, bg_color);
}
if (btn->text && ctx->draw_string) {
int len = string_len(btn->text);
int tx = btn->x + (btn->w - (len * 8)) / 2;
int ty = btn->y + (btn->h - 8) / 2;
ctx->draw_string(ctx->user_data, tx, ty, btn->text, COLOR_WHITE);
}
}
bool widget_button_handle_mouse(widget_button_t *btn, int mx, int my, bool mouse_down, bool mouse_clicked, void *user_data) {
bool in_bounds = (mx >= btn->x && mx < btn->x + btn->w && my >= btn->y && my < btn->y + btn->h);
btn->hovered = in_bounds;
if (mouse_clicked && in_bounds) {
btn->pressed = true;
return true;
}
if (!mouse_down && btn->pressed) {
btn->pressed = false;
if (in_bounds && btn->on_click) {
btn->on_click(user_data);
}
return true;
}
return in_bounds;
}
// --- Scrollbar Implementation ---
void widget_scrollbar_init(widget_scrollbar_t *sb, int x, int y, int w, int h) {
sb->x = x;
sb->y = y;
sb->w = w;
sb->h = h;
sb->content_height = h;
sb->scroll_y = 0;
sb->is_dragging = false;
sb->on_scroll = NULL;
}
void widget_scrollbar_update(widget_scrollbar_t *sb, int content_height, int scroll_y) {
sb->content_height = content_height;
sb->scroll_y = scroll_y;
}
void widget_scrollbar_draw(widget_context_t *ctx, widget_scrollbar_t *sb) {
// Only draw thumb if content is larger than view
if (sb->content_height > sb->h) {
int thumb_h = (sb->h * sb->h) / sb->content_height;
if (thumb_h < 20) thumb_h = 20;
int max_scroll = sb->content_height - sb->h;
if (sb->scroll_y > max_scroll) sb->scroll_y = max_scroll;
if (sb->scroll_y < 0) sb->scroll_y = 0;
int thumb_y = sb->y + (sb->scroll_y * (sb->h - thumb_h)) / max_scroll;
uint32_t color = 0xFF888888; // Subtle gray thumb for mac style
if (sb->is_dragging) color = 0xFF666666;
if (ctx->draw_rounded_rect_filled) {
// Pill shaped thumb with margin
int margin = 2;
int radius = (sb->w - margin*2) / 2;
ctx->draw_rounded_rect_filled(ctx->user_data, sb->x + margin, thumb_y + margin, sb->w - margin*2, thumb_h - margin*2, radius, color);
} else if (ctx->draw_rect) {
ctx->draw_rect(ctx->user_data, sb->x, thumb_y, sb->w, thumb_h, color);
}
}
}
bool widget_scrollbar_handle_mouse(widget_scrollbar_t *sb, int mx, int my, bool mouse_down, void *user_data) {
if (sb->content_height <= sb->h) return false;
int thumb_h = (sb->h * sb->h) / sb->content_height;
if (thumb_h < 20) thumb_h = 20;
int max_scroll = sb->content_height - sb->h;
if (sb->scroll_y > max_scroll) sb->scroll_y = max_scroll;
if (sb->scroll_y < 0) sb->scroll_y = 0;
int thumb_y = sb->y + (sb->scroll_y * (sb->h - thumb_h)) / max_scroll;
bool in_thumb = (mx >= sb->x && mx < sb->x + sb->w && my >= thumb_y && my < thumb_y + thumb_h);
bool in_track = (mx >= sb->x && mx < sb->x + sb->w && my >= sb->y && my < sb->y + sb->h);
if (mouse_down && !sb->is_dragging) {
if (in_thumb) {
sb->is_dragging = true;
sb->drag_start_my = my;
sb->drag_start_scroll_y = sb->scroll_y;
return true;
} else if (in_track) {
// Page scroll
if (my < thumb_y) {
sb->scroll_y -= sb->h;
} else {
sb->scroll_y += sb->h;
}
if (sb->scroll_y < 0) sb->scroll_y = 0;
if (sb->scroll_y > max_scroll) sb->scroll_y = max_scroll;
if (sb->on_scroll) sb->on_scroll(user_data, sb->scroll_y);
return true;
}
} else if (!mouse_down) {
sb->is_dragging = false;
}
if (sb->is_dragging && mouse_down) {
int dy = my - sb->drag_start_my;
int track_h = sb->h - thumb_h;
if (track_h > 0) {
float ratio = (float)max_scroll / (float)track_h;
int new_scroll = sb->drag_start_scroll_y + (int)(dy * ratio);
if (new_scroll < 0) new_scroll = 0;
if (new_scroll > max_scroll) new_scroll = max_scroll;
if (new_scroll != sb->scroll_y) {
sb->scroll_y = new_scroll;
if (sb->on_scroll) sb->on_scroll(user_data, sb->scroll_y);
}
}
return true;
}
return in_track || sb->is_dragging;
}
// --- TextBox Implementation ---
void widget_textbox_init(widget_textbox_t *tb, int x, int y, int w, int h, char *buffer, int max_len) {
tb->x = x; tb->y = y; tb->w = w; tb->h = h;
tb->text = buffer;
tb->max_len = max_len;
tb->cursor_pos = string_len(buffer);
tb->focused = false;
tb->on_change = NULL;
}
void widget_textbox_draw(widget_context_t *ctx, widget_textbox_t *tb) {
// Background and border
if (ctx->draw_rounded_rect_filled) {
ctx->draw_rounded_rect_filled(ctx->user_data, tb->x, tb->y, tb->w, tb->h, 4, MAC_BTN_BORDER);
ctx->draw_rounded_rect_filled(ctx->user_data, tb->x + 1, tb->y + 1, tb->w - 2, tb->h - 2, 3, COLOR_BLACK); // dark background
} else if (ctx->draw_rect) {
ctx->draw_rect(ctx->user_data, tb->x, tb->y, tb->w, tb->h, MAC_BTN_BORDER);
ctx->draw_rect(ctx->user_data, tb->x + 1, tb->y + 1, tb->w - 2, tb->h - 2, COLOR_BLACK);
}
if (ctx->draw_string && tb->text) {
int max_w = tb->w - 15;
int scroll_x = 0;
int text_w = 0;
if (ctx->measure_string_width) {
text_w = ctx->measure_string_width(ctx->user_data, tb->text);
} else {
text_w = string_len(tb->text) * 8;
}
if (text_w > max_w) scroll_x = text_w - max_w;
// Very basic simple drawing, without proper clipping since context lacks it
ctx->draw_string(ctx->user_data, tb->x + 5, tb->y + (tb->h - 8) / 2, tb->text, COLOR_WHITE);
if (tb->focused && ctx->draw_rect) {
int cx = 0;
if (ctx->measure_string_width) {
// measure up to cursor
char tmp[256];
int k = 0;
for (k = 0; k < tb->cursor_pos && tb->text[k]; k++) {
tmp[k] = tb->text[k];
}
tmp[k] = 0;
cx = ctx->measure_string_width(ctx->user_data, tmp);
} else {
cx = tb->cursor_pos * 8;
}
if (cx > max_w) cx = max_w; // clamped to visible end
ctx->draw_rect(ctx->user_data, tb->x + 5 + cx, tb->y + (tb->h - 12) / 2, 2, 12, COLOR_WHITE);
}
}
}
bool widget_textbox_handle_mouse(widget_textbox_t *tb, int mx, int my, bool mouse_clicked, void *user_data) {
bool in_bounds = (mx >= tb->x && mx < tb->x + tb->w && my >= tb->y && my < tb->y + tb->h);
if (mouse_clicked) {
tb->focused = in_bounds;
}
return in_bounds;
}
bool widget_textbox_handle_key(widget_textbox_t *tb, char c, void *user_data) {
if (!tb->focused || !tb->text) return false;
int len = string_len(tb->text);
if (c == '\b') { // backspace
if (len > 0) {
tb->text[len - 1] = '\0';
tb->cursor_pos = len - 1;
if (tb->on_change) tb->on_change(user_data);
}
} else if (c >= 32 && c < 127) {
if (len < tb->max_len - 1) {
tb->text[len] = c;
tb->text[len + 1] = '\0';
tb->cursor_pos = len + 1;
if (tb->on_change) tb->on_change(user_data);
}
}
return true;
}
// --- Dropdown Implementation ---
void widget_dropdown_init(widget_dropdown_t *dd, int x, int y, int w, int h, const char **items, int count) {
dd->x = x; dd->y = y; dd->w = w; dd->h = h;
dd->items = items;
dd->item_count = count;
dd->selected_idx = 0;
dd->is_open = false;
dd->on_select = NULL;
}
void widget_dropdown_draw(widget_context_t *ctx, widget_dropdown_t *dd) {
if (ctx->draw_rounded_rect_filled) {
ctx->draw_rounded_rect_filled(ctx->user_data, dd->x, dd->y, dd->w, dd->h, 4, MAC_BTN_BORDER);
ctx->draw_rounded_rect_filled(ctx->user_data, dd->x + 1, dd->y + 1, dd->w - 2, dd->h - 2, 3, MAC_BTN_BG_NORMAL);
} else if (ctx->draw_rect) {
ctx->draw_rect(ctx->user_data, dd->x, dd->y, dd->w, dd->h, MAC_BTN_BORDER);
ctx->draw_rect(ctx->user_data, dd->x + 1, dd->y + 1, dd->w - 2, dd->h - 2, MAC_BTN_BG_NORMAL);
}
if (ctx->draw_string && dd->items && dd->item_count > 0 && dd->selected_idx >= 0 && dd->selected_idx < dd->item_count) {
ctx->draw_string(ctx->user_data, dd->x + 5, dd->y + (dd->h - 8) / 2, dd->items[dd->selected_idx], COLOR_WHITE);
ctx->draw_string(ctx->user_data, dd->x + dd->w - 12, dd->y + (dd->h - 8) / 2, "v", COLOR_WHITE);
}
if (dd->is_open && ctx->draw_rect && dd->items) {
int menu_h = dd->item_count * dd->h;
ctx->draw_rect(ctx->user_data, dd->x, dd->y + dd->h, dd->w, menu_h, MAC_BTN_BORDER);
ctx->draw_rect(ctx->user_data, dd->x + 1, dd->y + dd->h + 1, dd->w - 2, menu_h - 2, MAC_BTN_BG_NORMAL);
for (int i = 0; i < dd->item_count; i++) {
if (ctx->draw_string) {
ctx->draw_string(ctx->user_data, dd->x + 5, dd->y + dd->h + i * dd->h + (dd->h - 8)/2, dd->items[i], COLOR_WHITE);
}
}
}
}
bool widget_dropdown_handle_mouse(widget_dropdown_t *dd, int mx, int my, bool mouse_clicked, void *user_data) {
if (!mouse_clicked) return false;
if (dd->is_open) {
int menu_h = dd->item_count * dd->h;
if (mx >= dd->x && mx < dd->x + dd->w && my >= dd->y + dd->h && my < dd->y + dd->h + menu_h) {
int clicked_idx = (my - (dd->y + dd->h)) / dd->h;
if (clicked_idx >= 0 && clicked_idx < dd->item_count) {
dd->selected_idx = clicked_idx;
dd->is_open = false;
if (dd->on_select) dd->on_select(user_data, clicked_idx);
return true;
}
}
dd->is_open = false;
}
if (mx >= dd->x && mx < dd->x + dd->w && my >= dd->y && my < dd->y + dd->h) {
dd->is_open = !dd->is_open;
return true;
}
return false;
}
// --- Checkbox / Radio Implementation ---
void widget_checkbox_init(widget_checkbox_t *cb, int x, int y, int w, int h, const char *text, bool is_radio) {
cb->x = x; cb->y = y; cb->w = w; cb->h = h;
cb->text = text;
cb->checked = false;
cb->is_radio = is_radio;
cb->on_toggle = NULL;
}
void widget_checkbox_draw(widget_context_t *ctx, widget_checkbox_t *cb) {
int box_size = 14;
int box_y = cb->y + (cb->h - box_size) / 2;
if (ctx->draw_rounded_rect_filled) {
int radius = cb->is_radio ? box_size / 2 : 3;
ctx->draw_rounded_rect_filled(ctx->user_data, cb->x, box_y, box_size, box_size, radius, MAC_BTN_BORDER);
ctx->draw_rounded_rect_filled(ctx->user_data, cb->x + 1, box_y + 1, box_size - 2, box_size - 2, radius - 1, MAC_BTN_BG_NORMAL);
if (cb->checked) {
int inner = box_size - 6;
int inner_rad = cb->is_radio ? inner / 2 : 2;
ctx->draw_rounded_rect_filled(ctx->user_data, cb->x + 3, box_y + 3, inner, inner, inner_rad, COLOR_WHITE);
}
} else if (ctx->draw_rect) {
ctx->draw_rect(ctx->user_data, cb->x, box_y, box_size, box_size, MAC_BTN_BORDER);
ctx->draw_rect(ctx->user_data, cb->x + 1, box_y + 1, box_size - 2, box_size - 2, MAC_BTN_BG_NORMAL);
if (cb->checked) {
int inner = box_size - 6;
ctx->draw_rect(ctx->user_data, cb->x + 3, box_y + 3, inner, inner, COLOR_WHITE);
}
}
if (ctx->draw_string && cb->text) {
ctx->draw_string(ctx->user_data, cb->x + box_size + 8, cb->y + (cb->h - 8) / 2, cb->text, COLOR_WHITE);
}
}
bool widget_checkbox_handle_mouse(widget_checkbox_t *cb, int mx, int my, bool mouse_clicked, void *user_data) {
if (!mouse_clicked) return false;
if (mx >= cb->x && mx < cb->x + cb->w && my >= cb->y && my < cb->y + cb->h) {
cb->checked = !cb->checked;
if (cb->on_toggle) cb->on_toggle(user_data, cb->checked);
return true;
}
return false;
}

92
src/wm/libwidget.h Normal file
View File

@@ -0,0 +1,92 @@
#ifndef LIBWIDGET_H
#define LIBWIDGET_H
#include <stdint.h>
#include <stdbool.h>
// Widget Context for abstract drawing backend
typedef struct {
void *user_data;
void (*draw_rect)(void *user_data, int x, int y, int w, int h, uint32_t color);
void (*draw_rounded_rect_filled)(void *user_data, int x, int y, int w, int h, int r, uint32_t color);
void (*draw_string)(void *user_data, int x, int y, const char *str, uint32_t color);
int (*measure_string_width)(void *user_data, const char *str);
void (*mark_dirty)(void *user_data, int x, int y, int w, int h);
} widget_context_t;
// --- Button ---
typedef struct {
int x, y, w, h;
const char *text;
bool pressed;
bool hovered;
void (*on_click)(void *user_data);
} widget_button_t;
void widget_button_init(widget_button_t *btn, int x, int y, int w, int h, const char *text);
void widget_button_draw(widget_context_t *ctx, widget_button_t *btn);
// Returns true if event was consumed
bool widget_button_handle_mouse(widget_button_t *btn, int mx, int my, bool mouse_down, bool mouse_clicked, void *user_data);
// --- Scrollbar ---
typedef struct {
int x, y, w, h;
int content_height;
int scroll_y;
bool is_dragging;
int drag_start_my;
int drag_start_scroll_y;
void (*on_scroll)(void *user_data, int new_scroll_y);
} widget_scrollbar_t;
void widget_scrollbar_init(widget_scrollbar_t *sb, int x, int y, int w, int h);
void widget_scrollbar_update(widget_scrollbar_t *sb, int content_height, int scroll_y);
void widget_scrollbar_draw(widget_context_t *ctx, widget_scrollbar_t *sb);
// Returns true if event was consumed
bool widget_scrollbar_handle_mouse(widget_scrollbar_t *sb, int mx, int my, bool mouse_down, void *user_data);
// --- TextBox ---
typedef struct {
int x, y, w, h;
char *text;
int max_len;
int cursor_pos;
bool focused;
void (*on_change)(void *user_data);
} widget_textbox_t;
void widget_textbox_init(widget_textbox_t *tb, int x, int y, int w, int h, char *buffer, int max_len);
void widget_textbox_draw(widget_context_t *ctx, widget_textbox_t *tb);
bool widget_textbox_handle_mouse(widget_textbox_t *tb, int mx, int my, bool mouse_clicked, void *user_data);
bool widget_textbox_handle_key(widget_textbox_t *tb, char c, void *user_data);
// --- Dropdown ---
typedef struct {
int x, y, w, h;
const char **items;
int item_count;
int selected_idx;
bool is_open;
void (*on_select)(void *user_data, int new_idx);
} widget_dropdown_t;
void widget_dropdown_init(widget_dropdown_t *dd, int x, int y, int w, int h, const char **items, int count);
void widget_dropdown_draw(widget_context_t *ctx, widget_dropdown_t *dd);
bool widget_dropdown_handle_mouse(widget_dropdown_t *dd, int mx, int my, bool mouse_clicked, void *user_data);
// --- Checkbox / Radio ---
typedef struct {
int x, y, w, h;
const char *text;
bool checked;
bool is_radio;
void (*on_toggle)(void *user_data, bool new_state);
} widget_checkbox_t;
void widget_checkbox_init(widget_checkbox_t *cb, int x, int y, int w, int h, const char *text, bool is_radio);
void widget_checkbox_draw(widget_context_t *ctx, widget_checkbox_t *cb);
bool widget_checkbox_handle_mouse(widget_checkbox_t *cb, int mx, int my, bool mouse_clicked, void *user_data);
#endif