Merge release-5-0 into master
[alexxy/gromacs.git] / src / programs / view / buttons.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 "config.h"
38
39 #include <stdlib.h>
40 #include <string.h>
41
42 #include <algorithm>
43
44 #include "macros.h"
45 #include "gromacs/utility/smalloc.h"
46
47 #include "x11.h"
48 #include "xutil.h"
49 #include "buttons.h"
50
51 #include "stop_ani.bm"
52 #include "play.bm"
53 #include "ff.bm"
54 #include "rewind.bm"
55
56 static void move_bbox(t_x11 *x11, t_butbox *bbox)
57 {
58     int        x0, y0;
59     int        i, bw;
60     real       idb, bh, one = 1.0;
61     t_windata *wd;
62
63     bw  = std::max(1, bbox->wd.width-2*(AIR+BORDER));
64     idb = bbox->nbut;
65     bh  = (bbox->wd.height-AIR*(bbox->nbut+1));
66     bh /= idb;
67     bh  = std::max(bh, one);
68
69     x0 = AIR;
70     y0 = AIR;
71     for (i = 0; (i < bbox->nbut); i++)
72     {
73         wd         = &(bbox->b[i].wd);
74         wd->width  = bw;
75         wd->height = bh;
76         wd->color  = WHITE;
77         XMoveWindow(x11->disp, wd->self, x0, y0);
78         XResizeWindow(x11->disp, wd->self, wd->width, wd->height);
79         y0 += AIR+bh;
80     }
81 }
82
83 static bool BBCallBack(t_x11 *x11, XEvent *event, Window /*w*/, void *data)
84 {
85     t_butbox *bbox;
86
87     if (event->type == ConfigureNotify)
88     {
89         bbox            = (t_butbox *)data;
90         bbox->wd.width  = event->xconfigure.width;
91         bbox->wd.height = event->xconfigure.height;
92         move_bbox(x11, bbox);
93     }
94     return false;
95 }
96
97 static bool VBCallBack(t_x11 *x11, XEvent *event, Window /*w*/, void *data)
98 {
99     t_butbox  *vbox;
100     int        y0;
101
102     if (event->type == Expose)
103     {
104         vbox = (t_butbox *)data;
105         y0   = XTextHeight(x11->font)+2*AIR+1;
106         XSetForeground(x11->disp, x11->gc, WHITE);
107         XClearArea(x11->disp, vbox->wd.self, 1, 1, vbox->wd.width-2, y0-1, False);
108         TextInRect(x11, vbox->wd.self, vbox->wd.text,
109                    1, 1, vbox->wd.width-2, y0-1, eXLeft, eYCenter);
110         XDrawLine(x11->disp, vbox->wd.self, x11->gc, 0, y0, vbox->wd.width, y0);
111         XSetForeground(x11->disp, x11->gc, x11->fg);
112     }
113     return false;
114 }
115
116 void set_vbtime(t_x11 *x11, t_butbox *vbox, char *text)
117 {
118     sfree(vbox->wd.text);
119     vbox->wd.text = strdup(text);
120     ExposeWin(x11->disp, vbox->wd.self);
121 }
122
123 static bool ButtonCallBack(t_x11 *x11, XEvent *event, Window /*w*/, void *data)
124 {
125     XEvent     letter;
126     t_mwbut   *but;
127     t_windata *wd;
128
129     but = (t_mwbut *)data;
130     wd  = &(but->wd);
131     switch (event->type)
132     {
133         case Expose:
134             XSetForeground(x11->disp, x11->gc, WHITE);
135             XDrawRoundRect(x11->disp, wd->self, x11->gc,
136                            0, 0, wd->width-1, wd->height-1);
137             TextInWin(x11, wd, wd->text, eXCenter, eYCenter);
138             XSetForeground(x11->disp, x11->gc, x11->fg);
139             break;
140
141         case EnterNotify:
142             /*    LightBorder(x11->disp,wd->self,WHITE);*/
143             XSetForeground(x11->disp, x11->gc, WHITE);
144             XDrawRoundRect(x11->disp, wd->self, x11->gc,
145                            1, 1, wd->width-3, wd->height-3);
146             XSetForeground(x11->disp, x11->gc, x11->fg);
147             break;
148         case LeaveNotify:
149             /*    LightBorder(x11->disp,wd->self,BLUE);*/
150             XSetForeground(x11->disp, x11->gc, BLUE);
151             XDrawRoundRect(x11->disp, wd->self, x11->gc,
152                            1, 1, wd->width-3, wd->height-3);
153             XSetForeground(x11->disp, x11->gc, x11->fg);
154
155             break;
156
157         case ButtonPress:
158             letter.type                 = ClientMessage;
159             letter.xclient.display      = x11->disp;
160             letter.xclient.window       = wd->Parent;
161             letter.xclient.message_type = 0;
162             letter.xclient.format       = 32;
163             letter.xclient.data.l[0]    = but->ID;
164             letter.xclient.data.l[1]    = (long)event->xbutton.button;
165             XSendEvent(x11->disp, wd->Parent, True, 0, &letter);
166             break;
167         default:
168             break;
169     }
170     return false;
171 }
172
173 t_butbox *init_vbox(t_x11 *x11, Window Parent, Window SendTo, unsigned long fg, unsigned long bg)
174 {
175     Pixmap             pm;
176     unsigned char     *data;
177     t_butbox          *vb;
178     int                i, ID, x, y0;
179
180     snew(vb, 1);
181     vb->nbut = IDNR-IDBUTNR-1;
182     snew(vb->b, vb->nbut);
183
184     /* VBox holder */
185     y0 = XTextHeight(x11->font)+2*AIR+2;
186     InitWin(&vb->wd, 0, 0, vb->nbut*(play_width+AIR)+AIR,
187             y0+play_height+2*AIR, 1, "VCR - Control");
188     vb->wd.self = XCreateSimpleWindow(x11->disp, Parent,
189                                       vb->wd.x, vb->wd.y, vb->wd.width, vb->wd.height,
190                                       vb->wd.bwidth, fg, bg);
191     x11->RegisterCallback(x11, vb->wd.self, Parent, VBCallBack, vb);
192     x11->SetInputMask(x11, vb->wd.self, ExposureMask);
193
194     x = AIR;
195     (void)CWBackPixmap;
196     for (i = 0; (i < vb->nbut); i++)
197     {
198         ID = IDBUTNR+i+1;
199         switch (ID)
200         {
201             case IDREWIND:
202                 data = &(rewind_bits[0]);
203                 break;
204             case IDSTEP:
205                 data = play_bits;
206                 break;
207             case IDFF:
208                 data = ff_bits;
209                 break;
210             case IDSTOP_ANI:
211                 data = stop_ani_bits;
212                 break;
213             default:
214                 fprintf(stderr, "Invalid bitmap in init_vbox %d\n", ID);
215                 exit(1);
216         }
217         /* Rely on the fact that all bitmaps are equal size */
218         pm = XCreatePixmapFromBitmapData(x11->disp, x11->root,
219                                          (char *)data, play_width, play_height,
220                                          BLACK, LIGHTGREY, x11->depth);
221         vb->b[i].ID        = ID;
222         vb->b[i].wd.Parent = SendTo;
223         vb->b[i].wd.self   =
224             XCreateSimpleWindow(x11->disp, vb->wd.self,
225                                 x, y0+AIR, play_width, play_height, 0, WHITE, BLACK);
226         XSetWindowBackgroundPixmap(x11->disp, vb->b[i].wd.self, pm);
227
228         x11->RegisterCallback(x11, vb->b[i].wd.self, vb->wd.self,
229                               ButtonCallBack, &(vb->b[i]));
230         x11->SetInputMask(x11, vb->b[i].wd.self,
231                           ButtonPressMask | StructureNotifyMask);
232         x += play_width+AIR;
233     }
234
235     return vb;
236 }
237
238 void show_but(t_x11 *x11, t_butbox *bbox)
239 {
240     XMapWindow(x11->disp, bbox->wd.self);
241     XMapSubwindows(x11->disp, bbox->wd.self);
242 }
243
244 void hide_but(t_x11 *x11, t_butbox *bbox)
245 {
246     XUnmapWindow(x11->disp, bbox->wd.self);
247     XUnmapSubwindows(x11->disp, bbox->wd.self);
248 }
249
250 t_butbox *init_bbox(t_x11 *x11, Window Parent, Window SendTo,
251                     int width, unsigned long fg, unsigned long bg)
252 {
253     t_butbox          *bbox;
254     static const char *lbut[IDBUTNR] = {
255         "< X-Rotate >", "< Y-Rotate >", "< Z-Rotate >",
256         "< X-Move >", "< Y-Move >", "< Z-Move >", "< Scale >",
257     };
258     int                i, y0, h0;
259     t_mwbut           *but;
260     Window             DrawOn;
261
262     snew(bbox, 1);
263     bbox->nbut = IDBUTNR;
264     snew(bbox->b, bbox->nbut);
265     y0 = XTextHeight(x11->font)+2*(AIR+BORDER);
266
267     InitWin(&(bbox->wd), 0, 0, /*width,(y0+AIR)*IDBUTNR+AIR+2*BORDER,*/ 1, 1,
268             1, "Button Box");
269     width        -= 2*AIR+2*BORDER;
270     bbox->wd.self = XCreateSimpleWindow(x11->disp, Parent,
271                                         bbox->wd.x, bbox->wd.y, bbox->wd.width,
272                                         bbox->wd.height, bbox->wd.bwidth,
273                                         fg, bg);
274     x11->RegisterCallback(x11, bbox->wd.self, Parent, BBCallBack, bbox);
275     x11->SetInputMask(x11, bbox->wd.self, StructureNotifyMask);
276
277     DrawOn = bbox->wd.self;
278     h0     = AIR;
279     for (i = 0; (i < bbox->nbut); i++)
280     {
281         but = &(bbox->b[i]);
282         InitWin(&but->wd, AIR, h0, width, y0, 1, lbut[i]);
283         h0            += y0+AIR;
284         but->wd.Parent = SendTo;
285         but->ID        = i;
286         but->wd.self   = XCreateSimpleWindow(x11->disp, DrawOn,
287                                              but->wd.x, but->wd.y,
288                                              but->wd.width, but->wd.height,
289                                              but->wd.bwidth, bg, bg);
290         x11->RegisterCallback(x11, but->wd.self, DrawOn, ButtonCallBack, but);
291         x11->SetInputMask(x11, but->wd.self, ExposureMask | ButtonPressMask |
292                           EnterLeave);
293     }
294     return bbox;
295 }
296
297 void done_bbox(t_x11 *x11, t_butbox *bbox)
298 {
299     int i;
300
301     for (i = 0; (i < bbox->nbut); i++)
302     {
303         x11->UnRegisterCallback(x11, bbox->b[i].wd.self);
304     }
305     x11->UnRegisterCallback(x11, bbox->wd.self);
306     sfree(bbox->b);
307     sfree(bbox);
308 }