Date: 2022-03-31T04:46:02+00:00

Actual source code: zoom.c

      2: #include <petscdraw.h>

      4: /*@C
      5:     PetscDrawZoom - Allows one to create a graphic that users may zoom into.

      7:     Collective on PetscDraw

      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:
     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;

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

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

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

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

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