35522720f621325806d02e7bcb2d3a285d5de444
[alexxy/gromacs.git] / src / ngmx / xutil.c
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
11  * 
12  * Copyright (c) 1991-1999
13  * BIOSON Research Institute, Dept. of Biophysical Chemistry
14  * University of Groningen, The Netherlands
15  * 
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)
20  * 
21  * Also check out our WWW page:
22  * http://md.chem.rug.nl/~gmx
23  * or e-mail to:
24  * gromacs@chem.rug.nl
25  * 
26  * And Hey:
27  * Great Red Oystrich Makes All Chemists Sane
28  */
29 static char *SRCID_xutil_c = "$Id$";
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <Xstuff.h>
35 #include <xutil.h>
36 #include "assert.h"
37 #include "smalloc.h"
38 #include "typedefs.h"
39 #include "string2.h"
40
41 int CheckWin(Window win,char *file, int line)
42 {
43   typedef struct {
44     int  n;
45     char *s;
46   } t_winerr;
47   t_winerr winerr[] = {
48     { BadAlloc,  "Bad Alloc" },
49     { BadColor,  "Bad Color" },
50     { BadCursor, "Bad Cursor"},
51     { BadMatch,  "Bad Match" },
52     { BadPixmap, "Bad Pixmap"},
53     { BadValue,  "Bad Value" },
54     { BadWindow, "Bad Window"}
55   };  
56 #define NERR (sizeof(winerr)/sizeof(winerr[0]))  
57   int i;
58
59   for(i=0; (i<NERR); i++)
60     if (win==winerr[i].n) {
61       fprintf(stderr,"%s",winerr[i].s);
62       break;
63     }
64   if (i==NERR) return 1;
65
66   fprintf(stderr," in file %s, line %d\n",file,line);
67   return 0;
68 }
69
70 void LightBorder(Display *disp, Window win, unsigned long color)
71 {
72   XSetWindowAttributes attributes;
73
74   attributes.border_pixel = color;
75   XChangeWindowAttributes(disp,win,CWBorderPixel,&attributes);
76 }
77
78 void SpecialTextInRect(t_x11 *x11,XFontStruct *font,Drawable win,
79                        char *s,int x,int y,int width,int height,
80                        eXPos eX,eYPos eY)
81 {
82   int         fw,fh,x0,y0;
83   XFontStruct *f;
84
85   if (font) {
86     XSetFont(x11->disp,x11->gc,font->fid);
87     f=font;
88   }
89   else
90     f=x11->font;
91
92   fw=XTextWidth(f,s,strlen(s));
93   fh=XTextHeight(f);
94   switch (eX) {
95   case eXLeft:
96     x0=x;
97     break;
98   case eXRight:
99     x0=x+width-fw;
100     break;
101   case eXCenter:
102   default:
103     x0=x+(width-fw)/2;
104     break;
105   }
106   switch (eY) {
107   case eYTop:
108     y0=y+f->ascent;
109     break;
110   case eYBottom:
111     y0=y+height-f->descent;
112     break;
113   case eYCenter:
114   default:
115     y0=y+(height-fh)/2+f->ascent;
116     break;
117   }
118   XDrawString(x11->disp,win,x11->gc,x0,y0,s,strlen(s));
119   if (font)
120     XSetFont(x11->disp,x11->gc,x11->font->fid);
121 }
122
123 void TextInRect(t_x11 *x11,Drawable win,
124                 char *s,int x,int y,int width,int height,
125                 eXPos eX,eYPos eY)
126 {
127   SpecialTextInRect(x11,NULL,win,s,x,y,width,height,eX,eY);
128 }
129
130 void TextInWin(t_x11 *x11, t_windata *win,
131                char *s, eXPos eX, eYPos eY)
132 {
133   TextInRect(x11,win->self,s,0,0,win->width,win->height,eX,eY);
134 }
135
136 void InitWin(t_windata *win, int x0,int y0, int w, int h, int bw, char *text)
137 {
138   win->self=0;
139   win->color=0;
140   win->x=x0;
141   win->y=y0;
142   win->width=w;
143   win->height=h;
144   win->bwidth=bw;
145   win->bFocus=FALSE;
146   win->cursor=0;
147   if (text)
148     win->text=strdup(text);
149   else
150     win->text=NULL;
151 #ifdef DEBUG
152   printf("%s: %d x %d at %d, %d\n",text,w,h,x0,y0);
153 #endif  
154 }
155
156 void FreeWin(Display *disp, t_windata *win)
157 {
158   if (win->text)
159     sfree(win->text);
160   if (win->cursor)
161     XFreeCursor(disp,win->cursor);
162 }
163
164 void ExposeWin(Display *disp,Window win)
165 {
166   XEvent event;
167
168   event.type = Expose;
169   event.xexpose.send_event=True;
170   event.xexpose.window = win;
171   event.xexpose.x=0;
172   event.xexpose.y=0;
173   event.xexpose.width=1000;
174   event.xexpose.height=1000;
175   event.xexpose.count = 0;
176   XSendEvent(disp,win,False,ExposureMask,&event);
177 }
178
179 void XDrawRoundRect(Display *disp, Window win, GC gc, 
180                     int x, int y, int w, int h)
181 {
182 #define RAD (OFFS_X/2)
183 #define SetPoint(pn,x0,y0) pn.x=x0; pn.y=y0
184
185   if ((w<10) || (h<10))
186     XDrawRectangle(disp,win,gc,x,y,w,h);
187   else {
188     XPoint p[9];
189
190     SetPoint(p[0],x+RAD,y);
191     SetPoint(p[1],w-2*RAD,0);
192     SetPoint(p[2],RAD,RAD);
193     SetPoint(p[3],0,h-2*RAD);
194     SetPoint(p[4],-RAD,RAD);
195     SetPoint(p[5],2*RAD-w,0);
196     SetPoint(p[6],-RAD,-RAD);
197     SetPoint(p[7],0,2*RAD-h);
198     SetPoint(p[8],RAD,-RAD);
199     XDrawLines(disp,win,gc,p,9,CoordModePrevious);
200   }
201 }
202
203 void RoundRectWin(Display *disp, GC gc, t_windata *win, 
204                   int offsx, int offsy,unsigned long color)
205 {
206   XSetLineAttributes(disp,gc,1,LineOnOffDash,CapButt,JoinRound);
207   XSetForeground(disp,gc,color);
208   XDrawRoundRect(disp,win->self,gc,offsx,offsy,
209                  win->width-2*offsx-1,win->height-2*offsy-1);
210   XSetLineAttributes(disp,gc,1,LineSolid,CapButt,JoinRound);
211 }
212
213 void RectWin(Display *disp, GC gc, t_windata *win, unsigned long color)
214 {
215   int bw=1; /*2*w.bwidth;*/
216
217   XSetForeground(disp,gc,color);
218   XDrawRoundRect(disp,win->self,gc,0,0,win->width-bw,win->height-bw);
219 }
220
221 typedef struct t_mpos {
222   int    x,y;
223   struct t_mpos *prev;
224 } t_mpos;
225
226 static t_mpos *mpos=NULL;
227
228 void PushMouse(Display *disp, Window dest, int x, int y)
229 {
230   Window root,child;
231   int    root_x,root_y;
232   int    win_x,win_y;
233   unsigned int   keybut;
234   t_mpos *newpos;
235   
236   snew(newpos,1);
237   XQueryPointer(disp,DefaultRootWindow(disp),&root,&child,&root_x,&root_y,
238                 &win_x,&win_y,&keybut);
239   newpos->x=root_x;
240   newpos->y=root_y;
241   newpos->prev=mpos;
242   mpos=newpos;
243   XWarpPointer(disp,None,dest,0,0,0,0,x,y);
244 #ifdef DEBUG
245   fprintf(stderr,"Pushmouse %d, %d\n",x,y);
246 #endif
247 }
248
249 void PopMouse(Display *disp)
250 {
251   t_mpos *old;
252   
253   old=mpos;
254   if (!old) return;
255
256   XWarpPointer(disp,None,DefaultRootWindow(disp),0,0,0,0,old->x,old->y);
257 #ifdef DEBUG
258   fprintf(stderr,"Popmouse %d, %d\n",old->x,old->y);
259 #endif
260   mpos=old->prev;
261   sfree(old);
262 }
263
264 bool HelpPressed(XEvent *event)
265 {
266 #define BUFSIZE 24
267   char           buf[BUFSIZE+1];
268   XComposeStatus compose;
269   KeySym         keysym;
270
271   (void)XLookupString(&(event->xkey),buf,BUFSIZE,&keysym,&compose);
272
273   return (keysym == XK_F1);
274 }
275
276 bool GrabOK(FILE *out, int err)
277 {
278   switch (err) {
279   case GrabSuccess:
280     return TRUE;
281   case GrabNotViewable:
282     fprintf(out,"GrabNotViewable\n"); break;
283   case AlreadyGrabbed:
284     fprintf(out,"AlreadyGrabbed\n"); break;
285   case GrabFrozen:
286     fprintf(out,"GrabFrozen\n"); break;
287   case GrabInvalidTime:
288     fprintf(out,"GrabInvalidTime\n"); break;
289   default:
290     break;
291   }
292   return FALSE;
293 }
294