swk

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

commit dae979d45b2a2ef32a34cc2cb0d91498c6452bfa
parent 523ced42462268d32bebebb07410ca8cd57a8e3a
Author: pancake <pancake@nopcode.org>
Date:   Wed,  9 Jun 2010 23:43:08 +0200

add some more notes in TODO file
define FONTFACTOR in gi_sdl and fix text width wrap
add IS_SCROLLBOX macro
some indent fixes and simplify some code
Diffstat:
TODO | 2++
gi_sdl.c | 36++++++++++++++++++------------------
swk.c | 17++++++++---------
swk.h | 1+
t/tlock.c | 14++++++--------
5 files changed, 35 insertions(+), 35 deletions(-)

diff --git a/TODO b/TODO @@ -1,5 +1,7 @@ TODO ==== + * allow to set absolute font size + * buttons must be 3 line height * 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 (?) diff --git a/gi_sdl.c b/gi_sdl.c @@ -8,6 +8,7 @@ #include "config.h" #define FONTNAME "Inconsolata.otf" +#define FONTFACTOR 2.1 #define BPP 32 #define SDLFLAGS SDL_DOUBLEBUF|SDL_RESIZABLE @@ -31,7 +32,8 @@ getscrpoint(SDL_Surface *scr, int x, int y) { return p; } -static void putpixel(SDL_Surface *scr, int x, int y, Uint32 pixel) { +static void +putpixel(SDL_Surface *scr, int x, int y, Uint32 pixel) { Uint8 *p = getscrpoint(scr, x, y); if(!p) return; #if BPP == 8 @@ -189,21 +191,19 @@ swk_gi_event(SwkWindow *w, int dowait) { ret->data.key.modmask |= Meta; if(ret->data.key.keycode != 0 && event.key.keysym.unicode != 0) { ret->data.key.keycode = event.key.keysym.unicode; - } else { - // TODO key aliases defined in config.h - switch((int)event.key.keysym.sym) { - case 1073741906: // n900 up key - case 273: - ret->data.key.keycode = KUp; - break; - case 1073741912: // n900 down key - case 274: - ret->data.key.keycode = KDown; - break; - default: - ret->data.key.keycode = event.key.keysym.sym; - break; - } + } else // TODO key aliases defined in config.h + switch((int)event.key.keysym.sym) { + case 1073741906: // n900 up key + case 273: + ret->data.key.keycode = KUp; + break; + case 1073741912: // n900 down key + case 274: + ret->data.key.keycode = KDown; + break; + default: + ret->data.key.keycode = event.key.keysym.sym; + break; } fprintf(stderr, "event: key %d %d\n", ret->data.key.modmask, ret->data.key.keycode); @@ -264,13 +264,13 @@ swk_gi_rect(Rect r, int color) { void swk_gi_text(Rect r, const char *text) { char *ptr = NULL; - int w = (int)((double)r.w * 1.6); // hacky + int w = (int)((double)r.w * FONTFACTOR); if(text && *text) { int len = text?strlen(text):0; if(len>w) { ptr = strdup(text); text = (const char *)ptr; - ptr[w] = '\0'; + ptr[w]='\0'; } SDL_Surface *ts = TTF_RenderText_Shaded(font, text, fontcolor, bgcolor); if(ts) { diff --git a/swk.c b/swk.c @@ -93,7 +93,7 @@ setscrollbox(int delta) { SwkBox *r = NULL; SwkBox *b = w->boxes; for(; b->cb; b++) { - if(b->r.w==-1 && b->r.h==-1 && ((int)(size_t)b->data)<0) + if(IS_SCROLLBOX(b)) r = b; if(w->box==b && r) break; @@ -131,13 +131,11 @@ swk_fit_row(SwkBox *a, SwkBox *b, int y) { static int countrows(SwkBox *b) { - int row = 0; - for(; b->cb; b++) { - if(b->r.w==-1&&b->r.h==-1) - if((int)(size_t)b->data>0) - row += (int)(size_t)b->data; - } - return row+7; // hacky + int row = 7; // hacky value to center widgets + for(; b->cb; b++) + if(IS_SCROLLBOX(b)) + row += (int)(size_t)b->data; + return row; } void @@ -361,7 +359,7 @@ swk_entry(SwkEvent *e) { break; case EExpose: // XXX: add support for cursor (handle arrow keys) - len = e->box->r.x+(strlen(e->box->text)*0.6); + len = e->box->r.x+(strlen(e->box->text)*0.7); swk_label(e); swk_gi_line(len, e->box->r.y, 0, 1, ColorFG); break; @@ -374,6 +372,7 @@ swk_button(SwkEvent *e) { switch(e->type) { case EExpose: r = e->box->r; + //r.h = 3; // TODO: add support for multiple-line widgets r.x++; swk_gi_text(r, e->box->text); r.x--; diff --git a/swk.h b/swk.h @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ +#define IS_SCROLLBOX(b) (b->r.w==-1 && b->r.h==-1 && ((int)(size_t)b->data)<0) #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)) diff --git a/t/tlock.c b/t/tlock.c @@ -8,16 +8,15 @@ // TODO: enable alarm when dpms on static SwkBox contents[]; -#define COUNT 0 -static int count = COUNT; - +static int count = 0; static char timestring[80]; + static void settimestring() { struct tm lt; time_t t = time(0); localtime_r(&t, &lt); snprintf(timestring, sizeof(timestring), - "%04d/%02d/%02d %02d:%02d:%02d", + " %04d/%02d/%02d %02d:%02d:%02d", 1900+lt.tm_year, lt.tm_mon+1, lt.tm_mday, lt.tm_hour, lt.tm_min, lt.tm_sec); } @@ -25,10 +24,8 @@ static void settimestring() { static void timepoll() { settimestring(); swk_update(); - if(count--<0) { - contents[2].scroll = 0; - count = COUNT; - } + if(count--<0) + count = contents[2].scroll = 0; alarm(1); } @@ -69,6 +66,7 @@ int main() { if(!swk_use(&w)) return 1; init_alarm(); + swk_fontsize_increase(); swk_loop(); return 0; }