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