2013-02-08 20:52:41 +00:00
|
|
|
/* Copyright 2011 Bert Muennich
|
2011-02-16 20:40:20 +00:00
|
|
|
*
|
2013-02-08 20:52:41 +00:00
|
|
|
* This file is part of sxiv.
|
2011-08-18 00:18:26 +01:00
|
|
|
*
|
2013-02-08 20:52:41 +00:00
|
|
|
* sxiv is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published
|
|
|
|
* by the Free Software Foundation; either version 2 of the License,
|
|
|
|
* or (at your option) any later version.
|
2011-08-18 00:18:26 +01:00
|
|
|
*
|
2013-02-08 20:52:41 +00:00
|
|
|
* sxiv is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with sxiv. If not, see <http://www.gnu.org/licenses/>.
|
2011-02-16 20:40:20 +00:00
|
|
|
*/
|
|
|
|
|
2011-09-08 19:54:24 +01:00
|
|
|
#define _POSIX_C_SOURCE 200112L
|
2011-09-11 20:01:24 +01:00
|
|
|
#define _THUMBS_CONFIG
|
2011-09-08 19:54:24 +01:00
|
|
|
|
2014-06-09 21:59:49 +01:00
|
|
|
#include <stdio.h>
|
2011-02-16 20:40:20 +00:00
|
|
|
#include <stdlib.h>
|
2011-02-16 22:03:42 +00:00
|
|
|
#include <string.h>
|
2011-04-07 00:26:08 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2011-09-08 19:54:24 +01:00
|
|
|
#include <utime.h>
|
2011-02-16 20:40:20 +00:00
|
|
|
|
|
|
|
#include "thumbs.h"
|
|
|
|
#include "util.h"
|
2011-07-22 13:49:06 +01:00
|
|
|
#include "config.h"
|
2011-02-16 20:40:20 +00:00
|
|
|
|
2014-06-09 21:59:49 +01:00
|
|
|
#if HAVE_LIBEXIF
|
|
|
|
#include <libexif/exif-data.h>
|
|
|
|
void exif_auto_orientate(const fileinfo_t*);
|
|
|
|
#endif
|
|
|
|
|
2013-01-08 19:55:02 +00:00
|
|
|
static const int thumb_dim = THUMB_SIZE + 10;
|
|
|
|
|
|
|
|
static char *cache_dir = NULL;
|
2011-02-16 20:40:20 +00:00
|
|
|
|
2013-02-08 21:05:31 +00:00
|
|
|
bool tns_cache_enabled(void)
|
|
|
|
{
|
2011-04-08 09:24:10 +01:00
|
|
|
struct stat stats;
|
|
|
|
|
2011-09-29 11:43:36 +01:00
|
|
|
return cache_dir != NULL && stat(cache_dir, &stats) == 0 &&
|
|
|
|
S_ISDIR(stats.st_mode) && access(cache_dir, W_OK) == 0;
|
2011-04-08 09:24:10 +01:00
|
|
|
}
|
|
|
|
|
2013-02-08 21:05:31 +00:00
|
|
|
char* tns_cache_filepath(const char *filepath)
|
|
|
|
{
|
2011-04-08 09:24:10 +01:00
|
|
|
size_t len;
|
|
|
|
char *cfile = NULL;
|
|
|
|
|
2011-09-29 11:43:36 +01:00
|
|
|
if (cache_dir == NULL || filepath == NULL || *filepath != '/')
|
2011-04-08 09:24:10 +01:00
|
|
|
return NULL;
|
|
|
|
|
2011-09-29 11:43:36 +01:00
|
|
|
if (strncmp(filepath, cache_dir, strlen(cache_dir)) != 0) {
|
|
|
|
/* don't cache images inside the cache directory! */
|
2011-08-17 23:38:55 +01:00
|
|
|
len = strlen(cache_dir) + strlen(filepath) + 6;
|
2011-04-08 09:24:10 +01:00
|
|
|
cfile = (char*) s_malloc(len);
|
2011-08-17 23:38:55 +01:00
|
|
|
snprintf(cfile, len, "%s/%s.png", cache_dir, filepath + 1);
|
2011-04-08 09:24:10 +01:00
|
|
|
}
|
|
|
|
return cfile;
|
|
|
|
}
|
|
|
|
|
2013-12-13 12:04:45 +00:00
|
|
|
Imlib_Image tns_cache_load(const char *filepath)
|
2013-02-08 21:05:31 +00:00
|
|
|
{
|
2011-04-08 09:24:10 +01:00
|
|
|
char *cfile;
|
|
|
|
struct stat cstats, fstats;
|
2013-12-13 12:04:45 +00:00
|
|
|
Imlib_Image im = NULL;
|
2011-04-08 09:24:10 +01:00
|
|
|
|
2011-09-29 11:43:36 +01:00
|
|
|
if (filepath == NULL)
|
2011-04-08 13:44:00 +01:00
|
|
|
return NULL;
|
2011-09-29 11:43:36 +01:00
|
|
|
if (stat(filepath, &fstats) < 0)
|
2011-04-08 09:24:10 +01:00
|
|
|
return NULL;
|
|
|
|
|
2011-09-29 11:43:36 +01:00
|
|
|
if ((cfile = tns_cache_filepath(filepath)) != NULL) {
|
|
|
|
if (stat(cfile, &cstats) == 0 && cstats.st_mtime == fstats.st_mtime)
|
2011-04-08 09:24:10 +01:00
|
|
|
im = imlib_load_image(cfile);
|
|
|
|
free(cfile);
|
|
|
|
}
|
|
|
|
return im;
|
|
|
|
}
|
|
|
|
|
2013-02-08 21:05:31 +00:00
|
|
|
void tns_cache_write(thumb_t *t, bool force)
|
|
|
|
{
|
2011-04-08 09:24:10 +01:00
|
|
|
char *cfile, *dirend;
|
|
|
|
struct stat cstats, fstats;
|
2011-09-08 19:54:24 +01:00
|
|
|
struct utimbuf times;
|
2011-04-08 09:24:10 +01:00
|
|
|
Imlib_Load_Error err = 0;
|
|
|
|
|
2011-09-29 11:43:36 +01:00
|
|
|
if (t == NULL || t->im == NULL)
|
2011-04-08 09:24:10 +01:00
|
|
|
return;
|
2011-09-29 11:43:36 +01:00
|
|
|
if (t->file == NULL || t->file->name == NULL || t->file->path == NULL)
|
|
|
|
return;
|
|
|
|
if (stat(t->file->path, &fstats) < 0)
|
2011-04-08 09:24:10 +01:00
|
|
|
return;
|
|
|
|
|
2011-09-29 11:43:36 +01:00
|
|
|
if ((cfile = tns_cache_filepath(t->file->path)) != NULL) {
|
|
|
|
if (force || stat(cfile, &cstats) < 0 ||
|
|
|
|
cstats.st_mtime != fstats.st_mtime)
|
|
|
|
{
|
|
|
|
if ((dirend = strrchr(cfile, '/')) != NULL) {
|
2011-04-08 09:24:10 +01:00
|
|
|
*dirend = '\0';
|
|
|
|
err = r_mkdir(cfile);
|
|
|
|
*dirend = '/';
|
|
|
|
}
|
|
|
|
|
2011-09-29 11:43:36 +01:00
|
|
|
if (err == 0) {
|
2011-04-08 09:24:10 +01:00
|
|
|
imlib_context_set_image(t->im);
|
|
|
|
imlib_image_set_format("png");
|
|
|
|
imlib_save_image_with_error_return(cfile, &err);
|
|
|
|
}
|
|
|
|
|
2011-09-29 11:43:36 +01:00
|
|
|
if (err == 0) {
|
2011-09-08 19:54:24 +01:00
|
|
|
times.actime = fstats.st_atime;
|
|
|
|
times.modtime = fstats.st_mtime;
|
|
|
|
utime(cfile, ×);
|
2011-09-29 11:43:36 +01:00
|
|
|
} else {
|
|
|
|
warn("could not cache thumbnail: %s", t->file->name);
|
2011-04-08 09:24:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
free(cfile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-08 21:05:31 +00:00
|
|
|
void tns_clean_cache(tns_t *tns)
|
|
|
|
{
|
2011-09-11 20:01:24 +01:00
|
|
|
int dirlen;
|
|
|
|
bool delete;
|
2011-04-08 13:44:00 +01:00
|
|
|
char *cfile, *filename, *tpos;
|
|
|
|
r_dir_t dir;
|
|
|
|
|
2011-09-29 11:43:36 +01:00
|
|
|
if (cache_dir == NULL)
|
2011-04-08 13:44:00 +01:00
|
|
|
return;
|
|
|
|
|
2011-09-29 11:43:36 +01:00
|
|
|
if (r_opendir(&dir, cache_dir) < 0) {
|
2011-04-08 13:44:00 +01:00
|
|
|
warn("could not open thumbnail cache directory: %s", cache_dir);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dirlen = strlen(cache_dir);
|
|
|
|
|
2011-09-29 11:43:36 +01:00
|
|
|
while ((cfile = r_readdir(&dir)) != NULL) {
|
2011-04-08 13:44:00 +01:00
|
|
|
filename = cfile + dirlen;
|
2011-09-11 20:01:24 +01:00
|
|
|
delete = false;
|
2011-04-08 13:44:00 +01:00
|
|
|
|
2011-09-29 11:43:36 +01:00
|
|
|
if ((tpos = strrchr(filename, '.')) != NULL) {
|
2011-04-08 13:44:00 +01:00
|
|
|
*tpos = '\0';
|
2011-09-29 11:43:36 +01:00
|
|
|
if (access(filename, F_OK) < 0)
|
2011-09-11 20:01:24 +01:00
|
|
|
delete = true;
|
2011-04-08 13:44:00 +01:00
|
|
|
*tpos = '.';
|
|
|
|
}
|
2011-09-29 11:43:36 +01:00
|
|
|
if (delete) {
|
|
|
|
if (unlink(cfile) < 0)
|
|
|
|
warn("could not delete cache file: %s", cfile);
|
|
|
|
}
|
2011-04-08 13:44:00 +01:00
|
|
|
free(cfile);
|
|
|
|
}
|
|
|
|
r_closedir(&dir);
|
|
|
|
}
|
|
|
|
|
2011-04-07 00:26:08 +01:00
|
|
|
|
2013-02-08 21:05:31 +00:00
|
|
|
void tns_init(tns_t *tns, int cnt, win_t *win)
|
|
|
|
{
|
2011-04-07 16:23:47 +01:00
|
|
|
int len;
|
2014-01-11 21:47:41 +00:00
|
|
|
const char *homedir, *dsuffix = "";
|
2011-04-07 16:23:47 +01:00
|
|
|
|
2011-09-29 11:43:36 +01:00
|
|
|
if (tns == NULL)
|
2011-02-16 22:03:42 +00:00
|
|
|
return;
|
|
|
|
|
2011-09-29 11:43:36 +01:00
|
|
|
if (cnt > 0) {
|
2011-04-08 13:44:00 +01:00
|
|
|
tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t));
|
|
|
|
memset(tns->thumbs, 0, cnt * sizeof(thumb_t));
|
|
|
|
} else {
|
|
|
|
tns->thumbs = NULL;
|
|
|
|
}
|
|
|
|
|
2011-02-27 12:29:24 +00:00
|
|
|
tns->cap = cnt;
|
2011-09-17 16:23:51 +01:00
|
|
|
tns->cnt = tns->first = tns->sel = 0;
|
|
|
|
tns->win = win;
|
2011-09-11 20:01:24 +01:00
|
|
|
tns->dirty = false;
|
2011-04-07 16:23:47 +01:00
|
|
|
|
2014-01-11 21:47:41 +00:00
|
|
|
if ((homedir = getenv("XDG_CACHE_HOME")) == NULL || homedir[0] == '\0') {
|
|
|
|
homedir = getenv("HOME");
|
|
|
|
dsuffix = "/.cache";
|
|
|
|
}
|
|
|
|
if (homedir != NULL) {
|
2011-09-29 11:43:36 +01:00
|
|
|
if (cache_dir != NULL)
|
2011-04-07 16:23:47 +01:00
|
|
|
free(cache_dir);
|
2014-01-11 21:47:41 +00:00
|
|
|
len = strlen(homedir) + strlen(dsuffix) + 6;
|
2013-01-08 19:55:02 +00:00
|
|
|
cache_dir = (char*) s_malloc(len);
|
2014-01-11 21:47:41 +00:00
|
|
|
snprintf(cache_dir, len, "%s%s/sxiv", homedir, dsuffix);
|
2011-04-08 13:44:00 +01:00
|
|
|
} else {
|
|
|
|
warn("could not locate thumbnail cache directory");
|
2011-04-07 16:23:47 +01:00
|
|
|
}
|
2011-02-16 20:40:20 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 21:05:31 +00:00
|
|
|
void tns_free(tns_t *tns)
|
|
|
|
{
|
2011-02-16 20:40:20 +00:00
|
|
|
int i;
|
|
|
|
|
2012-05-13 21:01:09 +01:00
|
|
|
if (tns == NULL)
|
2011-02-16 20:40:20 +00:00
|
|
|
return;
|
|
|
|
|
2011-09-29 11:43:36 +01:00
|
|
|
if (tns->thumbs != NULL) {
|
2011-07-26 17:01:29 +01:00
|
|
|
for (i = 0; i < tns->cnt; i++) {
|
2011-09-29 11:43:36 +01:00
|
|
|
if (tns->thumbs[i].im != NULL) {
|
2011-04-08 13:44:00 +01:00
|
|
|
imlib_context_set_image(tns->thumbs[i].im);
|
|
|
|
imlib_free_image();
|
|
|
|
}
|
2011-03-15 12:55:52 +00:00
|
|
|
}
|
2011-04-08 13:44:00 +01:00
|
|
|
free(tns->thumbs);
|
|
|
|
tns->thumbs = NULL;
|
2011-03-15 12:55:52 +00:00
|
|
|
}
|
2011-02-16 20:40:20 +00:00
|
|
|
|
2011-09-29 11:43:36 +01:00
|
|
|
if (cache_dir != NULL) {
|
2011-04-07 16:23:47 +01:00
|
|
|
free(cache_dir);
|
|
|
|
cache_dir = NULL;
|
|
|
|
}
|
2011-02-16 20:40:20 +00:00
|
|
|
}
|
|
|
|
|
2011-09-11 20:01:24 +01:00
|
|
|
bool tns_load(tns_t *tns, int n, const fileinfo_t *file,
|
2012-02-11 01:42:52 +00:00
|
|
|
bool force, bool silent)
|
2011-08-19 12:26:58 +01:00
|
|
|
{
|
2011-02-17 10:04:58 +00:00
|
|
|
int w, h;
|
2011-09-11 20:01:24 +01:00
|
|
|
bool use_cache, cache_hit = false;
|
2011-02-17 10:04:58 +00:00
|
|
|
float z, zw, zh;
|
|
|
|
thumb_t *t;
|
2014-06-09 21:59:49 +01:00
|
|
|
Imlib_Image im = NULL;
|
2011-02-17 10:04:58 +00:00
|
|
|
|
2011-09-29 11:43:36 +01:00
|
|
|
if (tns == NULL || tns->thumbs == NULL)
|
|
|
|
return false;
|
|
|
|
if (file == NULL || file->name == NULL || file->path == NULL)
|
2011-09-11 20:01:24 +01:00
|
|
|
return false;
|
2011-04-11 15:58:38 +01:00
|
|
|
if (n < 0 || n >= tns->cap)
|
2011-09-11 20:01:24 +01:00
|
|
|
return false;
|
2011-02-27 12:29:24 +00:00
|
|
|
|
2011-03-15 12:55:52 +00:00
|
|
|
t = &tns->thumbs[n];
|
2011-08-17 23:38:55 +01:00
|
|
|
t->file = file;
|
2011-03-15 12:55:52 +00:00
|
|
|
|
2011-09-29 11:43:36 +01:00
|
|
|
if (t->im != NULL) {
|
2011-03-15 12:55:52 +00:00
|
|
|
imlib_context_set_image(t->im);
|
|
|
|
imlib_free_image();
|
|
|
|
}
|
|
|
|
|
2011-04-07 15:17:10 +01:00
|
|
|
if ((use_cache = tns_cache_enabled())) {
|
2011-09-29 11:43:36 +01:00
|
|
|
if (!force && (im = tns_cache_load(file->path)) != NULL)
|
2011-09-11 20:01:24 +01:00
|
|
|
cache_hit = true;
|
2011-04-07 15:17:10 +01:00
|
|
|
}
|
|
|
|
|
2011-09-29 11:43:36 +01:00
|
|
|
if (!cache_hit) {
|
2014-06-09 21:59:49 +01:00
|
|
|
#if HAVE_LIBEXIF
|
|
|
|
int pw = 0, ph = 0, x = 0, y = 0;
|
|
|
|
bool err;
|
|
|
|
ExifData *ed;
|
|
|
|
ExifEntry *entry;
|
|
|
|
ExifContent *ifd;
|
|
|
|
ExifByteOrder byte_order;
|
|
|
|
int tmpfd;
|
|
|
|
char tmppath[] = "/tmp/sxiv-XXXXXX";
|
|
|
|
Imlib_Image tmpim;
|
|
|
|
|
|
|
|
if ((ed = exif_data_new_from_file(file->path)) != NULL &&
|
|
|
|
ed->data != NULL && ed->size > 0)
|
|
|
|
{
|
|
|
|
if ((tmpfd = mkstemp(tmppath)) >= 0) {
|
|
|
|
err = write(tmpfd, ed->data, ed->size) != ed->size;
|
|
|
|
close(tmpfd);
|
|
|
|
|
|
|
|
if (!err && (tmpim = imlib_load_image(tmppath)) != NULL) {
|
|
|
|
byte_order = exif_data_get_byte_order(ed);
|
|
|
|
ifd = ed->ifd[EXIF_IFD_EXIF];
|
|
|
|
entry = exif_content_get_entry(ifd, EXIF_TAG_PIXEL_X_DIMENSION);
|
|
|
|
if (entry != NULL)
|
|
|
|
pw = exif_get_long(entry->data, byte_order);
|
|
|
|
entry = exif_content_get_entry(ifd, EXIF_TAG_PIXEL_Y_DIMENSION);
|
|
|
|
if (entry != NULL)
|
|
|
|
ph = exif_get_long(entry->data, byte_order);
|
|
|
|
|
|
|
|
imlib_context_set_image(tmpim);
|
|
|
|
w = imlib_image_get_width();
|
|
|
|
h = imlib_image_get_height();
|
|
|
|
|
|
|
|
if (pw > w && ph > h) {
|
|
|
|
zw = (float) pw / (float) w;
|
|
|
|
zh = (float) ph / (float) h;
|
|
|
|
if (zw < zh) {
|
|
|
|
pw /= zh;
|
|
|
|
x = (w - pw) / 2;
|
|
|
|
w = pw;
|
|
|
|
} else if (zw > zh) {
|
|
|
|
ph /= zw;
|
|
|
|
y = (h - ph) / 2;
|
|
|
|
h = ph;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((im = imlib_create_cropped_image(x, y, w, h)) == NULL)
|
|
|
|
die("could not allocate memory");
|
|
|
|
imlib_free_image_and_decache();
|
|
|
|
}
|
|
|
|
unlink(tmppath);
|
|
|
|
}
|
|
|
|
exif_data_unref(ed);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (im == NULL && (access(file->path, R_OK) < 0 ||
|
|
|
|
(im = imlib_load_image(file->path)) == NULL))
|
2011-09-29 11:43:36 +01:00
|
|
|
{
|
|
|
|
if (!silent)
|
|
|
|
warn("could not open image: %s", file->name);
|
|
|
|
return false;
|
|
|
|
}
|
2011-04-11 15:58:38 +01:00
|
|
|
}
|
|
|
|
imlib_context_set_image(im);
|
|
|
|
imlib_context_set_anti_alias(1);
|
2011-02-17 10:04:58 +00:00
|
|
|
|
2014-06-09 21:59:49 +01:00
|
|
|
#if HAVE_LIBEXIF
|
|
|
|
if (!cache_hit)
|
2012-02-15 18:13:44 +00:00
|
|
|
exif_auto_orientate(file);
|
2014-06-09 21:59:49 +01:00
|
|
|
#endif
|
2011-09-06 10:09:57 +01:00
|
|
|
|
2011-02-17 10:04:58 +00:00
|
|
|
w = imlib_image_get_width();
|
|
|
|
h = imlib_image_get_height();
|
2011-04-11 15:58:38 +01:00
|
|
|
zw = (float) THUMB_SIZE / (float) w;
|
|
|
|
zh = (float) THUMB_SIZE / (float) h;
|
|
|
|
z = MIN(zw, zh);
|
2011-11-05 12:11:50 +00:00
|
|
|
z = MIN(z, 1.0);
|
2011-02-17 10:04:58 +00:00
|
|
|
t->w = z * w;
|
|
|
|
t->h = z * h;
|
|
|
|
|
2011-09-29 11:43:36 +01:00
|
|
|
t->im = imlib_create_cropped_scaled_image(0, 0, w, h, t->w, t->h);
|
|
|
|
if (t->im == NULL)
|
2011-03-15 12:55:52 +00:00
|
|
|
die("could not allocate memory");
|
2011-04-11 15:58:38 +01:00
|
|
|
|
|
|
|
imlib_free_image_and_decache();
|
|
|
|
|
2011-08-19 12:26:58 +01:00
|
|
|
if (use_cache && !cache_hit)
|
2012-02-14 21:12:09 +00:00
|
|
|
tns_cache_write(t, true);
|
2011-03-15 12:55:52 +00:00
|
|
|
|
2011-09-11 20:01:24 +01:00
|
|
|
tns->dirty = true;
|
|
|
|
return true;
|
2011-02-17 10:04:58 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 21:05:31 +00:00
|
|
|
void tns_check_view(tns_t *tns, bool scrolled)
|
|
|
|
{
|
2011-02-19 23:08:17 +00:00
|
|
|
int r;
|
|
|
|
|
2011-09-29 11:43:36 +01:00
|
|
|
if (tns == NULL)
|
2011-02-19 21:12:08 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
tns->first -= tns->first % tns->cols;
|
2011-02-21 13:59:29 +00:00
|
|
|
r = tns->sel % tns->cols;
|
2011-02-19 21:12:08 +00:00
|
|
|
|
|
|
|
if (scrolled) {
|
|
|
|
/* move selection into visible area */
|
2011-02-21 13:59:29 +00:00
|
|
|
if (tns->sel >= tns->first + tns->cols * tns->rows)
|
|
|
|
tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
|
|
|
|
else if (tns->sel < tns->first)
|
|
|
|
tns->sel = tns->first + r;
|
2011-02-19 21:12:08 +00:00
|
|
|
} else {
|
|
|
|
/* scroll to selection */
|
2011-02-21 13:59:29 +00:00
|
|
|
if (tns->first + tns->cols * tns->rows <= tns->sel) {
|
|
|
|
tns->first = tns->sel - r - tns->cols * (tns->rows - 1);
|
2011-09-11 20:01:24 +01:00
|
|
|
tns->dirty = true;
|
2011-02-21 13:59:29 +00:00
|
|
|
} else if (tns->first > tns->sel) {
|
|
|
|
tns->first = tns->sel - r;
|
2011-09-11 20:01:24 +01:00
|
|
|
tns->dirty = true;
|
2011-02-19 21:12:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-08 21:05:31 +00:00
|
|
|
void tns_render(tns_t *tns)
|
|
|
|
{
|
2011-02-19 21:12:08 +00:00
|
|
|
thumb_t *t;
|
2011-09-17 16:23:51 +01:00
|
|
|
win_t *win;
|
|
|
|
int i, cnt, r, x, y;
|
2011-02-16 20:40:20 +00:00
|
|
|
|
2011-09-29 11:43:36 +01:00
|
|
|
if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
|
2011-04-08 13:44:00 +01:00
|
|
|
return;
|
|
|
|
if (!tns->dirty)
|
2011-02-16 20:40:20 +00:00
|
|
|
return;
|
|
|
|
|
2011-09-17 16:23:51 +01:00
|
|
|
win = tns->win;
|
2011-02-19 21:12:08 +00:00
|
|
|
win_clear(win);
|
2011-03-15 12:55:52 +00:00
|
|
|
imlib_context_set_drawable(win->pm);
|
2011-02-19 21:12:08 +00:00
|
|
|
|
2011-02-17 15:22:54 +00:00
|
|
|
tns->cols = MAX(1, win->w / thumb_dim);
|
|
|
|
tns->rows = MAX(1, win->h / thumb_dim);
|
2011-02-16 20:40:20 +00:00
|
|
|
|
2011-02-19 21:12:08 +00:00
|
|
|
if (tns->cnt < tns->cols * tns->rows) {
|
|
|
|
tns->first = 0;
|
|
|
|
cnt = tns->cnt;
|
|
|
|
} else {
|
2011-09-11 20:01:24 +01:00
|
|
|
tns_check_view(tns, false);
|
2011-02-19 21:12:08 +00:00
|
|
|
cnt = tns->cols * tns->rows;
|
|
|
|
if ((r = tns->first + cnt - tns->cnt) >= tns->cols)
|
|
|
|
tns->first -= r - r % tns->cols;
|
|
|
|
if (r > 0)
|
|
|
|
cnt -= r % tns->cols;
|
|
|
|
}
|
2011-02-16 20:40:20 +00:00
|
|
|
|
2011-02-19 21:12:08 +00:00
|
|
|
r = cnt % tns->cols ? 1 : 0;
|
2011-02-18 13:57:24 +00:00
|
|
|
tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5;
|
2011-02-19 21:12:08 +00:00
|
|
|
tns->y = y = (win->h - (cnt / tns->cols + r) * thumb_dim) / 2 + 5;
|
|
|
|
|
2011-07-26 17:01:29 +01:00
|
|
|
for (i = 0; i < cnt; i++) {
|
2011-02-19 21:12:08 +00:00
|
|
|
t = &tns->thumbs[tns->first + i];
|
|
|
|
t->x = x + (THUMB_SIZE - t->w) / 2;
|
|
|
|
t->y = y + (THUMB_SIZE - t->h) / 2;
|
2011-03-15 12:55:52 +00:00
|
|
|
imlib_context_set_image(t->im);
|
|
|
|
imlib_render_image_part_on_drawable_at_size(0, 0, t->w, t->h,
|
|
|
|
t->x, t->y, t->w, t->h);
|
2013-11-14 13:37:08 +00:00
|
|
|
if (t->file->marked)
|
|
|
|
tns_mark(tns, tns->first + i, true);
|
2011-02-19 21:12:08 +00:00
|
|
|
if ((i + 1) % tns->cols == 0) {
|
2011-02-18 13:57:24 +00:00
|
|
|
x = tns->x;
|
2011-02-16 20:40:20 +00:00
|
|
|
y += thumb_dim;
|
|
|
|
} else {
|
|
|
|
x += thumb_dim;
|
|
|
|
}
|
|
|
|
}
|
2011-09-11 20:01:24 +01:00
|
|
|
tns->dirty = false;
|
2011-09-17 16:23:51 +01:00
|
|
|
tns_highlight(tns, tns->sel, true);
|
2011-02-16 20:40:20 +00:00
|
|
|
}
|
|
|
|
|
2013-11-14 13:37:08 +00:00
|
|
|
void tns_mark(tns_t *tns, int n, bool mark)
|
2013-02-08 21:05:31 +00:00
|
|
|
{
|
2011-09-29 11:43:36 +01:00
|
|
|
if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
|
2011-02-17 15:22:54 +00:00
|
|
|
return;
|
|
|
|
|
2013-11-14 13:37:08 +00:00
|
|
|
if (n >= 0 && n < tns->cnt) {
|
|
|
|
unsigned long col;
|
|
|
|
thumb_t *t = &tns->thumbs[n];
|
|
|
|
win_t *win = tns->win;
|
|
|
|
int x = t->x, y = t->y, w = t->w, h = t->h;
|
|
|
|
|
|
|
|
if (mark || n == tns->sel)
|
|
|
|
col = win->selcol;
|
|
|
|
else if (win->fullscreen)
|
|
|
|
col = win->fscol;
|
|
|
|
else
|
|
|
|
col = win->bgcol;
|
|
|
|
|
|
|
|
win_draw_rect(win, win->pm, x - 4, y - 4, 8, 2, true, 0, col);
|
|
|
|
win_draw_rect(win, win->pm, x - 4, y - 4, 2, 8, true, 0, col);
|
|
|
|
win_draw_rect(win, win->pm, x + w - 4, y - 4, 8, 2, true, 0, col);
|
|
|
|
win_draw_rect(win, win->pm, x + w + 2, y - 4, 2, 8, true, 0, col);
|
|
|
|
win_draw_rect(win, win->pm, x - 4, y + h + 2, 8, 2, true, 0, col);
|
|
|
|
win_draw_rect(win, win->pm, x - 4, y + h - 4, 2, 8, true, 0, col);
|
|
|
|
win_draw_rect(win, win->pm, x + w - 4, y + h + 2, 8, 2, true, 0, col);
|
|
|
|
win_draw_rect(win, win->pm, x + w + 2, y + h - 4, 2, 8, true, 0, col);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void tns_highlight(tns_t *tns, int n, bool hl)
|
|
|
|
{
|
|
|
|
if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
|
|
|
|
return;
|
2011-09-17 16:23:51 +01:00
|
|
|
|
2011-02-19 21:12:08 +00:00
|
|
|
if (n >= 0 && n < tns->cnt) {
|
2013-11-14 13:37:08 +00:00
|
|
|
unsigned long col;
|
|
|
|
thumb_t *t = &tns->thumbs[n];
|
|
|
|
win_t *win = tns->win;
|
2011-03-09 10:37:49 +00:00
|
|
|
|
|
|
|
if (hl)
|
|
|
|
col = win->selcol;
|
|
|
|
else if (win->fullscreen)
|
2012-03-02 18:42:14 +00:00
|
|
|
col = win->fscol;
|
2011-03-09 10:37:49 +00:00
|
|
|
else
|
|
|
|
col = win->bgcol;
|
|
|
|
|
2013-11-14 13:37:08 +00:00
|
|
|
win_draw_rect(win, win->pm, t->x - 3, t->y - 3, t->w + 6, t->h + 6,
|
2011-09-11 20:01:24 +01:00
|
|
|
false, 2, col);
|
2013-11-14 13:37:08 +00:00
|
|
|
|
|
|
|
if (!hl && t->file->marked)
|
|
|
|
tns_mark(tns, n, true);
|
2011-02-17 15:22:54 +00:00
|
|
|
}
|
2011-02-17 15:57:55 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 21:05:31 +00:00
|
|
|
bool tns_move_selection(tns_t *tns, direction_t dir, int cnt)
|
|
|
|
{
|
2012-07-15 17:52:30 +01:00
|
|
|
int old, max;
|
2011-02-17 15:57:55 +00:00
|
|
|
|
2011-09-29 11:43:36 +01:00
|
|
|
if (tns == NULL || tns->thumbs == NULL)
|
2011-09-11 20:01:24 +01:00
|
|
|
return false;
|
2011-02-17 15:57:55 +00:00
|
|
|
|
2011-02-21 13:59:29 +00:00
|
|
|
old = tns->sel;
|
2012-07-15 17:52:30 +01:00
|
|
|
cnt = cnt > 1 ? cnt : 1;
|
2011-02-17 15:57:55 +00:00
|
|
|
|
|
|
|
switch (dir) {
|
2011-07-22 13:49:06 +01:00
|
|
|
case DIR_UP:
|
2012-07-15 17:52:30 +01:00
|
|
|
tns->sel = MAX(tns->sel - cnt * tns->cols, tns->sel % tns->cols);
|
2011-02-17 15:57:55 +00:00
|
|
|
break;
|
2011-07-22 13:49:06 +01:00
|
|
|
case DIR_DOWN:
|
2012-07-15 17:52:30 +01:00
|
|
|
max = tns->cols * ((tns->cnt - 1) / tns->cols) +
|
|
|
|
MIN((tns->cnt - 1) % tns->cols, tns->sel % tns->cols);
|
|
|
|
tns->sel = MIN(tns->sel + cnt * tns->cols, max);
|
2012-03-16 19:56:10 +00:00
|
|
|
break;
|
|
|
|
case DIR_LEFT:
|
2012-07-15 17:52:30 +01:00
|
|
|
tns->sel = MAX(tns->sel - cnt, 0);
|
2012-03-16 19:56:10 +00:00
|
|
|
break;
|
|
|
|
case DIR_RIGHT:
|
2012-07-15 17:52:30 +01:00
|
|
|
tns->sel = MIN(tns->sel + cnt, tns->cnt - 1);
|
2011-02-17 15:57:55 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-02-18 14:33:52 +00:00
|
|
|
|
2011-02-21 13:59:29 +00:00
|
|
|
if (tns->sel != old) {
|
2011-09-17 16:23:51 +01:00
|
|
|
tns_highlight(tns, old, false);
|
2011-09-11 20:01:24 +01:00
|
|
|
tns_check_view(tns, false);
|
2011-02-19 21:12:08 +00:00
|
|
|
if (!tns->dirty)
|
2011-09-17 16:23:51 +01:00
|
|
|
tns_highlight(tns, tns->sel, true);
|
2011-02-18 14:33:52 +00:00
|
|
|
}
|
2011-02-21 13:59:29 +00:00
|
|
|
return tns->sel != old;
|
2011-02-17 15:22:54 +00:00
|
|
|
}
|
2011-02-17 16:28:13 +00:00
|
|
|
|
2013-02-08 21:05:31 +00:00
|
|
|
bool tns_scroll(tns_t *tns, direction_t dir, bool screen)
|
|
|
|
{
|
2011-10-27 15:21:01 +01:00
|
|
|
int d, max, old;
|
2011-02-19 23:08:17 +00:00
|
|
|
|
2011-09-29 11:43:36 +01:00
|
|
|
if (tns == NULL)
|
2011-09-11 20:01:24 +01:00
|
|
|
return false;
|
2011-02-19 23:08:17 +00:00
|
|
|
|
|
|
|
old = tns->first;
|
2011-10-27 15:21:01 +01:00
|
|
|
d = tns->cols * (screen ? tns->rows : 1);
|
|
|
|
|
|
|
|
if (dir == DIR_DOWN) {
|
|
|
|
max = tns->cnt - tns->cols * tns->rows;
|
|
|
|
if (tns->cnt % tns->cols != 0)
|
|
|
|
max += tns->cols - tns->cnt % tns->cols;
|
|
|
|
tns->first = MIN(tns->first + d, max);
|
|
|
|
} else if (dir == DIR_UP) {
|
|
|
|
tns->first = MAX(tns->first - d, 0);
|
|
|
|
}
|
2011-02-19 23:08:17 +00:00
|
|
|
|
2011-10-27 15:21:01 +01:00
|
|
|
if (tns->first != old) {
|
2011-09-11 20:01:24 +01:00
|
|
|
tns_check_view(tns, true);
|
|
|
|
tns->dirty = true;
|
2011-02-19 23:08:17 +00:00
|
|
|
}
|
2011-02-19 23:28:28 +00:00
|
|
|
return tns->first != old;
|
2011-02-19 23:08:17 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 21:05:31 +00:00
|
|
|
int tns_translate(tns_t *tns, int x, int y)
|
|
|
|
{
|
2011-02-17 16:28:13 +00:00
|
|
|
int n;
|
|
|
|
|
2011-09-29 11:43:36 +01:00
|
|
|
if (tns == NULL || tns->thumbs == NULL)
|
2011-04-08 13:44:00 +01:00
|
|
|
return -1;
|
|
|
|
if (x < tns->x || y < tns->y)
|
2011-02-17 16:28:13 +00:00
|
|
|
return -1;
|
|
|
|
|
2011-02-19 21:12:08 +00:00
|
|
|
n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
|
|
|
|
(x - tns->x) / thumb_dim;
|
2011-08-19 11:20:00 +01:00
|
|
|
if (n >= tns->cnt)
|
|
|
|
n = -1;
|
2011-02-18 14:02:49 +00:00
|
|
|
|
2011-08-19 11:20:00 +01:00
|
|
|
return n;
|
2011-02-17 16:28:13 +00:00
|
|
|
}
|