Krolyxon
Commit 83bdf67

Branches

Refs →

nsxivthumbs.c

313 lines · 6.6 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#include <sys/types.h>
22#include <sys/stat.h>
23#include <unistd.h>
24
25#include "config.h"
26#include "thumbs.h"
27#include "util.h"
28
29extern Imlib_Image *im_invalid;
30const int thumb_dim = THUMB_SIZE + 10;
31
32int tns_cache_enabled();
33void tns_cache_write(thumb_t*, Bool);
34
35void tns_init(tns_t *tns, int cnt) {
36 if (!tns)
37 return;
38
39 tns->cnt = tns->first = tns->sel = 0;
40 tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t));
41 memset(tns->thumbs, 0, cnt * sizeof(thumb_t));
42 tns->cap = cnt;
43 tns->dirty = 0;
44}
45
46void tns_free(tns_t *tns, win_t *win) {
47 int i;
48 Bool cache;
49
50 if (!tns || !tns->thumbs)
51 return;
52
53 cache = tns_cache_enabled();
54
55 for (i = 0; i < tns->cnt; ++i) {
56 if (tns->thumbs[i].im) {
57 if (cache)
58 tns_cache_write(&tns->thumbs[i], False);
59 imlib_context_set_image(tns->thumbs[i].im);
60 imlib_free_image();
61 }
62 }
63
64 free(tns->thumbs);
65 tns->thumbs = NULL;
66}
67
68void tns_load(tns_t *tns, win_t *win, int n, const char *filename) {
69 int w, h;
70 float z, zw, zh;
71 thumb_t *t;
72 Imlib_Image *im;
73
74 if (!tns || !win || !filename)
75 return;
76
77 if (n >= tns->cap)
78 return;
79 else if (n >= tns->cnt)
80 tns->cnt = n + 1;
81
82 t = &tns->thumbs[n];
83
84 if (t->im) {
85 imlib_context_set_image(t->im);
86 imlib_free_image();
87 }
88
89 if ((im = imlib_load_image(filename)))
90 imlib_context_set_image(im);
91 else
92 imlib_context_set_image(im_invalid);
93
94 w = imlib_image_get_width();
95 h = imlib_image_get_height();
96
97 if (im) {
98 t->filename = filename;
99 zw = (float) THUMB_SIZE / (float) w;
100 zh = (float) THUMB_SIZE / (float) h;
101 z = MIN(zw, zh);
102 } else {
103 t->filename = NULL;
104 z = 1.0;
105 }
106
107 t->w = z * w;
108 t->h = z * h;
109
110 imlib_context_set_anti_alias(1);
111 if (!(t->im = imlib_create_cropped_scaled_image(0, 0, w, h, t->w, t->h)))
112 die("could not allocate memory");
113 if (im)
114 imlib_free_image_and_decache();
115
116 tns->dirty = 1;
117}
118
119void tns_check_view(tns_t *tns, Bool scrolled) {
120 int r;
121
122 if (!tns)
123 return;
124
125 tns->first -= tns->first % tns->cols;
126 r = tns->sel % tns->cols;
127
128 if (scrolled) {
129 /* move selection into visible area */
130 if (tns->sel >= tns->first + tns->cols * tns->rows)
131 tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
132 else if (tns->sel < tns->first)
133 tns->sel = tns->first + r;
134 } else {
135 /* scroll to selection */
136 if (tns->first + tns->cols * tns->rows <= tns->sel) {
137 tns->first = tns->sel - r - tns->cols * (tns->rows - 1);
138 tns->dirty = 1;
139 } else if (tns->first > tns->sel) {
140 tns->first = tns->sel - r;
141 tns->dirty = 1;
142 }
143 }
144}
145
146void tns_render(tns_t *tns, win_t *win) {
147 int i, cnt, r, x, y;
148 thumb_t *t;
149
150 if (!tns || !tns->dirty || !win)
151 return;
152
153 win_clear(win);
154 imlib_context_set_drawable(win->pm);
155
156 tns->cols = MAX(1, win->w / thumb_dim);
157 tns->rows = MAX(1, win->h / thumb_dim);
158
159 if (tns->cnt < tns->cols * tns->rows) {
160 tns->first = 0;
161 cnt = tns->cnt;
162 } else {
163 tns_check_view(tns, False);
164 cnt = tns->cols * tns->rows;
165 if ((r = tns->first + cnt - tns->cnt) >= tns->cols)
166 tns->first -= r - r % tns->cols;
167 if (r > 0)
168 cnt -= r % tns->cols;
169 }
170
171 r = cnt % tns->cols ? 1 : 0;
172 tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5;
173 tns->y = y = (win->h - (cnt / tns->cols + r) * thumb_dim) / 2 + 5;
174
175 for (i = 0; i < cnt; ++i) {
176 t = &tns->thumbs[tns->first + i];
177 t->x = x + (THUMB_SIZE - t->w) / 2;
178 t->y = y + (THUMB_SIZE - t->h) / 2;
179 imlib_context_set_image(t->im);
180 imlib_render_image_part_on_drawable_at_size(0, 0, t->w, t->h,
181 t->x, t->y, t->w, t->h);
182 if ((i + 1) % tns->cols == 0) {
183 x = tns->x;
184 y += thumb_dim;
185 } else {
186 x += thumb_dim;
187 }
188 }
189
190 tns->dirty = 0;
191 tns_highlight(tns, win, tns->sel, True);
192}
193
194void tns_highlight(tns_t *tns, win_t *win, int n, Bool hl) {
195 thumb_t *t;
196 unsigned long col;
197
198 if (!tns || !win)
199 return;
200
201 if (n >= 0 && n < tns->cnt) {
202 t = &tns->thumbs[n];
203
204 if (hl)
205 col = win->selcol;
206 else if (win->fullscreen)
207 col = win->black;
208 else
209 col = win->bgcol;
210
211 win_draw_rect(win, win->pm, t->x - 2, t->y - 2, t->w + 4, t->h + 4,
212 False, 2, col);
213 }
214
215 win_draw(win);
216}
217
218int tns_move_selection(tns_t *tns, win_t *win, tnsdir_t dir) {
219 int old;
220
221 if (!tns || !win)
222 return 0;
223
224 old = tns->sel;
225
226 switch (dir) {
227 case TNS_LEFT:
228 if (tns->sel > 0)
229 --tns->sel;
230 break;
231 case TNS_RIGHT:
232 if (tns->sel < tns->cnt - 1)
233 ++tns->sel;
234 break;
235 case TNS_UP:
236 if (tns->sel >= tns->cols)
237 tns->sel -= tns->cols;
238 break;
239 case TNS_DOWN:
240 if (tns->sel + tns->cols < tns->cnt)
241 tns->sel += tns->cols;
242 break;
243 }
244
245 if (tns->sel != old) {
246 tns_highlight(tns, win, old, False);
247 tns_check_view(tns, False);
248 if (!tns->dirty)
249 tns_highlight(tns, win, tns->sel, True);
250 }
251
252 return tns->sel != old;
253}
254
255int tns_scroll(tns_t *tns, tnsdir_t dir) {
256 int old;
257
258 if (!tns)
259 return 0;
260
261 old = tns->first;
262
263 if (dir == TNS_DOWN && tns->first + tns->cols * tns->rows < tns->cnt) {
264 tns->first += tns->cols;
265 tns_check_view(tns, True);
266 tns->dirty = 1;
267 } else if (dir == TNS_UP && tns->first >= tns->cols) {
268 tns->first -= tns->cols;
269 tns_check_view(tns, True);
270 tns->dirty = 1;
271 }
272
273 return tns->first != old;
274}
275
276int tns_translate(tns_t *tns, int x, int y) {
277 int n;
278 thumb_t *t;
279
280 if (!tns || x < tns->x || y < tns->y)
281 return -1;
282
283 n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
284 (x - tns->x) / thumb_dim;
285
286 if (n < tns->cnt) {
287 t = &tns->thumbs[n];
288 if (x >= t->x && x <= t->x + t->w && y >= t->y && y <= t->y + t->h)
289 return n;
290 }
291
292 return -1;
293}
294
295int tns_cache_enabled() {
296 int len, ret = 0;
297 char *cpath, *homedir;
298 struct stat stats;
299
300 if ((homedir = getenv("HOME"))) {
301 len = strlen(homedir) + 10;
302 cpath = (char*) s_malloc(len * sizeof(char));
303 snprintf(cpath, len, "%s/.sxiv", homedir);
304 ret = !stat(cpath, &stats) && S_ISDIR(stats.st_mode) &&
305 !access(cpath, W_OK);
306 free(cpath);
307 }
308
309 return ret;
310}
311
312void tns_cache_write(thumb_t *t, Bool force) {
313}