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: */
 27: int XiOpenDisplay(PetscDraw_X* XiWin,char *display_name)
 28: {
 30:   XiWin->disp = XOpenDisplay(display_name);
 31:   if (!XiWin->disp) {
 32:     SETERRQ1(1,"Unable to open display on %sn.  Make sure your DISPLAY variablen
 33:     is set, or you use the -display name option and xhost + has beenn
 34:     run on your displaying machine.n",display_name);
 35:   }
 36:   XiWin->screen = DefaultScreen(XiWin->disp);
 37:   return(0);
 38: }


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

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

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

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

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

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

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

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

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

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

170: int XiQuickWindow(PetscDraw_X* w,char* host,char* name,int x,int y,int nx,int ny)
171: {
172:   int         ierr;

175:   XiOpenDisplay(w,host);

177:   w->vis    = DefaultVisual(w->disp,w->screen);
178:   w->depth  = DefaultDepth(w->disp,w->screen);

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

182:   XiDisplayWindow(w,name,x,y,nx,ny,(PixVal)0);
183:   XiSetGC(w,w->cmapping[1]);
184:   XiSetPixVal(w,w->background);
185:   XSetWindowBackground(w->disp,w->win,w->cmapping[0]);


188:   XiFontFixed(w,6,10,&w->font);
189:   XFillRectangle(w->disp,w->win,w->gc.set,0,0,nx,ny);
190:   return(0);
191: }

193: /* 
194:    A version from an already defined window 
195: */
196: int XiQuickWindowFromWindow(PetscDraw_X* w,char *host,Window win)
197: {
198:   Window            root;
199:   int               d,ierr;
200:   unsigned int      ud;
201:   XWindowAttributes attributes;

204:   if (XiOpenDisplay(w,host)) {
205:     SETERRQ(PETSC_ERR_LIB,"Could not open display: make sure your DISPLAY variablen
206:     is set, or you use the [-display name] option and xhost + has beenn
207:     run on your displaying machine.n");
208:   }

210:   w->win = win;
211:   XGetWindowAttributes(w->disp,w->win,&attributes);

213:   w->vis    = DefaultVisual(w->disp,w->screen);
214:   w->depth  = DefaultDepth(w->disp,w->screen);
215:   ierr      = PetscDrawSetColormap_X(w,host,attributes.colormap);

217:   XGetGeometry(w->disp,w->win,&root,&d,&d,
218:               (unsigned int *)&w->w,(unsigned int *)&w->h,&ud,&ud);
219:   w->x = w->y = 0;

221:   XiSetGC(w,w->cmapping[1]);
222:   XiSetPixVal(w,w->background);
223:   XSetWindowBackground(w->disp,w->win,w->cmapping[0]);
224:   XiFontFixed(w,6,10,&w->font);
225:   return(0);
226: }

228: /*
229:       XiSetWindowLabel - Sets new label in open window.
230: */
231: int XiSetWindowLabel(PetscDraw_X* Xiwin,char *label)
232: {
233:   XTextProperty prop;
234:   int           len,ierr;

237:   XGetWMName(Xiwin->disp,Xiwin->win,&prop);
238:   prop.value  = (unsigned char *)label;
239:   ierr        = PetscStrlen(label,&len);
240:   prop.nitems = (long) len;
241:   XSetWMName(Xiwin->disp,Xiwin->win,&prop);
242:   return(0);
243: }

245: int XiSetToBackground(PetscDraw_X* XiWin)
246: {
248:   if (XiWin->gc.cur_pix != XiWin->background) {
249:     XSetForeground(XiWin->disp,XiWin->gc.set,XiWin->background);
250:     XiWin->gc.cur_pix   = XiWin->background;
251:   }
252:   return(0);
253: }