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