From ef2bfd205250f5e8a7825aedbd41baf970090293 Mon Sep 17 00:00:00 2001 From: Allanis Date: Fri, 8 Mar 2013 13:39:30 +0000 Subject: [PATCH] [Add] Possibility for custom widgets. --- src/toolkit.c | 32 +++++++++++++++++++++++++++++++- src/toolkit.h | 5 +++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/toolkit.c b/src/toolkit.c index 41223aa..072cd61 100644 --- a/src/toolkit.c +++ b/src/toolkit.c @@ -13,7 +13,8 @@ typedef enum WidgetType_ { WIDGET_TEXT, WIDGET_IMAGE, WIDGET_LIST, - WIDGET_RECT + WIDGET_RECT, + WIDGET_CUST } WidgetType; typedef enum WidgetStatus_ { @@ -61,6 +62,10 @@ typedef struct Widget_ { glColour* colour; // Background colour. int border; // Border. } rct; + // Widget cust. + struct { + void(*render) (double bx, double by); + } cst; } dat; } Widget; @@ -234,6 +239,28 @@ void window_addRect(const unsigned int wid, const int x, const int y, const int else wgt->y = (double)y; } +void window_addCust(const unsigned int wid, const int x, const int y, + const int w, const int h, char* name, void(*render) (double x, double y)) { + + Window* wdw = window_wget(wid); + Widget* wgt = window_newWidget(wdw); + + // Generic. + wgt->type = WIDGET_CUST; + wgt->name = strdup(name); + + // Specific. + wgt->dat.cst.render = render; + + // Position/size. + wgt->w = (double)w; + wgt->h = (double)h; + if(x < 0) wgt->x = wdw->w - wgt->w + x; + else wgt->x = (double) x; + if(y < 0) wgt->y = wdw->h - wgt->h + y; + else wgt->y = (double) y; +} + // Return pointer to newly allocated widget. static Widget* window_newWidget(Window* w) { Widget* wgt = NULL; @@ -630,6 +657,9 @@ static void window_render(Window* w) { case WIDGET_RECT: toolkit_renderRect(&w->widgets[i], x, y); break; + case WIDGET_CUST: + (*w->widgets[i].dat.cst.render)(x, y); + break; } } // Focus widget. diff --git a/src/toolkit.h b/src/toolkit.h index e748c75..f5047a5 100644 --- a/src/toolkit.h +++ b/src/toolkit.h @@ -30,6 +30,11 @@ void window_addRect(const unsigned int wid, const int w, const int h, // size. char* name, glColour* colour, int border); // Properties. +void window_addCust(const unsigned int wid, + const int x, const int y, // Position. + const int w, const int h, // Size. + char* name, void(*render) (double x, double y)); + // Popups and alerts. void toolkit_alert(const char* fmt, ...);