A Simple X Image Viewer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

192 line
4.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. }
  33. void tns_free(tns_t *tns, win_t *win) {
  34. int i;
  35. if (!tns)
  36. return;
  37. for (i = 0; i < tns->cnt; ++i)
  38. win_free_pixmap(win, tns->thumbs[i].pm);
  39. free(tns->thumbs);
  40. tns->thumbs = NULL;
  41. }
  42. void tns_load(tns_t *tns, win_t *win, const char *filename) {
  43. int w, h;
  44. float z, zw, zh;
  45. thumb_t *t;
  46. Imlib_Image *im;
  47. if (!tns || !win || !filename)
  48. return;
  49. if ((im = imlib_load_image(filename)))
  50. imlib_context_set_image(im);
  51. else
  52. imlib_context_set_image(im_broken);
  53. w = imlib_image_get_width();
  54. h = imlib_image_get_height();
  55. zw = (float) THUMB_SIZE / (float) w;
  56. zh = (float) THUMB_SIZE / (float) h;
  57. z = MIN(zw, zh);
  58. if (!im && z > 1.0)
  59. z = 1.0;
  60. t = &tns->thumbs[tns->cnt++];
  61. t->w = z * w;
  62. t->h = z * h;
  63. t->pm = win_create_pixmap(win, t->w, t->h);
  64. imlib_context_set_drawable(t->pm);
  65. imlib_render_image_part_on_drawable_at_size(0, 0, w, h,
  66. 0, 0, t->w, t->h);
  67. imlib_free_image();
  68. }
  69. void tns_render(tns_t *tns, win_t *win) {
  70. int i, cnt, x, y;
  71. if (!tns || !win)
  72. return;
  73. tns->cols = MAX(1, win->w / thumb_dim);
  74. tns->rows = MAX(1, win->h / thumb_dim);
  75. cnt = tns->cols * tns->rows;
  76. if (tns->first && tns->first + cnt > tns->cnt)
  77. tns->first = MAX(0, tns->cnt - cnt);
  78. cnt = MIN(tns->first + cnt, tns->cnt);
  79. win_clear(win);
  80. i = cnt % tns->cols ? 1 : 0;
  81. tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5;
  82. tns->y = y = (win->h - (cnt / tns->cols + i) * thumb_dim) / 2 + 5;
  83. i = tns->first;
  84. while (i < cnt) {
  85. tns->thumbs[i].x = x + (THUMB_SIZE - tns->thumbs[i].w) / 2;
  86. tns->thumbs[i].y = y + (THUMB_SIZE - tns->thumbs[i].h) / 2;
  87. win_draw_pixmap(win, tns->thumbs[i].pm, tns->thumbs[i].x,
  88. tns->thumbs[i].y, tns->thumbs[i].w, tns->thumbs[i].h);
  89. if (++i % tns->cols == 0) {
  90. x = tns->x;
  91. y += thumb_dim;
  92. } else {
  93. x += thumb_dim;
  94. }
  95. }
  96. tns_highlight(tns, win, -1);
  97. }
  98. void tns_highlight(tns_t *tns, win_t *win, int old) {
  99. thumb_t *t;
  100. if (!tns || !win)
  101. return;
  102. if (old >= 0 && old < tns->cnt) {
  103. t = &tns->thumbs[old];
  104. win_draw_rect(win, t->x - 2, t->y - 2, t->w + 4, t->h + 4, False);
  105. }
  106. if (tns->sel < tns->cnt) {
  107. t = &tns->thumbs[tns->sel];
  108. win_draw_rect(win, t->x - 2, t->y - 2, t->w + 4, t->h + 4, True);
  109. }
  110. win_draw(win);
  111. }
  112. void tns_move_selection(tns_t *tns, win_t *win, movedir_t dir) {
  113. int sel, old;
  114. if (!tns || !win)
  115. return;
  116. sel = tns->sel;
  117. switch (dir) {
  118. case MOVE_LEFT:
  119. if (sel % tns->cols > 0)
  120. --sel;
  121. break;
  122. case MOVE_RIGHT:
  123. if (sel % tns->cols < tns->cols - 1 && sel < tns->cnt - 1)
  124. ++sel;
  125. break;
  126. case MOVE_UP:
  127. if (sel / tns->cols > 0)
  128. sel -= tns->cols;
  129. break;
  130. case MOVE_DOWN:
  131. if (sel / tns->cols < tns->rows - 1 && sel + tns->cols < tns->cnt)
  132. sel += tns->cols;
  133. break;
  134. }
  135. if (sel != tns->sel && tns->thumbs[sel].x != 0) {
  136. old = tns->sel;
  137. tns->sel = sel;
  138. tns_highlight(tns, win, old);
  139. }
  140. }
  141. int tns_translate(tns_t *tns, int x, int y) {
  142. int n;
  143. thumb_t *t;
  144. if (!tns || x < tns->x || y < tns->y)
  145. return -1;
  146. n = (y - tns->y) / thumb_dim * tns->cols + (x - tns->x) / thumb_dim;
  147. if (n < tns->cnt) {
  148. t = &tns->thumbs[n];
  149. if (x >= t->x && x <= t->x + t->w && y >= t->y && y <= t->y + t->h)
  150. return n;
  151. }
  152. return -1;
  153. }