Krolyxon
Commit a309012

Branches

Refs →

nsxiva309012

Mouse-wheel scrolling in thumb mode

Author
Bert
Date
2011-02-20 00:08 +0100
Parent
4cc6ca9
Commit
a30901210cfde971bac780b5c0bbdeda215ea809

+61 −20

4 files changed

  1. M Makefile +1 −1
  2. M main.c +16 −4
  3. M thumbs.c +36 −8
  4. M thumbs.h +8 −7
M Makefile View
@@ -1,6 +1,6 @@
11 all: sxiv
22
3−VERSION=git-20110219
3+VERSION=git-20110220
44
55 CC?=gcc
66 PREFIX?=/usr/local
M main.c View
@@ -397,19 +397,25 @@ void on_keypress(XKeyEvent *kev) {
397397 /* move selection */
398398 case XK_h:
399399 case XK_Left:
400− changed = tns_move_selection(&tns, &win, MOVE_LEFT);
400+ changed = tns_move_selection(&tns, &win, TNS_LEFT);
401401 break;
402402 case XK_j:
403403 case XK_Down:
404− changed = tns_move_selection(&tns, &win, MOVE_DOWN);
404+ changed = tns_move_selection(&tns, &win, TNS_DOWN);
405405 break;
406406 case XK_k:
407407 case XK_Up:
408− changed = tns_move_selection(&tns, &win, MOVE_UP);
408+ changed = tns_move_selection(&tns, &win, TNS_UP);
409409 break;
410410 case XK_l:
411411 case XK_Right:
412− changed = tns_move_selection(&tns, &win, MOVE_RIGHT);
412+ changed = tns_move_selection(&tns, &win, TNS_RIGHT);
413+ break;
414+
415+ /* scroll */
416+ case XK_bracketleft:
417+ break;
418+ case XK_bracketright:
413419 break;
414420 }
415421 }
@@ -524,6 +530,12 @@ void on_buttonpress(XButtonEvent *bev) {
524530 break;
525531 }
526532 break;
533+ case Button4:
534+ changed = tns_scroll(&tns, TNS_UP);
535+ break;
536+ case Button5:
537+ changed = tns_scroll(&tns, TNS_DOWN);
538+ break;
527539 }
528540 }
529541
M thumbs.c View
@@ -88,21 +88,27 @@ void tns_load(tns_t *tns, win_t *win, const char *filename) {
8888 }
8989
9090 void tns_check_view(tns_t *tns, Bool scrolled) {
91+ int r;
92+
9193 if (!tns)
9294 return;
9395
9496 tns->first -= tns->first % tns->cols;
97+ r = fileidx % tns->cols;
9598
9699 if (scrolled) {
97100 /* move selection into visible area */
101+ if (fileidx >= tns->first + tns->cols * tns->rows)
102+ fileidx = tns->first + r + tns->cols * (tns->rows - 1);
103+ else if (fileidx < tns->first)
104+ fileidx = tns->first + r;
98105 } else {
99106 /* scroll to selection */
100107 if (tns->first + tns->cols * tns->rows <= fileidx) {
101− tns->first = fileidx - fileidx % tns->cols -
102− tns->cols * (tns->rows - 1);
108+ tns->first = fileidx - r - tns->cols * (tns->rows - 1);
103109 tns->dirty = 1;
104110 } else if (tns->first > fileidx) {
105− tns->first = fileidx - fileidx % tns->cols;
111+ tns->first = fileidx - r;
106112 tns->dirty = 1;
107113 }
108114 }
@@ -167,7 +173,7 @@ void tns_highlight(tns_t *tns, win_t *win, int n, Bool hl) {
167173 win_draw(win);
168174 }
169175
170−int tns_move_selection(tns_t *tns, win_t *win, movedir_t dir) {
176+int tns_move_selection(tns_t *tns, win_t *win, tnsdir_t dir) {
171177 int old;
172178
173179 if (!tns || !win)
@@ -176,19 +182,19 @@ int tns_move_selection(tns_t *tns, win_t *win, movedir_t dir) {
176182 old = fileidx;
177183
178184 switch (dir) {
179− case MOVE_LEFT:
185+ case TNS_LEFT:
180186 if (fileidx > 0)
181187 --fileidx;
182188 break;
183− case MOVE_RIGHT:
189+ case TNS_RIGHT:
184190 if (fileidx < tns->cnt - 1)
185191 ++fileidx;
186192 break;
187− case MOVE_UP:
193+ case TNS_UP:
188194 if (fileidx >= tns->cols)
189195 fileidx -= tns->cols;
190196 break;
191− case MOVE_DOWN:
197+ case TNS_DOWN:
192198 if (fileidx + tns->cols < tns->cnt)
193199 fileidx += tns->cols;
194200 break;
@@ -204,6 +210,28 @@ int tns_move_selection(tns_t *tns, win_t *win, movedir_t dir) {
204210 return fileidx != old;
205211 }
206212
213+int tns_scroll(tns_t *tns, tnsdir_t dir) {
214+ int old;
215+
216+ if (!tns)
217+ return 0;
218+
219+ old = tns->first;
220+
221+ if (dir == TNS_DOWN && tns->first + tns->cols * tns->rows < tns->cnt)
222+ tns->first += tns->cols;
223+ else if (dir == TNS_UP && tns->first >= tns->cols)
224+ tns->first -= tns->cols;
225+
226+ if (tns->first != old) {
227+ tns_check_view(tns, True);
228+ tns->dirty = 1;
229+ return 1;
230+ } else {
231+ return 0;
232+ }
233+}
234+
207235 int tns_translate(tns_t *tns, int x, int y) {
208236 int n;
209237 thumb_t *t;
M thumbs.h View
@@ -21,12 +21,12 @@
2121
2222 #include "window.h"
2323
24−typedef enum movedir_e {
25− MOVE_LEFT = 0,
26− MOVE_RIGHT,
27− MOVE_UP,
28− MOVE_DOWN
29−} movedir_t;
24+typedef enum tnsdir_e {
25+ TNS_LEFT = 0,
26+ TNS_RIGHT,
27+ TNS_UP,
28+ TNS_DOWN
29+} tnsdir_t;
3030
3131 typedef struct thumb_s {
3232 Pixmap pm;
@@ -56,7 +56,8 @@ void tns_load(tns_t*, win_t*, const char*);
5656 void tns_render(tns_t*, win_t*);
5757 void tns_highlight(tns_t*, win_t*, int, Bool);
5858
59−int tns_move_selection(tns_t*, win_t*, movedir_t);
59+int tns_move_selection(tns_t*, win_t*, tnsdir_t);
60+int tns_scroll(tns_t*, tnsdir_t);
6061
6162 int tns_translate(tns_t*, int, int);
6263