A Simple X Image Viewer
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

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