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,int w,int h,PixVal backgnd_pixel)
 72: {
 73:   unsigned int            wavail,havail;
 74:   XSizeHints              size_hints;
 75:   XWindowAttributes       in_window_attributes;
 76:   XSetWindowAttributes    window_attributes;
 77:   int                     depth,border_width;
 78:   unsigned long           wmask;

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

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

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

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

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

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

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

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

182:   XiOpenDisplay(w,host);

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

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

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


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

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

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

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

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

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

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

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

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