A Simple X Image Viewer
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

281 linhas
5.9 KiB

  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. #include <stdlib.h>
  19. #include <string.h>
  20. #include "config.h"
  21. #include "thumbs.h"
  22. #include "util.h"
  23. extern Imlib_Image *im_invalid;
  24. const int thumb_dim = THUMB_SIZE + 10;
  25. void tns_init(tns_t *tns, int cnt) {
  26. if (!tns)
  27. return;
  28. tns->cnt = tns->first = tns->sel = 0;
  29. tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t));
  30. memset(tns->thumbs, 0, cnt * sizeof(thumb_t));
  31. tns->cap = cnt;
  32. tns->dirty = 0;
  33. }
  34. void tns_free(tns_t *tns, win_t *win) {
  35. int i;
  36. if (!tns || !tns->thumbs)
  37. return;
  38. for (i = 0; i < tns->cnt; ++i) {
  39. if (tns->thumbs[i].im) {
  40. imlib_context_set_image(tns->thumbs[i].im);
  41. imlib_free_image();
  42. }
  43. }
  44. free(tns->thumbs);
  45. tns->thumbs = NULL;
  46. }
  47. void tns_load(tns_t *tns, win_t *win, int n, const char *filename) {
  48. int w, h;
  49. float z, zw, zh;
  50. thumb_t *t;
  51. Imlib_Image *im;
  52. if (!tns || !win || !filename)
  53. return;
  54. if (n >= tns->cap)
  55. return;
  56. else if (n >= tns->cnt)
  57. tns->cnt = n + 1;
  58. t = &tns->thumbs[n];
  59. if (t->im) {
  60. imlib_context_set_image(t->im);
  61. imlib_free_image();
  62. }
  63. if ((im = imlib_load_image(filename)))
  64. imlib_context_set_image(im);
  65. else
  66. imlib_context_set_image(im_invalid);
  67. w = imlib_image_get_width();
  68. h = imlib_image_get_height();
  69. if (im) {
  70. zw = (float) THUMB_SIZE / (float) w;
  71. zh = (float) THUMB_SIZE / (float) h;
  72. z = MIN(zw, zh);
  73. } else {
  74. z = 1.0;
  75. }
  76. t->w = z * w;
  77. t->h = z * h;
  78. imlib_context_set_anti_alias(1);
  79. if (!(t->im = imlib_create_cropped_scaled_image(0, 0, w, h, t->w, t->h)))
  80. die("could not allocate memory");
  81. if (im)
  82. imlib_free_image_and_decache();
  83. tns->dirty = 1;
  84. }
  85. void tns_check_view(tns_t *tns, Bool scrolled) {
  86. int r;
  87. if (!tns)
  88. return;
  89. tns->first -= tns->first % tns->cols;
  90. r = tns->sel % tns->cols;
  91. if (scrolled) {
  92. /* move selection into visible area */
  93. if (tns->sel >= tns->first + tns->cols * tns->rows)
  94. tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
  95. else if (tns->sel < tns->first)
  96. tns->sel = tns->first + r;
  97. } else {
  98. /* scroll to selection */
  99. if (tns->first + tns->cols * tns->rows <= tns->sel) {
  100. tns->first = tns->sel - r - tns->cols * (tns->rows - 1);
  101. tns->dirty = 1;
  102. } else if (tns->first > tns->sel) {
  103. tns->first = tns->sel - r;
  104. tns->dirty = 1;
  105. }
  106. }
  107. }
  108. void tns_render(tns_t *tns, win_t *win) {
  109. int i, cnt, r, x, y;
  110. thumb_t *t;
  111. if (!tns || !tns->dirty || !win)
  112. return;
  113. win_clear(win);
  114. imlib_context_set_drawable(win->pm);
  115. tns->cols = MAX(1, win->w / thumb_dim);
  116. tns->rows = MAX(1, win->h / thumb_dim);
  117. if (tns->cnt < tns->cols * tns->rows) {
  118. tns->first = 0;
  119. cnt = tns->cnt;
  120. } else {
  121. tns_check_view(tns, False);
  122. cnt = tns->cols * tns->rows;
  123. if ((r = tns->first + cnt - tns->cnt) >= tns->cols)
  124. tns->first -= r - r % tns->cols;
  125. if (r > 0)
  126. cnt -= r % tns->cols;
  127. }
  128. r = cnt % tns->cols ? 1 : 0;
  129. tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5;
  130. tns->y = y = (win->h - (cnt / tns->cols + r) * thumb_dim) / 2 + 5;
  131. for (i = 0; i < cnt; ++i) {
  132. t = &tns->thumbs[tns->first + i];
  133. t->x = x + (THUMB_SIZE - t->w) / 2;
  134. t->y = y + (THUMB_SIZE - t->h) / 2;
  135. imlib_context_set_image(t->im);
  136. imlib_render_image_part_on_drawable_at_size(0, 0, t->w, t->h,
  137. t->x, t->y, t->w, t->h);
  138. if ((i + 1) % tns->cols == 0) {
  139. x = tns->x;
  140. y += thumb_dim;
  141. } else {
  142. x += thumb_dim;
  143. }
  144. }
  145. tns->dirty = 0;
  146. tns_highlight(tns, win, tns->sel, True);
  147. }
  148. void tns_highlight(tns_t *tns, win_t *win, int n, Bool hl) {
  149. thumb_t *t;
  150. unsigned long col;
  151. if (!tns || !win)
  152. return;
  153. if (n >= 0 && n < tns->cnt) {
  154. t = &tns->thumbs[n];
  155. if (hl)
  156. col = win->selcol;
  157. else if (win->fullscreen)
  158. col = win->black;
  159. else
  160. col = win->bgcol;
  161. win_draw_rect(win, win->pm, t->x - 2, t->y - 2, t->w + 4, t->h + 4,
  162. False, 2, col);
  163. }
  164. win_draw(win);
  165. }
  166. int tns_move_selection(tns_t *tns, win_t *win, tnsdir_t dir) {
  167. int old;
  168. if (!tns || !win)
  169. return 0;
  170. old = tns->sel;
  171. switch (dir) {
  172. case TNS_LEFT:
  173. if (tns->sel > 0)
  174. --tns->sel;
  175. break;
  176. case TNS_RIGHT:
  177. if (tns->sel < tns->cnt - 1)
  178. ++tns->sel;
  179. break;
  180. case TNS_UP:
  181. if (tns->sel >= tns->cols)
  182. tns->sel -= tns->cols;
  183. break;
  184. case TNS_DOWN:
  185. if (tns->sel + tns->cols < tns->cnt)
  186. tns->sel += tns->cols;
  187. break;
  188. }
  189. if (tns->sel != old) {
  190. tns_highlight(tns, win, old, False);
  191. tns_check_view(tns, False);
  192. if (!tns->dirty)
  193. tns_highlight(tns, win, tns->sel, True);
  194. }
  195. return tns->sel != old;
  196. }
  197. int tns_scroll(tns_t *tns, tnsdir_t dir) {
  198. int old;
  199. if (!tns)
  200. return 0;
  201. old = tns->first;
  202. if (dir == TNS_DOWN && tns->first + tns->cols * tns->rows < tns->cnt) {
  203. tns->first += tns->cols;
  204. tns_check_view(tns, True);
  205. tns->dirty = 1;
  206. } else if (dir == TNS_UP && tns->first >= tns->cols) {
  207. tns->first -= tns->cols;
  208. tns_check_view(tns, True);
  209. tns->dirty = 1;
  210. }
  211. return tns->first != old;
  212. }
  213. int tns_translate(tns_t *tns, int x, int y) {
  214. int n;
  215. thumb_t *t;
  216. if (!tns || x < tns->x || y < tns->y)
  217. return -1;
  218. n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
  219. (x - tns->x) / thumb_dim;
  220. if (n < tns->cnt) {
  221. t = &tns->thumbs[n];
  222. if (x >= t->x && x <= t->x + t->w && y >= t->y && y <= t->y + t->h)
  223. return n;
  224. }
  225. return -1;
  226. }