Partial commit of the project to remove all static variables.
[gromacs.git] / src / ngmx / nener.c
blob8d56efbaf1e6e6b0e07d5d6b48e72dc204b360e9
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.1
11 * Copyright (c) 1991-2001, University of Groningen, The Netherlands
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * If you want to redistribute modifications, please consider that
18 * scientific software is very special. Version control is crucial -
19 * bugs must be traceable. We will be happy to consider code for
20 * inclusion in the official distribution, but derived work must not
21 * be called official GROMACS. Details are found in the README & COPYING
22 * files - if they are missing, get the official version at www.gromacs.org.
24 * To help us fund GROMACS development, we humbly ask that you cite
25 * the papers on the package - you can find them in the top README file.
27 * For more info, check our website at http://www.gromacs.org
29 * And Hey:
30 * Glycine aRginine prOline Methionine Alanine Cystine Serine
33 #include <math.h>
34 #include <smalloc.h>
35 #include <macros.h>
36 #include <names.h>
37 #include "nener.h"
38 #include "buttons.h"
40 static void DrawEGraph(t_x11 *x11,t_enerwin *ew)
42 t_windata *wd;
43 int i,EHeight,EZero;
44 real epr,scale,MaxE,MinE;
45 char maxstr[80];
46 int y;
48 wd=&(ew->wd);
49 /* Clear */
50 XClearWindow(x11->disp,wd->self);
52 /* Calculate boundaries */
53 MaxE=MinE=ew->e[ew->etype][0];
54 for (i=1; (i<ew->nlast); i++) {
55 MaxE=max(ew->e[ew->etype][i],MaxE);
56 MinE=min(ew->e[ew->etype][i],MinE);
59 /* Print title */
60 epr=max(fabs(MaxE),fabs(MinE));
61 sprintf(maxstr,"%.0f",epr);
62 EHeight=XTextHeight(x11->font)+AIR;
63 TextInRect(x11,wd->self,EType[ew->etype],AIR,0,
64 wd->width-2*AIR,EHeight,eXLeft,eYCenter);
65 TextInRect(x11,wd->self,maxstr,AIR,0,
66 wd->width-2*AIR,EHeight,eXRight,eYCenter);
67 XDrawLine(x11->disp, wd->self,x11->gc,0,EHeight,wd->width,EHeight);
69 if (ew->nlast==0)
70 return;
72 if (fabs(MaxE-MinE) < 1e-5)
73 return;
75 EZero=(wd->height-EHeight)/2;
76 scale=EZero/(real) epr;
77 EZero+=EHeight;
78 XDrawLine(x11->disp,wd->self,x11->gc,0,EZero,wd->width,EZero);
80 for(i=0; (i<ew->nlast); i++) {
81 y=ew->e[ew->etype][i]*scale;
82 if (y)
83 XDrawLine(x11->disp,wd->self,x11->gc,i,EZero,i,EZero-y);
87 static bool EWCallBack(t_x11 *x11,XEvent *event, Window w, void *data)
89 t_enerwin *ew;
90 int i,x,y,width;
92 return FALSE;
93 ew=(t_enerwin *)data;
94 switch(event->type) {
95 case Expose:
96 XSetForeground(x11->disp,x11->gc,WHITE);
97 DrawEGraph(x11,ew);
98 XSetForeground(x11->disp,x11->gc,x11->fg);
99 break;
100 case ConfigureNotify:
101 ew->wd.x=event->xconfigure.x;
102 ew->wd.y=event->xconfigure.y;
103 ew->wd.width=event->xconfigure.width;
104 ew->wd.height=event->xconfigure.height;
105 if (ew->wd.width > ew->nwidth) {
106 ew->nwidth=ew->wd.width;
107 for (i=0; (i<ew->nre); i++)
108 srenew(ew->e[i],ew->nwidth);
110 break;
111 case ButtonPress:
112 x=event->xbutton.x;
113 y=ew->wd.y+event->xbutton.y;
114 width=menu_width(ew->selener);
115 x=min(x+ew->wd.x,ew->wd.x+ew->wd.width-width);
116 printf("Showing at %d,%d, width %d\n",x,y,width);
117 show_menu(x11,ew->selener,x,y,TRUE);
118 break;
119 case ClientMessage:
120 ew->etype=event->xclient.data.l[0];
121 ExposeWin(x11->disp,ew->wd.self);
122 /* Fall thru... */
123 case ButtonRelease:
124 hide_menu(x11,ew->selener);
125 break;
126 default:
127 break;
129 return FALSE;
132 static void create_selener(t_x11 *x11,t_enerwin *ew,Window Parent)
134 static t_mentry *se;
135 int i;
137 snew(se,ew->nre);
138 for(i=0; (i<ew->nre); i++) {
139 se[i].send_to=ew->wd.self;
140 se[i].nreturn=i;
141 se[i].bChecked=FALSE;
142 se[i].str=EType[i];
144 ew->selener=init_menu(x11,Parent,x11->fg,x11->bg,ew->nre,se,1);
147 t_enerwin *init_ew(t_x11 *x11,Window Parent,
148 int x,int y,int width,int height,
149 unsigned long fg,unsigned long bg)
151 t_enerwin *ew;
152 int i;
154 snew(ew,1);
155 ew->etype=0;
156 ew->nlast=0;
157 ew->nwidth=width;
158 ew->nre=F_NRE;
159 snew(ew->e,ew->nre);
160 for(i=0; (i<ew->nre); i++)
161 snew(ew->e[i],width);
162 InitWin(&ew->wd,x,y,width,height,1,"Ener Window");
163 ew->wd.self=XCreateSimpleWindow(x11->disp,Parent,x,y,1,1,1,fg,bg);
164 x11->RegisterCallback(x11,ew->wd.self,Parent,EWCallBack,ew);
165 x11->SetInputMask(x11,ew->wd.self,ExposureMask | ButtonPressMask |
166 ButtonReleaseMask | StructureNotifyMask |
167 OwnerGrabButtonMask);
168 create_selener(x11,ew,Parent);
170 return ew;
173 void map_ewin(t_x11 *x11,t_enerwin *ew)
175 XMapWindow(x11->disp,ew->wd.self);
178 void add_ener(t_x11 *x11,t_enerwin *ew,t_energy e[])
180 int i,j,w;
182 w=ew->nwidth/2;
183 if (ew->nlast >= ew->nwidth) {
184 for(j=0; (j<ew->nre); j++)
185 for(i=0; (i<w); i++)
186 ew->e[j][i]=ew->e[j][i+w];
187 ew->nlast=w;
190 for(j=0; (j<ew->nre); j++) {
191 ew->e[j][ew->nlast]=e[j].e;
193 ew->nlast++;
194 ExposeWin(x11->disp,ew->wd.self);
197 void rewind_ener(t_x11 *x11,t_enerwin *ew)
199 ew->nlast=0;
200 ExposeWin(x11->disp,ew->wd.self);
203 void done_ew(t_x11 *x11,t_enerwin *ew)
205 done_menu(x11,ew->selener);
206 x11->UnRegisterCallback(x11,ew->wd.self);
207 sfree(ew);