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.
 
 
 
 
 
 

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