Krolyxon
Commit aadba6f

Branches

Refs →

nsxivthumbs.c

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