Krolyxon
Commit b2f1b99

Branches

Refs →

nsxivthumbs.c

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