Turn on most -Wunused warnings for C++ code.
[alexxy/gromacs.git] / src / programs / view / buttons.cpp
1 /*
2  *
3  *                This source code is part of
4  *
5  *                 G   R   O   M   A   C   S
6  *
7  *          GROningen MAchine for Chemical Simulations
8  *
9  *                        VERSION 3.2.0
10  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12  * Copyright (c) 2001-2013, The GROMACS development team,
13  * check out http://www.gromacs.org for more information.
14
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * If you want to redistribute modifications, please consider that
21  * scientific software is very special. Version control is crucial -
22  * bugs must be traceable. We will be happy to consider code for
23  * inclusion in the official distribution, but derived work must not
24  * be called official GROMACS. Details are found in the README & COPYING
25  * files - if they are missing, get the official version at www.gromacs.org.
26  *
27  * To help us fund GROMACS development, we humbly ask that you cite
28  * the papers on the package - you can find them in the top README file.
29  *
30  * For more info, check our website at http://www.gromacs.org
31  *
32  * And Hey:
33  * Gyas ROwers Mature At Cryogenic Speed
34  */
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #include <sysstuff.h>
40 #include <string.h>
41 #include <smalloc.h>
42 #include <macros.h>
43 #include "x11.h"
44 #include "xutil.h"
45 #include "buttons.h"
46 #include "stop_ani.bm"
47 #include "play.bm"
48 #include "ff.bm"
49 #include "rewind.bm"
50
51 static void move_bbox(t_x11 *x11, t_butbox *bbox)
52 {
53     int        x0, y0;
54     int        i, bw;
55     real       idb, bh, one = 1.0;
56     t_windata *wd;
57
58     bw  = std::max(1, bbox->wd.width-2*(AIR+BORDER));
59     idb = bbox->nbut;
60     bh  = (bbox->wd.height-AIR*(bbox->nbut+1));
61     bh /= idb;
62     bh  = std::max(bh, one);
63
64     x0 = AIR;
65     y0 = AIR;
66     for (i = 0; (i < bbox->nbut); i++)
67     {
68         wd         = &(bbox->b[i].wd);
69         wd->width  = bw;
70         wd->height = bh;
71         wd->color  = WHITE;
72         XMoveWindow(x11->disp, wd->self, x0, y0);
73         XResizeWindow(x11->disp, wd->self, wd->width, wd->height);
74         y0 += AIR+bh;
75     }
76 }
77
78 static bool BBCallBack(t_x11 *x11, XEvent *event, Window /*w*/, void *data)
79 {
80     t_butbox *bbox;
81
82     if (event->type == ConfigureNotify)
83     {
84         bbox            = (t_butbox *)data;
85         bbox->wd.width  = event->xconfigure.width;
86         bbox->wd.height = event->xconfigure.height;
87         move_bbox(x11, bbox);
88     }
89     return false;
90 }
91
92 static bool VBCallBack(t_x11 *x11, XEvent *event, Window /*w*/, void *data)
93 {
94     t_butbox  *vbox;
95     int        y0;
96
97     if (event->type == Expose)
98     {
99         vbox = (t_butbox *)data;
100         y0   = XTextHeight(x11->font)+2*AIR+1;
101         XSetForeground(x11->disp, x11->gc, WHITE);
102         XClearArea(x11->disp, vbox->wd.self, 1, 1, vbox->wd.width-2, y0-1, False);
103         TextInRect(x11, vbox->wd.self, vbox->wd.text,
104                    1, 1, vbox->wd.width-2, y0-1, eXLeft, eYCenter);
105         XDrawLine(x11->disp, vbox->wd.self, x11->gc, 0, y0, vbox->wd.width, y0);
106         XSetForeground(x11->disp, x11->gc, x11->fg);
107     }
108     return false;
109 }
110
111 void set_vbtime(t_x11 *x11, t_butbox *vbox, char *text)
112 {
113     sfree(vbox->wd.text);
114     vbox->wd.text = strdup(text);
115     ExposeWin(x11->disp, vbox->wd.self);
116 }
117
118 static bool ButtonCallBack(t_x11 *x11, XEvent *event, Window /*w*/, void *data)
119 {
120     XEvent     letter;
121     t_mwbut   *but;
122     t_windata *wd;
123
124     but = (t_mwbut *)data;
125     wd  = &(but->wd);
126     switch (event->type)
127     {
128         case Expose:
129             XSetForeground(x11->disp, x11->gc, WHITE);
130             XDrawRoundRect(x11->disp, wd->self, x11->gc,
131                            0, 0, wd->width-1, wd->height-1);
132             TextInWin(x11, wd, wd->text, eXCenter, eYCenter);
133             XSetForeground(x11->disp, x11->gc, x11->fg);
134             break;
135
136         case EnterNotify:
137             /*    LightBorder(x11->disp,wd->self,WHITE);*/
138             XSetForeground(x11->disp, x11->gc, WHITE);
139             XDrawRoundRect(x11->disp, wd->self, x11->gc,
140                            1, 1, wd->width-3, wd->height-3);
141             XSetForeground(x11->disp, x11->gc, x11->fg);
142             break;
143         case LeaveNotify:
144             /*    LightBorder(x11->disp,wd->self,BLUE);*/
145             XSetForeground(x11->disp, x11->gc, BLUE);
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
150             break;
151
152         case ButtonPress:
153             letter.type                 = ClientMessage;
154             letter.xclient.display      = x11->disp;
155             letter.xclient.window       = wd->Parent;
156             letter.xclient.message_type = 0;
157             letter.xclient.format       = 32;
158             letter.xclient.data.l[0]    = but->ID;
159             letter.xclient.data.l[1]    = (long)event->xbutton.button;
160             XSendEvent(x11->disp, wd->Parent, True, 0, &letter);
161             break;
162         default:
163             break;
164     }
165     return false;
166 }
167
168 t_butbox *init_vbox(t_x11 *x11, Window Parent, Window SendTo, unsigned long fg, unsigned long bg)
169 {
170     Pixmap             pm;
171     unsigned char     *data;
172     t_butbox          *vb;
173     int                i, ID, x, y0;
174
175     snew(vb, 1);
176     vb->nbut = IDNR-IDBUTNR-1;
177     snew(vb->b, vb->nbut);
178
179     /* VBox holder */
180     y0 = XTextHeight(x11->font)+2*AIR+2;
181     InitWin(&vb->wd, 0, 0, vb->nbut*(play_width+AIR)+AIR,
182             y0+play_height+2*AIR, 1, "VCR - Control");
183     vb->wd.self = XCreateSimpleWindow(x11->disp, Parent,
184                                       vb->wd.x, vb->wd.y, vb->wd.width, vb->wd.height,
185                                       vb->wd.bwidth, fg, bg);
186     x11->RegisterCallback(x11, vb->wd.self, Parent, VBCallBack, vb);
187     x11->SetInputMask(x11, vb->wd.self, ExposureMask);
188
189     x = AIR;
190     (void)CWBackPixmap;
191     for (i = 0; (i < vb->nbut); i++)
192     {
193         ID = IDBUTNR+i+1;
194         switch (ID)
195         {
196             case IDREWIND:
197                 data = &(rewind_bits[0]);
198                 break;
199             case IDSTEP:
200                 data = play_bits;
201                 break;
202             case IDFF:
203                 data = ff_bits;
204                 break;
205             case IDSTOP_ANI:
206                 data = stop_ani_bits;
207                 break;
208             default:
209                 fprintf(stderr, "Invalid bitmap in init_vbox %d\n", ID);
210                 exit(1);
211         }
212         /* Rely on the fact that all bitmaps are equal size */
213         pm = XCreatePixmapFromBitmapData(x11->disp, x11->root,
214                                          (char *)data, play_width, play_height,
215                                          BLACK, LIGHTGREY, x11->depth);
216         vb->b[i].ID        = ID;
217         vb->b[i].wd.Parent = SendTo;
218         vb->b[i].wd.self   =
219             XCreateSimpleWindow(x11->disp, vb->wd.self,
220                                 x, y0+AIR, play_width, play_height, 0, WHITE, BLACK);
221         XSetWindowBackgroundPixmap(x11->disp, vb->b[i].wd.self, pm);
222
223         x11->RegisterCallback(x11, vb->b[i].wd.self, vb->wd.self,
224                               ButtonCallBack, &(vb->b[i]));
225         x11->SetInputMask(x11, vb->b[i].wd.self,
226                           ButtonPressMask | StructureNotifyMask);
227         x += play_width+AIR;
228     }
229
230     return vb;
231 }
232
233 void show_but(t_x11 *x11, t_butbox *bbox)
234 {
235     XMapWindow(x11->disp, bbox->wd.self);
236     XMapSubwindows(x11->disp, bbox->wd.self);
237 }
238
239 void hide_but(t_x11 *x11, t_butbox *bbox)
240 {
241     XUnmapWindow(x11->disp, bbox->wd.self);
242     XUnmapSubwindows(x11->disp, bbox->wd.self);
243 }
244
245 t_butbox *init_bbox(t_x11 *x11, Window Parent, Window SendTo,
246                     int width, unsigned long fg, unsigned long bg)
247 {
248     t_butbox          *bbox;
249     static const char *lbut[IDBUTNR] = {
250         "< X-Rotate >", "< Y-Rotate >", "< Z-Rotate >",
251         "< X-Move >", "< Y-Move >", "< Z-Move >", "< Scale >",
252     };
253     int                i, y0, h0;
254     t_mwbut           *but;
255     Window             DrawOn;
256
257     snew(bbox, 1);
258     bbox->nbut = IDBUTNR;
259     snew(bbox->b, bbox->nbut);
260     y0 = XTextHeight(x11->font)+2*(AIR+BORDER);
261
262     InitWin(&(bbox->wd), 0, 0, /*width,(y0+AIR)*IDBUTNR+AIR+2*BORDER,*/ 1, 1,
263             1, "Button Box");
264     width        -= 2*AIR+2*BORDER;
265     bbox->wd.self = XCreateSimpleWindow(x11->disp, Parent,
266                                         bbox->wd.x, bbox->wd.y, bbox->wd.width,
267                                         bbox->wd.height, bbox->wd.bwidth,
268                                         fg, bg);
269     x11->RegisterCallback(x11, bbox->wd.self, Parent, BBCallBack, bbox);
270     x11->SetInputMask(x11, bbox->wd.self, StructureNotifyMask);
271
272     DrawOn = bbox->wd.self;
273     h0     = AIR;
274     for (i = 0; (i < bbox->nbut); i++)
275     {
276         but = &(bbox->b[i]);
277         InitWin(&but->wd, AIR, h0, width, y0, 1, lbut[i]);
278         h0            += y0+AIR;
279         but->wd.Parent = SendTo;
280         but->ID        = i;
281         but->wd.self   = XCreateSimpleWindow(x11->disp, DrawOn,
282                                              but->wd.x, but->wd.y,
283                                              but->wd.width, but->wd.height,
284                                              but->wd.bwidth, bg, bg);
285         x11->RegisterCallback(x11, but->wd.self, DrawOn, ButtonCallBack, but);
286         x11->SetInputMask(x11, but->wd.self, ExposureMask | ButtonPressMask |
287                           EnterLeave);
288     }
289     return bbox;
290 }
291
292 void done_bbox(t_x11 *x11, t_butbox *bbox)
293 {
294     int i;
295
296     for (i = 0; (i < bbox->nbut); i++)
297     {
298         x11->UnRegisterCallback(x11, bbox->b[i].wd.self);
299     }
300     x11->UnRegisterCallback(x11, bbox->wd.self);
301     sfree(bbox->b);
302     sfree(bbox);
303 }