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