Krolyxon
Commit 00d4b0f

Branches

Refs →

nsxivthumbs.c

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