Actual source code: draw.c

petsc-3.8.4 2018-03-24
Report Typos and Errors

  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: .keywords: Petsc, destroy, package, mathematica
 18: .seealso: PetscFinalize()
 19: @*/
 20: PetscErrorCode  PetscDrawFinalizePackage(void)
 21: {

 25:   PetscFunctionListDestroy(&PetscDrawList);
 26:   PetscDrawPackageInitialized = PETSC_FALSE;
 27:   PetscDrawRegisterAllCalled  = PETSC_FALSE;
 28:   return(0);
 29: }

 31: /*@C
 32:   PetscInitializeDrawPackage - This function initializes everything in the PetscDraw package. It is called
 33:   from PetscDLLibraryRegister() when using dynamic libraries, and on the call to PetscInitialize()
 34:   when using static libraries.

 36:   Level: developer

 38: .keywords: Petsc, initialize, package
 39: .seealso: PetscInitialize()
 40: @*/
 41: PetscErrorCode  PetscDrawInitializePackage(void)
 42: {
 43:   char           logList[256];
 44:   char           *className;
 45:   PetscBool      opt;

 49:   if (PetscDrawPackageInitialized) return(0);
 50:   PetscDrawPackageInitialized = PETSC_TRUE;
 51:   /* Register Classes */
 52:   PetscClassIdRegister("Draw",&PETSC_DRAW_CLASSID);
 53:   PetscClassIdRegister("Draw Axis",&PETSC_DRAWAXIS_CLASSID);
 54:   PetscClassIdRegister("Line Graph",&PETSC_DRAWLG_CLASSID);
 55:   PetscClassIdRegister("Histogram",&PETSC_DRAWHG_CLASSID);
 56:   PetscClassIdRegister("Bar Graph",&PETSC_DRAWBAR_CLASSID);
 57:   PetscClassIdRegister("Scatter Plot",&PETSC_DRAWSP_CLASSID);
 58:   /* Register Constructors */
 59:   PetscDrawRegisterAll();
 60:   /* Process info exclusions */
 61:   PetscOptionsGetString(NULL,NULL, "-info_exclude", logList, 256, &opt);
 62:   if (opt) {
 63:     PetscStrstr(logList, "draw", &className);
 64:     if (className) {
 65:       PetscInfoDeactivateClass(0);
 66:     }
 67:   }
 68:   /* Process summary exclusions */
 69:   PetscOptionsGetString(NULL,NULL, "-log_exclude", logList, 256, &opt);
 70:   if (opt) {
 71:     PetscStrstr(logList, "draw", &className);
 72:     if (className) {
 73:       PetscLogEventDeactivateClass(0);
 74:     }
 75:   }
 76:   PetscRegisterFinalize(PetscDrawFinalizePackage);
 77:   return(0);
 78: }

 80: /*@
 81:    PetscDrawResizeWindow - Allows one to resize a window from a program.

 83:    Collective on PetscDraw

 85:    Input Parameter:
 86: +  draw - the window
 87: -  w,h - the new width and height of the window

 89:    Level: intermediate

 91: .seealso: PetscDrawCheckResizedWindow()
 92: @*/
 93: PetscErrorCode  PetscDrawResizeWindow(PetscDraw draw,int w,int h)
 94: {

101:   if (draw->ops->resizewindow) {
102:     (*draw->ops->resizewindow)(draw,w,h);
103:   }
104:   return(0);
105: }

107: /*@
108:    PetscDrawGetWindowSize - Gets the size of the window.

110:    Not collective

112:    Input Parameter:
113: .  draw - the window

115:    Output Parameters:
116: .  w,h - the window width and height

118:    Level: intermediate

120: .seealso: PetscDrawResizeWindow(), PetscDrawCheckResizedWindow()
121: @*/
122: PetscErrorCode  PetscDrawGetWindowSize(PetscDraw draw,int *w,int *h)
123: {
128:   if (w) *w = draw->w;
129:   if (h) *h = draw->h;
130:   return(0);
131: }

133: /*@
134:    PetscDrawCheckResizedWindow - Checks if the user has resized the window.

136:    Collective on PetscDraw

138:    Input Parameter:
139: .  draw - the window

141:    Level: advanced

143: .seealso: PetscDrawResizeWindow()

145: @*/
146: PetscErrorCode  PetscDrawCheckResizedWindow(PetscDraw draw)
147: {

152:   if (draw->ops->checkresizedwindow) {
153:     (*draw->ops->checkresizedwindow)(draw);
154:   }
155:   return(0);
156: }

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

161:    Not collective

163:    Input Parameter:
164: .  draw - the graphics context

166:    Output Parameter:
167: .  title - the title

169:    Level: intermediate

171: .seealso: PetscDrawSetTitle()
172: @*/
173: PetscErrorCode  PetscDrawGetTitle(PetscDraw draw,char **title)
174: {
178:   *title = draw->title;
179:   return(0);
180: }

182: /*@C
183:    PetscDrawSetTitle - Sets the title of a PetscDraw context.

185:    Collective on PetscDraw

187:    Input Parameters:
188: +  draw - the graphics context
189: -  title - the title

191:    Level: intermediate

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

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

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

201: .seealso: PetscDrawGetTitle(), PetscDrawAppendTitle()
202: @*/
203: PetscErrorCode  PetscDrawSetTitle(PetscDraw draw,const char title[])
204: {

210:   PetscFree(draw->title);
211:   PetscStrallocpy(title,&draw->title);
212:   if (draw->ops->settitle) {
213:     (*draw->ops->settitle)(draw,draw->title);
214:   }
215:   return(0);
216: }

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

221:    Collective on PetscDraw

223:    Input Parameters:
224: +  draw - the graphics context
225: -  title - the title

227:    Note:
228:    A copy of the string is made, so you may destroy the
229:    title string after calling this routine.

231:    Level: advanced

233: .seealso: PetscDrawSetTitle(), PetscDrawGetTitle()
234: @*/
235: PetscErrorCode  PetscDrawAppendTitle(PetscDraw draw,const char title[])
236: {

242:   if (!title || !title[0]) return(0);

244:   if (draw->title) {
245:     size_t len1,len2;
246:     char   *newtitle;
247:     PetscStrlen(title,&len1);
248:     PetscStrlen(draw->title,&len2);
249:     PetscMalloc1(len1 + len2 + 1,&newtitle);
250:     PetscStrcpy(newtitle,draw->title);
251:     PetscStrcat(newtitle,title);
252:     PetscFree(draw->title);
253:     draw->title = newtitle;
254:   } else {
255:     PetscStrallocpy(title,&draw->title);
256:   }
257:   if (draw->ops->settitle) {
258:     (*draw->ops->settitle)(draw,draw->title);
259:   }
260:   return(0);
261: }

263: static PetscErrorCode PetscDrawDestroy_Private(PetscDraw draw)
264: {

268:   if (!draw->ops->save && !draw->ops->getimage) return(0);
269:   PetscDrawSaveMovie(draw);
270:   if (draw->savefinalfilename) {
271:     draw->savesinglefile = PETSC_TRUE;
272:     PetscDrawSetSave(draw,draw->savefinalfilename);
273:     PetscDrawSave(draw);
274:   }
275:   PetscBarrier((PetscObject)draw);
276:   return(0);
277: }

279: /*@
280:    PetscDrawDestroy - Deletes a draw context.

282:    Collective on PetscDraw

284:    Input Parameters:
285: .  draw - the drawing context

287:    Level: beginner

289: .seealso: PetscDrawCreate()

291: @*/
292: PetscErrorCode  PetscDrawDestroy(PetscDraw *draw)
293: {

297:   if (!*draw) return(0);
299:   if (--((PetscObject)(*draw))->refct > 0) return(0);

301:   if ((*draw)->pause == -2) {
302:     (*draw)->pause = -1;
303:     PetscDrawPause(*draw);
304:   }

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

309:   PetscDrawDestroy_Private(*draw);

311:   if ((*draw)->ops->destroy) {
312:     (*(*draw)->ops->destroy)(*draw);
313:   }
314:   PetscDrawDestroy(&(*draw)->popup);
315:   PetscFree((*draw)->title);
316:   PetscFree((*draw)->display);
317:   PetscFree((*draw)->savefilename);
318:   PetscFree((*draw)->saveimageext);
319:   PetscFree((*draw)->savemovieext);
320:   PetscFree((*draw)->savefinalfilename);
321:   PetscHeaderDestroy(draw);
322:   return(0);
323: }

325: /*@
326:    PetscDrawGetPopup - Creates a popup window associated with a PetscDraw window.

328:    Collective on PetscDraw

330:    Input Parameter:
331: .  draw - the original window

333:    Output Parameter:
334: .  popup - the new popup window

336:    Level: advanced

338: .seealso: PetscDrawScalePopup(), PetscDrawCreate()

340: @*/
341: PetscErrorCode  PetscDrawGetPopup(PetscDraw draw,PetscDraw *popup)
342: {


349:   if (draw->popup) *popup = draw->popup;
350:   else if (draw->ops->getpopup) {
351:     (*draw->ops->getpopup)(draw,popup);
352:     if (*popup) {
353:       PetscObjectSetOptionsPrefix((PetscObject)*popup,"popup_");
354:       (*popup)->pause = 0.0;
355:       PetscDrawSetFromOptions(*popup);
356:     }
357:   } else *popup = NULL;
358:   return(0);
359: }

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

364:   Input Parameter:
365: + draw - the drawing context
366: - display - the X windows display

368:   Level: advanced

370: .seealso: PetscDrawCreate()

372: @*/
373: PetscErrorCode  PetscDrawSetDisplay(PetscDraw draw,const char display[])
374: {

378:   PetscFree(draw->display);
379:   PetscStrallocpy(display,&draw->display);
380:   return(0);
381: }


384: /*@
385:    PetscDrawSetDoubleBuffer - Sets a window to be double buffered.

387:    Logically Collective on PetscDraw

389:    Input Parameter:
390: .  draw - the drawing context

392:    Level: intermediate

394:    Concepts: drawing^double buffer
395:    Concepts: graphics^double buffer
396:    Concepts: double buffer

398: @*/
399: PetscErrorCode  PetscDrawSetDoubleBuffer(PetscDraw draw)
400: {

405:   if (draw->ops->setdoublebuffer) {
406:     (*draw->ops->setdoublebuffer)(draw);
407:   }
408:   return(0);
409: }

411: /*@C
412:    PetscDrawGetSingleton - Gain access to a PetscDraw object as if it were owned
413:         by the one process.

415:    Collective on PetscDraw

417:    Input Parameter:
418: .  draw - the original window

420:    Output Parameter:
421: .  sdraw - the singleton window

423:    Level: advanced

425: .seealso: PetscDrawRestoreSingleton(), PetscViewerGetSingleton(), PetscViewerRestoreSingleton()

427: @*/
428: PetscErrorCode  PetscDrawGetSingleton(PetscDraw draw,PetscDraw *sdraw)
429: {
431:   PetscMPIInt    size;


437:   MPI_Comm_size(PetscObjectComm((PetscObject)draw),&size);
438:   if (size == 1) {
439:     PetscObjectReference((PetscObject)draw);
440:     *sdraw = draw;
441:   } else {
442:     if (draw->ops->getsingleton) {
443:       (*draw->ops->getsingleton)(draw,sdraw);
444:     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot get singleton for this type %s of draw object",((PetscObject)draw)->type_name);
445:   }
446:   return(0);
447: }

449: /*@C
450:    PetscDrawRestoreSingleton - Remove access to a PetscDraw object as if it were owned
451:         by the one process.

453:    Collective on PetscDraw

455:    Input Parameters:
456: +  draw - the original window
457: -  sdraw - the singleton window

459:    Level: advanced

461: .seealso: PetscDrawGetSingleton(), PetscViewerGetSingleton(), PetscViewerRestoreSingleton()

463: @*/
464: PetscErrorCode  PetscDrawRestoreSingleton(PetscDraw draw,PetscDraw *sdraw)
465: {
467:   PetscMPIInt    size;


474:   MPI_Comm_size(PetscObjectComm((PetscObject)draw),&size);
475:   if (size == 1) {
476:     if (draw == *sdraw) {
477:       PetscObjectDereference((PetscObject)draw);
478:       *sdraw = NULL;
479:     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot restore singleton, it is not the parent draw");
480:   } else {
481:     if (draw->ops->restoresingleton) {
482:       (*draw->ops->restoresingleton)(draw,sdraw);
483:     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot restore singleton for this type %s of draw object",((PetscObject)draw)->type_name);
484:   }
485:   return(0);
486: }