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/src/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: }
158: /* make the window visible */
159: XSelectInput(XiWin->disp,XiWin->win,ExposureMask | StructureNotifyMask);
160: XMapWindow(XiWin->disp,XiWin->win);
162: /* some window systems are cruel and interfere with the placement of
163: windows. We wait here for the window to be created or to die */
164: if (Xi_wait_map(XiWin)){
165: XiWin->win = (Window)0;
166: PetscFunctionReturn(1);
167: }
168: /* Initial values for the upper left corner */
169: XiWin->x = 0;
170: XiWin->y = 0;
171: return(0);
172: }
176: PetscErrorCode XiQuickWindow(PetscDraw_X* w,char* host,char* name,int x,int y,int nx,int ny)
177: {
181: XiOpenDisplay(w,host);
183: w->vis = DefaultVisual(w->disp,w->screen);
184: w->depth = DefaultDepth(w->disp,w->screen);
186: PetscDrawSetColormap_X(w,host,(Colormap)0);
188: XiDisplayWindow(w,name,x,y,nx,ny,(PixVal)0);
189: XiSetGC(w,w->cmapping[1]);
190: XiSetPixVal(w,w->background);
191: XSetWindowBackground(w->disp,w->win,w->cmapping[0]);
194: XiFontFixed(w,6,10,&w->font);
195: XFillRectangle(w->disp,w->win,w->gc.set,0,0,nx,ny);
196: return(0);
197: }
199: /*
200: A version from an already defined window
201: */
204: PetscErrorCode XiQuickWindowFromWindow(PetscDraw_X* w,char *host,Window win)
205: {
206: Window root;
208: int d;
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: PetscErrorCode XiSetWindowLabel(PetscDraw_X* Xiwin,char *label)
238: {
239: XTextProperty prop;
240: size_t len;
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: PetscErrorCode 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: }