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