A Simple X Image Viewer
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

250 satır
5.4 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->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. win_free_pixmap(win, tns->thumbs[i].pm);
  40. free(tns->thumbs);
  41. tns->thumbs = NULL;
  42. }
  43. void tns_load(tns_t *tns, win_t *win, const char *filename) {
  44. int w, h;
  45. float z, zw, zh;
  46. thumb_t *t;
  47. Imlib_Image *im;
  48. if (!tns || !win || !filename)
  49. return;
  50. if ((im = imlib_load_image(filename)))
  51. imlib_context_set_image(im);
  52. else
  53. imlib_context_set_image(im_broken);
  54. w = imlib_image_get_width();
  55. h = imlib_image_get_height();
  56. zw = (float) THUMB_SIZE / (float) w;
  57. zh = (float) THUMB_SIZE / (float) h;
  58. z = MIN(zw, zh);
  59. if (!im && z > 1.0)
  60. z = 1.0;
  61. t = &tns->thumbs[tns->cnt++];
  62. t->w = z * w;
  63. t->h = z * h;
  64. t->pm = win_create_pixmap(win, t->w, t->h);
  65. imlib_context_set_drawable(t->pm);
  66. imlib_render_image_part_on_drawable_at_size(0, 0, w, h,
  67. 0, 0, t->w, t->h);
  68. imlib_free_image();
  69. tns->dirty = 1;
  70. }
  71. void tns_check_view(tns_t *tns, Bool scrolled) {
  72. int r;
  73. if (!tns)
  74. return;
  75. tns->first -= tns->first % tns->cols;
  76. r = tns->sel % tns->cols;
  77. if (scrolled) {
  78. /* move selection into visible area */
  79. if (tns->sel >= tns->first + tns->cols * tns->rows)
  80. tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
  81. else if (tns->sel < tns->first)
  82. tns->sel = tns->first + r;
  83. } else {
  84. /* scroll to selection */
  85. if (tns->first + tns->cols * tns->rows <= tns->sel) {
  86. tns->first = tns->sel - r - tns->cols * (tns->rows - 1);
  87. tns->dirty = 1;
  88. } else if (tns->first > tns->sel) {
  89. tns->first = tns->sel - r;
  90. tns->dirty = 1;
  91. }
  92. }
  93. }
  94. void tns_render(tns_t *tns, win_t *win) {
  95. int i, cnt, r, x, y;
  96. thumb_t *t;
  97. if (!tns || !tns->dirty || !win)
  98. return;
  99. win_clear(win);
  100. tns->cols = MAX(1, win->w / thumb_dim);
  101. tns->rows = MAX(1, win->h / thumb_dim);
  102. if (tns->cnt < tns->cols * tns->rows) {
  103. tns->first = 0;
  104. cnt = tns->cnt;
  105. } else {
  106. tns_check_view(tns, False);
  107. cnt = tns->cols * tns->rows;
  108. if ((r = tns->first + cnt - tns->cnt) >= tns->cols)
  109. tns->first -= r - r % tns->cols;
  110. if (r > 0)
  111. cnt -= r % tns->cols;
  112. }
  113. r = cnt % tns->cols ? 1 : 0;
  114. tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5;
  115. tns->y = y = (win->h - (cnt / tns->cols + r) * thumb_dim) / 2 + 5;
  116. for (i = 0; i < cnt; ++i) {
  117. t = &tns->thumbs[tns->first + i];
  118. t->x = x + (THUMB_SIZE - t->w) / 2;
  119. t->y = y + (THUMB_SIZE - t->h) / 2;
  120. win_draw_pixmap(win, t->pm, t->x, t->y, t->w, t->h);
  121. if ((i + 1) % tns->cols == 0) {
  122. x = tns->x;
  123. y += thumb_dim;
  124. } else {
  125. x += thumb_dim;
  126. }
  127. }
  128. tns->dirty = 0;
  129. tns_highlight(tns, win, tns->sel, True);
  130. }
  131. void tns_highlight(tns_t *tns, win_t *win, int n, Bool hl) {
  132. thumb_t *t;
  133. if (!tns || !win)
  134. return;
  135. if (n >= 0 && n < tns->cnt) {
  136. t = &tns->thumbs[n];
  137. win_draw_rect(win, t->x - 2, t->y - 2, t->w + 4, t->h + 4, hl);
  138. }
  139. win_draw(win);
  140. }
  141. int tns_move_selection(tns_t *tns, win_t *win, tnsdir_t dir) {
  142. int old;
  143. if (!tns || !win)
  144. return 0;
  145. old = tns->sel;
  146. switch (dir) {
  147. case TNS_LEFT:
  148. if (tns->sel > 0)
  149. --tns->sel;
  150. break;
  151. case TNS_RIGHT:
  152. if (tns->sel < tns->cnt - 1)
  153. ++tns->sel;
  154. break;
  155. case TNS_UP:
  156. if (tns->sel >= tns->cols)
  157. tns->sel -= tns->cols;
  158. break;
  159. case TNS_DOWN:
  160. if (tns->sel + tns->cols < tns->cnt)
  161. tns->sel += tns->cols;
  162. break;
  163. }
  164. if (tns->sel != old) {
  165. tns_highlight(tns, win, old, False);
  166. tns_check_view(tns, False);
  167. if (!tns->dirty)
  168. tns_highlight(tns, win, tns->sel, True);
  169. }
  170. return tns->sel != old;
  171. }
  172. int tns_scroll(tns_t *tns, tnsdir_t dir) {
  173. int old;
  174. if (!tns)
  175. return 0;
  176. old = tns->first;
  177. if (dir == TNS_DOWN && tns->first + tns->cols * tns->rows < tns->cnt) {
  178. tns->first += tns->cols;
  179. tns_check_view(tns, True);
  180. tns->dirty = 1;
  181. } else if (dir == TNS_UP && tns->first >= tns->cols) {
  182. tns->first -= tns->cols;
  183. tns_check_view(tns, True);
  184. tns->dirty = 1;
  185. }
  186. return tns->first != old;
  187. }
  188. int tns_translate(tns_t *tns, int x, int y) {
  189. int n;
  190. thumb_t *t;
  191. if (!tns || x < tns->x || y < tns->y)
  192. return -1;
  193. n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
  194. (x - tns->x) / thumb_dim;
  195. if (n < tns->cnt) {
  196. t = &tns->thumbs[n];
  197. if (x >= t->x && x <= t->x + t->w && y >= t->y && y <= t->y + t->h)
  198. return n;
  199. }
  200. return -1;
  201. }