Krolyxon
Commit 5a35484

Branches

Refs →

nsxivthumbs.c

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