Actual source code: draw.c

petsc-3.6.4 2016-04-12
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("Axis",&PETSC_DRAWAXIS_CLASSID);
 58:   PetscClassIdRegister("Line Graph",&PETSC_DRAWLG_CLASSID);
 59:   PetscClassIdRegister("Histogram",&PETSC_DRAWHG_CLASSID);
 60:   PetscClassIdRegister("Bar Grap",&PETSC_DRAWBAR_CLASSID);
 61:   PetscClassIdRegister("Scatter Plot",&PETSC_DRAWSP_CLASSID);
 62:   /* Register Constructors */
 63:   PetscDrawRegisterAll();
 64:   /* Process info exclusions */
 65:   PetscOptionsGetString(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, "-log_summary_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: {

104:   if (draw->ops->resizewindow) {
105:     (*draw->ops->resizewindow)(draw,w,h);
106:   }
107:   return(0);
108: }

112: /*@
113:    PetscDrawCheckResizedWindow - Checks if the user has resized the window.

115:    Collective on PetscDraw

117:    Input Parameter:
118: .  draw - the window

120:    Level: advanced

122: .seealso: PetscDrawResizeWindow()

124: @*/
125: PetscErrorCode  PetscDrawCheckResizedWindow(PetscDraw draw)
126: {

130:   if (draw->ops->checkresizedwindow) {
131:     (*draw->ops->checkresizedwindow)(draw);
132:   }
133:   return(0);
134: }

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

141:    Not collective

143:    Input Parameter:
144: .  draw - the graphics context

146:    Output Parameter:
147: .  title - the title

149:    Level: intermediate

151: .seealso: PetscDrawSetTitle()
152: @*/
153: PetscErrorCode  PetscDrawGetTitle(PetscDraw draw,char **title)
154: {
158:   *title = draw->title;
159:   return(0);
160: }

164: /*@C
165:    PetscDrawSetTitle - Sets the title of a PetscDraw context.

167:    Not collective (any processor or all may call this)

169:    Input Parameters:
170: +  draw - the graphics context
171: -  title - the title

173:    Level: intermediate

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

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

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

183: .seealso: PetscDrawGetTitle(), PetscDrawAppendTitle()
184: @*/
185: PetscErrorCode  PetscDrawSetTitle(PetscDraw draw,const char title[])
186: {

192:   PetscFree(draw->title);
193:   PetscStrallocpy(title,&draw->title);
194:   if (draw->ops->settitle) {
195:     (*draw->ops->settitle)(draw,title);
196:   }
197:   return(0);
198: }

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

205:    Not collective (any processor or all can call this)

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

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

215:    Level: advanced

217: .seealso: PetscDrawSetTitle(), PetscDrawGetTitle()
218: @*/
219: PetscErrorCode  PetscDrawAppendTitle(PetscDraw draw,const char title[])
220: {
222:   size_t         len1,len2,len;
223:   char           *newtitle;

227:   if (!title) return(0);

229:   if (draw->title) {
230:     PetscStrlen(title,&len1);
231:     PetscStrlen(draw->title,&len2);
232:     len  = len1 + len2;
233:     PetscMalloc1(len + 1,&newtitle);
234:     PetscStrcpy(newtitle,draw->title);
235:     PetscStrcat(newtitle,title);
236:     PetscFree(draw->title);

238:     draw->title = newtitle;
239:   } else {
240:     PetscStrallocpy(title,&draw->title);
241:   }
242:   if (draw->ops->settitle) {
243:     (*draw->ops->settitle)(draw,draw->title);
244:   }
245:   return(0);
246: }

250: /*@
251:    PetscDrawDestroy - Deletes a draw context.

253:    Collective on PetscDraw

255:    Input Parameters:
256: .  draw - the drawing context

258:    Level: beginner

260: .seealso: PetscDrawCreate()

262: @*/
263: PetscErrorCode  PetscDrawDestroy(PetscDraw *draw)
264: {

268:   if (!*draw) return(0);
270:   if (--((PetscObject)(*draw))->refct > 0) return(0);

272:   if ((*draw)->pause == -2) {
273:     (*draw)->pause = -1;

275:     PetscDrawPause(*draw);
276:   }

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

281:   if ((*draw)->ops->destroy) {
282:     (*(*draw)->ops->destroy)(*draw);
283:   }
284:   PetscFree((*draw)->title);
285:   PetscFree((*draw)->display);
286:   PetscFree((*draw)->savefilename);
287:   PetscFree((*draw)->savefilenameext);
288:   PetscFree((*draw)->savefinalfilename);
289:   PetscHeaderDestroy(draw);
290:   return(0);
291: }

295: /*@
296:    PetscDrawGetPopup - Creates a popup window associated with a PetscDraw window.

298:    Collective on PetscDraw

300:    Input Parameter:
301: .  draw - the original window

303:    Output Parameter:
304: .  popup - the new popup window

306:    Level: advanced

308: @*/
309: PetscErrorCode  PetscDrawGetPopup(PetscDraw draw,PetscDraw *popup)
310: {


317:   if (draw->popup) *popup = draw->popup;
318:   else if (draw->ops->getpopup) {
319:     (*draw->ops->getpopup)(draw,popup);
320:     if (*popup) {
321:       PetscObjectSetOptionsPrefix((PetscObject)*popup,"popup_");
322:       PetscDrawSetFromOptions(*popup);
323:     }
324:   } else *popup = NULL;
325:   return(0);
326: }

330: PetscErrorCode PetscDrawDestroy_Null(PetscDraw draw)
331: {
333:   return(0);
334: }

338: /*
339:   PetscDrawOpenNull - Opens a null drawing context. All draw commands to
340:   it are ignored.

342:   Output Parameter:
343: . win - the drawing context

345:    Level: advanced

347: */
348: PetscErrorCode  PetscDrawOpenNull(MPI_Comm comm,PetscDraw *win)
349: {

353:   PetscDrawCreate(comm,NULL,NULL,0,0,1,1,win);
354:   PetscDrawSetType(*win,PETSC_DRAW_NULL);
355:   return(0);
356: }

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

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

367:   Level: advanced

369: @*/
370: PetscErrorCode  PetscDrawSetDisplay(PetscDraw draw,const char display[])
371: {

375:   PetscFree(draw->display);
376:   PetscStrallocpy(display,&draw->display);
377:   return(0);
378: }

382: /*
383:   PetscDrawCreate_Null - Opens a null drawing context. All draw commands to
384:   it are ignored.

386:   Input Parameter:
387: . win - the drawing context
388: */
389: PETSC_EXTERN PetscErrorCode PetscDrawCreate_Null(PetscDraw draw)
390: {

394:   PetscMemzero(draw->ops,sizeof(struct _PetscDrawOps));

396:   draw->ops->destroy = PetscDrawDestroy_Null;
397:   draw->ops->view    = 0;
398:   draw->pause        = 0.0;
399:   draw->coor_xl      = 0.0;  draw->coor_xr = 1.0;
400:   draw->coor_yl      = 0.0;  draw->coor_yr = 1.0;
401:   draw->port_xl      = 0.0;  draw->port_xr = 1.0;
402:   draw->port_yl      = 0.0;  draw->port_yr = 1.0;
403:   draw->popup        = 0;
404:   return(0);
405: }

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

413:    Collective on PetscDraw

415:    Input Parameter:
416: .  draw - the original window

418:    Output Parameter:
419: .  sdraw - the singleton window

421:    Level: advanced

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

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


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

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

451:    Collective on PetscDraw

453:    Input Parameters:
454: +  draw - the original window
455: -  sdraw - the singleton window

457:    Level: advanced

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

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


472:   MPI_Comm_size(PetscObjectComm((PetscObject)draw),&size);
473:   if (size != 1) {
474:     if (draw->ops->restoresingleton) {
475:       (*draw->ops->restoresingleton)(draw,sdraw);
476:     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot restore singleton for this type %s of draw object",((PetscObject)draw)->type_name);
477:   }
478:   return(0);
479: }