Mouse-wheel scrolling in thumb mode

This commit is contained in:
Bert 2011-02-20 00:08:17 +01:00
parent 4cc6ca939c
commit a30901210c
4 changed files with 61 additions and 20 deletions

View file

@ -1,6 +1,6 @@
all: sxiv all: sxiv
VERSION=git-20110219 VERSION=git-20110220
CC?=gcc CC?=gcc
PREFIX?=/usr/local PREFIX?=/usr/local

20
main.c
View file

@ -397,19 +397,25 @@ void on_keypress(XKeyEvent *kev) {
/* move selection */ /* move selection */
case XK_h: case XK_h:
case XK_Left: case XK_Left:
changed = tns_move_selection(&tns, &win, MOVE_LEFT); changed = tns_move_selection(&tns, &win, TNS_LEFT);
break; break;
case XK_j: case XK_j:
case XK_Down: case XK_Down:
changed = tns_move_selection(&tns, &win, MOVE_DOWN); changed = tns_move_selection(&tns, &win, TNS_DOWN);
break; break;
case XK_k: case XK_k:
case XK_Up: case XK_Up:
changed = tns_move_selection(&tns, &win, MOVE_UP); changed = tns_move_selection(&tns, &win, TNS_UP);
break; break;
case XK_l: case XK_l:
case XK_Right: case XK_Right:
changed = tns_move_selection(&tns, &win, MOVE_RIGHT); changed = tns_move_selection(&tns, &win, TNS_RIGHT);
break;
/* scroll */
case XK_bracketleft:
break;
case XK_bracketright:
break; break;
} }
} }
@ -524,6 +530,12 @@ void on_buttonpress(XButtonEvent *bev) {
break; break;
} }
break; break;
case Button4:
changed = tns_scroll(&tns, TNS_UP);
break;
case Button5:
changed = tns_scroll(&tns, TNS_DOWN);
break;
} }
} }

View file

@ -88,21 +88,27 @@ void tns_load(tns_t *tns, win_t *win, const char *filename) {
} }
void tns_check_view(tns_t *tns, Bool scrolled) { void tns_check_view(tns_t *tns, Bool scrolled) {
int r;
if (!tns) if (!tns)
return; return;
tns->first -= tns->first % tns->cols; tns->first -= tns->first % tns->cols;
r = fileidx % tns->cols;
if (scrolled) { if (scrolled) {
/* move selection into visible area */ /* move selection into visible area */
if (fileidx >= tns->first + tns->cols * tns->rows)
fileidx = tns->first + r + tns->cols * (tns->rows - 1);
else if (fileidx < tns->first)
fileidx = tns->first + r;
} else { } else {
/* scroll to selection */ /* scroll to selection */
if (tns->first + tns->cols * tns->rows <= fileidx) { if (tns->first + tns->cols * tns->rows <= fileidx) {
tns->first = fileidx - fileidx % tns->cols - tns->first = fileidx - r - tns->cols * (tns->rows - 1);
tns->cols * (tns->rows - 1);
tns->dirty = 1; tns->dirty = 1;
} else if (tns->first > fileidx) { } else if (tns->first > fileidx) {
tns->first = fileidx - fileidx % tns->cols; tns->first = fileidx - r;
tns->dirty = 1; tns->dirty = 1;
} }
} }
@ -167,7 +173,7 @@ void tns_highlight(tns_t *tns, win_t *win, int n, Bool hl) {
win_draw(win); win_draw(win);
} }
int tns_move_selection(tns_t *tns, win_t *win, movedir_t dir) { int tns_move_selection(tns_t *tns, win_t *win, tnsdir_t dir) {
int old; int old;
if (!tns || !win) if (!tns || !win)
@ -176,19 +182,19 @@ int tns_move_selection(tns_t *tns, win_t *win, movedir_t dir) {
old = fileidx; old = fileidx;
switch (dir) { switch (dir) {
case MOVE_LEFT: case TNS_LEFT:
if (fileidx > 0) if (fileidx > 0)
--fileidx; --fileidx;
break; break;
case MOVE_RIGHT: case TNS_RIGHT:
if (fileidx < tns->cnt - 1) if (fileidx < tns->cnt - 1)
++fileidx; ++fileidx;
break; break;
case MOVE_UP: case TNS_UP:
if (fileidx >= tns->cols) if (fileidx >= tns->cols)
fileidx -= tns->cols; fileidx -= tns->cols;
break; break;
case MOVE_DOWN: case TNS_DOWN:
if (fileidx + tns->cols < tns->cnt) if (fileidx + tns->cols < tns->cnt)
fileidx += tns->cols; fileidx += tns->cols;
break; break;
@ -204,6 +210,28 @@ int tns_move_selection(tns_t *tns, win_t *win, movedir_t dir) {
return fileidx != old; return fileidx != old;
} }
int tns_scroll(tns_t *tns, tnsdir_t dir) {
int old;
if (!tns)
return 0;
old = tns->first;
if (dir == TNS_DOWN && tns->first + tns->cols * tns->rows < tns->cnt)
tns->first += tns->cols;
else if (dir == TNS_UP && tns->first >= tns->cols)
tns->first -= tns->cols;
if (tns->first != old) {
tns_check_view(tns, True);
tns->dirty = 1;
return 1;
} else {
return 0;
}
}
int tns_translate(tns_t *tns, int x, int y) { int tns_translate(tns_t *tns, int x, int y) {
int n; int n;
thumb_t *t; thumb_t *t;

View file

@ -21,12 +21,12 @@
#include "window.h" #include "window.h"
typedef enum movedir_e { typedef enum tnsdir_e {
MOVE_LEFT = 0, TNS_LEFT = 0,
MOVE_RIGHT, TNS_RIGHT,
MOVE_UP, TNS_UP,
MOVE_DOWN TNS_DOWN
} movedir_t; } tnsdir_t;
typedef struct thumb_s { typedef struct thumb_s {
Pixmap pm; Pixmap pm;
@ -56,7 +56,8 @@ void tns_load(tns_t*, win_t*, const char*);
void tns_render(tns_t*, win_t*); void tns_render(tns_t*, win_t*);
void tns_highlight(tns_t*, win_t*, int, Bool); void tns_highlight(tns_t*, win_t*, int, Bool);
int tns_move_selection(tns_t*, win_t*, movedir_t); int tns_move_selection(tns_t*, win_t*, tnsdir_t);
int tns_scroll(tns_t*, tnsdir_t);
int tns_translate(tns_t*, int, int); int tns_translate(tns_t*, int, int);