A Simple X Image Viewer
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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