From: Carl Worth Date: Thu, 7 Nov 2013 04:27:55 +0000 (-0800) Subject: Add interactive zooming and panning. X-Git-Url: https://git.cworth.org/git?p=acre;a=commitdiff_plain;h=1a4594b5739b8448bf711a30b1c8bf23c0cd1912 Add interactive zooming and panning. Left and right to pan, plus and minus to zoom, and Home to return to full data range. --- diff --git a/acre-x.c b/acre-x.c index 4e2cd0f..c1f7c41 100644 --- a/acre-x.c +++ b/acre-x.c @@ -75,7 +75,7 @@ load_chart (void) static void draw (Display *dpy, Window window, Visual *visual, acre_t *acre, - int width, int height) + int width, int height, double x_min, double x_max) { cairo_t *cr; cairo_surface_t *surface; @@ -88,6 +88,7 @@ draw (Display *dpy, Window window, Visual *visual, acre_t *acre, cairo_set_source_rgb (cr, 1, 1, 1); cairo_paint (cr); + acre_set_x_axis_range (acre, x_min, x_max); acre_draw (acre, cr, width, height); cairo_destroy (cr); @@ -102,19 +103,62 @@ handle_events(Display *dpy, Window window, Visual *visual, { XEvent xev; KeyCode quit_code = XKeysymToKeycode (dpy, XStringToKeysym("Q")); + KeyCode left_code = XKeysymToKeycode (dpy, XStringToKeysym("Left")); + KeyCode right_code = XKeysymToKeycode (dpy, XStringToKeysym("Right")); + KeyCode plus_code = XKeysymToKeycode (dpy, XStringToKeysym("plus")); + KeyCode equal_code = XKeysymToKeycode (dpy, XStringToKeysym("equal")); + KeyCode minus_code = XKeysymToKeycode (dpy, XStringToKeysym("minus")); + KeyCode home_code = XKeysymToKeycode (dpy, XStringToKeysym("Home")); + KeyCode keycode; bool need_redraw = false; + double x_min, x_max, shift; + + acre_get_x_axis_data_range (acre, &x_min, &x_max); while (1) { if (! XPending (dpy) && need_redraw) - draw (dpy, window, visual, acre, width, height); + draw (dpy, window, visual, acre, + width, height, x_min, x_max); XNextEvent (dpy, &xev); switch (xev.type) { case KeyPress: - if (xev.xkey.keycode == quit_code) { + keycode = xev.xkey.keycode; + if (keycode == quit_code) + { return; - } - break; + } + else if (keycode == left_code) + { + shift = 0.25 * (x_max - x_min); + x_min += shift; + x_max += shift; + } + else if (keycode == right_code) + { + shift = 0.25 * (x_max - x_min); + x_min -= shift; + x_max -= shift; + } + else if (keycode == plus_code || + keycode == equal_code) + { + shift = 0.25 * (x_max - x_min); + x_min += shift; + x_max -= shift; + } + else if (keycode == minus_code) + { + shift = 0.5 * (x_max - x_min); + x_min -= shift; + x_max += shift; + } + else if (keycode == home_code) + { + acre_get_x_axis_data_range (acre, &x_min, &x_max); + } + need_redraw = 1; + break; case ConfigureNotify: width = xev.xconfigure.width; height = xev.xconfigure.height;