swk

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

commit eade25b9e11f451f15d7ebc3af029574e5a7b0dd
parent aeb200cd903c013b50f5bf06eb284d073d75d216
Author: pancake <pancake@nopcode.org>
Date:   Wed, 28 Apr 2010 03:46:01 +0200

add support for mouse wheel to scroll
compile shared library
Diffstat:
Makefile | 14+++++++++++---
gi_sdl.c | 10+++++-----
swk.c | 50++++++++++++++++++++++++++++++--------------------
test.c | 14+++++++-------
4 files changed, 53 insertions(+), 35 deletions(-)

diff --git a/Makefile b/Makefile @@ -11,8 +11,9 @@ GI?=sdl GI_LIBS=-lSDL -lSDL_ttf GI_OBJS=gi_${GI}.o +GI_SRCS=gi_${GI}.c -all: static test +all: static shared test config.h: cp config.def.h config.h @@ -21,11 +22,12 @@ test: config.h test.o libswk.a ${CC} test.o -o test libswk.a ${GI_LIBS} clean: - rm -f swk.pc swk.mk libswk.a test.o swk.o test ${GI_OBJS} + rm -f swk.pc swk.mk libswk.a libswk.so test.o swk.o test ${GI_OBJS} install: mkdir -p ${DESTDIR}/${LIBDIR} cp libswk.a ${DESTDIR}/${LIBDIR} + cp libswk.so ${DESTDIR}/${LIBDIR} mkdir -p ${DESTDIR}/${LIBDIR}/mk cp swk.mk ${DESTDIR}/${LIBDIR}/mk/swk.mk mkdir -p ${DESTDIR}/${LIBDIR}/pkgconfig @@ -33,6 +35,11 @@ install: static: libswk.a +shared: libswk.so + +libswk.so: config.h swk.o ${GI_OBJS} + ${CC} ${CFLAGS} -fPIC -shared swk.c ${GI_SRCS} -o libswk.so + swk.o: config.h libswk.a: config.h swk.o ${GI_OBJS} @@ -46,5 +53,6 @@ libswk.a: config.h swk.o ${GI_OBJS} echo Name: swk >> swk.pc echo Version: ${VERSION} >> swk.pc echo Description: simple widget kit >> swk.pc - echo Libs: ${PREFIX}/lib/libswk.a ${GI_LIBS} >> swk.pc + echo Libs: -L${PREFIX}/lib -lswk ${GI_LIBS} >> swk.pc + echo Libs.private: ${PREFIX}/lib/libswk.a ${GI_LIBS} >> swk.pc echo Cflags: -I${PREFIX}/include >> swk.pc diff --git a/gi_sdl.c b/gi_sdl.c @@ -25,7 +25,7 @@ static void putpixel(int x, int y, Uint32 pixel) { 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)) + if((p<((Uint8 *)screen->pixels)) || (p>=pend)) return; #if BPP == 8 *p = pixel; @@ -54,7 +54,7 @@ swk_gi_fontsize(int sz) { fprintf(stderr, "Cannot open font '%s'\n", FONTNAME); return 0; } else - if (FONTBOLD) + if(FONTBOLD) TTF_SetFontStyle(font, TTF_STYLE_BOLD); return 1; } @@ -83,7 +83,7 @@ swk_gi_init(SwkWindow *w) { int swk_gi_update(SwkWindow *w) { screen = SDL_GetVideoSurface(); - if (screen == NULL) + if(screen == NULL) return 0; w->r.w = (screen->w / fs)-1; w->r.h = (screen->h / fs)-1; @@ -203,7 +203,7 @@ swk_gi_line(int x1, int y1, int x2, int y2, int color) { void swk_gi_fill(Rect r, int color, int lil) { SDL_Rect area = { r.x*fs, r.y*fs, r.w*fs, r.h*fs }; - if (lil) { + if(lil) { const int s = fs/4; area.x += s; area.y += s; @@ -226,7 +226,7 @@ swk_gi_text(Rect r, const char *text) { char *ptr = NULL; int len = strlen(text); int w = (int)((double)r.w * 1.6); // hacky - if (len>w) { + if(len>w) { ptr = strdup(text); text = (const char *)ptr; ptr[w] = '\0'; diff --git a/swk.c b/swk.c @@ -8,7 +8,7 @@ int swk_init(SwkWindow *w) { w->_e.win = w; - if (w->box == NULL) + if(w->box == NULL) swk_focus_first(w); if(w->r.w == 0 || w->r.h == 0) { w->r.w = WINWIDTH; @@ -73,7 +73,7 @@ void swk_scroll_up(SwkWindow *w) { SwkBox *b = w->boxes; for(; b->cb; 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) { b->scroll++; return; } @@ -84,7 +84,7 @@ void swk_scroll_down(SwkWindow *w) { SwkBox *b = w->boxes; for(; b->cb; 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) { b->scroll--; return; } @@ -114,7 +114,7 @@ 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) + if((int)(size_t)b->data>0) row += (int)(size_t)b->data; } return row+7; // hacky @@ -166,14 +166,14 @@ swk_handle_event(SwkEvent *e) { switch(e->type) { case EKey: for(i=0; keys[i].cb; i++) { - if (e->data.key.modmask == keys[i].modmask + if(e->data.key.modmask == keys[i].modmask && e->data.key.keycode == keys[i].keycode) { keys[i].cb(e->win); break; } } /* XXX: this must be implemented in app? */ - if (e->data.key.keycode==27) { + if(e->data.key.keycode==27) { e->box = e->win->box; e->type = EQuit; swk_exit(e->win); @@ -196,13 +196,23 @@ swk_handle_event(SwkEvent *e) { } break; case EClick: - for(b=e->win->boxes; b->cb; b++) { - if(SWK_HIT(b->r, e->data.click.point)) { - e->box = e->win->box = b; - e->box->cb(e); - swk_update(e->win); + // TODO: do this needs to be in config.h? + switch(e->data.click.button) { + case 4: + swk_scroll_up(e->win); + break; + case 5: + swk_scroll_down(e->win); + break; + default: + for(b=e->win->boxes; b->cb; b++) { + if(SWK_HIT(b->r, e->data.click.point)) { + e->box = e->win->box = b; + e->box->cb(e); + } } } + swk_update(e->win); break; case EExpose: swk_update(e->win); @@ -282,7 +292,7 @@ swk_password(SwkEvent *e) { if(e->win->box == e->box) swk_gi_line(r.x, r.y+1, r.w, 0, ColorHI); len = strlen(e->box->text); - if (len>0) { + if(len>0) { ptr = str = malloc(len+1); for(;len--;ptr++) *ptr='*'; @@ -358,8 +368,8 @@ swk_option(SwkEvent *e) { SwkBox **b = (SwkBox**)e->box->data; switch(e->type) { case EClick: - if (b==(void*)0) e->box->data = (void*)1; - else if (b==(void*)1) e->box->data = (void*)0; + if(b==(void*)0) e->box->data = (void*)1; + else if(b==(void*)1) e->box->data = (void*)0; else *b = (e->box==*b)?NULL:e->box; break; case EExpose: @@ -367,9 +377,9 @@ swk_option(SwkEvent *e) { if(e->win->box == e->box) swk_gi_line(r.x, r.y+1, r.w, 0, ColorHI); r.w = r.h = 1; - if (b==(void*)1) swk_gi_fill(r, ColorHI, 1); - else if (b==(void*)0) swk_gi_fill(r, ColorFG, 1); - else if (e->box==*b) swk_gi_fill(r, ColorHI, 1); + if(b==(void*)1) swk_gi_fill(r, ColorHI, 1); + else if(b==(void*)0) swk_gi_fill(r, ColorFG, 1); + else if(e->box==*b) swk_gi_fill(r, ColorHI, 1); else swk_gi_fill(r, ColorFG, 1); r = e->box->r; r.x += 2; @@ -410,10 +420,10 @@ swk_progress(SwkEvent *e) { 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; + if(pc<0) pc = 0; + else if(pc>100) pc = 100; r.w = (int)((float)r.w*((float)pc/100)); - if (r.w>0) + if(r.w>0) swk_gi_fill(r, ColorFG, 1); break; default: diff --git a/test.c b/test.c @@ -13,10 +13,10 @@ static void mybutton_about(SwkEvent *e); static void mybutton_about_ok(SwkEvent *e); static void mybutton(SwkEvent *e) { - if (e->type == EClick) { + if(e->type == EClick) { sprintf(text, "Do it again %d times\n", count); helloworld[0].text = text; - if (opt == NULL) + if(opt == NULL) printf("Option: none\n"); else printf("Option: %s\n", opt->text); if(count-- == 0) @@ -26,15 +26,15 @@ static void mybutton(SwkEvent *e) { } static void myprogressbutton(SwkEvent *e) { - if (e->type == EClick) { + 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); } + sprintf(pctext, "%d%%", pccount); + about[10].text = pctext; } swk_button(e); } @@ -63,7 +63,7 @@ static SwkBox about[] = { }; static void mybutton_about_ok(SwkEvent *e) { - if (e->type == EClick) { + if(e->type == EClick) { e->win->boxes = helloworld; swk_update(e->win); } @@ -71,7 +71,7 @@ static void mybutton_about_ok(SwkEvent *e) { } static void mybutton_about(SwkEvent *e) { - if (e->type == EClick) { + if(e->type == EClick) { e->win->boxes = about; swk_update(e->win); }