Actual source code: xinit.c

  1: /*$Id: xinit.c,v 1.72 2001/03/23 23:20:15 balay Exp $*/

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

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

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

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

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


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

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

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

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

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

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

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

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

150:     size_hints.x            = x;
151:     size_hints.y            = y;
152:     size_hints.min_width    = 4*border_width;
153:     size_hints.min_height   = 4*border_width;
154:     size_hints.width        = w;
155:     size_hints.height       = h;
156:     size_hints.flags        = USPosition | USSize | PMinSize;
157: 
158:     XSetWMProperties(XiWin->disp,XiWin->win,&windowname,&iconname,0,0,&size_hints,&wm_hints,&class_hints);
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: int XiQuickWindow(PetscDraw_X* w,char* host,char* name,int x,int y,int nx,int ny)
179: {
180:   int         ierr;

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: int XiQuickWindowFromWindow(PetscDraw_X* w,char *host,Window win)
207: {
208:   Window            root;
209:   int               d,ierr;
210:   unsigned int      ud;
211:   XWindowAttributes attributes;

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

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

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

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

233: /*
234:       XiSetWindowLabel - Sets new label in open window.
235: */
238: int XiSetWindowLabel(PetscDraw_X* Xiwin,char *label)
239: {
240:   XTextProperty prop;
241:   int           len,ierr;

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

254: int XiSetToBackground(PetscDraw_X* XiWin)
255: {
257:   if (XiWin->gc.cur_pix != XiWin->background) {
258:     XSetForeground(XiWin->disp,XiWin->gc.set,XiWin->background);
259:     XiWin->gc.cur_pix   = XiWin->background;
260:   }
261:   return(0);
262: }