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