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,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,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,PetscDrawType *type)
192: {
194:   *type = draw->type_name;
195:   return(0);
196: }

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

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

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

217:    Collective on PetscDraw

219:    Input Parameter:
220: .     draw - the graphics context

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

227:    Level: intermediate

229:    Notes: 
230:     Must be called after PetscDrawCreate() before the PetscDrawtor is used.

232:     Concepts: drawing^setting options
233:     Concepts: graphics^setting options

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

237: @*/
238: PetscErrorCode  PetscDrawSetFromOptions(PetscDraw draw)
239: {
241:   PetscTruth flg,nox;
242:   char       vtype[256];
243:   const char *def;
244: #if !defined(PETSC_HAVE_WINDOWS_H) && !defined(PETSC_HAVE_X11)
245:   PetscTruth warn;
246: #endif


251:   if (!PetscDrawList) {
252:     PetscDrawRegisterAll(PETSC_NULL);
253:   }

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