changed reading hint
[gromacs/adressmacs.git] / src / ngmx / xrama.c
bloba4edbdb31a086ef8852ef2af4908e28756c01a11
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 2.0
12 * Copyright (c) 1991-1999
13 * BIOSON Research Institute, Dept. of Biophysical Chemistry
14 * University of Groningen, The Netherlands
16 * Please refer to:
17 * GROMACS: A message-passing parallel molecular dynamics implementation
18 * H.J.C. Berendsen, D. van der Spoel and R. van Drunen
19 * Comp. Phys. Comm. 91, 43-56 (1995)
21 * Also check out our WWW page:
22 * http://md.chem.rug.nl/~gmx
23 * or e-mail to:
24 * gromacs@chem.rug.nl
26 * And Hey:
27 * Great Red Oystrich Makes All Chemists Sane
29 static char *SRCID_xrama_c = "$Id$";
31 #include <stdlib.h>
32 #include <math.h>
33 #include <string.h>
34 #include "Xstuff.h"
35 #include "copyrite.h"
36 #include "macros.h"
37 #include "xutil.h"
38 #include "futil.h"
39 #include "x11.h"
40 #include "smalloc.h"
41 #include "statutil.h"
42 #include "rama.bm"
43 #include "nrama.h"
44 #include "lutab.h"
46 #define MAXDEG 360
48 enum { ebQuit, ebStart, ebStop, ebRewind, ebGly, ebutNR };
50 static real scx,scy;
51 #define SX(x) ((int)(((x)+M_PI)*scx))
52 #define SY(y) ((int)((M_PI-(y))*scy))
54 enum { esStop, esGo, esNR };
56 typedef struct {
57 int status;
58 bool bShowGly;
59 bool *bIsGly;
60 t_windata wd;
61 t_windata xrwd;
62 t_xrama *xr;
63 t_windata but[ebutNR];
64 } t_app;
66 static void plot_pp(t_x11 *x11,Window w,t_phipsi *pp,t_dih dih[])
68 int x0,y0;
69 int th=(XTextHeight(x11->font)+6)/2;
71 x0=SX(dih[pp->iphi].ang);
72 y0=SY(dih[pp->ipsi].ang);
73 XFillRectangle(x11->disp,w,x11->gc,x0-1,y0-1,4,4);
74 /* Draw Label ? */
75 if (pp->bShow)
76 TextInRect(x11,w,pp->label,x0+6,y0-th,30,2*th,eXLeft,eYCenter);
79 static bool label_pp(t_x11 *x11,Window w,int npp,t_phipsi pp[],
80 t_dih dih[],int mx,int my)
82 int d,md,x0,y0;
83 int i,imin;
85 imin=-1;
86 md=16;
87 for(i=0; (i<npp); i++) {
88 x0=SX(dih[pp[i].iphi].ang);
89 y0=SY(dih[pp[i].ipsi].ang);
90 d=(mx-x0)*(mx-x0)+(my-y0)*(my-y0);
91 if (d < md) {
92 md=d;
93 imin=i;
96 if (imin != -1) {
97 pp[imin].bShow=!pp[imin].bShow;
98 return TRUE;
100 return FALSE;
103 static bool xrCallBack(struct t_x11 *x11,XEvent *event, Window w, void *data)
105 t_app *app;
106 t_xrama *xr;
107 char buf[256];
108 int i;
110 (void)XTextHeight(x11->font);
111 app=(t_app *)data;
112 xr=app->xr;
113 scx=app->xrwd.width/(2.0*M_PI);
114 scy=app->xrwd.height/(2.0*M_PI);
115 switch (event->type) {
116 case Expose:
117 XClearWindow(x11->disp,app->xrwd.self);
118 XDrawLine(x11->disp,app->xrwd.self,x11->gc,
119 SX(0),SY(-M_PI)+1,SX(0),SY(M_PI)-1);
120 XDrawLine(x11->disp,app->xrwd.self,x11->gc,
121 SX(-M_PI)+1,SY(0),SX(M_PI)-1,SY(0));
122 TextInRect(x11,app->xrwd.self,"Phi",SX(M_PI)-50,SY(0)+4,46,20,eXRight,eYTop);
123 TextInRect(x11,app->xrwd.self,"Psi",SX(0)+4,4,46,20,eXLeft,eYTop);
124 for(i=0; (i<xr->npp); i++)
125 if (app->bShowGly || !app->bIsGly[i])
126 plot_pp(x11,app->xrwd.self,&(xr->pp[i]),xr->dih);
127 break;
128 case ButtonPress:
129 if (label_pp(x11,app->xrwd.self,xr->npp,xr->pp,xr->dih,
130 event->xbutton.x,event->xbutton.y))
131 ExposeWin(x11->disp,app->xrwd.self);
132 break;
133 case ConfigureNotify:
134 app->xrwd.width=event->xconfigure.width;
135 app->xrwd.height=event->xconfigure.height;
136 break;
138 if (app->status == esGo) {
139 if (!new_data(app->xr))
140 app->status=ebStop;
141 else {
142 ExposeWin(x11->disp,app->xrwd.self);
143 sprintf(buf,"Rama: t=%.2f",app->xr->t);
144 XSetStandardProperties(x11->disp,app->wd.self,buf,
145 "Rama",0,NULL,0,NULL);
149 return FALSE;
152 static bool appCallBack(struct t_x11 *x11,XEvent *event, Window w, void *data)
154 t_app *app;
155 int win;
157 app=(t_app *)data;
158 for(win=0; (win<ebutNR); win++)
159 if (app->but[win].self == w)
160 break;
161 if (win==ebutNR)
162 return FALSE;
164 switch(event->type) {
165 case Expose:
166 TextInWin(x11,&(app->but[win]),app->but[win].text,eXCenter,eYCenter);
167 break;
168 case ButtonPress:
169 switch (win) {
170 case ebQuit:
171 exit(1);
172 case ebStart:
173 app->status=esGo;
174 ExposeWin(x11->disp,app->xrwd.self);
175 break;
176 case ebStop:
177 app->status=esStop;
178 break;
179 case ebRewind:
180 rewind_trj(app->xr->traj);
181 break;
182 case ebGly:
183 app->bShowGly=!app->bShowGly;
184 ExposeWin(x11->disp,app->xrwd.self);
185 break;
186 default:
187 XBell(x11->disp,50);
188 break;
190 break;
192 return FALSE;
195 static void size_app(t_x11 *x11,t_app *app)
197 int i,dx,th;
199 th=XTextHeight(x11->font)+4;
200 dx=app->wd.width/ebutNR;
201 for(i=0; (i<ebutNR); i++) {
202 app->but[i].width=dx-4;
203 app->but[i].height=th+4;
204 XMoveResizeWindow(x11->disp,app->but[i].self,i*dx+2,2,dx-4,th+4);
206 XMoveResizeWindow(x11->disp,app->xrwd.self,2,th+10,
207 app->wd.width-6,app->wd.height-th-10-4);
210 static bool mainCallBack(struct t_x11 *x11,XEvent *event, Window w, void *data)
212 t_app *app;
213 int wt,ht;
215 app=(t_app *)data;
216 switch (event->type) {
217 case ConfigureNotify:
218 wt=event->xconfigure.width;
219 ht=event->xconfigure.height;
220 if ((app->wd.width != wt) || (app->wd.height != ht)) {
221 fprintf(stderr,"New wxh = %dx%d\n",wt,ht);
222 app->wd.width=wt;
223 app->wd.height=ht;
224 size_app(x11,app);
226 break;
228 return FALSE;
231 static t_xrama *init_xrama(t_x11 *x11,Window Parent,int y0,t_app *app)
233 t_xrama *xr;
235 snew(xr,1);
237 InitWin(&(app->xrwd),2,y0,MAXDEG+1,MAXDEG+1,1,"Ramachandran Movie");
238 app->xrwd.self=XCreateSimpleWindow(x11->disp,Parent,app->xrwd.x,app->xrwd.y,
239 app->xrwd.width,app->xrwd.height,
240 app->xrwd.bwidth,x11->fg,x11->bg);
241 x11->RegisterCallback(x11,app->xrwd.self,Parent,xrCallBack,app);
242 x11->SetInputMask(x11,app->xrwd.self,ButtonPressMask | ExposureMask |
243 StructureNotifyMask);
245 return xr;
248 static t_app *init_app(t_x11 *x11,int argc,char *argv[])
250 static char *but_nm[ebutNR] = { "Quit", "Start", "Stop", "Rewind", "Toggle Gly" };
251 XSizeHints hints;
252 Pixmap pm;
253 int th;
254 t_app *app;
255 t_windata *wd;
256 int i,dx;
258 snew(app,1);
259 th=XTextHeight(x11->font)+4;
260 InitWin(&(app->wd),0,0,MAXDEG+6,MAXDEG+6+th+6,0,"Ramachandran Movie");
261 dx=app->wd.width/ebutNR;
262 app->wd.self=XCreateSimpleWindow(x11->disp,x11->root,app->wd.x,app->wd.y,
263 app->wd.width,app->wd.height,
264 app->wd.bwidth,x11->fg,x11->bg);
265 x11->RegisterCallback(x11,app->wd.self,x11->root,mainCallBack,app);
266 x11->SetInputMask(x11,app->wd.self,StructureNotifyMask);
267 hints.flags=0;
268 pm=XCreatePixmapFromBitmapData(x11->disp,x11->root,(char *)rama_bits,
269 rama_width,rama_height,WHITE,BLACK,1);
270 XSetStandardProperties(x11->disp,app->wd.self,app->wd.text,
271 "Rama",pm,argv,argc,&hints);
272 x11->RegisterCallback(x11,app->wd.self,x11->root,appCallBack,app);
273 x11->SetInputMask(x11,app->wd.self,ButtonPressMask | ExposureMask |
274 StructureNotifyMask);
276 app->xr=init_xrama(x11,app->wd.self,th+6,app);
277 for(i=0; (i<ebutNR); i++) {
278 wd=&(app->but[i]);
279 InitWin(wd,i*dx+2,2,dx-4,th,1,but_nm[i]);
280 wd->self=XCreateSimpleWindow(x11->disp,app->wd.self,
281 wd->x,wd->y,wd->width,wd->height,
282 wd->bwidth,x11->fg,x11->bg);
283 x11->RegisterCallback(x11,wd->self,app->wd.self,appCallBack,app);
284 x11->SetInputMask(x11,wd->self,ButtonPressMask | ExposureMask);
286 return app;
289 static void mk_gly(t_app *app)
291 int i;
293 snew(app->bIsGly,app->xr->npp);
294 for(i=0; (i<app->xr->npp); i++) {
295 if (strstr(app->xr->pp[i].label,"GLY") != NULL)
296 app->bIsGly[i]=TRUE;
300 int main(int argc,char *argv[])
302 static char *desc[] = {
303 "xrama shows a Ramachandran movie, that is, it shows",
304 "the Phi/Psi angles as a function of time in an X-Window.[PAR]"
305 "Static Phi/Psi plots for printing can be made with g_rama.[PAR]",
306 "Some of the more common X command line options can be used:[BR]",
307 "-bg, -fg change colors, -font fontname, changes the font."
310 t_x11 *x11;
311 t_app *app;
312 t_filenm fnm[] = {
313 { efTRX, "-f", NULL, ffREAD },
314 { efTPX, NULL, NULL, ffREAD }
316 #define NFILE asize(fnm)
318 CopyRight(stderr,argv[0]);
319 parse_common_args(&argc,argv,PCA_CAN_TIME,FALSE,NFILE,fnm,0,NULL,
320 asize(desc),desc,0,NULL);
322 init_lookup_table(stdout);
324 if ((x11=GetX11(&argc,argv)) == NULL) {
325 fprintf(stderr,"Can't open display, set your DISPLAY environment variable\n");
326 exit(1);
328 XSetForeground(x11->disp,x11->gc,x11->fg);
329 app=init_app(x11,argc,argv);
331 init_rama(ftp2fn(efTRX,NFILE,fnm),ftp2fn(efTPX,NFILE,fnm),app->xr);
332 mk_gly(app);
334 XMapWindow(x11->disp,app->wd.self);
335 XMapSubwindows(x11->disp,app->wd.self);
336 x11->MainLoop(x11);
337 x11->CleanUp(x11);
339 thanx(stdout);
341 return 0;