xref: /petsc/src/sys/classes/draw/interface/draw.c (revision fe998a80077c9ee0917a39496df43fc256e1b478)
1 
2 /*
3        Provides the calling sequences for all the basic PetscDraw routines.
4 */
5 #include <petsc-private/drawimpl.h>  /*I "petscdraw.h" I*/
6 #include <petscviewer.h>
7 
8 PetscClassId PETSC_DRAW_CLASSID;
9 
10 static PetscBool PetscDrawPackageInitialized = PETSC_FALSE;
11 #undef __FUNCT__
12 #define __FUNCT__ "PetscDrawFinalizePackage"
13 /*@C
14   PetscDrawFinalizePackage - This function destroys everything in the Petsc interface to the Draw package. It is
15   called from PetscFinalize().
16 
17   Level: developer
18 
19 .keywords: Petsc, destroy, package, mathematica
20 .seealso: PetscFinalize()
21 @*/
22 PetscErrorCode  PetscDrawFinalizePackage(void)
23 {
24   PetscErrorCode ierr;
25 
26   PetscFunctionBegin;
27   ierr = PetscFunctionListDestroy(&PetscDrawList);CHKERRQ(ierr);
28   PetscDrawPackageInitialized = PETSC_FALSE;
29   PetscFunctionReturn(0);
30 }
31 
32 #undef __FUNCT__
33 #define __FUNCT__ "PetscDrawInitializePackage"
34 /*@C
35   PetscInitializeDrawPackage - This function initializes everything in the PetscDraw package. It is called
36   from PetscDLLibraryRegister() when using dynamic libraries, and on the call to PetscInitialize()
37   when using static libraries.
38 
39   Level: developer
40 
41 .keywords: Petsc, initialize, package
42 .seealso: PetscInitialize()
43 @*/
44 PetscErrorCode  PetscDrawInitializePackage(void)
45 {
46   char           logList[256];
47   char           *className;
48   PetscBool      opt;
49   PetscErrorCode ierr;
50 
51   PetscFunctionBegin;
52   if (PetscDrawPackageInitialized) PetscFunctionReturn(0);
53   PetscDrawPackageInitialized = PETSC_TRUE;
54   /* Register Classes */
55   ierr = PetscClassIdRegister("Draw",&PETSC_DRAW_CLASSID);CHKERRQ(ierr);
56   ierr = PetscClassIdRegister("Axis",&PETSC_DRAWAXIS_CLASSID);CHKERRQ(ierr);
57   ierr = PetscClassIdRegister("Line Graph",&PETSC_DRAWLG_CLASSID);CHKERRQ(ierr);
58   ierr = PetscClassIdRegister("Histogram",&PETSC_DRAWHG_CLASSID);CHKERRQ(ierr);
59   ierr = PetscClassIdRegister("Scatter Plot",&PETSC_DRAWSP_CLASSID);CHKERRQ(ierr);
60   /* Register Constructors */
61   ierr = PetscDrawRegisterAll();CHKERRQ(ierr);
62   /* Process info exclusions */
63   ierr = PetscOptionsGetString(NULL, "-info_exclude", logList, 256, &opt);CHKERRQ(ierr);
64   if (opt) {
65     ierr = PetscStrstr(logList, "draw", &className);CHKERRQ(ierr);
66     if (className) {
67       ierr = PetscInfoDeactivateClass(0);CHKERRQ(ierr);
68     }
69   }
70   /* Process summary exclusions */
71   ierr = PetscOptionsGetString(NULL, "-log_summary_exclude", logList, 256, &opt);CHKERRQ(ierr);
72   if (opt) {
73     ierr = PetscStrstr(logList, "draw", &className);CHKERRQ(ierr);
74     if (className) {
75       ierr = PetscLogEventDeactivateClass(0);CHKERRQ(ierr);
76     }
77   }
78   ierr = PetscRegisterFinalize(PetscDrawFinalizePackage);CHKERRQ(ierr);
79   PetscFunctionReturn(0);
80 }
81 
82 #undef __FUNCT__
83 #define __FUNCT__ "PetscDrawResizeWindow"
84 /*@
85    PetscDrawResizeWindow - Allows one to resize a window from a program.
86 
87    Collective on PetscDraw
88 
89    Input Parameter:
90 +  draw - the window
91 -  w,h - the new width and height of the window
92 
93    Level: intermediate
94 
95 .seealso: PetscDrawCheckResizedWindow()
96 @*/
97 PetscErrorCode  PetscDrawResizeWindow(PetscDraw draw,int w,int h)
98 {
99   PetscErrorCode ierr;
100 
101   PetscFunctionBegin;
102   if (draw->ops->resizewindow) {
103     ierr = (*draw->ops->resizewindow)(draw,w,h);CHKERRQ(ierr);
104   }
105   PetscFunctionReturn(0);
106 }
107 
108 #undef __FUNCT__
109 #define __FUNCT__ "PetscDrawCheckResizedWindow"
110 /*@
111    PetscDrawCheckResizedWindow - Checks if the user has resized the window.
112 
113    Collective on PetscDraw
114 
115    Input Parameter:
116 .  draw - the window
117 
118    Level: advanced
119 
120 .seealso: PetscDrawResizeWindow()
121 
122 @*/
123 PetscErrorCode  PetscDrawCheckResizedWindow(PetscDraw draw)
124 {
125   PetscErrorCode ierr;
126 
127   PetscFunctionBegin;
128   if (draw->ops->checkresizedwindow) {
129     ierr = (*draw->ops->checkresizedwindow)(draw);CHKERRQ(ierr);
130   }
131   PetscFunctionReturn(0);
132 }
133 
134 #undef __FUNCT__
135 #define __FUNCT__ "PetscDrawGetTitle"
136 /*@C
137    PetscDrawGetTitle - Gets pointer to title of a PetscDraw context.
138 
139    Not collective
140 
141    Input Parameter:
142 .  draw - the graphics context
143 
144    Output Parameter:
145 .  title - the title
146 
147    Level: intermediate
148 
149 .seealso: PetscDrawSetTitle()
150 @*/
151 PetscErrorCode  PetscDrawGetTitle(PetscDraw draw,char **title)
152 {
153   PetscFunctionBegin;
154   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
155   PetscValidPointer(title,2);
156   *title = draw->title;
157   PetscFunctionReturn(0);
158 }
159 
160 #undef __FUNCT__
161 #define __FUNCT__ "PetscDrawSetTitle"
162 /*@C
163    PetscDrawSetTitle - Sets the title of a PetscDraw context.
164 
165    Not collective (any processor or all may call this)
166 
167    Input Parameters:
168 +  draw - the graphics context
169 -  title - the title
170 
171    Level: intermediate
172 
173    Note: The title is positioned in the windowing system title bar for the window. Hence it will not be saved with -draw_save
174    in the image.
175 
176    A copy of the string is made, so you may destroy the
177    title string after calling this routine.
178 
179    You can use PetscDrawAxisSetLabels() to indicate a title within the window
180 
181 .seealso: PetscDrawGetTitle(), PetscDrawAppendTitle()
182 @*/
183 PetscErrorCode  PetscDrawSetTitle(PetscDraw draw,const char title[])
184 {
185   PetscErrorCode ierr;
186 
187   PetscFunctionBegin;
188   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
189   PetscValidCharPointer(title,2);
190   ierr = PetscFree(draw->title);CHKERRQ(ierr);
191   ierr = PetscStrallocpy(title,&draw->title);CHKERRQ(ierr);
192   if (draw->ops->settitle) {
193     ierr = (*draw->ops->settitle)(draw,title);CHKERRQ(ierr);
194   }
195   PetscFunctionReturn(0);
196 }
197 
198 #undef __FUNCT__
199 #define __FUNCT__ "PetscDrawAppendTitle"
200 /*@C
201    PetscDrawAppendTitle - Appends to the title of a PetscDraw context.
202 
203    Not collective (any processor or all can call this)
204 
205    Input Parameters:
206 +  draw - the graphics context
207 -  title - the title
208 
209    Note:
210    A copy of the string is made, so you may destroy the
211    title string after calling this routine.
212 
213    Level: advanced
214 
215 .seealso: PetscDrawSetTitle(), PetscDrawGetTitle()
216 @*/
217 PetscErrorCode  PetscDrawAppendTitle(PetscDraw draw,const char title[])
218 {
219   PetscErrorCode ierr;
220   size_t         len1,len2,len;
221   char           *newtitle;
222 
223   PetscFunctionBegin;
224   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
225   if (!title) PetscFunctionReturn(0);
226 
227   if (draw->title) {
228     ierr = PetscStrlen(title,&len1);CHKERRQ(ierr);
229     ierr = PetscStrlen(draw->title,&len2);CHKERRQ(ierr);
230     len  = len1 + len2;
231     ierr = PetscMalloc1(len + 1,&newtitle);CHKERRQ(ierr);
232     ierr = PetscStrcpy(newtitle,draw->title);CHKERRQ(ierr);
233     ierr = PetscStrcat(newtitle,title);CHKERRQ(ierr);
234     ierr = PetscFree(draw->title);CHKERRQ(ierr);
235 
236     draw->title = newtitle;
237   } else {
238     ierr = PetscStrallocpy(title,&draw->title);CHKERRQ(ierr);
239   }
240   if (draw->ops->settitle) {
241     ierr = (*draw->ops->settitle)(draw,draw->title);CHKERRQ(ierr);
242   }
243   PetscFunctionReturn(0);
244 }
245 
246 #undef __FUNCT__
247 #define __FUNCT__ "PetscDrawDestroy"
248 /*@
249    PetscDrawDestroy - Deletes a draw context.
250 
251    Collective on PetscDraw
252 
253    Input Parameters:
254 .  draw - the drawing context
255 
256    Level: beginner
257 
258 .seealso: PetscDrawCreate()
259 
260 @*/
261 PetscErrorCode  PetscDrawDestroy(PetscDraw *draw)
262 {
263   PetscErrorCode ierr;
264 
265   PetscFunctionBegin;
266   if (!*draw) PetscFunctionReturn(0);
267   PetscValidHeaderSpecific(*draw,PETSC_DRAW_CLASSID,1);
268   if (--((PetscObject)(*draw))->refct > 0) PetscFunctionReturn(0);
269 
270   if ((*draw)->pause == -2) {
271     (*draw)->pause = -1;
272 
273     ierr = PetscDrawPause(*draw);CHKERRQ(ierr);
274   }
275 
276   /* if memory was published then destroy it */
277   ierr = PetscObjectSAWsViewOff((PetscObject)*draw);CHKERRQ(ierr);
278 
279   if ((*draw)->ops->destroy) {
280     ierr = (*(*draw)->ops->destroy)(*draw);CHKERRQ(ierr);
281   }
282   ierr = PetscFree((*draw)->title);CHKERRQ(ierr);
283   ierr = PetscFree((*draw)->display);CHKERRQ(ierr);
284   ierr = PetscFree((*draw)->savefilename);CHKERRQ(ierr);
285   ierr = PetscFree((*draw)->savefinalfilename);CHKERRQ(ierr);
286   ierr = PetscHeaderDestroy(draw);CHKERRQ(ierr);
287   PetscFunctionReturn(0);
288 }
289 
290 #undef __FUNCT__
291 #define __FUNCT__ "PetscDrawGetPopup"
292 /*@
293    PetscDrawGetPopup - Creates a popup window associated with a PetscDraw window.
294 
295    Collective on PetscDraw
296 
297    Input Parameter:
298 .  draw - the original window
299 
300    Output Parameter:
301 .  popup - the new popup window
302 
303    Level: advanced
304 
305 @*/
306 PetscErrorCode  PetscDrawGetPopup(PetscDraw draw,PetscDraw *popup)
307 {
308   PetscErrorCode ierr;
309 
310   PetscFunctionBegin;
311   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
312   PetscValidPointer(popup,2);
313 
314   if (draw->popup) *popup = draw->popup;
315   else if (draw->ops->getpopup) {
316     ierr = (*draw->ops->getpopup)(draw,popup);CHKERRQ(ierr);
317     if (*popup) {
318       ierr = PetscObjectSetOptionsPrefix((PetscObject)*popup,"popup_");CHKERRQ(ierr);
319       ierr = PetscDrawSetFromOptions(*popup);CHKERRQ(ierr);
320     }
321   } else *popup = NULL;
322   PetscFunctionReturn(0);
323 }
324 
325 #undef __FUNCT__
326 #define __FUNCT__ "PetscDrawDestroy_Null"
327 PetscErrorCode PetscDrawDestroy_Null(PetscDraw draw)
328 {
329   PetscFunctionBegin;
330   PetscFunctionReturn(0);
331 }
332 
333 #undef __FUNCT__
334 #define __FUNCT__ "PetscDrawOpenNull"
335 /*
336   PetscDrawOpenNull - Opens a null drawing context. All draw commands to
337   it are ignored.
338 
339   Output Parameter:
340 . win - the drawing context
341 
342    Level: advanced
343 
344 */
345 PetscErrorCode  PetscDrawOpenNull(MPI_Comm comm,PetscDraw *win)
346 {
347   PetscErrorCode ierr;
348 
349   PetscFunctionBegin;
350   ierr = PetscDrawCreate(comm,NULL,NULL,0,0,1,1,win);CHKERRQ(ierr);
351   ierr = PetscDrawSetType(*win,PETSC_DRAW_NULL);CHKERRQ(ierr);
352   PetscFunctionReturn(0);
353 }
354 
355 #undef __FUNCT__
356 #define __FUNCT__ "PetscDrawSetDisplay"
357 /*@
358   PetscDrawSetDisplay - Sets the display where a PetscDraw object will be displayed
359 
360   Input Parameter:
361 + draw - the drawing context
362 - display - the X windows display
363 
364   Level: advanced
365 
366 @*/
367 PetscErrorCode  PetscDrawSetDisplay(PetscDraw draw,const char display[])
368 {
369   PetscErrorCode ierr;
370 
371   PetscFunctionBegin;
372   ierr = PetscFree(draw->display);CHKERRQ(ierr);
373   ierr = PetscStrallocpy(display,&draw->display);CHKERRQ(ierr);
374   PetscFunctionReturn(0);
375 }
376 
377 #undef __FUNCT__
378 #define __FUNCT__ "PetscDrawCreate_Null"
379 /*
380   PetscDrawCreate_Null - Opens a null drawing context. All draw commands to
381   it are ignored.
382 
383   Input Parameter:
384 . win - the drawing context
385 */
386 PETSC_EXTERN PetscErrorCode PetscDrawCreate_Null(PetscDraw draw)
387 {
388   PetscErrorCode ierr;
389 
390   PetscFunctionBegin;
391   ierr = PetscMemzero(draw->ops,sizeof(struct _PetscDrawOps));CHKERRQ(ierr);
392 
393   draw->ops->destroy = PetscDrawDestroy_Null;
394   draw->ops->view    = 0;
395   draw->pause        = 0.0;
396   draw->coor_xl      = 0.0;  draw->coor_xr = 1.0;
397   draw->coor_yl      = 0.0;  draw->coor_yr = 1.0;
398   draw->port_xl      = 0.0;  draw->port_xr = 1.0;
399   draw->port_yl      = 0.0;  draw->port_yr = 1.0;
400   draw->popup        = 0;
401   PetscFunctionReturn(0);
402 }
403 
404 #undef __FUNCT__
405 #define __FUNCT__ "PetscDrawGetSingleton"
406 /*@C
407    PetscDrawGetSingleton - Gain access to a PetscDraw object as if it were owned
408         by the one process.
409 
410    Collective on PetscDraw
411 
412    Input Parameter:
413 .  draw - the original window
414 
415    Output Parameter:
416 .  sdraw - the singleton window
417 
418    Level: advanced
419 
420 .seealso: PetscDrawRestoreSingleton(), PetscViewerGetSingleton(), PetscViewerRestoreSingleton()
421 
422 @*/
423 PetscErrorCode  PetscDrawGetSingleton(PetscDraw draw,PetscDraw *sdraw)
424 {
425   PetscErrorCode ierr;
426   PetscMPIInt    size;
427 
428   PetscFunctionBegin;
429   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
430   PetscValidPointer(sdraw,2);
431 
432   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)draw),&size);CHKERRQ(ierr);
433   if (size == 1) *sdraw = draw;
434   else {
435     if (draw->ops->getsingleton) {
436       ierr = (*draw->ops->getsingleton)(draw,sdraw);CHKERRQ(ierr);
437     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot get singleton for this type %s of draw object",((PetscObject)draw)->type_name);
438   }
439   PetscFunctionReturn(0);
440 }
441 
442 #undef __FUNCT__
443 #define __FUNCT__ "PetscDrawRestoreSingleton"
444 /*@C
445    PetscDrawRestoreSingleton - Remove access to a PetscDraw object as if it were owned
446         by the one process.
447 
448    Collective on PetscDraw
449 
450    Input Parameters:
451 +  draw - the original window
452 -  sdraw - the singleton window
453 
454    Level: advanced
455 
456 .seealso: PetscDrawGetSingleton(), PetscViewerGetSingleton(), PetscViewerRestoreSingleton()
457 
458 @*/
459 PetscErrorCode  PetscDrawRestoreSingleton(PetscDraw draw,PetscDraw *sdraw)
460 {
461   PetscErrorCode ierr;
462   PetscMPIInt    size;
463 
464   PetscFunctionBegin;
465   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
466   PetscValidPointer(sdraw,2);
467   PetscValidHeaderSpecific(*sdraw,PETSC_DRAW_CLASSID,2);
468 
469   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)draw),&size);CHKERRQ(ierr);
470   if (size != 1) {
471     if (draw->ops->restoresingleton) {
472       ierr = (*draw->ops->restoresingleton)(draw,sdraw);CHKERRQ(ierr);
473     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot restore singleton for this type %s of draw object",((PetscObject)draw)->type_name);
474   }
475   PetscFunctionReturn(0);
476 }
477 
478 
479 
480 
481 
482 
483 
484