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