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