1 2 #include <petscdraw.h> /*I "petscdraw.h" I*/ 3 4 /*@C 5 PetscDrawZoom - Allows one to create a graphic that users may zoom into. 6 7 Collective on PetscDraw 8 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 13 14 Level: advanced 15 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; 23 24 PetscFunctionBegin; 25 PetscCall(PetscDrawIsNull(draw,&isnull)); 26 if (isnull) PetscFunctionReturn(0); 27 28 PetscCall(PetscDrawCheckResizedWindow(draw)); 29 PetscCall(PetscDrawClear(draw)); 30 PetscDrawCollectiveBegin(draw); 31 PetscCall((*func)(draw,ctx)); 32 PetscDrawCollectiveEnd(draw); 33 PetscCall(PetscDrawFlush(draw)); 34 35 PetscCall(PetscDrawGetPause(draw,&dpause)); 36 if (dpause >= 0) { 37 PetscCall(PetscSleep(dpause)); 38 goto theend; 39 } 40 if (dpause != -1) goto theend; 41 42 PetscCall(PetscDrawGetMouseButton(draw,&button,&xc,&yc,NULL,NULL)); 43 PetscCall(PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr)); 44 xmin = xl; xmax = xr; w = xr - xl; 45 ymin = yl; ymax = yr; h = yr - yl; 46 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 PetscCall(PetscDrawClear(draw)); 61 PetscCall(PetscDrawSetCoordinates(draw,xl,yl,xr,yr)); 62 PetscDrawCollectiveBegin(draw); 63 PetscCall((*func)(draw,ctx)); 64 PetscDrawCollectiveEnd(draw); 65 PetscCall(PetscDrawFlush(draw)); 66 PetscCall(PetscDrawGetMouseButton(draw,&button,&xc,&yc,NULL,NULL)); 67 } 68 PetscCall(PetscDrawSetCoordinates(draw,xmin,ymin,xmax,ymax)); 69 theend: 70 PetscFunctionReturn(0); 71 } 72