xref: /petsc/src/sys/classes/draw/interface/draw.c (revision 39d7646b82f3c61fed60464cb8cf8b037dcb3c88)
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   PetscFunctionBegin;
25   PetscDrawPackageInitialized = PETSC_FALSE;
26   PetscDrawList               = 0;
27   PetscFunctionReturn(0);
28 }
29 
30 #undef __FUNCT__
31 #define __FUNCT__ "PetscDrawInitializePackage"
32 /*@C
33   PetscInitializeDrawPackage - This function initializes everything in the PetscDraw package. It is called
34   from PetscDLLibraryRegister() when using dynamic libraries, and on the call to PetscInitialize()
35   when using static libraries.
36 
37   Input Parameter:
38   path - The dynamic library path, or NULL
39 
40   Level: developer
41 
42 .keywords: Petsc, initialize, package
43 .seealso: PetscInitialize()
44 @*/
45 PetscErrorCode  PetscDrawInitializePackage(const char path[])
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(path);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 = PetscMalloc((len + 1)*sizeof(char*),&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 = PetscObjectDepublish(*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 = 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   } else *popup = NULL;
318   PetscFunctionReturn(0);
319 }
320 
321 #undef __FUNCT__
322 #define __FUNCT__ "PetscDrawDestroy_Null"
323 PetscErrorCode PetscDrawDestroy_Null(PetscDraw draw)
324 {
325   PetscFunctionBegin;
326   PetscFunctionReturn(0);
327 }
328 
329 #undef __FUNCT__
330 #define __FUNCT__ "PetscDrawOpenNull"
331 /*
332   PetscDrawOpenNull - Opens a null drawing context. All draw commands to
333   it are ignored.
334 
335   Output Parameter:
336 . win - the drawing context
337 
338    Level: advanced
339 
340 */
341 PetscErrorCode  PetscDrawOpenNull(MPI_Comm comm,PetscDraw *win)
342 {
343   PetscErrorCode ierr;
344 
345   PetscFunctionBegin;
346   ierr = PetscDrawCreate(comm,NULL,NULL,0,0,1,1,win);CHKERRQ(ierr);
347   ierr = PetscDrawSetType(*win,PETSC_DRAW_NULL);CHKERRQ(ierr);
348   PetscFunctionReturn(0);
349 }
350 
351 #undef __FUNCT__
352 #define __FUNCT__ "PetscDrawSetDisplay"
353 /*@
354   PetscDrawSetDisplay - Sets the display where a PetscDraw object will be displayed
355 
356   Input Parameter:
357 + draw - the drawing context
358 - display - the X windows display
359 
360   Level: advanced
361 
362 @*/
363 PetscErrorCode  PetscDrawSetDisplay(PetscDraw draw,const char display[])
364 {
365   PetscErrorCode ierr;
366 
367   PetscFunctionBegin;
368   ierr = PetscFree(draw->display);CHKERRQ(ierr);
369   ierr = PetscStrallocpy(display,&draw->display);CHKERRQ(ierr);
370   PetscFunctionReturn(0);
371 }
372 
373 #undef __FUNCT__
374 #define __FUNCT__ "PetscDrawCreate_Null"
375 /*
376   PetscDrawCreate_Null - Opens a null drawing context. All draw commands to
377   it are ignored.
378 
379   Input Parameter:
380 . win - the drawing context
381 */
382 PETSC_EXTERN PetscErrorCode PetscDrawCreate_Null(PetscDraw draw)
383 {
384   PetscErrorCode ierr;
385 
386   PetscFunctionBegin;
387   ierr = PetscMemzero(draw->ops,sizeof(struct _PetscDrawOps));CHKERRQ(ierr);
388 
389   draw->ops->destroy = PetscDrawDestroy_Null;
390   draw->ops->view    = 0;
391   draw->pause        = 0.0;
392   draw->coor_xl      = 0.0;  draw->coor_xr = 1.0;
393   draw->coor_yl      = 0.0;  draw->coor_yr = 1.0;
394   draw->port_xl      = 0.0;  draw->port_xr = 1.0;
395   draw->port_yl      = 0.0;  draw->port_yr = 1.0;
396   draw->popup        = 0;
397   PetscFunctionReturn(0);
398 }
399 
400 #undef __FUNCT__
401 #define __FUNCT__ "PetscDrawGetSingleton"
402 /*@C
403    PetscDrawGetSingleton - Gain access to a PetscDraw object as if it were owned
404         by the one process.
405 
406    Collective on PetscDraw
407 
408    Input Parameter:
409 .  draw - the original window
410 
411    Output Parameter:
412 .  sdraw - the singleton window
413 
414    Level: advanced
415 
416 .seealso: PetscDrawRestoreSingleton(), PetscViewerGetSingleton(), PetscViewerRestoreSingleton()
417 
418 @*/
419 PetscErrorCode  PetscDrawGetSingleton(PetscDraw draw,PetscDraw *sdraw)
420 {
421   PetscErrorCode ierr;
422   PetscMPIInt    size;
423 
424   PetscFunctionBegin;
425   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
426   PetscValidPointer(sdraw,2);
427 
428   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)draw),&size);CHKERRQ(ierr);
429   if (size == 1) *sdraw = draw;
430   else {
431     if (draw->ops->getsingleton) {
432       ierr = (*draw->ops->getsingleton)(draw,sdraw);CHKERRQ(ierr);
433     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot get singleton for this type %s of draw object",((PetscObject)draw)->type_name);
434   }
435   PetscFunctionReturn(0);
436 }
437 
438 #undef __FUNCT__
439 #define __FUNCT__ "PetscDrawRestoreSingleton"
440 /*@C
441    PetscDrawRestoreSingleton - Remove access to a PetscDraw object as if it were owned
442         by the one process.
443 
444    Collective on PetscDraw
445 
446    Input Parameters:
447 +  draw - the original window
448 -  sdraw - the singleton window
449 
450    Level: advanced
451 
452 .seealso: PetscDrawGetSingleton(), PetscViewerGetSingleton(), PetscViewerRestoreSingleton()
453 
454 @*/
455 PetscErrorCode  PetscDrawRestoreSingleton(PetscDraw draw,PetscDraw *sdraw)
456 {
457   PetscErrorCode ierr;
458   PetscMPIInt    size;
459 
460   PetscFunctionBegin;
461   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
462   PetscValidPointer(sdraw,2);
463   PetscValidHeaderSpecific(*sdraw,PETSC_DRAW_CLASSID,2);
464 
465   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)draw),&size);CHKERRQ(ierr);
466   if (size != 1) {
467     if (draw->ops->restoresingleton) {
468       ierr = (*draw->ops->restoresingleton)(draw,sdraw);CHKERRQ(ierr);
469     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot restore singleton for this type %s of draw object",((PetscObject)draw)->type_name);
470   }
471   PetscFunctionReturn(0);
472 }
473 
474 
475 
476 
477 
478 
479 
480