Actual source code: draw.c

petsc-3.4.5 2014-06-29
  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:   return(0);
 30: }

 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.

 39:   Level: developer

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

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

 84: /*@
 85:    PetscDrawResizeWindow - Allows one to resize a window from a program.

 87:    Collective on PetscDraw

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

 93:    Level: intermediate

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

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

110: /*@
111:    PetscDrawCheckResizedWindow - Checks if the user has resized the window.

113:    Collective on PetscDraw

115:    Input Parameter:
116: .  draw - the window

118:    Level: advanced

120: .seealso: PetscDrawResizeWindow()

122: @*/
123: PetscErrorCode  PetscDrawCheckResizedWindow(PetscDraw draw)
124: {

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

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

139:    Not collective

141:    Input Parameter:
142: .  draw - the graphics context

144:    Output Parameter:
145: .  title - the title

147:    Level: intermediate

149: .seealso: PetscDrawSetTitle()
150: @*/
151: PetscErrorCode  PetscDrawGetTitle(PetscDraw draw,char **title)
152: {
156:   *title = draw->title;
157:   return(0);
158: }

162: /*@C
163:    PetscDrawSetTitle - Sets the title of a PetscDraw context.

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

167:    Input Parameters:
168: +  draw - the graphics context
169: -  title - the title

171:    Level: intermediate

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.

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

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

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

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

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

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

205:    Input Parameters:
206: +  draw - the graphics context
207: -  title - the title

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

213:    Level: advanced

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

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

227:   if (draw->title) {
228:     PetscStrlen(title,&len1);
229:     PetscStrlen(draw->title,&len2);
230:     len  = len1 + len2;
231:     PetscMalloc((len + 1)*sizeof(char*),&newtitle);
232:     PetscStrcpy(newtitle,draw->title);
233:     PetscStrcat(newtitle,title);
234:     PetscFree(draw->title);

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

248: /*@
249:    PetscDrawDestroy - Deletes a draw context.

251:    Collective on PetscDraw

253:    Input Parameters:
254: .  draw - the drawing context

256:    Level: beginner

258: .seealso: PetscDrawCreate()

260: @*/
261: PetscErrorCode  PetscDrawDestroy(PetscDraw *draw)
262: {

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

270:   if ((*draw)->pause == -2) {
271:     (*draw)->pause = -1;

273:     PetscDrawPause(*draw);
274:   }

276:   /* if memory was published then destroy it */
277:   PetscObjectAMSViewOff((PetscObject)*draw);

279:   if ((*draw)->ops->destroy) {
280:     (*(*draw)->ops->destroy)(*draw);
281:   }
282:   PetscFree((*draw)->title);
283:   PetscFree((*draw)->display);
284:   PetscFree((*draw)->savefilename);
285:   PetscHeaderDestroy(draw);
286:   return(0);
287: }

291: /*@
292:    PetscDrawGetPopup - Creates a popup window associated with a PetscDraw window.

294:    Collective on PetscDraw

296:    Input Parameter:
297: .  draw - the original window

299:    Output Parameter:
300: .  popup - the new popup window

302:    Level: advanced

304: @*/
305: PetscErrorCode  PetscDrawGetPopup(PetscDraw draw,PetscDraw *popup)
306: {


313:   if (draw->popup) *popup = draw->popup;
314:   else if (draw->ops->getpopup) {
315:     (*draw->ops->getpopup)(draw,popup);
316:   } else *popup = NULL;
317:   return(0);
318: }

322: PetscErrorCode PetscDrawDestroy_Null(PetscDraw draw)
323: {
325:   return(0);
326: }

330: /*
331:   PetscDrawOpenNull - Opens a null drawing context. All draw commands to
332:   it are ignored.

334:   Output Parameter:
335: . win - the drawing context

337:    Level: advanced

339: */
340: PetscErrorCode  PetscDrawOpenNull(MPI_Comm comm,PetscDraw *win)
341: {

345:   PetscDrawCreate(comm,NULL,NULL,0,0,1,1,win);
346:   PetscDrawSetType(*win,PETSC_DRAW_NULL);
347:   return(0);
348: }

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

355:   Input Parameter:
356: + draw - the drawing context
357: - display - the X windows display

359:   Level: advanced

361: @*/
362: PetscErrorCode  PetscDrawSetDisplay(PetscDraw draw,const char display[])
363: {

367:   PetscFree(draw->display);
368:   PetscStrallocpy(display,&draw->display);
369:   return(0);
370: }

374: /*
375:   PetscDrawCreate_Null - Opens a null drawing context. All draw commands to
376:   it are ignored.

378:   Input Parameter:
379: . win - the drawing context
380: */
381: PETSC_EXTERN PetscErrorCode PetscDrawCreate_Null(PetscDraw draw)
382: {

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

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:   return(0);
397: }

401: /*@C
402:    PetscDrawGetSingleton - Gain access to a PetscDraw object as if it were owned
403:         by the one process.

405:    Collective on PetscDraw

407:    Input Parameter:
408: .  draw - the original window

410:    Output Parameter:
411: .  sdraw - the singleton window

413:    Level: advanced

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

417: @*/
418: PetscErrorCode  PetscDrawGetSingleton(PetscDraw draw,PetscDraw *sdraw)
419: {
421:   PetscMPIInt    size;


427:   MPI_Comm_size(PetscObjectComm((PetscObject)draw),&size);
428:   if (size == 1) *sdraw = draw;
429:   else {
430:     if (draw->ops->getsingleton) {
431:       (*draw->ops->getsingleton)(draw,sdraw);
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:   return(0);
435: }

439: /*@C
440:    PetscDrawRestoreSingleton - Remove access to a PetscDraw object as if it were owned
441:         by the one process.

443:    Collective on PetscDraw

445:    Input Parameters:
446: +  draw - the original window
447: -  sdraw - the singleton window

449:    Level: advanced

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

453: @*/
454: PetscErrorCode  PetscDrawRestoreSingleton(PetscDraw draw,PetscDraw *sdraw)
455: {
457:   PetscMPIInt    size;


464:   MPI_Comm_size(PetscObjectComm((PetscObject)draw),&size);
465:   if (size != 1) {
466:     if (draw->ops->restoresingleton) {
467:       (*draw->ops->restoresingleton)(draw,sdraw);
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:   return(0);
471: }