[hackers] [swk] add support for mouse wheel to scroll || pancake

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

changeset: 25:b01b2c9cf9f4
tag: tip
user: pancake <pancake_AT_nopcode.org>
date: Wed Apr 28 03:46:01 2010 +0200
files: Makefile gi_sdl.c swk.c test.c
description:
add support for mouse wheel to scroll
compile shared library

diff -r c9bb185814c7 -r b01b2c9cf9f4 Makefile
--- a/Makefile Wed Apr 28 03:06:32 2010 +0200
+++ b/Makefile Wed Apr 28 03:46:01 2010 +0200
@@ -11,8 +11,9 @@
 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 @@
         ${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 @@
 
 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 @@
         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 -r c9bb185814c7 -r b01b2c9cf9f4 gi_sdl.c
--- a/gi_sdl.c Wed Apr 28 03:06:32 2010 +0200
+++ b/gi_sdl.c Wed Apr 28 03:46:01 2010 +0200
@@ -25,7 +25,7 @@
         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 @@
                 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 @@
 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 @@
 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 @@
         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 -r c9bb185814c7 -r b01b2c9cf9f4 swk.c
--- a/swk.c Wed Apr 28 03:06:32 2010 +0200
+++ b/swk.c Wed Apr 28 03:46:01 2010 +0200
@@ -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 @@
 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 @@
 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 @@
         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 @@
         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 @@
                 }
                 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 @@
                 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 @@
         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 @@
                 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 @@
                 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 -r c9bb185814c7 -r b01b2c9cf9f4 test.c
--- a/test.c Wed Apr 28 03:06:32 2010 +0200
+++ b/test.c Wed Apr 28 03:46:01 2010 +0200
@@ -13,10 +13,10 @@
 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 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 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(SwkEvent *e) {
- if (e->type == EClick) {
+ if(e->type == EClick) {
                 e->win->boxes = about;
                 swk_update(e->win);
         }
Received on Wed Apr 28 2010 - 01:52:26 UTC

This archive was generated by hypermail 2.2.0 : Wed Apr 28 2010 - 02:00:07 UTC