Actual source code: zoom.c


  2: #include <petscdraw.h>

  4: /*@C
  5:     PetscDrawZoom - Allows one to provide a function that gets called for zooming in on a drawing using the mouse buttons

  7:     Collective draw

  9:     Input Parameters:
 10: +   draw - the window where the graph will be made.
 11: .   func - users function that draws the graphic
 12: -   ctx - pointer to any user required data

 14:   Level: advanced

 16: .seealso: `PetscDraw`, `PetscDrawCreate()`
 17: @*/
 18: PetscErrorCode PetscDrawZoom(PetscDraw draw, PetscErrorCode (*func)(PetscDraw, void *), void *ctx)
 19: {
 20:   PetscDrawButton button;
 21:   PetscReal       dpause, xc, yc, scale = 1.0, w, h, xr, xl, yr, yl, xmin, xmax, ymin, ymax;
 22:   PetscBool       isnull;

 24:   PetscDrawIsNull(draw, &isnull);
 25:   if (isnull) return 0;

 27:   PetscDrawCheckResizedWindow(draw);
 28:   PetscDrawClear(draw);
 29:   PetscDrawCollectiveBegin(draw);
 30:   (*func)(draw, ctx);
 31:   PetscDrawCollectiveEnd(draw);
 32:   PetscDrawFlush(draw);

 34:   PetscDrawGetPause(draw, &dpause);
 35:   if (dpause >= 0) {
 36:     PetscSleep(dpause);
 37:     goto theend;
 38:   }
 39:   if (dpause != -1) goto theend;

 41:   PetscDrawGetMouseButton(draw, &button, &xc, &yc, NULL, NULL);
 42:   PetscDrawGetCoordinates(draw, &xl, &yl, &xr, &yr);
 43:   xmin = xl;
 44:   xmax = xr;
 45:   w    = xr - xl;
 46:   ymin = yl;
 47:   ymax = yr;
 48:   h    = yr - yl;

 50:   while (button != PETSC_BUTTON_NONE && button != PETSC_BUTTON_RIGHT) {
 51:     switch (button) {
 52:     case PETSC_BUTTON_LEFT:
 53:       scale = 0.5;
 54:       break;
 55:     case PETSC_BUTTON_CENTER:
 56:       scale = 2.0;
 57:       break;
 58:     case PETSC_BUTTON_WHEEL_UP:
 59:       scale = 8 / 10.;
 60:       break;
 61:     case PETSC_BUTTON_WHEEL_DOWN:
 62:       scale = 10 / 8.;
 63:       break;
 64:     default:
 65:       scale = 1.0;
 66:     }
 67:     xl = scale * (xl + w - xc) + xc - w * scale;
 68:     xr = scale * (xr - w - xc) + xc + w * scale;
 69:     yl = scale * (yl + h - yc) + yc - h * scale;
 70:     yr = scale * (yr - h - yc) + yc + h * scale;
 71:     w *= scale;
 72:     h *= scale;
 73:     PetscDrawClear(draw);
 74:     PetscDrawSetCoordinates(draw, xl, yl, xr, yr);
 75:     PetscDrawCollectiveBegin(draw);
 76:     (*func)(draw, ctx);
 77:     PetscDrawCollectiveEnd(draw);
 78:     PetscDrawFlush(draw);
 79:     PetscDrawGetMouseButton(draw, &button, &xc, &yc, NULL, NULL);
 80:   }
 81:   PetscDrawSetCoordinates(draw, xmin, ymin, xmax, ymax);
 82: theend:
 83:   return 0;
 84: }