swk

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

commit 71b99bef932ca15d70c9575f4d62d80d7c82f9bb
parent a704775a268f1c3ff36430d778e72ca8ff63eac3
Author: pancake <pancake@nopcode.org>
Date:   Wed, 28 Apr 2010 02:58:40 +0200

implement progressbar widget
some modifications in vertical filler code.. still ugly
Diffstat:
swk.c | 36+++++++++++++++++++++++++++++++-----
swk.h | 1+
test.c | 28+++++++++++++++++++++++++---
3 files changed, 57 insertions(+), 8 deletions(-)

diff --git a/swk.c b/swk.c @@ -32,9 +32,9 @@ swk_update(SwkWindow *w) { roy = oy = 0; for(;b->cb; b++) { w->_e.box = b; - if (b->r.w==-1 && b->r.h==-1 && ((int)(size_t)b->data)<0) + if(b->r.w==-1 && b->r.h==-1 && ((int)(size_t)b->data)<0) roy = oy+1; - if (roy && b->r.y < roy) + if(roy && b->r.y < roy) swk_gi_line(0, roy, w->r.w, 0, ColorHI); else b->cb(&w->_e); oy = b->r.y; @@ -114,10 +114,10 @@ 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; + if ((int)(size_t)b->data>0) + row += (int)(size_t)b->data; } - return (1+row) * 0.7; // hacky + return row+7; // hacky } void @@ -394,3 +394,29 @@ swk_separator(SwkEvent *e) { break; } } + +void +swk_progress(SwkEvent *e) { + int pc, len; + Rect r; + switch(e->type) { + case EExpose: + r = e->box->r; + r.x+=1; + swk_gi_text(r, e->box->text); + r.x-=1; + swk_gi_rect(r, ColorFG); + len = strlen(e->box->text)+2; + r.x += len*0.8; + r.w -= len*0.6; + pc = atoi(e->box->text); + if (pc<0) pc = 0; + else if (pc>100) pc = 100; + r.w = (int)((float)r.w*((float)pc/100)); + if (r.w>0) + swk_gi_fill(r, ColorFG, 1); + break; + default: + break; + } +} diff --git a/swk.h b/swk.h @@ -100,6 +100,7 @@ void swk_password(SwkEvent *e); void swk_filler(SwkEvent *e); void swk_option(SwkEvent *e); void swk_separator(SwkEvent *e); +void swk_progress(SwkEvent *e); /* graphic backend */ diff --git a/test.c b/test.c @@ -2,8 +2,11 @@ #include "swk.h" static int count = 3; +static int pccount = 0; static char text[64]; +static char pctext[64]; static SwkBox helloworld[]; +static SwkBox about[]; static SwkBox *opt = NULL; static void mybutton(SwkEvent *e); static void mybutton_about(SwkEvent *e); @@ -22,19 +25,38 @@ static void mybutton(SwkEvent *e) { swk_button(e); } +static void myprogressbutton(SwkEvent *e) { + if (e->type == EClick) { + pccount+=15; + sprintf(pctext, "%d%%", pccount); + about[10].text = pctext; + if(pccount > 100) { + pccount = 0; + e->win->boxes = helloworld; + swk_update(e->win); + } + } + swk_button(e); +} + static SwkBox about[] = { { .cb=swk_label, .text="About this program...", }, SWK_BOX_NEWLINE(1), { .cb=swk_separator }, SWK_BOX_NEWLINE(2), { .cb=swk_label, .text="This is a test program for swk" }, -#if 0 SWK_BOX_NEWLINE(1), { .cb=swk_label, .text=" ... a simple widget kit " }, SWK_BOX_NEWLINE(1), { .cb=swk_label, .text=" ... from the suckless.org project" }, -#endif - SWK_BOX_NEWLINE(-1), + SWK_BOX_NEWLINE(2), + { .cb=swk_progress, .text="0%", }, + SWK_BOX_NEWLINE(1), + { .cb=swk_filler }, + { .cb=myprogressbutton, .text="next", }, + { .cb=swk_filler }, + SWK_BOX_NEWLINE(-1), + //SWK_BOX_NEWLINE(3), { .cb=swk_filler }, { .cb=mybutton_about_ok, .text="Ok" }, { .cb=NULL }