nsxive310136
Mouse drag translates pointer position to image area
This makes mouse panning more direct and faster.
+40 −64
7 files changed
- M Makefile +1 −1
- M commands.c +23 −54
- M image.c +8 −3
- M image.h +1 −0
- M main.c +1 −0
- M types.h +1 −1
- M window.c +5 −5
| @@ -1,4 +1,4 @@ | |||
| 1 | − | VERSION := git-20170908 | |
| 1 | + | VERSION := git-20171004 | |
| 2 | 2 | ||
| 3 | 3 | all: sxiv | |
| 4 | 4 | ||
| @@ -320,72 +320,41 @@ bool ci_scroll_to_edge(arg_t dir) | |||
| 320 | 320 | return img_pan_edge(&img, dir); | |
| 321 | 321 | } | |
| 322 | 322 | ||
| 323 | − | /* Xlib helper function for i_drag() */ | |
| 324 | − | Bool is_motionnotify(Display *d, XEvent *e, XPointer a) | |
| 325 | − | { | |
| 326 | − | return e != NULL && e->type == MotionNotify; | |
| 327 | − | } | |
| 328 | − | ||
| 329 | − | #define WARP(x,y) \ | |
| 330 | − | XWarpPointer(win.env.dpy, None, win.xwin, 0, 0, 0, 0, x, y); \ | |
| 331 | − | ox = x, oy = y; \ | |
| 332 | − | break | |
| 333 | − | ||
| 334 | 323 | bool ci_drag(arg_t _) | |
| 335 | 324 | { | |
| 336 | − | int dx = 0, dy = 0, i, ox, oy, x, y; | |
| 325 | + | int i, x, y; | |
| 326 | + | float px, py; | |
| 337 | 327 | unsigned int ui; | |
| 338 | − | bool dragging = true, next = false; | |
| 339 | 328 | XEvent e; | |
| 340 | 329 | Window w; | |
| 341 | 330 | ||
| 342 | − | if (!XQueryPointer(win.env.dpy, win.xwin, &w, &w, &i, &i, &ox, &oy, &ui)) | |
| 331 | + | if ((int)(img.w * img.zoom) < win.w && (int)(img.h * img.zoom) < win.h) | |
| 332 | + | return false; | |
| 333 | + | if (!XQueryPointer(win.env.dpy, win.xwin, &w, &w, &i, &i, &x, &y, &ui)) | |
| 343 | 334 | return false; | |
| 344 | 335 | ||
| 345 | − | win_set_cursor(&win, CURSOR_HAND); | |
| 346 | − | ||
| 347 | − | while (dragging) { | |
| 348 | − | if (!next) | |
| 349 | − | XMaskEvent(win.env.dpy, | |
| 350 | − | ButtonPressMask | ButtonReleaseMask | PointerMotionMask, &e); | |
| 351 | − | switch (e.type) { | |
| 352 | − | case ButtonPress: | |
| 353 | − | case ButtonRelease: | |
| 354 | − | dragging = false; | |
| 355 | − | break; | |
| 356 | − | case MotionNotify: | |
| 357 | − | x = e.xmotion.x; | |
| 358 | − | y = e.xmotion.y; | |
| 359 | − | ||
| 360 | − | /* wrap the mouse around */ | |
| 361 | − | if (x <= 0) { | |
| 362 | − | WARP(win.w - 2, y); | |
| 363 | − | } else if (x >= win.w - 1) { | |
| 364 | − | WARP(1, y); | |
| 365 | − | } else if (y <= 0) { | |
| 366 | − | WARP(x, win.h - 2); | |
| 367 | − | } else if (y >= win.h - 1) { | |
| 368 | − | WARP(x, 1); | |
| 369 | − | } | |
| 370 | − | dx += x - ox; | |
| 371 | − | dy += y - oy; | |
| 372 | − | ox = x; | |
| 373 | − | oy = y; | |
| 374 | − | break; | |
| 375 | − | } | |
| 376 | − | if (dragging) | |
| 377 | − | next = XCheckIfEvent(win.env.dpy, &e, is_motionnotify, None); | |
| 378 | − | if ((!dragging || !next) && (dx != 0 || dy != 0)) { | |
| 379 | − | if (img_move(&img, dx, dy)) { | |
| 380 | − | img_render(&img); | |
| 381 | − | win_draw(&win); | |
| 382 | − | } | |
| 383 | − | dx = dy = 0; | |
| 336 | + | win_set_cursor(&win, CURSOR_DRAG); | |
| 337 | + | ||
| 338 | + | for (;;) { | |
| 339 | + | px = MIN(MAX(0.0, x - win.w*0.1), win.w*0.8) / (win.w*0.8) | |
| 340 | + | * (win.w - img.w * img.zoom); | |
| 341 | + | py = MIN(MAX(0.0, y - win.h*0.1), win.h*0.8) / (win.h*0.8) | |
| 342 | + | * (win.h - img.h * img.zoom); | |
| 343 | + | ||
| 344 | + | if (img_pos(&img, px, py)) { | |
| 345 | + | img_render(&img); | |
| 346 | + | win_draw(&win); | |
| 384 | 347 | } | |
| 348 | + | XMaskEvent(win.env.dpy, | |
| 349 | + | ButtonPressMask | ButtonReleaseMask | PointerMotionMask, &e); | |
| 350 | + | if (e.type == ButtonPress || e.type == ButtonRelease) | |
| 351 | + | break; | |
| 352 | + | while (XCheckTypedEvent(win.env.dpy, MotionNotify, &e)); | |
| 353 | + | x = e.xmotion.x; | |
| 354 | + | y = e.xmotion.y; | |
| 385 | 355 | } | |
| 386 | 356 | win_set_cursor(&win, CURSOR_ARROW); | |
| 387 | 357 | set_timeout(reset_cursor, TO_CURSOR_HIDE, true); | |
| 388 | − | reset_timeout(redraw); | |
| 389 | 358 | ||
| 390 | 359 | return true; | |
| 391 | 360 | } | |
| @@ -567,15 +567,15 @@ bool img_zoom_out(img_t *img) | |||
| 567 | 567 | return false; | |
| 568 | 568 | } | |
| 569 | 569 | ||
| 570 | − | bool img_move(img_t *img, float dx, float dy) | |
| 570 | + | bool img_pos(img_t *img, float x, float y) | |
| 571 | 571 | { | |
| 572 | 572 | float ox, oy; | |
| 573 | 573 | ||
| 574 | 574 | ox = img->x; | |
| 575 | 575 | oy = img->y; | |
| 576 | 576 | ||
| 577 | − | img->x += dx; | |
| 578 | − | img->y += dy; | |
| 577 | + | img->x = x; | |
| 578 | + | img->y = y; | |
| 579 | 579 | ||
| 580 | 580 | img_check_pan(img, true); | |
| 581 | 581 | ||
| @@ -587,6 +587,11 @@ bool img_move(img_t *img, float dx, float dy) | |||
| 587 | 587 | } | |
| 588 | 588 | } | |
| 589 | 589 | ||
| 590 | + | bool img_move(img_t *img, float dx, float dy) | |
| 591 | + | { | |
| 592 | + | return img_pos(img, img->x + dx, img->y + dy); | |
| 593 | + | } | |
| 594 | + | ||
| 590 | 595 | bool img_pan(img_t *img, direction_t dir, int d) | |
| 591 | 596 | { | |
| 592 | 597 | /* d < 0: screen-wise | |
| @@ -80,6 +80,7 @@ bool img_zoom(img_t*, float); | |||
| 80 | 80 | bool img_zoom_in(img_t*); | |
| 81 | 81 | bool img_zoom_out(img_t*); | |
| 82 | 82 | ||
| 83 | + | bool img_pos(img_t*, float, float); | |
| 83 | 84 | bool img_move(img_t*, float, float); | |
| 84 | 85 | bool img_pan(img_t*, direction_t, int); | |
| 85 | 86 | bool img_pan_edge(img_t*, direction_t); | |
| @@ -741,6 +741,7 @@ void run(void) | |||
| 741 | 741 | XPeekEvent(win.env.dpy, &nextev); | |
| 742 | 742 | switch (ev.type) { | |
| 743 | 743 | case ConfigureNotify: | |
| 744 | + | case MotionNotify: | |
| 744 | 745 | discard = ev.type == nextev.type; | |
| 745 | 746 | break; | |
| 746 | 747 | case KeyPress: | |
| @@ -66,7 +66,7 @@ typedef enum { | |||
| 66 | 66 | typedef enum { | |
| 67 | 67 | CURSOR_ARROW, | |
| 68 | 68 | CURSOR_NONE, | |
| 69 | − | CURSOR_HAND, | |
| 69 | + | CURSOR_DRAG, | |
| 70 | 70 | CURSOR_WATCH | |
| 71 | 71 | } cursor_t; | |
| 72 | 72 | ||
| @@ -37,7 +37,7 @@ enum { | |||
| 37 | 37 | ||
| 38 | 38 | static Cursor carrow; | |
| 39 | 39 | static Cursor cnone; | |
| 40 | − | static Cursor chand; | |
| 40 | + | static Cursor cdrag; | |
| 41 | 41 | static Cursor cwatch; | |
| 42 | 42 | static GC gc; | |
| 43 | 43 | ||
| @@ -210,7 +210,7 @@ void win_open(win_t *win) | |||
| 210 | 210 | PointerMotionMask | StructureNotifyMask); | |
| 211 | 211 | ||
| 212 | 212 | carrow = XCreateFontCursor(e->dpy, XC_left_ptr); | |
| 213 | − | chand = XCreateFontCursor(e->dpy, XC_fleur); | |
| 213 | + | cdrag = XCreateFontCursor(e->dpy, XC_dotbox); | |
| 214 | 214 | cwatch = XCreateFontCursor(e->dpy, XC_watch); | |
| 215 | 215 | ||
| 216 | 216 | if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black", | |
| @@ -277,7 +277,7 @@ CLEANUP void win_close(win_t *win) | |||
| 277 | 277 | { | |
| 278 | 278 | XFreeCursor(win->env.dpy, carrow); | |
| 279 | 279 | XFreeCursor(win->env.dpy, cnone); | |
| 280 | − | XFreeCursor(win->env.dpy, chand); | |
| 280 | + | XFreeCursor(win->env.dpy, cdrag); | |
| 281 | 281 | XFreeCursor(win->env.dpy, cwatch); | |
| 282 | 282 | ||
| 283 | 283 | XFreeGC(win->env.dpy, gc); | |
| @@ -465,8 +465,8 @@ void win_set_cursor(win_t *win, cursor_t cursor) | |||
| 465 | 465 | case CURSOR_NONE: | |
| 466 | 466 | XDefineCursor(win->env.dpy, win->xwin, cnone); | |
| 467 | 467 | break; | |
| 468 | − | case CURSOR_HAND: | |
| 469 | − | XDefineCursor(win->env.dpy, win->xwin, chand); | |
| 468 | + | case CURSOR_DRAG: | |
| 469 | + | XDefineCursor(win->env.dpy, win->xwin, cdrag); | |
| 470 | 470 | break; | |
| 471 | 471 | case CURSOR_WATCH: | |
| 472 | 472 | XDefineCursor(win->env.dpy, win->xwin, cwatch); | |