[hackers] [swk] implement progressbar widget || pancake

From: <hg_AT_suckless.org>
Date: Wed, 28 Apr 2010 01:05:05 +0000 (UTC)

changeset: 23:3a376982a2be
tag: tip
user: pancake <pancake_AT_nopcode.org>
date: Wed Apr 28 02:58:40 2010 +0200
files: swk.c swk.h test.c
description:
implement progressbar widget
some modifications in vertical filler code.. still ugly

diff -r 4a5d00a8a540 -r 3a376982a2be swk.c
--- a/swk.c Wed Apr 28 02:14:08 2010 +0200
+++ b/swk.c Wed Apr 28 02:58:40 2010 +0200
@@ -32,9 +32,9 @@
                 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 @@
         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 @@
                 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 -r 4a5d00a8a540 -r 3a376982a2be swk.h
--- a/swk.h Wed Apr 28 02:14:08 2010 +0200
+++ b/swk.h Wed Apr 28 02:58:40 2010 +0200
@@ -100,6 +100,7 @@
 void swk_filler(SwkEvent *e);
 void swk_option(SwkEvent *e);
 void swk_separator(SwkEvent *e);
+void swk_progress(SwkEvent *e);
 
 /* graphic backend */
 
diff -r 4a5d00a8a540 -r 3a376982a2be test.c
--- a/test.c Wed Apr 28 02:14:08 2010 +0200
+++ b/test.c Wed Apr 28 02:58:40 2010 +0200
@@ -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 @@
         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 }
Received on Wed Apr 28 2010 - 01:05:05 UTC

This archive was generated by hypermail 2.2.0 : Wed Apr 28 2010 - 01:12:03 UTC