Apply re-formatting to C++ in src/ tree.
[alexxy/gromacs.git] / src / programs / view / pulldown.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,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 "pulldown.h"
40
41 #include <cstdio>
42 #include <cstring>
43
44 #include <algorithm>
45
46 #include "gromacs/utility/smalloc.h"
47
48 #include "popup.h"
49 #include "x11.h"
50
51 static bool PDCallBack(t_x11* x11, XEvent* event, Window w, void* data)
52 {
53     t_pulldown* pd;
54     int         i, x, x1, y, nsel;
55
56     pd = (t_pulldown*)data;
57     y  = pd->wd.height;
58     switch (event->type)
59     {
60         case Expose:
61             XSetForeground(x11->disp, x11->gc, x11->fg);
62             XDrawLine(x11->disp, w, x11->gc, 0, y - 1, pd->wd.width, y - 1);
63             for (i = 0; (i < pd->nmenu); i++)
64             {
65                 XDrawString(x11->disp,
66                             pd->wd.self,
67                             x11->gc,
68                             pd->xpos[i],
69                             x11->font->ascent,
70                             pd->title[i],
71                             std::strlen(pd->title[i]));
72             }
73             break;
74         case ButtonPress:
75             if (pd->nsel == -1)
76             {
77                 x = event->xbutton.x;
78                 for (nsel = 0; (pd->xpos[nsel + 1] < x) && (nsel < pd->nmenu - 1); nsel++)
79                 {
80                     ;
81                 }
82                 pd->nsel = nsel;
83                 x1 = std::max(0, std::min(pd_width(pd) - menu_width(pd->m[nsel]), pd->xpos[nsel]));
84                 show_menu(x11, pd->m[nsel], x1, y + 1, false);
85             }
86             break;
87         case ButtonRelease: hide_pd(x11, pd); break;
88         default: break;
89     }
90     return false;
91 }
92
93 t_pulldown* init_pd(t_x11*        x11,
94                     Window        Parent,
95                     int           width,
96                     unsigned long fg,
97                     unsigned long bg,
98                     int           nmenu,
99                     int*          nsub,
100                     t_mentry*     ent[],
101                     const char**  title)
102 {
103     t_pulldown* pd;
104     int         i;
105
106     snew(pd, 1);
107     pd->title = title;
108     pd->nmenu = nmenu;
109     pd->nsel  = -1;
110     snew(pd->m, nmenu);
111     snew(pd->xpos, nmenu + 1);
112     pd->xpos[0] = 5;
113     for (i = 1; (i <= nmenu); i++)
114     {
115         pd->xpos[i] =
116                 20 + pd->xpos[i - 1] + XTextWidth(x11->font, title[i - 1], std::strlen(title[i - 1]));
117     }
118     if (pd->xpos[nmenu] > width)
119     {
120         std::printf("Menu too wide\n");
121     }
122
123     InitWin(&(pd->wd), 0, 0, width, XTextHeight(x11->font) + 2, 0, "PullDown");
124     pd->wd.self = XCreateSimpleWindow(
125             x11->disp, Parent, pd->wd.x, pd->wd.y, pd->wd.width, pd->wd.height, pd->wd.bwidth, fg, bg);
126     x11->RegisterCallback(x11, pd->wd.self, Parent, PDCallBack, pd);
127     x11->SetInputMask(
128             x11, pd->wd.self, ExposureMask | ButtonPressMask | OwnerGrabButtonMask | ButtonReleaseMask);
129     XMapWindow(x11->disp, pd->wd.self);
130
131     for (i = 0; (i < nmenu); i++)
132     {
133         pd->m[i] = init_menu(x11, Parent, fg, bg, nsub[i], ent[i], 1);
134     }
135
136     return pd;
137 }
138
139 void hide_pd(t_x11* x11, t_pulldown* pd)
140 {
141     if (pd->nsel != -1)
142     {
143         hide_menu(x11, pd->m[pd->nsel]);
144     }
145     pd->nsel = -1;
146 }
147
148 void check_pd_item(t_pulldown* pd, int nreturn, bool bStatus)
149 {
150     int i;
151
152     for (i = 0; (i < pd->nmenu); i++)
153     {
154         check_menu_item(pd->m[i], nreturn, bStatus);
155     }
156 }
157
158 void done_pd(t_x11* x11, t_pulldown* pd)
159 {
160     int i;
161
162     for (i = 0; (i < pd->nmenu); i++)
163     {
164         done_menu(x11, pd->m[i]);
165     }
166     x11->UnRegisterCallback(x11, pd->wd.self);
167     sfree(pd->m);
168     sfree(pd->xpos);
169 }
170
171 int pd_width(t_pulldown* pd)
172 {
173     int i, w;
174
175     w = 0;
176     for (i = 0; (i < pd->nmenu); i++)
177     {
178         w = std::max(w, menu_width(pd->m[i]));
179     }
180     w = std::max(w, pd->xpos[pd->nmenu]);
181     return w;
182 }
183
184 int pd_height(t_pulldown* pd)
185 {
186     int i, h;
187
188     h = 0;
189     for (i = 0; (i < pd->nmenu); i++)
190     {
191         h = std::max(h, menu_height(pd->m[i]));
192     }
193
194     return h;
195 }