[hackers] [swk] implement drag scrolling || pancake

From: <hg_AT_suckless.org>
Date: Wed, 28 Apr 2010 10:23:29 +0000 (UTC)

changeset: 26:3bd77e24c15b
tag: tip
user: pancake <pancake_AT_nopcode.org>
date: Wed Apr 28 12:17:04 2010 +0200
files: gi_sdl.c swk.c
description:
implement drag scrolling
some minor source simplifications

diff -r b01b2c9cf9f4 -r 3bd77e24c15b gi_sdl.c
--- a/gi_sdl.c Wed Apr 28 03:46:01 2010 +0200
+++ b/gi_sdl.c Wed Apr 28 12:17:04 2010 +0200
@@ -20,11 +20,11 @@
 static SDL_Event lastev = { .type=-1 };
 
 static void putpixel(int x, int y, Uint32 pixel) {
+ Uint8 *p, *pend;
         int delta, bpp = screen->format->BytesPerPixel;
- Uint8 *p, *pend;
         delta = y * screen->pitch + x * bpp;
         p = (Uint8 *)screen->pixels + delta;
- pend = (Uint8 *)screen->pixels + ((screen->h*screen->w)*bpp);
+ pend = (Uint8 *)screen->pixels + (screen->h*screen->w*bpp);
         if((p<((Uint8 *)screen->pixels)) || (p>=pend))
                 return;
 #if BPP == 8
@@ -104,6 +104,7 @@
 
 SwkEvent *
 swk_gi_event(SwkWindow *w, int dowait) {
+ static int mousedowny, mousedown = 0;
         SDL_Event event;
         SwkEvent *ret = &w->_e;
 
@@ -123,18 +124,32 @@
                 ret->data.expose.w = ret->data.expose.h = 0;
                 break;
         case SDL_MOUSEMOTION:
- ret->type = EMotion;
- ret->data.motion.x = event.motion.x / fs;
- ret->data.motion.y = event.motion.y / fs;
- // fprintf(stderr, "event: motion %d %d\n",
- // event.motion.x, event.motion.y);
+ if(mousedown) {
+ if(event.motion.y>mousedowny+fs) {
+ mousedowny = event.motion.y;
+ swk_scroll_up(w);
+ } else
+ if(event.motion.y<mousedowny-fs) {
+ mousedowny = event.motion.y;
+ swk_scroll_down(w);
+ }
+ } else {
+ ret->type = EMotion;
+ ret->data.motion.x = event.motion.x / fs;
+ ret->data.motion.y = event.motion.y / fs;
+ }
+ break;
+ case SDL_MOUSEBUTTONUP:
+ mousedown = 0;
                 break;
         case SDL_MOUSEBUTTONDOWN:
+ mousedown = 1;
+ mousedowny = event.motion.y;
+ fprintf(stderr, "event: click %d\n", event.button.button);
                 ret->type = EClick;
                 ret->data.click.button = event.button.button;
                 ret->data.click.point.x = event.button.x / fs;
                 ret->data.click.point.y = event.button.y / fs;
- fprintf(stderr, "event: click %d\n", event.button.button);
                 break;
         case SDL_KEYDOWN:
                 ret->data.key.modmask = 0;
@@ -169,7 +184,6 @@
                 }
                 break;
         case SDL_QUIT:
- fprintf(stderr, "event: quit\n");
                 ret->type = ret->type = EQuit;
                 break;
         }
diff -r b01b2c9cf9f4 -r 3bd77e24c15b swk.c
--- a/swk.c Wed Apr 28 03:46:01 2010 +0200
+++ b/swk.c Wed Apr 28 12:17:04 2010 +0200
@@ -69,26 +69,24 @@
         swk_update(w);
 }
 
+
+static SwkBox *
+getscrollbox(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)
+ return b;
+ return w->boxes;
+}
+
 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) {
- b->scroll++;
- return;
- }
- w->boxes->scroll++;
+ getscrollbox(w)->scroll++;
 }
 
 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) {
- b->scroll--;
- return;
- }
- w->boxes->scroll--;
+ getscrollbox(w)->scroll--;
 }
 
 static void swk_fit_row(SwkWindow *w, SwkBox *a, SwkBox *b, int y) {
@@ -131,7 +129,7 @@
                         swk_fit_row(w, b2, b, y);
                         y += x-skip;
                         // vertical align //
- if(x<0) y+=(w->r.h-countrows(b2));
+ if(x<0) y+=w->r.h-countrows(b2);
                         b2 = b+1;
                 }
                 y += b->scroll;
@@ -167,7 +165,7 @@
         case EKey:
                 for(i=0; keys[i].cb; i++) {
                         if(e->data.key.modmask == keys[i].modmask
- && e->data.key.keycode == keys[i].keycode) {
+ && e->data.key.keycode == keys[i].keycode) {
                                 keys[i].cb(e->win);
                                 break;
                         }
@@ -420,8 +418,7 @@
                 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)
                         swk_gi_fill(r, ColorFG, 1);
Received on Wed Apr 28 2010 - 10:23:29 UTC

This archive was generated by hypermail 2.2.0 : Wed Apr 28 2010 - 10:24:03 UTC