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