Actual source code: drawreg.c

petsc-3.3-p7 2013-05-11
  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:   draw->savefilename = PETSC_NULL;
 70:   draw->savefilemovie = PETSC_FALSE;
 71:   *indraw       = draw;
 72:   return(0);
 73: }
 74: 
 77: /*@C
 78:    PetscDrawSetType - Builds graphics object for a particular implementation 

 80:    Collective on PetscDraw

 82:    Input Parameter:
 83: +  draw      - the graphics context
 84: -  type      - for example, PETSC_DRAW_X

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

 90:    Level: intermediate

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

 96:    Concepts: drawing^X windows
 97:    Concepts: X windows^graphics
 98:    Concepts: drawing^postscript
 99:    Concepts: postscript^graphics
100:    Concepts: drawing^Microsoft Windows

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


114:   PetscObjectTypeCompare((PetscObject)draw,type,&match);
115:   if (match) return(0);

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

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

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

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

159: /*@C
160:    PetscDrawRegisterDestroy - Frees the list of PetscDraw methods that were
161:    registered by PetscDrawRegisterDynamic().

163:    Not Collective

165:    Level: developer

167: .seealso: PetscDrawRegisterDynamic(), PetscDrawRegisterAll()
168: @*/
169: PetscErrorCode  PetscDrawRegisterDestroy(void)
170: {

174:   PetscFListDestroy(&PetscDrawList);
175:   return(0);
176: }

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

183:    Not Collective

185:    Input Parameter:
186: .  draw - Krylov context 

188:    Output Parameters:
189: .  name - name of PetscDraw method 

191:    Level: advanced

193: @*/
194: PetscErrorCode  PetscDrawGetType(PetscDraw draw,const PetscDrawType *type)
195: {
199:   *type = ((PetscObject)draw)->type_name;
200:   return(0);
201: }

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

211:   PetscFListConcat(path,name,fullname);
212:   PetscFListAdd(&PetscDrawList,sname,fullname,(void (*)(void))function);
213:   return(0);
214: }

218: /*@C
219:    PetscDrawSetFromOptions - Sets the graphics type from the options database.
220:       Defaults to a PETSc X windows graphics.

222:    Collective on PetscDraw

224:    Input Parameter:
225: .     draw - the graphics context

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

232:    Level: intermediate

234:    Notes: 
235:     Must be called after PetscDrawCreate() before the PetscDrawtor is used.

237:     Concepts: drawing^setting options
238:     Concepts: graphics^setting options

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

242: @*/
243: PetscErrorCode  PetscDrawSetFromOptions(PetscDraw draw)
244: {
246:   PetscBool      flg,nox;
247:   char           vtype[256];
248:   const char     *def;
249:   PetscBool      save;
250: #if !defined(PETSC_USE_WINDOWS_GRAPHICS) && !defined(PETSC_HAVE_X)
251:   PetscBool      warn;
252: #endif
253:   char           filename[PETSC_MAX_PATH_LEN];
254:   PetscBool      movie = PETSC_FALSE;


259:   if (!PetscDrawList) {
260:     PetscDrawRegisterAll(PETSC_NULL);
261:   }

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

293:     /* process any options handlers added with PetscObjectAddOptionsHandler() */
294:     PetscObjectProcessOptionsHandlers((PetscObject)draw);
295:   PetscOptionsEnd();
296:   return(0);
297: }

301: /*@C
302:    PetscDrawSave - Saves images produced in a PetscDraw into a file as a Gif file using AfterImage

304:    Collective on PetscDraw

306:    Input Parameter:
307: +  draw      - the graphics context
308: .  filename  - name of the file, if PETSC_NULL uses name of draw object
309: -  movie - produce a movie of all the images 

311:    Options Database Command:
312: +  -draw_save  <filename>
313: -  -draw_save_movie

315:    Level: intermediate

317:    Concepts: X windows^graphics
318:    Concepts: drawing^postscript
319:    Concepts: postscript^graphics
320:    Concepts: drawing^Microsoft Windows

322:    Notes: Requires that PETSc be configured with the option --with-afterimage


325: .seealso: PetscDrawSetFromOptions(), PetscDrawCreate(), PetscDrawDestroy(), PetscDrawSave()
326: @*/
327: PetscErrorCode  PetscDrawSetSave(PetscDraw draw,const char *filename,PetscBool movie)
328: {

333:   PetscFree(draw->savefilename);
334:   draw->savefilecount = 0;
335:   draw->savefilemovie = movie;
336:   if (filename && filename[0]) {
337:     PetscStrallocpy(filename,&draw->savefilename);
338:   } else {
339:     const char *name;
340:     PetscObjectGetName((PetscObject)draw,&name);
341:     PetscStrallocpy(name,&draw->savefilename);
342:   }
343:   if (draw->ops->setsave) {
344:     (*draw->ops->setsave)(draw,filename);
345:   }
346:   return(0);
347: }