Fix AVX-512 SIMD test for C
[gromacs.git] / src / programs / view / xutil.cpp
blob2c53b3e78508953665f28840446ce925e1130a69
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2013, The GROMACS development team.
6 * Copyright (c) 2013,2014,2015,2017, 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.
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.
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.
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.
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.
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.
37 #include "gmxpre.h"
39 #include "xutil.h"
41 #include <cstdio>
42 #include <cstring>
44 #include "gromacs/utility/cstringutil.h"
45 #include "gromacs/utility/smalloc.h"
47 #include "Xstuff.h"
49 int CheckWin(Window win, const char *file, int line)
51 typedef struct {
52 Window n;
53 const char *s;
54 } t_winerr;
55 t_winerr winerr[] = {
56 { BadAlloc, "Bad Alloc" },
57 { BadColor, "Bad Color" },
58 { BadCursor, "Bad Cursor"},
59 { BadMatch, "Bad Match" },
60 { BadPixmap, "Bad Pixmap"},
61 { BadValue, "Bad Value" },
62 { BadWindow, "Bad Window"}
64 #define NERR (sizeof(winerr)/sizeof(winerr[0]))
65 unsigned int i;
67 for (i = 0; (i < NERR); i++)
69 if (win == winerr[i].n)
71 std::fprintf(stderr, "%s", winerr[i].s);
72 break;
75 if (i == NERR)
77 return 1;
80 std::fprintf(stderr, " in file %s, line %d\n", file, line);
81 return 0;
84 void LightBorder(Display *disp, Window win, unsigned long color)
86 XSetWindowAttributes attributes;
88 attributes.border_pixel = color;
89 XChangeWindowAttributes(disp, win, CWBorderPixel, &attributes);
92 void SpecialTextInRect(t_x11 *x11, XFontStruct *font, Drawable win,
93 const char *s, int x, int y, int width, int height,
94 eXPos eX, eYPos eY)
96 int fw, fh, x0, y0;
97 XFontStruct *f;
99 if (font)
101 XSetFont(x11->disp, x11->gc, font->fid);
102 f = font;
104 else
106 f = x11->font;
109 fw = XTextWidth(f, s, std::strlen(s));
110 fh = XTextHeight(f);
111 switch (eX)
113 case eXLeft:
114 x0 = x;
115 break;
116 case eXRight:
117 x0 = x+width-fw;
118 break;
119 case eXCenter:
120 default:
121 x0 = x+(width-fw)/2;
122 break;
124 switch (eY)
126 case eYTop:
127 y0 = y+f->ascent;
128 break;
129 case eYBottom:
130 y0 = y+height-f->descent;
131 break;
132 case eYCenter:
133 default:
134 y0 = y+(height-fh)/2+f->ascent;
135 break;
137 XDrawString(x11->disp, win, x11->gc, x0, y0, s, std::strlen(s));
138 if (font)
140 XSetFont(x11->disp, x11->gc, x11->font->fid);
144 void TextInRect(t_x11 *x11, Drawable win,
145 const char *s, int x, int y, int width, int height,
146 eXPos eX, eYPos eY)
148 SpecialTextInRect(x11, nullptr, win, s, x, y, width, height, eX, eY);
151 void TextInWin(t_x11 *x11, t_windata *win,
152 const char *s, eXPos eX, eYPos eY)
154 TextInRect(x11, win->self, s, 0, 0, win->width, win->height, eX, eY);
157 void InitWin(t_windata *win, int x0, int y0, int w, int h, int bw, const char *text)
159 win->self = 0;
160 win->color = 0;
161 win->x = x0;
162 win->y = y0;
163 win->width = w;
164 win->height = h;
165 win->bwidth = bw;
166 win->bFocus = false;
167 win->cursor = 0;
168 if (text)
170 win->text = gmx_strdup(text);
172 else
174 win->text = nullptr;
176 #ifdef DEBUG
177 std::printf("%s: %d x %d at %d, %d\n", text, w, h, x0, y0);
178 #endif
181 void FreeWin(Display *disp, t_windata *win)
183 if (win->text)
185 sfree(win->text);
187 if (win->cursor)
189 XFreeCursor(disp, win->cursor);
193 void ExposeWin(Display *disp, Window win)
195 XEvent event;
197 event.type = Expose;
198 event.xexpose.send_event = True;
199 event.xexpose.window = win;
200 event.xexpose.x = 0;
201 event.xexpose.y = 0;
202 event.xexpose.width = 1000;
203 event.xexpose.height = 1000;
204 event.xexpose.count = 0;
205 XSendEvent(disp, win, False, ExposureMask, &event);
208 void XDrawRoundRect(Display *disp, Window win, GC gc,
209 int x, int y, int w, int h)
211 #define RAD (OFFS_X/2)
212 #define SetPoint(pn, x0, y0) pn.x = x0; pn.y = y0
214 if ((w < 10) || (h < 10))
216 XDrawRectangle(disp, win, gc, x, y, w, h);
218 else
220 XPoint p[9];
222 SetPoint(p[0], x+RAD, y);
223 SetPoint(p[1], w-2*RAD, 0);
224 SetPoint(p[2], RAD, RAD);
225 SetPoint(p[3], 0, h-2*RAD);
226 SetPoint(p[4], -RAD, RAD);
227 SetPoint(p[5], 2*RAD-w, 0);
228 SetPoint(p[6], -RAD, -RAD);
229 SetPoint(p[7], 0, 2*RAD-h);
230 SetPoint(p[8], RAD, -RAD);
231 XDrawLines(disp, win, gc, p, 9, CoordModePrevious);
235 void RoundRectWin(Display *disp, GC gc, t_windata *win,
236 int offsx, int offsy, unsigned long color)
238 XSetLineAttributes(disp, gc, 1, LineOnOffDash, CapButt, JoinRound);
239 XSetForeground(disp, gc, color);
240 XDrawRoundRect(disp, win->self, gc, offsx, offsy,
241 win->width-2*offsx-1, win->height-2*offsy-1);
242 XSetLineAttributes(disp, gc, 1, LineSolid, CapButt, JoinRound);
245 void RectWin(Display *disp, GC gc, t_windata *win, unsigned long color)
247 int bw = 1; /*2*w.bwidth;*/
249 XSetForeground(disp, gc, color);
250 XDrawRoundRect(disp, win->self, gc, 0, 0, win->width-bw, win->height-bw);
253 typedef struct t_mpos {
254 int x, y;
255 struct t_mpos *prev;
256 } t_mpos;
258 static t_mpos *mpos = nullptr;
260 void PushMouse(Display *disp, Window dest, int x, int y)
262 Window root, child;
263 int root_x, root_y;
264 int win_x, win_y;
265 unsigned int keybut;
266 t_mpos *newpos;
268 snew(newpos, 1);
269 XQueryPointer(disp, DefaultRootWindow(disp), &root, &child, &root_x, &root_y,
270 &win_x, &win_y, &keybut);
271 newpos->x = root_x;
272 newpos->y = root_y;
273 newpos->prev = mpos;
274 mpos = newpos;
275 XWarpPointer(disp, None, dest, 0, 0, 0, 0, x, y);
276 #ifdef DEBUG
277 std::fprintf(stderr, "Pushmouse %d, %d\n", x, y);
278 #endif
281 void PopMouse(Display *disp)
283 t_mpos *old;
285 old = mpos;
286 if (!old)
288 return;
291 XWarpPointer(disp, None, DefaultRootWindow(disp), 0, 0, 0, 0, old->x, old->y);
292 #ifdef DEBUG
293 std::fprintf(stderr, "Popmouse %d, %d\n", old->x, old->y);
294 #endif
295 mpos = old->prev;
296 sfree(old);
299 bool HelpPressed(XEvent *event)
301 #define BUFSIZE 24
302 char buf[BUFSIZE+1];
303 XComposeStatus compose;
304 KeySym keysym;
306 (void)XLookupString(&(event->xkey), buf, BUFSIZE, &keysym, &compose);
308 return (keysym == XK_F1);
311 bool GrabOK(FILE *out, int err)
313 switch (err)
315 case GrabSuccess:
316 return true;
317 case GrabNotViewable:
318 std::fprintf(out, "GrabNotViewable\n"); break;
319 case AlreadyGrabbed:
320 std::fprintf(out, "AlreadyGrabbed\n"); break;
321 case GrabFrozen:
322 std::fprintf(out, "GrabFrozen\n"); break;
323 case GrabInvalidTime:
324 std::fprintf(out, "GrabInvalidTime\n"); break;
325 default:
326 break;
328 return false;