Tagged files with gromacs 3.0 header and added some license info
[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 3.0
11  * 
12  * Copyright (c) 1991-2001
13  * BIOSON Research Institute, Dept. of Biophysical Chemistry
14  * University of Groningen, The Netherlands
15  * 
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  * 
21  * If you want to redistribute modifications, please consider that
22  * scientific software is very special. Version control is crucial -
23  * bugs must be traceable. We will be happy to consider code for
24  * inclusion in the official distribution, but derived work must not
25  * be called official GROMACS. Details are found in the README & COPYING
26  * files - if they are missing, get the official version at www.gromacs.org.
27  * 
28  * To help us fund GROMACS development, we humbly ask that you cite
29  * the papers on the package - you can find them in the top README file.
30  * 
31  * Do check out http://www.gromacs.org , or mail us at gromacs@gromacs.org .
32  * 
33  * And Hey:
34  * Giving Russians Opium May Alter Current Situation
35  */
36 static char *SRCID_xutil_c = "$Id$";
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <Xstuff.h>
41 #include <xutil.h>
42 #include "assert.h"
43 #include "smalloc.h"
44 #include "typedefs.h"
45 #include "string2.h"
46
47 int CheckWin(Window win,char *file, int line)
48 {
49   typedef struct {
50     int  n;
51     char *s;
52   } t_winerr;
53   t_winerr winerr[] = {
54     { BadAlloc,  "Bad Alloc" },
55     { BadColor,  "Bad Color" },
56     { BadCursor, "Bad Cursor"},
57     { BadMatch,  "Bad Match" },
58     { BadPixmap, "Bad Pixmap"},
59     { BadValue,  "Bad Value" },
60     { BadWindow, "Bad Window"}
61   };  
62 #define NERR (sizeof(winerr)/sizeof(winerr[0]))  
63   int i;
64
65   for(i=0; (i<NERR); i++)
66     if (win==winerr[i].n) {
67       fprintf(stderr,"%s",winerr[i].s);
68       break;
69     }
70   if (i==NERR) return 1;
71
72   fprintf(stderr," in file %s, line %d\n",file,line);
73   return 0;
74 }
75
76 void LightBorder(Display *disp, Window win, unsigned long color)
77 {
78   XSetWindowAttributes attributes;
79
80   attributes.border_pixel = color;
81   XChangeWindowAttributes(disp,win,CWBorderPixel,&attributes);
82 }
83
84 void SpecialTextInRect(t_x11 *x11,XFontStruct *font,Drawable win,
85                        char *s,int x,int y,int width,int height,
86                        eXPos eX,eYPos eY)
87 {
88   int         fw,fh,x0,y0;
89   XFontStruct *f;
90
91   if (font) {
92     XSetFont(x11->disp,x11->gc,font->fid);
93     f=font;
94   }
95   else
96     f=x11->font;
97
98   fw=XTextWidth(f,s,strlen(s));
99   fh=XTextHeight(f);
100   switch (eX) {
101   case eXLeft:
102     x0=x;
103     break;
104   case eXRight:
105     x0=x+width-fw;
106     break;
107   case eXCenter:
108   default:
109     x0=x+(width-fw)/2;
110     break;
111   }
112   switch (eY) {
113   case eYTop:
114     y0=y+f->ascent;
115     break;
116   case eYBottom:
117     y0=y+height-f->descent;
118     break;
119   case eYCenter:
120   default:
121     y0=y+(height-fh)/2+f->ascent;
122     break;
123   }
124   XDrawString(x11->disp,win,x11->gc,x0,y0,s,strlen(s));
125   if (font)
126     XSetFont(x11->disp,x11->gc,x11->font->fid);
127 }
128
129 void TextInRect(t_x11 *x11,Drawable win,
130                 char *s,int x,int y,int width,int height,
131                 eXPos eX,eYPos eY)
132 {
133   SpecialTextInRect(x11,NULL,win,s,x,y,width,height,eX,eY);
134 }
135
136 void TextInWin(t_x11 *x11, t_windata *win,
137                char *s, eXPos eX, eYPos eY)
138 {
139   TextInRect(x11,win->self,s,0,0,win->width,win->height,eX,eY);
140 }
141
142 void InitWin(t_windata *win, int x0,int y0, int w, int h, int bw, char *text)
143 {
144   win->self=0;
145   win->color=0;
146   win->x=x0;
147   win->y=y0;
148   win->width=w;
149   win->height=h;
150   win->bwidth=bw;
151   win->bFocus=FALSE;
152   win->cursor=0;
153   if (text)
154     win->text=strdup(text);
155   else
156     win->text=NULL;
157 #ifdef DEBUG
158   printf("%s: %d x %d at %d, %d\n",text,w,h,x0,y0);
159 #endif  
160 }
161
162 void FreeWin(Display *disp, t_windata *win)
163 {
164   if (win->text)
165     sfree(win->text);
166   if (win->cursor)
167     XFreeCursor(disp,win->cursor);
168 }
169
170 void ExposeWin(Display *disp,Window win)
171 {
172   XEvent event;
173
174   event.type = Expose;
175   event.xexpose.send_event=True;
176   event.xexpose.window = win;
177   event.xexpose.x=0;
178   event.xexpose.y=0;
179   event.xexpose.width=1000;
180   event.xexpose.height=1000;
181   event.xexpose.count = 0;
182   XSendEvent(disp,win,False,ExposureMask,&event);
183 }
184
185 void XDrawRoundRect(Display *disp, Window win, GC gc, 
186                     int x, int y, int w, int h)
187 {
188 #define RAD (OFFS_X/2)
189 #define SetPoint(pn,x0,y0) pn.x=x0; pn.y=y0
190
191   if ((w<10) || (h<10))
192     XDrawRectangle(disp,win,gc,x,y,w,h);
193   else {
194     XPoint p[9];
195
196     SetPoint(p[0],x+RAD,y);
197     SetPoint(p[1],w-2*RAD,0);
198     SetPoint(p[2],RAD,RAD);
199     SetPoint(p[3],0,h-2*RAD);
200     SetPoint(p[4],-RAD,RAD);
201     SetPoint(p[5],2*RAD-w,0);
202     SetPoint(p[6],-RAD,-RAD);
203     SetPoint(p[7],0,2*RAD-h);
204     SetPoint(p[8],RAD,-RAD);
205     XDrawLines(disp,win,gc,p,9,CoordModePrevious);
206   }
207 }
208
209 void RoundRectWin(Display *disp, GC gc, t_windata *win, 
210                   int offsx, int offsy,unsigned long color)
211 {
212   XSetLineAttributes(disp,gc,1,LineOnOffDash,CapButt,JoinRound);
213   XSetForeground(disp,gc,color);
214   XDrawRoundRect(disp,win->self,gc,offsx,offsy,
215                  win->width-2*offsx-1,win->height-2*offsy-1);
216   XSetLineAttributes(disp,gc,1,LineSolid,CapButt,JoinRound);
217 }
218
219 void RectWin(Display *disp, GC gc, t_windata *win, unsigned long color)
220 {
221   int bw=1; /*2*w.bwidth;*/
222
223   XSetForeground(disp,gc,color);
224   XDrawRoundRect(disp,win->self,gc,0,0,win->width-bw,win->height-bw);
225 }
226
227 typedef struct t_mpos {
228   int    x,y;
229   struct t_mpos *prev;
230 } t_mpos;
231
232 static t_mpos *mpos=NULL;
233
234 void PushMouse(Display *disp, Window dest, int x, int y)
235 {
236   Window root,child;
237   int    root_x,root_y;
238   int    win_x,win_y;
239   unsigned int   keybut;
240   t_mpos *newpos;
241   
242   snew(newpos,1);
243   XQueryPointer(disp,DefaultRootWindow(disp),&root,&child,&root_x,&root_y,
244                 &win_x,&win_y,&keybut);
245   newpos->x=root_x;
246   newpos->y=root_y;
247   newpos->prev=mpos;
248   mpos=newpos;
249   XWarpPointer(disp,None,dest,0,0,0,0,x,y);
250 #ifdef DEBUG
251   fprintf(stderr,"Pushmouse %d, %d\n",x,y);
252 #endif
253 }
254
255 void PopMouse(Display *disp)
256 {
257   t_mpos *old;
258   
259   old=mpos;
260   if (!old) return;
261
262   XWarpPointer(disp,None,DefaultRootWindow(disp),0,0,0,0,old->x,old->y);
263 #ifdef DEBUG
264   fprintf(stderr,"Popmouse %d, %d\n",old->x,old->y);
265 #endif
266   mpos=old->prev;
267   sfree(old);
268 }
269
270 bool HelpPressed(XEvent *event)
271 {
272 #define BUFSIZE 24
273   char           buf[BUFSIZE+1];
274   XComposeStatus compose;
275   KeySym         keysym;
276
277   (void)XLookupString(&(event->xkey),buf,BUFSIZE,&keysym,&compose);
278
279   return (keysym == XK_F1);
280 }
281
282 bool GrabOK(FILE *out, int err)
283 {
284   switch (err) {
285   case GrabSuccess:
286     return TRUE;
287   case GrabNotViewable:
288     fprintf(out,"GrabNotViewable\n"); break;
289   case AlreadyGrabbed:
290     fprintf(out,"AlreadyGrabbed\n"); break;
291   case GrabFrozen:
292     fprintf(out,"GrabFrozen\n"); break;
293   case GrabInvalidTime:
294     fprintf(out,"GrabInvalidTime\n"); break;
295   default:
296     break;
297   }
298   return FALSE;
299 }
300