xref: /petsc/src/sys/classes/draw/interface/draw.c (revision a197972ac2354be954b7aae623d40f6de50a3e72)
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)->savefinalfilename);CHKERRQ(ierr);
287   ierr = PetscHeaderDestroy(draw);CHKERRQ(ierr);
288   PetscFunctionReturn(0);
289 }
290 
291 #undef __FUNCT__
292 #define __FUNCT__ "PetscDrawGetPopup"
293 /*@
294    PetscDrawGetPopup - Creates a popup window associated with a PetscDraw window.
295 
296    Collective on PetscDraw
297 
298    Input Parameter:
299 .  draw - the original window
300 
301    Output Parameter:
302 .  popup - the new popup window
303 
304    Level: advanced
305 
306 @*/
307 PetscErrorCode  PetscDrawGetPopup(PetscDraw draw,PetscDraw *popup)
308 {
309   PetscErrorCode ierr;
310 
311   PetscFunctionBegin;
312   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
313   PetscValidPointer(popup,2);
314 
315   if (draw->popup) *popup = draw->popup;
316   else if (draw->ops->getpopup) {
317     ierr = (*draw->ops->getpopup)(draw,popup);CHKERRQ(ierr);
318     if (*popup) {
319       ierr = PetscObjectSetOptionsPrefix((PetscObject)*popup,"popup_");CHKERRQ(ierr);
320       ierr = PetscDrawSetFromOptions(*popup);CHKERRQ(ierr);
321     }
322   } else *popup = NULL;
323   PetscFunctionReturn(0);
324 }
325 
326 #undef __FUNCT__
327 #define __FUNCT__ "PetscDrawDestroy_Null"
328 PetscErrorCode PetscDrawDestroy_Null(PetscDraw draw)
329 {
330   PetscFunctionBegin;
331   PetscFunctionReturn(0);
332 }
333 
334 #undef __FUNCT__
335 #define __FUNCT__ "PetscDrawOpenNull"
336 /*
337   PetscDrawOpenNull - Opens a null drawing context. All draw commands to
338   it are ignored.
339 
340   Output Parameter:
341 . win - the drawing context
342 
343    Level: advanced
344 
345 */
346 PetscErrorCode  PetscDrawOpenNull(MPI_Comm comm,PetscDraw *win)
347 {
348   PetscErrorCode ierr;
349 
350   PetscFunctionBegin;
351   ierr = PetscDrawCreate(comm,NULL,NULL,0,0,1,1,win);CHKERRQ(ierr);
352   ierr = PetscDrawSetType(*win,PETSC_DRAW_NULL);CHKERRQ(ierr);
353   PetscFunctionReturn(0);
354 }
355 
356 #undef __FUNCT__
357 #define __FUNCT__ "PetscDrawSetDisplay"
358 /*@
359   PetscDrawSetDisplay - Sets the display where a PetscDraw object will be displayed
360 
361   Input Parameter:
362 + draw - the drawing context
363 - display - the X windows display
364 
365   Level: advanced
366 
367 @*/
368 PetscErrorCode  PetscDrawSetDisplay(PetscDraw draw,const char display[])
369 {
370   PetscErrorCode ierr;
371 
372   PetscFunctionBegin;
373   ierr = PetscFree(draw->display);CHKERRQ(ierr);
374   ierr = PetscStrallocpy(display,&draw->display);CHKERRQ(ierr);
375   PetscFunctionReturn(0);
376 }
377 
378 #undef __FUNCT__
379 #define __FUNCT__ "PetscDrawCreate_Null"
380 /*
381   PetscDrawCreate_Null - Opens a null drawing context. All draw commands to
382   it are ignored.
383 
384   Input Parameter:
385 . win - the drawing context
386 */
387 PETSC_EXTERN PetscErrorCode PetscDrawCreate_Null(PetscDraw draw)
388 {
389   PetscErrorCode ierr;
390 
391   PetscFunctionBegin;
392   ierr = PetscMemzero(draw->ops,sizeof(struct _PetscDrawOps));CHKERRQ(ierr);
393 
394   draw->ops->destroy = PetscDrawDestroy_Null;
395   draw->ops->view    = 0;
396   draw->pause        = 0.0;
397   draw->coor_xl      = 0.0;  draw->coor_xr = 1.0;
398   draw->coor_yl      = 0.0;  draw->coor_yr = 1.0;
399   draw->port_xl      = 0.0;  draw->port_xr = 1.0;
400   draw->port_yl      = 0.0;  draw->port_yr = 1.0;
401   draw->popup        = 0;
402   PetscFunctionReturn(0);
403 }
404 
405 #undef __FUNCT__
406 #define __FUNCT__ "PetscDrawGetSingleton"
407 /*@C
408    PetscDrawGetSingleton - Gain access to a PetscDraw object as if it were owned
409         by the one process.
410 
411    Collective on PetscDraw
412 
413    Input Parameter:
414 .  draw - the original window
415 
416    Output Parameter:
417 .  sdraw - the singleton window
418 
419    Level: advanced
420 
421 .seealso: PetscDrawRestoreSingleton(), PetscViewerGetSingleton(), PetscViewerRestoreSingleton()
422 
423 @*/
424 PetscErrorCode  PetscDrawGetSingleton(PetscDraw draw,PetscDraw *sdraw)
425 {
426   PetscErrorCode ierr;
427   PetscMPIInt    size;
428 
429   PetscFunctionBegin;
430   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
431   PetscValidPointer(sdraw,2);
432 
433   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)draw),&size);CHKERRQ(ierr);
434   if (size == 1) *sdraw = draw;
435   else {
436     if (draw->ops->getsingleton) {
437       ierr = (*draw->ops->getsingleton)(draw,sdraw);CHKERRQ(ierr);
438     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot get singleton for this type %s of draw object",((PetscObject)draw)->type_name);
439   }
440   PetscFunctionReturn(0);
441 }
442 
443 #undef __FUNCT__
444 #define __FUNCT__ "PetscDrawRestoreSingleton"
445 /*@C
446    PetscDrawRestoreSingleton - Remove access to a PetscDraw object as if it were owned
447         by the one process.
448 
449    Collective on PetscDraw
450 
451    Input Parameters:
452 +  draw - the original window
453 -  sdraw - the singleton window
454 
455    Level: advanced
456 
457 .seealso: PetscDrawGetSingleton(), PetscViewerGetSingleton(), PetscViewerRestoreSingleton()
458 
459 @*/
460 PetscErrorCode  PetscDrawRestoreSingleton(PetscDraw draw,PetscDraw *sdraw)
461 {
462   PetscErrorCode ierr;
463   PetscMPIInt    size;
464 
465   PetscFunctionBegin;
466   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
467   PetscValidPointer(sdraw,2);
468   PetscValidHeaderSpecific(*sdraw,PETSC_DRAW_CLASSID,2);
469 
470   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)draw),&size);CHKERRQ(ierr);
471   if (size != 1) {
472     if (draw->ops->restoresingleton) {
473       ierr = (*draw->ops->restoresingleton)(draw,sdraw);CHKERRQ(ierr);
474     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot restore singleton for this type %s of draw object",((PetscObject)draw)->type_name);
475   }
476   PetscFunctionReturn(0);
477 }
478 
479 
480 
481 
482 
483 
484 
485