Update copyright statements and change license to LGPL
[alexxy/gromacs.git] / src / ngmx / pulldown.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 <string.h>
43 #include <smalloc.h>
44 #include <x11.h>
45 #include <macros.h>
46 #include "popup.h"
47 #include "pulldown.h"
48
49 static gmx_bool PDCallBack(t_x11 *x11,XEvent *event,Window w,void *data)
50 {
51   t_pulldown *pd;
52   int        i,x,x1,y,nsel;
53
54   pd=(t_pulldown *)data;
55   y=pd->wd.height;
56   switch(event->type) {
57   case Expose:
58     XSetForeground(x11->disp,x11->gc,x11->fg);
59     XDrawLine(x11->disp,w,x11->gc,0,y-1,pd->wd.width,y-1);
60     for(i=0; (i<pd->nmenu); i++)
61       XDrawString(x11->disp,pd->wd.self,x11->gc,pd->xpos[i],x11->font->ascent,
62                   pd->title[i],strlen(pd->title[i]));
63     break;
64   case ButtonPress:
65     if (pd->nsel==-1) {
66       x=event->xbutton.x;
67       for(nsel=0; (pd->xpos[nsel+1] < x) && (nsel < pd->nmenu-1); nsel++);
68       pd->nsel=nsel;
69       x1=max(0,min(pd_width(pd)-menu_width(pd->m[nsel]),pd->xpos[nsel]));
70       show_menu(x11,pd->m[nsel],x1,y+1,FALSE);
71     }
72     break;
73   case ButtonRelease:
74     hide_pd(x11,pd);
75     break;
76   default:
77     break;
78   }
79   return FALSE;
80 }
81
82 t_pulldown *init_pd(t_x11 *x11,Window Parent,int width,int height,
83                     unsigned long fg,unsigned long bg,
84                     int nmenu,int *nsub,t_mentry *ent[],const char **title)
85 {
86   t_pulldown *pd;
87   int        i;
88
89   snew(pd,1);
90   pd->title=title;
91   pd->nmenu=nmenu;
92   pd->nsel=-1;
93   snew(pd->m,nmenu);
94   snew(pd->xpos,nmenu+1);
95   pd->xpos[0]=5;
96   for(i=1; (i<=nmenu); i++)
97     pd->xpos[i]=20+pd->xpos[i-1]+
98       XTextWidth(x11->font,title[i-1],strlen(title[i-1]));
99   if (pd->xpos[nmenu] > width) 
100     printf("Menu too wide\n");
101
102   InitWin(&(pd->wd),0,0,width,XTextHeight(x11->font)+2,0,"PullDown");
103   pd->wd.self=XCreateSimpleWindow(x11->disp,Parent,
104                                   pd->wd.x, pd->wd.y,
105                                   pd->wd.width,pd->wd.height,
106                                   pd->wd.bwidth,fg,bg);
107   x11->RegisterCallback(x11,pd->wd.self,Parent,PDCallBack,pd);
108   x11->SetInputMask(x11,pd->wd.self,ExposureMask | ButtonPressMask | 
109                     OwnerGrabButtonMask | ButtonReleaseMask);
110   XMapWindow(x11->disp,pd->wd.self);
111
112   for(i=0; (i<nmenu); i++) 
113     pd->m[i]=init_menu(x11,Parent,fg,bg,nsub[i],ent[i],1);
114
115   return pd;
116 }
117
118 void hide_pd(t_x11 *x11,t_pulldown *pd)
119 {
120   if (pd->nsel != -1)
121     hide_menu(x11,pd->m[pd->nsel]);
122   pd->nsel=-1;
123 }
124
125 void check_pd_item(t_pulldown *pd,int nreturn,gmx_bool bStatus)
126 {
127   int i;
128
129   for(i=0; (i<pd->nmenu); i++)
130     check_menu_item(pd->m[i],nreturn,bStatus);
131 }
132
133 void done_pd(t_x11 *x11,t_pulldown *pd)
134 {
135   int i;
136
137   for(i=0; (i<pd->nmenu); i++)
138     done_menu(x11,pd->m[i]);
139   x11->UnRegisterCallback(x11,pd->wd.self);
140   sfree(pd->m);
141   sfree(pd->xpos);
142 }
143
144 int pd_width(t_pulldown *pd)
145 {
146   int i,w;
147   
148   w=0;
149   for(i=0; (i<pd->nmenu); i++)
150     w=max(w,menu_width(pd->m[i]));
151   w=max(w,pd->xpos[pd->nmenu]);
152   return w;
153 }
154
155 int pd_height(t_pulldown *pd)
156 {
157   int i,h;
158   
159   h=0;
160   for(i=0; (i<pd->nmenu); i++)
161     h=max(h,menu_height(pd->m[i]));
162
163   return h;
164 }
165