Redefine the default boolean type to gmx_bool.
[alexxy/gromacs.git] / src / ngmx / nener.c
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-2004, 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 <math.h>
40 #include <smalloc.h>
41 #include <macros.h>
42 #include <names.h>
43 #include "nener.h"
44 #include "buttons.h"
45
46 static void DrawEGraph(t_x11 *x11,t_enerwin *ew)
47 {
48   t_windata *wd;
49   int       i,EHeight,EZero;
50   real      epr,scale,MaxE,MinE;
51   char      maxstr[80];
52   int       y;
53
54   wd=&(ew->wd);
55   /* Clear */
56   XClearWindow(x11->disp,wd->self);
57
58   /* Calculate boundaries */
59   MaxE=MinE=ew->e[ew->etype][0];
60   for (i=1; (i<ew->nlast); i++) {
61     MaxE=max(ew->e[ew->etype][i],MaxE);
62     MinE=min(ew->e[ew->etype][i],MinE);
63   }
64
65   /* Print title */
66   epr=max(fabs(MaxE),fabs(MinE));
67   sprintf(maxstr,"%.0f",epr);
68   EHeight=XTextHeight(x11->font)+AIR;
69   TextInRect(x11,wd->self,EType[ew->etype],AIR,0,
70              wd->width-2*AIR,EHeight,eXLeft,eYCenter);
71   TextInRect(x11,wd->self,maxstr,AIR,0,
72              wd->width-2*AIR,EHeight,eXRight,eYCenter);
73   XDrawLine(x11->disp, wd->self,x11->gc,0,EHeight,wd->width,EHeight);
74   
75   if (ew->nlast==0)
76     return;
77
78   if (fabs(MaxE-MinE) < 1e-5)
79     return;
80   
81   EZero=(wd->height-EHeight)/2;
82   scale=EZero/(real) epr;
83   EZero+=EHeight;
84   XDrawLine(x11->disp,wd->self,x11->gc,0,EZero,wd->width,EZero);
85   
86   for(i=0; (i<ew->nlast); i++) {
87     y=ew->e[ew->etype][i]*scale;
88     if (y)
89       XDrawLine(x11->disp,wd->self,x11->gc,i,EZero,i,EZero-y);
90   }
91 }
92
93 static gmx_bool EWCallBack(t_x11 *x11,XEvent *event, Window w, void *data)
94 {
95   t_enerwin *ew;
96   int       i,x,y,width;
97
98   return FALSE;
99   ew=(t_enerwin *)data;
100   switch(event->type) {
101   case Expose:
102     XSetForeground(x11->disp,x11->gc,WHITE);
103     DrawEGraph(x11,ew);
104     XSetForeground(x11->disp,x11->gc,x11->fg);
105     break;
106   case ConfigureNotify:
107     ew->wd.x=event->xconfigure.x;
108     ew->wd.y=event->xconfigure.y;
109     ew->wd.width=event->xconfigure.width;
110     ew->wd.height=event->xconfigure.height;
111     if (ew->wd.width > ew->nwidth) {
112       ew->nwidth=ew->wd.width;
113       for (i=0; (i<ew->nre); i++)
114         srenew(ew->e[i],ew->nwidth);
115     }
116     break;
117   case ButtonPress:
118     x=event->xbutton.x;
119     y=ew->wd.y+event->xbutton.y;
120     width=menu_width(ew->selener);
121     x=min(x+ew->wd.x,ew->wd.x+ew->wd.width-width);
122     printf("Showing at %d,%d, width %d\n",x,y,width);
123     show_menu(x11,ew->selener,x,y,TRUE);
124     break;
125   case ClientMessage:
126     ew->etype=event->xclient.data.l[0];
127     ExposeWin(x11->disp,ew->wd.self);
128     /* Fall thru... */
129   case ButtonRelease:
130     hide_menu(x11,ew->selener);
131     break;
132   default:
133     break;
134   }
135   return FALSE;
136 }
137
138 static void create_selener(t_x11 *x11,t_enerwin *ew,Window Parent)
139 {
140   static t_mentry *se;
141   int    i;
142
143   snew(se,ew->nre);
144   for(i=0; (i<ew->nre); i++) {
145     se[i].send_to=ew->wd.self;
146     se[i].nreturn=i;
147     se[i].bChecked=FALSE;
148     se[i].str=EType[i];
149   }
150   ew->selener=init_menu(x11,Parent,x11->fg,x11->bg,ew->nre,se,1);
151 }
152
153 t_enerwin *init_ew(t_x11 *x11,Window Parent,
154                    int x,int y,int width,int height,
155                    unsigned long fg,unsigned long bg)
156 {
157   t_enerwin *ew;
158   int       i;
159   
160   snew(ew,1);
161   ew->etype=0;
162   ew->nlast=0;
163   ew->nwidth=width;
164   ew->nre=F_NRE;
165   snew(ew->e,ew->nre);
166   for(i=0; (i<ew->nre); i++)
167     snew(ew->e[i],width);
168   InitWin(&ew->wd,x,y,width,height,1,"Ener Window");
169   ew->wd.self=XCreateSimpleWindow(x11->disp,Parent,x,y,1,1,1,fg,bg);
170   x11->RegisterCallback(x11,ew->wd.self,Parent,EWCallBack,ew);
171   x11->SetInputMask(x11,ew->wd.self,ExposureMask | ButtonPressMask |
172                     ButtonReleaseMask |  StructureNotifyMask |
173                     OwnerGrabButtonMask);
174   create_selener(x11,ew,Parent);
175
176   return ew;
177 }
178
179 void map_ewin(t_x11 *x11,t_enerwin *ew)
180 {
181   XMapWindow(x11->disp,ew->wd.self);
182 }
183
184 void add_ener(t_x11 *x11,t_enerwin *ew,t_energy e[])
185 {
186   int i,j,w;
187   
188   w=ew->nwidth/2;
189   if (ew->nlast >= ew->nwidth) {
190     for(j=0; (j<ew->nre); j++)
191       for(i=0; (i<w); i++)
192         ew->e[j][i]=ew->e[j][i+w];
193     ew->nlast=w;
194   }
195
196   for(j=0; (j<ew->nre); j++) {
197     ew->e[j][ew->nlast]=e[j].e;
198   }
199   ew->nlast++;
200   ExposeWin(x11->disp,ew->wd.self);
201 }
202
203 void rewind_ener(t_x11 *x11,t_enerwin *ew)
204 {
205   ew->nlast=0;
206   ExposeWin(x11->disp,ew->wd.self);
207 }
208
209 void done_ew(t_x11 *x11,t_enerwin *ew)
210 {
211   done_menu(x11,ew->selener);
212   x11->UnRegisterCallback(x11,ew->wd.self);
213   sfree(ew);
214 }
215
216