Remove repeat sfree(state->nosehoover_xi)
[alexxy/gromacs.git] / src / programs / view / popup.cpp
1 /*
2  *
3  *                This source code is part of
4  *
5  *                 G   R   O   M   A   C   S
6  *
7  *          GROningen MAchine for Chemical Simulations
8  *
9  *                        VERSION 3.2.0
10  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12  * Copyright (c) 2001-2013, The GROMACS development team,
13  * check out http://www.gromacs.org for more information.
14
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * If you want to redistribute modifications, please consider that
21  * scientific software is very special. Version control is crucial -
22  * bugs must be traceable. We will be happy to consider code for
23  * inclusion in the official distribution, but derived work must not
24  * be called official GROMACS. Details are found in the README & COPYING
25  * files - if they are missing, get the official version at www.gromacs.org.
26  *
27  * To help us fund GROMACS development, we humbly ask that you cite
28  * the papers on the package - you can find them in the top README file.
29  *
30  * For more info, check our website at http://www.gromacs.org
31  *
32  * And Hey:
33  * Gyas ROwers Mature At Cryogenic Speed
34  */
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #include <string.h>
40 #include <math.h>
41 #include <macros.h>
42 #include <smalloc.h>
43 #include "x11.h"
44 #include "xutil.h"
45 #include "popup.h"
46
47 bool ChildCallBack(t_x11 *x11, XEvent *event, Window w, void *data)
48 {
49     t_child   *child;
50     t_mentry  *m;
51     t_windata *wd;
52     XEvent     letter;
53
54     child = (t_child *)data;
55     m     = child->m;
56     wd    = &(child->wd);
57     switch (event->type)
58     {
59         case Expose:
60             XSetForeground(x11->disp, x11->gc, x11->fg);
61             TextInRect(x11, w, m->str, 16, 0, wd->width-16-2, wd->height-2,
62                        eXLeft, eYCenter);
63             if (m->bChecked)
64             {
65                 int y = x11->font->ascent;
66                 XDrawLine(x11->disp, w, x11->gc, 2, (y*2)/3, 6, y);
67                 XDrawLine(x11->disp, w, x11->gc, 3, (y*2)/3, 7, y);
68                 XDrawLine(x11->disp, w, x11->gc, 7, y, 12, 2);
69             }
70             break;
71         case EnterNotify:
72             LightBorder(x11->disp, w, x11->fg);
73             break;
74         case LeaveNotify:
75             LightBorder(x11->disp, w, x11->bg);
76             break;
77         case ButtonRelease:
78             letter.type                 = ClientMessage;
79             letter.xclient.display      = x11->disp;
80             letter.xclient.window       = m->send_to ? m->send_to : child->Parent;
81             letter.xclient.message_type = 0;
82             letter.xclient.format       = 32;
83             letter.xclient.data.l[0]    = m->nreturn;
84             XSendEvent(x11->disp, letter.xclient.window, True, 0, &letter);
85             break;
86         default:
87             break;
88     }
89     return false;
90 }
91
92 bool MenuCallBack(t_x11 *x11, XEvent *event, Window w, void *data)
93 {
94     t_menu *m;
95
96     m = (t_menu *)data;
97     switch (event->type)
98     {
99         case Expose:
100             /* Nothing to be done */
101             if (m->bGrabbed)
102             {
103                 m->bGrabbed =
104                     GrabOK(stderr, XGrabPointer(x11->disp, m->wd.self, True,
105                                                 ButtonReleaseMask, GrabModeAsync,
106                                                 GrabModeAsync, m->wd.self, None, CurrentTime));
107             }
108             break;
109         case ButtonRelease:
110             hide_menu(x11, m);
111             break;
112         case ClientMessage:
113             event->xclient.window = m->Parent;
114             XSendEvent(x11->disp, m->Parent, True, 0, event);
115             break;
116         default:
117             break;
118     }
119     return false;
120 }
121
122 t_menu *init_menu(t_x11 *x11, Window Parent, unsigned long fg, unsigned long bg,
123                   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, 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   = 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(x11->disp, Parent,
174                                      m->wd.x, m->wd.y,
175                                      m->wd.width, m->wd.height,
176                                      m->wd.bwidth, fg, bg);
177     x11->RegisterCallback(x11, m->wd.self, Parent, MenuCallBack, m);
178     x11->SetInputMask(x11, m->wd.self, ExposureMask |
179                       OwnerGrabButtonMask | ButtonReleaseMask);
180
181     for (j = l = 0; (j < fcol); j++)
182     {
183         for (k = 0; (k < frows) && (l < nent); k++, l++)
184         {
185             kid         = &(m->item[l]);
186             kid->m      = &(ent[l]);
187             kid->Parent = Parent;
188             w           = &(kid->wd);
189             InitWin(w, j*mlen, k*mht, mlen-2, mht-2, 1, NULL);
190             w->self = XCreateSimpleWindow(x11->disp, m->wd.self,
191                                           w->x, w->y, w->width, w->height,
192                                           w->bwidth, bg, bg);
193             x11->RegisterCallback(x11, w->self, m->wd.self,
194                                   ChildCallBack, kid);
195             x11->SetInputMask(x11, w->self,
196                               ButtonPressMask | ButtonReleaseMask |
197                               OwnerGrabButtonMask | ExposureMask |
198                               EnterWindowMask | LeaveWindowMask);
199         }
200     }
201
202     return m;
203 }
204
205 void show_menu(t_x11 *x11, t_menu *m, int x, int y, bool bGrab)
206 {
207     XMoveWindow(x11->disp, m->wd.self, x, y);
208     m->bGrabbed = bGrab;
209     XMapWindow(x11->disp, m->wd.self);
210     XMapSubwindows(x11->disp, m->wd.self);
211 }
212
213 void hide_menu(t_x11 *x11, t_menu *m)
214 {
215     if (m->bGrabbed)
216     {
217         XUngrabPointer(x11->disp, CurrentTime);
218     }
219     XUnmapWindow(x11->disp, m->wd.self);
220 }
221
222 void check_menu_item(t_menu *m, int nreturn, bool bStatus)
223 {
224     int i;
225
226     for (i = 0; (i < m->nitem); i++)
227     {
228         if (m->item[i].m->nreturn == nreturn)
229         {
230             m->item[i].m->bChecked = bStatus;
231         }
232     }
233 }
234
235 void done_menu(t_x11 *x11, t_menu *m)
236 {
237     int i;
238
239     for (i = 0; (i < m->nitem); i++)
240     {
241         x11->UnRegisterCallback(x11, m->item[i].wd.self);
242     }
243     sfree(m->item);
244     x11->UnRegisterCallback(x11, m->wd.self);
245     sfree(m);
246 }
247
248 int menu_width(t_menu *m)
249 {
250     return m->wd.width;
251 }
252
253 int menu_height(t_menu *m)
254 {
255     return m->wd.height;
256 }