baeac635eb2772ce4f04e233f4f5477a772b40dd
[alexxy/gromacs.git] / src / ngmx / buttons.c
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-2004, The GROMACS development team,
6  * check out http://www.gromacs.org for more information.
7  * Copyright (c) 2012, by the GROMACS development team, led by
8  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9  * others, as listed in the AUTHORS file in the top-level source
10  * directory and at http://www.gromacs.org.
11  *
12  * GROMACS is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation; either version 2.1
15  * of the License, or (at your option) any later version.
16  *
17  * GROMACS is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with GROMACS; if not, see
24  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
26  *
27  * If you want to redistribute modifications to GROMACS, please
28  * consider that scientific software is very special. Version
29  * control is crucial - bugs must be traceable. We will be happy to
30  * consider code for inclusion in the official distribution, but
31  * derived work must not be called official GROMACS. Details are found
32  * in the README & COPYING files - if they are missing, get the
33  * official version at http://www.gromacs.org.
34  *
35  * To help us fund GROMACS development, we humbly ask that you cite
36  * the research papers on the package. Check out http://www.gromacs.org.
37  */
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42 #include <sysstuff.h>
43 #include <string.h>
44 #include <smalloc.h>
45 #include <macros.h>
46 #include <x11.h>
47 #include <xutil.h>
48 #include "buttons.h"
49 #include "stop_ani.bm"
50 #include "play.bm"
51 #include "ff.bm"
52 #include "rewind.bm"
53
54 static void move_bbox(t_x11 *x11,t_butbox *bbox)
55 {
56   int       x0,y0;
57   int       i,bw;
58   real      idb,bh;
59   t_windata *wd;
60
61   bw=max(1,bbox->wd.width-2*(AIR+BORDER));
62   idb=bbox->nbut;
63   bh=(bbox->wd.height-AIR*(bbox->nbut+1));
64   bh/=idb;
65   bh=max(bh,1.0);
66
67   x0=AIR;
68   y0=AIR;
69   for (i=0; (i<bbox->nbut); i++) {
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 gmx_bool BBCallBack(t_x11 *x11,XEvent *event, Window w,void *data)
81 {
82   t_butbox *bbox;
83
84   if (event->type==ConfigureNotify) {
85     bbox=(t_butbox *)data;
86     bbox->wd.width=event->xconfigure.width;
87     bbox->wd.height=event->xconfigure.height;
88     move_bbox(x11,bbox);
89   }
90   return FALSE;
91 }
92
93 static gmx_bool VBCallBack(t_x11 *x11,XEvent *event, Window w,void *data)
94 {
95   t_butbox *vbox;
96   int        y0;
97
98   if (event->type==Expose) {
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 gmx_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   case Expose:
128     XSetForeground(x11->disp,x11->gc,WHITE);
129     XDrawRoundRect(x11->disp, wd->self, x11->gc,
130                    0,0,wd->width-1,wd->height-1);    
131     TextInWin(x11,wd,wd->text,eXCenter,eYCenter);
132     XSetForeground(x11->disp,x11->gc,x11->fg);
133     break;
134
135   case EnterNotify:
136     /*    LightBorder(x11->disp,wd->self,WHITE);*/
137     XSetForeground(x11->disp,x11->gc,WHITE);
138     XDrawRoundRect(x11->disp, wd->self, x11->gc,
139                    1,1,wd->width-3,wd->height-3);    
140     XSetForeground(x11->disp,x11->gc,x11->fg);
141     break;
142   case LeaveNotify:
143     /*    LightBorder(x11->disp,wd->self,BLUE);*/
144     XSetForeground(x11->disp,x11->gc,BLUE);
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
149     break;
150
151   case ButtonPress:
152     letter.type=ClientMessage;
153     letter.xclient.display=x11->disp;
154     letter.xclient.window=wd->Parent;
155     letter.xclient.message_type=0;
156     letter.xclient.format=32;
157     letter.xclient.data.l[0]=but->ID;
158     letter.xclient.data.l[1]=(long)event->xbutton.button;
159     XSendEvent(x11->disp,wd->Parent,True,0,&letter);
160     break;
161   default:
162     break;
163   }
164   return FALSE;
165 }
166
167 t_butbox *init_vbox(t_x11 *x11,Window Parent,Window SendTo,unsigned long fg,unsigned long bg)
168 {
169   Pixmap   pm;
170   unsigned char     *data;
171   t_butbox *vb;
172   int      i,ID,x,y0;
173
174   snew(vb,1);
175   vb->nbut=IDNR-IDBUTNR-1;
176   snew(vb->b,vb->nbut);
177
178   /* VBox holder */
179   y0=XTextHeight(x11->font)+2*AIR+2;
180   InitWin(&vb->wd,0,0,vb->nbut*(play_width+AIR)+AIR,
181           y0+play_height+2*AIR,1,"VCR - Control");
182   vb->wd.self=XCreateSimpleWindow(x11->disp,Parent,
183                                   vb->wd.x,vb->wd.y,vb->wd.width,vb->wd.height,
184                                   vb->wd.bwidth,WHITE,BLACK);
185   x11->RegisterCallback(x11,vb->wd.self,Parent,VBCallBack,vb);
186   x11->SetInputMask(x11,vb->wd.self,ExposureMask);
187   
188   x=AIR;
189   (void)CWBackPixmap;
190   for(i=0; (i<vb->nbut); i++) {
191     ID=IDBUTNR+i+1;
192     switch (ID) {
193     case IDREWIND:
194       data=&(rewind_bits[0]);
195       break;
196     case IDSTEP:
197       data=play_bits;
198       break;
199     case IDFF:
200       data=ff_bits;
201       break;
202     case IDSTOP_ANI:
203       data=stop_ani_bits;
204       break;
205     default:
206       fprintf(stderr,"Invalid bitmap in init_vbox %d\n",ID);
207       exit(1);
208     }
209     /* Rely on the fact that all bitmaps are equal size */
210     pm=XCreatePixmapFromBitmapData(x11->disp,x11->root,
211                                    (char *)data,play_width,play_height,
212                                    BLACK,LIGHTGREY,x11->depth);
213     vb->b[i].ID=ID;
214     vb->b[i].wd.Parent=SendTo;
215     vb->b[i].wd.self=
216       XCreateSimpleWindow(x11->disp,vb->wd.self,
217                           x,y0+AIR,play_width,play_height,0,WHITE,BLACK);
218     XSetWindowBackgroundPixmap(x11->disp,vb->b[i].wd.self,pm);
219                                
220     x11->RegisterCallback(x11,vb->b[i].wd.self,vb->wd.self,
221                           ButtonCallBack,&(vb->b[i]));
222     x11->SetInputMask(x11,vb->b[i].wd.self,
223                       ButtonPressMask | StructureNotifyMask);
224     x+=play_width+AIR;
225   }
226   
227   return vb;
228 }
229
230 void show_but(t_x11 *x11,t_butbox *bbox)
231 {
232   XMapWindow(x11->disp,bbox->wd.self);
233   XMapSubwindows(x11->disp,bbox->wd.self);
234 }
235
236 void hide_but(t_x11 *x11,t_butbox *bbox)
237 {
238   XUnmapWindow(x11->disp,bbox->wd.self);
239   XUnmapSubwindows(x11->disp,bbox->wd.self);
240 }
241
242 t_butbox *init_bbox(t_x11 *x11,Window Parent,Window SendTo,
243                     int width,unsigned long fg,unsigned long bg)
244 {
245   t_butbox *bbox;
246   static const char *lbut[IDBUTNR] = {
247     "< X-Rotate >", "< Y-Rotate >", "< Z-Rotate >", 
248     "< X-Move >", "< Y-Move >", "< Z-Move >", "< Scale >", 
249     };
250   int       i,y0,h0;
251   t_mwbut   *but;
252   Window    DrawOn;
253
254   snew(bbox,1);
255   bbox->nbut=IDBUTNR;
256   snew(bbox->b,bbox->nbut);
257   y0=XTextHeight(x11->font)+2*(AIR+BORDER);
258   
259   InitWin(&(bbox->wd),0,0,/*width,(y0+AIR)*IDBUTNR+AIR+2*BORDER,*/1,1,
260           1,"Button Box");
261   width-=2*AIR+2*BORDER;
262   bbox->wd.self=XCreateSimpleWindow(x11->disp,Parent,
263                                     bbox->wd.x,bbox->wd.y,bbox->wd.width,
264                                     bbox->wd.height,bbox->wd.bwidth,
265                                     fg,bg);
266   x11->RegisterCallback(x11,bbox->wd.self,Parent,BBCallBack,bbox);
267   x11->SetInputMask(x11,bbox->wd.self,StructureNotifyMask);
268
269   DrawOn=bbox->wd.self;
270   h0=AIR;
271   for (i=0; (i<bbox->nbut); i++) {
272     but=&(bbox->b[i]);
273     InitWin(&but->wd,AIR,h0,width,y0,1,lbut[i]);
274     h0+=y0+AIR;
275     but->wd.Parent=SendTo;
276     but->ID=i;
277     but->wd.self=XCreateSimpleWindow(x11->disp,DrawOn,
278                                      but->wd.x,but->wd.y,
279                                      but->wd.width,but->wd.height,
280                                      but->wd.bwidth,bg,bg);
281     x11->RegisterCallback(x11,but->wd.self,DrawOn,ButtonCallBack,but);
282     x11->SetInputMask(x11,but->wd.self,ExposureMask | ButtonPressMask |
283                       EnterLeave);
284   }
285   return bbox;
286 }
287
288 void done_bbox(t_x11 *x11,t_butbox *bbox)
289 {
290   int i;
291
292   for(i=0; (i<bbox->nbut); i++)
293     x11->UnRegisterCallback(x11,bbox->b[i].wd.self);
294   x11->UnRegisterCallback(x11,bbox->wd.self);
295   sfree(bbox->b);
296   sfree(bbox);
297 }