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