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