Actual source code: xinit.c

  1: #define PETSC_DLL
  2: /* 
  3:    This file contains routines to open an X window display and window
  4:    This consists of a number of routines that set the various
  5:    fields in the Window structure, which is passed to 
  6:    all of these routines.

  8:    Note that if you use the default visual and colormap, then you
  9:    can use these routines with any X toolkit that will give you the
 10:    Window id of the window that it is managing.  Use that instead of the
 11:    call to XiCreateWindow .  Similarly for the Display.
 12: */

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

 16: EXTERN PetscErrorCode XiUniformHues(PetscDraw_X *,int);
 17: EXTERN PetscErrorCode Xi_wait_map(PetscDraw_X*);
 18: EXTERN PetscErrorCode XiInitColors(PetscDraw_X*,Colormap);
 19: EXTERN PetscErrorCode XiFontFixed(PetscDraw_X*,int,int,XiFont**);
 20: EXTERN PetscErrorCode XiInitCmap(PetscDraw_X*);
 21: EXTERN PetscErrorCode PetscDrawSetColormap_X(PetscDraw_X*,char *,Colormap);

 23: /*
 24:   XiOpenDisplay - Open a display
 25: */
 28: PetscErrorCode XiOpenDisplay(PetscDraw_X* XiWin,char *display_name)
 29: {
 31:   XiWin->disp = XOpenDisplay(display_name);
 32:   if (!XiWin->disp) {
 33:     SETERRQ1(PETSC_ERR_LIB,"Unable to open display on %s\n.  Make sure your COMPUTE NODES are authorized to connect \n\
 34:     to this X server and either your DISPLAY variable\n\
 35:     is set or you use the -display name option\n",display_name);
 36:   }
 37:   XiWin->screen = DefaultScreen(XiWin->disp);
 38:   return(0);
 39: }


 42: /* 
 43:    XiSetGC - set the GC structure in the base window
 44: */
 47: PetscErrorCode XiSetGC(PetscDraw_X* XiWin,PixVal fg)
 48: {
 49:   XGCValues       gcvalues;       /* window graphics context values */

 52:   /* Set the graphics contexts */
 53:   /* create a gc for the ROP_SET operation (writing the fg value to a pixel) */
 54:   /* (do this with function GXcopy; GXset will automatically write 1) */
 55:   gcvalues.function   = GXcopy;
 56:   gcvalues.foreground = fg;
 57:   XiWin->gc.cur_pix   = fg;
 58:   XiWin->gc.set = XCreateGC(XiWin->disp,RootWindow(XiWin->disp,XiWin->screen),
 59:                               GCFunction | GCForeground,&gcvalues);
 60:   return(0);
 61: }

 63: /*
 64:     Actually display a window at [x,y] with sizes (w,h)
 65:     If w and/or h are 0, use the sizes in the fields of XiWin
 66:     (which may have been set by, for example, XiSetWindowSize)
 67: */
 70: PetscErrorCode XiDisplayWindow(PetscDraw_X* XiWin,char *label,int x,int y,int w,int h,PixVal backgnd_pixel)
 71: {
 72:   unsigned int            wavail,havail;
 73:   XSizeHints              size_hints;
 74:   XWindowAttributes       in_window_attributes;
 75:   XSetWindowAttributes    window_attributes;
 76:   int                     depth,border_width;
 77:   unsigned long           wmask;

 80:   /* get the available widths */
 81:   wavail              = DisplayWidth(XiWin->disp,XiWin->screen);
 82:   havail              = DisplayHeight(XiWin->disp,XiWin->screen);
 83:   if (w <= 0 || h <= 0) PetscFunctionReturn(2);
 84:   if ((unsigned int) w > wavail) w = wavail;
 85:   if ((unsigned int) h > havail) h = havail;

 87:   /* changed the next line from xtools version */
 88:   border_width   = 0;
 89:   if (x < 0) x   = 0;
 90:   if (y < 0) y   = 0;
 91:   x   = ((unsigned int) x + w > wavail) ? wavail - w : x;
 92:   y   = ((unsigned int) y + h > havail) ? havail - h : y;

 94:   /* We need XCreateWindow since we may need an visual other than
 95:    the default one */
 96:   XGetWindowAttributes(XiWin->disp,RootWindow(XiWin->disp,XiWin->screen),&in_window_attributes);
 97:   window_attributes.background_pixmap = None;
 98:   window_attributes.background_pixel  = backgnd_pixel;
 99:   /* No border for now */
100:   window_attributes.border_pixmap     = None;
101:   /* 
102:   window_attributes.border_pixel      = border_pixel; 
103:   */
104:   window_attributes.bit_gravity       = in_window_attributes.bit_gravity;
105:   window_attributes.win_gravity       = in_window_attributes.win_gravity;
106:         /* Backing store is too slow in color systems */
107:   window_attributes.backing_store     = 0;
108:   window_attributes.backing_pixel     = backgnd_pixel;
109:   window_attributes.save_under        = 1;
110:   window_attributes.event_mask        = 0;
111:   window_attributes.do_not_propagate_mask = 0;
112:   window_attributes.override_redirect = 0;
113:   window_attributes.colormap          = XiWin->cmap;
114:   /* None for cursor does NOT mean none, it means cursor of Parent */
115:   window_attributes.cursor            = None;
116:   wmask   = CWBackPixmap | CWBackPixel | CWBorderPixmap | CWBitGravity |
117:             CWWinGravity | CWBackingStore |CWBackingPixel|CWOverrideRedirect |
118:             CWSaveUnder  | CWEventMask    | CWDontPropagate |
119:             CWCursor     | CWColormap ;
120:   depth       = XiWin->depth;
121:   /* DefaultDepth(XiWin->disp,XiWin->screen); */
122:   XiWin->win  = XCreateWindow(XiWin->disp,
123:                              RootWindow(XiWin->disp,XiWin->screen),
124:                              x,y,w,h,border_width,
125:                              depth,InputOutput,XiWin->vis,
126:                              wmask,&window_attributes);

128:   if (!XiWin->win)  PetscFunctionReturn(2);

130:   /* set window manager hints */
131:   {
132:     XWMHints      wm_hints;
133:     XClassHint    class_hints;
134:     XTextProperty windowname,iconname;
135: 
136:     if (label) { XStringListToTextProperty(&label,1,&windowname);}
137:     else       { XStringListToTextProperty(&label,0,&windowname);}
138:     if (label) { XStringListToTextProperty(&label,1,&iconname);}
139:     else       { XStringListToTextProperty(&label,0,&iconname);}
140: 
141:     wm_hints.initial_state  = NormalState;
142:     wm_hints.input          = True;
143:     wm_hints.flags          = StateHint|InputHint;
144: 
145:     class_hints.res_name    = 0;
146:     class_hints.res_class   = (char*)"BaseClass"; /* this is nonsense */

148:     size_hints.x            = x;
149:     size_hints.y            = y;
150:     size_hints.min_width    = 4*border_width;
151:     size_hints.min_height   = 4*border_width;
152:     size_hints.width        = w;
153:     size_hints.height       = h;
154:     size_hints.flags        = USPosition | USSize | PMinSize;
155: 
156:     XSetWMProperties(XiWin->disp,XiWin->win,&windowname,&iconname,0,0,&size_hints,&wm_hints,&class_hints);
157:     XFree((void*)windowname.value);
158:     XFree((void*)iconname.value);
159:   }
160:   /* make the window visible */
161:   XSelectInput(XiWin->disp,XiWin->win,ExposureMask | StructureNotifyMask);
162:   XMapWindow(XiWin->disp,XiWin->win);

164:   /* some window systems are cruel and interfere with the placement of
165:      windows.  We wait here for the window to be created or to die */
166:   if (Xi_wait_map(XiWin)){
167:     XiWin->win    = (Window)0;
168:     PetscFunctionReturn(1);
169:   }
170:   /* Initial values for the upper left corner */
171:   XiWin->x = 0;
172:   XiWin->y = 0;
173:   return(0);
174: }

178: PetscErrorCode XiQuickWindow(PetscDraw_X* w,char* host,char* name,int x,int y,int nx,int ny)
179: {

183:   XiOpenDisplay(w,host);

185:   w->vis    = DefaultVisual(w->disp,w->screen);
186:   w->depth  = DefaultDepth(w->disp,w->screen);

188:   PetscDrawSetColormap_X(w,host,(Colormap)0);

190:   XiDisplayWindow(w,name,x,y,nx,ny,(PixVal)0);
191:   XiSetGC(w,w->cmapping[1]);
192:   XiSetPixVal(w,w->background);
193:   XSetWindowBackground(w->disp,w->win,w->cmapping[0]);


196:   XiFontFixed(w,6,10,&w->font);
197:   XFillRectangle(w->disp,w->win,w->gc.set,0,0,nx,ny);
198:   return(0);
199: }

201: /* 
202:    A version from an already defined window 
203: */
206: PetscErrorCode XiQuickWindowFromWindow(PetscDraw_X* w,char *host,Window win)
207: {
208:   Window            root;
210:   int               d;
211:   unsigned int      ud;
212:   XWindowAttributes attributes;

215:   XiOpenDisplay(w,host);
216:   w->win = win;
217:   XGetWindowAttributes(w->disp,w->win,&attributes);

219:   w->vis    = DefaultVisual(w->disp,w->screen);
220:   w->depth  = DefaultDepth(w->disp,w->screen);
221:   PetscDrawSetColormap_X(w,host,attributes.colormap);

223:   XGetGeometry(w->disp,w->win,&root,&d,&d,
224:               (unsigned int *)&w->w,(unsigned int *)&w->h,&ud,&ud);
225:   w->x = w->y = 0;

227:   XiSetGC(w,w->cmapping[1]);
228:   XiSetPixVal(w,w->background);
229:   XSetWindowBackground(w->disp,w->win,w->cmapping[0]);
230:   XiFontFixed(w,6,10,&w->font);
231:   return(0);
232: }

234: /*
235:       XiSetWindowLabel - Sets new label in open window.
236: */
239: PetscErrorCode XiSetWindowLabel(PetscDraw_X* Xiwin,char *label)
240: {
241:   XTextProperty prop;
242:   size_t        len;

246:   XGetWMName(Xiwin->disp,Xiwin->win,&prop);
247:   prop.value  = (unsigned char *)label;
248:   PetscStrlen(label,&len);
249:   prop.nitems = (long) len;
250:   XSetWMName(Xiwin->disp,Xiwin->win,&prop);
251:   return(0);
252: }

256: PetscErrorCode XiSetToBackground(PetscDraw_X* XiWin)
257: {
259:   if (XiWin->gc.cur_pix != XiWin->background) {
260:     XSetForeground(XiWin->disp,XiWin->gc.set,XiWin->background);
261:     XiWin->gc.cur_pix   = XiWin->background;
262:   }
263:   return(0);
264: }