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.
 
 
 
 
 
 

263 linhas
5.7 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. imlib_image_set_changes_on_disk();
  58. } else {
  59. imlib_context_set_image(im_broken);
  60. }
  61. w = imlib_image_get_width();
  62. h = imlib_image_get_height();
  63. zw = (float) THUMB_SIZE / (float) w;
  64. zh = (float) THUMB_SIZE / (float) h;
  65. z = MIN(zw, zh);
  66. if (!im && z > 1.0)
  67. z = 1.0;
  68. t = &tns->thumbs[n];
  69. t->w = z * w;
  70. t->h = z * h;
  71. if (t->pm)
  72. win_free_pixmap(win, t->pm);
  73. t->pm = win_create_pixmap(win, t->w, t->h);
  74. imlib_context_set_drawable(t->pm);
  75. imlib_context_set_anti_alias(1);
  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();
  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. if (!tns || !win)
  145. return;
  146. if (n >= 0 && n < tns->cnt) {
  147. t = &tns->thumbs[n];
  148. win_draw_rect(win, t->x - 2, t->y - 2, t->w + 4, t->h + 4, hl);
  149. }
  150. win_draw(win);
  151. }
  152. int tns_move_selection(tns_t *tns, win_t *win, tnsdir_t dir) {
  153. int old;
  154. if (!tns || !win)
  155. return 0;
  156. old = tns->sel;
  157. switch (dir) {
  158. case TNS_LEFT:
  159. if (tns->sel > 0)
  160. --tns->sel;
  161. break;
  162. case TNS_RIGHT:
  163. if (tns->sel < tns->cnt - 1)
  164. ++tns->sel;
  165. break;
  166. case TNS_UP:
  167. if (tns->sel >= tns->cols)
  168. tns->sel -= tns->cols;
  169. break;
  170. case TNS_DOWN:
  171. if (tns->sel + tns->cols < tns->cnt)
  172. tns->sel += tns->cols;
  173. break;
  174. }
  175. if (tns->sel != old) {
  176. tns_highlight(tns, win, old, False);
  177. tns_check_view(tns, False);
  178. if (!tns->dirty)
  179. tns_highlight(tns, win, tns->sel, True);
  180. }
  181. return tns->sel != old;
  182. }
  183. int tns_scroll(tns_t *tns, tnsdir_t dir) {
  184. int old;
  185. if (!tns)
  186. return 0;
  187. old = tns->first;
  188. if (dir == TNS_DOWN && tns->first + tns->cols * tns->rows < tns->cnt) {
  189. tns->first += tns->cols;
  190. tns_check_view(tns, True);
  191. tns->dirty = 1;
  192. } else if (dir == TNS_UP && tns->first >= tns->cols) {
  193. tns->first -= tns->cols;
  194. tns_check_view(tns, True);
  195. tns->dirty = 1;
  196. }
  197. return tns->first != old;
  198. }
  199. int tns_translate(tns_t *tns, int x, int y) {
  200. int n;
  201. thumb_t *t;
  202. if (!tns || x < tns->x || y < tns->y)
  203. return -1;
  204. n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
  205. (x - tns->x) / thumb_dim;
  206. if (n < tns->cnt) {
  207. t = &tns->thumbs[n];
  208. if (x >= t->x && x <= t->x + t->w && y >= t->y && y <= t->y + t->h)
  209. return n;
  210. }
  211. return -1;
  212. }