Krolyxon
Commit c6726ed

Branches

Refs →

nsxivthumbs.c

191 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}
39
40void tns_free(tns_t *tns, win_t *win) {
41 int i;
42
43 if (!tns)
44 return;
45
46 for (i = 0; i < tns->cnt; ++i)
47 win_free_pixmap(win, tns->thumbs[i].pm);
48
49 free(tns->thumbs);
50 tns->thumbs = NULL;
51}
52
53void tns_load(tns_t *tns, win_t *win, const char *filename) {
54 int w, h;
55 float z, zw, zh;
56 thumb_t *t;
57 Imlib_Image *im;
58
59 if (!tns || !win || !filename)
60 return;
61
62 if ((im = imlib_load_image(filename)))
63 imlib_context_set_image(im);
64 else
65 imlib_context_set_image(im_broken);
66
67 w = imlib_image_get_width();
68 h = imlib_image_get_height();
69 zw = (float) THUMB_SIZE / (float) w;
70 zh = (float) THUMB_SIZE / (float) h;
71 z = MIN(zw, zh);
72 if (!im && z > 1.0)
73 z = 1.0;
74
75 t = &tns->thumbs[tns->cnt++];
76 t->w = z * w;
77 t->h = z * h;
78
79 t->pm = win_create_pixmap(win, t->w, t->h);
80 imlib_context_set_drawable(t->pm);
81 imlib_render_image_part_on_drawable_at_size(0, 0, w, h,
82 0, 0, t->w, t->h);
83 imlib_free_image();
84}
85
86void tns_render(tns_t *tns, win_t *win) {
87 int i, cnt, x, y;
88
89 if (!tns || !win)
90 return;
91
92 tns->cols = MAX(1, win->w / thumb_dim);
93 tns->rows = MAX(1, win->h / thumb_dim);
94
95 cnt = tns->cols * tns->rows;
96 if (tns->first && tns->first + cnt > tns->cnt)
97 tns->first = MAX(0, tns->cnt - cnt);
98 cnt = MIN(tns->first + cnt, tns->cnt);
99
100 win_clear(win);
101
102 i = cnt % tns->cols ? 1 : 0;
103 tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5;
104 tns->y = y = (win->h - (cnt / tns->cols + i) * thumb_dim) / 2 + 5;
105 i = tns->first;
106
107 while (i < cnt) {
108 tns->thumbs[i].x = x + (THUMB_SIZE - tns->thumbs[i].w) / 2;
109 tns->thumbs[i].y = y + (THUMB_SIZE - tns->thumbs[i].h) / 2;
110 win_draw_pixmap(win, tns->thumbs[i].pm, tns->thumbs[i].x,
111 tns->thumbs[i].y, tns->thumbs[i].w, tns->thumbs[i].h);
112 if (++i % tns->cols == 0) {
113 x = tns->x;
114 y += thumb_dim;
115 } else {
116 x += thumb_dim;
117 }
118 }
119
120 tns_highlight(tns, win, -1);
121}
122
123void tns_highlight(tns_t *tns, win_t *win, int old) {
124 thumb_t *t;
125
126 if (!tns || !win)
127 return;
128
129 if (old >= 0 && old < tns->cnt) {
130 t = &tns->thumbs[old];
131 win_draw_rect(win, t->x - 2, t->y - 2, t->w + 4, t->h + 4, False);
132 }
133 if (tns->sel < tns->cnt) {
134 t = &tns->thumbs[tns->sel];
135 win_draw_rect(win, t->x - 2, t->y - 2, t->w + 4, t->h + 4, True);
136 }
137
138 win_draw(win);
139}
140
141void tns_move_selection(tns_t *tns, win_t *win, movedir_t dir) {
142 int sel, old;
143
144 if (!tns || !win)
145 return;
146
147 sel = tns->sel;
148
149 switch (dir) {
150 case MOVE_LEFT:
151 if (sel % tns->cols > 0)
152 --sel;
153 break;
154 case MOVE_RIGHT:
155 if (sel % tns->cols < tns->cols - 1 && sel < tns->cnt - 1)
156 ++sel;
157 break;
158 case MOVE_UP:
159 if (sel / tns->cols > 0)
160 sel -= tns->cols;
161 break;
162 case MOVE_DOWN:
163 if (sel / tns->cols < tns->rows - 1 && sel + tns->cols < tns->cnt)
164 sel += tns->cols;
165 break;
166 }
167
168 if (sel != tns->sel && tns->thumbs[sel].x != 0) {
169 old = tns->sel;
170 tns->sel = sel;
171 tns_highlight(tns, win, old);
172 }
173}
174
175int tns_translate(tns_t *tns, int x, int y) {
176 int n;
177 thumb_t *t;
178
179 if (!tns || x < tns->x || y < tns->y)
180 return -1;
181
182 n = (y - tns->y) / thumb_dim * tns->cols + (x - tns->x) / thumb_dim;
183
184 if (n < tns->cnt) {
185 t = &tns->thumbs[n];
186 if (x >= t->x && x <= t->x + t->w && y >= t->y && y <= t->y + t->h)
187 return n;
188 }
189
190 return -1;
191}