Merge branch 'master' of git://git.gromacs.org/gromacs
[gromacs/adressmacs.git] / src / ngmx / xutil.c
blobc5a9f7ea33e8ba93824181c0d5758892fa5c5ce9
1 /*
2 *
3 * This source code is part of
4 *
5 * G R O M A C S
6 *
7 * GROningen MAchine for Chemical Simulations
8 *
9 * VERSION 3.2.0
10 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12 * Copyright (c) 2001-2004, The GROMACS development team,
13 * check out http://www.gromacs.org for more information.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * If you want to redistribute modifications, please consider that
21 * scientific software is very special. Version control is crucial -
22 * bugs must be traceable. We will be happy to consider code for
23 * inclusion in the official distribution, but derived work must not
24 * be called official GROMACS. Details are found in the README & COPYING
25 * files - if they are missing, get the official version at www.gromacs.org.
27 * To help us fund GROMACS development, we humbly ask that you cite
28 * the papers on the package - you can find them in the top README file.
30 * For more info, check our website at http://www.gromacs.org
32 * And Hey:
33 * Gyas ROwers Mature At Cryogenic Speed
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <xutil.h>
43 #include <Xstuff.h>
44 #include "smalloc.h"
45 #include "typedefs.h"
46 #include "string2.h"
48 int CheckWin(Window win,const char *file, int line)
50 typedef struct {
51 int n;
52 const char *s;
53 } t_winerr;
54 t_winerr winerr[] = {
55 { BadAlloc, "Bad Alloc" },
56 { BadColor, "Bad Color" },
57 { BadCursor, "Bad Cursor"},
58 { BadMatch, "Bad Match" },
59 { BadPixmap, "Bad Pixmap"},
60 { BadValue, "Bad Value" },
61 { BadWindow, "Bad Window"}
62 };
63 #define NERR (sizeof(winerr)/sizeof(winerr[0]))
64 int i;
66 for(i=0; (i<NERR); i++)
67 if (win==winerr[i].n) {
68 fprintf(stderr,"%s",winerr[i].s);
69 break;
71 if (i==NERR) return 1;
73 fprintf(stderr," in file %s, line %d\n",file,line);
74 return 0;
77 void LightBorder(Display *disp, Window win, unsigned long color)
79 XSetWindowAttributes attributes;
81 attributes.border_pixel = color;
82 XChangeWindowAttributes(disp,win,CWBorderPixel,&attributes);
85 void SpecialTextInRect(t_x11 *x11,XFontStruct *font,Drawable win,
86 const char *s,int x,int y,int width,int height,
87 eXPos eX,eYPos eY)
89 int fw,fh,x0,y0;
90 XFontStruct *f;
92 if (font) {
93 XSetFont(x11->disp,x11->gc,font->fid);
94 f=font;
96 else
97 f=x11->font;
99 fw=XTextWidth(f,s,strlen(s));
100 fh=XTextHeight(f);
101 switch (eX) {
102 case eXLeft:
103 x0=x;
104 break;
105 case eXRight:
106 x0=x+width-fw;
107 break;
108 case eXCenter:
109 default:
110 x0=x+(width-fw)/2;
111 break;
113 switch (eY) {
114 case eYTop:
115 y0=y+f->ascent;
116 break;
117 case eYBottom:
118 y0=y+height-f->descent;
119 break;
120 case eYCenter:
121 default:
122 y0=y+(height-fh)/2+f->ascent;
123 break;
125 XDrawString(x11->disp,win,x11->gc,x0,y0,s,strlen(s));
126 if (font)
127 XSetFont(x11->disp,x11->gc,x11->font->fid);
130 void TextInRect(t_x11 *x11,Drawable win,
131 const char *s,int x,int y,int width,int height,
132 eXPos eX,eYPos eY)
134 SpecialTextInRect(x11,NULL,win,s,x,y,width,height,eX,eY);
137 void TextInWin(t_x11 *x11, t_windata *win,
138 const char *s, eXPos eX, eYPos eY)
140 TextInRect(x11,win->self,s,0,0,win->width,win->height,eX,eY);
143 void InitWin(t_windata *win, int x0,int y0, int w, int h, int bw, const char *text)
145 win->self=0;
146 win->color=0;
147 win->x=x0;
148 win->y=y0;
149 win->width=w;
150 win->height=h;
151 win->bwidth=bw;
152 win->bFocus=FALSE;
153 win->cursor=0;
154 if (text)
155 win->text=strdup(text);
156 else
157 win->text=NULL;
158 #ifdef DEBUG
159 printf("%s: %d x %d at %d, %d\n",text,w,h,x0,y0);
160 #endif
163 void FreeWin(Display *disp, t_windata *win)
165 if (win->text)
166 sfree(win->text);
167 if (win->cursor)
168 XFreeCursor(disp,win->cursor);
171 void ExposeWin(Display *disp,Window win)
173 XEvent event;
175 event.type = Expose;
176 event.xexpose.send_event=True;
177 event.xexpose.window = win;
178 event.xexpose.x=0;
179 event.xexpose.y=0;
180 event.xexpose.width=1000;
181 event.xexpose.height=1000;
182 event.xexpose.count = 0;
183 XSendEvent(disp,win,False,ExposureMask,&event);
186 void XDrawRoundRect(Display *disp, Window win, GC gc,
187 int x, int y, int w, int h)
189 #define RAD (OFFS_X/2)
190 #define SetPoint(pn,x0,y0) pn.x=x0; pn.y=y0
192 if ((w<10) || (h<10))
193 XDrawRectangle(disp,win,gc,x,y,w,h);
194 else {
195 XPoint p[9];
197 SetPoint(p[0],x+RAD,y);
198 SetPoint(p[1],w-2*RAD,0);
199 SetPoint(p[2],RAD,RAD);
200 SetPoint(p[3],0,h-2*RAD);
201 SetPoint(p[4],-RAD,RAD);
202 SetPoint(p[5],2*RAD-w,0);
203 SetPoint(p[6],-RAD,-RAD);
204 SetPoint(p[7],0,2*RAD-h);
205 SetPoint(p[8],RAD,-RAD);
206 XDrawLines(disp,win,gc,p,9,CoordModePrevious);
210 void RoundRectWin(Display *disp, GC gc, t_windata *win,
211 int offsx, int offsy,unsigned long color)
213 XSetLineAttributes(disp,gc,1,LineOnOffDash,CapButt,JoinRound);
214 XSetForeground(disp,gc,color);
215 XDrawRoundRect(disp,win->self,gc,offsx,offsy,
216 win->width-2*offsx-1,win->height-2*offsy-1);
217 XSetLineAttributes(disp,gc,1,LineSolid,CapButt,JoinRound);
220 void RectWin(Display *disp, GC gc, t_windata *win, unsigned long color)
222 int bw=1; /*2*w.bwidth;*/
224 XSetForeground(disp,gc,color);
225 XDrawRoundRect(disp,win->self,gc,0,0,win->width-bw,win->height-bw);
228 typedef struct t_mpos {
229 int x,y;
230 struct t_mpos *prev;
231 } t_mpos;
233 static t_mpos *mpos=NULL;
235 void PushMouse(Display *disp, Window dest, int x, int y)
237 Window root,child;
238 int root_x,root_y;
239 int win_x,win_y;
240 unsigned int keybut;
241 t_mpos *newpos;
243 snew(newpos,1);
244 XQueryPointer(disp,DefaultRootWindow(disp),&root,&child,&root_x,&root_y,
245 &win_x,&win_y,&keybut);
246 newpos->x=root_x;
247 newpos->y=root_y;
248 newpos->prev=mpos;
249 mpos=newpos;
250 XWarpPointer(disp,None,dest,0,0,0,0,x,y);
251 #ifdef DEBUG
252 fprintf(stderr,"Pushmouse %d, %d\n",x,y);
253 #endif
256 void PopMouse(Display *disp)
258 t_mpos *old;
260 old=mpos;
261 if (!old) return;
263 XWarpPointer(disp,None,DefaultRootWindow(disp),0,0,0,0,old->x,old->y);
264 #ifdef DEBUG
265 fprintf(stderr,"Popmouse %d, %d\n",old->x,old->y);
266 #endif
267 mpos=old->prev;
268 sfree(old);
271 gmx_bool HelpPressed(XEvent *event)
273 #define BUFSIZE 24
274 char buf[BUFSIZE+1];
275 XComposeStatus compose;
276 KeySym keysym;
278 (void)XLookupString(&(event->xkey),buf,BUFSIZE,&keysym,&compose);
280 return (keysym == XK_F1);
283 gmx_bool GrabOK(FILE *out, int err)
285 switch (err) {
286 case GrabSuccess:
287 return TRUE;
288 case GrabNotViewable:
289 fprintf(out,"GrabNotViewable\n"); break;
290 case AlreadyGrabbed:
291 fprintf(out,"AlreadyGrabbed\n"); break;
292 case GrabFrozen:
293 fprintf(out,"GrabFrozen\n"); break;
294 case GrabInvalidTime:
295 fprintf(out,"GrabInvalidTime\n"); break;
296 default:
297 break;
299 return FALSE;