commit 514d165468205162fef7756b210f2b86bb4c27f0
parent eade25b9e11f451f15d7ebc3af029574e5a7b0dd
Author: pancake <pancake@nopcode.org>
Date: Wed, 28 Apr 2010 12:17:04 +0200
implement drag scrolling
some minor source simplifications
Diffstat:
gi_sdl.c | | | 32 | +++++++++++++++++++++++--------- |
swk.c | | | 33 | +++++++++++++++------------------ |
2 files changed, 38 insertions(+), 27 deletions(-)
diff --git a/gi_sdl.c b/gi_sdl.c
@@ -20,11 +20,11 @@ static int has_event = 0;
static SDL_Event lastev = { .type=-1 };
static void putpixel(int x, int y, Uint32 pixel) {
- int delta, bpp = screen->format->BytesPerPixel;
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);
+ pend = (Uint8 *)screen->pixels + (screen->h*screen->w*bpp);
if((p<((Uint8 *)screen->pixels)) || (p>=pend))
return;
#if BPP == 8
@@ -104,6 +104,7 @@ swk_gi_has_event(SwkWindow *w) {
SwkEvent *
swk_gi_event(SwkWindow *w, int dowait) {
+ static int mousedowny, mousedown = 0;
SDL_Event event;
SwkEvent *ret = &w->_e;
@@ -123,18 +124,32 @@ swk_gi_event(SwkWindow *w, int dowait) {
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 @@ swk_gi_event(SwkWindow *w, int dowait) {
}
break;
case SDL_QUIT:
- fprintf(stderr, "event: quit\n");
ret->type = ret->type = EQuit;
break;
}
diff --git a/swk.c b/swk.c
@@ -69,26 +69,24 @@ swk_fontsize_decrease(SwkWindow *w) {
swk_update(w);
}
-void
-swk_scroll_up(SwkWindow *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) {
- b->scroll++;
- return;
- }
- w->boxes->scroll++;
+ 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) {
+ 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(SwkWindow *w) {
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 @@ swk_handle_event(SwkEvent *e) {
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 @@ 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)
swk_gi_fill(r, ColorFG, 1);