Krolyxon
Commit a152f82

Branches

Refs →

nsxiva152f82

Sync thumb selection & fileidx; check thumb view

Author
Bert
Date
2011-02-19 22:12 +0100
Parent
2194df8
Commit
a152f82ce6e010859232f83f466a4c1628bb18ed

+76 −56

4 files changed

  1. M Makefile +1 −1
  2. M main.c +3 −10
  3. M thumbs.c +71 −44
  4. M thumbs.h +1 −1
M Makefile View
@@ -4,7 +4,7 @@ VERSION=git-20110219
44
55 CC?=gcc
66 PREFIX?=/usr/local
7−CFLAGS+= -Wall -pedantic -DVERSION=\"$(VERSION)\"
7+CFLAGS+= -Wall -pedantic -DVERSION=\"$(VERSION)\" -g
88 LDFLAGS+=
99 LIBS+= -lX11 -lImlib2
1010
M main.c View
@@ -156,8 +156,8 @@ void update_title() {
156156
157157 if (mode == MODE_THUMBS) {
158158 n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] %s",
159− tns.cnt ? tns.sel + 1 : 0, tns.cnt,
160− tns.cnt ? filenames[tns.sel] : "");
159+ tns.cnt ? fileidx + 1 : 0, tns.cnt,
160+ tns.cnt ? filenames[fileidx] : "");
161161 } else {
162162 if (img.valid) {
163163 size = filesize;
@@ -272,7 +272,7 @@ void on_keypress(XKeyEvent *kev) {
272272 unsigned int w, h;
273273 char key;
274274 KeySym ksym;
275− int changed, sel;
275+ int changed;
276276
277277 if (!kev)
278278 return;
@@ -381,10 +381,6 @@ void on_keypress(XKeyEvent *kev) {
381381 /* switch to thumnail mode */
382382 case XK_Return:
383383 if (options->thumbnails) {
384− if (fileidx < tns.cnt)
385− tns.sel = fileidx;
386− else
387− tns.sel = 0;
388384 mode = MODE_THUMBS;
389385 changed = tns.dirty = 1;
390386 }
@@ -401,12 +397,9 @@ void on_keypress(XKeyEvent *kev) {
401397 }
402398 } else {
403399 /* thumbnail mode */
404− sel = tns.sel;
405−
406400 switch (ksym) {
407401 /* open selected image */
408402 case XK_Return:
409− fileidx = sel;
410403 load_image();
411404 mode = MODE_NORMAL;
412405 win_set_cursor(&win, CURSOR_ARROW);
M thumbs.c View
@@ -25,14 +25,16 @@
2525 #include "thumbs.h"
2626 #include "util.h"
2727
28+extern int fileidx;
2829 extern Imlib_Image *im_broken;
30+
2931 const int thumb_dim = THUMB_SIZE + 10;
3032
3133 void tns_init(tns_t *tns, int cnt) {
3234 if (!tns)
3335 return;
3436
35− tns->cnt = tns->first = tns->sel = 0;
37+ tns->cnt = tns->first = 0;
3638 tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t));
3739 memset(tns->thumbs, 0, cnt * sizeof(thumb_t));
3840 tns->dirty = 0;
@@ -82,37 +84,64 @@ void tns_load(tns_t *tns, win_t *win, const char *filename) {
8284 imlib_render_image_part_on_drawable_at_size(0, 0, w, h,
8385 0, 0, t->w, t->h);
8486 imlib_free_image();
85−
8687 tns->dirty = 1;
8788 }
8889
90+void tns_check_view(tns_t *tns, Bool scrolled) {
91+ if (!tns)
92+ return;
93+
94+ tns->first -= tns->first % tns->cols;
95+
96+ if (scrolled) {
97+ /* move selection into visible area */
98+ } else {
99+ /* scroll to selection */
100+ if (tns->first + tns->cols * tns->rows <= fileidx) {
101+ tns->first = fileidx - fileidx % tns->cols -
102+ tns->cols * (tns->rows - 1);
103+ tns->dirty = 1;
104+ } else if (tns->first > fileidx) {
105+ tns->first = fileidx - fileidx % tns->cols;
106+ tns->dirty = 1;
107+ }
108+ }
109+}
110+
89111 void tns_render(tns_t *tns, win_t *win) {
90− int i, cnt, x, y;
112+ int i, cnt, r, x, y;
113+ thumb_t *t;
91114
92115 if (!tns || !tns->dirty || !win)
93116 return;
94117
118+ win_clear(win);
119+
95120 tns->cols = MAX(1, win->w / thumb_dim);
96121 tns->rows = MAX(1, win->h / thumb_dim);
97122
98− cnt = tns->cols * tns->rows;
99− if (tns->first && tns->first + cnt > tns->cnt)
100− tns->first = MAX(0, tns->cnt - cnt);
101− cnt = MIN(tns->first + cnt, tns->cnt);
102−
103− win_clear(win);
123+ if (tns->cnt < tns->cols * tns->rows) {
124+ tns->first = 0;
125+ cnt = tns->cnt;
126+ } else {
127+ tns_check_view(tns, False);
128+ cnt = tns->cols * tns->rows;
129+ if ((r = tns->first + cnt - tns->cnt) >= tns->cols)
130+ tns->first -= r - r % tns->cols;
131+ if (r > 0)
132+ cnt -= r % tns->cols;
133+ }
104134
105− i = cnt % tns->cols ? 1 : 0;
135+ r = cnt % tns->cols ? 1 : 0;
106136 tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5;
107− tns->y = y = (win->h - (cnt / tns->cols + i) * thumb_dim) / 2 + 5;
108− i = tns->first;
109−
110− while (i < cnt) {
111− tns->thumbs[i].x = x + (THUMB_SIZE - tns->thumbs[i].w) / 2;
112− tns->thumbs[i].y = y + (THUMB_SIZE - tns->thumbs[i].h) / 2;
113− win_draw_pixmap(win, tns->thumbs[i].pm, tns->thumbs[i].x,
114− tns->thumbs[i].y, tns->thumbs[i].w, tns->thumbs[i].h);
115− if (++i % tns->cols == 0) {
137+ tns->y = y = (win->h - (cnt / tns->cols + r) * thumb_dim) / 2 + 5;
138+
139+ for (i = 0; i < cnt; ++i) {
140+ t = &tns->thumbs[tns->first + i];
141+ t->x = x + (THUMB_SIZE - t->w) / 2;
142+ t->y = y + (THUMB_SIZE - t->h) / 2;
143+ win_draw_pixmap(win, t->pm, t->x, t->y, t->w, t->h);
144+ if ((i + 1) % tns->cols == 0) {
116145 x = tns->x;
117146 y += thumb_dim;
118147 } else {
@@ -121,61 +150,58 @@ void tns_render(tns_t *tns, win_t *win) {
121150 }
122151
123152 tns->dirty = 0;
124−
125− tns_highlight(tns, win, -1);
153+ tns_highlight(tns, win, fileidx, True);
126154 }
127155
128−void tns_highlight(tns_t *tns, win_t *win, int old) {
156+void tns_highlight(tns_t *tns, win_t *win, int n, Bool hl) {
129157 thumb_t *t;
130158
131159 if (!tns || !win)
132160 return;
133161
134− if (old >= 0 && old < tns->cnt) {
135− t = &tns->thumbs[old];
136− win_draw_rect(win, t->x - 2, t->y - 2, t->w + 4, t->h + 4, False);
137− }
138− if (tns->sel < tns->cnt) {
139− t = &tns->thumbs[tns->sel];
140− win_draw_rect(win, t->x - 2, t->y - 2, t->w + 4, t->h + 4, True);
162+ if (n >= 0 && n < tns->cnt) {
163+ t = &tns->thumbs[n];
164+ win_draw_rect(win, t->x - 2, t->y - 2, t->w + 4, t->h + 4, hl);
141165 }
142166
143167 win_draw(win);
144168 }
145169
146170 int tns_move_selection(tns_t *tns, win_t *win, movedir_t dir) {
147− int sel, old;
171+ int old;
148172
149173 if (!tns || !win)
150174 return 0;
151175
152− sel = old = tns->sel;
176+ old = fileidx;
153177
154178 switch (dir) {
155179 case MOVE_LEFT:
156− if (sel % tns->cols > 0)
157− --sel;
180+ if (fileidx > 0)
181+ --fileidx;
158182 break;
159183 case MOVE_RIGHT:
160− if (sel % tns->cols < tns->cols - 1 && sel < tns->cnt - 1)
161− ++sel;
184+ if (fileidx < tns->cnt - 1)
185+ ++fileidx;
162186 break;
163187 case MOVE_UP:
164− if (sel / tns->cols > 0)
165− sel -= tns->cols;
188+ if (fileidx >= tns->cols)
189+ fileidx -= tns->cols;
166190 break;
167191 case MOVE_DOWN:
168− if (sel / tns->cols < tns->rows - 1 && sel + tns->cols < tns->cnt)
169− sel += tns->cols;
192+ if (fileidx + tns->cols < tns->cnt)
193+ fileidx += tns->cols;
170194 break;
171195 }
172196
173− if (sel != old && tns->thumbs[sel].x != 0) {
174− tns->sel = sel;
175− tns_highlight(tns, win, old);
197+ if (fileidx != old) {
198+ tns_highlight(tns, win, old, False);
199+ tns_check_view(tns, False);
200+ if (!tns->dirty)
201+ tns_highlight(tns, win, fileidx, True);
176202 }
177203
178− return sel != old;
204+ return fileidx != old;
179205 }
180206
181207 int tns_translate(tns_t *tns, int x, int y) {
@@ -185,7 +211,8 @@ int tns_translate(tns_t *tns, int x, int y) {
185211 if (!tns || x < tns->x || y < tns->y)
186212 return -1;
187213
188− n = (y - tns->y) / thumb_dim * tns->cols + (x - tns->x) / thumb_dim;
214+ n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
215+ (x - tns->x) / thumb_dim;
189216
190217 if (n < tns->cnt) {
191218 t = &tns->thumbs[n];
M thumbs.h View
@@ -54,7 +54,7 @@ void tns_free(tns_t*, win_t*);
5454 void tns_load(tns_t*, win_t*, const char*);
5555
5656 void tns_render(tns_t*, win_t*);
57−void tns_highlight(tns_t*, win_t*, int);
57+void tns_highlight(tns_t*, win_t*, int, Bool);
5858
5959 int tns_move_selection(tns_t*, win_t*, movedir_t);
6060