xref: /petsc/src/sys/classes/draw/interface/draw.c (revision 9895aa37ac365bac650f6bd8bf977519f7222510)
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:
175    A copy of the string is made, so you may destroy the
176    title string after calling this routine.
177 
178 .seealso: PetscDrawGetTitle(), PetscDrawAppendTitle()
179 @*/
180 PetscErrorCode  PetscDrawSetTitle(PetscDraw draw,const char title[])
181 {
182   PetscErrorCode ierr;
183 
184   PetscFunctionBegin;
185   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
186   PetscValidCharPointer(title,2);
187   ierr = PetscFree(draw->title);CHKERRQ(ierr);
188   ierr = PetscStrallocpy(title,&draw->title);CHKERRQ(ierr);
189   if (draw->ops->settitle) {
190     ierr = (*draw->ops->settitle)(draw,title);CHKERRQ(ierr);
191   }
192   PetscFunctionReturn(0);
193 }
194 
195 #undef __FUNCT__
196 #define __FUNCT__ "PetscDrawAppendTitle"
197 /*@C
198    PetscDrawAppendTitle - Appends to the title of a PetscDraw context.
199 
200    Not collective (any processor or all can call this)
201 
202    Input Parameters:
203 +  draw - the graphics context
204 -  title - the title
205 
206    Note:
207    A copy of the string is made, so you may destroy the
208    title string after calling this routine.
209 
210    Level: advanced
211 
212 .seealso: PetscDrawSetTitle(), PetscDrawGetTitle()
213 @*/
214 PetscErrorCode  PetscDrawAppendTitle(PetscDraw draw,const char title[])
215 {
216   PetscErrorCode ierr;
217   size_t         len1,len2,len;
218   char           *newtitle;
219 
220   PetscFunctionBegin;
221   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
222   if (!title) PetscFunctionReturn(0);
223 
224   if (draw->title) {
225     ierr = PetscStrlen(title,&len1);CHKERRQ(ierr);
226     ierr = PetscStrlen(draw->title,&len2);CHKERRQ(ierr);
227     len  = len1 + len2;
228     ierr = PetscMalloc((len + 1)*sizeof(char*),&newtitle);CHKERRQ(ierr);
229     ierr = PetscStrcpy(newtitle,draw->title);CHKERRQ(ierr);
230     ierr = PetscStrcat(newtitle,title);CHKERRQ(ierr);
231     ierr = PetscFree(draw->title);CHKERRQ(ierr);
232 
233     draw->title = newtitle;
234   } else {
235     ierr = PetscStrallocpy(title,&draw->title);CHKERRQ(ierr);
236   }
237   if (draw->ops->settitle) {
238     ierr = (*draw->ops->settitle)(draw,draw->title);CHKERRQ(ierr);
239   }
240   PetscFunctionReturn(0);
241 }
242 
243 #undef __FUNCT__
244 #define __FUNCT__ "PetscDrawDestroy"
245 /*@
246    PetscDrawDestroy - Deletes a draw context.
247 
248    Collective on PetscDraw
249 
250    Input Parameters:
251 .  draw - the drawing context
252 
253    Level: beginner
254 
255 .seealso: PetscDrawCreate()
256 
257 @*/
258 PetscErrorCode  PetscDrawDestroy(PetscDraw *draw)
259 {
260   PetscErrorCode ierr;
261 
262   PetscFunctionBegin;
263   if (!*draw) PetscFunctionReturn(0);
264   PetscValidHeaderSpecific(*draw,PETSC_DRAW_CLASSID,1);
265   if (--((PetscObject)(*draw))->refct > 0) PetscFunctionReturn(0);
266 
267   if ((*draw)->pause == -2) {
268     (*draw)->pause = -1;
269 
270     ierr = PetscDrawPause(*draw);CHKERRQ(ierr);
271   }
272 
273   /* if memory was published then destroy it */
274   ierr = PetscObjectDepublish(*draw);CHKERRQ(ierr);
275 
276   if ((*draw)->ops->destroy) {
277     ierr = (*(*draw)->ops->destroy)(*draw);CHKERRQ(ierr);
278   }
279   ierr = PetscFree((*draw)->title);CHKERRQ(ierr);
280   ierr = PetscFree((*draw)->display);CHKERRQ(ierr);
281   ierr = PetscFree((*draw)->savefilename);CHKERRQ(ierr);
282   ierr = PetscHeaderDestroy(draw);CHKERRQ(ierr);
283   PetscFunctionReturn(0);
284 }
285 
286 #undef __FUNCT__
287 #define __FUNCT__ "PetscDrawGetPopup"
288 /*@
289    PetscDrawGetPopup - Creates a popup window associated with a PetscDraw window.
290 
291    Collective on PetscDraw
292 
293    Input Parameter:
294 .  draw - the original window
295 
296    Output Parameter:
297 .  popup - the new popup window
298 
299    Level: advanced
300 
301 @*/
302 PetscErrorCode  PetscDrawGetPopup(PetscDraw draw,PetscDraw *popup)
303 {
304   PetscErrorCode ierr;
305 
306   PetscFunctionBegin;
307   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
308   PetscValidPointer(popup,2);
309 
310   if (draw->popup) *popup = draw->popup;
311   else if (draw->ops->getpopup) {
312     ierr = (*draw->ops->getpopup)(draw,popup);CHKERRQ(ierr);
313   } else *popup = NULL;
314   PetscFunctionReturn(0);
315 }
316 
317 #undef __FUNCT__
318 #define __FUNCT__ "PetscDrawDestroy_Null"
319 PetscErrorCode PetscDrawDestroy_Null(PetscDraw draw)
320 {
321   PetscFunctionBegin;
322   PetscFunctionReturn(0);
323 }
324 
325 #undef __FUNCT__
326 #define __FUNCT__ "PetscDrawOpenNull"
327 /*
328   PetscDrawOpenNull - Opens a null drawing context. All draw commands to
329   it are ignored.
330 
331   Output Parameter:
332 . win - the drawing context
333 
334    Level: advanced
335 
336 */
337 PetscErrorCode  PetscDrawOpenNull(MPI_Comm comm,PetscDraw *win)
338 {
339   PetscErrorCode ierr;
340 
341   PetscFunctionBegin;
342   ierr = PetscDrawCreate(comm,NULL,NULL,0,0,1,1,win);CHKERRQ(ierr);
343   ierr = PetscDrawSetType(*win,PETSC_DRAW_NULL);CHKERRQ(ierr);
344   PetscFunctionReturn(0);
345 }
346 
347 #undef __FUNCT__
348 #define __FUNCT__ "PetscDrawSetDisplay"
349 /*@
350   PetscDrawSetDisplay - Sets the display where a PetscDraw object will be displayed
351 
352   Input Parameter:
353 + draw - the drawing context
354 - display - the X windows display
355 
356   Level: advanced
357 
358 @*/
359 PetscErrorCode  PetscDrawSetDisplay(PetscDraw draw,const char display[])
360 {
361   PetscErrorCode ierr;
362 
363   PetscFunctionBegin;
364   ierr = PetscFree(draw->display);CHKERRQ(ierr);
365   ierr = PetscStrallocpy(display,&draw->display);CHKERRQ(ierr);
366   PetscFunctionReturn(0);
367 }
368 
369 EXTERN_C_BEGIN
370 #undef __FUNCT__
371 #define __FUNCT__ "PetscDrawCreate_Null"
372 /*
373   PetscDrawCreate_Null - Opens a null drawing context. All draw commands to
374   it are ignored.
375 
376   Input Parameter:
377 . win - the drawing context
378 */
379 PetscErrorCode PetscDrawCreate_Null(PetscDraw draw)
380 {
381   PetscErrorCode ierr;
382 
383   PetscFunctionBegin;
384   ierr = PetscMemzero(draw->ops,sizeof(struct _PetscDrawOps));CHKERRQ(ierr);
385 
386   draw->ops->destroy = PetscDrawDestroy_Null;
387   draw->ops->view    = 0;
388   draw->pause        = 0.0;
389   draw->coor_xl      = 0.0;  draw->coor_xr = 1.0;
390   draw->coor_yl      = 0.0;  draw->coor_yr = 1.0;
391   draw->port_xl      = 0.0;  draw->port_xr = 1.0;
392   draw->port_yl      = 0.0;  draw->port_yr = 1.0;
393   draw->popup        = 0;
394   PetscFunctionReturn(0);
395 }
396 EXTERN_C_END
397 
398 #undef __FUNCT__
399 #define __FUNCT__ "PetscDrawGetSingleton"
400 /*@C
401    PetscDrawGetSingleton - Gain access to a PetscDraw object as if it were owned
402         by the one process.
403 
404    Collective on PetscDraw
405 
406    Input Parameter:
407 .  draw - the original window
408 
409    Output Parameter:
410 .  sdraw - the singleton window
411 
412    Level: advanced
413 
414 .seealso: PetscDrawRestoreSingleton(), PetscViewerGetSingleton(), PetscViewerRestoreSingleton()
415 
416 @*/
417 PetscErrorCode  PetscDrawGetSingleton(PetscDraw draw,PetscDraw *sdraw)
418 {
419   PetscErrorCode ierr;
420   PetscMPIInt    size;
421 
422   PetscFunctionBegin;
423   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
424   PetscValidPointer(sdraw,2);
425 
426   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)draw),&size);CHKERRQ(ierr);
427   if (size == 1) *sdraw = draw;
428   else {
429     if (draw->ops->getsingleton) {
430       ierr = (*draw->ops->getsingleton)(draw,sdraw);CHKERRQ(ierr);
431     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot get singleton for this type %s of draw object",((PetscObject)draw)->type_name);
432   }
433   PetscFunctionReturn(0);
434 }
435 
436 #undef __FUNCT__
437 #define __FUNCT__ "PetscDrawRestoreSingleton"
438 /*@C
439    PetscDrawRestoreSingleton - Remove access to a PetscDraw object as if it were owned
440         by the one process.
441 
442    Collective on PetscDraw
443 
444    Input Parameters:
445 +  draw - the original window
446 -  sdraw - the singleton window
447 
448    Level: advanced
449 
450 .seealso: PetscDrawGetSingleton(), PetscViewerGetSingleton(), PetscViewerRestoreSingleton()
451 
452 @*/
453 PetscErrorCode  PetscDrawRestoreSingleton(PetscDraw draw,PetscDraw *sdraw)
454 {
455   PetscErrorCode ierr;
456   PetscMPIInt    size;
457 
458   PetscFunctionBegin;
459   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
460   PetscValidPointer(sdraw,2);
461   PetscValidHeaderSpecific(*sdraw,PETSC_DRAW_CLASSID,2);
462 
463   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)draw),&size);CHKERRQ(ierr);
464   if (size != 1) {
465     if (draw->ops->restoresingleton) {
466       ierr = (*draw->ops->restoresingleton)(draw,sdraw);CHKERRQ(ierr);
467     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot restore singleton for this type %s of draw object",((PetscObject)draw)->type_name);
468   }
469   PetscFunctionReturn(0);
470 }
471 
472 
473 
474 
475 
476 
477 
478