xref: /petsc/src/sys/classes/draw/impls/x/xops.c (revision e611a964e9853b74d61a56642fe9d06a6e51780f)
1 /*
2     Defines the operations for the X PetscDraw implementation.
3 */
4 
5 #include <../src/sys/classes/draw/impls/x/ximpl.h>         /*I  "petscsys.h" I*/
6 
7 /*
8      These macros transform from the users coordinates to the  X-window pixel coordinates.
9 */
10 #define XTRANS(draw,xwin,x)  ((int)(((xwin)->w-1)*((draw)->port_xl + (((x - (draw)->coor_xl)*((draw)->port_xr - (draw)->port_xl))/((draw)->coor_xr - (draw)->coor_xl)))))
11 #define YTRANS(draw,xwin,y)  (((xwin)->h-1) - (int)(((xwin)->h-1)*((draw)->port_yl + (((y - (draw)->coor_yl)*((draw)->port_yr - (draw)->port_yl))/((draw)->coor_yr - (draw)->coor_yl)))))
12 
13 #define ITRANS(draw,xwin,i)  ((draw)->coor_xl + (((PetscReal)(i))*((draw)->coor_xr - (draw)->coor_xl)/((xwin)->w-1) - (draw)->port_xl)/((draw)->port_xr - (draw)->port_xl))
14 #define JTRANS(draw,xwin,j)  ((draw)->coor_yl + (((PetscReal)(j))/((xwin)->h-1) + (draw)->port_yl - 1)*((draw)->coor_yr - (draw)->coor_yl)/((draw)->port_yl - (draw)->port_yr))
15 
16 
17 #undef __FUNCT__
18 #define __FUNCT__ "PetscDrawSetViewport_X"
19 static PetscErrorCode PetscDrawSetViewport_X(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr)
20 {
21   PetscDraw_X    *XiWin = (PetscDraw_X*)draw->data;
22   int            xa,ya,xb,yb,xmax = XiWin->w-1,ymax = XiWin->h-1;
23   XRectangle     box;
24   PetscErrorCode ierr;
25 
26   PetscFunctionBegin;
27   xa = (int)(xl*xmax); ya = ymax - (int)(yr*ymax);
28   xb = (int)(xr*xmax); yb = ymax - (int)(yl*ymax);
29   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
30   box.x = (short)xa; box.width  = (unsigned short)(xb + 1 - xa);
31   box.y = (short)ya; box.height = (unsigned short)(yb + 1 - ya);
32   XSetClipRectangles(XiWin->disp,XiWin->gc.set,0,0,&box,1,Unsorted);
33   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
34   PetscFunctionReturn(0);
35 }
36 
37 #undef __FUNCT__
38 #define __FUNCT__ "PetscDrawCoordinateToPixel_X"
39 static PetscErrorCode PetscDrawCoordinateToPixel_X(PetscDraw draw,PetscReal x,PetscReal y,int *i,int *j)
40 {
41   PetscDraw_X *XiWin = (PetscDraw_X*)draw->data;
42 
43   PetscFunctionBegin;
44   *i = XTRANS(draw,XiWin,x);
45   *j = YTRANS(draw,XiWin,y);
46   PetscFunctionReturn(0);
47 }
48 
49 #undef __FUNCT__
50 #define __FUNCT__ "PetscDrawPixelToCoordinate_X"
51 static PetscErrorCode PetscDrawPixelToCoordinate_X(PetscDraw draw,int i,int j,PetscReal *x,PetscReal *y)
52 {
53   PetscDraw_X *XiWin = (PetscDraw_X*)draw->data;
54 
55   PetscFunctionBegin;
56   *x = ITRANS(draw,XiWin,i);
57   *y = JTRANS(draw,XiWin,j);
58   PetscFunctionReturn(0);
59 }
60 
61 #undef __FUNCT__
62 #define __FUNCT__ "PetscDrawPoint_X"
63 static PetscErrorCode PetscDrawPoint_X(PetscDraw draw,PetscReal x,PetscReal y,int c)
64 {
65   PetscDraw_X *XiWin = (PetscDraw_X*)draw->data;
66   int         xx,yy,i,j;
67 
68   PetscFunctionBegin;
69   xx = XTRANS(draw,XiWin,x);
70   yy = YTRANS(draw,XiWin,y);
71   PetscDrawXiSetColor(XiWin,c);
72   for (i=-1; i<2; i++) {
73     for (j=-1; j<2; j++) {
74       XDrawPoint(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,xx+i,yy+j);
75     }
76   }
77   PetscFunctionReturn(0);
78 }
79 
80 #undef __FUNCT__
81 #define __FUNCT__ "PetscDrawPointPixel_X"
82 static PetscErrorCode PetscDrawPointPixel_X(PetscDraw draw,int x,int y,int c)
83 {
84   PetscDraw_X *XiWin = (PetscDraw_X*)draw->data;
85 
86   PetscFunctionBegin;
87   PetscDrawXiSetColor(XiWin,c);
88   XDrawPoint(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x,y);
89   PetscFunctionReturn(0);
90 }
91 
92 #undef __FUNCT__
93 #define __FUNCT__ "PetscDrawLine_X"
94 static PetscErrorCode PetscDrawLine_X(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr,int cl)
95 {
96   PetscDraw_X *XiWin = (PetscDraw_X*)draw->data;
97   int         x_1,y_1,x_2,y_2;
98 
99   PetscFunctionBegin;
100   PetscDrawXiSetColor(XiWin,cl);
101   x_1 = XTRANS(draw,XiWin,xl); x_2  = XTRANS(draw,XiWin,xr);
102   y_1 = YTRANS(draw,XiWin,yl); y_2  = YTRANS(draw,XiWin,yr);
103   XDrawLine(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x_1,y_1,x_2,y_2);
104   PetscFunctionReturn(0);
105 }
106 
107 #undef __FUNCT__
108 #define __FUNCT__ "PetscDrawArrow_X"
109 static PetscErrorCode PetscDrawArrow_X(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr,int cl)
110 {
111   PetscDraw_X *XiWin = (PetscDraw_X*)draw->data;
112   int         x_1,y_1,x_2,y_2;
113 
114   PetscFunctionBegin;
115   PetscDrawXiSetColor(XiWin,cl);
116   x_1 = XTRANS(draw,XiWin,xl); x_2 = XTRANS(draw,XiWin,xr);
117   y_1 = YTRANS(draw,XiWin,yl); y_2 = YTRANS(draw,XiWin,yr);
118   XDrawLine(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x_1,y_1,x_2,y_2);
119   if (x_1 == x_2 && y_1 == y_2) PetscFunctionReturn(0);
120   if (x_1 == x_2 && PetscAbs(y_1 - y_2) > 7) {
121     if (y_2 > y_1) {
122       XDrawLine(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x_2,y_2,x_2-3,y_2-3);
123       XDrawLine(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x_2,y_2,x_2+3,y_2-3);
124     } else {
125       XDrawLine(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x_2,y_2,x_2-3,y_2+3);
126       XDrawLine(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x_2,y_2,x_2+3,y_2+3);
127     }
128   }
129   if (y_1 == y_2 && PetscAbs(x_1 - x_2) > 7) {
130     if (x_2 > x_1) {
131       XDrawLine(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x_2-3,y_2-3,x_2,y_2);
132       XDrawLine(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x_2-3,y_2+3,x_2,y_2);
133     } else {
134       XDrawLine(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x_2,y_2,x_2+3,y_2-3);
135       XDrawLine(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x_2,y_2,x_2+3,y_2+3);
136     }
137   }
138   PetscFunctionReturn(0);
139 }
140 
141 #undef __FUNCT__
142 #define __FUNCT__ "PetscDrawRectangle_X"
143 static PetscErrorCode PetscDrawRectangle_X(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr,int c1,int c2,int c3,int c4)
144 {
145   PetscDraw_X *XiWin = (PetscDraw_X*)draw->data;
146   int         x,y,w,h,c = (c1 + c2 + c3 + c4)/4;
147 
148   PetscFunctionBegin;
149   PetscDrawXiSetColor(XiWin,c);
150   x = XTRANS(draw,XiWin,xl); w = XTRANS(draw,XiWin,xr) + 1 - x; if (w <= 0) w = 1;
151   y = YTRANS(draw,XiWin,yr); h = YTRANS(draw,XiWin,yl) + 1 - y; if (h <= 0) h = 1;
152   XFillRectangle(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x,y,w,h);
153   PetscFunctionReturn(0);
154 }
155 
156 #undef __FUNCT__
157 #define __FUNCT__ "PetscDrawEllipse_X"
158 static PetscErrorCode PetscDrawEllipse_X(PetscDraw draw,PetscReal x,PetscReal y,PetscReal a,PetscReal b,int c)
159 {
160   PetscDraw_X *XiWin = (PetscDraw_X*)draw->data;
161   int         xA,yA,w,h;
162 
163   PetscFunctionBegin;
164   PetscDrawXiSetColor(XiWin, c);
165   xA = XTRANS(draw,XiWin, x - a/2); w = XTRANS(draw,XiWin, x + a/2) + 1 - xA; w = PetscAbs(w);
166   yA = YTRANS(draw,XiWin, y + b/2); h = YTRANS(draw,XiWin, y - b/2) + 1 - yA; h = PetscAbs(h);
167   XFillArc(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,xA,yA,w,h,0,360*64);
168   PetscFunctionReturn(0);
169 }
170 
171 PETSC_INTERN PetscErrorCode PetscDrawInterpolatedTriangle_X(PetscDraw_X*,int,int,int,int,int,int,int,int,int);
172 
173 #undef __FUNCT__
174 #define __FUNCT__ "PetscDrawTriangle_X"
175 static PetscErrorCode PetscDrawTriangle_X(PetscDraw draw,PetscReal X1,PetscReal Y_1,PetscReal X2,PetscReal Y2,PetscReal X3,PetscReal Y3,int c1,int c2,int c3)
176 {
177   PetscDraw_X    *XiWin = (PetscDraw_X*)draw->data;
178   PetscErrorCode ierr;
179 
180   PetscFunctionBegin;
181   if (c1 == c2 && c2 == c3) {
182     XPoint pt[3];
183     PetscDrawXiSetColor(XiWin,c1);
184     pt[0].x = XTRANS(draw,XiWin,X1);
185     pt[0].y = YTRANS(draw,XiWin,Y_1);
186     pt[1].x = XTRANS(draw,XiWin,X2);
187     pt[1].y = YTRANS(draw,XiWin,Y2);
188     pt[2].x = XTRANS(draw,XiWin,X3);
189     pt[2].y = YTRANS(draw,XiWin,Y3);
190     XFillPolygon(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,pt,3,Convex,CoordModeOrigin);
191   } else {
192     int x1,y_1,x2,y2,x3,y3;
193     x1   = XTRANS(draw,XiWin,X1);
194     y_1  = YTRANS(draw,XiWin,Y_1);
195     x2   = XTRANS(draw,XiWin,X2);
196     y2   = YTRANS(draw,XiWin,Y2);
197     x3   = XTRANS(draw,XiWin,X3);
198     y3   = YTRANS(draw,XiWin,Y3);
199     ierr = PetscDrawInterpolatedTriangle_X(XiWin,x1,y_1,c1,x2,y2,c2,x3,y3,c3);CHKERRQ(ierr);
200   }
201   PetscFunctionReturn(0);
202 }
203 
204 #undef __FUNCT__
205 #define __FUNCT__ "PetscDrawStringSetSize_X"
206 static PetscErrorCode PetscDrawStringSetSize_X(PetscDraw draw,PetscReal x,PetscReal y)
207 {
208   PetscDraw_X    *XiWin = (PetscDraw_X*)draw->data;
209   int            w,h;
210   PetscErrorCode ierr;
211 
212   PetscFunctionBegin;
213   w    = (int)((XiWin->w)*x*(draw->port_xr - draw->port_xl)/(draw->coor_xr - draw->coor_xl));
214   h    = (int)((XiWin->h)*y*(draw->port_yr - draw->port_yl)/(draw->coor_yr - draw->coor_yl));
215   ierr = PetscFree(XiWin->font);CHKERRQ(ierr);
216   ierr = PetscDrawXiFontFixed(XiWin,w,h,&XiWin->font);CHKERRQ(ierr);
217   PetscFunctionReturn(0);
218 }
219 
220 #undef __FUNCT__
221 #define __FUNCT__ "PetscDrawStringGetSize_X"
222 static PetscErrorCode PetscDrawStringGetSize_X(PetscDraw draw,PetscReal *x,PetscReal  *y)
223 {
224   PetscDraw_X *XiWin = (PetscDraw_X*)draw->data;
225   PetscReal   w,h;
226 
227   PetscFunctionBegin;
228   w = XiWin->font->font_w; h = XiWin->font->font_h;
229   if (x) *x = w*(draw->coor_xr - draw->coor_xl)/((XiWin->w)*(draw->port_xr - draw->port_xl));
230   if (y) *y = h*(draw->coor_yr - draw->coor_yl)/((XiWin->h)*(draw->port_yr - draw->port_yl));
231   PetscFunctionReturn(0);
232 }
233 
234 #undef __FUNCT__
235 #define __FUNCT__ "PetscDrawString_X"
236 static PetscErrorCode PetscDrawString_X(PetscDraw draw,PetscReal x,PetscReal y,int c,const char chrs[])
237 {
238   PetscDraw_X    *XiWin = (PetscDraw_X*)draw->data;
239   int            xx,yy,descent = XiWin->font->font_descent;
240   size_t         len;
241   char           *substr;
242   PetscToken     token;
243   PetscErrorCode ierr;
244 
245   PetscFunctionBegin;
246   xx = XTRANS(draw,XiWin,x);
247   yy = YTRANS(draw,XiWin,y);
248   PetscDrawXiSetColor(XiWin,c);
249 
250   ierr = PetscTokenCreate(chrs,'\n',&token);CHKERRQ(ierr);
251   ierr = PetscTokenFind(token,&substr);CHKERRQ(ierr);
252   while (substr) {
253     ierr = PetscStrlen(substr,&len);CHKERRQ(ierr);
254     XDrawString(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,xx,yy-descent,substr,len);
255     yy  += XiWin->font->font_h;
256     ierr = PetscTokenFind(token,&substr);CHKERRQ(ierr);
257   }
258   ierr = PetscTokenDestroy(&token);CHKERRQ(ierr);
259   PetscFunctionReturn(0);
260 }
261 
262 #undef __FUNCT__
263 #define __FUNCT__ "PetscDrawStringVertical_X"
264 static PetscErrorCode PetscDrawStringVertical_X(PetscDraw draw,PetscReal x,PetscReal y,int c,const char text[])
265 {
266   PetscDraw_X    *XiWin = (PetscDraw_X*)draw->data;
267   int            xx,yy,offset = XiWin->font->font_h - XiWin->font->font_descent;
268   char           chr[2] = {0, 0};
269 
270   PetscFunctionBegin;
271   xx = XTRANS(draw,XiWin,x);
272   yy = YTRANS(draw,XiWin,y);
273   PetscDrawXiSetColor(XiWin,c);
274   while ((chr[0] = *text++)) {
275     XDrawString(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,xx,yy+offset,chr,1);
276     yy += XiWin->font->font_h;
277   }
278   PetscFunctionReturn(0);
279 }
280 
281 #undef __FUNCT__
282 #define __FUNCT__ "PetscDrawFlush_X"
283 static PetscErrorCode PetscDrawFlush_X(PetscDraw draw)
284 {
285   PetscDraw_X    *XiWin = (PetscDraw_X*)draw->data;
286   PetscMPIInt    rank;
287   PetscErrorCode ierr;
288 
289   PetscFunctionBegin;
290   /* make sure the X server processed requests from all processes */
291   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
292   XSync(XiWin->disp,False);
293   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
294   ierr = MPI_Barrier(PetscObjectComm((PetscObject)draw));CHKERRQ(ierr);
295 
296   /* transfer pixmap contents to window (only the first process does this) */
297   if (XiWin->drw && XiWin->win) {
298     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)draw),&rank);CHKERRQ(ierr);
299     ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
300     if (!rank) XCopyArea(XiWin->disp,XiWin->drw,XiWin->win,XiWin->gc.set,0,0,XiWin->w,XiWin->h,0,0);
301     if (!rank) XSync(XiWin->disp,False);
302     ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
303     ierr = MPI_Barrier(PetscObjectComm((PetscObject)draw));CHKERRQ(ierr);
304   }
305   PetscFunctionReturn(0);
306 }
307 
308 #undef __FUNCT__
309 #define __FUNCT__ "PetscDrawClear_X"
310 static PetscErrorCode PetscDrawClear_X(PetscDraw draw)
311 {
312   PetscDraw_X    *XiWin = (PetscDraw_X*)draw->data;
313   int            xmax = XiWin->w-1,  ymax = XiWin->h-1;
314   PetscReal      xl = draw->port_xl, yl = draw->port_yl;
315   PetscReal      xr = draw->port_xr, yr = draw->port_yr;
316   PetscMPIInt    rank;
317   PetscErrorCode ierr;
318 
319   PetscFunctionBegin;
320   /* make sure the X server processed requests from all processes */
321   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
322   XSync(XiWin->disp,False);
323   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
324   ierr = MPI_Barrier(PetscObjectComm((PetscObject)draw));CHKERRQ(ierr);
325 
326   /* only the first process handles the clearing business */
327   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
328   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)draw),&rank);CHKERRQ(ierr);
329   if (!rank) {
330     int xa = (int)(xl*xmax), ya = ymax - (int)(yr*ymax);
331     int xb = (int)(xr*xmax), yb = ymax - (int)(yl*ymax);
332     unsigned int w = (unsigned int)(xb + 1 - xa);
333     unsigned int h = (unsigned int)(yb + 1 - ya);
334     PetscDrawXiSetPixVal(XiWin,XiWin->background);
335     XFillRectangle(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,xa,ya,w,h);
336     XSync(XiWin->disp,False);
337   }
338   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
339   ierr = MPI_Barrier(PetscObjectComm((PetscObject)draw));CHKERRQ(ierr);
340   PetscFunctionReturn(0);
341 }
342 
343 #undef __FUNCT__
344 #define __FUNCT__ "PetscDrawSetDoubleBuffer_X"
345 static PetscErrorCode PetscDrawSetDoubleBuffer_X(PetscDraw draw)
346 {
347   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
348   PetscMPIInt    rank;
349   PetscErrorCode ierr;
350 
351   PetscFunctionBegin;
352   if (win->drw) PetscFunctionReturn(0);
353   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)draw),&rank);CHKERRQ(ierr);
354 
355   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
356   if (!rank) {ierr = PetscDrawXiQuickPixmap(win);CHKERRQ(ierr);}
357   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
358   ierr = MPI_Bcast(&win->drw,1,MPI_UNSIGNED_LONG,0,PetscObjectComm((PetscObject)draw));CHKERRQ(ierr);
359   PetscFunctionReturn(0);
360 }
361 
362 #undef __FUNCT__
363 #define __FUNCT__ "PetscDrawGetPopup_X"
364 static PetscErrorCode PetscDrawGetPopup_X(PetscDraw draw,PetscDraw *popup)
365 {
366   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
367   PetscBool      flg  = PETSC_TRUE;
368   PetscErrorCode ierr;
369 
370   PetscFunctionBegin;
371   ierr = PetscOptionsGetBool(((PetscObject)draw)->options,((PetscObject)draw)->prefix,"-draw_popup",&flg,NULL);CHKERRQ(ierr);
372   if (!flg || !win->win) {*popup = NULL; PetscFunctionReturn(0);}
373 
374   ierr = PetscDrawCreate(PetscObjectComm((PetscObject)draw),draw->display,NULL,win->x,win->y+win->h+10,220,220,popup);CHKERRQ(ierr);
375   ierr = PetscObjectSetOptionsPrefix((PetscObject)*popup,"popup_");CHKERRQ(ierr);
376   ierr = PetscObjectAppendOptionsPrefix((PetscObject)*popup,((PetscObject)draw)->prefix);CHKERRQ(ierr);
377   ierr = PetscDrawSetType(*popup,PETSC_DRAW_X);CHKERRQ(ierr);
378   draw->popup = *popup;
379   PetscFunctionReturn(0);
380 }
381 
382 #undef __FUNCT__
383 #define __FUNCT__ "PetscDrawSetTitle_X"
384 static PetscErrorCode PetscDrawSetTitle_X(PetscDraw draw,const char title[])
385 {
386   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
387   PetscMPIInt    rank;
388   PetscErrorCode ierr;
389 
390   PetscFunctionBegin;
391   if (!win->win) PetscFunctionReturn(0);
392   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)draw),&rank);CHKERRQ(ierr);
393   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
394   if (!rank) {
395     size_t        len;
396     XTextProperty prop;
397     ierr = PetscStrlen(title,&len);CHKERRQ(ierr);
398     XGetWMName(win->disp,win->win,&prop);
399     XFree((void*)prop.value);
400     prop.value  = (unsigned char*)title;
401     prop.nitems = (long)len;
402     XSetWMName(win->disp,win->win,&prop);
403   }
404   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
405   PetscFunctionReturn(0);
406 }
407 
408 #undef __FUNCT__
409 #define __FUNCT__ "PetscDrawCheckResizedWindow_X"
410 static PetscErrorCode PetscDrawCheckResizedWindow_X(PetscDraw draw)
411 {
412   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
413   int            xywh[4];
414   PetscMPIInt    rank;
415   PetscErrorCode ierr;
416 
417   PetscFunctionBegin;
418   if (!win->win) PetscFunctionReturn(0);
419   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)draw),&rank);CHKERRQ(ierr);
420 
421   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
422   if (!rank) {ierr = PetscDrawXiGetGeometry(win,xywh,xywh+1,xywh+2,xywh+3);CHKERRQ(ierr);}
423   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
424   ierr = MPI_Bcast(xywh,4,MPI_INT,0,PetscObjectComm((PetscObject)draw));CHKERRQ(ierr);
425 
426   /* record new window position */
427   draw->x = win->x = xywh[0];
428   draw->y = win->y = xywh[1];
429   if (xywh[2] == win->w && xywh[3] == win->h) PetscFunctionReturn(0);
430   /* record new window sizes */
431   draw->w = win->w = xywh[2];
432   draw->h = win->h = xywh[3];
433 
434   /* recreate pixmap (only first processor does this) */
435   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
436   if (!rank && win->drw) {ierr = PetscDrawXiQuickPixmap(win);CHKERRQ(ierr);}
437   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
438   ierr = MPI_Bcast(&win->drw,1,MPI_UNSIGNED_LONG,0,PetscObjectComm((PetscObject)draw));CHKERRQ(ierr);
439   /* reset the clipping */
440   ierr = PetscDrawSetViewport_X(draw,draw->port_xl,draw->port_yl,draw->port_xr,draw->port_yr);CHKERRQ(ierr);
441   PetscFunctionReturn(0);
442 }
443 
444 #undef __FUNCT__
445 #define __FUNCT__ "PetscDrawResizeWindow_X"
446 static PetscErrorCode PetscDrawResizeWindow_X(PetscDraw draw,int w,int h)
447 {
448   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
449   PetscMPIInt    rank;
450   PetscErrorCode ierr;
451 
452   PetscFunctionBegin;
453   if (w == win->w && h == win->h) PetscFunctionReturn(0);
454   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)draw),&rank);CHKERRQ(ierr);
455 
456   if (win->win) {
457     ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
458     if (!rank) {ierr = PetscDrawXiResizeWindow(win,w,h);CHKERRQ(ierr);}
459     ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
460     ierr = PetscDrawCheckResizedWindow_X(draw);CHKERRQ(ierr);
461   } else if (win->drw) {
462     draw->w = win->w = w; draw->h = win->h = h;
463     /* recreate pixmap (only first processor does this) */
464     ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
465     if (!rank) {ierr = PetscDrawXiQuickPixmap(win);CHKERRQ(ierr);}
466     ierr = MPI_Bcast(&win->drw,1,MPI_UNSIGNED_LONG,0,PetscObjectComm((PetscObject)draw));CHKERRQ(ierr);
467     /* reset the clipping */
468     ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
469     ierr = PetscDrawSetViewport_X(draw,draw->port_xl,draw->port_yl,draw->port_xr,draw->port_yr);CHKERRQ(ierr);
470   }
471   PetscFunctionReturn(0);
472 }
473 
474 #include <X11/cursorfont.h>
475 
476 #undef __FUNCT__
477 #define __FUNCT__ "PetscDrawGetMouseButton_X"
478 static PetscErrorCode PetscDrawGetMouseButton_X(PetscDraw draw,PetscDrawButton *button,PetscReal *x_user,PetscReal *y_user,PetscReal *x_phys,PetscReal *y_phys)
479 {
480   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
481   Cursor         cursor;
482   XEvent         report;
483   Window         root,child;
484   int            root_x,root_y,px=0,py=0;
485   unsigned int   w,h,border,depth;
486   unsigned int   keys_button;
487   PetscMPIInt    rank;
488   PetscReal      xx,yy;
489   PetscErrorCode ierr;
490 
491   PetscFunctionBegin;
492   *button = PETSC_BUTTON_NONE;
493   if (!win->win) PetscFunctionReturn(0);
494   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)draw),&rank);CHKERRQ(ierr);
495 
496   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
497   if (rank) goto finally;
498 
499   /* change cursor to indicate input */
500   cursor = XCreateFontCursor(win->disp,XC_hand2); if (!cursor) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Unable to create X cursor");
501   XDefineCursor(win->disp,win->win,cursor);
502   /* wait for mouse button events */
503   XSelectInput(win->disp,win->win,ButtonPressMask|ButtonReleaseMask);
504   while (XCheckTypedEvent(win->disp,ButtonPress,&report));
505   XMaskEvent(win->disp,ButtonReleaseMask,&report);
506   /* get mouse pointer coordinates */
507   XQueryPointer(win->disp,report.xmotion.window,&root,&child,&root_x,&root_y,&px,&py,&keys_button);
508   /* the user may resize the window before pressing the mouse button */
509   XGetGeometry(win->disp,win->win,&root,&root_x,&root_y,&w,&h,&border,&depth);
510   /* cleanup input event handler and cursor  */
511   XSelectInput(win->disp,win->win,NoEventMask);
512   XUndefineCursor(win->disp,win->win);
513   XFreeCursor(win->disp, cursor);
514   XSync(win->disp,False);
515 
516   switch (report.xbutton.button) {
517   case Button1: *button = PETSC_BUTTON_LEFT; break;
518   case Button2: *button = PETSC_BUTTON_CENTER; break;
519   case Button3: *button = PETSC_BUTTON_RIGHT; break;
520   case Button4: *button = PETSC_BUTTON_WHEEL_UP; break;
521   case Button5: *button = PETSC_BUTTON_WHEEL_DOWN; break;
522   }
523   if (report.xbutton.state & ShiftMask) {
524     switch (report.xbutton.button) {
525     case Button1: *button = PETSC_BUTTON_LEFT_SHIFT; break;
526     case Button2: *button = PETSC_BUTTON_CENTER_SHIFT; break;
527     case Button3: *button = PETSC_BUTTON_RIGHT_SHIFT; break;
528     }
529   }
530   xx = ((PetscReal)px)/w;
531   yy = 1 - ((PetscReal)py)/h;
532   if (x_user) *x_user = draw->coor_xl + (xx - draw->port_xl)*(draw->coor_xr - draw->coor_xl)/(draw->port_xr - draw->port_xl);
533   if (y_user) *y_user = draw->coor_yl + (yy - draw->port_yl)*(draw->coor_yr - draw->coor_yl)/(draw->port_yr - draw->port_yl);
534   if (x_phys) *x_phys = xx;
535   if (y_phys) *y_phys = yy;
536 
537 finally:
538   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
539   ierr = PetscDrawCheckResizedWindow_X(draw);CHKERRQ(ierr);
540   PetscFunctionReturn(0);
541 }
542 
543 #undef __FUNCT__
544 #define __FUNCT__ "PetscDrawPause_X"
545 static PetscErrorCode PetscDrawPause_X(PetscDraw draw)
546 {
547   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
548   PetscErrorCode ierr;
549 
550   PetscFunctionBegin;
551   if (!win->win) PetscFunctionReturn(0);
552   if (draw->pause > 0) PetscSleep(draw->pause);
553   else if (draw->pause == -1) {
554     PetscDrawButton button = PETSC_BUTTON_NONE;
555     ierr = PetscDrawGetMouseButton(draw,&button,NULL,NULL,NULL,NULL);CHKERRQ(ierr);
556     if (button == PETSC_BUTTON_CENTER) draw->pause = 0;
557   }
558   PetscFunctionReturn(0);
559 }
560 
561 #undef __FUNCT__
562 #define __FUNCT__ "PetscDrawDestroy_X"
563 static PetscErrorCode PetscDrawDestroy_X(PetscDraw draw)
564 {
565   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
566   PetscErrorCode ierr;
567 
568   PetscFunctionBegin;
569   ierr = PetscDrawDestroy(&draw->popup);CHKERRQ(ierr);
570   ierr = PetscDrawXiClose(win);CHKERRQ(ierr);
571   ierr = PetscFree(draw->data);CHKERRQ(ierr);
572   PetscFunctionReturn(0);
573 }
574 
575 static       PetscErrorCode PetscDrawGetSingleton_X(PetscDraw,PetscDraw*);
576 static       PetscErrorCode PetscDrawRestoreSingleton_X(PetscDraw,PetscDraw*);
577 PETSC_INTERN PetscErrorCode PetscDrawGetImage_X(PetscDraw,unsigned char[][3],unsigned int*,unsigned int*,unsigned char*[]);
578 
579 static struct _PetscDrawOps DvOps = { PetscDrawSetDoubleBuffer_X,
580                                       PetscDrawFlush_X,
581                                       PetscDrawLine_X,
582                                       0,
583                                       0,
584                                       PetscDrawPoint_X,
585                                       0,
586                                       PetscDrawString_X,
587                                       PetscDrawStringVertical_X,
588                                       PetscDrawStringSetSize_X,
589                                       PetscDrawStringGetSize_X,
590                                       PetscDrawSetViewport_X,
591                                       PetscDrawClear_X,
592                                       PetscDrawRectangle_X,
593                                       PetscDrawTriangle_X,
594                                       PetscDrawEllipse_X,
595                                       PetscDrawGetMouseButton_X,
596                                       PetscDrawPause_X,
597                                       0,
598                                       0,
599                                       PetscDrawGetPopup_X,
600                                       PetscDrawSetTitle_X,
601                                       PetscDrawCheckResizedWindow_X,
602                                       PetscDrawResizeWindow_X,
603                                       PetscDrawDestroy_X,
604                                       0,
605                                       PetscDrawGetSingleton_X,
606                                       PetscDrawRestoreSingleton_X,
607                                       0,
608                                       PetscDrawGetImage_X,
609                                       0,
610                                       PetscDrawArrow_X,
611                                       PetscDrawCoordinateToPixel_X,
612                                       PetscDrawPixelToCoordinate_X,
613                                       PetscDrawPointPixel_X,
614                                       0};
615 
616 
617 #undef __FUNCT__
618 #define __FUNCT__ "PetscDrawGetSingleton_X"
619 static PetscErrorCode PetscDrawGetSingleton_X(PetscDraw draw,PetscDraw *sdraw)
620 {
621   PetscDraw_X    *Xwin = (PetscDraw_X*)draw->data,*sXwin;
622   PetscErrorCode ierr;
623 
624   PetscFunctionBegin;
625   ierr = PetscDrawCreate(PETSC_COMM_SELF,draw->display,draw->title,draw->x,draw->y,draw->w,draw->h,sdraw);CHKERRQ(ierr);
626   ierr = PetscObjectChangeTypeName((PetscObject)*sdraw,PETSC_DRAW_X);CHKERRQ(ierr);
627   ierr = PetscMemcpy((*sdraw)->ops,&DvOps,sizeof(DvOps));CHKERRQ(ierr);
628 
629   if (draw->popup) {
630     ierr = PetscDrawGetSingleton(draw->popup,&(*sdraw)->popup);CHKERRQ(ierr);
631   }
632   (*sdraw)->pause   = draw->pause;
633   (*sdraw)->coor_xl = draw->coor_xl;
634   (*sdraw)->coor_xr = draw->coor_xr;
635   (*sdraw)->coor_yl = draw->coor_yl;
636   (*sdraw)->coor_yr = draw->coor_yr;
637   (*sdraw)->port_xl = draw->port_xl;
638   (*sdraw)->port_xr = draw->port_xr;
639   (*sdraw)->port_yl = draw->port_yl;
640   (*sdraw)->port_yr = draw->port_yr;
641 
642   /* share drawables (windows and/or pixmap) from the parent draw */
643   ierr = PetscNewLog(*sdraw,&sXwin);CHKERRQ(ierr);
644   (*sdraw)->data = (void*)sXwin;
645   ierr = PetscDrawXiInit(sXwin,draw->display);CHKERRQ(ierr);
646   if (Xwin->win) {
647     ierr = PetscDrawXiQuickWindowFromWindow(sXwin,Xwin->win);CHKERRQ(ierr);
648     sXwin->drw = Xwin->drw; /* XXX If the window is ever resized, this is wrong! */
649   } else if (Xwin->drw) {
650     ierr = PetscDrawXiColormap(sXwin);CHKERRQ(ierr);
651     sXwin->drw = Xwin->drw;
652   }
653   ierr = PetscDrawXiGetGeometry(sXwin,&sXwin->x,&sXwin->y,&sXwin->w,&sXwin->h);CHKERRQ(ierr);
654   (*sdraw)->x = sXwin->x; (*sdraw)->y = sXwin->y;
655   (*sdraw)->w = sXwin->w; (*sdraw)->h = sXwin->h;
656   PetscFunctionReturn(0);
657 }
658 
659 #undef __FUNCT__
660 #define __FUNCT__ "PetscDrawRestoreSingleton_X"
661 static PetscErrorCode PetscDrawRestoreSingleton_X(PetscDraw draw,PetscDraw *sdraw)
662 {
663   PetscErrorCode ierr;
664 
665   PetscFunctionBegin;
666   if (draw->popup && (*sdraw)->popup) {
667     PetscBool isdrawx;
668     PetscDraw_X *pXwin = (PetscDraw_X*)draw->popup->data;
669     PetscDraw_X *sXwin = (PetscDraw_X*)(*sdraw)->popup->data;
670     ierr = PetscObjectTypeCompare((PetscObject)draw->popup,PETSC_DRAW_X,&isdrawx);CHKERRQ(ierr);
671     if (!isdrawx) goto finally;
672     ierr = PetscObjectTypeCompare((PetscObject)(*sdraw)->popup,PETSC_DRAW_X,&isdrawx);CHKERRQ(ierr);
673     if (!isdrawx) goto finally;
674     if (sXwin->win == pXwin->win) {
675       ierr = PetscDrawRestoreSingleton(draw->popup,&(*sdraw)->popup);CHKERRQ(ierr);
676     }
677   }
678 finally:
679   ierr = PetscDrawDestroy(sdraw);CHKERRQ(ierr);
680   PetscFunctionReturn(0);
681 }
682 
683 #undef __FUNCT__
684 #define __FUNCT__ "PetscDrawXGetDisplaySize_Private"
685 static PetscErrorCode PetscDrawXGetDisplaySize_Private(const char name[],int *width,int *height)
686 {
687   Display *display;
688 
689   PetscFunctionBegin;
690   display = XOpenDisplay(name);
691   if (!display) {
692     *width  = *height = 0;
693     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Unable to open display on %s\n\
694     Make sure your COMPUTE NODES are authorized to connect\n\
695     to this X server and either your DISPLAY variable\n\
696     is set or you use the -display name option\n",name);
697   }
698   *width  = (int)DisplayWidth(display,DefaultScreen(display));
699   *height = (int)DisplayHeight(display,DefaultScreen(display));
700   XCloseDisplay(display);
701   PetscFunctionReturn(0);
702 }
703 
704 /*MC
705      PETSC_DRAW_X  - PETSc graphics device that uses either X windows or its virtual version Xvfb
706 
707    Options Database Keys:
708 +  -display <display> - sets the display to use
709 .  -x_virtual - forces use of a X virtual display Xvfb that will not display anything but -draw_save will still work.
710                 Xvfb is automatically started up in PetscSetDisplay() with this option
711 .  -draw_size w,h - percentage of screeen (either 1, .5, .3, .25), or size in pixels
712 .  -geometry x,y,w,h - set location and size in pixels
713 .  -draw_virtual - do not open a window (draw on a pixmap), -draw_save will still work
714 -  -draw_double_buffer - avoid window flickering (draw on pixmap and flush to window)
715 
716    Level: beginner
717 
718 .seealso:  PetscDrawOpenX(), PetscDrawSetDisplay(), PetscDrawSetFromOptions()
719 
720 M*/
721 
722 #undef __FUNCT__
723 #define __FUNCT__ "PetscDrawCreate_X"
724 PETSC_EXTERN PetscErrorCode PetscDrawCreate_X(PetscDraw draw)
725 {
726   PetscDraw_X    *Xwin;
727   PetscErrorCode ierr;
728   PetscMPIInt    rank;
729   int            x = draw->x,y = draw->y,w = draw->w,h = draw->h;
730   static int     xavailable = 0,yavailable = 0,ybottom = 0,xmax = 0,ymax = 0;
731   PetscBool      set,dvirtual = PETSC_FALSE,doublebuffer = PETSC_TRUE;
732   PetscInt       xywh[4],osize = 4,nsizes=2;
733   PetscReal      sizes[2] = {.3,.3};
734 
735   PetscFunctionBegin;
736   /* get the display variable */
737   if (!draw->display) {
738     ierr = PetscMalloc1(256,&draw->display);CHKERRQ(ierr);
739     ierr = PetscGetDisplay(draw->display,256);CHKERRQ(ierr);
740   }
741 
742   /* initialize the display size */
743   if (!xmax) {
744     ierr = PetscDrawXGetDisplaySize_Private(draw->display,&xmax,&ymax);
745     /* if some processors fail on this and others succed then this is a problem ! */
746     if (ierr) {
747       (*PetscErrorPrintf)("PETSc unable to use X windows\nproceeding without graphics\n");
748       ierr = PetscDrawSetType(draw,PETSC_DRAW_NULL);CHKERRQ(ierr);
749       PetscFunctionReturn(0);
750     }
751   }
752 
753   /* allow user to set size of drawable */
754   ierr = PetscOptionsGetRealArray(((PetscObject)draw)->options,((PetscObject)draw)->prefix,"-draw_size",sizes,&nsizes,&set);CHKERRQ(ierr);
755   if (set && nsizes == 1 && sizes[0] > 1.0) sizes[1] = sizes[0];
756   if (set) {
757     if (sizes[0] > 1.0)       w = (int)sizes[0];
758     else if (sizes[0] == 1.0) w = PETSC_DRAW_FULL_SIZE;
759     else if (sizes[0] == .5)  w = PETSC_DRAW_HALF_SIZE;
760     else if (sizes[0] == .3)  w = PETSC_DRAW_THIRD_SIZE;
761     else if (sizes[0] == .25) w = PETSC_DRAW_QUARTER_SIZE;
762     if (sizes[1] > 1.0)       h = (int)sizes[1];
763     else if (sizes[1] == 1.0) h = PETSC_DRAW_FULL_SIZE;
764     else if (sizes[1] == .5)  h = PETSC_DRAW_HALF_SIZE;
765     else if (sizes[1] == .3)  h = PETSC_DRAW_THIRD_SIZE;
766     else if (sizes[1] == .25) h = PETSC_DRAW_QUARTER_SIZE;
767   }
768   if (w == PETSC_DECIDE || w == PETSC_DEFAULT) w = draw->w = 300;
769   if (h == PETSC_DECIDE || h == PETSC_DEFAULT) h = draw->h = 300;
770   switch (w) {
771   case PETSC_DRAW_FULL_SIZE:    w = draw->w = (xmax - 10);   break;
772   case PETSC_DRAW_HALF_SIZE:    w = draw->w = (xmax - 20)/2; break;
773   case PETSC_DRAW_THIRD_SIZE:   w = draw->w = (xmax - 30)/3; break;
774   case PETSC_DRAW_QUARTER_SIZE: w = draw->w = (xmax - 40)/4; break;
775   }
776   switch (h) {
777   case PETSC_DRAW_FULL_SIZE:    h = draw->h = (ymax - 10);   break;
778   case PETSC_DRAW_HALF_SIZE:    h = draw->h = (ymax - 20)/2; break;
779   case PETSC_DRAW_THIRD_SIZE:   h = draw->h = (ymax - 30)/3; break;
780   case PETSC_DRAW_QUARTER_SIZE: h = draw->h = (ymax - 40)/4; break;
781   }
782 
783   ierr = PetscOptionsGetBool(((PetscObject)draw)->options,((PetscObject)draw)->prefix,"-draw_virtual",&dvirtual,NULL);CHKERRQ(ierr);
784 
785   if (!dvirtual) {
786 
787     /* allow user to set location and size of window */
788     xywh[0] = x; xywh[1] = y; xywh[2] = w; xywh[3] = h;
789     ierr = PetscOptionsGetIntArray(((PetscObject)draw)->options,((PetscObject)draw)->prefix,"-geometry",xywh,&osize,NULL);CHKERRQ(ierr);
790     x = (int)xywh[0]; y = (int)xywh[1]; w = (int)xywh[2]; h = (int)xywh[3];
791     if (w == PETSC_DECIDE || w == PETSC_DEFAULT) w = 300;
792     if (h == PETSC_DECIDE || h == PETSC_DEFAULT) h = 300;
793     draw->x = x; draw->y = y; draw->w = w; draw->h = h;
794 
795     if (draw->x == PETSC_DECIDE || draw->y == PETSC_DECIDE) {
796       /*
797        PETSc tries to place windows starting in the upper left corner
798         and moving across to the right.
799 
800        +0,0-------------------------------------------+
801        |  Region used so far  +xavailable,yavailable  |
802        |                      |                       |
803        |                      |                       |
804        +--------------------- +ybottom                |
805        |                                              |
806        |                                              |
807        +----------------------------------------------+xmax,ymax
808 
809       */
810       /*  First: can we add it to the right? */
811       if (xavailable + w + 10 <= xmax) {
812         x       = xavailable;
813         y       = yavailable;
814         ybottom = PetscMax(ybottom,y + h + 30);
815       } else {
816         /* No, so add it below on the left */
817         xavailable = x = 0;
818         yavailable = y = ybottom;
819         ybottom    = ybottom + h + 30;
820       }
821     }
822     /* update available region */
823     xavailable = PetscMax(xavailable,x + w + 10);
824     if (xavailable >= xmax) {
825       xavailable = 0;
826       yavailable = yavailable + h + 30;
827       ybottom    = yavailable;
828     }
829     if (yavailable >= ymax) {
830       y          = 0;
831       yavailable = 0;
832       ybottom    = 0;
833     }
834 
835   } /* endif(!dvirtual) */
836 
837   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)draw),&rank);CHKERRQ(ierr);
838   if (!rank && (w <= 0 || h <= 0)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative window width or height");
839 
840   ierr = PetscNewLog(draw,&Xwin);CHKERRQ(ierr);
841   ierr = PetscMemcpy(draw->ops,&DvOps,sizeof(DvOps));CHKERRQ(ierr);
842   draw->data = (void*)Xwin;
843 
844   ierr = PetscDrawXiInit(Xwin,draw->display);CHKERRQ(ierr);
845   if (!dvirtual) {
846     Xwin->x = x; Xwin->y = y;
847     Xwin->w = w; Xwin->h = h;
848     if (!rank) {ierr = PetscDrawXiQuickWindow(Xwin,draw->title,x,y,w,h);CHKERRQ(ierr);}
849     ierr = MPI_Bcast(&Xwin->win,1,MPI_UNSIGNED_LONG,0,PetscObjectComm((PetscObject)draw));CHKERRQ(ierr);
850     if (rank) {ierr = PetscDrawXiQuickWindowFromWindow(Xwin,Xwin->win);CHKERRQ(ierr);}
851   } else {
852     Xwin->x = 0; Xwin->y = 0;
853     Xwin->w = w; Xwin->h = h;
854     ierr = PetscDrawXiColormap(Xwin);CHKERRQ(ierr);
855     if (!rank) {ierr = PetscDrawXiQuickPixmap(Xwin);CHKERRQ(ierr);}
856     ierr = MPI_Bcast(&Xwin->drw,1,MPI_UNSIGNED_LONG,0,PetscObjectComm((PetscObject)draw));CHKERRQ(ierr);
857   }
858   ierr = PetscDrawXiGetGeometry(Xwin,&Xwin->x,&Xwin->y,&Xwin->w,&Xwin->h);CHKERRQ(ierr);
859   draw->x = Xwin->x; draw->y = Xwin->y;
860   draw->w = Xwin->w; draw->h = Xwin->h;
861 
862   ierr = PetscOptionsGetBool(((PetscObject)draw)->options,((PetscObject)draw)->prefix,"-draw_double_buffer",&doublebuffer,NULL);CHKERRQ(ierr);
863   if (doublebuffer) {ierr = PetscDrawSetDoubleBuffer(draw);CHKERRQ(ierr);}
864   PetscFunctionReturn(0);
865 }
866