A Simple X Image Viewer
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

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