Use win_t member in img_t & tns_t instead of parameters

This commit is contained in:
Bert Münnich 2011-09-17 17:23:51 +02:00
parent 82df0b34e9
commit 515e410451
7 changed files with 150 additions and 135 deletions

View File

@ -1,4 +1,4 @@
VERSION = git-20110912
VERSION = git-20110917
CC = gcc
CFLAGS = -Wall -pedantic -O2

View File

@ -54,7 +54,7 @@ bool it_quit(arg_t a) {
bool it_switch_mode(arg_t a) {
if (mode == MODE_IMAGE) {
if (!tns.thumbs)
tns_init(&tns, filecnt);
tns_init(&tns, filecnt, &win);
img_close(&img, false);
reset_timeout(reset_cursor);
if (img.slideshow) {
@ -180,16 +180,16 @@ bool it_move(arg_t a) {
direction_t dir = (direction_t) a;
if (mode == MODE_IMAGE)
return img_pan(&img, &win, dir, false);
return img_pan(&img, dir, false);
else
return tns_move_selection(&tns, &win, dir);
return tns_move_selection(&tns, dir);
}
bool i_pan_screen(arg_t a) {
direction_t dir = (direction_t) a;
if (mode == MODE_IMAGE)
return img_pan(&img, &win, dir, true);
return img_pan(&img, dir, true);
else
return false;
}
@ -198,7 +198,7 @@ bool i_pan_edge(arg_t a) {
direction_t dir = (direction_t) a;
if (mode == MODE_IMAGE)
return img_pan_edge(&img, &win, dir);
return img_pan_edge(&img, dir);
else
return false;
}
@ -245,8 +245,8 @@ bool i_drag(arg_t a) {
if (dragging)
next = XCheckIfEvent(win.env.dpy, &e, is_motionnotify, None);
if ((!dragging || !next) && (dx != 0 || dy != 0)) {
if (img_move(&img, &win, dx, dy))
img_render(&img, &win);
if (img_move(&img, dx, dy))
img_render(&img);
dx = dy = 0;
}
}
@ -265,19 +265,19 @@ bool i_zoom(arg_t a) {
return false;
if (scale > 0)
return img_zoom_in(&img, &win);
return img_zoom_in(&img);
else if (scale < 0)
return img_zoom_out(&img, &win);
return img_zoom_out(&img);
else
return img_zoom(&img, &win, 1.0);
return img_zoom(&img, 1.0);
}
bool i_fit_to_win(arg_t a) {
bool ret;
if (mode == MODE_IMAGE) {
if ((ret = img_fit_win(&img, &win)))
img_center(&img, &win);
if ((ret = img_fit_win(&img)))
img_center(&img);
return ret;
} else {
return false;
@ -309,10 +309,10 @@ bool i_rotate(arg_t a) {
if (mode == MODE_IMAGE) {
if (dir == DIR_LEFT) {
img_rotate_left(&img, &win);
img_rotate_left(&img);
return true;
} else if (dir == DIR_RIGHT) {
img_rotate_right(&img, &win);
img_rotate_right(&img);
return true;
}
}

154
image.c
View File

@ -49,26 +49,26 @@ void img_init(img_t *img, win_t *win) {
zoom_min = zoom_levels[0] / 100.0;
zoom_max = zoom_levels[ARRLEN(zoom_levels) - 1] / 100.0;
if (img) {
img->im = NULL;
img->multi.cap = img->multi.cnt = 0;
img->multi.animate = false;
img->zoom = options->zoom;
img->zoom = MAX(img->zoom, zoom_min);
img->zoom = MIN(img->zoom, zoom_max);
img->checkpan = false;
img->dirty = false;
img->aa = options->aa;
img->alpha = true;
img->slideshow = false;
img->ss_delay = SLIDESHOW_DELAY * 1000;
}
if (!img || !win)
return;
if (win) {
imlib_context_set_display(win->env.dpy);
imlib_context_set_visual(win->env.vis);
imlib_context_set_colormap(win->env.cmap);
}
imlib_context_set_display(win->env.dpy);
imlib_context_set_visual(win->env.vis);
imlib_context_set_colormap(win->env.cmap);
img->im = NULL;
img->win = win;
img->zoom = options->zoom;
img->zoom = MAX(img->zoom, zoom_min);
img->zoom = MIN(img->zoom, zoom_max);
img->checkpan = false;
img->dirty = false;
img->aa = options->aa;
img->alpha = true;
img->slideshow = false;
img->ss_delay = SLIDESHOW_DELAY * 1000;
img->multi.cap = img->multi.cnt = 0;
img->multi.animate = false;
}
#if EXIF_SUPPORT
@ -311,14 +311,13 @@ bool img_load(img_t *img, const fileinfo_t *file) {
img_load_gif(img, file);
#endif
img->w = imlib_image_get_width();
img->h = imlib_image_get_height();
img->scalemode = options->scalemode;
img->re = false;
img->checkpan = false;
img->dirty = true;
img->w = imlib_image_get_width();
img->h = imlib_image_get_height();
return true;
}
@ -345,12 +344,14 @@ void img_close(img_t *img, bool decache) {
}
}
void img_check_pan(img_t *img, win_t *win, bool moved) {
void img_check_pan(img_t *img, bool moved) {
win_t *win;
int ox, oy;
if (!img || !win)
if (!img || !img->im || !img->win)
return;
win = img->win;
ox = img->x;
oy = img->y;
@ -375,15 +376,17 @@ void img_check_pan(img_t *img, win_t *win, bool moved) {
img->dirty = true;
}
bool img_fit(img_t *img, win_t *win) {
bool img_fit(img_t *img) {
float z, zmax, zw, zh;
if (!img || !win || img->scalemode == SCALE_ZOOM)
if (!img || !img->im || !img->win)
return false;
if (img->scalemode == SCALE_ZOOM)
return false;
zmax = img->scalemode == SCALE_DOWN ? 1.0 : zoom_max;
zw = (float) win->w / (float) img->w;
zh = (float) win->h / (float) img->h;
zw = (float) img->win->w / (float) img->w;
zh = (float) img->win->h / (float) img->h;
z = MIN(zw, zh);
z = MAX(z, zoom_min);
@ -398,14 +401,16 @@ bool img_fit(img_t *img, win_t *win) {
}
}
void img_render(img_t *img, win_t *win) {
void img_render(img_t *img) {
win_t *win;
int sx, sy, sw, sh;
int dx, dy, dw, dh;
if (!img || !img->im || !win)
if (!img || !img->im || !img->win)
return;
img_fit(img, win);
win = img->win;
img_fit(img);
if (!img->re) {
/* rendered for the first time */
@ -421,7 +426,7 @@ void img_render(img_t *img, win_t *win) {
}
if (img->checkpan) {
img_check_pan(img, win, false);
img_check_pan(img, false);
img->checkpan = false;
}
@ -467,25 +472,25 @@ void img_render(img_t *img, win_t *win) {
img->dirty = false;
}
bool img_fit_win(img_t *img, win_t *win) {
if (!img || !img->im || !win)
bool img_fit_win(img_t *img) {
if (!img || !img->im)
return false;
img->scalemode = SCALE_FIT;
return img_fit(img, win);
return img_fit(img);
}
bool img_center(img_t *img, win_t *win) {
bool img_center(img_t *img) {
int ox, oy;
if (!img || !win)
if (!img || !img->im || !img->win)
return false;
ox = img->x;
oy = img->y;
img->x = (win->w - img->w * img->zoom) / 2;
img->y = (win->h - img->h * img->zoom) / 2;
img->x = (img->win->w - img->w * img->zoom) / 2;
img->y = (img->win->h - img->h * img->zoom) / 2;
if (ox != img->x || oy != img->y) {
img->dirty = true;
@ -495,8 +500,8 @@ bool img_center(img_t *img, win_t *win) {
}
}
bool img_zoom(img_t *img, win_t *win, float z) {
if (!img || !img->im || !win)
bool img_zoom(img_t *img, float z) {
if (!img || !img->im || !img->win)
return false;
z = MAX(z, zoom_min);
@ -505,8 +510,8 @@ bool img_zoom(img_t *img, win_t *win, float z) {
img->scalemode = SCALE_ZOOM;
if (ZOOMDIFF(z, img->zoom)) {
img->x = win->w / 2 - (win->w / 2 - img->x) * z / img->zoom;
img->y = win->h / 2 - (win->h / 2 - img->y) * z / img->zoom;
img->x = img->win->w / 2 - (img->win->w / 2 - img->x) * z / img->zoom;
img->y = img->win->h / 2 - (img->win->h / 2 - img->y) * z / img->zoom;
img->zoom = z;
img->checkpan = true;
img->dirty = true;
@ -516,36 +521,36 @@ bool img_zoom(img_t *img, win_t *win, float z) {
}
}
bool img_zoom_in(img_t *img, win_t *win) {
bool img_zoom_in(img_t *img) {
int i;
if (!img || !img->im || !win)
if (!img || !img->im)
return false;
for (i = 1; i < ARRLEN(zoom_levels); i++) {
if (zoom_levels[i] > img->zoom * 100.0)
return img_zoom(img, win, zoom_levels[i] / 100.0);
return img_zoom(img, zoom_levels[i] / 100.0);
}
return false;
}
bool img_zoom_out(img_t *img, win_t *win) {
bool img_zoom_out(img_t *img) {
int i;
if (!img || !img->im || !win)
if (!img || !img->im)
return false;
for (i = ARRLEN(zoom_levels) - 2; i >= 0; i--) {
if (zoom_levels[i] < img->zoom * 100.0)
return img_zoom(img, win, zoom_levels[i] / 100.0);
return img_zoom(img, zoom_levels[i] / 100.0);
}
return false;
}
bool img_move(img_t *img, win_t *win, int dx, int dy) {
bool img_move(img_t *img, int dx, int dy) {
int ox, oy;
if (!img || !img->im || !win)
if (!img || !img->im)
return false;
ox = img->x;
@ -554,7 +559,7 @@ bool img_move(img_t *img, win_t *win, int dx, int dy) {
img->x += dx;
img->y += dy;
img_check_pan(img, win, true);
img_check_pan(img, true);
if (ox != img->x || oy != img->y) {
img->dirty = true;
@ -564,27 +569,27 @@ bool img_move(img_t *img, win_t *win, int dx, int dy) {
}
}
bool img_pan(img_t *img, win_t *win, direction_t dir, bool screen) {
if (!img || !img->im || !win)
bool img_pan(img_t *img, direction_t dir, bool screen) {
if (!img || !img->im || !img->win)
return false;
switch (dir) {
case DIR_LEFT:
return img_move(img, win, win->w / (screen ? 1 : 5), 0);
return img_move(img, img->win->w / (screen ? 1 : 5), 0);
case DIR_RIGHT:
return img_move(img, win, win->w / (screen ? 1 : 5) * -1, 0);
return img_move(img, img->win->w / (screen ? 1 : 5) * -1, 0);
case DIR_UP:
return img_move(img, win, 0, win->h / (screen ? 1 : 5));
return img_move(img, 0, img->win->h / (screen ? 1 : 5));
case DIR_DOWN:
return img_move(img, win, 0, win->h / (screen ? 1 : 5) * -1);
return img_move(img, 0, img->win->h / (screen ? 1 : 5) * -1);
}
return false;
}
bool img_pan_edge(img_t *img, win_t *win, direction_t dir) {
bool img_pan_edge(img_t *img, direction_t dir) {
int ox, oy;
if (!img || !img->im || !win)
if (!img || !img->im || !img->win)
return false;
ox = img->x;
@ -595,17 +600,17 @@ bool img_pan_edge(img_t *img, win_t *win, direction_t dir) {
img->x = 0;
break;
case DIR_RIGHT:
img->x = win->w - img->w * img->zoom;
img->x = img->win->w - img->w * img->zoom;
break;
case DIR_UP:
img->y = 0;
break;
case DIR_DOWN:
img->y = win->h - img->h * img->zoom;
img->y = img->win->h - img->h * img->zoom;
break;
}
img_check_pan(img, win, true);
img_check_pan(img, true);
if (ox != img->x || oy != img->y) {
img->dirty = true;
@ -615,12 +620,14 @@ bool img_pan_edge(img_t *img, win_t *win, direction_t dir) {
}
}
void img_rotate(img_t *img, win_t *win, int d) {
void img_rotate(img_t *img, int d) {
win_t *win;
int ox, oy, tmp;
if (!img || !img->im || !win)
if (!img || !img->im || !img->win)
return;
win = img->win;
ox = d == 1 ? img->x : win->w - img->x - img->w * img->zoom;
oy = d == 3 ? img->y : win->h - img->y - img->h * img->zoom;
@ -638,12 +645,12 @@ void img_rotate(img_t *img, win_t *win, int d) {
img->dirty = true;
}
void img_rotate_left(img_t *img, win_t *win) {
img_rotate(img, win, 3);
void img_rotate_left(img_t *img) {
img_rotate(img, 3);
}
void img_rotate_right(img_t *img, win_t *win) {
img_rotate(img, win, 1);
void img_rotate_right(img_t *img) {
img_rotate(img, 1);
}
void img_toggle_antialias(img_t *img) {
@ -657,10 +664,9 @@ void img_toggle_antialias(img_t *img) {
}
bool img_frame_goto(img_t *img, int n) {
if (!img || n < 0 || n >= img->multi.cnt)
if (!img || !img->im)
return false;
if (n == img->multi.sel)
if (n < 0 || n >= img->multi.cnt || n == img->multi.sel)
return false;
img->multi.sel = n;
@ -676,7 +682,7 @@ bool img_frame_goto(img_t *img, int n) {
}
bool img_frame_navigate(img_t *img, int d) {
if (!img || !img->multi.cnt || !d)
if (!img || !img->im || !img->multi.cnt || !d)
return false;
d += img->multi.sel;
@ -689,7 +695,7 @@ bool img_frame_navigate(img_t *img, int d) {
}
bool img_frame_animate(img_t *img, bool restart) {
if (!img || !img->multi.cnt)
if (!img || !img->im || !img->multi.cnt)
return false;
if (img->multi.sel + 1 >= img->multi.cnt) {

36
image.h
View File

@ -39,10 +39,15 @@ typedef struct {
typedef struct {
Imlib_Image *im;
multi_img_t multi;
int w;
int h;
win_t *win;
int x;
int y;
float zoom;
scalemode_t scalemode;
float zoom;
bool re;
bool checkpan;
@ -53,10 +58,7 @@ typedef struct {
bool slideshow;
int ss_delay; /* in ms */
int x;
int y;
int w;
int h;
multi_img_t multi;
} img_t;
void img_init(img_t*, win_t*);
@ -64,21 +66,21 @@ void img_init(img_t*, win_t*);
bool img_load(img_t*, const fileinfo_t*);
void img_close(img_t*, bool);
void img_render(img_t*, win_t*);
void img_render(img_t*);
bool img_fit_win(img_t*, win_t*);
bool img_center(img_t*, win_t*);
bool img_fit_win(img_t*);
bool img_center(img_t*);
bool img_zoom(img_t*, win_t*, float);
bool img_zoom_in(img_t*, win_t*);
bool img_zoom_out(img_t*, win_t*);
bool img_zoom(img_t*, float);
bool img_zoom_in(img_t*);
bool img_zoom_out(img_t*);
bool img_move(img_t*, win_t*, int, int);
bool img_pan(img_t*, win_t*, direction_t, bool);
bool img_pan_edge(img_t*, win_t*, direction_t);
bool img_move(img_t*, int, int);
bool img_pan(img_t*, direction_t, bool);
bool img_pan_edge(img_t*, direction_t);
void img_rotate_left(img_t*, win_t*);
void img_rotate_right(img_t*, win_t*);
void img_rotate_left(img_t*);
void img_rotate_right(img_t*);
void img_toggle_antialias(img_t*);

13
main.c
View File

@ -261,7 +261,7 @@ void update_title() {
void redraw() {
if (mode == MODE_IMAGE) {
img_render(&img, &win);
img_render(&img);
if (img.slideshow && !img.multi.animate) {
if (fileidx + 1 < filecnt)
set_timeout(slideshow, img.ss_delay, true);
@ -269,7 +269,7 @@ void redraw() {
img.slideshow = false;
}
} else {
tns_render(&tns, &win);
tns_render(&tns);
}
update_title();
reset_timeout(redraw);
@ -372,8 +372,8 @@ void on_buttonpress(XButtonEvent *bev) {
set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
load_image(tns.sel);
} else {
tns_highlight(&tns, &win, tns.sel, false);
tns_highlight(&tns, &win, sel, true);
tns_highlight(&tns, tns.sel, false);
tns_highlight(&tns, sel, true);
tns.sel = sel;
}
redraw();
@ -469,7 +469,7 @@ int main(int argc, char **argv) {
parse_options(argc, argv);
if (options->clean_cache) {
tns_init(&tns, 0);
tns_init(&tns, 0, NULL);
tns_clean_cache(&tns);
exit(0);
}
@ -513,7 +513,6 @@ int main(int argc, char **argv) {
continue;
}
start = fileidx;
printf("reading dir: %s\n", filename);
while ((filename = r_readdir(&dir))) {
check_add_file(filename);
free((void*) filename);
@ -538,7 +537,7 @@ int main(int argc, char **argv) {
if (options->thumb_mode) {
mode = MODE_THUMB;
tns_init(&tns, filecnt);
tns_init(&tns, filecnt, &win);
while (!tns_load(&tns, 0, &files[0], false, false))
remove_file(0, false);
tns.cnt = 1;

View File

@ -156,7 +156,7 @@ void tns_clean_cache(tns_t *tns) {
}
void tns_init(tns_t *tns, int cnt) {
void tns_init(tns_t *tns, int cnt, win_t *win) {
int len;
char *homedir;
@ -170,8 +170,9 @@ void tns_init(tns_t *tns, int cnt) {
tns->thumbs = NULL;
}
tns->cnt = tns->first = tns->sel = 0;
tns->cap = cnt;
tns->cnt = tns->first = tns->sel = 0;
tns->win = win;
tns->alpha = true;
tns->dirty = false;
@ -305,16 +306,17 @@ void tns_check_view(tns_t *tns, bool scrolled) {
}
}
void tns_render(tns_t *tns, win_t *win) {
int i, cnt, r, x, y;
void tns_render(tns_t *tns) {
thumb_t *t;
win_t *win;
int i, cnt, r, x, y;
if (!tns || !tns->thumbs || !win)
if (!tns || !tns->thumbs || !tns->win)
return;
if (!tns->dirty)
return;
win = tns->win;
win_clear(win);
imlib_context_set_drawable(win->pm);
@ -357,17 +359,20 @@ void tns_render(tns_t *tns, win_t *win) {
}
tns->dirty = false;
tns_highlight(tns, win, tns->sel, true);
tns_highlight(tns, tns->sel, true);
}
void tns_highlight(tns_t *tns, win_t *win, int n, bool hl) {
void tns_highlight(tns_t *tns, int n, bool hl) {
thumb_t *t;
win_t *win;
int x, y;
unsigned long col;
if (!tns || !tns->thumbs || !win)
if (!tns || !tns->thumbs || !tns->win)
return;
win = tns->win;
if (n >= 0 && n < tns->cnt) {
t = &tns->thumbs[n];
@ -387,10 +392,10 @@ void tns_highlight(tns_t *tns, win_t *win, int n, bool hl) {
win_draw(win);
}
bool tns_move_selection(tns_t *tns, win_t *win, direction_t dir) {
bool tns_move_selection(tns_t *tns, direction_t dir) {
int old;
if (!tns || !tns->thumbs || !win)
if (!tns || !tns->thumbs)
return false;
old = tns->sel;
@ -415,10 +420,10 @@ bool tns_move_selection(tns_t *tns, win_t *win, direction_t dir) {
}
if (tns->sel != old) {
tns_highlight(tns, win, old, false);
tns_highlight(tns, old, false);
tns_check_view(tns, false);
if (!tns->dirty)
tns_highlight(tns, win, tns->sel, true);
tns_highlight(tns, tns->sel, true);
}
return tns->sel != old;

View File

@ -26,39 +26,42 @@
#include "window.h"
typedef struct {
Imlib_Image *im;
const fileinfo_t *file;
int x;
int y;
Imlib_Image *im;
int w;
int h;
int x;
int y;
} thumb_t;
typedef struct {
thumb_t *thumbs;
int cap;
int cnt;
int first;
int sel;
win_t *win;
int x;
int y;
int cols;
int rows;
int first;
int sel;
bool alpha;
bool dirty;
} tns_t;
void tns_clean_cache(tns_t*);
void tns_init(tns_t*, int);
void tns_init(tns_t*, int, win_t*);
void tns_free(tns_t*);
bool tns_load(tns_t*, int, const fileinfo_t*, bool, bool);
void tns_render(tns_t*, win_t*);
void tns_highlight(tns_t*, win_t*, int, bool);
void tns_render(tns_t*);
void tns_highlight(tns_t*, int, bool);
bool tns_move_selection(tns_t*, win_t*, direction_t);
bool tns_move_selection(tns_t*, direction_t);
bool tns_scroll(tns_t*, direction_t);
int tns_translate(tns_t*, int, int);