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.
 
 
 
 
 
 

225 Zeilen
4.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 int fileidx;
  25. extern Imlib_Image *im_broken;
  26. const int thumb_dim = THUMB_SIZE + 10;
  27. void tns_init(tns_t *tns, int cnt) {
  28. if (!tns)
  29. return;
  30. tns->cnt = tns->first = 0;
  31. tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t));
  32. memset(tns->thumbs, 0, cnt * sizeof(thumb_t));
  33. tns->dirty = 0;
  34. }
  35. void tns_free(tns_t *tns, win_t *win) {
  36. int i;
  37. if (!tns)
  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, 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 ((im = imlib_load_image(filename)))
  52. imlib_context_set_image(im);
  53. else
  54. imlib_context_set_image(im_broken);
  55. w = imlib_image_get_width();
  56. h = imlib_image_get_height();
  57. zw = (float) THUMB_SIZE / (float) w;
  58. zh = (float) THUMB_SIZE / (float) h;
  59. z = MIN(zw, zh);
  60. if (!im && z > 1.0)
  61. z = 1.0;
  62. t = &tns->thumbs[tns->cnt++];
  63. t->w = z * w;
  64. t->h = z * h;
  65. t->pm = win_create_pixmap(win, t->w, t->h);
  66. imlib_context_set_drawable(t->pm);
  67. imlib_render_image_part_on_drawable_at_size(0, 0, w, h,
  68. 0, 0, t->w, t->h);
  69. imlib_free_image();
  70. tns->dirty = 1;
  71. }
  72. void tns_check_view(tns_t *tns, Bool scrolled) {
  73. if (!tns)
  74. return;
  75. tns->first -= tns->first % tns->cols;
  76. if (scrolled) {
  77. /* move selection into visible area */
  78. } else {
  79. /* scroll to selection */
  80. if (tns->first + tns->cols * tns->rows <= fileidx) {
  81. tns->first = fileidx - fileidx % tns->cols -
  82. tns->cols * (tns->rows - 1);
  83. tns->dirty = 1;
  84. } else if (tns->first > fileidx) {
  85. tns->first = fileidx - fileidx % tns->cols;
  86. tns->dirty = 1;
  87. }
  88. }
  89. }
  90. void tns_render(tns_t *tns, win_t *win) {
  91. int i, cnt, r, x, y;
  92. thumb_t *t;
  93. if (!tns || !tns->dirty || !win)
  94. return;
  95. win_clear(win);
  96. tns->cols = MAX(1, win->w / thumb_dim);
  97. tns->rows = MAX(1, win->h / thumb_dim);
  98. if (tns->cnt < tns->cols * tns->rows) {
  99. tns->first = 0;
  100. cnt = tns->cnt;
  101. } else {
  102. tns_check_view(tns, False);
  103. cnt = tns->cols * tns->rows;
  104. if ((r = tns->first + cnt - tns->cnt) >= tns->cols)
  105. tns->first -= r - r % tns->cols;
  106. if (r > 0)
  107. cnt -= r % tns->cols;
  108. }
  109. r = cnt % tns->cols ? 1 : 0;
  110. tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5;
  111. tns->y = y = (win->h - (cnt / tns->cols + r) * thumb_dim) / 2 + 5;
  112. for (i = 0; i < cnt; ++i) {
  113. t = &tns->thumbs[tns->first + i];
  114. t->x = x + (THUMB_SIZE - t->w) / 2;
  115. t->y = y + (THUMB_SIZE - t->h) / 2;
  116. win_draw_pixmap(win, t->pm, t->x, t->y, t->w, t->h);
  117. if ((i + 1) % tns->cols == 0) {
  118. x = tns->x;
  119. y += thumb_dim;
  120. } else {
  121. x += thumb_dim;
  122. }
  123. }
  124. tns->dirty = 0;
  125. tns_highlight(tns, win, fileidx, True);
  126. }
  127. void tns_highlight(tns_t *tns, win_t *win, int n, Bool hl) {
  128. thumb_t *t;
  129. if (!tns || !win)
  130. return;
  131. if (n >= 0 && n < tns->cnt) {
  132. t = &tns->thumbs[n];
  133. win_draw_rect(win, t->x - 2, t->y - 2, t->w + 4, t->h + 4, hl);
  134. }
  135. win_draw(win);
  136. }
  137. int tns_move_selection(tns_t *tns, win_t *win, movedir_t dir) {
  138. int old;
  139. if (!tns || !win)
  140. return 0;
  141. old = fileidx;
  142. switch (dir) {
  143. case MOVE_LEFT:
  144. if (fileidx > 0)
  145. --fileidx;
  146. break;
  147. case MOVE_RIGHT:
  148. if (fileidx < tns->cnt - 1)
  149. ++fileidx;
  150. break;
  151. case MOVE_UP:
  152. if (fileidx >= tns->cols)
  153. fileidx -= tns->cols;
  154. break;
  155. case MOVE_DOWN:
  156. if (fileidx + tns->cols < tns->cnt)
  157. fileidx += tns->cols;
  158. break;
  159. }
  160. if (fileidx != old) {
  161. tns_highlight(tns, win, old, False);
  162. tns_check_view(tns, False);
  163. if (!tns->dirty)
  164. tns_highlight(tns, win, fileidx, True);
  165. }
  166. return fileidx != old;
  167. }
  168. int tns_translate(tns_t *tns, int x, int y) {
  169. int n;
  170. thumb_t *t;
  171. if (!tns || x < tns->x || y < tns->y)
  172. return -1;
  173. n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
  174. (x - tns->x) / thumb_dim;
  175. if (n < tns->cnt) {
  176. t = &tns->thumbs[n];
  177. if (x >= t->x && x <= t->x + t->w && y >= t->y && y <= t->y + t->h)
  178. return n;
  179. }
  180. return -1;
  181. }