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