Actual source code: xops.c

  1: /* $Id: xops.c,v 1.155 2001/04/10 19:34:14 bsmith Exp $*/

  3: /*
  4:     Defines the operations for the X PetscDraw implementation.
  5: */

  7: #include "src/sys/src/draw/impls/x/ximpl.h"         /*I  "petsc.h" I*/

  9: /*
 10:      These macros transform from the users coordinates to the 
 11:    X-window pixel coordinates.
 12: */
 13: #define XTRANS(draw,xwin,x) 
 14:    (int)(((xwin)->w)*((draw)->port_xl + (((x - (draw)->coor_xl)*
 15:                                    ((draw)->port_xr - (draw)->port_xl))/
 16:                                    ((draw)->coor_xr - (draw)->coor_xl))))
 17: #define YTRANS(draw,xwin,y) 
 18:    (int)(((xwin)->h)*(1.0-(draw)->port_yl - (((y - (draw)->coor_yl)*
 19:                                        ((draw)->port_yr - (draw)->port_yl))/
 20:                                        ((draw)->coor_yr - (draw)->coor_yl))))

 22: int PetscDrawLine_X(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr,int cl)
 23: {
 24:   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;
 25:   int          x1,y_1,x2,y2;

 28:   XiSetColor(XiWin,cl);
 29:   x1 = XTRANS(draw,XiWin,xl);   x2  = XTRANS(draw,XiWin,xr);
 30:   y_1 = YTRANS(draw,XiWin,yl);   y2  = YTRANS(draw,XiWin,yr);
 31:   XDrawLine(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,x1,y_1,x2,y2);
 32:   return(0);
 33: }

 35: static int PetscDrawPoint_X(PetscDraw draw,PetscReal x,PetscReal  y,int c)
 36: {
 37:   int          xx,yy;
 38:   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;

 41:   xx = XTRANS(draw,XiWin,x);  yy = YTRANS(draw,XiWin,y);
 42:   XiSetColor(XiWin,c);
 43:   XDrawPoint(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,xx,yy);
 44:   return(0);
 45: }

 47: static int PetscDrawRectangle_X(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr,int c1,int c2,int c3,int c4)
 48: {
 49:   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;
 50:   int          x1,y_1,w,h,c = (c1 + c2 + c3 + c4)/4;

 53:   XiSetColor(XiWin,c);
 54:   x1 = XTRANS(draw,XiWin,xl);   w  = XTRANS(draw,XiWin,xr) - x1;
 55:   y_1 = YTRANS(draw,XiWin,yr);   h  = YTRANS(draw,XiWin,yl) - y_1;
 56:   if (w <= 0) w = 1; if (h <= 0) h = 1;
 57:   XFillRectangle(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,x1,y_1,w,h);
 58:   return(0);
 59: }

 61: EXTERN int PetscDrawInterpolatedTriangle_X(PetscDraw_X*,int,int,int,int,int,int,int,int,int);

 63: static int PetscDrawTriangle_X(PetscDraw draw,PetscReal X1,PetscReal Y_1,PetscReal X2,
 64:                           PetscReal Y2,PetscReal X3,PetscReal Y3,int c1,int c2,int c3)
 65: {
 66:   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;
 67:   int          ierr;

 70:   if (c1 == c2 && c2 == c3) {
 71:     XPoint pt[3];
 72:     XiSetColor(XiWin,c1);
 73:     pt[0].x = XTRANS(draw,XiWin,X1);
 74:     pt[0].y = YTRANS(draw,XiWin,Y_1);
 75:     pt[1].x = XTRANS(draw,XiWin,X2);
 76:     pt[1].y = YTRANS(draw,XiWin,Y2);
 77:     pt[2].x = XTRANS(draw,XiWin,X3);
 78:     pt[2].y = YTRANS(draw,XiWin,Y3);
 79:     XFillPolygon(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,pt,3,Convex,CoordModeOrigin);
 80:   } else {
 81:     int x1,y_1,x2,y2,x3,y3;
 82:     x1   = XTRANS(draw,XiWin,X1);
 83:     y_1  = YTRANS(draw,XiWin,Y_1);
 84:     x2   = XTRANS(draw,XiWin,X2);
 85:     y2   = YTRANS(draw,XiWin,Y2);
 86:     x3   = XTRANS(draw,XiWin,X3);
 87:     y3   = YTRANS(draw,XiWin,Y3);
 88:     PetscDrawInterpolatedTriangle_X(XiWin,x1,y_1,c1,x2,y2,c2,x3,y3,c3);
 89:   }
 90:   return(0);
 91: }

 93: static int PetscDrawString_X(PetscDraw draw,PetscReal x,PetscReal  y,int c,char *chrs)
 94: {
 95:   int          xx,yy,ierr,len;
 96:   PetscDraw_X  *XiWin = (PetscDraw_X*)draw->data;
 97:   char         *substr;

100:   xx = XTRANS(draw,XiWin,x);  yy = YTRANS(draw,XiWin,y);
101:   XiSetColor(XiWin,c);
102: 
103:   PetscStrtok(chrs,"n",&substr);
104:   PetscStrlen(substr,&len);
105:   XDrawString(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,xx,yy - XiWin->font->font_descent,substr,len);
106:   PetscStrtok(0,"n",&substr);
107:   while (substr) {
108:     yy  += 4*XiWin->font->font_descent;
109:     PetscStrlen(substr,&len);
110:     XDrawString(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,xx,yy - XiWin->font->font_descent,substr,len);
111:     PetscStrtok(0,"n",&substr);
112:   }

114:   return(0);
115: }

117: EXTERN int XiFontFixed(PetscDraw_X*,int,int,XiFont **);

119: static int PetscDrawStringSetSize_X(PetscDraw draw,PetscReal x,PetscReal  y)
120: {
121:   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;
122:   int          ierr,w,h;

125:   w = (int)((XiWin->w)*x*(draw->port_xr - draw->port_xl)/(draw->coor_xr - draw->coor_xl));
126:   h = (int)((XiWin->h)*y*(draw->port_yr - draw->port_yl)/(draw->coor_yr - draw->coor_yl));
127:   PetscFree(XiWin->font);
128:   XiFontFixed(XiWin,w,h,&XiWin->font);
129:   return(0);
130: }

132: int PetscDrawStringGetSize_X(PetscDraw draw,PetscReal *x,PetscReal  *y)
133: {
134:   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;
135:   PetscReal    w,h;

138:   w = XiWin->font->font_w; h = XiWin->font->font_h;
139:   *x = w*(draw->coor_xr - draw->coor_xl)/(XiWin->w)*(draw->port_xr - draw->port_xl);
140:   *y = h*(draw->coor_yr - draw->coor_yl)/(XiWin->h)*(draw->port_yr - draw->port_yl);
141:   return(0);
142: }

144: int PetscDrawStringVertical_X(PetscDraw draw,PetscReal x,PetscReal  y,int c,char *chrs)
145: {
146:   int         xx,yy,n,i,ierr;
147:   PetscDraw_X *XiWin = (PetscDraw_X*)draw->data;
148:   char        tmp[2];
149:   PetscReal   tw,th;
150: 
152:   ierr   = PetscStrlen(chrs,&n);
153:   tmp[1] = 0;
154:   XiSetColor(XiWin,c);
155:   PetscDrawStringGetSize_X(draw,&tw,&th);
156:   xx = XTRANS(draw,XiWin,x);
157:   for (i=0; i<n; i++) {
158:     tmp[0] = chrs[i];
159:     yy = YTRANS(draw,XiWin,y-th*i);
160:     XDrawString(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,
161:                 xx,yy - XiWin->font->font_descent,tmp,1);
162:   }
163:   return(0);
164: }

166: static int PetscDrawFlush_X(PetscDraw draw)
167: {
168:   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;

171:   if (XiWin->drw) {
172:     XCopyArea(XiWin->disp,XiWin->drw,XiWin->win,XiWin->gc.set,0,0,XiWin->w,XiWin->h,0,0);
173:   }
174:   XFlush(XiWin->disp); XSync(XiWin->disp,False);
175:   return(0);
176: }

178: static int PetscDrawSynchronizedFlush_X(PetscDraw draw)
179: {
180:   int          rank,ierr;
181:   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;

184:   XFlush(XiWin->disp);
185:   if (XiWin->drw) {
186:     MPI_Comm_rank(draw->comm,&rank);
187:     /* make sure data has actually arrived at server */
188:     XSync(XiWin->disp,False);
189:     MPI_Barrier(draw->comm);
190:     if (!rank) {
191:       XCopyArea(XiWin->disp,XiWin->drw,XiWin->win,XiWin->gc.set,0,0,XiWin->w,XiWin->h,0,0);
192:       XFlush(XiWin->disp);
193:     }
194:     XSync(XiWin->disp,False);
195:     MPI_Barrier(draw->comm);
196:   } else {
197:     MPI_Barrier(draw->comm);
198:     XSync(XiWin->disp,False);
199:     MPI_Barrier(draw->comm);
200:   }
201:   return(0);
202: }

204: static int PetscDrawSetViewport_X(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr)
205: {
206:   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;
207:   XRectangle   box;

210:   box.x = (int)(xl*XiWin->w);   box.y = (int)((1.0-yr)*XiWin->h);
211:   box.width = (int)((xr-xl)*XiWin->w);box.height = (int)((yr-yl)*XiWin->h);
212:   XSetClipRectangles(XiWin->disp,XiWin->gc.set,0,0,&box,1,Unsorted);
213:   return(0);
214: }

216: static int PetscDrawClear_X(PetscDraw draw)
217: {
218:   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;
219:   int          x, y, w, h;

222:   x = (int)(draw->port_xl*XiWin->w);
223:   w = (int)((draw->port_xr - draw->port_xl)*XiWin->w);
224:   y = (int)((1.0-draw->port_yr)*XiWin->h);
225:   h = (int)((draw->port_yr - draw->port_yl)*XiWin->h);
226:   XiSetPixVal(XiWin,XiWin->background);
227:   XFillRectangle(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,x,y,w,h);
228:   return(0);
229: }

231: static int PetscDrawSynchronizedClear_X(PetscDraw draw)
232: {
233:   int          rank,ierr;
234:   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;

237:   MPI_Barrier(draw->comm);
238:   MPI_Comm_rank(draw->comm,&rank);
239:   if (!rank) {
240:     PetscDrawClear_X(draw);
241:   }
242:   XFlush(XiWin->disp);
243:   MPI_Barrier(draw->comm);
244:   XSync(XiWin->disp,False);
245:   MPI_Barrier(draw->comm);
246:   return(0);
247: }

249: static int PetscDrawSetDoubleBuffer_X(PetscDraw draw)
250: {
251:   PetscDraw_X*  win = (PetscDraw_X*)draw->data;
252:   int           rank,ierr;

255:   if (win->drw) return(0);

257:   MPI_Comm_rank(draw->comm,&rank);
258:   if (!rank) {
259:     win->drw = XCreatePixmap(win->disp,win->win,win->w,win->h,win->depth);
260:   }
261:   /* try to make sure it is actually done before passing info to all */
262:   XSync(win->disp,False);
263:   MPI_Bcast(&win->drw,1,MPI_UNSIGNED_LONG,0,draw->comm);
264:   return(0);
265: }

267: #include <X11/cursorfont.h>

269: static int PetscDrawGetMouseButton_X(PetscDraw draw,PetscDrawButton *button,PetscReal* x_user,
270:                                 PetscReal *y_user,PetscReal *x_phys,PetscReal *y_phys)
271: {
272:   XEvent       report;
273:   PetscDraw_X* win = (PetscDraw_X*)draw->data;
274:   Window       root,child;
275:   int          root_x,root_y,px,py;
276:   unsigned int keys_button;
277:   Cursor       cursor = 0;

280:   /* change cursor to indicate input */
281:   if (!cursor) {
282:     cursor = XCreateFontCursor(win->disp,XC_hand2);
283:     if (!cursor) SETERRQ(PETSC_ERR_LIB,"Unable to create X cursor");
284:   }
285:   XDefineCursor(win->disp,win->win,cursor);

287:   XSelectInput(win->disp,win->win,ButtonPressMask | ButtonReleaseMask);

289:   while (XCheckTypedEvent(win->disp,ButtonPress,&report));
290:   XMaskEvent(win->disp,ButtonReleaseMask,&report);
291:   switch (report.xbutton.button) {
292:     case Button1: *button = BUTTON_LEFT;   break;
293:     case Button2: *button = BUTTON_CENTER; break;
294:     case Button3: *button = BUTTON_RIGHT;  break;
295:   }
296:   XQueryPointer(win->disp,report.xmotion.window,&root,&child,&root_x,&root_y,&px,&py,&keys_button);

298:   if (x_phys) *x_phys = ((double)px)/((double)win->w);
299:   if (y_phys) *y_phys = 1.0 - ((double)py)/((double)win->h);

301:   if (x_user) *x_user = draw->coor_xl + ((((double)px)/((double)win->w)-draw->port_xl))*
302:                         (draw->coor_xr - draw->coor_xl)/(draw->port_xr - draw->port_xl);
303:   if (y_user) *y_user = draw->coor_yl +
304:                         ((1.0 - ((double)py)/((double)win->h)-draw->port_yl))*
305:                         (draw->coor_yr - draw->coor_yl)/(draw->port_yr - draw->port_yl);

307:   XUndefineCursor(win->disp,win->win);
308:   XFlush(win->disp); XSync(win->disp,False);
309:   return(0);
310: }

312: static int PetscDrawPause_X(PetscDraw draw)
313: {

317:   if (draw->pause > 0) PetscSleep(draw->pause);
318:   else if (draw->pause < 0) {
319:     PetscDrawButton button;
320:     int        rank;
321:     MPI_Comm_rank(draw->comm,&rank);
322:     if (!rank) {
323:       PetscDrawGetMouseButton(draw,&button,0,0,0,0);
324:       if (button == BUTTON_CENTER) draw->pause = 0;
325:     }
326:     MPI_Bcast(&draw->pause,1,MPI_INT,0,draw->comm);
327:   }
328:   return(0);
329: }

331: static int PetscDrawGetPopup_X(PetscDraw draw,PetscDraw *popup)
332: {
333:   int          ierr;
334:   PetscDraw_X* win = (PetscDraw_X*)draw->data;

337:   PetscDrawOpenX(draw->comm,PETSC_NULL,PETSC_NULL,win->x,win->y+win->h+36,150,220,popup);
338:   draw->popup = *popup;
339:   return(0);
340: }

342: static int PetscDrawSetTitle_X(PetscDraw draw,char *title)
343: {
344:   PetscDraw_X   *win = (PetscDraw_X*)draw->data;
345:   XTextProperty prop;
346:   int           ierr,len;

349:   XGetWMName(win->disp,win->win,&prop);
350:   prop.value  = (unsigned char *)title;
351:   ierr        = PetscStrlen(title,&len);
352:   prop.nitems = (long) len;
353:   XSetWMName(win->disp,win->win,&prop);
354:   return(0);
355: }

357: static int PetscDrawResizeWindow_X(PetscDraw draw,int w,int h)
358: {
359:   PetscDraw_X  *win = (PetscDraw_X*)draw->data;
360:   unsigned int ww,hh,border,depth;
361:   int          x,y;
362:   int          ierr;
363:   Window       root;

366:   XResizeWindow(win->disp,win->win,w,h);
367:   XGetGeometry(win->disp,win->win,&root,&x,&y,&ww,&hh,&border,&depth);
368:   PetscDrawCheckResizedWindow(draw);
369:   return(0);
370: }

372: static int PetscDrawCheckResizedWindow_X(PetscDraw draw)
373: {
374:   PetscDraw_X  *win = (PetscDraw_X*)draw->data;
375:   int          x,y,rank,ierr;
376:   Window       root;
377:   unsigned int w,h,border,depth,geo[2];
378:   PetscReal    xl,xr,yl,yr;
379:   XRectangle   box;

382:   MPI_Comm_rank(draw->comm,&rank);
383:   if (!rank) {
384:     XSync(win->disp,False);
385:     XGetGeometry(win->disp,win->win,&root,&x,&y,geo,geo+1,&border,&depth);
386:   }
387:   MPI_Bcast(geo,2,MPI_INT,0,draw->comm);
388:   w = geo[0];
389:   h = geo[1];
390:   if (w == win->w && h == win->h) return(0);

392:   /* record new window sizes */

394:   win->h = h; win->w = w;

396:   /* Free buffer space and create new version (only first processor does this) */
397:   if (win->drw) {
398:     win->drw = XCreatePixmap(win->disp,win->win,win->w,win->h,win->depth);
399:   }
400:   /* reset the clipping */
401:   xl = draw->port_xl; yl = draw->port_yl;
402:   xr = draw->port_xr; yr = draw->port_yr;
403:   box.x     = (int)(xl*win->w);     box.y      = (int)((1.0-yr)*win->h);
404:   box.width = (int)((xr-xl)*win->w);box.height = (int)((yr-yl)*win->h);
405:   XSetClipRectangles(win->disp,win->gc.set,0,0,&box,1,Unsorted);

407:   /* try to make sure it is actually done before passing info to all */
408:   XSync(win->disp,False);
409:   MPI_Bcast(&win->drw,1,MPI_UNSIGNED_LONG,0,draw->comm);
410:   return(0);
411: }

413: static int PetscDrawGetSingleton_X(PetscDraw,PetscDraw*);
414: static int PetscDrawRestoreSingleton_X(PetscDraw,PetscDraw*);

416: int PetscDrawDestroy_X(PetscDraw draw)
417: {
418:   PetscDraw_X *win = (PetscDraw_X*)draw->data;
419:   int         ierr;

422:   XFreeGC(win->disp,win->gc.set);
423:   XCloseDisplay(win->disp);
424:   if (draw->popup) {PetscDrawDestroy(draw->popup);}
425:   PetscFree(win->font);
426:   PetscFree(win);
427:   return(0);
428: }

430: static struct _PetscDrawOps DvOps = { PetscDrawSetDoubleBuffer_X,
431:                                  PetscDrawFlush_X,PetscDrawLine_X,
432:                                  0,
433:                                  0,
434:                                  PetscDrawPoint_X,
435:                                  0,
436:                                  PetscDrawString_X,
437:                                  PetscDrawStringVertical_X,
438:                                  PetscDrawStringSetSize_X,
439:                                  PetscDrawStringGetSize_X,
440:                                  PetscDrawSetViewport_X,
441:                                  PetscDrawClear_X,
442:                                  PetscDrawSynchronizedFlush_X,
443:                                  PetscDrawRectangle_X,
444:                                  PetscDrawTriangle_X,
445:                                  PetscDrawGetMouseButton_X,
446:                                  PetscDrawPause_X,
447:                                  PetscDrawSynchronizedClear_X,
448:                                  0,
449:                                  0,
450:                                  PetscDrawGetPopup_X,
451:                                  PetscDrawSetTitle_X,
452:                                  PetscDrawCheckResizedWindow_X,
453:                                  PetscDrawResizeWindow_X,
454:                                  PetscDrawDestroy_X,
455:                                  0,
456:                                  PetscDrawGetSingleton_X,
457:                                  PetscDrawRestoreSingleton_X };


460: EXTERN int XiQuickWindow(PetscDraw_X*,char*,char*,int,int,int,int);
461: EXTERN int XiQuickWindowFromWindow(PetscDraw_X*,char*,Window);

463: static int PetscDrawGetSingleton_X(PetscDraw draw,PetscDraw *sdraw)
464: {
465:   int         ierr;
466:   PetscDraw_X *Xwin = (PetscDraw_X*)draw->data,*sXwin;


470:   PetscDrawCreate(PETSC_COMM_SELF,draw->display,draw->title,draw->x,draw->y,draw->w,draw->h,sdraw);
471:   PetscObjectChangeTypeName((PetscObject)*sdraw,PETSC_DRAW_X);
472:   PetscMemcpy((*sdraw)->ops,&DvOps,sizeof(DvOps));
473:   (*sdraw)->ops->destroy = 0;

475:   (*sdraw)->pause   = draw->pause;
476:   (*sdraw)->coor_xl = draw->coor_xl;
477:   (*sdraw)->coor_xr = draw->coor_xr;
478:   (*sdraw)->coor_yl = draw->coor_yl;
479:   (*sdraw)->coor_yr = draw->coor_yr;
480:   (*sdraw)->port_xl = draw->port_xl;
481:   (*sdraw)->port_xr = draw->port_xr;
482:   (*sdraw)->port_yl = draw->port_yl;
483:   (*sdraw)->port_yr = draw->port_yr;
484:   (*sdraw)->popup   = draw->popup;

486:   /* actually create and open the window */
487:   PetscMalloc(sizeof(PetscDraw_X),&sXwin);
488:   PetscMemzero(sXwin,sizeof(PetscDraw_X));
489:   XiQuickWindowFromWindow(sXwin,draw->display,Xwin->win);

491:   sXwin->x       = Xwin->x;
492:   sXwin->y       = Xwin->y;
493:   sXwin->w       = Xwin->w;
494:   sXwin->h       = Xwin->h;
495:   (*sdraw)->data = (void*)sXwin;
496:  return(0);
497: }

499: static int PetscDrawRestoreSingleton_X(PetscDraw draw,PetscDraw *sdraw)
500: {
501:   int         ierr;
502:   PetscDraw_X *sXwin = (PetscDraw_X*)(*sdraw)->data;

504:   XFreeGC(sXwin->disp,sXwin->gc.set);
505:   XCloseDisplay(sXwin->disp);
506:   if ((*sdraw)->popup)   {PetscDrawDestroy((*sdraw)->popup);}
507:   PetscStrfree((*sdraw)->title);
508:   PetscStrfree((*sdraw)->display);
509:   PetscFree(sXwin->font);
510:   PetscFree(sXwin);
511:   PetscHeaderDestroy(*sdraw);
513:   return(0);
514: }

516: int PetscDrawXGetDisplaySize_Private(const char name[],int *width,int *height)
517: {
518:   Display *display;

521:   display = XOpenDisplay(name);
522:   if (!display) {
523:     *width  = 0;
524:     *height = 0;
525:     SETERRQ1(1,"Unable to open display on %sn.  Make sure your DISPLAY variablen
526:     is set,or you use the -display name option and xhost + has beenn
527:     run on your displaying machine.n",name);
528:   }

530:   *width  = DisplayWidth(display,0);
531:   *height = DisplayHeight(display,0);

533:   XCloseDisplay(display);
534:   return(0);
535: }

537: EXTERN_C_BEGIN
538: int PetscDrawCreate_X(PetscDraw draw)
539: {
540:   PetscDraw_X *Xwin;
541:   int         ierr,rank,xywh[4],osize = 4;
542:   int         x = draw->x,y = draw->y,w = draw->w,h = draw->h;
543:   static int  xavailable = 0,yavailable = 0,xmax = 0,ymax = 0,ybottom = 0;
544:   PetscTruth  flg;

547:   if (!draw->display) {
548:     PetscMalloc(128*sizeof(char),&draw->display);
549:     PetscGetDisplay(draw->display,128);
550:   }

552:   /*
553:       Initialize the display size
554:   */
555:   if (!xmax) {
556:     PetscDrawXGetDisplaySize_Private(draw->display,&xmax,&ymax);
557:     /* if some processors fail on this and others succed then this is a problem ! */
558:     if (ierr) {
559:        (*PetscErrorPrintf)("PETSc unable to use X windowsnproceeding without graphicsn");
560:        PetscDrawSetType(draw,PETSC_DRAW_NULL);
561:        return(0);
562:     }
563:   }

565:   if (w == PETSC_DECIDE) w = draw->w = 300;
566:   if (h == PETSC_DECIDE) h = draw->h = 300;
567:   switch (w) {
568:     case PETSC_DRAW_FULL_SIZE: w = draw->w = xmax - 10;
569:                          break;
570:     case PETSC_DRAW_HALF_SIZE: w = draw->w = (xmax - 20)/2;
571:                          break;
572:     case PETSC_DRAW_THIRD_SIZE: w = draw->w = (xmax - 30)/3;
573:                          break;
574:     case PETSC_DRAW_QUARTER_SIZE: w = draw->w = (xmax - 40)/4;
575:                          break;
576:   }
577:   switch (h) {
578:     case PETSC_DRAW_FULL_SIZE: h = draw->h = ymax - 10;
579:                          break;
580:     case PETSC_DRAW_HALF_SIZE: h = draw->h = (ymax - 20)/2;
581:                          break;
582:     case PETSC_DRAW_THIRD_SIZE: h = draw->h = (ymax - 30)/3;
583:                          break;
584:     case PETSC_DRAW_QUARTER_SIZE: h = draw->h = (ymax - 40)/4;
585:                          break;
586:   }

588:   /* allow user to set location and size of window */
589:   xywh[0] = x; xywh[1] = y; xywh[2] = w; xywh[3] = h;
590:   PetscOptionsGetIntArray(PETSC_NULL,"-geometry",xywh,&osize,PETSC_NULL);
591:   x = xywh[0]; y = xywh[1]; w = xywh[2]; h = xywh[3];


594:   if (draw->x == PETSC_DECIDE || draw->y == PETSC_DECIDE) {
595:     /*
596:        PETSc tries to place windows starting in the upper left corner and 
597:        moving across to the right. 
598:     
599:               --------------------------------------------
600:               |  Region used so far +xavailable,yavailable |
601:               |                     +                      |
602:               |                     +                      |
603:               |++++++++++++++++++++++ybottom               |
604:               |                                            |
605:               |                                            |
606:               |--------------------------------------------|
607:     */
608:     /*  First: can we add it to the right? */
609:     if (xavailable+w+10 <= xmax) {
610:       x       = xavailable;
611:       y       = yavailable;
612:       ybottom = PetscMax(ybottom,y + h + 30);
613:     } else {
614:       /* No, so add it below on the left */
615:       xavailable = 0;
616:       x          = 0;
617:       yavailable = ybottom;
618:       y          = ybottom;
619:       ybottom    = ybottom + h + 30;
620:     }
621:   }
622:   /* update available region */
623:   xavailable = PetscMax(xavailable,x + w + 10);
624:   if (xavailable >= xmax) {
625:     xavailable = 0;
626:     yavailable = yavailable + h + 30;
627:     ybottom    = yavailable;
628:   }
629:   if (yavailable >= ymax) {
630:     y          = 0;
631:     yavailable = 0;
632:     ybottom    = 0;
633:   }

635:   PetscMemcpy(draw->ops,&DvOps,sizeof(DvOps));

637:   /* actually create and open the window */
638:   PetscMalloc(sizeof(PetscDraw_X),&Xwin);
639:   PetscLogObjectMemory(draw,sizeof(PetscDraw_X)+sizeof(struct _p_PetscDraw));
640:   PetscMemzero(Xwin,sizeof(PetscDraw_X));
641:   MPI_Comm_rank(draw->comm,&rank);

643:   if (!rank) {
644:     if (x < 0 || y < 0)   SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Negative corner of window");
645:     if (w <= 0 || h <= 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Negative window width or height");
646:     XiQuickWindow(Xwin,draw->display,draw->title,x,y,w,h);
647:     MPI_Bcast(&Xwin->win,1,MPI_UNSIGNED_LONG,0,draw->comm);
648:   } else {
649:     unsigned long win;
650:     MPI_Bcast(&win,1,MPI_UNSIGNED_LONG,0,draw->comm);
651:     XiQuickWindowFromWindow(Xwin,draw->display,win);
652:   }

654:   Xwin->x      = x;
655:   Xwin->y      = y;
656:   Xwin->w      = w;
657:   Xwin->h      = h;
658:   draw->data    = (void*)Xwin;

660:   /*
661:     Need barrier here so processor 0 does not destroy the window before other 
662:     processors have completed XiQuickWindow()
663:   */
664:   PetscDrawClear(draw);
665:   PetscDrawSynchronizedFlush(draw);

667:   PetscOptionsHasName(PETSC_NULL,"-draw_double_buffer",&flg);
668:   if (flg) {
669:      PetscDrawSetDoubleBuffer(draw);
670:   }

672:   return(0);
673: }
674: EXTERN_C_END