Krolyxon
Commit 88b5c5f

Branches

Refs →

nsxivthumbs.c

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