Actual source code: xops.c

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

  6: #include <../src/sys/draw/impls/x/ximpl.h>         /*I  "petscsys.h" I*/

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

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

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

 39: PetscErrorCode PetscDrawArrow_X(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr,int cl)
 40: {
 41:   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;
 42:   int          x1,y_1,x2,y2;

 45:   XiSetColor(XiWin,cl);
 46:   x1 = XTRANS(draw,XiWin,xl);   x2  = XTRANS(draw,XiWin,xr);
 47:   y_1 = YTRANS(draw,XiWin,yl);   y2  = YTRANS(draw,XiWin,yr);
 48:   if (x1 == x2 && y_1 == y2) return(0);
 49:   XDrawLine(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,x1,y_1,x2,y2);
 50:   if (x1 == x2 && PetscAbs(y_1 - y2) > 7) {
 51:     if (y2 > y_1) {
 52:        XDrawLine(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,x2,y2,x2-3,y2-3);
 53:        XDrawLine(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,x2,y2,x2+3,y2-3);
 54:     } else {
 55:        XDrawLine(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,x2,y2,x2-3,y2+3);
 56:        XDrawLine(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,x2,y2,x2+3,y2+3);
 57:     }
 58:   }
 59:   return(0);
 60: }

 64: static PetscErrorCode PetscDrawPoint_X(PetscDraw draw,PetscReal x,PetscReal  y,int c)
 65: {
 66:   int          xx,yy;
 67:   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;

 70:   xx = XTRANS(draw,XiWin,x);  yy = YTRANS(draw,XiWin,y);
 71:   XiSetColor(XiWin,c);
 72:   XDrawPoint(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,xx,yy);
 73:   return(0);
 74: }

 78: static PetscErrorCode PetscDrawRectangle_X(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr,int c1,int c2,int c3,int c4)
 79: {
 80:   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;
 81:   int          x1,y_1,w,h,c = (c1 + c2 + c3 + c4)/4;

 84:   XiSetColor(XiWin,c);
 85:   x1 = XTRANS(draw,XiWin,xl);   w  = XTRANS(draw,XiWin,xr) - x1;
 86:   y_1 = YTRANS(draw,XiWin,yr);   h  = YTRANS(draw,XiWin,yl) - y_1;
 87:   if (w <= 0) w = 1; if (h <= 0) h = 1;
 88:   XFillRectangle(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,x1,y_1,w,h);
 89:   return(0);
 90: }

 94: static PetscErrorCode PetscDrawEllipse_X(PetscDraw Win, PetscReal x, PetscReal y, PetscReal a, PetscReal b, int c)
 95: {
 96:   PetscDraw_X* XiWin = (PetscDraw_X*) Win->data;
 97:   int          xA, yA, w, h;

100:   XiSetColor(XiWin, c);
101:   xA = XTRANS(Win, XiWin, x - a/2.0); w = XTRANS(Win, XiWin, x + a/2.0) - xA;
102:   yA = YTRANS(Win, XiWin, y + b/2.0); h = YTRANS(Win, XiWin, y - b/2.0) - yA;
103:   XFillArc(XiWin->disp, XiDrawable(XiWin), XiWin->gc.set, xA, yA, w, h, 0, 23040);
104:   return(0);
105: }


111: static PetscErrorCode PetscDrawTriangle_X(PetscDraw draw,PetscReal X1,PetscReal Y_1,PetscReal X2,
112:                                           PetscReal Y2,PetscReal X3,PetscReal Y3,int c1,int c2,int c3)
113: {
114:   PetscDraw_X*   XiWin = (PetscDraw_X*)draw->data;

118:   if (c1 == c2 && c2 == c3) {
119:     XPoint pt[3];
120:     XiSetColor(XiWin,c1);
121:     pt[0].x = XTRANS(draw,XiWin,X1);
122:     pt[0].y = YTRANS(draw,XiWin,Y_1);
123:     pt[1].x = XTRANS(draw,XiWin,X2);
124:     pt[1].y = YTRANS(draw,XiWin,Y2);
125:     pt[2].x = XTRANS(draw,XiWin,X3);
126:     pt[2].y = YTRANS(draw,XiWin,Y3);
127:     XFillPolygon(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,pt,3,Convex,CoordModeOrigin);
128:   } else {
129:     int x1,y_1,x2,y2,x3,y3;
130:     x1   = XTRANS(draw,XiWin,X1);
131:     y_1  = YTRANS(draw,XiWin,Y_1);
132:     x2   = XTRANS(draw,XiWin,X2);
133:     y2   = YTRANS(draw,XiWin,Y2);
134:     x3   = XTRANS(draw,XiWin,X3);
135:     y3   = YTRANS(draw,XiWin,Y3);
136:     PetscDrawInterpolatedTriangle_X(XiWin,x1,y_1,c1,x2,y2,c2,x3,y3,c3);
137:   }
138:   return(0);
139: }

143: static PetscErrorCode PetscDrawString_X(PetscDraw draw,PetscReal x,PetscReal  y,int c,const char chrs[])
144: {
146:   int            xx,yy;
147:   size_t         len;
148:   PetscDraw_X    *XiWin = (PetscDraw_X*)draw->data;
149:   char           *substr;
150:   PetscToken     token;

153:   xx = XTRANS(draw,XiWin,x);  yy = YTRANS(draw,XiWin,y);
154:   XiSetColor(XiWin,c);
155: 
156:   PetscTokenCreate(chrs,'\n',&token);
157:   PetscTokenFind(token,&substr);
158:   PetscStrlen(substr,&len);
159:   XDrawString(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,xx,yy - XiWin->font->font_descent,substr,len);
160:   PetscTokenFind(token,&substr);
161:   while (substr) {
162:     yy  += 4*XiWin->font->font_descent;
163:     PetscStrlen(substr,&len);
164:     XDrawString(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,xx,yy - XiWin->font->font_descent,substr,len);
165:     PetscTokenFind(token,&substr);
166:   }
167:   PetscTokenDestroy(token);

169:   return(0);
170: }


176: static PetscErrorCode PetscDrawStringSetSize_X(PetscDraw draw,PetscReal x,PetscReal  y)
177: {
178:   PetscDraw_X*   XiWin = (PetscDraw_X*)draw->data;
180:   int            w,h;

183:   w = (int)((XiWin->w)*x*(draw->port_xr - draw->port_xl)/(draw->coor_xr - draw->coor_xl));
184:   h = (int)((XiWin->h)*y*(draw->port_yr - draw->port_yl)/(draw->coor_yr - draw->coor_yl));
185:   PetscFree(XiWin->font);
186:   XiFontFixed(XiWin,w,h,&XiWin->font);
187:   return(0);
188: }

192: PetscErrorCode PetscDrawStringGetSize_X(PetscDraw draw,PetscReal *x,PetscReal  *y)
193: {
194:   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;
195:   PetscReal    w,h;

198:   w = XiWin->font->font_w; h = XiWin->font->font_h;
199:   *x = w*(draw->coor_xr - draw->coor_xl)/((XiWin->w)*(draw->port_xr - draw->port_xl));
200:   *y = h*(draw->coor_yr - draw->coor_yl)/((XiWin->h)*(draw->port_yr - draw->port_yl));
201:   return(0);
202: }

206: PetscErrorCode PetscDrawStringVertical_X(PetscDraw draw,PetscReal x,PetscReal  y,int c,const char chrs[])
207: {
209:   int            xx,yy;
210:   PetscDraw_X    *XiWin = (PetscDraw_X*)draw->data;
211:   char           tmp[2];
212:   PetscReal      tw,th;
213:   size_t         i,n;
214: 
216:   PetscStrlen(chrs,&n);
217:   tmp[1] = 0;
218:   XiSetColor(XiWin,c);
219:   PetscDrawStringGetSize_X(draw,&tw,&th);
220:   xx = XTRANS(draw,XiWin,x);
221:   for (i=0; i<n; i++) {
222:     tmp[0] = chrs[i];
223:     yy = YTRANS(draw,XiWin,y-th*i);
224:     XDrawString(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set, xx,yy - XiWin->font->font_descent,tmp,1);
225:   }
226:   return(0);
227: }

231: static PetscErrorCode PetscDrawFlush_X(PetscDraw draw)
232: {
233:   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;

236:   if (XiWin->drw) {
237:     XCopyArea(XiWin->disp,XiWin->drw,XiWin->win,XiWin->gc.set,0,0,XiWin->w,XiWin->h,0,0);
238:   }
239:   XFlush(XiWin->disp); XSync(XiWin->disp,False);
240:   return(0);
241: }

245: static PetscErrorCode PetscDrawSynchronizedFlush_X(PetscDraw draw)
246: {
248:   PetscMPIInt    rank;
249:   PetscDraw_X*   XiWin = (PetscDraw_X*)draw->data;

252:   XFlush(XiWin->disp);
253:   if (XiWin->drw) {
254:     MPI_Comm_rank(((PetscObject)draw)->comm,&rank);
255:     /* make sure data has actually arrived at server */
256:     XSync(XiWin->disp,False);
257:     MPI_Barrier(((PetscObject)draw)->comm);
258:     if (!rank) {
259:       XCopyArea(XiWin->disp,XiWin->drw,XiWin->win,XiWin->gc.set,0,0,XiWin->w,XiWin->h,0,0);
260:       XFlush(XiWin->disp);
261:     }
262:     XSync(XiWin->disp,False);
263:     MPI_Barrier(((PetscObject)draw)->comm);
264:   } else {
265:     MPI_Barrier(((PetscObject)draw)->comm);
266:     XSync(XiWin->disp,False);
267:     MPI_Barrier(((PetscObject)draw)->comm);
268:   }
269:   return(0);
270: }

274: static PetscErrorCode PetscDrawSetViewport_X(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr)
275: {
276:   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;
277:   XRectangle   box;

280:   box.x = (int)(xl*XiWin->w);   box.y = (int)((1.0-yr)*XiWin->h);
281:   box.width = (int)((xr-xl)*XiWin->w);box.height = (int)((yr-yl)*XiWin->h);
282:   XSetClipRectangles(XiWin->disp,XiWin->gc.set,0,0,&box,1,Unsorted);
283:   return(0);
284: }

288: static PetscErrorCode PetscDrawClear_X(PetscDraw draw)
289: {
290:   PetscDraw_X*   XiWin = (PetscDraw_X*)draw->data;
291:   int            x, y, w, h;

295:   PetscDrawSave(draw);
296:   x = (int)(draw->port_xl*XiWin->w);
297:   w = (int)((draw->port_xr - draw->port_xl)*XiWin->w);
298:   y = (int)((1.0-draw->port_yr)*XiWin->h);
299:   h = (int)((draw->port_yr - draw->port_yl)*XiWin->h);
300:   XiSetPixVal(XiWin,XiWin->background);
301:   XFillRectangle(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,x,y,w,h);
302:   return(0);
303: }

307: static PetscErrorCode PetscDrawSynchronizedClear_X(PetscDraw draw)
308: {
310:   PetscMPIInt    rank;
311:   PetscDraw_X*   XiWin = (PetscDraw_X*)draw->data;

314:   MPI_Barrier(((PetscObject)draw)->comm);
315:   MPI_Comm_rank(((PetscObject)draw)->comm,&rank);
316:   if (!rank) {
317:     PetscDrawClear_X(draw);
318:   }
319:   XFlush(XiWin->disp);
320:   MPI_Barrier(((PetscObject)draw)->comm);
321:   XSync(XiWin->disp,False);
322:   MPI_Barrier(((PetscObject)draw)->comm);
323:   return(0);
324: }

328: static PetscErrorCode PetscDrawSetDoubleBuffer_X(PetscDraw draw)
329: {
330:   PetscDraw_X*   win = (PetscDraw_X*)draw->data;
332:   PetscMPIInt   rank;

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

337:   MPI_Comm_rank(((PetscObject)draw)->comm,&rank);
338:   if (!rank) {
339:     win->drw = XCreatePixmap(win->disp,win->win,win->w,win->h,win->depth);
340:   }
341:   /* try to make sure it is actually done before passing info to all */
342:   XSync(win->disp,False);
343:   MPI_Bcast(&win->drw,1,MPI_UNSIGNED_LONG,0,((PetscObject)draw)->comm);
344:   return(0);
345: }

347: #include <X11/cursorfont.h>

351: static PetscErrorCode PetscDrawGetMouseButton_X(PetscDraw draw,PetscDrawButton *button,PetscReal* x_user,PetscReal *y_user,PetscReal *x_phys,PetscReal *y_phys)
352: {
353:   XEvent       report;
354:   PetscDraw_X* win = (PetscDraw_X*)draw->data;
355:   Window       root,child;
356:   int          root_x,root_y,px,py;
357:   unsigned int keys_button;
358:   Cursor       cursor = 0;

361:   /* change cursor to indicate input */
362:   if (!cursor) {
363:     cursor = XCreateFontCursor(win->disp,XC_hand2);
364:     if (!cursor) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Unable to create X cursor");
365:   }
366:   XDefineCursor(win->disp,win->win,cursor);
367:   XSelectInput(win->disp,win->win,ButtonPressMask | ButtonReleaseMask);

369:   while (XCheckTypedEvent(win->disp,ButtonPress,&report));
370:   XMaskEvent(win->disp,ButtonReleaseMask,&report);
371:   switch (report.xbutton.button) {
372:     case Button1:
373:       if (report.xbutton.state & ShiftMask)
374:         *button = BUTTON_LEFT_SHIFT;
375:       else
376:         *button = BUTTON_LEFT;
377:       break;
378:     case Button2:
379:       if (report.xbutton.state & ShiftMask)
380:         *button = BUTTON_CENTER_SHIFT;
381:       else
382:         *button = BUTTON_CENTER;
383:       break;
384:     case Button3:
385:       if (report.xbutton.state & ShiftMask)
386:         *button = BUTTON_RIGHT_SHIFT;
387:       else
388:         *button = BUTTON_RIGHT;
389:       break;
390:   }
391:   XQueryPointer(win->disp,report.xmotion.window,&root,&child,&root_x,&root_y,&px,&py,&keys_button);

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

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

399:   XUndefineCursor(win->disp,win->win);
400:   XFlush(win->disp); XSync(win->disp,False);
401:   return(0);
402: }

406: static PetscErrorCode PetscDrawPause_X(PetscDraw draw)
407: {

411:   if (draw->pause > 0) PetscSleep(draw->pause);
412:   else if (draw->pause < 0) {
413:     PetscDrawButton button;
414:     PetscMPIInt     rank;
415:     MPI_Comm_rank(((PetscObject)draw)->comm,&rank);
416:     if (!rank) {
417:       PetscDrawGetMouseButton(draw,&button,0,0,0,0);
418:       if (button == BUTTON_CENTER) draw->pause = 0;
419:     }
420:     MPI_Bcast(&draw->pause,1,MPI_INT,0,((PetscObject)draw)->comm);
421:   }
422:   return(0);
423: }

427: static PetscErrorCode PetscDrawGetPopup_X(PetscDraw draw,PetscDraw *popup)
428: {
430:   PetscDraw_X*   win = (PetscDraw_X*)draw->data;

433:   PetscDrawOpenX(((PetscObject)draw)->comm,PETSC_NULL,PETSC_NULL,win->x,win->y+win->h+36,220,220,popup);
434:   draw->popup = *popup;
435:   return(0);
436: }

440: static PetscErrorCode PetscDrawSetTitle_X(PetscDraw draw,const char title[])
441: {
442:   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
443:   XTextProperty  prop;
445:   size_t         len;

448:   XGetWMName(win->disp,win->win,&prop);
449:   XFree((void*)prop.value);
450:   prop.value  = (unsigned char *)title;
451:   PetscStrlen(title,&len);
452:   prop.nitems = (long) len;
453:   XSetWMName(win->disp,win->win,&prop);
454:   return(0);
455: }

459: static PetscErrorCode PetscDrawResizeWindow_X(PetscDraw draw,int w,int h)
460: {
461:   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
462:   unsigned int   ww,hh,border,depth;
463:   int            x,y;
465:   Window         root;

468:   XResizeWindow(win->disp,win->win,w,h);
469:   XGetGeometry(win->disp,win->win,&root,&x,&y,&ww,&hh,&border,&depth);
470:   PetscDrawCheckResizedWindow(draw);
471:   return(0);
472: }

476: static PetscErrorCode PetscDrawCheckResizedWindow_X(PetscDraw draw)
477: {
478:   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
480:   int            x,y;
481:   PetscMPIInt    rank;
482:   Window         root;
483:   unsigned int   w,h,border,depth,geo[2];
484:   PetscReal      xl,xr,yl,yr;
485:   XRectangle     box;

488:   MPI_Comm_rank(((PetscObject)draw)->comm,&rank);
489:   if (!rank) {
490:     XSync(win->disp,False);
491:     XGetGeometry(win->disp,win->win,&root,&x,&y,geo,geo+1,&border,&depth);
492:   }
493:   MPI_Bcast(geo,2,MPI_INT,0,((PetscObject)draw)->comm);
494:   w = geo[0];
495:   h = geo[1];
496:   if (w == (unsigned int) win->w && h == (unsigned int) win->h) return(0);

498:   /* record new window sizes */

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

502:   /* Free buffer space and create new version (only first processor does this) */
503:   if (win->drw) {
504:     win->drw = XCreatePixmap(win->disp,win->win,win->w,win->h,win->depth);
505:   }
506:   /* reset the clipping */
507:   xl = draw->port_xl; yl = draw->port_yl;
508:   xr = draw->port_xr; yr = draw->port_yr;
509:   box.x     = (int)(xl*win->w);     box.y      = (int)((1.0-yr)*win->h);
510:   box.width = (int)((xr-xl)*win->w);box.height = (int)((yr-yl)*win->h);
511:   XSetClipRectangles(win->disp,win->gc.set,0,0,&box,1,Unsorted);

513:   /* try to make sure it is actually done before passing info to all */
514:   XSync(win->disp,False);
515:   MPI_Bcast(&win->drw,1,MPI_UNSIGNED_LONG,0,((PetscObject)draw)->comm);
516:   return(0);
517: }

519: static PetscErrorCode PetscDrawGetSingleton_X(PetscDraw,PetscDraw*);
520: static PetscErrorCode PetscDrawRestoreSingleton_X(PetscDraw,PetscDraw*);

524: PetscErrorCode PetscDrawDestroy_X(PetscDraw draw)
525: {
526:   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
528: #if defined(PETSC_HAVE_POPEN)
529:   char           command[PETSC_MAX_PATH_LEN];
530:   PetscMPIInt    rank;
531:   FILE           *fd;
532: #endif

535:   PetscDrawSynchronizedClear(draw);

537: #if defined(PETSC_HAVE_POPEN)
538:   MPI_Comm_rank(((PetscObject)draw)->comm,&rank);
539:   if (draw->savefilename && !rank) {
540:     PetscSNPrintf(command,PETSC_MAX_PATH_LEN,"ffmpeg  -i %s_%%d.Gif %s.m4v",draw->savefilename,draw->savefilename);
541:     PetscPOpen(((PetscObject)draw)->comm,PETSC_NULL,command,"r",&fd);
542:     PetscPClose(((PetscObject)draw)->comm,fd);
543:   }
544: #endif

546:   XFreeGC(win->disp,win->gc.set);
547:   XCloseDisplay(win->disp);
548:   PetscDrawDestroy(&draw->popup);
549:   PetscFree(win->font);
550:   PetscFree(win);
551:   return(0);
552: }

554: PetscErrorCode PetscDrawSave_X(PetscDraw);
555: PetscErrorCode PetscDrawSetSave_X(PetscDraw,const char*);

557: static struct _PetscDrawOps DvOps = { PetscDrawSetDoubleBuffer_X,
558:                                  PetscDrawFlush_X,PetscDrawLine_X,
559:                                  0,
560:                                  0,
561:                                  PetscDrawPoint_X,
562:                                  0,
563:                                  PetscDrawString_X,
564:                                  PetscDrawStringVertical_X,
565:                                  PetscDrawStringSetSize_X,
566:                                  PetscDrawStringGetSize_X,
567:                                  PetscDrawSetViewport_X,
568:                                  PetscDrawClear_X,
569:                                  PetscDrawSynchronizedFlush_X,
570:                                  PetscDrawRectangle_X,
571:                                  PetscDrawTriangle_X,
572:                                  PetscDrawEllipse_X,
573:                                  PetscDrawGetMouseButton_X,
574:                                  PetscDrawPause_X,
575:                                  PetscDrawSynchronizedClear_X,
576:                                  0,
577:                                  0,
578:                                  PetscDrawGetPopup_X,
579:                                  PetscDrawSetTitle_X,
580:                                  PetscDrawCheckResizedWindow_X,
581:                                  PetscDrawResizeWindow_X,
582:                                  PetscDrawDestroy_X,
583:                                  0,
584:                                  PetscDrawGetSingleton_X,
585:                                  PetscDrawRestoreSingleton_X,
586: #if defined(PETSC_HAVE_AFTERIMAGE) || defined(PETSC_HAVE_IMAGEMAGICK)
587:                                  PetscDrawSave_X,
588: #else
589:                                  0,
590: #endif
591:                                  PetscDrawSetSave_X,
592:                                  0,
593:                                  PetscDrawArrow_X};



601: static PetscErrorCode PetscDrawGetSingleton_X(PetscDraw draw,PetscDraw *sdraw)
602: {
604:   PetscDraw_X *Xwin = (PetscDraw_X*)draw->data,*sXwin;


608:   PetscDrawCreate(PETSC_COMM_SELF,draw->display,draw->title,draw->x,draw->y,draw->w,draw->h,sdraw);
609:   PetscObjectChangeTypeName((PetscObject)*sdraw,PETSC_DRAW_X);
610:   PetscMemcpy((*sdraw)->ops,&DvOps,sizeof(DvOps));
611:   (*sdraw)->ops->destroy = 0;

613:   (*sdraw)->pause   = draw->pause;
614:   (*sdraw)->coor_xl = draw->coor_xl;
615:   (*sdraw)->coor_xr = draw->coor_xr;
616:   (*sdraw)->coor_yl = draw->coor_yl;
617:   (*sdraw)->coor_yr = draw->coor_yr;
618:   (*sdraw)->port_xl = draw->port_xl;
619:   (*sdraw)->port_xr = draw->port_xr;
620:   (*sdraw)->port_yl = draw->port_yl;
621:   (*sdraw)->port_yr = draw->port_yr;
622:   (*sdraw)->popup   = draw->popup;

624:   /* actually create and open the window */
625:   PetscNew(PetscDraw_X,&sXwin);
626:   XiQuickWindowFromWindow(sXwin,draw->display,Xwin->win);

628:   sXwin->x       = Xwin->x;
629:   sXwin->y       = Xwin->y;
630:   sXwin->w       = Xwin->w;
631:   sXwin->h       = Xwin->h;
632:   (*sdraw)->data = (void*)sXwin;
633:  return(0);
634: }

638: static PetscErrorCode PetscDrawRestoreSingleton_X(PetscDraw draw,PetscDraw *sdraw)
639: {
641:   PetscDraw_X    *sXwin = (PetscDraw_X*)(*sdraw)->data;

644:   XFreeGC(sXwin->disp,sXwin->gc.set);
645:   XCloseDisplay(sXwin->disp);
646:   PetscDrawDestroy(&(*sdraw)->popup);
647:   PetscFree((*sdraw)->title);
648:   PetscFree((*sdraw)->display);
649:   PetscFree(sXwin->font);
650:   PetscFree(sXwin);
651:   PetscHeaderDestroy(sdraw);
652:   return(0);
653: }

657: PetscErrorCode PetscDrawXGetDisplaySize_Private(const char name[],int *width,int *height)
658: {
659:   Display *display;

662:   display = XOpenDisplay(name);
663:   if (!display) {
664:     *width  = 0;
665:     *height = 0;
666:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Unable to open display on %s\n.  Make sure your COMPUTE NODES are authorized to connect \n\
667:     to this X server and either your DISPLAY variable\n\
668:     is set or you use the -display name option\n",name);
669:   }
670:   *width  = DisplayWidth(display,0);
671:   *height = DisplayHeight(display,0);
672:   XCloseDisplay(display);
673:   return(0);
674: }

679: PetscErrorCode  PetscDrawCreate_X(PetscDraw draw)
680: {
681:   PetscDraw_X    *Xwin;
683:   PetscMPIInt    rank;
684:   PetscInt       xywh[4],osize = 4;
685:   int            x = draw->x,y = draw->y,w = draw->w,h = draw->h;
686:   static int     xavailable = 0,yavailable = 0,xmax = 0,ymax = 0,ybottom = 0;
687:   PetscBool      flg = PETSC_FALSE;

690:   if (!draw->display) {
691:     PetscMalloc(256*sizeof(char),&draw->display);
692:     PetscGetDisplay(draw->display,256);
693:   }

695:   /*
696:       Initialize the display size
697:   */
698:   if (!xmax) {
699:     PetscDrawXGetDisplaySize_Private(draw->display,&xmax,&ymax);
700:     /* if some processors fail on this and others succed then this is a problem ! */
701:     if (ierr) {
702:        (*PetscErrorPrintf)("PETSc unable to use X windows\nproceeding without graphics\n");
703:        PetscDrawSetType(draw,PETSC_DRAW_NULL);
704:        return(0);
705:     }
706:   }

708:   if (w == PETSC_DECIDE) w = draw->w = 300;
709:   if (h == PETSC_DECIDE) h = draw->h = 300;
710:   switch (w) {
711:     case PETSC_DRAW_FULL_SIZE: w = draw->w = xmax - 10;
712:                          break;
713:     case PETSC_DRAW_HALF_SIZE: w = draw->w = (xmax - 20)/2;
714:                          break;
715:     case PETSC_DRAW_THIRD_SIZE: w = draw->w = (xmax - 30)/3;
716:                          break;
717:     case PETSC_DRAW_QUARTER_SIZE: w = draw->w = (xmax - 40)/4;
718:                          break;
719:   }
720:   switch (h) {
721:     case PETSC_DRAW_FULL_SIZE: h = draw->h = ymax - 10;
722:                          break;
723:     case PETSC_DRAW_HALF_SIZE: h = draw->h = (ymax - 20)/2;
724:                          break;
725:     case PETSC_DRAW_THIRD_SIZE: h = draw->h = (ymax - 30)/3;
726:                          break;
727:     case PETSC_DRAW_QUARTER_SIZE: h = draw->h = (ymax - 40)/4;
728:                          break;
729:   }

731:   /* allow user to set location and size of window */
732:   xywh[0] = x; xywh[1] = y; xywh[2] = w; xywh[3] = h;
733:   PetscOptionsGetIntArray(PETSC_NULL,"-geometry",xywh,&osize,PETSC_NULL);
734:   x = (int) xywh[0]; y = (int) xywh[1]; w = (int) xywh[2]; h = (int) xywh[3];


737:   if (draw->x == PETSC_DECIDE || draw->y == PETSC_DECIDE) {
738:     /*
739:        PETSc tries to place windows starting in the upper left corner and 
740:        moving across to the right. 
741:     
742:               --------------------------------------------
743:               |  Region used so far +xavailable,yavailable |
744:               |                     +                      |
745:               |                     +                      |
746:               |++++++++++++++++++++++ybottom               |
747:               |                                            |
748:               |                                            |
749:               |--------------------------------------------|
750:     */
751:     /*  First: can we add it to the right? */
752:     if (xavailable+w+10 <= xmax) {
753:       x       = xavailable;
754:       y       = yavailable;
755:       ybottom = PetscMax(ybottom,y + h + 30);
756:     } else {
757:       /* No, so add it below on the left */
758:       xavailable = 0;
759:       x          = 0;
760:       yavailable = ybottom;
761:       y          = ybottom;
762:       ybottom    = ybottom + h + 30;
763:     }
764:   }
765:   /* update available region */
766:   xavailable = PetscMax(xavailable,x + w + 10);
767:   if (xavailable >= xmax) {
768:     xavailable = 0;
769:     yavailable = yavailable + h + 30;
770:     ybottom    = yavailable;
771:   }
772:   if (yavailable >= ymax) {
773:     y          = 0;
774:     yavailable = 0;
775:     ybottom    = 0;
776:   }

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

780:   /* actually create and open the window */
781:   PetscNew(PetscDraw_X,&Xwin);
782:   PetscLogObjectMemory(draw,sizeof(PetscDraw_X));
783:   MPI_Comm_rank(((PetscObject)draw)->comm,&rank);

785:   if (!rank) {
786:     if (x < 0 || y < 0)   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative corner of window");
787:     if (w <= 0 || h <= 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative window width or height");
788:     XiQuickWindow(Xwin,draw->display,draw->title,x,y,w,h);
789:     MPI_Bcast(&Xwin->win,1,MPI_UNSIGNED_LONG,0,((PetscObject)draw)->comm);
790:   } else {
791:     unsigned long win = 0;
792:     MPI_Bcast(&win,1,MPI_UNSIGNED_LONG,0,((PetscObject)draw)->comm);
793:     XiQuickWindowFromWindow(Xwin,draw->display,win);
794:   }

796:   Xwin->x      = x;
797:   Xwin->y      = y;
798:   Xwin->w      = w;
799:   Xwin->h      = h;
800:   draw->data    = (void*)Xwin;

802:   /*
803:     Need barrier here so processor 0 does not destroy the window before other 
804:     processors have completed XiQuickWindow()
805:   */
806:   PetscDrawClear(draw);
807:   PetscDrawSynchronizedFlush(draw);

809:   PetscOptionsGetBool(PETSC_NULL,"-draw_double_buffer",&flg,PETSC_NULL);
810:   if (flg) {
811:      PetscDrawSetDoubleBuffer(draw);
812:   }

814:   return(0);
815: }