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