diff --git a/src/toolkit.c b/src/toolkit.c index dd44a1d..c65c848 100644 --- a/src/toolkit.c +++ b/src/toolkit.c @@ -120,10 +120,12 @@ static glColour* toolkit_colDark = &cGrey30; /* Extern. */ extern void main_loop(void); /* lephisto.c */ /* Static. */ +/* Widgets. */ static Widget* window_newWidget(Window* w); static void widget_cleanup(Widget* widget); static Window* window_wget(const unsigned int wid); static Widget* window_getwgt(const unsigned int wid, char* name); +static void toolkit_setPos(Window* wdw, Widget* wgt, int x, int y); /* Input. */ static int toolkit_inputInput(Uint8 type, Widget* inp, SDLKey key); static void toolkit_mouseEvent(SDL_Event* event); @@ -160,6 +162,14 @@ static void dialogue_inputClose(char* str); static int loop_done; static int toolkit_loop(void); +/* Set the internal widget position. */ +static void toolkit_setPos(Window* wdw, Widget* wgt, int x, int y) { + 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; +} + /* Add a button that when pressed will trigger call, passing it's name as the */ /* only parameter. */ void window_addButton(const unsigned int wid, const int x, const int y, @@ -169,20 +179,19 @@ void window_addButton(const unsigned int wid, const int x, const int y, Window* wdw = window_wget(wid); Widget* wgt = window_newWidget(wdw); - /* Specific. */ + /* Generic. */ wgt->type = WIDGET_BUTTON; wgt->name = strdup(name); + + /* Specific. */ wgt->dat.btn.display = strdup(display); wgt->dat.btn.disabled = 0; /* Initially enabled. */ + wgt->dat.btn.fptr = call; - /* Set the properties. */ + /* 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; - wgt->dat.btn.fptr = call; + toolkit_setPos(wdw, wgt, x, y); if(wdw->focus == -1) /* Init the focus. */ toolkit_nextFocus(); @@ -196,23 +205,25 @@ void window_addText(const unsigned int wid, const int x, const int y, Window* wdw = window_wget(wid); Widget* wgt = window_newWidget(wdw); + /* Generic. */ wgt->type = WIDGET_TEXT; - wgt->name = strdup(name); /* Display the widgets name. */ + wgt->name = strdup(name); - /* Set the properties. */ - wgt->w = (double) w; - wgt->h = (double) h; + /* Specific. */ + if(font == NULL) wgt->dat.txt.font = &gl_defFont; + else wgt->dat.txt.font = font; if(font == NULL) wgt->dat.txt.font = &gl_defFont; else wgt->dat.txt.font = font; - if(x < 0) wgt->x = wdw->w - wgt->w + x; - else wgt->x = (double)x; - if(y < 0) wgt->y = wdw->h + y - h; - else wgt->y = (double) y; if(colour == NULL) wgt->dat.txt.colour = &cBlack; else wgt->dat.txt.colour = colour; wgt->dat.txt.centered = centered; if(string) wgt->dat.txt.text = strdup(string); else wgt->dat.txt.text = NULL; + + /* Position/Size. */ + wgt->w = (double)w; + wgt->h = (double)h; + toolkit_setPos(wdw, wgt, x, y); } /* Add a graphic to the window. */ @@ -222,20 +233,19 @@ void window_addImage(const unsigned int wid, const int x, const int y, Window* wdw = window_wget(wid); Widget* wgt = window_newWidget(wdw); + /* Generic. */ wgt->type = WIDGET_IMAGE; wgt->name = strdup(name); - /* Set the properties. */ + /* Specific. */ wgt->dat.img.image = image; wgt->dat.img.border = border; wgt->dat.img.colour = NULL; /* Normal colour. */ + /* Position/Size. */ wgt->w = (image == NULL) ? 0 : wgt->dat.img.image->sw; wgt->h = (image == NULL) ? 0 : wgt->dat.img.image->sh; - 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; + toolkit_setPos(wdw, wgt, x, y); } void window_addList(const unsigned int wid, const int x, const int y, @@ -245,21 +255,21 @@ void window_addList(const unsigned int wid, const int x, const int y, Window *wdw = window_wget(wid); Widget* wgt = window_newWidget(wdw); + /* Generic. */ wgt->type = WIDGET_LIST; wgt->name = strdup(name); + /* Specific. */ wgt->dat.lst.options = items; wgt->dat.lst.noptions = nitems; wgt->dat.lst.selected = defitem; /* -1 would be none. */ wgt->dat.lst.pos = 0; wgt->dat.lst.fptr = call; + /* Position/Size. */ wgt->w = (double) w; wgt->h = (double) h - ((h % (gl_defFont.h+2)) + 2); - 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; + toolkit_setPos(wdw, wgt, x, y); if(wdw->focus == -1) /* Initialize the focus. */ @@ -272,18 +282,18 @@ void window_addRect(const unsigned int wid, const int x, const int y, Window* wdw = window_wget(wid); Widget* wgt = window_newWidget(wdw); + /* Generic. */ wgt->type = WIDGET_RECT; wgt->name = strdup(name); + /* Specific. */ wgt->dat.rct.colour = colour; wgt->dat.rct.border = border; + /* 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; + toolkit_setPos(wdw, wgt, x, y); } void window_addCust(const unsigned int wid, const int x, const int y, @@ -306,10 +316,7 @@ void window_addCust(const unsigned int wid, const int x, const int y, /* 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; + toolkit_setPos(wdw, wgt, x, y); } /* Add an input widget. */ @@ -336,10 +343,7 @@ void window_addInput(const unsigned int wid, /* 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; + toolkit_setPos(wdw, wgt, x, y); } /* Return pointer to newly allocated widget. */ @@ -383,6 +387,25 @@ void window_modifyText(const unsigned int wid, char* name, char* newstring) { wgt->dat.txt.text = (newstring) ? strdup(newstring) : NULL; } +/* Get a widgets position. */ +void window_posWidget(const unsigned int wid, + char* name, int* x, int* y) { + Widget* wgt = window_getwgt(wid, name); + + (*x) = wgt->x; + (*y) = wgt->y; +} + +/* Move a widget. */ +void window_moveWidget(const unsigned int wid, + char* name, int x, int y) { + + Window* wdw = window_wget(wid); + Widget* wgt = window_getwgt(wid, name); + + toolkit_setPos(wdw, wgt, x, y); +} + /* Disable a button. */ void window_disableButton(const unsigned int wid, char* name) { Widget* wgt = window_getwgt(wid, name); diff --git a/src/toolkit.h b/src/toolkit.h index 1e32092..09957b5 100644 --- a/src/toolkit.h +++ b/src/toolkit.h @@ -63,13 +63,19 @@ void window_enableButton(const unsigned int wid, char* name); void window_modifyImage(const unsigned int wid, char* name, glTexture* image); void window_imgColour(const unsigned int wid, char* name, glColour* colour); -/* Get the window by name. */ +/* Get. */ +/* Generic. */ int window_exists(const char* wdwname); unsigned int window_get(const char* wdwname); +char* window_getInput(const unsigned int wid, char* name); +void window_posWidget(const unsigned int wid, + char* name, int* x, int* y); +void window_moveWidget(const unsigned int wid, + char* name, int x, int y); +/* Specific. */ char* toolkit_getList(const unsigned int wid, char* name); int toolkit_getListPos(const unsigned int wid, char* name); glTexture* window_getImage(const unsigned int wid, char* name); -char* window_getInput(const unsigned int wid, char* name); /* Destroy window. */ void window_destroy(const unsigned int wid);