Actual source code: drawreg.c

  1: #define PETSC_DLL
  2: /*
  3:        Provides the registration process for PETSc PetscDraw routines
  4: */
 5:  #include ../src/sys/draw/drawimpl.h

  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:   PetscInt       dpause;
 42:   PetscTruth     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_COOKIE,-1,"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;
 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:   PetscOptionsGetInt(PETSC_NULL,"-draw_pause",&dpause,&flag);
 68:   if (flag) draw->pause = (int) 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:   PetscTruth      match;
106:   PetscTruth      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:       PetscTruth 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,(void (**)(void)) &r);
147:   if (!r) SETERRQ1(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:   PetscTruth flg,nox;
244:   char       vtype[256];
245:   const char *def;
246: #if !defined(PETSC_HAVE_WINDOWS_H) && !defined(PETSC_HAVE_X11)
247:   PetscTruth warn;
248: #endif


253:   if (!PetscDrawList) {
254:     PetscDrawRegisterAll(PETSC_NULL);
255:   }

257:   if (((PetscObject)draw)->type_name) {
258:     def = ((PetscObject)draw)->type_name;
259:   } else {
260:     PetscOptionsHasName(PETSC_NULL,"-nox",&nox);
261:     def  = PETSC_DRAW_NULL;
262: #if defined(PETSC_HAVE_WINDOWS_H) && !defined(PETSC_HAVE_X11)
263:     if (!nox) def = PETSC_DRAW_WIN32;
264: #elif defined(PETSC_HAVE_X11)
265:     if (!nox) def = PETSC_DRAW_X;
266: #else
267:     PetscOptionsHasName(PETSC_NULL,"-nox_warning",&warn);
268:     if (!nox && !warn) {
269:       (*PetscErrorPrintf)("PETSc installed without X windows on this machine\nproceeding without graphics\n");
270:     }
271: #endif
272:   }
273:   PetscOptionsBegin(((PetscObject)draw)->comm,((PetscObject)draw)->prefix,"Graphics (PetscDraw) Options","Draw");
274:     PetscOptionsList("-draw_type","Type of graphical output","PetscDrawSetType",PetscDrawList,def,vtype,256,&flg);
275:     if (flg) {
276:       PetscDrawSetType(draw,vtype);
277:     } else if (!((PetscObject)draw)->type_name) {
278:       PetscDrawSetType(draw,def);
279:     }
280:     PetscOptionsName("-nox","Run without graphics","None",&nox);
281:   PetscOptionsEnd();
282:   return(0);
283: }