A Simple X Image Viewer
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

192 lignes
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. }
  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. i = cnt % tns->cols ? 1 : 0;
  80. tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5;
  81. tns->y = y = (win->h - (cnt / tns->cols + i) * thumb_dim) / 2 + 5;
  82. i = tns->first;
  83. while (i < cnt) {
  84. tns->thumbs[i].x = x + (THUMB_SIZE - tns->thumbs[i].w) / 2;
  85. tns->thumbs[i].y = y + (THUMB_SIZE - tns->thumbs[i].h) / 2;
  86. win_draw_pixmap(win, tns->thumbs[i].pm, tns->thumbs[i].x,
  87. tns->thumbs[i].y, tns->thumbs[i].w, tns->thumbs[i].h);
  88. if (++i % tns->cols == 0) {
  89. x = tns->x;
  90. y += thumb_dim;
  91. } else {
  92. x += thumb_dim;
  93. }
  94. }
  95. printf("%d, %d\n", tns->sel, tns->cnt);
  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;
  114. if (!tns || !win)
  115. return;
  116. sel = tns->sel;
  117. switch (dir) {
  118. case MOVE_LEFT:
  119. if (sel % tns->cols > 0) {
  120. --tns->sel;
  121. tns_highlight(tns, win, sel);
  122. }
  123. break;
  124. case MOVE_RIGHT:
  125. if (sel % tns->cols < tns->cols - 1 && sel < tns->cnt - 1) {
  126. ++tns->sel;
  127. tns_highlight(tns, win, sel);
  128. }
  129. break;
  130. case MOVE_UP:
  131. if (sel / tns->cols > 0) {
  132. tns->sel -= tns->cols;
  133. tns_highlight(tns, win, sel);
  134. }
  135. break;
  136. case MOVE_DOWN:
  137. if (sel / tns->cols < tns->rows - 1 && sel + tns->cols < tns->cnt) {
  138. tns->sel += tns->cols;
  139. tns_highlight(tns, win, sel);
  140. }
  141. break;
  142. }
  143. }
  144. int tns_translate(tns_t *tns, int x, int y) {
  145. int n;
  146. thumb_t *t;
  147. if (!tns || x < tns->x || y < tns->y)
  148. return -1;
  149. if ((n = (y - tns->y) / thumb_dim * tns->cols + (x - tns->x) / thumb_dim) < tns->cnt) {
  150. t = &tns->thumbs[n];
  151. if (x > t->x && x < t->x + t->w && y > t->y && y < t->y + t->h)
  152. return n;
  153. }
  154. return -1;
  155. }