No more expose handling

This commit is contained in:
Bert 2011-01-20 21:44:34 +01:00
parent 08018427c6
commit 029e1208b9
5 changed files with 47 additions and 43 deletions

41
image.c
View file

@ -31,7 +31,6 @@ void imlib_init(win_t *win) {
imlib_context_set_display(win->env.dpy); imlib_context_set_display(win->env.dpy);
imlib_context_set_visual(win->env.vis); imlib_context_set_visual(win->env.vis);
imlib_context_set_colormap(win->env.cmap); imlib_context_set_colormap(win->env.cmap);
imlib_context_set_drawable(win->xwin);
} }
void imlib_destroy() { void imlib_destroy() {
@ -86,43 +85,43 @@ void img_display(img_t *img, win_t *win) {
img->x = (win->w - img->w * img->zoom) / 2; img->x = (win->w - img->w * img->zoom) / 2;
img->y = (win->h - img->h * img->zoom) / 2; img->y = (win->h - img->h * img->zoom) / 2;
win_clear(win); img_render(img, win);
img_render(img, win, 0, 0, win->w, win->h);
} }
void img_render(img_t *img, win_t *win, int x, int y, int w, int h) { void img_render(img_t *img, win_t *win) {
int sx, sy, sw, sh; int sx, sy, sw, sh;
int dx, dy, dw, dh; int dx, dy, dw, dh;
if (!img || !win || !imlib_context_get_image()) if (!img || !win || !imlib_context_get_image())
return; return;
if (img->x < x) { if (img->x < 0) {
sx = (x - img->x) / img->zoom; sx = -img->x / img->zoom;
sw = MIN(w / img->zoom, img->w - sx); sw = win->w / img->zoom;
dx = x; dx = 0;
dw = sw * img->zoom; dw = win->w;
} else { } else {
sx = 0; sx = 0;
sw = MIN((w - img->x + x) / img->zoom, img->w); sw = img->w;
dx = img->x; dx = img->x;
dw = sw * img->zoom; dw = img->w * img->zoom;
} }
if (img->y < y) { if (img->y < 0) {
sy = (y - img->y) / img->zoom; sy = -img->y / img->zoom;
sh = MIN(h / img->zoom, img->h - sy); sh = win->h / img->zoom;
dy = y; dy = 0;
dh = sh * img->zoom; dh = win->h;
} else { } else {
sy = 0; sy = 0;
sh = MIN((h - img->y + y) / img->zoom, img->h); sh = img->h;
dy = img->y; dy = img->y;
dh = sh * img->zoom; dh = img->h * img->zoom;
} }
if (sw < 0 || sh < 0) win_clear(win);
return;
imlib_context_set_drawable(win->pm);
imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh); imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
win_draw(win);
} }

View file

@ -41,6 +41,6 @@ void imlib_destroy();
int img_load(img_t*, const char*); int img_load(img_t*, const char*);
void img_display(img_t*, win_t*); void img_display(img_t*, win_t*);
void img_render(img_t*, win_t*, int, int, int, int); void img_render(img_t*, win_t*);
#endif /* IMAGE_H */ #endif /* IMAGE_H */

14
main.c
View file

@ -29,14 +29,12 @@
void on_keypress(XEvent*); void on_keypress(XEvent*);
void on_configurenotify(XEvent*); void on_configurenotify(XEvent*);
void on_expose(XEvent*);
void update_title(); void update_title();
static void (*handler[LASTEvent])(XEvent*) = { static void (*handler[LASTEvent])(XEvent*) = {
[Expose] = on_expose, [KeyPress] = on_keypress,
[ConfigureNotify] = on_configurenotify, [ConfigureNotify] = on_configurenotify
[KeyPress] = on_keypress
}; };
img_t img; img_t img;
@ -154,14 +152,6 @@ void on_configurenotify(XEvent *ev) {
win_configure(&win, &ev->xconfigure); win_configure(&win, &ev->xconfigure);
} }
void on_expose(XEvent *ev) {
if (!ev)
return;
img_render(&img, &win, ev->xexpose.x, ev->xexpose.y,
ev->xexpose.width, ev->xexpose.height);
}
void update_title() { void update_title() {
int n; int n;

View file

@ -28,8 +28,7 @@ void win_open(win_t *win) {
win_env_t *e; win_env_t *e;
XClassHint *classhint; XClassHint *classhint;
XColor bgcol; XColor bgcol;
XSetWindowAttributes attr; XGCValues gcval;
unsigned long mask;
if (!win) if (!win)
return; return;
@ -58,19 +57,19 @@ void win_open(win_t *win) {
win->x = (e->scrw - win->w) / 2; win->x = (e->scrw - win->w) / 2;
win->y = (e->scrh - win->h) / 2; win->y = (e->scrh - win->h) / 2;
attr.backing_store = NotUseful;
attr.background_pixel = bgcol.pixel;
attr.save_under = False;
mask = CWBackingStore | CWBackPixel | CWSaveUnder;
win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr), win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
win->x, win->y, win->w, win->h, 0, win->x, win->y, win->w, win->h, 0,
e->depth, InputOutput, e->vis, mask, &attr); e->depth, InputOutput, e->vis, 0, None);
if (win->xwin == None) if (win->xwin == None)
DIE("could not create window"); DIE("could not create window");
XSelectInput(e->dpy, win->xwin, XSelectInput(e->dpy, win->xwin,
StructureNotifyMask | ExposureMask | KeyPressMask); StructureNotifyMask | KeyPressMask);
win->pm = 0;
gcval.foreground = bgcol.pixel;
win->bgc = XCreateGC(e->dpy, win->xwin, GCForeground, &gcval);
win_set_title(win, "sxiv"); win_set_title(win, "sxiv");
@ -124,5 +123,17 @@ void win_clear(win_t *win) {
if (!win) if (!win)
return; return;
if (win->pm)
XFreePixmap(win->env.dpy, win->pm);
win->pm = XCreatePixmap(win->env.dpy, win->xwin, win->w, win->h,
win->env.depth);
XFillRectangle(win->env.dpy, win->pm, win->bgc, 0, 0, win->w, win->h);
}
void win_draw(win_t *win) {
if (!win)
return;
XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->pm);
XClearWindow(win->env.dpy, win->xwin); XClearWindow(win->env.dpy, win->xwin);
} }

View file

@ -33,6 +33,8 @@ typedef struct win_env_s {
typedef struct win_s { typedef struct win_s {
Window xwin; Window xwin;
win_env_t env; win_env_t env;
Pixmap pm;
GC bgc;
int w; int w;
int h; int h;
@ -49,6 +51,8 @@ void win_close(win_t*);
void win_set_title(win_t*, const char*); void win_set_title(win_t*, const char*);
int win_configure(win_t*, XConfigureEvent*); int win_configure(win_t*, XConfigureEvent*);
void win_clear(win_t*); void win_clear(win_t*);
void win_draw(win_t*);
#endif /* WINDOW_H */ #endif /* WINDOW_H */