Merge release-5-0 into master
[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 "config.h"
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include "gromacs/utility/smalloc.h"
43 #include "typedefs.h"
44 #include "xutil.h"
45 #include "Xstuff.h"
46
47 int CheckWin(Window win, const char *file, int line)
48 {
49     typedef struct {
50         Window      n;
51         const 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     unsigned int      i;
64
65     for (i = 0; (i < NERR); i++)
66     {
67         if (win == winerr[i].n)
68         {
69             fprintf(stderr, "%s", winerr[i].s);
70             break;
71         }
72     }
73     if (i == NERR)
74     {
75         return 1;
76     }
77
78     fprintf(stderr, " in file %s, line %d\n", file, line);
79     return 0;
80 }
81
82 void LightBorder(Display *disp, Window win, unsigned long color)
83 {
84     XSetWindowAttributes attributes;
85
86     attributes.border_pixel = color;
87     XChangeWindowAttributes(disp, win, CWBorderPixel, &attributes);
88 }
89
90 void SpecialTextInRect(t_x11 *x11, XFontStruct *font, Drawable win,
91                        const char *s, int x, int y, int width, int height,
92                        eXPos eX, eYPos eY)
93 {
94     int          fw, fh, x0, y0;
95     XFontStruct *f;
96
97     if (font)
98     {
99         XSetFont(x11->disp, x11->gc, font->fid);
100         f = font;
101     }
102     else
103     {
104         f = x11->font;
105     }
106
107     fw = XTextWidth(f, s, strlen(s));
108     fh = XTextHeight(f);
109     switch (eX)
110     {
111         case eXLeft:
112             x0 = x;
113             break;
114         case eXRight:
115             x0 = x+width-fw;
116             break;
117         case eXCenter:
118         default:
119             x0 = x+(width-fw)/2;
120             break;
121     }
122     switch (eY)
123     {
124         case eYTop:
125             y0 = y+f->ascent;
126             break;
127         case eYBottom:
128             y0 = y+height-f->descent;
129             break;
130         case eYCenter:
131         default:
132             y0 = y+(height-fh)/2+f->ascent;
133             break;
134     }
135     XDrawString(x11->disp, win, x11->gc, x0, y0, s, strlen(s));
136     if (font)
137     {
138         XSetFont(x11->disp, x11->gc, x11->font->fid);
139     }
140 }
141
142 void TextInRect(t_x11 *x11, Drawable win,
143                 const char *s, int x, int y, int width, int height,
144                 eXPos eX, eYPos eY)
145 {
146     SpecialTextInRect(x11, NULL, win, s, x, y, width, height, eX, eY);
147 }
148
149 void TextInWin(t_x11 *x11, t_windata *win,
150                const char *s, eXPos eX, eYPos eY)
151 {
152     TextInRect(x11, win->self, s, 0, 0, win->width, win->height, eX, eY);
153 }
154
155 void InitWin(t_windata *win, int x0, int y0, int w, int h, int bw, const char *text)
156 {
157     win->self   = 0;
158     win->color  = 0;
159     win->x      = x0;
160     win->y      = y0;
161     win->width  = w;
162     win->height = h;
163     win->bwidth = bw;
164     win->bFocus = false;
165     win->cursor = 0;
166     if (text)
167     {
168         win->text = strdup(text);
169     }
170     else
171     {
172         win->text = NULL;
173     }
174 #ifdef DEBUG
175     printf("%s: %d x %d at %d, %d\n", text, w, h, x0, y0);
176 #endif
177 }
178
179 void FreeWin(Display *disp, t_windata *win)
180 {
181     if (win->text)
182     {
183         sfree(win->text);
184     }
185     if (win->cursor)
186     {
187         XFreeCursor(disp, win->cursor);
188     }
189 }
190
191 void ExposeWin(Display *disp, Window win)
192 {
193     XEvent event;
194
195     event.type               = Expose;
196     event.xexpose.send_event = True;
197     event.xexpose.window     = win;
198     event.xexpose.x          = 0;
199     event.xexpose.y          = 0;
200     event.xexpose.width      = 1000;
201     event.xexpose.height     = 1000;
202     event.xexpose.count      = 0;
203     XSendEvent(disp, win, False, ExposureMask, &event);
204 }
205
206 void XDrawRoundRect(Display *disp, Window win, GC gc,
207                     int x, int y, int w, int h)
208 {
209 #define RAD (OFFS_X/2)
210 #define SetPoint(pn, x0, y0) pn.x = x0; pn.y = y0
211
212     if ((w < 10) || (h < 10))
213     {
214         XDrawRectangle(disp, win, gc, x, y, w, h);
215     }
216     else
217     {
218         XPoint p[9];
219
220         SetPoint(p[0], x+RAD, y);
221         SetPoint(p[1], w-2*RAD, 0);
222         SetPoint(p[2], RAD, RAD);
223         SetPoint(p[3], 0, h-2*RAD);
224         SetPoint(p[4], -RAD, RAD);
225         SetPoint(p[5], 2*RAD-w, 0);
226         SetPoint(p[6], -RAD, -RAD);
227         SetPoint(p[7], 0, 2*RAD-h);
228         SetPoint(p[8], RAD, -RAD);
229         XDrawLines(disp, win, gc, p, 9, CoordModePrevious);
230     }
231 }
232
233 void RoundRectWin(Display *disp, GC gc, t_windata *win,
234                   int offsx, int offsy, unsigned long color)
235 {
236     XSetLineAttributes(disp, gc, 1, LineOnOffDash, CapButt, JoinRound);
237     XSetForeground(disp, gc, color);
238     XDrawRoundRect(disp, win->self, gc, offsx, offsy,
239                    win->width-2*offsx-1, win->height-2*offsy-1);
240     XSetLineAttributes(disp, gc, 1, LineSolid, CapButt, JoinRound);
241 }
242
243 void RectWin(Display *disp, GC gc, t_windata *win, unsigned long color)
244 {
245     int bw = 1; /*2*w.bwidth;*/
246
247     XSetForeground(disp, gc, color);
248     XDrawRoundRect(disp, win->self, gc, 0, 0, win->width-bw, win->height-bw);
249 }
250
251 typedef struct t_mpos {
252     int            x, y;
253     struct t_mpos *prev;
254 } t_mpos;
255
256 static t_mpos *mpos = NULL;
257
258 void PushMouse(Display *disp, Window dest, int x, int y)
259 {
260     Window         root, child;
261     int            root_x, root_y;
262     int            win_x, win_y;
263     unsigned int   keybut;
264     t_mpos        *newpos;
265
266     snew(newpos, 1);
267     XQueryPointer(disp, DefaultRootWindow(disp), &root, &child, &root_x, &root_y,
268                   &win_x, &win_y, &keybut);
269     newpos->x    = root_x;
270     newpos->y    = root_y;
271     newpos->prev = mpos;
272     mpos         = newpos;
273     XWarpPointer(disp, None, dest, 0, 0, 0, 0, x, y);
274 #ifdef DEBUG
275     fprintf(stderr, "Pushmouse %d, %d\n", x, y);
276 #endif
277 }
278
279 void PopMouse(Display *disp)
280 {
281     t_mpos *old;
282
283     old = mpos;
284     if (!old)
285     {
286         return;
287     }
288
289     XWarpPointer(disp, None, DefaultRootWindow(disp), 0, 0, 0, 0, old->x, old->y);
290 #ifdef DEBUG
291     fprintf(stderr, "Popmouse %d, %d\n", old->x, old->y);
292 #endif
293     mpos = old->prev;
294     sfree(old);
295 }
296
297 bool HelpPressed(XEvent *event)
298 {
299 #define BUFSIZE 24
300     char           buf[BUFSIZE+1];
301     XComposeStatus compose;
302     KeySym         keysym;
303
304     (void)XLookupString(&(event->xkey), buf, BUFSIZE, &keysym, &compose);
305
306     return (keysym == XK_F1);
307 }
308
309 bool GrabOK(FILE *out, int err)
310 {
311     switch (err)
312     {
313         case GrabSuccess:
314             return true;
315         case GrabNotViewable:
316             fprintf(out, "GrabNotViewable\n"); break;
317         case AlreadyGrabbed:
318             fprintf(out, "AlreadyGrabbed\n"); break;
319         case GrabFrozen:
320             fprintf(out, "GrabFrozen\n"); break;
321         case GrabInvalidTime:
322             fprintf(out, "GrabInvalidTime\n"); break;
323         default:
324             break;
325     }
326     return false;
327 }