Krolyxon
Commit 1c072cc

Branches

Refs →

nsxivthumbs.c

118 lines · 2.7 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
28const int thumb_dim = THUMB_SIZE + 10;
29
30void tns_init(tns_t *tns, int cnt) {
31 if (!tns)
32 return;
33
34 tns->cnt = tns->first = tns->sel = 0;
35 tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t));
36}
37
38void tns_free(tns_t *tns, win_t *win) {
39 int i;
40
41 if (!tns)
42 return;
43
44 for (i = 0; i < tns->cnt; ++i)
45 win_free_pixmap(win, tns->thumbs[i].pm);
46
47 free(tns->thumbs);
48 tns->thumbs = NULL;
49}
50
51void tns_load(tns_t *tns, win_t *win, const char *filename) {
52 int w, h;
53 float z, zw, zh;
54 thumb_t *t;
55 Imlib_Image *im;
56
57 if (!tns || !win || !filename)
58 return;
59
60 if (!(im = imlib_load_image(filename)))
61 return;
62
63 imlib_context_set_image(im);
64
65 w = imlib_image_get_width();
66 h = imlib_image_get_height();
67 zw = (float) THUMB_SIZE / (float) w;
68 zh = (float) THUMB_SIZE / (float) h;
69 z = MIN(zw, zh);
70
71 t = &tns->thumbs[tns->cnt++];
72 t->w = z * w;
73 t->h = z * h;
74
75 t->pm = win_create_pixmap(win, t->w, t->h);
76 imlib_context_set_drawable(t->pm);
77 imlib_render_image_part_on_drawable_at_size(0, 0, w, h,
78 0, 0, t->w, t->h);
79 imlib_free_image();
80}
81
82void tns_render(tns_t *tns, win_t *win) {
83 int i, cnt, x, y;
84
85 if (!tns || !win)
86 return;
87
88 printf("tns_render()\n");
89
90 tns->cols = win->w / thumb_dim;
91 tns->rows = win->h / thumb_dim;
92
93 cnt = tns->cols * tns->rows;
94 if (tns->first && tns->first + cnt > tns->cnt)
95 tns->first = MAX(0, tns->cnt - cnt);
96 cnt = MIN(tns->first + cnt, tns->cnt);
97
98 win_clear(win);
99
100 x = y = 5;
101 i = tns->first;
102
103 while (i < cnt) {
104 tns->thumbs[i].x = x + (THUMB_SIZE - tns->thumbs[i].w) / 2;
105 tns->thumbs[i].y = y + (THUMB_SIZE - tns->thumbs[i].h) / 2;
106 win_draw_pixmap(win, tns->thumbs[i].pm, tns->thumbs[i].x,
107 tns->thumbs[i].y, tns->thumbs[i].w, tns->thumbs[i].h);
108 if (++i % tns->cols == 0) {
109 x = 5;
110 y += thumb_dim;
111 } else {
112 x += thumb_dim;
113 }
114 }
115
116 win_draw(win);
117}
118