Actual source code: drawreg.c

  2: /*
  3:        Provides the registration process for PETSc PetscDraw routines
  4: */
  5: #include <../src/sys/draw/drawimpl.h>  /*I "petscdraw.h" I*/

  7: /*
  8:    Contains the list of registered PetscDraw routines
  9: */
 10: PetscFList PetscDrawList              = 0;

 14: /*@C
 15:    PetscDrawCreate - Creates a graphics context.

 17:    Collective on MPI_Comm

 19:    Input Parameter:
 20: +  comm - MPI communicator
 21: .  display - X display when using X windows
 22: .  title - optional title added to top of window
 23: .  x,y - coordinates of lower left corner of window or PETSC_DECIDE
 24: -  w, h - width and height of window or PETSC_DECIDE or PETSC_DRAW_HALF_SIZE, PETSC_DRAW_FULL_SIZE,
 25:           or PETSC_DRAW_THIRD_SIZE or PETSC_DRAW_QUARTER_SIZE

 27:    Output Parameter:
 28: .  draw - location to put the PetscDraw context

 30:    Level: beginner

 32:    Concepts: graphics^creating context
 33:    Concepts: drawing^creating context

 35: .seealso: PetscDrawSetFromOptions(), PetscDrawDestroy(), PetscDrawSetType()
 36: @*/
 37: PetscErrorCode  PetscDrawCreate(MPI_Comm comm,const char display[],const char title[],int x,int y,int w,int h,PetscDraw *indraw)
 38: {
 39:   PetscDraw      draw;
 41:   PetscReal      dpause;
 42:   PetscBool      flag;

 45: #ifndef PETSC_USE_DYNAMIC_LIBRARIES
 46:   PetscDrawInitializePackage(PETSC_NULL);
 47: #endif
 48:   *indraw = 0;
 49:   PetscHeaderCreate(draw,_p_PetscDraw,struct _PetscDrawOps,PETSC_DRAW_CLASSID,-1,"Draw","Graphics","Draw",comm,PetscDrawDestroy,0);
 50:   draw->data    = 0;
 51:   PetscStrallocpy(title,&draw->title);
 52:   PetscStrallocpy(display,&draw->display);
 53:   draw->x       = x;
 54:   draw->y       = y;
 55:   draw->w       = w;
 56:   draw->h       = h;
 57:   draw->pause   = 0.0;
 58:   draw->coor_xl = 0.0;
 59:   draw->coor_xr = 1.0;
 60:   draw->coor_yl = 0.0;
 61:   draw->coor_yr = 1.0;
 62:   draw->port_xl = 0.0;
 63:   draw->port_xr = 1.0;
 64:   draw->port_yl = 0.0;
 65:   draw->port_yr = 1.0;
 66:   draw->popup   = 0;
 67:   PetscOptionsGetReal(PETSC_NULL,"-draw_pause",&dpause,&flag);
 68:   if (flag) draw->pause = dpause;
 69:   *indraw       = draw;
 70:   return(0);
 71: }
 72: 
 75: /*@C
 76:    PetscDrawSetType - Builds graphics object for a particular implementation 

 78:    Collective on PetscDraw

 80:    Input Parameter:
 81: +  draw      - the graphics context
 82: -  type      - for example, PETSC_DRAW_X

 84:    Options Database Command:
 85: .  -draw_type  <type> - Sets the type; use -help for a list 
 86:     of available methods (for instance, x)

 88:    Level: intermediate

 90:    Notes:  
 91:    See "petsc/include/petscdraw.h" for available methods (for instance,
 92:    PETSC_DRAW_X)

 94:    Concepts: drawing^X windows
 95:    Concepts: X windows^graphics
 96:    Concepts: drawing^postscript
 97:    Concepts: postscript^graphics
 98:    Concepts: drawing^Microsoft Windows

100: .seealso: PetscDrawSetFromOptions(), PetscDrawCreate(), PetscDrawDestroy()
101: @*/
102: PetscErrorCode  PetscDrawSetType(PetscDraw draw,const PetscDrawType type)
103: {
104:   PetscErrorCode ierr,(*r)(PetscDraw);
105:   PetscBool       match;
106:   PetscBool       flg=PETSC_FALSE;


112:   PetscTypeCompare((PetscObject)draw,type,&match);
113:   if (match) return(0);

115:   /*  User requests no graphics */
116:   PetscOptionsHasName(PETSC_NULL,"-nox",&flg);

118:   /*
119:      This is not ideal, but it allows codes to continue to run if X graphics 
120:    was requested but is not installed on this machine. Mostly this is for
121:    testing.
122:    */
123: #if !defined(PETSC_HAVE_X11)
124:   if (!flg) {
125:     PetscStrcmp(type,PETSC_DRAW_X,&match);
126:     if (match) {
127:       PetscBool  dontwarn = PETSC_TRUE;
128:       flg = PETSC_TRUE;
129:       PetscOptionsHasName(PETSC_NULL,"-nox_warning",&dontwarn);
130:       if (!dontwarn) {
131:         (*PetscErrorPrintf)("PETSc installed without X windows on this machine\nproceeding without graphics\n");
132:       }
133:     }
134:   }
135: #endif
136:   if (flg) {
137:     type  = PETSC_DRAW_NULL;
138:   }

140:   if (draw->data) {
141:     /* destroy the old private PetscDraw context */
142:     (*draw->ops->destroy)(draw);
143:     draw->data = 0;
144:   }

146:    PetscFListFind(PetscDrawList,((PetscObject)draw)->comm,type,PETSC_TRUE,(void (**)(void)) &r);
147:   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown PetscDraw type given: %s",type);
148:   PetscObjectChangeTypeName((PetscObject)draw,type);
149:   draw->data        = 0;
150:   (*r)(draw);
151:   return(0);
152: }

156: /*@C
157:    PetscDrawRegisterDestroy - Frees the list of PetscDraw methods that were
158:    registered by PetscDrawRegisterDynamic().

160:    Not Collective

162:    Level: developer

164: .seealso: PetscDrawRegisterDynamic(), PetscDrawRegisterAll()
165: @*/
166: PetscErrorCode  PetscDrawRegisterDestroy(void)
167: {

171:   PetscFListDestroy(&PetscDrawList);
172:   return(0);
173: }

177: /*@C
178:    PetscDrawGetType - Gets the PetscDraw type as a string from the PetscDraw object.

180:    Not Collective

182:    Input Parameter:
183: .  draw - Krylov context 

185:    Output Parameters:
186: .  name - name of PetscDraw method 

188:    Level: advanced

190: @*/
191: PetscErrorCode  PetscDrawGetType(PetscDraw draw,const PetscDrawType *type)
192: {
196:   *type = ((PetscObject)draw)->type_name;
197:   return(0);
198: }

202: PetscErrorCode  PetscDrawRegister(const char *sname,const char *path,const char *name,PetscErrorCode (*function)(PetscDraw))
203: {
205:   char fullname[PETSC_MAX_PATH_LEN];

208:   PetscFListConcat(path,name,fullname);
209:   PetscFListAdd(&PetscDrawList,sname,fullname,(void (*)(void))function);
210:   return(0);
211: }

215: /*@C
216:    PetscDrawSetFromOptions - Sets the graphics type from the options database.
217:       Defaults to a PETSc X windows graphics.

219:    Collective on PetscDraw

221:    Input Parameter:
222: .     draw - the graphics context

224:    Options Database Keys:
225: +   -nox - do not use X graphics (ignore graphics calls, but run program correctly)
226: -   -nox_warning - when X windows support is not installed this prevents the warning message
227:                    from being printed

229:    Level: intermediate

231:    Notes: 
232:     Must be called after PetscDrawCreate() before the PetscDrawtor is used.

234:     Concepts: drawing^setting options
235:     Concepts: graphics^setting options

237: .seealso: PetscDrawCreate(), PetscDrawSetType()

239: @*/
240: PetscErrorCode  PetscDrawSetFromOptions(PetscDraw draw)
241: {
243:   PetscBool      flg,nox;
244:   char           vtype[256];
245:   const char     *def;
246:   PetscBool      save;
247: #if !defined(PETSC_USE_WINDOWS_GRAPHICS) && !defined(PETSC_HAVE_X11)
248:   PetscBool      warn;
249: #endif
250:   char           filename[PETSC_MAX_PATH_LEN];


255:   if (!PetscDrawList) {
256:     PetscDrawRegisterAll(PETSC_NULL);
257:   }

259:   if (((PetscObject)draw)->type_name) {
260:     def = ((PetscObject)draw)->type_name;
261:   } else {
262:     PetscOptionsHasName(PETSC_NULL,"-nox",&nox);
263:     def  = PETSC_DRAW_NULL;
264: #if defined(PETSC_USE_WINDOWS_GRAPHICS) && !defined(PETSC_HAVE_X11)
265:     if (!nox) def = PETSC_DRAW_WIN32;
266: #elif defined(PETSC_HAVE_X11)
267:     if (!nox) def = PETSC_DRAW_X;
268: #else
269:     PetscOptionsHasName(PETSC_NULL,"-nox_warning",&warn);
270:     if (!nox && !warn) {
271:       (*PetscErrorPrintf)("PETSc installed without X windows on this machine\nproceeding without graphics\n");
272:     }
273: #endif
274:   }
275:   PetscObjectOptionsBegin((PetscObject)draw);
276:     PetscOptionsList("-draw_type","Type of graphical output","PetscDrawSetType",PetscDrawList,def,vtype,256,&flg);
277:     if (flg) {
278:       PetscDrawSetType(draw,vtype);
279:     } else if (!((PetscObject)draw)->type_name) {
280:       PetscDrawSetType(draw,def);
281:     }
282:     PetscOptionsName("-nox","Run without graphics","None",&nox);
283:     PetscOptionsString("-draw_save","Save graphics to file","PetscDrawSetSave",filename,filename,PETSC_MAX_PATH_LEN,&save);
284:     if (save) {
285:       PetscDrawSetSave(draw,filename);
286:     }

288:     /* process any options handlers added with PetscObjectAddOptionsHandler() */
289:     PetscObjectProcessOptionsHandlers((PetscObject)draw);
290:   PetscOptionsEnd();
291:   return(0);
292: }

296: /*@C
297:    PetscDrawSave - Saves images produced in a PetscDraw into a file as JPGEG

299:    Collective on PetscDraw

301:    Input Parameter:
302: +  draw      - the graphics context
303: -  filename  - name of the file, if PETSC_NULL uses name of draw object

305:    Options Database Command:
306: .  -draw_save  <filename>

308:    Level: intermediate

310:    Concepts: X windows^graphics
311:    Concepts: drawing^postscript
312:    Concepts: postscript^graphics
313:    Concepts: drawing^Microsoft Windows

315: .seealso: PetscDrawSetFromOptions(), PetscDrawCreate(), PetscDrawDestroy()
316: @*/
317: PetscErrorCode  PetscDrawSetSave(PetscDraw draw,const char *filename)
318: {

323:   PetscFree(draw->savefilename);
324:   draw->savefilecount = 0;
325:   if (filename && filename[0]) {
326:     PetscStrallocpy(filename,&draw->savefilename);
327:   } else {
328:     const char *name;
329:     PetscObjectGetName((PetscObject)draw,&name);
330:     PetscStrallocpy(name,&draw->savefilename);
331:   }
332:   if (draw->ops->setsave) {
333:     (*draw->ops->setsave)(draw,filename);
334:   }
335:   return(0);
336: }