swk

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

commit 1d3f6757a67d88853336ab72bf0fa711698d744a
parent bb31db18c59a91be764cd4b076416319a814286e
Author: pancake <pancake@nopcode.org>
Date:   Fri,  7 May 2010 00:59:57 +0200

add null pointer check in putpixel()
add swk_gi_img_new() to get blank surface
display text cursor line in swk_entry
initial nonworking implementation of swk_sketch
Diffstat:
README | 1+
gi_sdl.c | 8+++++++-
swk.c | 41++++++++++++++++++++++++++++++++---------
swk.h | 2++
test.c | 2++
5 files changed, 44 insertions(+), 10 deletions(-)

diff --git a/README b/README @@ -12,6 +12,7 @@ Default keybindings are defined in config.h 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 * scroll on focus diff --git a/gi_sdl.c b/gi_sdl.c @@ -31,6 +31,7 @@ getscrpoint(SDL_Surface *scr, int x, int y) { static void putpixel(SDL_Surface *scr, int x, int y, Uint32 pixel) { Uint8 *p = getscrpoint(scr, x, y); + if(!p) return; #if BPP == 8 *p = pixel; #elif BPP == 16 @@ -272,7 +273,6 @@ swk_gi_text(Rect r, const char *text) { free(ptr); } -/* images */ void swk_gi_img(Rect r, void *img) { SDL_Surface *s = (SDL_Surface*)img; @@ -280,6 +280,12 @@ swk_gi_img(Rect r, void *img) { if(s) SDL_BlitSurface(s, NULL, screen, &area); } +/* image api */ +void* +swk_gi_img_new(int w, int h, int color) { + return SDL_CreateRGBSurface(NULL, w, h, BPP, 0, 0, 0, 0); +} + void* swk_gi_img_load(const char *str) { return IMG_Load(str); diff --git a/swk.c b/swk.c @@ -24,8 +24,7 @@ swk_init(SwkWindow *w) { void swk_update(SwkWindow *w) { char text[8]; - int roy, oy; - int scroll = 0; + int roy, oy, scroll = 0; w->_e.type = EExpose; if(swk_gi_update(w)) { SwkBox *b = w->boxes; @@ -106,9 +105,8 @@ swk_scroll_down(SwkWindow *w) { } static void swk_fit_row(SwkWindow *w, SwkBox *a, SwkBox *b, int y) { - int count, x = 0; SwkBox *btmp; - count = 0; + int count = 0, x = 0; for(btmp=a; btmp<b; btmp++) count++; if(count) { @@ -136,9 +134,8 @@ countrows(SwkBox *b) { void swk_fit(SwkWindow *w) { - int skip = 0; - int x, y = 0; 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; @@ -288,9 +285,9 @@ swk_label(SwkEvent *e) { switch(e->type) { case EExpose: r = e->box->r; - r.w+=6; + r.w += 6; swk_gi_text(r, e->box->text); - r.w-=6; + r.w -= 6; if(e->win->box == e->box) swk_gi_line(r.x, r.y+1, r.w, 0, ColorHI); break; @@ -301,9 +298,9 @@ swk_label(SwkEvent *e) { void swk_password(SwkEvent *e) { + char *str, *ptr; int len; Rect r; - char *str, *ptr; switch(e->type) { case EExpose: r = e->box->r; @@ -354,6 +351,12 @@ swk_entry(SwkEvent *e) { default: swk_label(e); break; + case EExpose: + // XXX: add support for cursor (handle arrow keys) + len = e->box->r.x+(strlen(e->box->text)*0.6); + swk_label(e); + swk_gi_line(len, e->box->r.y, 0, 1, ColorFG); + break; } } @@ -473,3 +476,23 @@ swk_image(SwkEvent *e) { break; } } + +void +swk_sketch(SwkEvent *e) { + if(e->box->data == NULL) + e->box->data = swk_gi_img_new(e->box->r.w, e->box->r.h, ColorHI); + switch(e->type) { + case EClick: + swk_gi_img_set(e->box->data, + e->data.click.point.x, e->data.click.point.y, + ColorFG); + printf("CLICKED %p %d %d\n", e->box->data, e->data.click.point.x, e->data.click.point.y); + break; + case EExpose: + swk_gi_img(e->box->r, e->box->data); + swk_gi_rect(e->box->r, ColorFG); + break; + default: + break; + } +} diff --git a/swk.h b/swk.h @@ -103,6 +103,7 @@ void swk_option(SwkEvent *e); void swk_separator(SwkEvent *e); void swk_progress(SwkEvent *e); void swk_image(SwkEvent *e); +void swk_sketch(SwkEvent *e); /* graphic backend */ @@ -124,6 +125,7 @@ void swk_gi_text(Rect r, const char *text); /* images */ void swk_gi_img(Rect r, void *img); +void* swk_gi_img_new(int w, int h, int color); void* swk_gi_img_load(const char *str); void swk_gi_img_free(void *s); void swk_gi_img_set(void *img, int x, int y, int color); diff --git a/test.c b/test.c @@ -91,6 +91,8 @@ static SwkBox helloworld[] = { { .cb=swk_password, .text="1234", }, SWK_BOX_NEWLINE(-1), { .cb=swk_filler, }, + { .cb=swk_sketch }, + SWK_BOX_NEWLINE(1), { .cb=swk_image, .text="image.png" }, { .cb=swk_image, .text="image.png" }, { .cb=swk_image, .text="image.png" },