Krolyxon
Commit 8919fbd

Branches

Refs →

nsxivthumbs.c

262 lines · 5.7 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 imlib_image_set_changes_on_disk();
72 } else {
73 imlib_context_set_image(im_broken);
74 }
75
76 w = imlib_image_get_width();
77 h = imlib_image_get_height();
78 zw = (float) THUMB_SIZE / (float) w;
79 zh = (float) THUMB_SIZE / (float) h;
80 z = MIN(zw, zh);
81 if (!im && z > 1.0)
82 z = 1.0;
83
84 t = &tns->thumbs[n];
85 t->w = z * w;
86 t->h = z * h;
87
88 if (t->pm)
89 win_free_pixmap(win, t->pm);
90 t->pm = win_create_pixmap(win, t->w, t->h);
91 imlib_context_set_drawable(t->pm);
92 imlib_context_set_anti_alias(1);
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();
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
176 if (!tns || !win)
177 return;
178
179 if (n >= 0 && n < tns->cnt) {
180 t = &tns->thumbs[n];
181 win_draw_rect(win, t->x - 2, t->y - 2, t->w + 4, t->h + 4, hl);
182 }
183
184 win_draw(win);
185}
186
187int tns_move_selection(tns_t *tns, win_t *win, tnsdir_t dir) {
188 int old;
189
190 if (!tns || !win)
191 return 0;
192
193 old = tns->sel;
194
195 switch (dir) {
196 case TNS_LEFT:
197 if (tns->sel > 0)
198 --tns->sel;
199 break;
200 case TNS_RIGHT:
201 if (tns->sel < tns->cnt - 1)
202 ++tns->sel;
203 break;
204 case TNS_UP:
205 if (tns->sel >= tns->cols)
206 tns->sel -= tns->cols;
207 break;
208 case TNS_DOWN:
209 if (tns->sel + tns->cols < tns->cnt)
210 tns->sel += tns->cols;
211 break;
212 }
213
214 if (tns->sel != old) {
215 tns_highlight(tns, win, old, False);
216 tns_check_view(tns, False);
217 if (!tns->dirty)
218 tns_highlight(tns, win, tns->sel, True);
219 }
220
221 return tns->sel != old;
222}
223
224int tns_scroll(tns_t *tns, tnsdir_t dir) {
225 int old;
226
227 if (!tns)
228 return 0;
229
230 old = tns->first;
231
232 if (dir == TNS_DOWN && tns->first + tns->cols * tns->rows < tns->cnt) {
233 tns->first += tns->cols;
234 tns_check_view(tns, True);
235 tns->dirty = 1;
236 } else if (dir == TNS_UP && tns->first >= tns->cols) {
237 tns->first -= tns->cols;
238 tns_check_view(tns, True);
239 tns->dirty = 1;
240 }
241
242 return tns->first != old;
243}
244
245int tns_translate(tns_t *tns, int x, int y) {
246 int n;
247 thumb_t *t;
248
249 if (!tns || x < tns->x || y < tns->y)
250 return -1;
251
252 n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
253 (x - tns->x) / thumb_dim;
254
255 if (n < tns->cnt) {
256 t = &tns->thumbs[n];
257 if (x >= t->x && x <= t->x + t->w && y >= t->y && y <= t->y + t->h)
258 return n;
259 }
260
261 return -1;
262}