SYCL: Avoid using no_init read accessor in rocFFT
[alexxy/gromacs.git] / src / programs / view / popup.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,2015,2017,2019,2020, 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 "popup.h"
40
41 #include <cmath>
42 #include <cstring>
43
44 #include <algorithm>
45
46 #include "gromacs/utility/smalloc.h"
47
48 #include "x11.h"
49 #include "xutil.h"
50
51 static bool ChildCallBack(t_x11* x11, XEvent* event, Window w, void* data)
52 {
53     t_child*   child;
54     t_mentry*  m;
55     t_windata* wd;
56     XEvent     letter;
57
58     child = (t_child*)data;
59     m     = child->m;
60     wd    = &(child->wd);
61     switch (event->type)
62     {
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, eXLeft, eYCenter);
66             if (m->bChecked)
67             {
68                 int y = x11->font->ascent;
69                 XDrawLine(x11->disp, w, x11->gc, 2, (y * 2) / 3, 6, y);
70                 XDrawLine(x11->disp, w, x11->gc, 3, (y * 2) / 3, 7, y);
71                 XDrawLine(x11->disp, w, x11->gc, 7, y, 12, 2);
72             }
73             break;
74         case EnterNotify: LightBorder(x11->disp, w, x11->fg); break;
75         case LeaveNotify: LightBorder(x11->disp, w, x11->bg); break;
76         case ButtonRelease:
77             letter.type                 = ClientMessage;
78             letter.xclient.display      = x11->disp;
79             letter.xclient.window       = m->send_to ? m->send_to : child->Parent;
80             letter.xclient.message_type = 0;
81             letter.xclient.format       = 32;
82             letter.xclient.data.l[0]    = m->nreturn;
83             XSendEvent(x11->disp, letter.xclient.window, True, 0, &letter);
84             break;
85         default: break;
86     }
87     return false;
88 }
89
90 static bool MenuCallBack(t_x11* x11, XEvent* event, Window /*w*/, void* data)
91 {
92     t_menu* m;
93
94     m = (t_menu*)data;
95     switch (event->type)
96     {
97         case Expose:
98             /* Nothing to be done */
99             if (m->bGrabbed)
100             {
101                 m->bGrabbed = GrabOK(stderr,
102                                      XGrabPointer(x11->disp,
103                                                   m->wd.self,
104                                                   True,
105                                                   ButtonReleaseMask,
106                                                   GrabModeAsync,
107                                                   GrabModeAsync,
108                                                   m->wd.self,
109                                                   None,
110                                                   CurrentTime));
111             }
112             break;
113         case ButtonRelease: hide_menu(x11, m); break;
114         case ClientMessage:
115             event->xclient.window = m->Parent;
116             XSendEvent(x11->disp, m->Parent, True, 0, event);
117             break;
118         default: break;
119     }
120     return false;
121 }
122
123 t_menu* init_menu(t_x11* x11, Window Parent, unsigned long fg, unsigned long bg, int nent, t_mentry ent[], int ncol)
124 {
125     int        i, mlen, mht, area, ht;
126     int        j, k, l;
127     int        frows, fcol;
128     t_menu*    m;
129     t_child*   kid;
130     t_windata* w;
131
132     snew(m, 1);
133     m->nitem  = nent;
134     m->Parent = Parent;
135
136     /* Calculate dimensions of the menu */
137     mlen = 0;
138     for (i = 0; (i < nent); i++)
139     {
140         mlen = std::max(mlen, XTextWidth(x11->font, ent[i].str, std::strlen(ent[i].str)));
141     }
142     mht = XTextHeight(x11->font);
143     /* Now we have the biggest single box, add a border of 2 pixels */
144     mlen += 20; /* We need extra space at the left for checkmarks */
145     mht += 4;
146     /* Calculate the area of the menu */
147     area = mlen * mht;
148     ht   = std::sqrt(area);
149     /* No the number of rows per column, only beyond 8 rows */
150     if (ncol == 0)
151     {
152         if (nent > 8)
153         {
154             frows = (1 + ht / mht);
155         }
156         else
157         {
158             frows = nent;
159         }
160         fcol = nent / frows;
161     }
162     else
163     {
164         fcol  = ncol;
165         frows = nent / ncol;
166         if (nent % ncol)
167         {
168             frows++;
169         }
170     }
171     InitWin(&(m->wd), 10, 10, fcol * mlen, frows * mht, 1, "Menu");
172     snew(m->item, nent);
173     m->wd.self = XCreateSimpleWindow(
174             x11->disp, Parent, m->wd.x, m->wd.y, m->wd.width, m->wd.height, m->wd.bwidth, fg, bg);
175     x11->RegisterCallback(x11, m->wd.self, Parent, MenuCallBack, m);
176     x11->SetInputMask(x11, m->wd.self, ExposureMask | OwnerGrabButtonMask | ButtonReleaseMask);
177
178     for (j = l = 0; (j < fcol); j++)
179     {
180         for (k = 0; (k < frows) && (l < nent); k++, l++)
181         {
182             kid         = &(m->item[l]);
183             kid->m      = &(ent[l]);
184             kid->Parent = Parent;
185             w           = &(kid->wd);
186             InitWin(w, j * mlen, k * mht, mlen - 2, mht - 2, 1, nullptr);
187             w->self = XCreateSimpleWindow(
188                     x11->disp, m->wd.self, w->x, w->y, w->width, w->height, w->bwidth, bg, bg);
189             x11->RegisterCallback(x11, w->self, m->wd.self, ChildCallBack, kid);
190             x11->SetInputMask(x11,
191                               w->self,
192                               ButtonPressMask | ButtonReleaseMask | OwnerGrabButtonMask
193                                       | ExposureMask | EnterWindowMask | LeaveWindowMask);
194         }
195     }
196
197     return m;
198 }
199
200 void show_menu(t_x11* x11, t_menu* m, int x, int y, bool bGrab)
201 {
202     XMoveWindow(x11->disp, m->wd.self, x, y);
203     m->bGrabbed = bGrab;
204     XMapWindow(x11->disp, m->wd.self);
205     XMapSubwindows(x11->disp, m->wd.self);
206 }
207
208 void hide_menu(t_x11* x11, t_menu* m)
209 {
210     if (m->bGrabbed)
211     {
212         XUngrabPointer(x11->disp, CurrentTime);
213     }
214     XUnmapWindow(x11->disp, m->wd.self);
215 }
216
217 void check_menu_item(t_menu* m, int nreturn, bool bStatus)
218 {
219     int i;
220
221     for (i = 0; (i < m->nitem); i++)
222     {
223         if (m->item[i].m->nreturn == nreturn)
224         {
225             m->item[i].m->bChecked = bStatus;
226         }
227     }
228 }
229
230 void done_menu(t_x11* x11, t_menu* m)
231 {
232     int i;
233
234     for (i = 0; (i < m->nitem); i++)
235     {
236         x11->UnRegisterCallback(x11, m->item[i].wd.self);
237     }
238     sfree(m->item);
239     x11->UnRegisterCallback(x11, m->wd.self);
240     sfree(m);
241 }
242
243 int menu_width(t_menu* m)
244 {
245     return m->wd.width;
246 }
247
248 int menu_height(t_menu* m)
249 {
250     return m->wd.height;
251 }