Actual source code: zoom.c
1: #include <petscsys.h>
2: #include <petscdraw.h>
4: #if defined(__GNUC__) && !defined(__clang__)
5: #pragma GCC diagnostic push
6: #pragma GCC diagnostic ignored "-Wclobbered"
7: #endif
8: /*@
9: PetscDrawZoom - Allows one to provide a function that gets called for zooming in on a drawing using the mouse buttons
11: Collective draw
13: Input Parameters:
14: + draw - the window where the graph will be made.
15: . func - users function that draws the graphic
16: - ctx - pointer to any application required data
18: Calling sequence of func:
19: + draw - the `PetscDraw` object to zoom on
20: - ctx - the context for the zooming operation
22: Level: advanced
24: .seealso: `PetscDraw`, `PetscDrawCreate()`
25: @*/
26: PetscErrorCode PetscDrawZoom(PetscDraw draw, PetscErrorCode (*func)(PetscDraw draw, PetscCtx ctx), PetscCtx ctx)
27: {
28: PetscDrawButton button;
29: PetscReal dpause, xc, yc, scale = 1.0, w, h, xr, xl, yr, yl, xmin, xmax, ymin, ymax;
30: PetscBool isnull;
32: PetscFunctionBegin;
33: PetscCall(PetscDrawIsNull(draw, &isnull));
34: if (isnull) PetscFunctionReturn(PETSC_SUCCESS);
36: PetscCall(PetscDrawCheckResizedWindow(draw));
37: PetscCall(PetscDrawClear(draw));
38: PetscDrawCollectiveBegin(draw);
39: PetscCall((*func)(draw, ctx));
40: PetscDrawCollectiveEnd(draw);
41: PetscCall(PetscDrawFlush(draw));
43: PetscCall(PetscDrawGetPause(draw, &dpause));
44: if (dpause >= 0) {
45: PetscCall(PetscSleep(dpause));
46: goto theend;
47: }
48: if (dpause != -1) goto theend;
50: PetscCall(PetscDrawGetMouseButton(draw, &button, &xc, &yc, NULL, NULL));
51: PetscCall(PetscDrawGetCoordinates(draw, &xl, &yl, &xr, &yr));
52: xmin = xl;
53: xmax = xr;
54: w = xr - xl;
55: ymin = yl;
56: ymax = yr;
57: h = yr - yl;
59: while (button != PETSC_BUTTON_NONE && button != PETSC_BUTTON_RIGHT) {
60: switch (button) {
61: case PETSC_BUTTON_LEFT:
62: scale = 0.5;
63: break;
64: case PETSC_BUTTON_CENTER:
65: scale = 2.0;
66: break;
67: case PETSC_BUTTON_WHEEL_UP:
68: scale = 8 / 10.;
69: break;
70: case PETSC_BUTTON_WHEEL_DOWN:
71: scale = 10 / 8.;
72: break;
73: default:
74: scale = 1.0;
75: }
76: xl = scale * (xl + w - xc) + xc - w * scale;
77: xr = scale * (xr - w - xc) + xc + w * scale;
78: yl = scale * (yl + h - yc) + yc - h * scale;
79: yr = scale * (yr - h - yc) + yc + h * scale;
80: w *= scale;
81: h *= scale;
82: PetscCall(PetscDrawClear(draw));
83: PetscCall(PetscDrawSetCoordinates(draw, xl, yl, xr, yr));
84: PetscDrawCollectiveBegin(draw);
85: PetscCall((*func)(draw, ctx));
86: PetscDrawCollectiveEnd(draw);
87: PetscCall(PetscDrawFlush(draw));
88: PetscCall(PetscDrawGetMouseButton(draw, &button, &xc, &yc, NULL, NULL));
89: }
90: PetscCall(PetscDrawSetCoordinates(draw, xmin, ymin, xmax, ymax));
91: theend:
92: PetscFunctionReturn(PETSC_SUCCESS);
93: }
94: #if defined(__GNUC__) && !defined(__clang__)
95: #pragma GCC diagnostic pop
96: #endif