Actual source code: draw.c
petsc-3.9.4 2018-09-11
2: /*
3: Provides the calling sequences for all the basic PetscDraw routines.
4: */
5: #include <petsc/private/drawimpl.h>
6: #include <petscviewer.h>
8: PetscClassId PETSC_DRAW_CLASSID;
10: static PetscBool PetscDrawPackageInitialized = PETSC_FALSE;
11: /*@C
12: PetscDrawFinalizePackage - This function destroys everything in the Petsc interface to the Draw package. It is
13: called from PetscFinalize().
15: Level: developer
17: .keywords: Petsc, destroy, package, mathematica
18: .seealso: PetscFinalize()
19: @*/
20: PetscErrorCode PetscDrawFinalizePackage(void)
21: {
25: PetscFunctionListDestroy(&PetscDrawList);
26: PetscDrawPackageInitialized = PETSC_FALSE;
27: PetscDrawRegisterAllCalled = PETSC_FALSE;
28: return(0);
29: }
31: /*@C
32: PetscInitializeDrawPackage - This function initializes everything in the PetscDraw package. It is called
33: from PetscDLLibraryRegister() when using dynamic libraries, and on the call to PetscInitialize()
34: when using static libraries.
36: Level: developer
38: .keywords: Petsc, initialize, package
39: .seealso: PetscInitialize()
40: @*/
41: PetscErrorCode PetscDrawInitializePackage(void)
42: {
43: char logList[256];
44: PetscBool opt,pkg;
48: if (PetscDrawPackageInitialized) return(0);
49: PetscDrawPackageInitialized = PETSC_TRUE;
50: /* Register Classes */
51: PetscClassIdRegister("Draw",&PETSC_DRAW_CLASSID);
52: PetscClassIdRegister("Draw Axis",&PETSC_DRAWAXIS_CLASSID);
53: PetscClassIdRegister("Line Graph",&PETSC_DRAWLG_CLASSID);
54: PetscClassIdRegister("Histogram",&PETSC_DRAWHG_CLASSID);
55: PetscClassIdRegister("Bar Graph",&PETSC_DRAWBAR_CLASSID);
56: PetscClassIdRegister("Scatter Plot",&PETSC_DRAWSP_CLASSID);
57: /* Register Constructors */
58: PetscDrawRegisterAll();
59: /* Process info exclusions */
60: PetscOptionsGetString(NULL,NULL,"-info_exclude",logList,sizeof(logList),&opt);
61: if (opt) {
62: PetscStrInList("draw",logList,',',&pkg);
63: if (pkg) {
64: PetscInfoDeactivateClass(PETSC_DRAW_CLASSID);
65: PetscInfoDeactivateClass(PETSC_DRAWAXIS_CLASSID);
66: PetscInfoDeactivateClass(PETSC_DRAWLG_CLASSID);
67: PetscInfoDeactivateClass(PETSC_DRAWHG_CLASSID);
68: PetscInfoDeactivateClass(PETSC_DRAWBAR_CLASSID);
69: PetscInfoDeactivateClass(PETSC_DRAWSP_CLASSID);
70: }
71: }
72: /* Process summary exclusions */
73: PetscOptionsGetString(NULL,NULL,"-log_exclude",logList,sizeof(logList),&opt);
74: if (opt) {
75: PetscStrInList("draw",logList,',',&pkg);
76: if (pkg) {
77: PetscLogEventDeactivateClass(PETSC_DRAW_CLASSID);
78: PetscLogEventDeactivateClass(PETSC_DRAWAXIS_CLASSID);
79: PetscLogEventDeactivateClass(PETSC_DRAWLG_CLASSID);
80: PetscLogEventDeactivateClass(PETSC_DRAWHG_CLASSID);
81: PetscLogEventDeactivateClass(PETSC_DRAWBAR_CLASSID);
82: PetscLogEventDeactivateClass(PETSC_DRAWSP_CLASSID);
83: }
84: }
85: /* Register package finalizer */
86: PetscRegisterFinalize(PetscDrawFinalizePackage);
87: return(0);
88: }
90: /*@
91: PetscDrawResizeWindow - Allows one to resize a window from a program.
93: Collective on PetscDraw
95: Input Parameter:
96: + draw - the window
97: - w,h - the new width and height of the window
99: Level: intermediate
101: .seealso: PetscDrawCheckResizedWindow()
102: @*/
103: PetscErrorCode PetscDrawResizeWindow(PetscDraw draw,int w,int h)
104: {
111: if (draw->ops->resizewindow) {
112: (*draw->ops->resizewindow)(draw,w,h);
113: }
114: return(0);
115: }
117: /*@
118: PetscDrawGetWindowSize - Gets the size of the window.
120: Not collective
122: Input Parameter:
123: . draw - the window
125: Output Parameters:
126: . w,h - the window width and height
128: Level: intermediate
130: .seealso: PetscDrawResizeWindow(), PetscDrawCheckResizedWindow()
131: @*/
132: PetscErrorCode PetscDrawGetWindowSize(PetscDraw draw,int *w,int *h)
133: {
138: if (w) *w = draw->w;
139: if (h) *h = draw->h;
140: return(0);
141: }
143: /*@
144: PetscDrawCheckResizedWindow - Checks if the user has resized the window.
146: Collective on PetscDraw
148: Input Parameter:
149: . draw - the window
151: Level: advanced
153: .seealso: PetscDrawResizeWindow()
155: @*/
156: PetscErrorCode PetscDrawCheckResizedWindow(PetscDraw draw)
157: {
162: if (draw->ops->checkresizedwindow) {
163: (*draw->ops->checkresizedwindow)(draw);
164: }
165: return(0);
166: }
168: /*@C
169: PetscDrawGetTitle - Gets pointer to title of a PetscDraw context.
171: Not collective
173: Input Parameter:
174: . draw - the graphics context
176: Output Parameter:
177: . title - the title
179: Level: intermediate
181: .seealso: PetscDrawSetTitle()
182: @*/
183: PetscErrorCode PetscDrawGetTitle(PetscDraw draw,const char *title[])
184: {
188: *title = draw->title;
189: return(0);
190: }
192: /*@C
193: PetscDrawSetTitle - Sets the title of a PetscDraw context.
195: Collective on PetscDraw
197: Input Parameters:
198: + draw - the graphics context
199: - title - the title
201: Level: intermediate
203: Note: The title is positioned in the windowing system title bar for the window. Hence it will not be saved with -draw_save
204: in the image.
206: A copy of the string is made, so you may destroy the
207: title string after calling this routine.
209: You can use PetscDrawAxisSetLabels() to indicate a title within the window
211: .seealso: PetscDrawGetTitle(), PetscDrawAppendTitle()
212: @*/
213: PetscErrorCode PetscDrawSetTitle(PetscDraw draw,const char title[])
214: {
220: PetscFree(draw->title);
221: PetscStrallocpy(title,&draw->title);
222: if (draw->ops->settitle) {
223: (*draw->ops->settitle)(draw,draw->title);
224: }
225: return(0);
226: }
228: /*@C
229: PetscDrawAppendTitle - Appends to the title of a PetscDraw context.
231: Collective on PetscDraw
233: Input Parameters:
234: + draw - the graphics context
235: - title - the title
237: Note:
238: A copy of the string is made, so you may destroy the
239: title string after calling this routine.
241: Level: advanced
243: .seealso: PetscDrawSetTitle(), PetscDrawGetTitle()
244: @*/
245: PetscErrorCode PetscDrawAppendTitle(PetscDraw draw,const char title[])
246: {
252: if (!title || !title[0]) return(0);
254: if (draw->title) {
255: size_t len1,len2;
256: char *newtitle;
257: PetscStrlen(title,&len1);
258: PetscStrlen(draw->title,&len2);
259: PetscMalloc1(len1 + len2 + 1,&newtitle);
260: PetscStrcpy(newtitle,draw->title);
261: PetscStrcat(newtitle,title);
262: PetscFree(draw->title);
263: draw->title = newtitle;
264: } else {
265: PetscStrallocpy(title,&draw->title);
266: }
267: if (draw->ops->settitle) {
268: (*draw->ops->settitle)(draw,draw->title);
269: }
270: return(0);
271: }
273: static PetscErrorCode PetscDrawDestroy_Private(PetscDraw draw)
274: {
278: if (!draw->ops->save && !draw->ops->getimage) return(0);
279: PetscDrawSaveMovie(draw);
280: if (draw->savefinalfilename) {
281: draw->savesinglefile = PETSC_TRUE;
282: PetscDrawSetSave(draw,draw->savefinalfilename);
283: PetscDrawSave(draw);
284: }
285: PetscBarrier((PetscObject)draw);
286: return(0);
287: }
289: /*@
290: PetscDrawDestroy - Deletes a draw context.
292: Collective on PetscDraw
294: Input Parameters:
295: . draw - the drawing context
297: Level: beginner
299: .seealso: PetscDrawCreate()
301: @*/
302: PetscErrorCode PetscDrawDestroy(PetscDraw *draw)
303: {
307: if (!*draw) return(0);
309: if (--((PetscObject)(*draw))->refct > 0) return(0);
311: if ((*draw)->pause == -2) {
312: (*draw)->pause = -1;
313: PetscDrawPause(*draw);
314: }
316: /* if memory was published then destroy it */
317: PetscObjectSAWsViewOff((PetscObject)*draw);
319: PetscDrawDestroy_Private(*draw);
321: if ((*draw)->ops->destroy) {
322: (*(*draw)->ops->destroy)(*draw);
323: }
324: PetscDrawDestroy(&(*draw)->popup);
325: PetscFree((*draw)->title);
326: PetscFree((*draw)->display);
327: PetscFree((*draw)->savefilename);
328: PetscFree((*draw)->saveimageext);
329: PetscFree((*draw)->savemovieext);
330: PetscFree((*draw)->savefinalfilename);
331: PetscHeaderDestroy(draw);
332: return(0);
333: }
335: /*@
336: PetscDrawGetPopup - Creates a popup window associated with a PetscDraw window.
338: Collective on PetscDraw
340: Input Parameter:
341: . draw - the original window
343: Output Parameter:
344: . popup - the new popup window
346: Level: advanced
348: .seealso: PetscDrawScalePopup(), PetscDrawCreate()
350: @*/
351: PetscErrorCode PetscDrawGetPopup(PetscDraw draw,PetscDraw *popup)
352: {
359: if (draw->popup) *popup = draw->popup;
360: else if (draw->ops->getpopup) {
361: (*draw->ops->getpopup)(draw,popup);
362: if (*popup) {
363: PetscObjectSetOptionsPrefix((PetscObject)*popup,"popup_");
364: (*popup)->pause = 0.0;
365: PetscDrawSetFromOptions(*popup);
366: }
367: } else *popup = NULL;
368: return(0);
369: }
371: /*@C
372: PetscDrawSetDisplay - Sets the display where a PetscDraw object will be displayed
374: Input Parameter:
375: + draw - the drawing context
376: - display - the X windows display
378: Level: advanced
380: .seealso: PetscDrawCreate()
382: @*/
383: PetscErrorCode PetscDrawSetDisplay(PetscDraw draw,const char display[])
384: {
388: PetscFree(draw->display);
389: PetscStrallocpy(display,&draw->display);
390: return(0);
391: }
394: /*@
395: PetscDrawSetDoubleBuffer - Sets a window to be double buffered.
397: Logically Collective on PetscDraw
399: Input Parameter:
400: . draw - the drawing context
402: Level: intermediate
404: Concepts: drawing^double buffer
405: Concepts: graphics^double buffer
406: Concepts: double buffer
408: @*/
409: PetscErrorCode PetscDrawSetDoubleBuffer(PetscDraw draw)
410: {
415: if (draw->ops->setdoublebuffer) {
416: (*draw->ops->setdoublebuffer)(draw);
417: }
418: return(0);
419: }
421: /*@C
422: PetscDrawGetSingleton - Gain access to a PetscDraw object as if it were owned
423: by the one process.
425: Collective on PetscDraw
427: Input Parameter:
428: . draw - the original window
430: Output Parameter:
431: . sdraw - the singleton window
433: Level: advanced
435: .seealso: PetscDrawRestoreSingleton(), PetscViewerGetSingleton(), PetscViewerRestoreSingleton()
437: @*/
438: PetscErrorCode PetscDrawGetSingleton(PetscDraw draw,PetscDraw *sdraw)
439: {
441: PetscMPIInt size;
447: MPI_Comm_size(PetscObjectComm((PetscObject)draw),&size);
448: if (size == 1) {
449: PetscObjectReference((PetscObject)draw);
450: *sdraw = draw;
451: } else {
452: if (draw->ops->getsingleton) {
453: (*draw->ops->getsingleton)(draw,sdraw);
454: } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot get singleton for this type %s of draw object",((PetscObject)draw)->type_name);
455: }
456: return(0);
457: }
459: /*@C
460: PetscDrawRestoreSingleton - Remove access to a PetscDraw object as if it were owned
461: by the one process.
463: Collective on PetscDraw
465: Input Parameters:
466: + draw - the original window
467: - sdraw - the singleton window
469: Level: advanced
471: .seealso: PetscDrawGetSingleton(), PetscViewerGetSingleton(), PetscViewerRestoreSingleton()
473: @*/
474: PetscErrorCode PetscDrawRestoreSingleton(PetscDraw draw,PetscDraw *sdraw)
475: {
477: PetscMPIInt size;
484: MPI_Comm_size(PetscObjectComm((PetscObject)draw),&size);
485: if (size == 1) {
486: if (draw == *sdraw) {
487: PetscObjectDereference((PetscObject)draw);
488: *sdraw = NULL;
489: } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot restore singleton, it is not the parent draw");
490: } else {
491: if (draw->ops->restoresingleton) {
492: (*draw->ops->restoresingleton)(draw,sdraw);
493: } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot restore singleton for this type %s of draw object",((PetscObject)draw)->type_name);
494: }
495: return(0);
496: }