Krolyxon
Commit a152f82

Branches

Refs →

nsxivthumbs.c

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