swk

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

commit 8095ebd990e8c44177d55a1ddf0f14abfb66a432
parent 654ccc02e5836d9d292146d9e50f022ceecad261
Author: pancake <pancake@nopcode.org>
Date:   Sat, 15 May 2010 13:24:31 +0200

Single global SwkWindow
w->running is moved outside window
swk_init is now named swk_use()
Diffstat:
TODO | 34++++++++++++++++++++++------------
swk.c | 67+++++++++++++++++++++++++++++++++++--------------------------------
swk.h | 33++++++++++++++++-----------------
t/test.c | 4++--
t/ui.c | 5++---
5 files changed, 77 insertions(+), 66 deletions(-)

diff --git a/TODO b/TODO @@ -1,15 +1,25 @@ TODO ==== + * receive fine-grained x,y in Point? for sketch or imaging stuff is important + * support for clipboard (implemented in gi_ backend) + * simple way to define callback for buttons instead of reimplementing widget (?) + * full text editing widget (multiline, cursor support, text format, ...) + * scroll on focus (if focused widget is not in screen, just scroll there..) + * mouse bindings in config.h (which mouse bindings we need? wheel to scroll?) + * allow widgets to force its own size (height, width) + * add support to resize images (scaling) + * only focus buttons and entries (input widgets) + * add function to click button 1, 2, 3, ... // clicking from code.. + * allow to load images from memory instead of files + * default image path (?) + * transitions (to change from one window to another) -* receive fine-grained x,y in Point? for sketch or imaging stuff is important -* support for clipboard (implemented in gi_ backend) -* simple way to define callback for buttons instead of reimplementing widget (?) -* full text editing widget (multiline, cursor support, text format, ...) -* scroll on focus (if focused widget is not in screen, just scroll there..) -* mouse bindings in config.h (which mouse bindings we need? wheel to scroll?) -* allow widgets to force its own size (height, width) -* add support to resize images (scaling) -* only focus buttons and entries (input widgets) -* add function to click button 1, 2, 3, ... // clicking from code.. -* allow to load images from memory instead of files -* default image path (?) +USABILITY GUIDELINE +=================== + * useful + * usable + * transparent + * either discoverable or well-documented + * reliable + * as simple as possible, and no simpler + * customizable diff --git a/swk.c b/swk.c @@ -5,24 +5,27 @@ #include "swk.h" #include "config.h" +static SwkWindow *w = NULL; +static int running = 0; + int -swk_init(SwkWindow *w) { - w->_e.win = w; - if(w->box == NULL) +swk_use(SwkWindow *win) { + w = win = win->_e.win = win; + if(win->box == NULL) swk_focus_first(w); if(w->r.w == 0 || w->r.h == 0) { w->r.w = WINWIDTH; w->r.h = WINHEIGHT; } - if(swk_gi_init(w)) { - w->running = 1; - swk_update(w); - } - return w->running; + if(!running && !swk_gi_init(w)) + return 0; + running = 1; + swk_update(w); + return 1; } void -swk_update(SwkWindow *w) { +swk_update() { char text[8]; int roy, oy, scroll = 0; w->_e.type = EExpose; @@ -51,16 +54,16 @@ swk_update(SwkWindow *w) { oy = b->r.y; } swk_gi_flip(); - } else w->running = 0; + } else running = 0; } void -swk_exit(SwkWindow *w) { - w->running = 0; +swk_exit() { + running = 0; } void -swk_loop(SwkWindow *w) { +swk_loop() { SwkEvent *e; do { if((e = swk_next_event(w))) @@ -69,20 +72,20 @@ swk_loop(SwkWindow *w) { } void -swk_fontsize_increase(SwkWindow *w) { +swk_fontsize_increase() { swk_gi_fontsize(1); swk_update(w); } void -swk_fontsize_decrease(SwkWindow *w) { +swk_fontsize_decrease() { swk_gi_fontsize(-1); swk_update(w); } static void -setscrollbox(SwkWindow *w, int delta) { +setscrollbox(int delta) { SwkBox *r = NULL; SwkBox *b = w->boxes; for(; b->cb; b++) { @@ -95,16 +98,16 @@ setscrollbox(SwkWindow *w, int delta) { } void -swk_scroll_up(SwkWindow *w) { - setscrollbox(w, 2); +swk_scroll_up() { + setscrollbox(2); } void -swk_scroll_down(SwkWindow *w) { - setscrollbox(w, -2); +swk_scroll_down() { + setscrollbox(-2); } -static void swk_fit_row(SwkWindow *w, SwkBox *a, SwkBox *b, int y) { +static void swk_fit_row(SwkBox *a, SwkBox *b, int y) { SwkBox *btmp; int count = 0, x = 0; for(btmp=a; btmp<b; btmp++) @@ -133,37 +136,37 @@ countrows(SwkBox *b) { } void -swk_fit(SwkWindow *w) { +swk_fit() { SwkBox *b, *b2; int x, y = 0, skip = 0; for(b=b2=w->boxes; b->cb; b++) { if(b->r.w==-1 && b->r.h==-1) { x = (int)(size_t)b->data; - swk_fit_row(w, b2, b, y); + swk_fit_row(b2, b, y); y += x-skip; // vertical align // - if(x<0) y+=w->r.h-countrows(b2); + if(x<0) y += w->r.h-countrows(b2); b2 = b+1; } y += b->scroll; } - swk_fit_row(w, b2, b, y); + swk_fit_row(b2, b, y); } int -swk_has_event(SwkWindow *w) { +swk_has_event() { // XXX: remove this useless wrap return swk_gi_has_event(w); } void -swk_focus_activate(SwkWindow *w) { +swk_focus_activate() { w->_e.box = w->box; w->_e.type = EClick; } SwkEvent * -swk_next_event(SwkWindow *w) { - if(w->running) +swk_next_event() { + if(running) return swk_gi_event(w, 1); w->_e.type = EQuit; w->_e.win = w; @@ -239,7 +242,7 @@ swk_handle_event(SwkEvent *e) { } void -swk_focus_first(SwkWindow *w) { +swk_focus_first() { w->box = w->boxes; while(w->box->cb == swk_filler) w->box++; @@ -248,7 +251,7 @@ swk_focus_first(SwkWindow *w) { } void -swk_focus_next(SwkWindow *w) { +swk_focus_next() { w->box++; if(w->box->cb == NULL) w->box = w->boxes; @@ -259,7 +262,7 @@ swk_focus_next(SwkWindow *w) { } void -swk_focus_prev(SwkWindow *w) { +swk_focus_prev() { if(w->box == w->boxes) { while(w->box->cb) w->box++; diff --git a/swk.h b/swk.h @@ -13,7 +13,7 @@ typedef struct SwkBox SwkBox; typedef struct SwkEvent SwkEvent; typedef struct SwkWindow SwkWindow; typedef void (*SwkEventCallback)(SwkEvent *e); -typedef void (*SwkKeyCallback)(SwkWindow *w); +typedef void (*SwkKeyCallback)(); typedef struct { int x; @@ -67,7 +67,6 @@ struct SwkBox { struct SwkWindow { char *title; - int running; SwkEventCallback cb; Rect r; SwkBox *boxes; @@ -76,23 +75,23 @@ struct SwkWindow { SwkEvent _e; }; -int swk_init(SwkWindow *w); -void swk_update(SwkWindow *w); -void swk_exit(SwkWindow *w); -void swk_fit(SwkWindow *w); -void swk_loop(SwkWindow *w); -SwkEvent *swk_next_event(SwkWindow *w); -int swk_has_event(SwkWindow *w); +int swk_use(SwkWindow *w); +void swk_update(); +void swk_exit(); +void swk_fit(); +void swk_loop(); +SwkEvent *swk_next_event(); +int swk_has_event(); void swk_handle_event(SwkEvent *e); -void swk_focus_first(SwkWindow *w); -void swk_focus_next(SwkWindow *w); -void swk_focus_prev(SwkWindow *w); -void swk_focus_activate(SwkWindow *w); -void swk_scroll_up(SwkWindow *w); -void swk_scroll_down(SwkWindow *w); -void swk_fontsize_increase(SwkWindow *w); -void swk_fontsize_decrease(SwkWindow *w); +void swk_focus_first(); +void swk_focus_next(); +void swk_focus_prev(); +void swk_focus_activate(); +void swk_scroll_up(); +void swk_scroll_down(); +void swk_fontsize_increase(); +void swk_fontsize_decrease(); void swk_button(SwkEvent *e); void swk_label(SwkEvent *e); diff --git a/t/test.c b/t/test.c @@ -152,8 +152,8 @@ main() { .cancel=cb */ }; - if(!swk_init(&w)) + if(!swk_use(&w)) return 1; - swk_loop(&w); + swk_loop(); return 0; } diff --git a/t/ui.c b/t/ui.c @@ -173,8 +173,7 @@ swk_ui(const char *text) { } text++; } - w->running = 1; - swk_init(w); + swk_use(w); return w; } @@ -191,7 +190,7 @@ static SwkWindow *w = NULL; int main() { w = swk_ui(UI); - if(!w||!swk_init(w)) + if(!w||!swk_use(w)) return 1; swk_loop(w); return 0;