Actual source code: draw.c

petsc-3.7.3 2016-08-01
Report Typos and Errors
  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>

  8: PetscClassId PETSC_DRAW_CLASSID;

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

 17:   Level: developer

 19: .keywords: Petsc, destroy, package, mathematica
 20: .seealso: PetscFinalize()
 21: @*/
 22: PetscErrorCode  PetscDrawFinalizePackage(void)
 23: {

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

 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.

 40:   Level: developer

 42: .keywords: Petsc, initialize, package
 43: .seealso: PetscInitialize()
 44: @*/
 45: PetscErrorCode  PetscDrawInitializePackage(void)
 46: {
 47:   char           logList[256];
 48:   char           *className;
 49:   PetscBool      opt;

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

 86: /*@
 87:    PetscDrawResizeWindow - Allows one to resize a window from a program.

 89:    Collective on PetscDraw

 91:    Input Parameter:
 92: +  draw - the window
 93: -  w,h - the new width and height of the window

 95:    Level: intermediate

 97: .seealso: PetscDrawCheckResizedWindow()
 98: @*/
 99: PetscErrorCode  PetscDrawResizeWindow(PetscDraw draw,int w,int h)
100: {

107:   if (draw->ops->resizewindow) {
108:     (*draw->ops->resizewindow)(draw,w,h);
109:   }
110:   return(0);
111: }

115: /*@
116:    PetscDrawGetWindowSize - Gets the size of the window.

118:    Not collective

120:    Input Parameter:
121: .  draw - the window

123:    Output Parameters:
124: .  w,h - the window width and height

126:    Level: intermediate

128: .seealso: PetscDrawResizeWindow(), PetscDrawCheckResizedWindow()
129: @*/
130: PetscErrorCode  PetscDrawGetWindowSize(PetscDraw draw,int *w,int *h)
131: {
136:   if (w) *w = draw->w;
137:   if (h) *h = draw->h;
138:   return(0);
139: }

143: /*@
144:    PetscDrawCheckResizedWindow - Checks if the user has resized the window.

146:    Collective on PetscDraw

148:    Input Parameter:
149: .  draw - the window

151:    Level: advanced

153: .seealso: PetscDrawResizeWindow()

155: @*/
156: PetscErrorCode  PetscDrawCheckResizedWindow(PetscDraw draw)
157: {

162:   if (draw->ops->checkresizedwindow) {
163:     (*draw->ops->checkresizedwindow)(draw);
164:   }
165:   return(0);
166: }

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

173:    Not collective

175:    Input Parameter:
176: .  draw - the graphics context

178:    Output Parameter:
179: .  title - the title

181:    Level: intermediate

183: .seealso: PetscDrawSetTitle()
184: @*/
185: PetscErrorCode  PetscDrawGetTitle(PetscDraw draw,char **title)
186: {
190:   *title = draw->title;
191:   return(0);
192: }

196: /*@C
197:    PetscDrawSetTitle - Sets the title of a PetscDraw context.

199:    Collective on PetscDraw

201:    Input Parameters:
202: +  draw - the graphics context
203: -  title - the title

205:    Level: intermediate

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

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

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

215: .seealso: PetscDrawGetTitle(), PetscDrawAppendTitle()
216: @*/
217: PetscErrorCode  PetscDrawSetTitle(PetscDraw draw,const char title[])
218: {

224:   PetscFree(draw->title);
225:   PetscStrallocpy(title,&draw->title);
226:   if (draw->ops->settitle) {
227:     (*draw->ops->settitle)(draw,draw->title);
228:   }
229:   return(0);
230: }

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

237:    Collective on PetscDraw

239:    Input Parameters:
240: +  draw - the graphics context
241: -  title - the title

243:    Note:
244:    A copy of the string is made, so you may destroy the
245:    title string after calling this routine.

247:    Level: advanced

249: .seealso: PetscDrawSetTitle(), PetscDrawGetTitle()
250: @*/
251: PetscErrorCode  PetscDrawAppendTitle(PetscDraw draw,const char title[])
252: {

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

260:   if (draw->title) {
261:     size_t len1,len2;
262:     char   *newtitle;
263:     PetscStrlen(title,&len1);
264:     PetscStrlen(draw->title,&len2);
265:     PetscMalloc1(len1 + len2 + 1,&newtitle);
266:     PetscStrcpy(newtitle,draw->title);
267:     PetscStrcat(newtitle,title);
268:     PetscFree(draw->title);
269:     draw->title = newtitle;
270:   } else {
271:     PetscStrallocpy(title,&draw->title);
272:   }
273:   if (draw->ops->settitle) {
274:     (*draw->ops->settitle)(draw,draw->title);
275:   }
276:   return(0);
277: }

281: static PetscErrorCode PetscDrawDestroy_Private(PetscDraw draw)
282: {

286:   if (!draw->ops->save && !draw->ops->getimage) return(0);
287:   PetscDrawSaveMovie(draw);
288:   if (draw->savefinalfilename) {
289:     draw->savesinglefile = PETSC_TRUE;
290:     PetscDrawSetSave(draw,draw->savefinalfilename);
291:     PetscDrawSave(draw);
292:   }
293:   PetscBarrier((PetscObject)draw);
294:   return(0);
295: }

299: /*@
300:    PetscDrawDestroy - Deletes a draw context.

302:    Collective on PetscDraw

304:    Input Parameters:
305: .  draw - the drawing context

307:    Level: beginner

309: .seealso: PetscDrawCreate()

311: @*/
312: PetscErrorCode  PetscDrawDestroy(PetscDraw *draw)
313: {

317:   if (!*draw) return(0);
319:   if (--((PetscObject)(*draw))->refct > 0) return(0);

321:   if ((*draw)->pause == -2) {
322:     (*draw)->pause = -1;
323:     PetscDrawPause(*draw);
324:   }

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

329:   PetscDrawDestroy_Private(*draw);

331:   if ((*draw)->ops->destroy) {
332:     (*(*draw)->ops->destroy)(*draw);
333:   }
334:   PetscDrawDestroy(&(*draw)->popup);
335:   PetscFree((*draw)->title);
336:   PetscFree((*draw)->display);
337:   PetscFree((*draw)->savefilename);
338:   PetscFree((*draw)->saveimageext);
339:   PetscFree((*draw)->savemovieext);
340:   PetscFree((*draw)->savefinalfilename);
341:   PetscHeaderDestroy(draw);
342:   return(0);
343: }

347: /*@
348:    PetscDrawGetPopup - Creates a popup window associated with a PetscDraw window.

350:    Collective on PetscDraw

352:    Input Parameter:
353: .  draw - the original window

355:    Output Parameter:
356: .  popup - the new popup window

358:    Level: advanced

360: @*/
361: PetscErrorCode  PetscDrawGetPopup(PetscDraw draw,PetscDraw *popup)
362: {


369:   if (draw->popup) *popup = draw->popup;
370:   else if (draw->ops->getpopup) {
371:     (*draw->ops->getpopup)(draw,popup);
372:     if (*popup) {
373:       PetscObjectSetOptionsPrefix((PetscObject)*popup,"popup_");
374:       (*popup)->pause = 0.0;
375:       PetscDrawSetFromOptions(*popup);
376:     }
377:   } else *popup = NULL;
378:   return(0);
379: }

383: /*@
384:   PetscDrawSetDisplay - Sets the display where a PetscDraw object will be displayed

386:   Input Parameter:
387: + draw - the drawing context
388: - display - the X windows display

390:   Level: advanced

392: @*/
393: PetscErrorCode  PetscDrawSetDisplay(PetscDraw draw,const char display[])
394: {

398:   PetscFree(draw->display);
399:   PetscStrallocpy(display,&draw->display);
400:   return(0);
401: }


406: /*@
407:    PetscDrawSetDoubleBuffer - Sets a window to be double buffered.

409:    Logically Collective on PetscDraw

411:    Input Parameter:
412: .  draw - the drawing context

414:    Level: intermediate

416:    Concepts: drawing^double buffer
417:    Concepts: graphics^double buffer
418:    Concepts: double buffer

420: @*/
421: PetscErrorCode  PetscDrawSetDoubleBuffer(PetscDraw draw)
422: {

427:   if (draw->ops->setdoublebuffer) {
428:     (*draw->ops->setdoublebuffer)(draw);
429:   }
430:   return(0);
431: }

435: /*@C
436:    PetscDrawGetSingleton - Gain access to a PetscDraw object as if it were owned
437:         by the one process.

439:    Collective on PetscDraw

441:    Input Parameter:
442: .  draw - the original window

444:    Output Parameter:
445: .  sdraw - the singleton window

447:    Level: advanced

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

451: @*/
452: PetscErrorCode  PetscDrawGetSingleton(PetscDraw draw,PetscDraw *sdraw)
453: {
455:   PetscMPIInt    size;


461:   MPI_Comm_size(PetscObjectComm((PetscObject)draw),&size);
462:   if (size == 1) {
463:     PetscObjectReference((PetscObject)draw);
464:     *sdraw = draw;
465:   } else {
466:     if (draw->ops->getsingleton) {
467:       (*draw->ops->getsingleton)(draw,sdraw);
468:     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot get singleton for this type %s of draw object",((PetscObject)draw)->type_name);
469:   }
470:   return(0);
471: }

475: /*@C
476:    PetscDrawRestoreSingleton - Remove access to a PetscDraw object as if it were owned
477:         by the one process.

479:    Collective on PetscDraw

481:    Input Parameters:
482: +  draw - the original window
483: -  sdraw - the singleton window

485:    Level: advanced

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

489: @*/
490: PetscErrorCode  PetscDrawRestoreSingleton(PetscDraw draw,PetscDraw *sdraw)
491: {
493:   PetscMPIInt    size;


500:   MPI_Comm_size(PetscObjectComm((PetscObject)draw),&size);
501:   if (size == 1) {
502:     if (draw == *sdraw) {
503:       PetscObjectDereference((PetscObject)draw);
504:       *sdraw = NULL;
505:     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot restore singleton, it is not the parent draw");
506:   } else {
507:     if (draw->ops->restoresingleton) {
508:       (*draw->ops->restoresingleton)(draw,sdraw);
509:     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot restore singleton for this type %s of draw object",((PetscObject)draw)->type_name);
510:   }
511:   return(0);
512: }