#include #include #include #include "wElement.h" Widget *wElement(SDL_Surface *icon, char *name, int size) { Widget *w = malloc(sizeof(Widget)); if (!w) return NULL; w->type = WIDGET_CUSTOM; w->displayBounds = -1; w->Hexpansive = 1; w->Vexpansive = 0; w->parent = NULL; w->construct = NULL; w->background = NULL; w->isDynamic = 1; w->isLayout = 0; w->callBack = NULL; w->freeArgsType = WSURFACE; w->bounds.w = icon->w + 7*strlen(name) + 40; w->bounds.h = 22; w->bounds.x = (320-w->bounds.w)/2; w->bounds.y = (240-w->bounds.h)/2; w->draw = DrawElement; w->activate = ActivateElement; w->close = CloseElement; w->add = NULL; w->customArgs = NULL; w->freeCustomArgs = NULL; w->args = malloc(sizeof(ElementArgs)); if (!w->args) { free(w); return NULL; } ElementArgs *args = w->args; args->icon = icon; args->name = name; args->size = size; return w; } void DrawElement(Widget *w) { SDL_Surface *scr = w->construct->scr; wTHEME *theme = w->construct->theme; ElementArgs *args = w->args; int x = w->bounds.x + 3, y = w->bounds.y + 2; char size[16]; sprintf(size, "%i", args->size); char *p = strstr(args->name, ".tns"); if (p) *p = 0; // on dessine DrawSurface(args->icon, NULL, scr, &((SDL_Rect) {x, y, args->icon->w, args->icon->h})); DrawClippedStr(scr, theme->font, x+29, y+5, args->name); if (args->size >= 0) DrawClippedStr(scr, theme->font, x+w->bounds.w - 6 - nSDL_GetStringWidth(theme->font, size), y+5, size); if (p) *p = '.'; } int ActivateElement(Widget *w) { SDL_Surface *scr = w->construct->scr; wTHEME *theme = w->construct->theme; int ok = ACTION_CONTINUE; while (!K_ESC()) { // on dessine la bordure roundedBoxColor(scr, w->bounds.x, w->bounds.y, w->bounds.x+w->bounds.w-1, w->bounds.y+w->bounds.h-1, 3, theme->color3); roundedRectangleColor(scr, w->bounds.x, w->bounds.y, w->bounds.x+w->bounds.w-1, w->bounds.y+w->bounds.h-1, 3, theme->color2); DrawElement(w); // on dessine les infos SDL_Flip(scr); wait_key_pressed(); if (K_CLICK() || K_ENTER()) { if ((ok=wExecCallback(w, SIGNAL_CLICK)) != ACTION_CONTINUE) return ok; SDL_Flip(scr); } else if ((ok=wExecCallback(w, SIGNAL_KEY)) != ACTION_CONTINUE) return ok; if (K_UP() || K_DOWN() || K_TAB() || K_LEFT() || K_RIGHT() || K_SCRATCHPAD() || K_ESC()) return ACTION_CONTINUE; SDL_Flip(scr); } return ACTION_CONTINUE; } void CloseElement(Widget *w) { if (!w) return; ElementArgs *args = w->args; if (args) free(args->name); }