Krolyxon
Commit ef24ded

Branches

Refs →

nsxivthumbs.c

135 lines · 3.2 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}
38
39void tns_free(tns_t *tns, win_t *win) {
40 int i;
41
42 if (!tns)
43 return;
44
45 for (i = 0; i < tns->cnt; ++i)
46 win_free_pixmap(win, tns->thumbs[i].pm);
47
48 free(tns->thumbs);
49 tns->thumbs = NULL;
50}
51
52void tns_load(tns_t *tns, win_t *win, const char *filename) {
53 int w, h;
54 float z, zw, zh;
55 thumb_t *t;
56 Imlib_Image *im;
57
58 if (!tns || !win || !filename)
59 return;
60
61 if ((im = imlib_load_image(filename)))
62 imlib_context_set_image(im);
63 else
64 imlib_context_set_image(im_broken);
65
66 w = imlib_image_get_width();
67 h = imlib_image_get_height();
68 zw = (float) THUMB_SIZE / (float) w;
69 zh = (float) THUMB_SIZE / (float) h;
70 z = MIN(zw, zh);
71 if (!im && z > 1.0)
72 z = 1.0;
73
74 t = &tns->thumbs[tns->cnt++];
75 t->w = z * w;
76 t->h = z * h;
77
78 t->pm = win_create_pixmap(win, t->w, t->h);
79 imlib_context_set_drawable(t->pm);
80 imlib_render_image_part_on_drawable_at_size(0, 0, w, h,
81 0, 0, t->w, t->h);
82 imlib_free_image();
83}
84
85void tns_render(tns_t *tns, win_t *win) {
86 int i, cnt, x, y;
87
88 if (!tns || !win)
89 return;
90
91 tns->cols = MAX(1, win->w / thumb_dim);
92 tns->rows = MAX(1, win->h / thumb_dim);
93
94 cnt = tns->cols * tns->rows;
95 if (tns->first && tns->first + cnt > tns->cnt)
96 tns->first = MAX(0, tns->cnt - cnt);
97 cnt = MIN(tns->first + cnt, tns->cnt);
98
99 win_clear(win);
100
101 x = y = 5;
102 i = tns->first;
103
104 while (i < cnt) {
105 tns->thumbs[i].x = x + (THUMB_SIZE - tns->thumbs[i].w) / 2;
106 tns->thumbs[i].y = y + (THUMB_SIZE - tns->thumbs[i].h) / 2;
107 win_draw_pixmap(win, tns->thumbs[i].pm, tns->thumbs[i].x,
108 tns->thumbs[i].y, tns->thumbs[i].w, tns->thumbs[i].h);
109 if (++i % tns->cols == 0) {
110 x = 5;
111 y += thumb_dim;
112 } else {
113 x += thumb_dim;
114 }
115 }
116
117 tns_highlight(tns, win, -1);
118 win_draw(win);
119}
120
121void tns_highlight(tns_t *tns, win_t *win, int old) {
122 thumb_t *t;
123
124 if (!tns || !win)
125 return;
126
127 if (old >= 0 && old < tns->cnt) {
128 t = &tns->thumbs[old];
129 win_draw_rect(win, t->x - 2, t->y - 2, t->w + 4, t->h + 4, False);
130 }
131 if (tns->sel < tns->cnt) {
132 t = &tns->thumbs[tns->sel];
133 win_draw_rect(win, t->x - 2, t->y - 2, t->w + 4, t->h + 4, True);
134 }
135}