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