Handle window resize events properly
This commit is contained in:
parent
60a9ed7567
commit
0f714bccda
44
image.c
44
image.c
|
@ -65,6 +65,9 @@ int img_load(img_t *img, const char *filename) {
|
|||
imlib_context_set_image(im);
|
||||
|
||||
img->re = 0;
|
||||
img->checkpan = 0;
|
||||
img->zoomed = 0;
|
||||
|
||||
img->w = imlib_image_get_width();
|
||||
img->h = imlib_image_get_height();
|
||||
|
||||
|
@ -101,32 +104,32 @@ void img_render(img_t *img, win_t *win) {
|
|||
if (!img || !win || !imlib_context_get_image())
|
||||
return;
|
||||
|
||||
if ((!img->re || !img->zoomed) && SCALE_MODE != SCALE_ZOOM) {
|
||||
/* set zoom level to fit image into window */
|
||||
zw = (float) win->w / (float) img->w;
|
||||
zh = (float) win->h / (float) img->h;
|
||||
img->zoom = MIN(zw, zh);
|
||||
|
||||
if (img->zoom < zoom_min)
|
||||
img->zoom = zoom_min;
|
||||
else if (img->zoom > zoom_max)
|
||||
img->zoom = zoom_max;
|
||||
|
||||
if (SCALE_MODE == SCALE_DOWN && img->zoom > 1.0)
|
||||
img->zoom = 1.0;
|
||||
}
|
||||
|
||||
if (!img->re) {
|
||||
/* rendered for the first time */
|
||||
img->re = 1;
|
||||
|
||||
/* set zoom level to fit image into window */
|
||||
if (SCALE_MODE != SCALE_ZOOM) {
|
||||
zw = (float) win->w / (float) img->w;
|
||||
zh = (float) win->h / (float) img->h;
|
||||
img->zoom = MIN(zw, zh);
|
||||
|
||||
if (img->zoom < zoom_min)
|
||||
img->zoom = zoom_min;
|
||||
else if (img->zoom > zoom_max)
|
||||
img->zoom = zoom_max;
|
||||
|
||||
if (SCALE_MODE == SCALE_DOWN && img->zoom > 1.0)
|
||||
img->zoom = 1.0;
|
||||
}
|
||||
|
||||
/* center image in window */
|
||||
img->x = (win->w - img->w * img->zoom) / 2;
|
||||
img->y = (win->h - img->h * img->zoom) / 2;
|
||||
} else if (img->cp) {
|
||||
/* only useful after zooming */
|
||||
}
|
||||
|
||||
if (img->checkpan) {
|
||||
img_check_pan(img, win);
|
||||
img->cp = 0;
|
||||
img->checkpan = 0;
|
||||
}
|
||||
|
||||
/* calculate source and destination offsets */
|
||||
|
@ -174,7 +177,8 @@ int img_zoom(img_t *img, float z) {
|
|||
img->x -= (img->w * z - img->w * img->zoom) / 2;
|
||||
img->y -= (img->h * z - img->h * img->zoom) / 2;
|
||||
img->zoom = z;
|
||||
img->cp = 1;
|
||||
img->checkpan = 1;
|
||||
img->zoomed = 1;
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
|
|
3
image.h
3
image.h
|
@ -37,7 +37,8 @@ typedef enum pandir_e {
|
|||
typedef struct img_s {
|
||||
float zoom;
|
||||
unsigned char re;
|
||||
unsigned char cp;
|
||||
unsigned char checkpan;
|
||||
unsigned char zoomed;
|
||||
int x;
|
||||
int y;
|
||||
int w;
|
||||
|
|
33
main.c
33
main.c
|
@ -18,6 +18,7 @@
|
|||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/select.h>
|
||||
|
||||
#include <X11/Xlib.h>
|
||||
#include <X11/Xutil.h>
|
||||
|
@ -45,14 +46,34 @@ const char **filenames;
|
|||
unsigned int filecnt;
|
||||
unsigned int fileidx;
|
||||
|
||||
unsigned char timeout;
|
||||
|
||||
#define TITLE_LEN 256
|
||||
char win_title[TITLE_LEN];
|
||||
|
||||
void run() {
|
||||
int xfd;
|
||||
fd_set fds;
|
||||
struct timeval t;
|
||||
XEvent ev;
|
||||
|
||||
while (!XNextEvent(win.env.dpy, &ev)) {
|
||||
if (handler[ev.type])
|
||||
timeout = 0;
|
||||
|
||||
while (1) {
|
||||
if (timeout) {
|
||||
t.tv_sec = 0;
|
||||
t.tv_usec = 250;
|
||||
xfd = ConnectionNumber(win.env.dpy);
|
||||
FD_ZERO(&fds);
|
||||
FD_SET(xfd, &fds);
|
||||
|
||||
if (!XPending(win.env.dpy) && !select(xfd + 1, &fds, 0, 0, &t)) {
|
||||
img_render(&img, &win);
|
||||
timeout = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (!XNextEvent(win.env.dpy, &ev) && handler[ev.type])
|
||||
handler[ev.type](&ev);
|
||||
}
|
||||
}
|
||||
|
@ -175,14 +196,18 @@ void on_keypress(XEvent *ev) {
|
|||
if (changed) {
|
||||
img_render(&img, &win);
|
||||
update_title();
|
||||
timeout = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void on_configurenotify(XEvent *ev) {
|
||||
if (!ev)
|
||||
return;
|
||||
|
||||
win_configure(&win, &ev->xconfigure);
|
||||
|
||||
if (win_configure(&win, &ev->xconfigure)) {
|
||||
img.checkpan = 1;
|
||||
timeout = 1;
|
||||
}
|
||||
}
|
||||
|
||||
void update_title() {
|
||||
|
|
17
window.c
17
window.c
|
@ -102,19 +102,20 @@ void win_set_title(win_t *win, const char *title) {
|
|||
XSetIconName(win->env.dpy, win->xwin, title);
|
||||
}
|
||||
|
||||
int win_configure(win_t *win, XConfigureEvent *cev) {
|
||||
int win_configure(win_t *win, XConfigureEvent *c) {
|
||||
int changed;
|
||||
|
||||
if (!win)
|
||||
return 0;
|
||||
|
||||
changed = win->x != cev->x || win->y != cev->y ||
|
||||
win->w != cev->width || win->h != cev->height;
|
||||
win->x = cev->x;
|
||||
win->y = cev->y;
|
||||
win->w = cev->width;
|
||||
win->h = cev->height;
|
||||
win->bw = cev->border_width;
|
||||
changed = win->w != c->width || win->h != c->height;
|
||||
|
||||
win->x = c->x;
|
||||
win->y = c->y;
|
||||
win->w = c->width;
|
||||
win->h = c->height;
|
||||
win->bw = c->border_width;
|
||||
|
||||
return changed;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue