Krolyxon
Commit 72e8baf

Branches

Refs →

nsxivthumbs.c

251 lines · 5.4 KB · C

1/* sxiv: thumbs.c
2 * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19#include <stdlib.h>
20#include <string.h>
21
22#include <Imlib2.h>
23
24#include "config.h"
25#include "thumbs.h"
26#include "util.h"
27
28extern int fileidx;
29extern Imlib_Image *im_broken;
30
31const int thumb_dim = THUMB_SIZE + 10;
32
33void tns_init(tns_t *tns, int cnt) {
34 if (!tns)
35 return;
36
37 tns->cnt = tns->first = 0;
38 tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t));
39 memset(tns->thumbs, 0, cnt * sizeof(thumb_t));
40 tns->dirty = 0;
41}
42
43void tns_free(tns_t *tns, win_t *win) {
44 int i;
45
46 if (!tns)
47 return;
48
49 for (i = 0; i < tns->cnt; ++i)
50 win_free_pixmap(win, tns->thumbs[i].pm);
51
52 free(tns->thumbs);
53 tns->thumbs = NULL;
54}
55
56void tns_load(tns_t *tns, win_t *win, const char *filename) {
57 int w, h;
58 float z, zw, zh;
59 thumb_t *t;
60 Imlib_Image *im;
61
62 if (!tns || !win || !filename)
63 return;
64
65 if ((im = imlib_load_image(filename)))
66 imlib_context_set_image(im);
67 else
68 imlib_context_set_image(im_broken);
69
70 w = imlib_image_get_width();
71 h = imlib_image_get_height();
72 zw = (float) THUMB_SIZE / (float) w;
73 zh = (float) THUMB_SIZE / (float) h;
74 z = MIN(zw, zh);
75 if (!im && z > 1.0)
76 z = 1.0;
77
78 t = &tns->thumbs[tns->cnt++];
79 t->w = z * w;
80 t->h = z * h;
81
82 t->pm = win_create_pixmap(win, t->w, t->h);
83 imlib_context_set_drawable(t->pm);
84 imlib_render_image_part_on_drawable_at_size(0, 0, w, h,
85 0, 0, t->w, t->h);
86 imlib_free_image();
87 tns->dirty = 1;
88}
89
90void tns_check_view(tns_t *tns, Bool scrolled) {
91 int r;
92
93 if (!tns)
94 return;
95
96 tns->first -= tns->first % tns->cols;
97 r = fileidx % tns->cols;
98
99 if (scrolled) {
100 /* 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;
105 } else {
106 /* scroll to selection */
107 if (tns->first + tns->cols * tns->rows <= fileidx) {
108 tns->first = fileidx - r - tns->cols * (tns->rows - 1);
109 tns->dirty = 1;
110 } else if (tns->first > fileidx) {
111 tns->first = fileidx - r;
112 tns->dirty = 1;
113 }
114 }
115}
116
117void tns_render(tns_t *tns, win_t *win) {
118 int i, cnt, r, x, y;
119 thumb_t *t;
120
121 if (!tns || !tns->dirty || !win)
122 return;
123
124 win_clear(win);
125
126 tns->cols = MAX(1, win->w / thumb_dim);
127 tns->rows = MAX(1, win->h / thumb_dim);
128
129 if (tns->cnt < tns->cols * tns->rows) {
130 tns->first = 0;
131 cnt = tns->cnt;
132 } else {
133 tns_check_view(tns, False);
134 cnt = tns->cols * tns->rows;
135 if ((r = tns->first + cnt - tns->cnt) >= tns->cols)
136 tns->first -= r - r % tns->cols;
137 if (r > 0)
138 cnt -= r % tns->cols;
139 }
140
141 r = cnt % tns->cols ? 1 : 0;
142 tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5;
143 tns->y = y = (win->h - (cnt / tns->cols + r) * thumb_dim) / 2 + 5;
144
145 for (i = 0; i < cnt; ++i) {
146 t = &tns->thumbs[tns->first + i];
147 t->x = x + (THUMB_SIZE - t->w) / 2;
148 t->y = y + (THUMB_SIZE - t->h) / 2;
149 win_draw_pixmap(win, t->pm, t->x, t->y, t->w, t->h);
150 if ((i + 1) % tns->cols == 0) {
151 x = tns->x;
152 y += thumb_dim;
153 } else {
154 x += thumb_dim;
155 }
156 }
157
158 tns->dirty = 0;
159 tns_highlight(tns, win, fileidx, True);
160}
161
162void tns_highlight(tns_t *tns, win_t *win, int n, Bool hl) {
163 thumb_t *t;
164
165 if (!tns || !win)
166 return;
167
168 if (n >= 0 && n < tns->cnt) {
169 t = &tns->thumbs[n];
170 win_draw_rect(win, t->x - 2, t->y - 2, t->w + 4, t->h + 4, hl);
171 }
172
173 win_draw(win);
174}
175
176int tns_move_selection(tns_t *tns, win_t *win, tnsdir_t dir) {
177 int old;
178
179 if (!tns || !win)
180 return 0;
181
182 old = fileidx;
183
184 switch (dir) {
185 case TNS_LEFT:
186 if (fileidx > 0)
187 --fileidx;
188 break;
189 case TNS_RIGHT:
190 if (fileidx < tns->cnt - 1)
191 ++fileidx;
192 break;
193 case TNS_UP:
194 if (fileidx >= tns->cols)
195 fileidx -= tns->cols;
196 break;
197 case TNS_DOWN:
198 if (fileidx + tns->cols < tns->cnt)
199 fileidx += tns->cols;
200 break;
201 }
202
203 if (fileidx != old) {
204 tns_highlight(tns, win, old, False);
205 tns_check_view(tns, False);
206 if (!tns->dirty)
207 tns_highlight(tns, win, fileidx, True);
208 }
209
210 return fileidx != old;
211}
212
213int 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 tns_check_view(tns, True);
224 tns->dirty = 1;
225 } else if (dir == TNS_UP && tns->first >= tns->cols) {
226 tns->first -= tns->cols;
227 tns_check_view(tns, True);
228 tns->dirty = 1;
229 }
230
231 return tns->first != old;
232}
233
234int tns_translate(tns_t *tns, int x, int y) {
235 int n;
236 thumb_t *t;
237
238 if (!tns || x < tns->x || y < tns->y)
239 return -1;
240
241 n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
242 (x - tns->x) / thumb_dim;
243
244 if (n < tns->cnt) {
245 t = &tns->thumbs[n];
246 if (x >= t->x && x <= t->x + t->w && y >= t->y && y <= t->y + t->h)
247 return n;
248 }
249
250 return -1;
251}