Actual source code: drawreg.c

  1: /*$Id: drawreg.c,v 1.45 2001/06/21 21:15:18 bsmith Exp $*/
  2: /*
  3:        Provides the registration process for PETSc PetscDraw routines
  4: */
 5:  #include src/sys/src/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: int PetscDrawCreate(MPI_Comm comm,const char display[],const char title[],int x,int y,int w,int h,PetscDraw *indraw)
 38: {
 39:   PetscDraw draw;
 40:   int  ierr;

 43:   *indraw = 0;
 44:   PetscHeaderCreate(draw,_p_PetscDraw,struct _PetscDrawOps,PETSC_DRAW_COOKIE,-1,"Draw",comm,PetscDrawDestroy,0);
 45:   PetscLogObjectCreate(draw);
 46:   draw->type    = -1;
 47:   draw->data    = 0;
 48:   PetscStrallocpy(title,&draw->title);
 49:   PetscStrallocpy(display,&draw->display);
 50:   draw->x       = x;
 51:   draw->y       = y;
 52:   draw->w       = w;
 53:   draw->h       = h;
 54:   draw->pause   = 0;
 55:   draw->coor_xl = 0.0;
 56:   draw->coor_xr = 1.0;
 57:   draw->coor_yl = 0.0;
 58:   draw->coor_yr = 1.0;
 59:   draw->port_xl = 0.0;
 60:   draw->port_xr = 1.0;
 61:   draw->port_yl = 0.0;
 62:   draw->port_yr = 1.0;
 63:   draw->popup   = 0;
 64:   PetscOptionsGetInt(PETSC_NULL,"-draw_pause",&draw->pause,PETSC_NULL);
 65:   *indraw       = draw;
 66:   return(0);
 67: }
 68: 
 71: /*@C
 72:    PetscDrawSetType - Builds graphics object for a particular implementation 

 74:    Collective on PetscDraw

 76:    Input Parameter:
 77: +  draw      - the graphics context
 78: -  type      - for example, PETSC_DRAW_X

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

 84:    Level: intermediate

 86:    Notes:  
 87:    See "petsc/include/petscdraw.h" for available methods (for instance,
 88:    PETSC_DRAW_X)

 90:    Concepts: drawing^X windows
 91:    Concepts: X windows^graphics
 92:    Concepts: drawing^postscript
 93:    Concepts: postscript^graphics
 94:    Concepts: drawing^Microsoft Windows

 96: .seealso: PetscDrawSetFromOptions(), PetscDrawCreate(), PetscDrawDestroy()
 97: @*/
 98: int PetscDrawSetType(PetscDraw draw,PetscDrawType type)
 99: {
100:   int        ierr,(*r)(PetscDraw);
101:   PetscTruth match;
102:   PetscTruth flg=PETSC_FALSE;


108:   PetscTypeCompare((PetscObject)draw,type,&match);
109:   if (match) return(0);

111: #if defined(PETSC_HAVE_X11)
112:   /*  User requests no graphics */
113:   PetscOptionsHasName(PETSC_NULL,"-nox",&flg);
114: #endif

116:   /*
117:      This is not ideal, but it allows codes to continue to run if X graphics 
118:    was requested but is not installed on this machine. Mostly this is for
119:    testing.
120:    */
121: #if !defined(PETSC_HAVE_X11)
122:   {
123:     PetscStrcmp(type,PETSC_DRAW_X,&match);
124:     if (match) flg = PETSC_TRUE;
125:   }
126: #endif
127:   if (flg) {
128:     type = PETSC_DRAW_NULL;
129:   }

131:   if (draw->data) {
132:     /* destroy the old private PetscDraw context */
133:     (*draw->ops->destroy)(draw);
134:     draw->data = 0;
135:   }

137:   /* Get the function pointers for the graphics method requested */
138:   if (!PetscDrawList) SETERRQ(1,"No draw implementations ierr");

140:    PetscFListFind(draw->comm,PetscDrawList,type,(void (**)(void)) &r);

142:   if (!r) SETERRQ1(1,"Unknown PetscDraw type given: %s",type);

144:   PetscObjectChangeTypeName((PetscObject)draw,type);

146:   draw->data        = 0;
147:   (*r)(draw);

149:   return(0);
150: }

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

158:    Not Collective

160:    Level: developer

162: .seealso: PetscDrawRegisterDynamic(), PetscDrawRegisterAll()
163: @*/
164: int PetscDrawRegisterDestroy(void)
165: {

169:   if (PetscDrawList) {
170:     PetscFListDestroy(&PetscDrawList);
171:     PetscDrawList = 0;
172:   }
173:   return(0);
174: }

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

181:    Not Collective

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

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

189:    Level: advanced

191: @*/
192: int PetscDrawGetType(PetscDraw draw,PetscDrawType *type)
193: {
195:   *type = draw->type_name;
196:   return(0);
197: }

201: int PetscDrawRegister(char *sname,char *path,char *name,int (*function)(PetscDraw))
202: {
203:   int  ierr;
204:   char fullname[PETSC_MAX_PATH_LEN];

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

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

218:    Collective on PetscDraw

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

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

228:    Level: intermediate

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

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

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

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


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