Krolyxon
Commit 87a9828

Branches

Refs →

nsxivthumbs.c

277 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_broken;
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_broken);
82
83 w = imlib_image_get_width();
84 h = imlib_image_get_height();
85 zw = (float) THUMB_SIZE / (float) w;
86 zh = (float) THUMB_SIZE / (float) h;
87 z = MIN(zw, zh);
88 if (!im && z > 1.0)
89 z = 1.0;
90
91 t->w = z * w;
92 t->h = z * h;
93
94 imlib_context_set_anti_alias(1);
95 if (!(t->im = imlib_create_cropped_scaled_image(0, 0, w, h, t->w, t->h)))
96 die("could not allocate memory");
97 if (im)
98 imlib_free_image_and_decache();
99
100 tns->dirty = 1;
101}
102
103void tns_check_view(tns_t *tns, Bool scrolled) {
104 int r;
105
106 if (!tns)
107 return;
108
109 tns->first -= tns->first % tns->cols;
110 r = tns->sel % tns->cols;
111
112 if (scrolled) {
113 /* move selection into visible area */
114 if (tns->sel >= tns->first + tns->cols * tns->rows)
115 tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
116 else if (tns->sel < tns->first)
117 tns->sel = tns->first + r;
118 } else {
119 /* scroll to selection */
120 if (tns->first + tns->cols * tns->rows <= tns->sel) {
121 tns->first = tns->sel - r - tns->cols * (tns->rows - 1);
122 tns->dirty = 1;
123 } else if (tns->first > tns->sel) {
124 tns->first = tns->sel - r;
125 tns->dirty = 1;
126 }
127 }
128}
129
130void tns_render(tns_t *tns, win_t *win) {
131 int i, cnt, r, x, y;
132 thumb_t *t;
133
134 if (!tns || !tns->dirty || !win)
135 return;
136
137 win_clear(win);
138 imlib_context_set_drawable(win->pm);
139
140 tns->cols = MAX(1, win->w / thumb_dim);
141 tns->rows = MAX(1, win->h / thumb_dim);
142
143 if (tns->cnt < tns->cols * tns->rows) {
144 tns->first = 0;
145 cnt = tns->cnt;
146 } else {
147 tns_check_view(tns, False);
148 cnt = tns->cols * tns->rows;
149 if ((r = tns->first + cnt - tns->cnt) >= tns->cols)
150 tns->first -= r - r % tns->cols;
151 if (r > 0)
152 cnt -= r % tns->cols;
153 }
154
155 r = cnt % tns->cols ? 1 : 0;
156 tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5;
157 tns->y = y = (win->h - (cnt / tns->cols + r) * thumb_dim) / 2 + 5;
158
159 for (i = 0; i < cnt; ++i) {
160 t = &tns->thumbs[tns->first + i];
161 t->x = x + (THUMB_SIZE - t->w) / 2;
162 t->y = y + (THUMB_SIZE - t->h) / 2;
163 imlib_context_set_image(t->im);
164 imlib_render_image_part_on_drawable_at_size(0, 0, t->w, t->h,
165 t->x, t->y, t->w, t->h);
166 if ((i + 1) % tns->cols == 0) {
167 x = tns->x;
168 y += thumb_dim;
169 } else {
170 x += thumb_dim;
171 }
172 }
173
174 tns->dirty = 0;
175 tns_highlight(tns, win, tns->sel, True);
176}
177
178void tns_highlight(tns_t *tns, win_t *win, int n, Bool hl) {
179 thumb_t *t;
180 unsigned long col;
181
182 if (!tns || !win)
183 return;
184
185 if (n >= 0 && n < tns->cnt) {
186 t = &tns->thumbs[n];
187
188 if (hl)
189 col = win->selcol;
190 else if (win->fullscreen)
191 col = win->black;
192 else
193 col = win->bgcol;
194
195 win_draw_rect(win, win->pm, t->x - 2, t->y - 2, t->w + 4, t->h + 4,
196 False, 2, col);
197 }
198
199 win_draw(win);
200}
201
202int tns_move_selection(tns_t *tns, win_t *win, tnsdir_t dir) {
203 int old;
204
205 if (!tns || !win)
206 return 0;
207
208 old = tns->sel;
209
210 switch (dir) {
211 case TNS_LEFT:
212 if (tns->sel > 0)
213 --tns->sel;
214 break;
215 case TNS_RIGHT:
216 if (tns->sel < tns->cnt - 1)
217 ++tns->sel;
218 break;
219 case TNS_UP:
220 if (tns->sel >= tns->cols)
221 tns->sel -= tns->cols;
222 break;
223 case TNS_DOWN:
224 if (tns->sel + tns->cols < tns->cnt)
225 tns->sel += tns->cols;
226 break;
227 }
228
229 if (tns->sel != old) {
230 tns_highlight(tns, win, old, False);
231 tns_check_view(tns, False);
232 if (!tns->dirty)
233 tns_highlight(tns, win, tns->sel, True);
234 }
235
236 return tns->sel != old;
237}
238
239int tns_scroll(tns_t *tns, tnsdir_t dir) {
240 int old;
241
242 if (!tns)
243 return 0;
244
245 old = tns->first;
246
247 if (dir == TNS_DOWN && tns->first + tns->cols * tns->rows < tns->cnt) {
248 tns->first += tns->cols;
249 tns_check_view(tns, True);
250 tns->dirty = 1;
251 } else if (dir == TNS_UP && tns->first >= tns->cols) {
252 tns->first -= tns->cols;
253 tns_check_view(tns, True);
254 tns->dirty = 1;
255 }
256
257 return tns->first != old;
258}
259
260int tns_translate(tns_t *tns, int x, int y) {
261 int n;
262 thumb_t *t;
263
264 if (!tns || x < tns->x || y < tns->y)
265 return -1;
266
267 n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
268 (x - tns->x) / thumb_dim;
269
270 if (n < tns->cnt) {
271 t = &tns->thumbs[n];
272 if (x >= t->x && x <= t->x + t->w && y >= t->y && y <= t->y + t->h)
273 return n;
274 }
275
276 return -1;
277}