swk

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

commit 8c51acff73d4d739dada94a877f29ba0c623f8c1
parent 8d23f1c2cc7906dfb5d528533853d7a0fe3d4f00
Author: pancake <pancake@nopcode.org>
Date:   Wed, 28 Apr 2010 17:55:20 +0200

add image support to gi backend
tested in example
added global event handler in SwkWindow
Diffstat:
Makefile | 2+-
config.def.h | 1-
gi_sdl.c | 49+++++++++++++++++++++++++++++++++++++++++--------
image.png | 0
swk.c | 28++++++++++++++++++++++++++++
swk.h | 9+++++++++
test.c | 7+++++++
7 files changed, 86 insertions(+), 10 deletions(-)

diff --git a/Makefile b/Makefile @@ -8,7 +8,7 @@ CFLAGS+=-I. # graphic backend GI?=sdl -GI_LIBS=-lSDL -lSDL_ttf +GI_LIBS=-lSDL -lSDL_ttf -lSDL_image GI_OBJS=gi_${GI}.o GI_SRCS=gi_${GI}.c diff --git a/config.def.h b/config.def.h @@ -8,7 +8,6 @@ #endif /* appearance */ -#define FONTSIZE 40 #define FONTBOLD 1 #define WINWIDTH 640 #define WINHEIGHT 480 diff --git a/gi_sdl.c b/gi_sdl.c @@ -1,6 +1,7 @@ /* See LICENSE file for copyright and license details. */ #define _BSD_SOURCE // strdup #include <SDL/SDL.h> +#include <SDL/SDL_image.h> #include <SDL/SDL_ttf.h> #include "swk.h" #include "config.h" @@ -19,13 +20,13 @@ static TTF_Font *font = NULL; static int has_event = 0; static SDL_Event lastev = { .type=-1 }; -static void putpixel(int x, int y, Uint32 pixel) { +static void putpixel(SDL_Surface *scr, int x, int y, Uint32 pixel) { Uint8 *p, *pend; - int delta, bpp = screen->format->BytesPerPixel; - delta = y * screen->pitch + x * bpp; - p = (Uint8 *)screen->pixels + delta; - pend = (Uint8 *)screen->pixels + (screen->h*screen->w*bpp); - if((p<((Uint8 *)screen->pixels)) || (p>=pend)) + int delta, bpp = scr->format->BytesPerPixel; + delta = y * scr->pitch + x * bpp; + p = (Uint8 *)scr->pixels + delta; + pend = (Uint8 *)scr->pixels + (scr->h*scr->w*bpp); + if((p<((Uint8 *)scr->pixels)) || (p>=pend)) return; #if BPP == 8 *p = pixel; @@ -209,9 +210,9 @@ swk_gi_line(int x1, int y1, int x2, int y2, int color) { int i; x1 *= fs; y1 *= fs; x2 *= fs; y2 *= fs; - if(x2==0) for(i=0;i<y2;i++) putpixel(x1, y1+i, pal[color]); + if(x2==0) for(i=0;i<y2;i++) putpixel(screen, x1, y1+i, pal[color]); else - if(y2==0) for(i=0;i<x2;i++) putpixel(x1+i, y1, pal[color]); + if(y2==0) for(i=0;i<x2;i++) putpixel(screen, x1+i, y1, pal[color]); } void @@ -255,3 +256,35 @@ 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; + SDL_Rect area = { r.x*fs, r.y*fs, r.w*fs, r.h*fs }; + if (s) SDL_BlitSurface(s, NULL, screen, &area); +} + +void* +swk_gi_img_load(const char *str) { + return IMG_Load(str); +} + +void* +swk_gi_img_free(const char *str) { + return IMG_Load(str); +} + +void +swk_gi_img_set(void *img, int x, int y, int color) { + SDL_Surface *s = (SDL_Surface*)img; + if (s) putpixel(s, x, y, color); +} + +int +swk_gi_img_get(void *img, int x, int y) { + /* TODO */ + return 0; +} + diff --git a/image.png b/image.png Binary files differ. diff --git a/swk.c b/swk.c @@ -161,6 +161,8 @@ void swk_handle_event(SwkEvent *e) { int i; SwkBox *b; + if(e->win->cb) + e->win->cb(e); switch(e->type) { case EKey: for(i=0; keys[i].cb; i++) { @@ -427,3 +429,29 @@ swk_progress(SwkEvent *e) { break; } } + +/* -- */ +void +swk_image_free(SwkBox *b) { + swk_gi_img_free(b->data); + b->data = NULL; +} + +void +swk_image(SwkEvent *e) { + if(e->box->data == NULL) + e->box->data = swk_gi_img_load(e->box->text); + switch(e->type) { + case EExpose: + if (e->box->data) + swk_gi_img(e->box->r, e->box->data); + else swk_gi_rect(e->box->r, ColorFG); + if(e->win->box == e->box) { + Rect r = e->box->r; + swk_gi_line(r.x, r.y+1, r.w, 0, ColorHI); + } + break; + default: + break; + } +} diff --git a/swk.h b/swk.h @@ -68,6 +68,7 @@ struct SwkBox { struct SwkWindow { char *title; int running; + SwkEventCallback cb; Rect r; SwkBox *boxes; /* internal use */ @@ -101,6 +102,7 @@ void swk_filler(SwkEvent *e); void swk_option(SwkEvent *e); void swk_separator(SwkEvent *e); void swk_progress(SwkEvent *e); +void swk_image(SwkEvent *e); /* graphic backend */ @@ -119,3 +121,10 @@ void swk_gi_line(int x1, int y1, int x2, int y2, int color); void swk_gi_fill(Rect r, int color, int lil); void swk_gi_rect(Rect r, int color); void swk_gi_text(Rect r, const char *text); + +/* images */ +void swk_gi_img(Rect r, void *img); +void* swk_gi_img_load(const char *str); +void* swk_gi_img_free(const char *str); +void swk_gi_img_set(void *img, int x, int y, int color); +int swk_gi_img_get(void *img, int x, int y); diff --git a/test.c b/test.c @@ -89,6 +89,13 @@ static SwkBox helloworld[] = { { .cb=swk_label, .text="Password:", }, { .cb=swk_password, .text="1234", }, SWK_BOX_NEWLINE(-1), + { .cb=swk_filler, }, + { .cb=swk_image, .text="image.png" }, + { .cb=swk_image, .text="image.png" }, + { .cb=swk_image, .text="image.png" }, + { .cb=swk_image, .text="image.png" }, + { .cb=swk_filler, }, + SWK_BOX_NEWLINE(2), { .cb=mybutton, .text="yes" }, { .cb=mybutton, .text="no" }, { .cb=swk_filler, },