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