cepheid

An Aurora 4X clone
Log | Files | Refs | README

commit 574c3f5315ca86e89cd4a6f572194b2e18ea7cc2
parent 85f05e0104c1114103ea9830f454afa37e152f9f
Author: hhvn <dev@hhvn.uk>
Date:   Thu, 24 Nov 2022 00:23:43 +0000

Unified Guicallback

Diffstat:
Msrc/gui.c | 6+++---
Msrc/struct.h | 40+++++++++++++++++++++-------------------
Msrc/views/smenu.c | 55++++++++++++++++++++++++++-----------------------------
3 files changed, 50 insertions(+), 51 deletions(-)

diff --git a/src/gui.c b/src/gui.c @@ -260,7 +260,7 @@ gui_click_button(MouseButton button, Geom *geom, void *elem) { if (b->submit && b->submit->onenter) gui_enter_input(b->submit); else if (b->func) - b->func(b->arg); + b->func(GUI_BUTTON, b); } } @@ -363,7 +363,7 @@ gui_click_input(MouseButton button, Geom *geom, void *elem) { static void gui_enter_input(Input *in) { wcstombs(in->str, in->wstr, INPUT_MAX); - if (in->onenter(in)) + if (in->onenter(GUI_INPUT, in)) edittrunc(in->wstr, &in->len, &in->cur); } @@ -476,7 +476,7 @@ gui_click_treeview(MouseButton button, Geom *geom, void *elem) { if (p->type & tv->selmask && (!(p->type & tv->colmask) || mouse.x > geom->x + PAD * (depth + 2))) { if (gui_double_click() && tv->dclick) - tv->dclick(tv); + tv->dclick(GUI_TREEVIEW, tv); else tv->sel = p; } diff --git a/src/struct.h b/src/struct.h @@ -24,7 +24,6 @@ typedef void (*Treesetter)(char *dir, char *group, char *name, int depth, Tree * typedef void (*Treefree)(Tree *t); typedef int (*Treefilter)(Tree *t, void *data); typedef void (*Treeprinter)(int x, int y, Treeview *tv, Tree *t); -typedef void (*Treedclick)(Treeview *tv); typedef int (*Treecompar)(Tree *a, Tree *b, void *data); @@ -162,6 +161,24 @@ typedef struct { int scroll; } Mouse; +/* gui.c */ +enum GuiElements { + GUI_TAB, + GUI_CHECKBOX, + GUI_BUTTON, + GUI_INPUT, + GUI_DROPDOWN, + GUI_TREEVIEW, + GUI_ELEMS, +}; + +typedef struct { + enum GuiElements type; + void *p; +} Focus; + +typedef int (*Guicallback)(int type, void *elem); + #define TABS_MAX 16 typedef struct { int n; @@ -187,14 +204,14 @@ struct Input { char *placeholder; int len; int cur; - int (*onenter)(Input *); /* return positive to clear */ + Guicallback onenter; /* return non-zero to clear */ int (*accept)(wchar_t); }; typedef struct { int enabled; char *label; - void (*func)(int); + Guicallback func; int arg; Input *submit; /* run the onenter in an Input instead */ } Button; @@ -220,27 +237,12 @@ struct Treeview { void *fdata; /* data to pass to filter() */ Treefilter filter; /* hide nodes when 0 is returned */ Treeprinter print; /* prints data related to the node */ - Treedclick dclick; /* runs on double click of selected node */ + Guicallback dclick; /* runs on double click of selected node */ /* internal */ Geom rect; Pane pane; }; -enum GuiElements { - GUI_TAB, - GUI_CHECKBOX, - GUI_BUTTON, - GUI_INPUT, - GUI_DROPDOWN, - GUI_TREEVIEW, - GUI_ELEMS, -}; - -typedef struct { - enum GuiElements type; - void *p; -} Focus; - enum UiViews { VIEW_SMENU, VIEW_MAIN, diff --git a/src/views/smenu.c b/src/views/smenu.c @@ -16,16 +16,14 @@ #define BUTTON_W 50 static void savecheck(char *msg, void (*action)(void)); -static void savecheck_callback(int arg); -static void buttonhandler(int arg); +static int savecheck_callback(int type, void *elem); +static int buttonhandler(int type, void *elem); static void newhandler(void); -static int newhandler_actual(Input *in); -static void newhandler_back(int arg); +static int newhandler_actual(int type, void *elem); +static int newhandler_back(int type, void *elem); static void quithandler(void); static void loadhandler(void); -static void loadhandler_actual(void); -static void loadhandler_click(Treeview *tv); -static void loadhandler_button(int arg); +static int loadhandler_actual(int type, void *elem); static void loadprinter(int x, int y, Treeview *tv, Tree *t); static void loadadd(char *dir, time_t mod); static void loadinit(char *sdir); @@ -76,9 +74,9 @@ View_smenu view_smenu = { .colmask = 0x00, .filter = NULL, .print = loadprinter, - .dclick = loadhandler_click, + .dclick = loadhandler_actual, }, - .load = {0, "Load", loadhandler_button, 0 }, + .load = {0, "Load", loadhandler_actual, 0 }, } }; @@ -92,18 +90,25 @@ savecheck(char *msg, void (*action)(void)) { v->save.func = action; } -static void -savecheck_callback(int arg) { +static int +savecheck_callback(int type, void *elem) { + Button *b = elem; + int arg = b->arg; + v->save.check = 0; if (arg) save_write(); if (arg != -1) v->save.func(); + return 0; } -static void -buttonhandler(int arg) { +static int +buttonhandler(int type, void *elem) { + Button *b = elem; + int arg = b->arg; + switch (arg) { case SMENU_NEW: if (save && save_changed()) @@ -132,6 +137,7 @@ buttonhandler(int arg) { checkbeforequit(); break; } + return 0; } static void @@ -140,7 +146,7 @@ newhandler(void) { } static int -newhandler_actual(Input *in) { +newhandler_actual(int type, void *elem) { save_create(v->new.name.str); save_read(v->new.name.str); loadadd(v->new.name.str, time(NULL)); @@ -149,10 +155,11 @@ newhandler_actual(Input *in) { return 1; } -static void -newhandler_back(int arg) { +static int +newhandler_back(int type, void *elem) { gui_input_clear(&v->new.name); v->new.disp = 0; + return 0; } static void @@ -167,25 +174,15 @@ loadhandler(void) { /* v->b[SMENU_CONT].enabled = 1; */ } -static void -loadhandler_actual(void) { +static int +loadhandler_actual(int type, void *elem) { struct Loadable *l; l = v->load.savelist.sel->data; save_read(l->name); view_tabs.sel = VIEW_MAIN; v->load.disp = 0; - return; -} - -static void -loadhandler_click(Treeview *tv) { - loadhandler_actual(); -} - -static void -loadhandler_button(int arg) { - loadhandler_actual(); + return 0; } static void