Always support thumbs; start thumb-mode with single -t

This commit is contained in:
Bert 2011-02-21 14:59:29 +01:00
parent 72e8baf13e
commit f2b8a75f6c
4 changed files with 66 additions and 64 deletions

View file

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

65
main.c
View file

@ -129,15 +129,14 @@ int main(int argc, char **argv) {
win_open(&win);
img_init(&img, &win);
if (options->thumbnails)
tns_init(&tns, filecnt);
if (options->thumbnails == 2) {
if (options->thumbnails) {
mode = MODE_THUMBS;
tns_init(&tns, filecnt);
win_clear(&win);
win_draw(&win);
} else {
mode = MODE_NORMAL;
tns.thumbs = NULL;
load_image();
img_render(&img, &win);
}
@ -309,6 +308,18 @@ void on_keypress(XKeyEvent *kev) {
changed = load_image();
}
break;
case XK_g:
if (fileidx != 0) {
fileidx = 0;
changed = load_image();
}
break;
case XK_G:
if (fileidx != filecnt - 1) {
fileidx = filecnt - 1;
changed = load_image();
}
break;
/* zooming */
case XK_plus:
@ -368,10 +379,11 @@ void on_keypress(XKeyEvent *kev) {
/* switch to thumnail mode */
case XK_Return:
if (options->thumbnails) {
if (!tns.thumbs)
tns_init(&tns, filecnt);
mode = MODE_THUMBS;
tns.sel = fileidx;
changed = tns.dirty = 1;
}
break;
/* miscellaneous */
@ -388,6 +400,7 @@ void on_keypress(XKeyEvent *kev) {
switch (ksym) {
/* open selected image */
case XK_Return:
fileidx = tns.sel;
load_image();
mode = MODE_NORMAL;
win_set_cursor(&win, CURSOR_ARROW);
@ -411,6 +424,17 @@ void on_keypress(XKeyEvent *kev) {
case XK_Right:
changed = tns_move_selection(&tns, &win, TNS_RIGHT);
break;
case XK_g:
if (tns.sel != 0) {
tns.sel = 0;
changed = tns.dirty = 1;
}
break;
case XK_G:
if (tns.sel != tns.cnt - 1) {
tns.sel = tns.cnt - 1;
changed = tns.dirty = 1;
}
}
}
@ -423,27 +447,6 @@ void on_keypress(XKeyEvent *kev) {
cleanup();
exit(0);
case XK_g:
if (fileidx != 0) {
fileidx = 0;
changed = 1;
if (mode == MODE_NORMAL)
load_image();
else
tns.dirty = 1;
}
break;
case XK_G:
if (fileidx != filecnt - 1) {
fileidx = filecnt - 1;
changed = 1;
if (mode == MODE_NORMAL)
load_image();
else
tns.dirty = 1;
}
break;
case XK_f:
win_toggle_fullscreen(&win);
/* render on next configurenotify */
@ -511,14 +514,15 @@ void on_buttonpress(XButtonEvent *bev) {
switch (bev->button) {
case Button1:
if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
if (sel == fileidx) {
if (sel == tns.sel) {
fileidx = tns.sel;
load_image();
mode = MODE_NORMAL;
win_set_cursor(&win, CURSOR_ARROW);
} else {
tns_highlight(&tns, &win, fileidx, False);
tns_highlight(&tns, &win, tns.sel, False);
tns_highlight(&tns, &win, sel, True);
fileidx = sel;
tns.sel = sel;
}
changed = 1;
break;
@ -585,6 +589,7 @@ void run() {
FD_SET(xfd, &fds);
if (!XPending(win.env.dpy) && !select(xfd + 1, &fds, 0, 0, &t))
/* timeout fired */
redraw();
}

View file

@ -87,8 +87,7 @@ void parse_options(int argc, char **argv) {
_options.scalemode = SCALE_FIT;
break;
case 't':
if (_options.thumbnails < 2)
++_options.thumbnails;
_options.thumbnails = 1;
break;
case 'v':
print_version();

View file

@ -25,16 +25,14 @@
#include "thumbs.h"
#include "util.h"
extern int fileidx;
extern Imlib_Image *im_broken;
const int thumb_dim = THUMB_SIZE + 10;
void tns_init(tns_t *tns, int cnt) {
if (!tns)
return;
tns->cnt = tns->first = 0;
tns->cnt = tns->first = tns->sel = 0;
tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t));
memset(tns->thumbs, 0, cnt * sizeof(thumb_t));
tns->dirty = 0;
@ -43,7 +41,7 @@ void tns_init(tns_t *tns, int cnt) {
void tns_free(tns_t *tns, win_t *win) {
int i;
if (!tns)
if (!tns || !tns->thumbs)
return;
for (i = 0; i < tns->cnt; ++i)
@ -94,21 +92,21 @@ void tns_check_view(tns_t *tns, Bool scrolled) {
return;
tns->first -= tns->first % tns->cols;
r = fileidx % tns->cols;
r = tns->sel % tns->cols;
if (scrolled) {
/* 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;
if (tns->sel >= tns->first + tns->cols * tns->rows)
tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
else if (tns->sel < tns->first)
tns->sel = tns->first + r;
} else {
/* scroll to selection */
if (tns->first + tns->cols * tns->rows <= fileidx) {
tns->first = fileidx - r - tns->cols * (tns->rows - 1);
if (tns->first + tns->cols * tns->rows <= tns->sel) {
tns->first = tns->sel - r - tns->cols * (tns->rows - 1);
tns->dirty = 1;
} else if (tns->first > fileidx) {
tns->first = fileidx - r;
} else if (tns->first > tns->sel) {
tns->first = tns->sel - r;
tns->dirty = 1;
}
}
@ -156,7 +154,7 @@ void tns_render(tns_t *tns, win_t *win) {
}
tns->dirty = 0;
tns_highlight(tns, win, fileidx, True);
tns_highlight(tns, win, tns->sel, True);
}
void tns_highlight(tns_t *tns, win_t *win, int n, Bool hl) {
@ -179,35 +177,35 @@ int tns_move_selection(tns_t *tns, win_t *win, tnsdir_t dir) {
if (!tns || !win)
return 0;
old = fileidx;
old = tns->sel;
switch (dir) {
case TNS_LEFT:
if (fileidx > 0)
--fileidx;
if (tns->sel > 0)
--tns->sel;
break;
case TNS_RIGHT:
if (fileidx < tns->cnt - 1)
++fileidx;
if (tns->sel < tns->cnt - 1)
++tns->sel;
break;
case TNS_UP:
if (fileidx >= tns->cols)
fileidx -= tns->cols;
if (tns->sel >= tns->cols)
tns->sel -= tns->cols;
break;
case TNS_DOWN:
if (fileidx + tns->cols < tns->cnt)
fileidx += tns->cols;
if (tns->sel + tns->cols < tns->cnt)
tns->sel += tns->cols;
break;
}
if (fileidx != old) {
if (tns->sel != old) {
tns_highlight(tns, win, old, False);
tns_check_view(tns, False);
if (!tns->dirty)
tns_highlight(tns, win, fileidx, True);
tns_highlight(tns, win, tns->sel, True);
}
return fileidx != old;
return tns->sel != old;
}
int tns_scroll(tns_t *tns, tnsdir_t dir) {