From f2b8a75f6cb3a9cbbaddd2e16f9ba9105b650142 Mon Sep 17 00:00:00 2001
From: Bert <ber.t@gmx.com>
Date: Mon, 21 Feb 2011 14:59:29 +0100
Subject: [PATCH] Always support thumbs; start thumb-mode with single -t

---
 Makefile  |  2 +-
 main.c    | 75 +++++++++++++++++++++++++++++--------------------------
 options.c |  3 +--
 thumbs.c  | 50 ++++++++++++++++++-------------------
 4 files changed, 66 insertions(+), 64 deletions(-)

diff --git a/Makefile b/Makefile
index 7db2bb2..78c99c9 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
 all: sxiv
 
-VERSION=git-20110220
+VERSION=git-20110221
 
 CC?=gcc
 PREFIX?=/usr/local
diff --git a/main.c b/main.c
index 476617c..e3a2237 100644
--- a/main.c
+++ b/main.c
@@ -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);
 	}
@@ -163,11 +162,11 @@ void update_title() {
 			size = filesize;
 			size_readable(&size, &unit);
 			n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] <%d%%> (%.2f%s) %s",
-									 fileidx + 1, filecnt, (int) (img.zoom * 100.0), size, unit,
-									 filenames[fileidx]);
+			             fileidx + 1, filecnt, (int) (img.zoom * 100.0), size, unit,
+			             filenames[fileidx]);
 		} else {
 			n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] broken: %s",
-									 fileidx + 1, filecnt, filenames[fileidx]);
+			             fileidx + 1, filecnt, filenames[fileidx]);
 		}
 	}
 
@@ -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) {
-					mode = MODE_THUMBS;
-					changed = tns.dirty = 1;
-				}
+				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();
 		}
 
diff --git a/options.c b/options.c
index ac9ae86..19bd7d4 100644
--- a/options.c
+++ b/options.c
@@ -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();
diff --git a/thumbs.c b/thumbs.c
index cae660d..89f48ff 100644
--- a/thumbs.c
+++ b/thumbs.c
@@ -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) {