xref: /petsc/src/sys/classes/draw/utils/zoom.c (revision 356ed81403a8ddb9cbcae868d64486ea275d004c)
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   PetscErrorCode ierr;
24 
25   PetscFunctionBegin;
26   PetscCall(PetscDrawIsNull(draw,&isnull));
27   if (isnull) PetscFunctionReturn(0);
28 
29   PetscCall(PetscDrawCheckResizedWindow(draw));
30   PetscCall(PetscDrawClear(draw));
31   ierr = PetscDrawCollectiveBegin(draw);PetscCall(ierr);
32   PetscCall((*func)(draw,ctx));
33   ierr = PetscDrawCollectiveEnd(draw);PetscCall(ierr);
34   PetscCall(PetscDrawFlush(draw));
35 
36   PetscCall(PetscDrawGetPause(draw,&dpause));
37   if (dpause >= 0) {
38     PetscCall(PetscSleep(dpause));
39     goto theend;
40   }
41   if (dpause != -1) goto theend;
42 
43   PetscCall(PetscDrawGetMouseButton(draw,&button,&xc,&yc,NULL,NULL));
44   PetscCall(PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr));
45   xmin = xl; xmax = xr; w = xr - xl;
46   ymin = yl; ymax = yr; h = yr - yl;
47 
48   while (button != PETSC_BUTTON_NONE && button != PETSC_BUTTON_RIGHT) {
49     switch (button) {
50     case PETSC_BUTTON_LEFT:       scale = 0.5;   break;
51     case PETSC_BUTTON_CENTER:     scale = 2.0;   break;
52     case PETSC_BUTTON_WHEEL_UP:   scale = 8/10.; break;
53     case PETSC_BUTTON_WHEEL_DOWN: scale = 10/8.; break;
54     default:                      scale = 1.0;
55     }
56     xl = scale*(xl + w - xc) + xc - w*scale;
57     xr = scale*(xr - w - xc) + xc + w*scale;
58     yl = scale*(yl + h - yc) + yc - h*scale;
59     yr = scale*(yr - h - yc) + yc + h*scale;
60     w *= scale; h *= scale;
61     PetscCall(PetscDrawClear(draw));
62     PetscCall(PetscDrawSetCoordinates(draw,xl,yl,xr,yr));
63     ierr = PetscDrawCollectiveBegin(draw);PetscCall(ierr);
64     PetscCall((*func)(draw,ctx));
65     ierr = PetscDrawCollectiveEnd(draw);PetscCall(ierr);
66     PetscCall(PetscDrawFlush(draw));
67     PetscCall(PetscDrawGetMouseButton(draw,&button,&xc,&yc,NULL,NULL));
68   }
69   PetscCall(PetscDrawSetCoordinates(draw,xmin,ymin,xmax,ymax));
70 theend:
71   PetscFunctionReturn(0);
72 }
73