Tagged files with gromacs 3.0 header and added some license info
[alexxy/gromacs.git] / src / ngmx / nener.c
1 /*
2  * $Id$
3  * 
4  *                This source code is part of
5  * 
6  *                 G   R   O   M   A   C   S
7  * 
8  *          GROningen MAchine for Chemical Simulations
9  * 
10  *                        VERSION 3.0
11  * 
12  * Copyright (c) 1991-2001
13  * BIOSON Research Institute, Dept. of Biophysical Chemistry
14  * University of Groningen, The Netherlands
15  * 
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  * 
21  * If you want to redistribute modifications, please consider that
22  * scientific software is very special. Version control is crucial -
23  * bugs must be traceable. We will be happy to consider code for
24  * inclusion in the official distribution, but derived work must not
25  * be called official GROMACS. Details are found in the README & COPYING
26  * files - if they are missing, get the official version at www.gromacs.org.
27  * 
28  * To help us fund GROMACS development, we humbly ask that you cite
29  * the papers on the package - you can find them in the top README file.
30  * 
31  * Do check out http://www.gromacs.org , or mail us at gromacs@gromacs.org .
32  * 
33  * And Hey:
34  * Good gRace! Old Maple Actually Chews Slate
35  */
36 static char *SRCID_nener_c = "$Id$";
37 #include <math.h>
38 #include <smalloc.h>
39 #include <macros.h>
40 #include <names.h>
41 #include "nener.h"
42 #include "buttons.h"
43
44 static void DrawEGraph(t_x11 *x11,t_enerwin *ew)
45 {
46   t_windata *wd;
47   int       i,EHeight,EZero;
48   real      epr,scale,MaxE,MinE;
49   char      maxstr[80];
50   int       y;
51
52   wd=&(ew->wd);
53   /* Clear */
54   XClearWindow(x11->disp,wd->self);
55
56   /* Calculate boundaries */
57   MaxE=MinE=ew->e[ew->etype][0];
58   for (i=1; (i<ew->nlast); i++) {
59     MaxE=max(ew->e[ew->etype][i],MaxE);
60     MinE=min(ew->e[ew->etype][i],MinE);
61   }
62
63   /* Print title */
64   epr=max(fabs(MaxE),fabs(MinE));
65   sprintf(maxstr,"%.0f",epr);
66   EHeight=XTextHeight(x11->font)+AIR;
67   TextInRect(x11,wd->self,EType[ew->etype],AIR,0,
68              wd->width-2*AIR,EHeight,eXLeft,eYCenter);
69   TextInRect(x11,wd->self,maxstr,AIR,0,
70              wd->width-2*AIR,EHeight,eXRight,eYCenter);
71   XDrawLine(x11->disp, wd->self,x11->gc,0,EHeight,wd->width,EHeight);
72   
73   if (ew->nlast==0)
74     return;
75
76   if (fabs(MaxE-MinE) < 1e-5)
77     return;
78   
79   EZero=(wd->height-EHeight)/2;
80   scale=EZero/(real) epr;
81   EZero+=EHeight;
82   XDrawLine(x11->disp,wd->self,x11->gc,0,EZero,wd->width,EZero);
83   
84   for(i=0; (i<ew->nlast); i++) {
85     y=ew->e[ew->etype][i]*scale;
86     if (y)
87       XDrawLine(x11->disp,wd->self,x11->gc,i,EZero,i,EZero-y);
88   }
89 }
90
91 static bool EWCallBack(t_x11 *x11,XEvent *event, Window w, void *data)
92 {
93   t_enerwin *ew;
94   int       i,x,y,width;
95
96   return FALSE;
97   ew=(t_enerwin *)data;
98   switch(event->type) {
99   case Expose:
100     XSetForeground(x11->disp,x11->gc,WHITE);
101     DrawEGraph(x11,ew);
102     XSetForeground(x11->disp,x11->gc,x11->fg);
103     break;
104   case ConfigureNotify:
105     ew->wd.x=event->xconfigure.x;
106     ew->wd.y=event->xconfigure.y;
107     ew->wd.width=event->xconfigure.width;
108     ew->wd.height=event->xconfigure.height;
109     if (ew->wd.width > ew->nwidth) {
110       ew->nwidth=ew->wd.width;
111       for (i=0; (i<ew->nre); i++)
112         srenew(ew->e[i],ew->nwidth);
113     }
114     break;
115   case ButtonPress:
116     x=event->xbutton.x;
117     y=ew->wd.y+event->xbutton.y;
118     width=menu_width(ew->selener);
119     x=min(x+ew->wd.x,ew->wd.x+ew->wd.width-width);
120     printf("Showing at %d,%d, width %d\n",x,y,width);
121     show_menu(x11,ew->selener,x,y,TRUE);
122     break;
123   case ClientMessage:
124     ew->etype=event->xclient.data.l[0];
125     ExposeWin(x11->disp,ew->wd.self);
126     /* Fall thru... */
127   case ButtonRelease:
128     hide_menu(x11,ew->selener);
129     break;
130   default:
131     break;
132   }
133   return FALSE;
134 }
135
136 static void create_selener(t_x11 *x11,t_enerwin *ew,Window Parent)
137 {
138   static t_mentry *se;
139   int    i;
140
141   snew(se,ew->nre);
142   for(i=0; (i<ew->nre); i++) {
143     se[i].send_to=ew->wd.self;
144     se[i].nreturn=i;
145     se[i].bChecked=FALSE;
146     se[i].str=EType[i];
147   }
148   ew->selener=init_menu(x11,Parent,x11->fg,x11->bg,ew->nre,se,1);
149 }
150
151 t_enerwin *init_ew(t_x11 *x11,Window Parent,
152                    int x,int y,int width,int height,
153                    unsigned long fg,unsigned long bg)
154 {
155   t_enerwin *ew;
156   int       i;
157   
158   snew(ew,1);
159   ew->etype=0;
160   ew->nlast=0;
161   ew->nwidth=width;
162   ew->nre=F_NRE;
163   snew(ew->e,ew->nre);
164   for(i=0; (i<ew->nre); i++)
165     snew(ew->e[i],width);
166   InitWin(&ew->wd,x,y,width,height,1,"Ener Window");
167   ew->wd.self=XCreateSimpleWindow(x11->disp,Parent,x,y,1,1,1,fg,bg);
168   x11->RegisterCallback(x11,ew->wd.self,Parent,EWCallBack,ew);
169   x11->SetInputMask(x11,ew->wd.self,ExposureMask | ButtonPressMask |
170                     ButtonReleaseMask |  StructureNotifyMask |
171                     OwnerGrabButtonMask);
172   create_selener(x11,ew,Parent);
173
174   return ew;
175 }
176
177 void map_ewin(t_x11 *x11,t_enerwin *ew)
178 {
179   XMapWindow(x11->disp,ew->wd.self);
180 }
181
182 void add_ener(t_x11 *x11,t_enerwin *ew,t_energy e[])
183 {
184   int i,j,w;
185   
186   w=ew->nwidth/2;
187   if (ew->nlast >= ew->nwidth) {
188     for(j=0; (j<ew->nre); j++)
189       for(i=0; (i<w); i++)
190         ew->e[j][i]=ew->e[j][i+w];
191     ew->nlast=w;
192   }
193
194   for(j=0; (j<ew->nre); j++) {
195     ew->e[j][ew->nlast]=e[j].e;
196   }
197   ew->nlast++;
198   ExposeWin(x11->disp,ew->wd.self);
199 }
200
201 void rewind_ener(t_x11 *x11,t_enerwin *ew)
202 {
203   ew->nlast=0;
204   ExposeWin(x11->disp,ew->wd.self);
205 }
206
207 void done_ew(t_x11 *x11,t_enerwin *ew)
208 {
209   done_menu(x11,ew->selener);
210   x11->UnRegisterCallback(x11,ew->wd.self);
211   sfree(ew);
212 }
213
214