| 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 | |
| 28 | extern Imlib_Image *im_broken; |
| 29 | const int thumb_dim = THUMB_SIZE + 10; |
| 30 | |
| 31 | void tns_init(tns_t *tns, int cnt) { |
| 32 | if (!tns) |
| 33 | return; |
| 34 | |
| 35 | tns->cnt = tns->first = tns->sel = tns->vis = 0; |
| 36 | tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t)); |
| 37 | } |
| 38 | |
| 39 | void 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 | |
| 52 | void 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 | |
| 85 | void 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 | i = cnt % tns->cols ? 1 : 0; |
| 102 | tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5; |
| 103 | tns->y = y = (win->h - (cnt / tns->cols + i) * thumb_dim) / 2 + 5; |
| 104 | i = tns->first; |
| 105 | |
| 106 | while (i < cnt) { |
| 107 | tns->thumbs[i].x = x + (THUMB_SIZE - tns->thumbs[i].w) / 2; |
| 108 | tns->thumbs[i].y = y + (THUMB_SIZE - tns->thumbs[i].h) / 2; |
| 109 | win_draw_pixmap(win, tns->thumbs[i].pm, tns->thumbs[i].x, |
| 110 | tns->thumbs[i].y, tns->thumbs[i].w, tns->thumbs[i].h); |
| 111 | if (++i % tns->cols == 0) { |
| 112 | x = tns->x; |
| 113 | y += thumb_dim; |
| 114 | } else { |
| 115 | x += thumb_dim; |
| 116 | } |
| 117 | } |
| 118 | tns->vis = i - tns->first; |
| 119 | |
| 120 | tns_highlight(tns, win, -1); |
| 121 | } |
| 122 | |
| 123 | void 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 | |
| 141 | void tns_move_selection(tns_t *tns, win_t *win, movedir_t dir) { |
| 142 | int sel; |
| 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 | --tns->sel; |
| 153 | tns_highlight(tns, win, sel); |
| 154 | } |
| 155 | break; |
| 156 | case MOVE_RIGHT: |
| 157 | if (sel % tns->cols < tns->cols - 1 && sel < tns->cnt - 1) { |
| 158 | ++tns->sel; |
| 159 | tns_highlight(tns, win, sel); |
| 160 | } |
| 161 | break; |
| 162 | case MOVE_UP: |
| 163 | if (sel / tns->cols > 0) { |
| 164 | tns->sel -= tns->cols; |
| 165 | tns_highlight(tns, win, sel); |
| 166 | } |
| 167 | break; |
| 168 | case MOVE_DOWN: |
| 169 | if (sel / tns->cols < tns->rows - 1 && sel + tns->cols < tns->vis) { |
| 170 | tns->sel += tns->cols; |
| 171 | tns_highlight(tns, win, sel); |
| 172 | } |
| 173 | break; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | int tns_translate(tns_t *tns, int x, int y) { |
| 178 | int n; |
| 179 | thumb_t *t; |
| 180 | |
| 181 | if (!tns || x < tns->x || y < tns->y) |
| 182 | return -1; |
| 183 | |
| 184 | n = (y - tns->y) / thumb_dim * tns->cols + (x - tns->x) / thumb_dim; |
| 185 | |
| 186 | if (n < tns->cnt) { |
| 187 | t = &tns->thumbs[n]; |
| 188 | if (x >= t->x && x <= t->x + t->w && y >= t->y && y <= t->y + t->h) |
| 189 | return n; |
| 190 | } |
| 191 | |
| 192 | return -1; |
| 193 | } |