Fix AVX-512 SIMD test for C
[gromacs.git] / src / programs / view / popup.cpp
blob94fae9e02073980f8bb25f5af74edff58d5fdd99
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 "popup.h"
41 #include <cmath>
42 #include <cstring>
44 #include <algorithm>
46 #include "gromacs/utility/smalloc.h"
48 #include "x11.h"
49 #include "xutil.h"
51 static bool ChildCallBack(t_x11 *x11, XEvent *event, Window w, void *data)
53 t_child *child;
54 t_mentry *m;
55 t_windata *wd;
56 XEvent letter;
58 child = (t_child *)data;
59 m = child->m;
60 wd = &(child->wd);
61 switch (event->type)
63 case Expose:
64 XSetForeground(x11->disp, x11->gc, x11->fg);
65 TextInRect(x11, w, m->str, 16, 0, wd->width-16-2, wd->height-2,
66 eXLeft, eYCenter);
67 if (m->bChecked)
69 int y = x11->font->ascent;
70 XDrawLine(x11->disp, w, x11->gc, 2, (y*2)/3, 6, y);
71 XDrawLine(x11->disp, w, x11->gc, 3, (y*2)/3, 7, y);
72 XDrawLine(x11->disp, w, x11->gc, 7, y, 12, 2);
74 break;
75 case EnterNotify:
76 LightBorder(x11->disp, w, x11->fg);
77 break;
78 case LeaveNotify:
79 LightBorder(x11->disp, w, x11->bg);
80 break;
81 case ButtonRelease:
82 letter.type = ClientMessage;
83 letter.xclient.display = x11->disp;
84 letter.xclient.window = m->send_to ? m->send_to : child->Parent;
85 letter.xclient.message_type = 0;
86 letter.xclient.format = 32;
87 letter.xclient.data.l[0] = m->nreturn;
88 XSendEvent(x11->disp, letter.xclient.window, True, 0, &letter);
89 break;
90 default:
91 break;
93 return false;
96 static bool MenuCallBack(t_x11 *x11, XEvent *event, Window /*w*/, void *data)
98 t_menu *m;
100 m = (t_menu *)data;
101 switch (event->type)
103 case Expose:
104 /* Nothing to be done */
105 if (m->bGrabbed)
107 m->bGrabbed =
108 GrabOK(stderr, XGrabPointer(x11->disp, m->wd.self, True,
109 ButtonReleaseMask, GrabModeAsync,
110 GrabModeAsync, m->wd.self, None, CurrentTime));
112 break;
113 case ButtonRelease:
114 hide_menu(x11, m);
115 break;
116 case ClientMessage:
117 event->xclient.window = m->Parent;
118 XSendEvent(x11->disp, m->Parent, True, 0, event);
119 break;
120 default:
121 break;
123 return false;
126 t_menu *init_menu(t_x11 *x11, Window Parent, unsigned long fg, unsigned long bg,
127 int nent, t_mentry ent[], int ncol)
129 int i, mlen, mht, area, ht;
130 int j, k, l;
131 int frows, fcol;
132 t_menu *m;
133 t_child *kid;
134 t_windata *w;
136 snew(m, 1);
137 m->nitem = nent;
138 m->Parent = Parent;
140 /* Calculate dimensions of the menu */
141 mlen = 0;
142 for (i = 0; (i < nent); i++)
144 mlen = std::max(mlen, XTextWidth(x11->font, ent[i].str, std::strlen(ent[i].str)));
146 mht = XTextHeight(x11->font);
147 /* Now we have the biggest single box, add a border of 2 pixels */
148 mlen += 20; /* We need extra space at the left for checkmarks */
149 mht += 4;
150 /* Calculate the area of the menu */
151 area = mlen*mht;
152 ht = std::sqrt(area);
153 /* No the number of rows per column, only beyond 8 rows */
154 if (ncol == 0)
156 if (nent > 8)
158 frows = (1+ht/mht);
160 else
162 frows = nent;
164 fcol = nent/frows;
166 else
168 fcol = ncol;
169 frows = nent/ncol;
170 if (nent % ncol)
172 frows++;
175 InitWin(&(m->wd), 10, 10, fcol*mlen, frows*mht, 1, "Menu");
176 snew(m->item, nent);
177 m->wd.self = XCreateSimpleWindow(x11->disp, Parent,
178 m->wd.x, m->wd.y,
179 m->wd.width, m->wd.height,
180 m->wd.bwidth, fg, bg);
181 x11->RegisterCallback(x11, m->wd.self, Parent, MenuCallBack, m);
182 x11->SetInputMask(x11, m->wd.self, ExposureMask |
183 OwnerGrabButtonMask | ButtonReleaseMask);
185 for (j = l = 0; (j < fcol); j++)
187 for (k = 0; (k < frows) && (l < nent); k++, l++)
189 kid = &(m->item[l]);
190 kid->m = &(ent[l]);
191 kid->Parent = Parent;
192 w = &(kid->wd);
193 InitWin(w, j*mlen, k*mht, mlen-2, mht-2, 1, nullptr);
194 w->self = XCreateSimpleWindow(x11->disp, m->wd.self,
195 w->x, w->y, w->width, w->height,
196 w->bwidth, bg, bg);
197 x11->RegisterCallback(x11, w->self, m->wd.self,
198 ChildCallBack, kid);
199 x11->SetInputMask(x11, w->self,
200 ButtonPressMask | ButtonReleaseMask |
201 OwnerGrabButtonMask | ExposureMask |
202 EnterWindowMask | LeaveWindowMask);
206 return m;
209 void show_menu(t_x11 *x11, t_menu *m, int x, int y, bool bGrab)
211 XMoveWindow(x11->disp, m->wd.self, x, y);
212 m->bGrabbed = bGrab;
213 XMapWindow(x11->disp, m->wd.self);
214 XMapSubwindows(x11->disp, m->wd.self);
217 void hide_menu(t_x11 *x11, t_menu *m)
219 if (m->bGrabbed)
221 XUngrabPointer(x11->disp, CurrentTime);
223 XUnmapWindow(x11->disp, m->wd.self);
226 void check_menu_item(t_menu *m, int nreturn, bool bStatus)
228 int i;
230 for (i = 0; (i < m->nitem); i++)
232 if (m->item[i].m->nreturn == nreturn)
234 m->item[i].m->bChecked = bStatus;
239 void done_menu(t_x11 *x11, t_menu *m)
241 int i;
243 for (i = 0; (i < m->nitem); i++)
245 x11->UnRegisterCallback(x11, m->item[i].wd.self);
247 sfree(m->item);
248 x11->UnRegisterCallback(x11, m->wd.self);
249 sfree(m);
252 int menu_width(t_menu *m)
254 return m->wd.width;
257 int menu_height(t_menu *m)
259 return m->wd.height;