changed reading hint
[gromacs/adressmacs.git] / src / ngmx / xutil.c
blob9d94c5b4562fc69959bf4251525e357a0b362f33
1 /*
2 * $Id$
3 *
4 * This source code is part of
5 *
6 * G R O M A C S
7 *
8 * GROningen MAchine for Chemical Simulations
9 *
10 * VERSION 2.0
12 * Copyright (c) 1991-1999
13 * BIOSON Research Institute, Dept. of Biophysical Chemistry
14 * University of Groningen, The Netherlands
16 * Please refer to:
17 * GROMACS: A message-passing parallel molecular dynamics implementation
18 * H.J.C. Berendsen, D. van der Spoel and R. van Drunen
19 * Comp. Phys. Comm. 91, 43-56 (1995)
21 * Also check out our WWW page:
22 * http://md.chem.rug.nl/~gmx
23 * or e-mail to:
24 * gromacs@chem.rug.nl
26 * And Hey:
27 * Great Red Oystrich Makes All Chemists Sane
29 static char *SRCID_xutil_c = "$Id$";
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <assert.h>
35 #include <smalloc.h>
36 #include <typedefs.h>
37 #include <Xstuff.h>
38 #include <xutil.h>
40 int CheckWin(Window win,char *file, int line)
42 typedef struct {
43 int n;
44 char *s;
45 } t_winerr;
46 t_winerr winerr[] = {
47 { BadAlloc, "Bad Alloc" },
48 { BadColor, "Bad Color" },
49 { BadCursor, "Bad Cursor"},
50 { BadMatch, "Bad Match" },
51 { BadPixmap, "Bad Pixmap"},
52 { BadValue, "Bad Value" },
53 { BadWindow, "Bad Window"}
54 };
55 #define NERR (sizeof(winerr)/sizeof(winerr[0]))
56 int i;
58 for(i=0; (i<NERR); i++)
59 if (win==winerr[i].n) {
60 fprintf(stderr,"%s",winerr[i].s);
61 break;
63 if (i==NERR) return 1;
65 fprintf(stderr," in file %s, line %d\n",file,line);
66 return 0;
69 void LightBorder(Display *disp, Window win, unsigned long color)
71 XSetWindowAttributes attributes;
73 attributes.border_pixel = color;
74 XChangeWindowAttributes(disp,win,CWBorderPixel,&attributes);
77 void SpecialTextInRect(t_x11 *x11,XFontStruct *font,Drawable win,
78 char *s,int x,int y,int width,int height,
79 eXPos eX,eYPos eY)
81 int fw,fh,x0,y0;
82 XFontStruct *f;
84 if (font) {
85 XSetFont(x11->disp,x11->gc,font->fid);
86 f=font;
88 else
89 f=x11->font;
91 fw=XTextWidth(f,s,strlen(s));
92 fh=XTextHeight(f);
93 switch (eX) {
94 case eXLeft:
95 x0=x;
96 break;
97 case eXRight:
98 x0=x+width-fw;
99 break;
100 case eXCenter:
101 default:
102 x0=x+(width-fw)/2;
103 break;
105 switch (eY) {
106 case eYTop:
107 y0=y+f->ascent;
108 break;
109 case eYBottom:
110 y0=y+height-f->descent;
111 break;
112 case eYCenter:
113 default:
114 y0=y+(height-fh)/2+f->ascent;
115 break;
117 XDrawString(x11->disp,win,x11->gc,x0,y0,s,strlen(s));
118 if (font)
119 XSetFont(x11->disp,x11->gc,x11->font->fid);
122 void TextInRect(t_x11 *x11,Drawable win,
123 char *s,int x,int y,int width,int height,
124 eXPos eX,eYPos eY)
126 SpecialTextInRect(x11,NULL,win,s,x,y,width,height,eX,eY);
129 void TextInWin(t_x11 *x11, t_windata *win,
130 char *s, eXPos eX, eYPos eY)
132 TextInRect(x11,win->self,s,0,0,win->width,win->height,eX,eY);
135 void InitWin(t_windata *win, int x0,int y0, int w, int h, int bw, char *text)
137 win->self=0;
138 win->color=0;
139 win->x=x0;
140 win->y=y0;
141 win->width=w;
142 win->height=h;
143 win->bwidth=bw;
144 win->bFocus=FALSE;
145 win->cursor=0;
146 if (text)
147 win->text=strdup(text);
148 else
149 win->text=NULL;
150 #ifdef DEBUG
151 printf("%s: %d x %d at %d, %d\n",text,w,h,x0,y0);
152 #endif
155 void FreeWin(Display *disp, t_windata *win)
157 if (win->text)
158 sfree(win->text);
159 if (win->cursor)
160 XFreeCursor(disp,win->cursor);
163 void ExposeWin(Display *disp,Window win)
165 XEvent event;
167 event.type = Expose;
168 event.xexpose.send_event=True;
169 event.xexpose.window = win;
170 event.xexpose.x=0;
171 event.xexpose.y=0;
172 event.xexpose.width=1000;
173 event.xexpose.height=1000;
174 event.xexpose.count = 0;
175 XSendEvent(disp,win,False,ExposureMask,&event);
178 void XDrawRoundRect(Display *disp, Window win, GC gc,
179 int x, int y, int w, int h)
181 #define RAD (OFFS_X/2)
182 #define SetPoint(pn,x0,y0) pn.x=x0; pn.y=y0
184 if ((w<10) || (h<10))
185 XDrawRectangle(disp,win,gc,x,y,w,h);
186 else {
187 XPoint p[9];
189 SetPoint(p[0],x+RAD,y);
190 SetPoint(p[1],w-2*RAD,0);
191 SetPoint(p[2],RAD,RAD);
192 SetPoint(p[3],0,h-2*RAD);
193 SetPoint(p[4],-RAD,RAD);
194 SetPoint(p[5],2*RAD-w,0);
195 SetPoint(p[6],-RAD,-RAD);
196 SetPoint(p[7],0,2*RAD-h);
197 SetPoint(p[8],RAD,-RAD);
198 XDrawLines(disp,win,gc,p,9,CoordModePrevious);
202 void RoundRectWin(Display *disp, GC gc, t_windata *win,
203 int offsx, int offsy,unsigned long color)
205 XSetLineAttributes(disp,gc,1,LineOnOffDash,CapButt,JoinRound);
206 XSetForeground(disp,gc,color);
207 XDrawRoundRect(disp,win->self,gc,offsx,offsy,
208 win->width-2*offsx-1,win->height-2*offsy-1);
209 XSetLineAttributes(disp,gc,1,LineSolid,CapButt,JoinRound);
212 void RectWin(Display *disp, GC gc, t_windata *win, unsigned long color)
214 int bw=1; /*2*w.bwidth;*/
216 XSetForeground(disp,gc,color);
217 XDrawRoundRect(disp,win->self,gc,0,0,win->width-bw,win->height-bw);
220 typedef struct t_mpos {
221 int x,y;
222 struct t_mpos *prev;
223 } t_mpos;
225 static t_mpos *mpos=NULL;
227 void PushMouse(Display *disp, Window dest, int x, int y)
229 Window root,child;
230 int root_x,root_y;
231 int win_x,win_y;
232 unsigned int keybut;
233 t_mpos *newpos;
235 snew(newpos,1);
236 XQueryPointer(disp,DefaultRootWindow(disp),&root,&child,&root_x,&root_y,
237 &win_x,&win_y,&keybut);
238 newpos->x=root_x;
239 newpos->y=root_y;
240 newpos->prev=mpos;
241 mpos=newpos;
242 XWarpPointer(disp,None,dest,0,0,0,0,x,y);
243 #ifdef DEBUG
244 fprintf(stderr,"Pushmouse %d, %d\n",x,y);
245 #endif
248 void PopMouse(Display *disp)
250 t_mpos *old;
252 old=mpos;
253 if (!old) return;
255 XWarpPointer(disp,None,DefaultRootWindow(disp),0,0,0,0,old->x,old->y);
256 #ifdef DEBUG
257 fprintf(stderr,"Popmouse %d, %d\n",old->x,old->y);
258 #endif
259 mpos=old->prev;
260 sfree(old);
263 bool HelpPressed(XEvent *event)
265 #define BUFSIZE 24
266 char buf[BUFSIZE+1];
267 XComposeStatus compose;
268 KeySym keysym;
270 (void)XLookupString(&(event->xkey),buf,BUFSIZE,&keysym,&compose);
272 return (keysym == XK_F1);
275 bool GrabOK(FILE *out, int err)
277 switch (err) {
278 case GrabSuccess:
279 return TRUE;
280 case GrabNotViewable:
281 fprintf(out,"GrabNotViewable\n"); break;
282 case AlreadyGrabbed:
283 fprintf(out,"AlreadyGrabbed\n"); break;
284 case GrabFrozen:
285 fprintf(out,"GrabFrozen\n"); break;
286 case GrabInvalidTime:
287 fprintf(out,"GrabInvalidTime\n"); break;
288 default:
289 break;
291 return FALSE;