swk

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

commit 29d3b902c839e1690d05e9bd2757315178dbfa28
parent b5353a87ec56edadf1eb4a9f88812d7ce902d1d1
Author: pancake <pancake@nopcode.org>
Date:   Wed, 21 Apr 2010 14:21:54 +0200

default colors from dwm by default
use bold font in sdl backend
do not render empty text
fix mouse over widget focus
Diffstat:
gi_sdl.c | 28++++++++++++++--------------
swk.c | 38++++++++++++++++++++------------------
swk.h | 5+++--
3 files changed, 37 insertions(+), 34 deletions(-)

diff --git a/gi_sdl.c b/gi_sdl.c @@ -2,12 +2,11 @@ #include <SDL/SDL_ttf.h> #include "swk.h" -#define HICOLOR 0xa0,0x00,0x00 -#define FGCOLOR 0xa0,0xa0,0xa0 +#define HICOLOR 0x00,0x66,0xff +#define FGCOLOR 0xff,0xff,0xff #define BGCOLOR 0x00,0x00,0x00 -#define TFCOLOR 0xff,0xff,0xff +#define TFCOLOR 0xcc,0xcc,0xcc #define FONTNAME "Inconsolata.otf" -//#define FONTSIZE 16 #define FONTSIZE 14 #define FS FONTSIZE #define BPP 32 @@ -63,7 +62,7 @@ swk_gi_init(SwkWindow *w) { if (font == NULL) { fprintf(stderr, "Cannot open font '%s'\n", FONTNAME); return 0; - } //else TTF_SetFontStyle(font, TTF_STYLE_BOLD); + } else TTF_SetFontStyle(font, TTF_STYLE_BOLD); return 1; } @@ -98,9 +97,8 @@ swk_gi_event(int dowait) { static SwkEvent ev; SwkEvent *ret = NULL; - if(has_event) { - event = lastev; - } else has_event = SDL_WaitEvent(&event); + if(has_event) event = lastev; + else has_event = SDL_WaitEvent(&event); if (has_event); switch(event.type) { @@ -197,10 +195,12 @@ swk_gi_rect(int x, int y, int w, int h, int color) { void swk_gi_text(int x, int y, const char *text) { - SDL_Surface *ts = TTF_RenderText_Solid(font, text, fontcolor); - if (ts) { - SDL_Rect to = { x*FS, y*FS, ts->w, ts->h }; - SDL_BlitSurface(ts, NULL, screen, &to); - SDL_FreeSurface(ts); - } else fprintf(stderr, "Cannot render string (%s)\n", text); + if (*text) { + SDL_Surface *ts = TTF_RenderText_Solid(font, text, fontcolor); + if (ts) { + SDL_Rect to = { x*FS, y*FS, ts->w, ts->h }; + SDL_BlitSurface(ts, NULL, screen, &to); + SDL_FreeSurface(ts); + } else fprintf(stderr, "Cannot render string (%s)\n", text); + } } diff --git a/swk.c b/swk.c @@ -11,11 +11,11 @@ int swk_init(SwkWindow* window) { w = window; w->box = w->boxes; - if (w->r.w == 0 || w->r.h == 0) { + if(w->r.w == 0 || w->r.h == 0) { w->r.w = 640; w->r.h = 480; } - if (swk_gi_init(w)) { + if(swk_gi_init(w)) { running = 1; swk_update(); } @@ -25,9 +25,10 @@ swk_init(SwkWindow* window) { void swk_update() { SwkEvent ev = { .type = EExpose }; - if (swk_gi_update(w)) { + if(swk_gi_update(w)) { SwkBox *b = w->boxes; swk_fit(); + swk_gi_clear(); for(;b->cb; b++) { ev.box = b; b->cb(&ev); @@ -45,7 +46,7 @@ void swk_loop() { SwkEvent *e; do { - if ((e = swk_event(1))) + if((e = swk_event(1))) swk_event_handle(e); } while (!e || e->type != EQuit); } @@ -56,7 +57,7 @@ static void swk_fit_row(SwkBox *a, SwkBox *b, int y) { count = 0; for(btmp=a; btmp<b; btmp++) count++; - if (count) { + if(count) { int winc = w->r.w / count; for(btmp=a; btmp<b; btmp++) { btmp->r.x = x; @@ -85,7 +86,7 @@ swk_fit() { SwkEvent * swk_event(int dowait) { static SwkEvent ev; - if (running) + if(running) return swk_gi_event(); ev.type = EQuit; return &ev; @@ -96,25 +97,26 @@ swk_event_handle(SwkEvent *e) { SwkBox *b; switch(e->type) { case EKey: - if (e->data.key.keycode == 9) { // TAB - if (e->data.key.modmask) + // TODO: handle ^Y and ^P to copypasta box->text + if(e->data.key.keycode == 9) { // TAB + if(e->data.key.modmask) swk_focus_prev(); else swk_focus_next(); swk_update(); } else - if (e->data.key.keycode == 13) { // ENTER + if(e->data.key.keycode == 13) { // ENTER e->box = w->box; e->type = EClick; } // send key to focused box e->box = w->box; - if (w->box) + if(w->box) w->box->cb(e); swk_update(); break; case EMotion: for(b=w->boxes; b->cb; b++) { - if (SWK_HIT(b->r, e->data.click.point)) { + if(SWK_HIT(b->r, e->data.motion)) { w->box = e->box = b; b->cb(e); swk_update(); @@ -124,7 +126,7 @@ swk_event_handle(SwkEvent *e) { break; case EClick: for(b=w->boxes; b->cb; b++) { - if (SWK_HIT(b->r, e->data.click.point)) { + if(SWK_HIT(b->r, e->data.click.point)) { e->box = w->box = b; e->box->cb(e); swk_update(); @@ -145,7 +147,7 @@ swk_event_handle(SwkEvent *e) { void swk_focus_next() { w->box++; - if (w->box->cb == NULL) + if(w->box->cb == NULL) w->box = w->boxes; while(w->box->cb == swk_filler) w->box++; @@ -155,7 +157,7 @@ swk_focus_next() { void swk_focus_prev() { - if (w->box == w->boxes) { + if(w->box == w->boxes) { while(w->box->cb) w->box++; w->box--; @@ -163,7 +165,7 @@ swk_focus_prev() { w->box--; while (w->box->cb == swk_filler) { w->box--; - if (w->box < w->boxes) { + if(w->box < w->boxes) { w->box = w->boxes; swk_focus_prev(); return; @@ -179,7 +181,7 @@ swk_label(SwkEvent *e) { switch(e->type) { case EExpose: r = e->box->r; - if (w->box == e->box) + if(w->box == e->box) swk_gi_line(r.x, r.y+1, r.w, 0, ColorHI); swk_gi_text(r.x, r.y, e->box->text); break; @@ -195,7 +197,7 @@ swk_entry(SwkEvent *e) { switch(e->type) { case EKey: key = e->data.key.keycode; - if (key==8) { + if(key == 8) { ptr = strdup (e->box->text); if(e->box->data) free(e->box->text); @@ -222,7 +224,7 @@ swk_button(SwkEvent *e) { switch(e->type) { case EExpose: r = e->box->r; - if (w->box == e->box) + if(w->box == e->box) swk_gi_rect(r.x, r.y, r.w, r.h, ColorHI); else swk_gi_rect(r.x, r.y, r.w, r.h, ColorFG); swk_gi_text(r.x+1, r.y, e->box->text); diff --git a/swk.h b/swk.h @@ -61,14 +61,12 @@ typedef struct { } SwkWindow; int swk_init(SwkWindow *w); -int swk_gi_update(SwkWindow *w); void swk_update(); void swk_exit(); void swk_fit(); void swk_loop(); SwkEvent * swk_event(); void swk_event_handle(SwkEvent *e); -int swk_gi_has_event(); void swk_focus_next(); void swk_focus_prev(); @@ -83,7 +81,10 @@ void swk_filler(SwkEvent *e); int swk_gi_init(SwkWindow *w); void swk_gi_exit(); SwkEvent * swk_gi_event(); +int swk_gi_update(SwkWindow *w); +int swk_gi_has_event(); +void swk_gi_clear(); void swk_gi_flip(); void swk_gi_line(int x, int y, int w, int h, int color);