Krolyxon
Commit 2194df8

Branches

Refs →

nsxivthumbs.c

197 lines · 4.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)
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
86 tns->dirty = 1;
87}
88
89void tns_render(tns_t *tns, win_t *win) {
90 int i, cnt, x, y;
91
92 if (!tns || !tns->dirty || !win)
93 return;
94
95 tns->cols = MAX(1, win->w / thumb_dim);
96 tns->rows = MAX(1, win->h / thumb_dim);
97
98 cnt = tns->cols * tns->rows;
99 if (tns->first && tns->first + cnt > tns->cnt)
100 tns->first = MAX(0, tns->cnt - cnt);
101 cnt = MIN(tns->first + cnt, tns->cnt);
102
103 win_clear(win);
104
105 i = cnt % tns->cols ? 1 : 0;
106 tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5;
107 tns->y = y = (win->h - (cnt / tns->cols + i) * thumb_dim) / 2 + 5;
108 i = tns->first;
109
110 while (i < cnt) {
111 tns->thumbs[i].x = x + (THUMB_SIZE - tns->thumbs[i].w) / 2;
112 tns->thumbs[i].y = y + (THUMB_SIZE - tns->thumbs[i].h) / 2;
113 win_draw_pixmap(win, tns->thumbs[i].pm, tns->thumbs[i].x,
114 tns->thumbs[i].y, tns->thumbs[i].w, tns->thumbs[i].h);
115 if (++i % tns->cols == 0) {
116 x = tns->x;
117 y += thumb_dim;
118 } else {
119 x += thumb_dim;
120 }
121 }
122
123 tns->dirty = 0;
124
125 tns_highlight(tns, win, -1);
126}
127
128void tns_highlight(tns_t *tns, win_t *win, int old) {
129 thumb_t *t;
130
131 if (!tns || !win)
132 return;
133
134 if (old >= 0 && old < tns->cnt) {
135 t = &tns->thumbs[old];
136 win_draw_rect(win, t->x - 2, t->y - 2, t->w + 4, t->h + 4, False);
137 }
138 if (tns->sel < tns->cnt) {
139 t = &tns->thumbs[tns->sel];
140 win_draw_rect(win, t->x - 2, t->y - 2, t->w + 4, t->h + 4, True);
141 }
142
143 win_draw(win);
144}
145
146int tns_move_selection(tns_t *tns, win_t *win, movedir_t dir) {
147 int sel, old;
148
149 if (!tns || !win)
150 return 0;
151
152 sel = old = tns->sel;
153
154 switch (dir) {
155 case MOVE_LEFT:
156 if (sel % tns->cols > 0)
157 --sel;
158 break;
159 case MOVE_RIGHT:
160 if (sel % tns->cols < tns->cols - 1 && sel < tns->cnt - 1)
161 ++sel;
162 break;
163 case MOVE_UP:
164 if (sel / tns->cols > 0)
165 sel -= tns->cols;
166 break;
167 case MOVE_DOWN:
168 if (sel / tns->cols < tns->rows - 1 && sel + tns->cols < tns->cnt)
169 sel += tns->cols;
170 break;
171 }
172
173 if (sel != old && tns->thumbs[sel].x != 0) {
174 tns->sel = sel;
175 tns_highlight(tns, win, old);
176 }
177
178 return sel != old;
179}
180
181int tns_translate(tns_t *tns, int x, int y) {
182 int n;
183 thumb_t *t;
184
185 if (!tns || x < tns->x || y < tns->y)
186 return -1;
187
188 n = (y - tns->y) / thumb_dim * tns->cols + (x - tns->x) / thumb_dim;
189
190 if (n < tns->cnt) {
191 t = &tns->thumbs[n];
192 if (x >= t->x && x <= t->x + t->w && y >= t->y && y <= t->y + t->h)
193 return n;
194 }
195
196 return -1;
197}