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