A Simple X Image Viewer
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

278 řádky
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. 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. t->w = z * w;
  75. t->h = z * h;
  76. imlib_context_set_anti_alias(1);
  77. if (!(t->im = imlib_create_cropped_scaled_image(0, 0, w, h, t->w, t->h)))
  78. die("could not allocate memory");
  79. if (im)
  80. imlib_free_image_and_decache();
  81. tns->dirty = 1;
  82. }
  83. void tns_check_view(tns_t *tns, Bool scrolled) {
  84. int r;
  85. if (!tns)
  86. return;
  87. tns->first -= tns->first % tns->cols;
  88. r = tns->sel % tns->cols;
  89. if (scrolled) {
  90. /* move selection into visible area */
  91. if (tns->sel >= tns->first + tns->cols * tns->rows)
  92. tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
  93. else if (tns->sel < tns->first)
  94. tns->sel = tns->first + r;
  95. } else {
  96. /* scroll to selection */
  97. if (tns->first + tns->cols * tns->rows <= tns->sel) {
  98. tns->first = tns->sel - r - tns->cols * (tns->rows - 1);
  99. tns->dirty = 1;
  100. } else if (tns->first > tns->sel) {
  101. tns->first = tns->sel - r;
  102. tns->dirty = 1;
  103. }
  104. }
  105. }
  106. void tns_render(tns_t *tns, win_t *win) {
  107. int i, cnt, r, x, y;
  108. thumb_t *t;
  109. if (!tns || !tns->dirty || !win)
  110. return;
  111. win_clear(win);
  112. imlib_context_set_drawable(win->pm);
  113. tns->cols = MAX(1, win->w / thumb_dim);
  114. tns->rows = MAX(1, win->h / thumb_dim);
  115. if (tns->cnt < tns->cols * tns->rows) {
  116. tns->first = 0;
  117. cnt = tns->cnt;
  118. } else {
  119. tns_check_view(tns, False);
  120. cnt = tns->cols * tns->rows;
  121. if ((r = tns->first + cnt - tns->cnt) >= tns->cols)
  122. tns->first -= r - r % tns->cols;
  123. if (r > 0)
  124. cnt -= r % tns->cols;
  125. }
  126. r = cnt % tns->cols ? 1 : 0;
  127. tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5;
  128. tns->y = y = (win->h - (cnt / tns->cols + r) * thumb_dim) / 2 + 5;
  129. for (i = 0; i < cnt; ++i) {
  130. t = &tns->thumbs[tns->first + i];
  131. t->x = x + (THUMB_SIZE - t->w) / 2;
  132. t->y = y + (THUMB_SIZE - t->h) / 2;
  133. imlib_context_set_image(t->im);
  134. imlib_render_image_part_on_drawable_at_size(0, 0, t->w, t->h,
  135. t->x, t->y, t->w, t->h);
  136. if ((i + 1) % tns->cols == 0) {
  137. x = tns->x;
  138. y += thumb_dim;
  139. } else {
  140. x += thumb_dim;
  141. }
  142. }
  143. tns->dirty = 0;
  144. tns_highlight(tns, win, tns->sel, True);
  145. }
  146. void tns_highlight(tns_t *tns, win_t *win, int n, Bool hl) {
  147. thumb_t *t;
  148. unsigned long col;
  149. if (!tns || !win)
  150. return;
  151. if (n >= 0 && n < tns->cnt) {
  152. t = &tns->thumbs[n];
  153. if (hl)
  154. col = win->selcol;
  155. else if (win->fullscreen)
  156. col = win->black;
  157. else
  158. col = win->bgcol;
  159. win_draw_rect(win, win->pm, t->x - 2, t->y - 2, t->w + 4, t->h + 4,
  160. False, 2, col);
  161. }
  162. win_draw(win);
  163. }
  164. int tns_move_selection(tns_t *tns, win_t *win, tnsdir_t dir) {
  165. int old;
  166. if (!tns || !win)
  167. return 0;
  168. old = tns->sel;
  169. switch (dir) {
  170. case TNS_LEFT:
  171. if (tns->sel > 0)
  172. --tns->sel;
  173. break;
  174. case TNS_RIGHT:
  175. if (tns->sel < tns->cnt - 1)
  176. ++tns->sel;
  177. break;
  178. case TNS_UP:
  179. if (tns->sel >= tns->cols)
  180. tns->sel -= tns->cols;
  181. break;
  182. case TNS_DOWN:
  183. if (tns->sel + tns->cols < tns->cnt)
  184. tns->sel += tns->cols;
  185. break;
  186. }
  187. if (tns->sel != old) {
  188. tns_highlight(tns, win, old, False);
  189. tns_check_view(tns, False);
  190. if (!tns->dirty)
  191. tns_highlight(tns, win, tns->sel, True);
  192. }
  193. return tns->sel != old;
  194. }
  195. int tns_scroll(tns_t *tns, tnsdir_t dir) {
  196. int old;
  197. if (!tns)
  198. return 0;
  199. old = tns->first;
  200. if (dir == TNS_DOWN && tns->first + tns->cols * tns->rows < tns->cnt) {
  201. tns->first += tns->cols;
  202. tns_check_view(tns, True);
  203. tns->dirty = 1;
  204. } else if (dir == TNS_UP && tns->first >= tns->cols) {
  205. tns->first -= tns->cols;
  206. tns_check_view(tns, True);
  207. tns->dirty = 1;
  208. }
  209. return tns->first != old;
  210. }
  211. int tns_translate(tns_t *tns, int x, int y) {
  212. int n;
  213. thumb_t *t;
  214. if (!tns || x < tns->x || y < tns->y)
  215. return -1;
  216. n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
  217. (x - tns->x) / thumb_dim;
  218. if (n < tns->cnt) {
  219. t = &tns->thumbs[n];
  220. if (x >= t->x && x <= t->x + t->w && y >= t->y && y <= t->y + t->h)
  221. return n;
  222. }
  223. return -1;
  224. }