]> git.cworth.org Git - acre/commitdiff
Add interactive zooming and panning.
authorCarl Worth <cworth@cworth.org>
Thu, 7 Nov 2013 04:27:55 +0000 (20:27 -0800)
committerCarl Worth <cworth@cworth.org>
Thu, 7 Nov 2013 04:27:55 +0000 (20:27 -0800)
Left and right to pan, plus and minus to zoom, and Home to return to
full data range.

acre-x.c

index 4e2cd0fa5e157a6786f98bc3eee090239dd4e90e..c1f7c414f42ef26b74cdb563d8ace4aba7e9dd67 100644 (file)
--- 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;