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