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
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: XDrawLine(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,x1,y_1,x2,y2);
33: return(0);
34: }
38: static PetscErrorCode PetscDrawPoint_X(PetscDraw draw,PetscReal x,PetscReal y,int c)
39: {
40: int xx,yy;
41: PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;
44: xx = XTRANS(draw,XiWin,x); yy = YTRANS(draw,XiWin,y);
45: XiSetColor(XiWin,c);
46: XDrawPoint(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,xx,yy);
47: return(0);
48: }
52: static PetscErrorCode PetscDrawRectangle_X(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr,int c1,int c2,int c3,int c4)
53: {
54: PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;
55: int x1,y_1,w,h,c = (c1 + c2 + c3 + c4)/4;
58: XiSetColor(XiWin,c);
59: x1 = XTRANS(draw,XiWin,xl); w = XTRANS(draw,XiWin,xr) - x1;
60: y_1 = YTRANS(draw,XiWin,yr); h = YTRANS(draw,XiWin,yl) - y_1;
61: if (w <= 0) w = 1; if (h <= 0) h = 1;
62: XFillRectangle(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,x1,y_1,w,h);
63: return(0);
64: }
68: static PetscErrorCode PetscDrawEllipse_X(PetscDraw Win, PetscReal x, PetscReal y, PetscReal a, PetscReal b, int c)
69: {
70: PetscDraw_X* XiWin = (PetscDraw_X*) Win->data;
71: int xA, yA, w, h;
74: XiSetColor(XiWin, c);
75: xA = XTRANS(Win, XiWin, x - a/2.0); w = XTRANS(Win, XiWin, x + a/2.0) - xA;
76: yA = YTRANS(Win, XiWin, y + b/2.0); h = YTRANS(Win, XiWin, y - b/2.0) - yA;
77: XFillArc(XiWin->disp, XiDrawable(XiWin), XiWin->gc.set, xA, yA, w, h, 0, 23040);
78: return(0);
79: }
81: EXTERN PetscErrorCode PetscDrawInterpolatedTriangle_X(PetscDraw_X*,int,int,int,int,int,int,int,int,int);
85: static PetscErrorCode PetscDrawTriangle_X(PetscDraw draw,PetscReal X1,PetscReal Y_1,PetscReal X2,
86: PetscReal Y2,PetscReal X3,PetscReal Y3,int c1,int c2,int c3)
87: {
88: PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;
92: if (c1 == c2 && c2 == c3) {
93: XPoint pt[3];
94: XiSetColor(XiWin,c1);
95: pt[0].x = XTRANS(draw,XiWin,X1);
96: pt[0].y = YTRANS(draw,XiWin,Y_1);
97: pt[1].x = XTRANS(draw,XiWin,X2);
98: pt[1].y = YTRANS(draw,XiWin,Y2);
99: pt[2].x = XTRANS(draw,XiWin,X3);
100: pt[2].y = YTRANS(draw,XiWin,Y3);
101: XFillPolygon(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,pt,3,Convex,CoordModeOrigin);
102: } else {
103: int x1,y_1,x2,y2,x3,y3;
104: x1 = XTRANS(draw,XiWin,X1);
105: y_1 = YTRANS(draw,XiWin,Y_1);
106: x2 = XTRANS(draw,XiWin,X2);
107: y2 = YTRANS(draw,XiWin,Y2);
108: x3 = XTRANS(draw,XiWin,X3);
109: y3 = YTRANS(draw,XiWin,Y3);
110: PetscDrawInterpolatedTriangle_X(XiWin,x1,y_1,c1,x2,y2,c2,x3,y3,c3);
111: }
112: return(0);
113: }
117: static PetscErrorCode PetscDrawString_X(PetscDraw draw,PetscReal x,PetscReal y,int c,const char chrs[])
118: {
120: int xx,yy;
121: size_t len;
122: PetscDraw_X *XiWin = (PetscDraw_X*)draw->data;
123: char *substr;
124: PetscToken token;
127: xx = XTRANS(draw,XiWin,x); yy = YTRANS(draw,XiWin,y);
128: XiSetColor(XiWin,c);
129:
130: PetscTokenCreate(chrs,'\n',&token);
131: PetscTokenFind(token,&substr);
132: PetscStrlen(substr,&len);
133: XDrawString(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,xx,yy - XiWin->font->font_descent,substr,len);
134: PetscTokenFind(token,&substr);
135: while (substr) {
136: yy += 4*XiWin->font->font_descent;
137: PetscStrlen(substr,&len);
138: XDrawString(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,xx,yy - XiWin->font->font_descent,substr,len);
139: PetscTokenFind(token,&substr);
140: }
141: PetscTokenDestroy(token);
143: return(0);
144: }
146: EXTERN PetscErrorCode XiFontFixed(PetscDraw_X*,int,int,XiFont **);
150: static PetscErrorCode PetscDrawStringSetSize_X(PetscDraw draw,PetscReal x,PetscReal y)
151: {
152: PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;
154: int w,h;
157: w = (int)((XiWin->w)*x*(draw->port_xr - draw->port_xl)/(draw->coor_xr - draw->coor_xl));
158: h = (int)((XiWin->h)*y*(draw->port_yr - draw->port_yl)/(draw->coor_yr - draw->coor_yl));
159: PetscFree(XiWin->font);
160: XiFontFixed(XiWin,w,h,&XiWin->font);
161: return(0);
162: }
166: PetscErrorCode PetscDrawStringGetSize_X(PetscDraw draw,PetscReal *x,PetscReal *y)
167: {
168: PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;
169: PetscReal w,h;
172: w = XiWin->font->font_w; h = XiWin->font->font_h;
173: *x = w*(draw->coor_xr - draw->coor_xl)/(XiWin->w)*(draw->port_xr - draw->port_xl);
174: *y = h*(draw->coor_yr - draw->coor_yl)/(XiWin->h)*(draw->port_yr - draw->port_yl);
175: return(0);
176: }
180: PetscErrorCode PetscDrawStringVertical_X(PetscDraw draw,PetscReal x,PetscReal y,int c,const char chrs[])
181: {
183: int xx,yy;
184: PetscDraw_X *XiWin = (PetscDraw_X*)draw->data;
185: char tmp[2];
186: PetscReal tw,th;
187: size_t i,n;
188:
190: PetscStrlen(chrs,&n);
191: tmp[1] = 0;
192: XiSetColor(XiWin,c);
193: PetscDrawStringGetSize_X(draw,&tw,&th);
194: xx = XTRANS(draw,XiWin,x);
195: for (i=0; i<n; i++) {
196: tmp[0] = chrs[i];
197: yy = YTRANS(draw,XiWin,y-th*i);
198: XDrawString(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,
199: xx,yy - XiWin->font->font_descent,tmp,1);
200: }
201: return(0);
202: }
206: static PetscErrorCode PetscDrawFlush_X(PetscDraw draw)
207: {
208: PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;
211: if (XiWin->drw) {
212: XCopyArea(XiWin->disp,XiWin->drw,XiWin->win,XiWin->gc.set,0,0,XiWin->w,XiWin->h,0,0);
213: }
214: XFlush(XiWin->disp); XSync(XiWin->disp,False);
215: return(0);
216: }
220: static PetscErrorCode PetscDrawSynchronizedFlush_X(PetscDraw draw)
221: {
223: PetscMPIInt rank;
224: PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;
227: XFlush(XiWin->disp);
228: if (XiWin->drw) {
229: MPI_Comm_rank(((PetscObject)draw)->comm,&rank);
230: /* make sure data has actually arrived at server */
231: XSync(XiWin->disp,False);
232: MPI_Barrier(((PetscObject)draw)->comm);
233: if (!rank) {
234: XCopyArea(XiWin->disp,XiWin->drw,XiWin->win,XiWin->gc.set,0,0,XiWin->w,XiWin->h,0,0);
235: XFlush(XiWin->disp);
236: }
237: XSync(XiWin->disp,False);
238: MPI_Barrier(((PetscObject)draw)->comm);
239: } else {
240: MPI_Barrier(((PetscObject)draw)->comm);
241: XSync(XiWin->disp,False);
242: MPI_Barrier(((PetscObject)draw)->comm);
243: }
244: return(0);
245: }
249: static PetscErrorCode PetscDrawSetViewport_X(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr)
250: {
251: PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;
252: XRectangle box;
255: box.x = (int)(xl*XiWin->w); box.y = (int)((1.0-yr)*XiWin->h);
256: box.width = (int)((xr-xl)*XiWin->w);box.height = (int)((yr-yl)*XiWin->h);
257: XSetClipRectangles(XiWin->disp,XiWin->gc.set,0,0,&box,1,Unsorted);
258: return(0);
259: }
263: static PetscErrorCode PetscDrawClear_X(PetscDraw draw)
264: {
265: PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;
266: int x, y, w, h;
269: x = (int)(draw->port_xl*XiWin->w);
270: w = (int)((draw->port_xr - draw->port_xl)*XiWin->w);
271: y = (int)((1.0-draw->port_yr)*XiWin->h);
272: h = (int)((draw->port_yr - draw->port_yl)*XiWin->h);
273: XiSetPixVal(XiWin,XiWin->background);
274: XFillRectangle(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,x,y,w,h);
275: return(0);
276: }
280: static PetscErrorCode PetscDrawSynchronizedClear_X(PetscDraw draw)
281: {
283: PetscMPIInt rank;
284: PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;
287: MPI_Barrier(((PetscObject)draw)->comm);
288: MPI_Comm_rank(((PetscObject)draw)->comm,&rank);
289: if (!rank) {
290: PetscDrawClear_X(draw);
291: }
292: XFlush(XiWin->disp);
293: MPI_Barrier(((PetscObject)draw)->comm);
294: XSync(XiWin->disp,False);
295: MPI_Barrier(((PetscObject)draw)->comm);
296: return(0);
297: }
301: static PetscErrorCode PetscDrawSetDoubleBuffer_X(PetscDraw draw)
302: {
303: PetscDraw_X* win = (PetscDraw_X*)draw->data;
305: PetscMPIInt rank;
308: if (win->drw) return(0);
310: MPI_Comm_rank(((PetscObject)draw)->comm,&rank);
311: if (!rank) {
312: win->drw = XCreatePixmap(win->disp,win->win,win->w,win->h,win->depth);
313: }
314: /* try to make sure it is actually done before passing info to all */
315: XSync(win->disp,False);
316: MPI_Bcast(&win->drw,1,MPI_UNSIGNED_LONG,0,((PetscObject)draw)->comm);
317: return(0);
318: }
320: #include <X11/cursorfont.h>
324: static PetscErrorCode PetscDrawGetMouseButton_X(PetscDraw draw,PetscDrawButton *button,PetscReal* x_user,PetscReal *y_user,PetscReal *x_phys,PetscReal *y_phys)
325: {
326: XEvent report;
327: PetscDraw_X* win = (PetscDraw_X*)draw->data;
328: Window root,child;
329: int root_x,root_y,px,py;
330: unsigned int keys_button;
331: Cursor cursor = 0;
334: /* change cursor to indicate input */
335: if (!cursor) {
336: cursor = XCreateFontCursor(win->disp,XC_hand2);
337: if (!cursor) SETERRQ(PETSC_ERR_LIB,"Unable to create X cursor");
338: }
339: XDefineCursor(win->disp,win->win,cursor);
340: XSelectInput(win->disp,win->win,ButtonPressMask | ButtonReleaseMask);
342: while (XCheckTypedEvent(win->disp,ButtonPress,&report));
343: XMaskEvent(win->disp,ButtonReleaseMask,&report);
344: switch (report.xbutton.button) {
345: case Button1:
346: if (report.xbutton.state & ShiftMask)
347: *button = BUTTON_LEFT_SHIFT;
348: else
349: *button = BUTTON_LEFT;
350: break;
351: case Button2:
352: if (report.xbutton.state & ShiftMask)
353: *button = BUTTON_CENTER_SHIFT;
354: else
355: *button = BUTTON_CENTER;
356: break;
357: case Button3:
358: if (report.xbutton.state & ShiftMask)
359: *button = BUTTON_RIGHT_SHIFT;
360: else
361: *button = BUTTON_RIGHT;
362: break;
363: }
364: XQueryPointer(win->disp,report.xmotion.window,&root,&child,&root_x,&root_y,&px,&py,&keys_button);
366: if (x_phys) *x_phys = ((double)px)/((double)win->w);
367: if (y_phys) *y_phys = 1.0 - ((double)py)/((double)win->h);
369: 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);
370: 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);
372: XUndefineCursor(win->disp,win->win);
373: XFlush(win->disp); XSync(win->disp,False);
374: return(0);
375: }
379: static PetscErrorCode PetscDrawPause_X(PetscDraw draw)
380: {
384: if (draw->pause > 0) PetscSleep(draw->pause);
385: else if (draw->pause < 0) {
386: PetscDrawButton button;
387: PetscMPIInt rank;
388: MPI_Comm_rank(((PetscObject)draw)->comm,&rank);
389: if (!rank) {
390: PetscDrawGetMouseButton(draw,&button,0,0,0,0);
391: if (button == BUTTON_CENTER) draw->pause = 0;
392: }
393: MPI_Bcast(&draw->pause,1,MPI_INT,0,((PetscObject)draw)->comm);
394: }
395: return(0);
396: }
400: static PetscErrorCode PetscDrawGetPopup_X(PetscDraw draw,PetscDraw *popup)
401: {
403: PetscDraw_X* win = (PetscDraw_X*)draw->data;
406: PetscDrawOpenX(((PetscObject)draw)->comm,PETSC_NULL,PETSC_NULL,win->x,win->y+win->h+36,150,220,popup);
407: draw->popup = *popup;
408: return(0);
409: }
413: static PetscErrorCode PetscDrawSetTitle_X(PetscDraw draw,const char title[])
414: {
415: PetscDraw_X *win = (PetscDraw_X*)draw->data;
416: XTextProperty prop;
418: size_t len;
421: XGetWMName(win->disp,win->win,&prop);
422: prop.value = (unsigned char *)title;
423: PetscStrlen(title,&len);
424: prop.nitems = (long) len;
425: XSetWMName(win->disp,win->win,&prop);
426: return(0);
427: }
431: static PetscErrorCode PetscDrawResizeWindow_X(PetscDraw draw,int w,int h)
432: {
433: PetscDraw_X *win = (PetscDraw_X*)draw->data;
434: unsigned int ww,hh,border,depth;
435: int x,y;
437: Window root;
440: XResizeWindow(win->disp,win->win,w,h);
441: XGetGeometry(win->disp,win->win,&root,&x,&y,&ww,&hh,&border,&depth);
442: PetscDrawCheckResizedWindow(draw);
443: return(0);
444: }
448: static PetscErrorCode PetscDrawCheckResizedWindow_X(PetscDraw draw)
449: {
450: PetscDraw_X *win = (PetscDraw_X*)draw->data;
452: int x,y;
453: PetscMPIInt rank;
454: Window root;
455: unsigned int w,h,border,depth,geo[2];
456: PetscReal xl,xr,yl,yr;
457: XRectangle box;
460: MPI_Comm_rank(((PetscObject)draw)->comm,&rank);
461: if (!rank) {
462: XSync(win->disp,False);
463: XGetGeometry(win->disp,win->win,&root,&x,&y,geo,geo+1,&border,&depth);
464: }
465: MPI_Bcast(geo,2,MPI_INT,0,((PetscObject)draw)->comm);
466: w = geo[0];
467: h = geo[1];
468: if (w == (unsigned int) win->w && h == (unsigned int) win->h) return(0);
470: /* record new window sizes */
472: win->h = h; win->w = w;
474: /* Free buffer space and create new version (only first processor does this) */
475: if (win->drw) {
476: win->drw = XCreatePixmap(win->disp,win->win,win->w,win->h,win->depth);
477: }
478: /* reset the clipping */
479: xl = draw->port_xl; yl = draw->port_yl;
480: xr = draw->port_xr; yr = draw->port_yr;
481: box.x = (int)(xl*win->w); box.y = (int)((1.0-yr)*win->h);
482: box.width = (int)((xr-xl)*win->w);box.height = (int)((yr-yl)*win->h);
483: XSetClipRectangles(win->disp,win->gc.set,0,0,&box,1,Unsorted);
485: /* try to make sure it is actually done before passing info to all */
486: XSync(win->disp,False);
487: MPI_Bcast(&win->drw,1,MPI_UNSIGNED_LONG,0,((PetscObject)draw)->comm);
488: return(0);
489: }
491: static PetscErrorCode PetscDrawGetSingleton_X(PetscDraw,PetscDraw*);
492: static PetscErrorCode PetscDrawRestoreSingleton_X(PetscDraw,PetscDraw*);
496: PetscErrorCode PetscDrawDestroy_X(PetscDraw draw)
497: {
498: PetscDraw_X *win = (PetscDraw_X*)draw->data;
502: XFreeGC(win->disp,win->gc.set);
503: XCloseDisplay(win->disp);
504: if (draw->popup) {PetscDrawDestroy(draw->popup);}
505: PetscFree(win->font);
506: PetscFree(win);
507: return(0);
508: }
510: static struct _PetscDrawOps DvOps = { PetscDrawSetDoubleBuffer_X,
511: PetscDrawFlush_X,PetscDrawLine_X,
512: 0,
513: 0,
514: PetscDrawPoint_X,
515: 0,
516: PetscDrawString_X,
517: PetscDrawStringVertical_X,
518: PetscDrawStringSetSize_X,
519: PetscDrawStringGetSize_X,
520: PetscDrawSetViewport_X,
521: PetscDrawClear_X,
522: PetscDrawSynchronizedFlush_X,
523: PetscDrawRectangle_X,
524: PetscDrawTriangle_X,
525: PetscDrawEllipse_X,
526: PetscDrawGetMouseButton_X,
527: PetscDrawPause_X,
528: PetscDrawSynchronizedClear_X,
529: 0,
530: 0,
531: PetscDrawGetPopup_X,
532: PetscDrawSetTitle_X,
533: PetscDrawCheckResizedWindow_X,
534: PetscDrawResizeWindow_X,
535: PetscDrawDestroy_X,
536: 0,
537: PetscDrawGetSingleton_X,
538: PetscDrawRestoreSingleton_X };
541: EXTERN PetscErrorCode XiQuickWindow(PetscDraw_X*,char*,char*,int,int,int,int);
542: EXTERN PetscErrorCode XiQuickWindowFromWindow(PetscDraw_X*,char*,Window);
546: static PetscErrorCode PetscDrawGetSingleton_X(PetscDraw draw,PetscDraw *sdraw)
547: {
549: PetscDraw_X *Xwin = (PetscDraw_X*)draw->data,*sXwin;
553: PetscDrawCreate(PETSC_COMM_SELF,draw->display,draw->title,draw->x,draw->y,draw->w,draw->h,sdraw);
554: PetscObjectChangeTypeName((PetscObject)*sdraw,PETSC_DRAW_X);
555: PetscMemcpy((*sdraw)->ops,&DvOps,sizeof(DvOps));
556: (*sdraw)->ops->destroy = 0;
558: (*sdraw)->pause = draw->pause;
559: (*sdraw)->coor_xl = draw->coor_xl;
560: (*sdraw)->coor_xr = draw->coor_xr;
561: (*sdraw)->coor_yl = draw->coor_yl;
562: (*sdraw)->coor_yr = draw->coor_yr;
563: (*sdraw)->port_xl = draw->port_xl;
564: (*sdraw)->port_xr = draw->port_xr;
565: (*sdraw)->port_yl = draw->port_yl;
566: (*sdraw)->port_yr = draw->port_yr;
567: (*sdraw)->popup = draw->popup;
569: /* actually create and open the window */
570: PetscNew(PetscDraw_X,&sXwin);
571: XiQuickWindowFromWindow(sXwin,draw->display,Xwin->win);
573: sXwin->x = Xwin->x;
574: sXwin->y = Xwin->y;
575: sXwin->w = Xwin->w;
576: sXwin->h = Xwin->h;
577: (*sdraw)->data = (void*)sXwin;
578: return(0);
579: }
583: static PetscErrorCode PetscDrawRestoreSingleton_X(PetscDraw draw,PetscDraw *sdraw)
584: {
586: PetscDraw_X *sXwin = (PetscDraw_X*)(*sdraw)->data;
589: XFreeGC(sXwin->disp,sXwin->gc.set);
590: XCloseDisplay(sXwin->disp);
591: if ((*sdraw)->popup) {PetscDrawDestroy((*sdraw)->popup);}
592: PetscStrfree((*sdraw)->title);
593: PetscStrfree((*sdraw)->display);
594: PetscFree(sXwin->font);
595: PetscFree(sXwin);
596: PetscHeaderDestroy(*sdraw);
597: return(0);
598: }
602: PetscErrorCode PetscDrawXGetDisplaySize_Private(const char name[],int *width,int *height)
603: {
604: Display *display;
607: display = XOpenDisplay(name);
608: if (!display) {
609: *width = 0;
610: *height = 0;
611: SETERRQ1(PETSC_ERR_LIB,"Unable to open display on %s\n. Make sure your COMPUTE NODES are authorized to connect \n\
612: to this X server and either your DISPLAY variable\n\
613: is set or you use the -display name option\n",name);
614: }
615: *width = DisplayWidth(display,0);
616: *height = DisplayHeight(display,0);
617: XCloseDisplay(display);
618: return(0);
619: }
624: PetscErrorCode PetscDrawCreate_X(PetscDraw draw)
625: {
626: PetscDraw_X *Xwin;
628: PetscMPIInt rank;
629: PetscInt xywh[4],osize = 4;
630: int x = draw->x,y = draw->y,w = draw->w,h = draw->h;
631: static int xavailable = 0,yavailable = 0,xmax = 0,ymax = 0,ybottom = 0;
632: PetscTruth flg;
635: if (!draw->display) {
636: PetscMalloc(128*sizeof(char),&draw->display);
637: PetscGetDisplay(draw->display,128);
638: }
640: /*
641: Initialize the display size
642: */
643: if (!xmax) {
644: PetscDrawXGetDisplaySize_Private(draw->display,&xmax,&ymax);
645: /* if some processors fail on this and others succed then this is a problem ! */
646: if (ierr) {
647: (*PetscErrorPrintf)("PETSc unable to use X windows\nproceeding without graphics\n");
648: PetscDrawSetType(draw,PETSC_DRAW_NULL);
649: return(0);
650: }
651: }
653: if (w == PETSC_DECIDE) w = draw->w = 300;
654: if (h == PETSC_DECIDE) h = draw->h = 300;
655: switch (w) {
656: case PETSC_DRAW_FULL_SIZE: w = draw->w = xmax - 10;
657: break;
658: case PETSC_DRAW_HALF_SIZE: w = draw->w = (xmax - 20)/2;
659: break;
660: case PETSC_DRAW_THIRD_SIZE: w = draw->w = (xmax - 30)/3;
661: break;
662: case PETSC_DRAW_QUARTER_SIZE: w = draw->w = (xmax - 40)/4;
663: break;
664: }
665: switch (h) {
666: case PETSC_DRAW_FULL_SIZE: h = draw->h = ymax - 10;
667: break;
668: case PETSC_DRAW_HALF_SIZE: h = draw->h = (ymax - 20)/2;
669: break;
670: case PETSC_DRAW_THIRD_SIZE: h = draw->h = (ymax - 30)/3;
671: break;
672: case PETSC_DRAW_QUARTER_SIZE: h = draw->h = (ymax - 40)/4;
673: break;
674: }
676: /* allow user to set location and size of window */
677: xywh[0] = x; xywh[1] = y; xywh[2] = w; xywh[3] = h;
678: PetscOptionsGetIntArray(PETSC_NULL,"-geometry",xywh,&osize,PETSC_NULL);
679: x = (int) xywh[0]; y = (int) xywh[1]; w = (int) xywh[2]; h = (int) xywh[3];
682: if (draw->x == PETSC_DECIDE || draw->y == PETSC_DECIDE) {
683: /*
684: PETSc tries to place windows starting in the upper left corner and
685: moving across to the right.
686:
687: --------------------------------------------
688: | Region used so far +xavailable,yavailable |
689: | + |
690: | + |
691: |++++++++++++++++++++++ybottom |
692: | |
693: | |
694: |--------------------------------------------|
695: */
696: /* First: can we add it to the right? */
697: if (xavailable+w+10 <= xmax) {
698: x = xavailable;
699: y = yavailable;
700: ybottom = PetscMax(ybottom,y + h + 30);
701: } else {
702: /* No, so add it below on the left */
703: xavailable = 0;
704: x = 0;
705: yavailable = ybottom;
706: y = ybottom;
707: ybottom = ybottom + h + 30;
708: }
709: }
710: /* update available region */
711: xavailable = PetscMax(xavailable,x + w + 10);
712: if (xavailable >= xmax) {
713: xavailable = 0;
714: yavailable = yavailable + h + 30;
715: ybottom = yavailable;
716: }
717: if (yavailable >= ymax) {
718: y = 0;
719: yavailable = 0;
720: ybottom = 0;
721: }
723: PetscMemcpy(draw->ops,&DvOps,sizeof(DvOps));
725: /* actually create and open the window */
726: PetscNew(PetscDraw_X,&Xwin);
727: PetscLogObjectMemory(draw,sizeof(PetscDraw_X));
728: MPI_Comm_rank(((PetscObject)draw)->comm,&rank);
730: if (!rank) {
731: if (x < 0 || y < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Negative corner of window");
732: if (w <= 0 || h <= 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Negative window width or height");
733: XiQuickWindow(Xwin,draw->display,draw->title,x,y,w,h);
734: MPI_Bcast(&Xwin->win,1,MPI_UNSIGNED_LONG,0,((PetscObject)draw)->comm);
735: } else {
736: unsigned long win = 0;
737: MPI_Bcast(&win,1,MPI_UNSIGNED_LONG,0,((PetscObject)draw)->comm);
738: XiQuickWindowFromWindow(Xwin,draw->display,win);
739: }
741: Xwin->x = x;
742: Xwin->y = y;
743: Xwin->w = w;
744: Xwin->h = h;
745: draw->data = (void*)Xwin;
747: /*
748: Need barrier here so processor 0 does not destroy the window before other
749: processors have completed XiQuickWindow()
750: */
751: PetscDrawClear(draw);
752: PetscDrawSynchronizedFlush(draw);
754: PetscOptionsHasName(PETSC_NULL,"-draw_double_buffer",&flg);
755: if (flg) {
756: PetscDrawSetDoubleBuffer(draw);
757: }
759: return(0);
760: }