Actual source code: xops.c

  1: /* $Id: xops.c,v 1.156 2001/09/07 20:08:05 bsmith Exp $*/

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

 7:  #include src/sys/src/draw/impls/x/ximpl.h

  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))))

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

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

 39: static int PetscDrawPoint_X(PetscDraw draw,PetscReal x,PetscReal  y,int c)
 40: {
 41:   int          xx,yy;
 42:   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;

 45:   xx = XTRANS(draw,XiWin,x);  yy = YTRANS(draw,XiWin,y);
 46:   XiSetColor(XiWin,c);
 47:   XDrawPoint(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,xx,yy);
 48:   return(0);
 49: }

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

 59:   XiSetColor(XiWin,c);
 60:   x1 = XTRANS(draw,XiWin,xl);   w  = XTRANS(draw,XiWin,xr) - x1;
 61:   y_1 = YTRANS(draw,XiWin,yr);   h  = YTRANS(draw,XiWin,yl) - y_1;
 62:   if (w <= 0) w = 1; if (h <= 0) h = 1;
 63:   XFillRectangle(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,x1,y_1,w,h);
 64:   return(0);
 65: }

 69: static int PetscDrawEllipse_X(PetscDraw Win, PetscReal x, PetscReal y, PetscReal a, PetscReal b, int c)
 70: {
 71:   PetscDraw_X* XiWin = (PetscDraw_X*) Win->data;
 72:   int          xA, yA, w, h;

 75:   XiSetColor(XiWin, c);
 76:   xA = XTRANS(Win, XiWin, x - a/2.0); w = XTRANS(Win, XiWin, x + a/2.0) - xA;
 77:   yA = YTRANS(Win, XiWin, y + b/2.0); h = YTRANS(Win, XiWin, y - b/2.0) - yA;
 78:   XFillArc(XiWin->disp, XiDrawable(XiWin), XiWin->gc.set, xA, yA, w, h, 0, 23040);
 79:   return(0);
 80: }

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

 86: static int PetscDrawTriangle_X(PetscDraw draw,PetscReal X1,PetscReal Y_1,PetscReal X2,
 87:                           PetscReal Y2,PetscReal X3,PetscReal Y3,int c1,int c2,int c3)
 88: {
 89:   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;
 90:   int          ierr;

 93:   if (c1 == c2 && c2 == c3) {
 94:     XPoint pt[3];
 95:     XiSetColor(XiWin,c1);
 96:     pt[0].x = XTRANS(draw,XiWin,X1);
 97:     pt[0].y = YTRANS(draw,XiWin,Y_1);
 98:     pt[1].x = XTRANS(draw,XiWin,X2);
 99:     pt[1].y = YTRANS(draw,XiWin,Y2);
100:     pt[2].x = XTRANS(draw,XiWin,X3);
101:     pt[2].y = YTRANS(draw,XiWin,Y3);
102:     XFillPolygon(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,pt,3,Convex,CoordModeOrigin);
103:   } else {
104:     int x1,y_1,x2,y2,x3,y3;
105:     x1   = XTRANS(draw,XiWin,X1);
106:     y_1  = YTRANS(draw,XiWin,Y_1);
107:     x2   = XTRANS(draw,XiWin,X2);
108:     y2   = YTRANS(draw,XiWin,Y2);
109:     x3   = XTRANS(draw,XiWin,X3);
110:     y3   = YTRANS(draw,XiWin,Y3);
111:     PetscDrawInterpolatedTriangle_X(XiWin,x1,y_1,c1,x2,y2,c2,x3,y3,c3);
112:   }
113:   return(0);
114: }

118: static int PetscDrawString_X(PetscDraw draw,PetscReal x,PetscReal  y,int c,const char chrs[])
119: {
120:   int          xx,yy,ierr,len;
121:   PetscDraw_X  *XiWin = (PetscDraw_X*)draw->data;
122:   char         *substr;
123:   PetscToken   *token;

126:   xx = XTRANS(draw,XiWin,x);  yy = YTRANS(draw,XiWin,y);
127:   XiSetColor(XiWin,c);
128: 
129:   PetscTokenCreate(chrs,'\n',&token);
130:   PetscTokenFind(token,&substr);
131:   PetscStrlen(substr,&len);
132:   XDrawString(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,xx,yy - XiWin->font->font_descent,substr,len);
133:   PetscTokenFind(token,&substr);
134:   while (substr) {
135:     yy  += 4*XiWin->font->font_descent;
136:     PetscStrlen(substr,&len);
137:     XDrawString(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,xx,yy - XiWin->font->font_descent,substr,len);
138:     PetscTokenFind(token,&substr);
139:   }
140:   PetscTokenDestroy(token);

142:   return(0);
143: }

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

149: static int PetscDrawStringSetSize_X(PetscDraw draw,PetscReal x,PetscReal  y)
150: {
151:   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;
152:   int          ierr,w,h;

155:   w = (int)((XiWin->w)*x*(draw->port_xr - draw->port_xl)/(draw->coor_xr - draw->coor_xl));
156:   h = (int)((XiWin->h)*y*(draw->port_yr - draw->port_yl)/(draw->coor_yr - draw->coor_yl));
157:   PetscFree(XiWin->font);
158:   XiFontFixed(XiWin,w,h,&XiWin->font);
159:   return(0);
160: }

164: int PetscDrawStringGetSize_X(PetscDraw draw,PetscReal *x,PetscReal  *y)
165: {
166:   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;
167:   PetscReal    w,h;

170:   w = XiWin->font->font_w; h = XiWin->font->font_h;
171:   *x = w*(draw->coor_xr - draw->coor_xl)/(XiWin->w)*(draw->port_xr - draw->port_xl);
172:   *y = h*(draw->coor_yr - draw->coor_yl)/(XiWin->h)*(draw->port_yr - draw->port_yl);
173:   return(0);
174: }

178: int PetscDrawStringVertical_X(PetscDraw draw,PetscReal x,PetscReal  y,int c,const char chrs[])
179: {
180:   int         xx,yy,n,i,ierr;
181:   PetscDraw_X *XiWin = (PetscDraw_X*)draw->data;
182:   char        tmp[2];
183:   PetscReal   tw,th;
184: 
186:   PetscStrlen(chrs,&n);
187:   tmp[1] = 0;
188:   XiSetColor(XiWin,c);
189:   PetscDrawStringGetSize_X(draw,&tw,&th);
190:   xx = XTRANS(draw,XiWin,x);
191:   for (i=0; i<n; i++) {
192:     tmp[0] = chrs[i];
193:     yy = YTRANS(draw,XiWin,y-th*i);
194:     XDrawString(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,
195:                 xx,yy - XiWin->font->font_descent,tmp,1);
196:   }
197:   return(0);
198: }

202: static int PetscDrawFlush_X(PetscDraw draw)
203: {
204:   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;

207:   if (XiWin->drw) {
208:     XCopyArea(XiWin->disp,XiWin->drw,XiWin->win,XiWin->gc.set,0,0,XiWin->w,XiWin->h,0,0);
209:   }
210:   XFlush(XiWin->disp); XSync(XiWin->disp,False);
211:   return(0);
212: }

216: static int PetscDrawSynchronizedFlush_X(PetscDraw draw)
217: {
218:   int          rank,ierr;
219:   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;

222:   XFlush(XiWin->disp);
223:   if (XiWin->drw) {
224:     MPI_Comm_rank(draw->comm,&rank);
225:     /* make sure data has actually arrived at server */
226:     XSync(XiWin->disp,False);
227:     MPI_Barrier(draw->comm);
228:     if (!rank) {
229:       XCopyArea(XiWin->disp,XiWin->drw,XiWin->win,XiWin->gc.set,0,0,XiWin->w,XiWin->h,0,0);
230:       XFlush(XiWin->disp);
231:     }
232:     XSync(XiWin->disp,False);
233:     MPI_Barrier(draw->comm);
234:   } else {
235:     MPI_Barrier(draw->comm);
236:     XSync(XiWin->disp,False);
237:     MPI_Barrier(draw->comm);
238:   }
239:   return(0);
240: }

244: static int PetscDrawSetViewport_X(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr)
245: {
246:   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;
247:   XRectangle   box;

250:   box.x = (int)(xl*XiWin->w);   box.y = (int)((1.0-yr)*XiWin->h);
251:   box.width = (int)((xr-xl)*XiWin->w);box.height = (int)((yr-yl)*XiWin->h);
252:   XSetClipRectangles(XiWin->disp,XiWin->gc.set,0,0,&box,1,Unsorted);
253:   return(0);
254: }

258: static int PetscDrawClear_X(PetscDraw draw)
259: {
260:   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;
261:   int          x, y, w, h;

264:   x = (int)(draw->port_xl*XiWin->w);
265:   w = (int)((draw->port_xr - draw->port_xl)*XiWin->w);
266:   y = (int)((1.0-draw->port_yr)*XiWin->h);
267:   h = (int)((draw->port_yr - draw->port_yl)*XiWin->h);
268:   XiSetPixVal(XiWin,XiWin->background);
269:   XFillRectangle(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,x,y,w,h);
270:   return(0);
271: }

275: static int PetscDrawSynchronizedClear_X(PetscDraw draw)
276: {
277:   int          rank,ierr;
278:   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;

281:   MPI_Barrier(draw->comm);
282:   MPI_Comm_rank(draw->comm,&rank);
283:   if (!rank) {
284:     PetscDrawClear_X(draw);
285:   }
286:   XFlush(XiWin->disp);
287:   MPI_Barrier(draw->comm);
288:   XSync(XiWin->disp,False);
289:   MPI_Barrier(draw->comm);
290:   return(0);
291: }

295: static int PetscDrawSetDoubleBuffer_X(PetscDraw draw)
296: {
297:   PetscDraw_X*  win = (PetscDraw_X*)draw->data;
298:   int           rank,ierr;

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

303:   MPI_Comm_rank(draw->comm,&rank);
304:   if (!rank) {
305:     win->drw = XCreatePixmap(win->disp,win->win,win->w,win->h,win->depth);
306:   }
307:   /* try to make sure it is actually done before passing info to all */
308:   XSync(win->disp,False);
309:   MPI_Bcast(&win->drw,1,MPI_UNSIGNED_LONG,0,draw->comm);
310:   return(0);
311: }

313: #include <X11/cursorfont.h>

317: static int PetscDrawGetMouseButton_X(PetscDraw draw,PetscDrawButton *button,PetscReal* x_user,
318:                                 PetscReal *y_user,PetscReal *x_phys,PetscReal *y_phys)
319: {
320:   XEvent       report;
321:   PetscDraw_X* win = (PetscDraw_X*)draw->data;
322:   Window       root,child;
323:   int          root_x,root_y,px,py;
324:   unsigned int keys_button;
325:   Cursor       cursor = 0;

328:   /* change cursor to indicate input */
329:   if (!cursor) {
330:     cursor = XCreateFontCursor(win->disp,XC_hand2);
331:     if (!cursor) SETERRQ(PETSC_ERR_LIB,"Unable to create X cursor");
332:   }
333:   XDefineCursor(win->disp,win->win,cursor);

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

337:   while (XCheckTypedEvent(win->disp,ButtonPress,&report));
338:   XMaskEvent(win->disp,ButtonReleaseMask,&report);
339:   switch (report.xbutton.button) {
340:     case Button1:
341:       if (report.xbutton.state & ShiftMask)
342:         *button = BUTTON_LEFT_SHIFT;
343:       else
344:         *button = BUTTON_LEFT;
345:       break;
346:     case Button2:
347:       if (report.xbutton.state & ShiftMask)
348:         *button = BUTTON_CENTER_SHIFT;
349:       else
350:         *button = BUTTON_CENTER;
351:       break;
352:     case Button3:
353:       if (report.xbutton.state & ShiftMask)
354:         *button = BUTTON_RIGHT_SHIFT;
355:       else
356:         *button = BUTTON_RIGHT;
357:       break;
358:   }
359:   XQueryPointer(win->disp,report.xmotion.window,&root,&child,&root_x,&root_y,&px,&py,&keys_button);

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

364:   if (x_user) *x_user = draw->coor_xl + ((((double)px)/((double)win->w)-draw->port_xl))*
365:                         (draw->coor_xr - draw->coor_xl)/(draw->port_xr - draw->port_xl);
366:   if (y_user) *y_user = draw->coor_yl +
367:                         ((1.0 - ((double)py)/((double)win->h)-draw->port_yl))*
368:                         (draw->coor_yr - draw->coor_yl)/(draw->port_yr - draw->port_yl);

370:   XUndefineCursor(win->disp,win->win);
371:   XFlush(win->disp); XSync(win->disp,False);
372:   return(0);
373: }

377: static int PetscDrawPause_X(PetscDraw draw)
378: {

382:   if (draw->pause > 0) PetscSleep(draw->pause);
383:   else if (draw->pause < 0) {
384:     PetscDrawButton button;
385:     int        rank;
386:     MPI_Comm_rank(draw->comm,&rank);
387:     if (!rank) {
388:       PetscDrawGetMouseButton(draw,&button,0,0,0,0);
389:       if (button == BUTTON_CENTER) draw->pause = 0;
390:     }
391:     MPI_Bcast(&draw->pause,1,MPI_INT,0,draw->comm);
392:   }
393:   return(0);
394: }

398: static int PetscDrawGetPopup_X(PetscDraw draw,PetscDraw *popup)
399: {
400:   int          ierr;
401:   PetscDraw_X* win = (PetscDraw_X*)draw->data;

404:   PetscDrawOpenX(draw->comm,PETSC_NULL,PETSC_NULL,win->x,win->y+win->h+36,150,220,popup);
405:   draw->popup = *popup;
406:   return(0);
407: }

411: static int PetscDrawSetTitle_X(PetscDraw draw,const char title[])
412: {
413:   PetscDraw_X   *win = (PetscDraw_X*)draw->data;
414:   XTextProperty prop;
415:   int           ierr,len;

418:   XGetWMName(win->disp,win->win,&prop);
419:   prop.value  = (unsigned char *)title;
420:   PetscStrlen(title,&len);
421:   prop.nitems = (long) len;
422:   XSetWMName(win->disp,win->win,&prop);
423:   return(0);
424: }

428: static int PetscDrawResizeWindow_X(PetscDraw draw,int w,int h)
429: {
430:   PetscDraw_X  *win = (PetscDraw_X*)draw->data;
431:   unsigned int ww,hh,border,depth;
432:   int          x,y;
433:   int          ierr;
434:   Window       root;

437:   XResizeWindow(win->disp,win->win,w,h);
438:   XGetGeometry(win->disp,win->win,&root,&x,&y,&ww,&hh,&border,&depth);
439:   PetscDrawCheckResizedWindow(draw);
440:   return(0);
441: }

445: static int PetscDrawCheckResizedWindow_X(PetscDraw draw)
446: {
447:   PetscDraw_X  *win = (PetscDraw_X*)draw->data;
448:   int          x,y,rank,ierr;
449:   Window       root;
450:   unsigned int w,h,border,depth,geo[2];
451:   PetscReal    xl,xr,yl,yr;
452:   XRectangle   box;

455:   MPI_Comm_rank(draw->comm,&rank);
456:   if (!rank) {
457:     XSync(win->disp,False);
458:     XGetGeometry(win->disp,win->win,&root,&x,&y,geo,geo+1,&border,&depth);
459:   }
460:   MPI_Bcast(geo,2,MPI_INT,0,draw->comm);
461:   w = geo[0];
462:   h = geo[1];
463:   if (w == (unsigned int) win->w && h == (unsigned int) win->h) return(0);

465:   /* record new window sizes */

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

469:   /* Free buffer space and create new version (only first processor does this) */
470:   if (win->drw) {
471:     win->drw = XCreatePixmap(win->disp,win->win,win->w,win->h,win->depth);
472:   }
473:   /* reset the clipping */
474:   xl = draw->port_xl; yl = draw->port_yl;
475:   xr = draw->port_xr; yr = draw->port_yr;
476:   box.x     = (int)(xl*win->w);     box.y      = (int)((1.0-yr)*win->h);
477:   box.width = (int)((xr-xl)*win->w);box.height = (int)((yr-yl)*win->h);
478:   XSetClipRectangles(win->disp,win->gc.set,0,0,&box,1,Unsorted);

480:   /* try to make sure it is actually done before passing info to all */
481:   XSync(win->disp,False);
482:   MPI_Bcast(&win->drw,1,MPI_UNSIGNED_LONG,0,draw->comm);
483:   return(0);
484: }

486: static int PetscDrawGetSingleton_X(PetscDraw,PetscDraw*);
487: static int PetscDrawRestoreSingleton_X(PetscDraw,PetscDraw*);

491: int PetscDrawDestroy_X(PetscDraw draw)
492: {
493:   PetscDraw_X *win = (PetscDraw_X*)draw->data;
494:   int         ierr;

497:   XFreeGC(win->disp,win->gc.set);
498:   XCloseDisplay(win->disp);
499:   if (draw->popup) {PetscDrawDestroy(draw->popup);}
500:   PetscFree(win->font);
501:   PetscFree(win);
502:   return(0);
503: }

505: static struct _PetscDrawOps DvOps = { PetscDrawSetDoubleBuffer_X,
506:                                  PetscDrawFlush_X,PetscDrawLine_X,
507:                                  0,
508:                                  0,
509:                                  PetscDrawPoint_X,
510:                                  0,
511:                                  PetscDrawString_X,
512:                                  PetscDrawStringVertical_X,
513:                                  PetscDrawStringSetSize_X,
514:                                  PetscDrawStringGetSize_X,
515:                                  PetscDrawSetViewport_X,
516:                                  PetscDrawClear_X,
517:                                  PetscDrawSynchronizedFlush_X,
518:                                  PetscDrawRectangle_X,
519:                                  PetscDrawTriangle_X,
520:                                  PetscDrawEllipse_X,
521:                                  PetscDrawGetMouseButton_X,
522:                                  PetscDrawPause_X,
523:                                  PetscDrawSynchronizedClear_X,
524:                                  0,
525:                                  0,
526:                                  PetscDrawGetPopup_X,
527:                                  PetscDrawSetTitle_X,
528:                                  PetscDrawCheckResizedWindow_X,
529:                                  PetscDrawResizeWindow_X,
530:                                  PetscDrawDestroy_X,
531:                                  0,
532:                                  PetscDrawGetSingleton_X,
533:                                  PetscDrawRestoreSingleton_X };


536: EXTERN int XiQuickWindow(PetscDraw_X*,char*,char*,int,int,int,int);
537: EXTERN int XiQuickWindowFromWindow(PetscDraw_X*,char*,Window);

541: static int PetscDrawGetSingleton_X(PetscDraw draw,PetscDraw *sdraw)
542: {
543:   int         ierr;
544:   PetscDraw_X *Xwin = (PetscDraw_X*)draw->data,*sXwin;


548:   PetscDrawCreate(PETSC_COMM_SELF,draw->display,draw->title,draw->x,draw->y,draw->w,draw->h,sdraw);
549:   PetscObjectChangeTypeName((PetscObject)*sdraw,PETSC_DRAW_X);
550:   PetscMemcpy((*sdraw)->ops,&DvOps,sizeof(DvOps));
551:   (*sdraw)->ops->destroy = 0;

553:   (*sdraw)->pause   = draw->pause;
554:   (*sdraw)->coor_xl = draw->coor_xl;
555:   (*sdraw)->coor_xr = draw->coor_xr;
556:   (*sdraw)->coor_yl = draw->coor_yl;
557:   (*sdraw)->coor_yr = draw->coor_yr;
558:   (*sdraw)->port_xl = draw->port_xl;
559:   (*sdraw)->port_xr = draw->port_xr;
560:   (*sdraw)->port_yl = draw->port_yl;
561:   (*sdraw)->port_yr = draw->port_yr;
562:   (*sdraw)->popup   = draw->popup;

564:   /* actually create and open the window */
565:   PetscNew(PetscDraw_X,&sXwin);
566:   PetscMemzero(sXwin,sizeof(PetscDraw_X));
567:   XiQuickWindowFromWindow(sXwin,draw->display,Xwin->win);

569:   sXwin->x       = Xwin->x;
570:   sXwin->y       = Xwin->y;
571:   sXwin->w       = Xwin->w;
572:   sXwin->h       = Xwin->h;
573:   (*sdraw)->data = (void*)sXwin;
574:  return(0);
575: }

579: static int PetscDrawRestoreSingleton_X(PetscDraw draw,PetscDraw *sdraw)
580: {
581:   int         ierr;
582:   PetscDraw_X *sXwin = (PetscDraw_X*)(*sdraw)->data;

584:   XFreeGC(sXwin->disp,sXwin->gc.set);
585:   XCloseDisplay(sXwin->disp);
586:   if ((*sdraw)->popup)   {PetscDrawDestroy((*sdraw)->popup);}
587:   PetscStrfree((*sdraw)->title);
588:   PetscStrfree((*sdraw)->display);
589:   PetscFree(sXwin->font);
590:   PetscFree(sXwin);
591:   PetscHeaderDestroy(*sdraw);
593:   return(0);
594: }

598: int PetscDrawXGetDisplaySize_Private(const char name[],int *width,int *height)
599: {
600:   Display *display;

603:   display = XOpenDisplay(name);
604:   if (!display) {
605:     *width  = 0;
606:     *height = 0;
607:     SETERRQ1(1,"Unable to open display on %s\n.  Make sure your COMPUTE NODES are authorized to connect \n\
608:     to this X server and either your DISPLAY variable\n\
609:     is set or you use the -display name option\n",name);
610:   }

612:   *width  = DisplayWidth(display,0);
613:   *height = DisplayHeight(display,0);

615:   XCloseDisplay(display);
616:   return(0);
617: }

619: EXTERN_C_BEGIN
622: int PetscDrawCreate_X(PetscDraw draw)
623: {
624:   PetscDraw_X *Xwin;
625:   int         ierr,rank,xywh[4],osize = 4;
626:   int         x = draw->x,y = draw->y,w = draw->w,h = draw->h;
627:   static int  xavailable = 0,yavailable = 0,xmax = 0,ymax = 0,ybottom = 0;
628:   PetscTruth  flg;

631:   if (!draw->display) {
632:     PetscMalloc(128*sizeof(char),&draw->display);
633:     PetscGetDisplay(draw->display,128);
634:   }

636:   /*
637:       Initialize the display size
638:   */
639:   if (!xmax) {
640:     PetscDrawXGetDisplaySize_Private(draw->display,&xmax,&ymax);
641:     /* if some processors fail on this and others succed then this is a problem ! */
642:     if (ierr) {
643:        (*PetscErrorPrintf)("PETSc unable to use X windows\nproceeding without graphics\n");
644:        PetscDrawSetType(draw,PETSC_DRAW_NULL);
645:        return(0);
646:     }
647:   }

649:   if (w == PETSC_DECIDE) w = draw->w = 300;
650:   if (h == PETSC_DECIDE) h = draw->h = 300;
651:   switch (w) {
652:     case PETSC_DRAW_FULL_SIZE: w = draw->w = xmax - 10;
653:                          break;
654:     case PETSC_DRAW_HALF_SIZE: w = draw->w = (xmax - 20)/2;
655:                          break;
656:     case PETSC_DRAW_THIRD_SIZE: w = draw->w = (xmax - 30)/3;
657:                          break;
658:     case PETSC_DRAW_QUARTER_SIZE: w = draw->w = (xmax - 40)/4;
659:                          break;
660:   }
661:   switch (h) {
662:     case PETSC_DRAW_FULL_SIZE: h = draw->h = ymax - 10;
663:                          break;
664:     case PETSC_DRAW_HALF_SIZE: h = draw->h = (ymax - 20)/2;
665:                          break;
666:     case PETSC_DRAW_THIRD_SIZE: h = draw->h = (ymax - 30)/3;
667:                          break;
668:     case PETSC_DRAW_QUARTER_SIZE: h = draw->h = (ymax - 40)/4;
669:                          break;
670:   }

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


678:   if (draw->x == PETSC_DECIDE || draw->y == PETSC_DECIDE) {
679:     /*
680:        PETSc tries to place windows starting in the upper left corner and 
681:        moving across to the right. 
682:     
683:               --------------------------------------------
684:               |  Region used so far +xavailable,yavailable |
685:               |                     +                      |
686:               |                     +                      |
687:               |++++++++++++++++++++++ybottom               |
688:               |                                            |
689:               |                                            |
690:               |--------------------------------------------|
691:     */
692:     /*  First: can we add it to the right? */
693:     if (xavailable+w+10 <= xmax) {
694:       x       = xavailable;
695:       y       = yavailable;
696:       ybottom = PetscMax(ybottom,y + h + 30);
697:     } else {
698:       /* No, so add it below on the left */
699:       xavailable = 0;
700:       x          = 0;
701:       yavailable = ybottom;
702:       y          = ybottom;
703:       ybottom    = ybottom + h + 30;
704:     }
705:   }
706:   /* update available region */
707:   xavailable = PetscMax(xavailable,x + w + 10);
708:   if (xavailable >= xmax) {
709:     xavailable = 0;
710:     yavailable = yavailable + h + 30;
711:     ybottom    = yavailable;
712:   }
713:   if (yavailable >= ymax) {
714:     y          = 0;
715:     yavailable = 0;
716:     ybottom    = 0;
717:   }

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

721:   /* actually create and open the window */
722:   PetscNew(PetscDraw_X,&Xwin);
723:   PetscLogObjectMemory(draw,sizeof(PetscDraw_X)+sizeof(struct _p_PetscDraw));
724:   PetscMemzero(Xwin,sizeof(PetscDraw_X));
725:   MPI_Comm_rank(draw->comm,&rank);

727:   if (!rank) {
728:     if (x < 0 || y < 0)   SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Negative corner of window");
729:     if (w <= 0 || h <= 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Negative window width or height");
730:     XiQuickWindow(Xwin,draw->display,draw->title,x,y,w,h);
731:     MPI_Bcast(&Xwin->win,1,MPI_UNSIGNED_LONG,0,draw->comm);
732:   } else {
733:     unsigned long win = 0;
734:     MPI_Bcast(&win,1,MPI_UNSIGNED_LONG,0,draw->comm);
735:     XiQuickWindowFromWindow(Xwin,draw->display,win);
736:   }

738:   Xwin->x      = x;
739:   Xwin->y      = y;
740:   Xwin->w      = w;
741:   Xwin->h      = h;
742:   draw->data    = (void*)Xwin;

744:   /*
745:     Need barrier here so processor 0 does not destroy the window before other 
746:     processors have completed XiQuickWindow()
747:   */
748:   PetscDrawClear(draw);
749:   PetscDrawSynchronizedFlush(draw);

751:   PetscOptionsHasName(PETSC_NULL,"-draw_double_buffer",&flg);
752:   if (flg) {
753:      PetscDrawSetDoubleBuffer(draw);
754:   }

756:   return(0);
757: }
758: EXTERN_C_END