swk

static widget kit
git clone git://git.suckless.org/swk
Log | Files | Refs | README | LICENSE

commit ab0626c9a9a78fe128fbbdaa1216227b9990a004
parent a0e35a79ba83c7146791f474aeddb2ee0abf9878
Author: pancake <pancake@nopcode.org>
Date:   Mon, 26 Apr 2010 22:36:47 +0200

define default window width/height in config.h
implemented vertical filler widget
option widget supports sharing widget root
visual fixes in option widget
Diffstat:
config.def.h | 2++
gi_sdl.c | 36+++++++++++++-----------------------
swk.c | 50+++++++++++++++++++++++++++++++++++---------------
swk.h | 1+
test.c | 14++++++++++----
5 files changed, 61 insertions(+), 42 deletions(-)

diff --git a/config.def.h b/config.def.h @@ -2,6 +2,8 @@ /* appearance */ #define FONTSIZE 14 +#define WINWIDTH 640 +#define WINHEIGHT 480 // SDL #define SWK_COLOR(r,g,b) 0x##r,0x##g,0x##b // X11 diff --git a/gi_sdl.c b/gi_sdl.c @@ -4,16 +4,6 @@ #include "swk.h" #include "config.h" -//#define SWK_COLOR(r,g,b) 0x##r,0x##g,0x##b - -#if 0 -#define HICOLOR 0x00,0x66,0xff -#define FGCOLOR 0xff,0xff,0xff -#define BGCOLOR 0x00,0x00,0x00 -#define TFCOLOR 0xcc,0xcc,0xcc -#endif - -/* --- */ #define FONTNAME "Inconsolata.otf" #define FS FONTSIZE #define BPP 32 @@ -23,6 +13,9 @@ static Uint32 pal[ColorLast]; static SDL_Color fontcolor = { TFCOLOR }; static SDL_Surface *screen = NULL; static TTF_Font *font = NULL; +/* FIXME: put ugly statics into void *aux of SwkWindow */ +static int has_event = 0; +static SDL_Event lastev = { .type=-1 }; static void putpixel(int x, int y, Uint32 pixel) { int bpp = screen->format->BytesPerPixel; @@ -33,15 +26,15 @@ static void putpixel(int x, int y, Uint32 pixel) { #elif BPP == 16 *(Uint16 *)p = pixel; #elif BPP == 24 - #if SDL_BYTEORDER == SDL_BIG_ENDIAN - p[0] = (pixel >> 16) & 0xff; - p[1] = (pixel >> 8) & 0xff; - p[2] = pixel & 0xff; - #else - p[0] = pixel & 0xff; - p[1] = (pixel >> 8) & 0xff; - p[2] = (pixel >> 16) & 0xff; - #endif +# if SDL_BYTEORDER == SDL_BIG_ENDIAN + p[0] = (pixel >> 16) & 0xff; + p[1] = (pixel >> 8) & 0xff; + p[2] = pixel & 0xff; +# else + p[0] = pixel & 0xff; + p[1] = (pixel >> 8) & 0xff; + p[2] = (pixel >> 16) & 0xff; +# endif #elif BPP == 32 *(Uint32 *)p = pixel; #endif @@ -86,10 +79,6 @@ swk_gi_exit() { SDL_Quit(); } -/* FIXME: put ugly statics into void *aux of SwkWindow */ -static int has_event = 0; -static SDL_Event lastev = {.type=-1}; - int swk_gi_has_event(SwkWindow *w) { if(!has_event) @@ -108,6 +97,7 @@ swk_gi_event(SwkWindow *w, int dowait) { if(has_event); switch(event.type) { default: ret = NULL; break; + case SDL_ACTIVEEVENT: case SDL_VIDEORESIZE: fprintf(stderr, "resize %d %d\n", event.resize.w, event.resize.h); SDL_SetVideoMode(event.resize.w, event.resize.h, BPP, SDLFLAGS); diff --git a/swk.c b/swk.c @@ -14,8 +14,8 @@ swk_init(SwkWindow *w) { if (w->box == NULL) swk_focus_first(w); if(w->r.w == 0 || w->r.h == 0) { - w->r.w = 640; - w->r.h = 480; + w->r.w = WINWIDTH; + w->r.h = WINHEIGHT; } if(swk_gi_init(w)) { running = 1; @@ -66,20 +66,42 @@ static void swk_fit_row(SwkWindow *w, SwkBox *a, SwkBox *b, int y) { btmp->r.y = y; btmp->r.w = winc; btmp->r.h = 1; - x+=winc; + x += winc; } } } +static int +countrows(SwkBox *b) { + int row = 0; + for(; b->cb; b++) { + if(b->r.w==-1&&b->r.h==-1) + row += (int)(size_t)b->data; + else row += b->r.h; + } + return row; +} + void swk_fit(SwkWindow *w) { - int y = 0; + int x, y = 0; SwkBox *b, *b2; for(b=b2=w->boxes; b->cb; b++) { if(b->r.w==-1 && b->r.h==-1) { - swk_fit_row(w, b2, b, y); - y += (int)(size_t)b->data; - b2 = b+1; + x = (int)(size_t)b->data; + if (x>0) { + swk_fit_row(w, b2, b, y); + y += (int)(size_t)b->data; + b2 = b+1; + } else { + swk_fit_row(w, b2, b, y); + b2 = b+1; + y += (w->r.h-countrows(b2)); + if (y<0) { + fprintf(stderr, "overflow: must scroll\n"); + y=0; + } + } } } swk_fit_row(w, b2, b, y); @@ -298,22 +320,20 @@ swk_filler(SwkEvent *e) { void swk_option(SwkEvent *e) { + SwkBox **b = (SwkBox**)e->box->data; Rect r; switch(e->type) { case EClick: - if(e->box==e->box->data) - e->box->data = NULL; - else e->box->data = e->box; + *b = (e->box==*b)?NULL:e->box; break; case EExpose: r = e->box->r; - r.w = r.h = 1; - if(e->box==e->box->data) - swk_gi_fill(r, ColorFG); - else swk_gi_rect(r, ColorFG); if(e->win->box == e->box) swk_gi_line(r.x, r.y+1, r.w, 0, ColorHI); - r.x+=2; + r.w = r.h = 1; + if(e->box==*b) swk_gi_fill(r, ColorFG); + else swk_gi_rect(r, ColorFG); + r.x += 2; swk_gi_text(r, e->box->text); break; default: diff --git a/swk.h b/swk.h @@ -1,6 +1,7 @@ /* See LICENSE file for copyright and license details. */ #define SWK_BOX_NEWLINE(x) { .data=(void*)(size_t)x, .r.w=-1, .r.h=-1, .cb = swk_filler } +#define SWK_BOX_VFILL(x) { .data=(void*)(size_t)x, .r.w=-1, .r.h=-2, .cb = swk_filler } #define SWK_HIT(r,p) (p.x>=r.x && p.x<(r.x+r.w) && p.y>=r.y && p.y<(r.y+r.h)) typedef enum { EVoid, EClick, EMotion, EKey, EExpose, EQuit, ELast } SwkEventType; diff --git a/test.c b/test.c @@ -4,11 +4,15 @@ static int count = 3; static char text[64]; static SwkBox helloworld[]; +static SwkBox *opt = NULL; static void mybutton(SwkEvent *e) { if (e->type == EClick) { sprintf(text, "Do it again %d times\n", count); helloworld[0].text = text; + if (opt == NULL) + printf("Option: none\n"); + else printf("Option: %s\n", opt->text); if(count-- == 0) swk_exit(); } @@ -25,13 +29,14 @@ static SwkBox helloworld[] = { { .cb=swk_label, .text="Password:", }, { .cb=swk_password, .text="1234", }, { .cb=swk_filler, }, - SWK_BOX_NEWLINE(2), + SWK_BOX_NEWLINE(-1), { .cb=mybutton, .text="yes" }, { .cb=mybutton, .text="no" }, { .cb=swk_filler, }, + SWK_BOX_NEWLINE(2), + { .cb=swk_option, .text="remember values", .data=&opt }, SWK_BOX_NEWLINE(1), - { .cb=swk_option, .text="remember values", }, - { .cb=swk_option, .text="pasta barata", }, + { .cb=swk_option, .text="pasta barata", .data=&opt }, SWK_BOX_NEWLINE(5), { .cb=swk_label, .text="--swktest", }, { .cb=NULL } @@ -42,7 +47,8 @@ main() { SwkWindow w = { .title="Hello World", .boxes=helloworld, - .box=helloworld+10 + .box=helloworld+10, + //.r = { 0, 0, 320, 240 }, /* // TODO: application workflow .ok=cb