Actual source code: draw.c


  2: /*
  3:        Provides the calling sequences for all the basic PetscDraw routines.
  4: */
  5: #include <petsc/private/drawimpl.h>
  6: #include <petscviewer.h>

  8: PetscClassId PETSC_DRAW_CLASSID;

 10: static PetscBool PetscDrawPackageInitialized = PETSC_FALSE;
 11: /*@C
 12:   PetscDrawFinalizePackage - This function destroys everything in the Petsc interface to the Draw package. It is
 13:   called from PetscFinalize().

 15:   Level: developer

 17: .seealso: `PetscFinalize()`
 18: @*/
 19: PetscErrorCode  PetscDrawFinalizePackage(void)
 20: {
 21:   PetscFunctionListDestroy(&PetscDrawList);
 22:   PetscDrawPackageInitialized = PETSC_FALSE;
 23:   PetscDrawRegisterAllCalled  = PETSC_FALSE;
 24:   return 0;
 25: }

 27: /*@C
 28:   PetscInitializeDrawPackage - This function initializes everything in the PetscDraw package. It is called
 29:   from PetscDLLibraryRegister_petsc() when using dynamic libraries, and on the call to PetscInitialize()
 30:   when using shared or static libraries.

 32:   Level: developer

 34: .seealso: `PetscInitialize()`
 35: @*/
 36: PetscErrorCode  PetscDrawInitializePackage(void)
 37: {
 38:   char           logList[256];
 39:   PetscBool      opt,pkg;

 41:   if (PetscDrawPackageInitialized) return 0;
 42:   PetscDrawPackageInitialized = PETSC_TRUE;
 43:   /* Register Classes */
 44:   PetscClassIdRegister("Draw",&PETSC_DRAW_CLASSID);
 45:   PetscClassIdRegister("Draw Axis",&PETSC_DRAWAXIS_CLASSID);
 46:   PetscClassIdRegister("Line Graph",&PETSC_DRAWLG_CLASSID);
 47:   PetscClassIdRegister("Histogram",&PETSC_DRAWHG_CLASSID);
 48:   PetscClassIdRegister("Bar Graph",&PETSC_DRAWBAR_CLASSID);
 49:   PetscClassIdRegister("Scatter Plot",&PETSC_DRAWSP_CLASSID);
 50:   /* Register Constructors */
 51:   PetscDrawRegisterAll();
 52:   /* Process Info */
 53:   {
 54:     PetscClassId  classids[6];

 56:     classids[0] = PETSC_DRAW_CLASSID;
 57:     classids[1] = PETSC_DRAWAXIS_CLASSID;
 58:     classids[2] = PETSC_DRAWLG_CLASSID;
 59:     classids[3] = PETSC_DRAWHG_CLASSID;
 60:     classids[4] = PETSC_DRAWBAR_CLASSID;
 61:     classids[5] = PETSC_DRAWSP_CLASSID;
 62:     PetscInfoProcessClass("draw", 6, classids);
 63:   }
 64:   /* Process summary exclusions */
 65:   PetscOptionsGetString(NULL,NULL,"-log_exclude",logList,sizeof(logList),&opt);
 66:   if (opt) {
 67:     PetscStrInList("draw",logList,',',&pkg);
 68:     if (pkg) {
 69:       PetscLogEventExcludeClass(PETSC_DRAW_CLASSID);
 70:       PetscLogEventExcludeClass(PETSC_DRAWAXIS_CLASSID);
 71:       PetscLogEventExcludeClass(PETSC_DRAWLG_CLASSID);
 72:       PetscLogEventExcludeClass(PETSC_DRAWHG_CLASSID);
 73:       PetscLogEventExcludeClass(PETSC_DRAWBAR_CLASSID);
 74:       PetscLogEventExcludeClass(PETSC_DRAWSP_CLASSID);
 75:     }
 76:   }
 77:   /* Register package finalizer */
 78:   PetscRegisterFinalize(PetscDrawFinalizePackage);
 79:   return 0;
 80: }

 82: /*@
 83:    PetscDrawResizeWindow - Allows one to resize a window from a program.

 85:    Collective on PetscDraw

 87:    Input Parameters:
 88: +  draw - the window
 89: -  w,h - the new width and height of the window

 91:    Level: intermediate

 93: .seealso: `PetscDrawCheckResizedWindow()`
 94: @*/
 95: PetscErrorCode  PetscDrawResizeWindow(PetscDraw draw,int w,int h)
 96: {
100:   if (draw->ops->resizewindow) (*draw->ops->resizewindow)(draw,w,h);
101:   return 0;
102: }

104: /*@
105:    PetscDrawGetWindowSize - Gets the size of the window.

107:    Not collective

109:    Input Parameter:
110: .  draw - the window

112:    Output Parameters:
113: .  w,h - the window width and height

115:    Level: intermediate

117: .seealso: `PetscDrawResizeWindow()`, `PetscDrawCheckResizedWindow()`
118: @*/
119: PetscErrorCode  PetscDrawGetWindowSize(PetscDraw draw,int *w,int *h)
120: {
124:   if (w) *w = draw->w;
125:   if (h) *h = draw->h;
126:   return 0;
127: }

129: /*@
130:    PetscDrawCheckResizedWindow - Checks if the user has resized the window.

132:    Collective on PetscDraw

134:    Input Parameter:
135: .  draw - the window

137:    Level: advanced

139: .seealso: `PetscDrawResizeWindow()`

141: @*/
142: PetscErrorCode  PetscDrawCheckResizedWindow(PetscDraw draw)
143: {
145:   if (draw->ops->checkresizedwindow) (*draw->ops->checkresizedwindow)(draw);
146:   return 0;
147: }

149: /*@C
150:    PetscDrawGetTitle - Gets pointer to title of a PetscDraw context.

152:    Not collective

154:    Input Parameter:
155: .  draw - the graphics context

157:    Output Parameter:
158: .  title - the title

160:    Level: intermediate

162: .seealso: `PetscDrawSetTitle()`
163: @*/
164: PetscErrorCode  PetscDrawGetTitle(PetscDraw draw,const char *title[])
165: {
168:   *title = draw->title;
169:   return 0;
170: }

172: /*@C
173:    PetscDrawSetTitle - Sets the title of a PetscDraw context.

175:    Collective on PetscDraw

177:    Input Parameters:
178: +  draw - the graphics context
179: -  title - the title

181:    Level: intermediate

183:    Note: The title is positioned in the windowing system title bar for the window. Hence it will not be saved with -draw_save
184:    in the image.

186:    A copy of the string is made, so you may destroy the
187:    title string after calling this routine.

189:    You can use PetscDrawAxisSetLabels() to indicate a title within the window

191: .seealso: `PetscDrawGetTitle()`, `PetscDrawAppendTitle()`
192: @*/
193: PetscErrorCode  PetscDrawSetTitle(PetscDraw draw,const char title[])
194: {
197:   PetscFree(draw->title);
198:   PetscStrallocpy(title,&draw->title);
199:   if (draw->ops->settitle) (*draw->ops->settitle)(draw,draw->title);
200:   return 0;
201: }

203: /*@C
204:    PetscDrawAppendTitle - Appends to the title of a PetscDraw context.

206:    Collective on PetscDraw

208:    Input Parameters:
209: +  draw - the graphics context
210: -  title - the title

212:    Note:
213:    A copy of the string is made, so you may destroy the
214:    title string after calling this routine.

216:    Level: advanced

218: .seealso: `PetscDrawSetTitle()`, `PetscDrawGetTitle()`
219: @*/
220: PetscErrorCode  PetscDrawAppendTitle(PetscDraw draw,const char title[])
221: {
224:   if (!title || !title[0]) return 0;

226:   if (draw->title) {
227:     size_t len1,len2;
228:     char   *newtitle;
229:     PetscStrlen(title,&len1);
230:     PetscStrlen(draw->title,&len2);
231:     PetscMalloc1(len1 + len2 + 1,&newtitle);
232:     PetscStrcpy(newtitle,draw->title);
233:     PetscStrcat(newtitle,title);
234:     PetscFree(draw->title);
235:     draw->title = newtitle;
236:   } else {
237:     PetscStrallocpy(title,&draw->title);
238:   }
239:   if (draw->ops->settitle) (*draw->ops->settitle)(draw,draw->title);
240:   return 0;
241: }

243: static PetscErrorCode PetscDrawDestroy_Private(PetscDraw draw)
244: {
245:   if (!draw->ops->save && !draw->ops->getimage) return 0;
246:   PetscDrawSaveMovie(draw);
247:   if (draw->savefinalfilename) {
248:     draw->savesinglefile = PETSC_TRUE;
249:     PetscDrawSetSave(draw,draw->savefinalfilename);
250:     PetscDrawSave(draw);
251:   }
252:   PetscBarrier((PetscObject)draw);
253:   return 0;
254: }

256: /*@
257:    PetscDrawDestroy - Deletes a draw context.

259:    Collective on PetscDraw

261:    Input Parameters:
262: .  draw - the drawing context

264:    Level: beginner

266: .seealso: `PetscDrawCreate()`

268: @*/
269: PetscErrorCode  PetscDrawDestroy(PetscDraw *draw)
270: {
271:   if (!*draw) return 0;
273:   if (--((PetscObject)(*draw))->refct > 0) return 0;

275:   if ((*draw)->pause == -2) {
276:     (*draw)->pause = -1;
277:     PetscDrawPause(*draw);
278:   }

280:   /* if memory was published then destroy it */
281:   PetscObjectSAWsViewOff((PetscObject)*draw);

283:   PetscDrawDestroy_Private(*draw);

285:   if ((*draw)->ops->destroy) {
286:     (*(*draw)->ops->destroy)(*draw);
287:   }
288:   PetscDrawDestroy(&(*draw)->popup);
289:   PetscFree((*draw)->title);
290:   PetscFree((*draw)->display);
291:   PetscFree((*draw)->savefilename);
292:   PetscFree((*draw)->saveimageext);
293:   PetscFree((*draw)->savemovieext);
294:   PetscFree((*draw)->savefinalfilename);
295:   PetscHeaderDestroy(draw);
296:   return 0;
297: }

299: /*@
300:    PetscDrawGetPopup - Creates a popup window associated with a PetscDraw window.

302:    Collective on PetscDraw

304:    Input Parameter:
305: .  draw - the original window

307:    Output Parameter:
308: .  popup - the new popup window

310:    Level: advanced

312: .seealso: `PetscDrawScalePopup()`, `PetscDrawCreate()`

314: @*/
315: PetscErrorCode  PetscDrawGetPopup(PetscDraw draw,PetscDraw *popup)
316: {

320:   if (draw->popup) *popup = draw->popup;
321:   else if (draw->ops->getpopup) {
322:     (*draw->ops->getpopup)(draw,popup);
323:     if (*popup) {
324:       PetscObjectSetOptionsPrefix((PetscObject)*popup,"popup_");
325:       (*popup)->pause = 0.0;
326:       PetscDrawSetFromOptions(*popup);
327:     }
328:   } else *popup = NULL;
329:   return 0;
330: }

332: /*@C
333:   PetscDrawSetDisplay - Sets the display where a PetscDraw object will be displayed

335:   Input Parameters:
336: + draw - the drawing context
337: - display - the X windows display

339:   Level: advanced

341: .seealso: `PetscDrawCreate()`

343: @*/
344: PetscErrorCode  PetscDrawSetDisplay(PetscDraw draw,const char display[])
345: {
346:   PetscFree(draw->display);
347:   PetscStrallocpy(display,&draw->display);
348:   return 0;
349: }

351: /*@
352:    PetscDrawSetDoubleBuffer - Sets a window to be double buffered.

354:    Logically Collective on PetscDraw

356:    Input Parameter:
357: .  draw - the drawing context

359:    Level: intermediate

361: @*/
362: PetscErrorCode  PetscDrawSetDoubleBuffer(PetscDraw draw)
363: {
365:   if (draw->ops->setdoublebuffer) (*draw->ops->setdoublebuffer)(draw);
366:   return 0;
367: }

369: /*@C
370:    PetscDrawGetSingleton - Gain access to a PetscDraw object as if it were owned
371:         by the one process.

373:    Collective on PetscDraw

375:    Input Parameter:
376: .  draw - the original window

378:    Output Parameter:
379: .  sdraw - the singleton window

381:    Level: advanced

383: .seealso: `PetscDrawRestoreSingleton()`, `PetscViewerGetSingleton()`, `PetscViewerRestoreSingleton()`

385: @*/
386: PetscErrorCode  PetscDrawGetSingleton(PetscDraw draw,PetscDraw *sdraw)
387: {
388:   PetscMPIInt    size;


393:   MPI_Comm_size(PetscObjectComm((PetscObject)draw),&size);
394:   if (size == 1) {
395:     PetscObjectReference((PetscObject)draw);
396:     *sdraw = draw;
397:   } else {
398:     if (draw->ops->getsingleton) {
399:       (*draw->ops->getsingleton)(draw,sdraw);
400:     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot get singleton for this type %s of draw object",((PetscObject)draw)->type_name);
401:   }
402:   return 0;
403: }

405: /*@C
406:    PetscDrawRestoreSingleton - Remove access to a PetscDraw object as if it were owned
407:         by the one process.

409:    Collective on PetscDraw

411:    Input Parameters:
412: +  draw - the original window
413: -  sdraw - the singleton window

415:    Level: advanced

417: .seealso: `PetscDrawGetSingleton()`, `PetscViewerGetSingleton()`, `PetscViewerRestoreSingleton()`

419: @*/
420: PetscErrorCode  PetscDrawRestoreSingleton(PetscDraw draw,PetscDraw *sdraw)
421: {
422:   PetscMPIInt    size;


428:   MPI_Comm_size(PetscObjectComm((PetscObject)draw),&size);
429:   if (size == 1) {
430:     if (draw == *sdraw) {
431:       PetscObjectDereference((PetscObject)draw);
432:       *sdraw = NULL;
433:     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot restore singleton, it is not the parent draw");
434:   } else {
435:     if (draw->ops->restoresingleton) {
436:       (*draw->ops->restoresingleton)(draw,sdraw);
437:     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot restore singleton for this type %s of draw object",((PetscObject)draw)->type_name);
438:   }
439:   return 0;
440: }