Code beautification with uncrustify
[alexxy/gromacs.git] / src / ngmx / g_xrama.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 <stdlib.h>
40 #include <math.h>
41 #include <string.h>
42 #include "macros.h"
43 #include "Xstuff.h"
44 #include "copyrite.h"
45 #include "xutil.h"
46 #include "futil.h"
47 #include "x11.h"
48 #include "smalloc.h"
49 #include "statutil.h"
50 #include "rama.bm"
51 #include "nrama.h"
52
53 #define MAXDEG 360
54
55 enum {
56     ebQuit, ebStart, ebStop, ebRewind, ebGly, ebutNR
57 };
58
59 static real scx, scy;
60 #define SX(x) ((int)(((x)+M_PI)*scx))
61 #define SY(y) ((int)((M_PI-(y))*scy))
62
63 enum {
64     esStop, esGo, esNR
65 };
66
67 typedef struct {
68     int            status;
69     gmx_bool       bShowGly;
70     gmx_bool      *bIsGly;
71     t_windata      wd;
72     t_windata      xrwd;
73     t_xrama       *xr;
74     t_windata      but[ebutNR];
75 } t_app;
76
77 static void plot_pp(t_x11 *x11, Window w, t_phipsi *pp, t_dih dih[])
78 {
79     int x0, y0;
80     int th = (XTextHeight(x11->font)+6)/2;
81
82     x0 = SX(dih[pp->iphi].ang);
83     y0 = SY(dih[pp->ipsi].ang);
84     XFillRectangle(x11->disp, w, x11->gc, x0-1, y0-1, 4, 4);
85     /* Draw Label ? */
86     if (pp->bShow)
87     {
88         TextInRect(x11, w, pp->label, x0+6, y0-th, 30, 2*th, eXLeft, eYCenter);
89     }
90 }
91
92 static gmx_bool label_pp(t_x11 *x11, Window w, int npp, t_phipsi pp[],
93                          t_dih dih[], int mx, int my)
94 {
95     int d, md, x0, y0;
96     int i, imin;
97
98     imin = -1;
99     md   = 16;
100     for (i = 0; (i < npp); i++)
101     {
102         x0 = SX(dih[pp[i].iphi].ang);
103         y0 = SY(dih[pp[i].ipsi].ang);
104         d  = (mx-x0)*(mx-x0)+(my-y0)*(my-y0);
105         if (d < md)
106         {
107             md   = d;
108             imin = i;
109         }
110     }
111     if (imin != -1)
112     {
113         pp[imin].bShow = !pp[imin].bShow;
114         return TRUE;
115     }
116     return FALSE;
117 }
118
119 static gmx_bool xrCallBack(struct t_x11 *x11, XEvent *event, Window w, void *data)
120 {
121     t_app   *app;
122     t_xrama *xr;
123     char     buf[256];
124     int      i;
125
126     (void)XTextHeight(x11->font);
127     app = (t_app *)data;
128     xr  = app->xr;
129     scx = app->xrwd.width/(2.0*M_PI);
130     scy = app->xrwd.height/(2.0*M_PI);
131     switch (event->type)
132     {
133         case Expose:
134             XClearWindow(x11->disp, app->xrwd.self);
135             XDrawLine(x11->disp, app->xrwd.self, x11->gc,
136                       SX(0), SY(-M_PI)+1, SX(0), SY(M_PI)-1);
137             XDrawLine(x11->disp, app->xrwd.self, x11->gc,
138                       SX(-M_PI)+1, SY(0), SX(M_PI)-1, SY(0));
139             TextInRect(x11, app->xrwd.self, "Phi", SX(M_PI)-50, SY(0)+4, 46, 20, eXRight, eYTop);
140             TextInRect(x11, app->xrwd.self, "Psi", SX(0)+4, 4, 46, 20, eXLeft, eYTop);
141             for (i = 0; (i < xr->npp); i++)
142             {
143                 if (app->bShowGly || !app->bIsGly[i])
144                 {
145                     plot_pp(x11, app->xrwd.self, &(xr->pp[i]), xr->dih);
146                 }
147             }
148             break;
149         case ButtonPress:
150             if (label_pp(x11, app->xrwd.self, xr->npp, xr->pp, xr->dih,
151                          event->xbutton.x, event->xbutton.y))
152             {
153                 ExposeWin(x11->disp, app->xrwd.self);
154             }
155             break;
156         case ConfigureNotify:
157             app->xrwd.width  = event->xconfigure.width;
158             app->xrwd.height = event->xconfigure.height;
159             break;
160     }
161     if (app->status == esGo)
162     {
163         if (!new_data(app->xr))
164         {
165             app->status = ebStop;
166         }
167         else
168         {
169             ExposeWin(x11->disp, app->xrwd.self);
170             sprintf(buf, "Rama: t=%.2f", app->xr->t);
171             XSetStandardProperties(x11->disp, app->wd.self, buf,
172                                    "Rama", 0, NULL, 0, NULL);
173
174         }
175     }
176     return FALSE;
177 }
178
179 static gmx_bool appCallBack(struct t_x11 *x11, XEvent *event, Window w, void *data)
180 {
181     t_app  *app;
182     int     win;
183
184     app = (t_app *)data;
185     for (win = 0; (win < ebutNR); win++)
186     {
187         if (app->but[win].self == w)
188         {
189             break;
190         }
191     }
192     if (win == ebutNR)
193     {
194         return FALSE;
195     }
196
197     switch (event->type)
198     {
199         case Expose:
200             TextInWin(x11, &(app->but[win]), app->but[win].text, eXCenter, eYCenter);
201             break;
202         case ButtonPress:
203             switch (win)
204             {
205                 case ebQuit:
206                     exit(1);
207                 case ebStart:
208                     app->status = esGo;
209                     ExposeWin(x11->disp, app->xrwd.self);
210                     break;
211                 case ebStop:
212                     app->status = esStop;
213                     break;
214                 case ebRewind:
215                     rewind_trj(app->xr->traj);
216                     break;
217                 case ebGly:
218                     app->bShowGly = !app->bShowGly;
219                     ExposeWin(x11->disp, app->xrwd.self);
220                     break;
221                 default:
222                     XBell(x11->disp, 50);
223                     break;
224             }
225             break;
226     }
227     return FALSE;
228 }
229
230 static void size_app(t_x11 *x11, t_app *app)
231 {
232     int i, dx, th;
233
234     th = XTextHeight(x11->font)+4;
235     dx = app->wd.width/ebutNR;
236     for (i = 0; (i < ebutNR); i++)
237     {
238         app->but[i].width  = dx-4;
239         app->but[i].height = th+4;
240         XMoveResizeWindow(x11->disp, app->but[i].self, i*dx+2, 2, dx-4, th+4);
241     }
242     XMoveResizeWindow(x11->disp, app->xrwd.self, 2, th+10,
243                       app->wd.width-6, app->wd.height-th-10-4);
244 }
245
246 static gmx_bool mainCallBack(struct t_x11 *x11, XEvent *event, Window w, void *data)
247 {
248     t_app *app;
249     int    wt, ht;
250
251     app = (t_app *)data;
252     switch (event->type)
253     {
254         case ConfigureNotify:
255             wt = event->xconfigure.width;
256             ht = event->xconfigure.height;
257             if ((app->wd.width != wt) || (app->wd.height != ht))
258             {
259                 fprintf(stderr, "New wxh = %dx%d\n", wt, ht);
260                 app->wd.width  = wt;
261                 app->wd.height = ht;
262                 size_app(x11, app);
263             }
264             break;
265     }
266     return FALSE;
267 }
268
269 static t_xrama *init_xrama(t_x11 *x11, Window Parent, int y0, t_app *app)
270 {
271     t_xrama *xr;
272
273     snew(xr, 1);
274
275     InitWin(&(app->xrwd), 2, y0, MAXDEG+1, MAXDEG+1, 1, "Ramachandran Movie");
276     app->xrwd.self = XCreateSimpleWindow(x11->disp, Parent, app->xrwd.x, app->xrwd.y,
277                                          app->xrwd.width, app->xrwd.height,
278                                          app->xrwd.bwidth, x11->fg, x11->bg);
279     x11->RegisterCallback(x11, app->xrwd.self, Parent, xrCallBack, app);
280     x11->SetInputMask(x11, app->xrwd.self, ButtonPressMask | ExposureMask |
281                       StructureNotifyMask);
282
283     return xr;
284 }
285
286 static t_app *init_app(t_x11 *x11, int argc, char *argv[])
287 {
288     static const char *but_nm[ebutNR] = { "Quit", "Start", "Stop", "Rewind", "Toggle Gly" };
289     XSizeHints         hints;
290     Pixmap             pm;
291     int                th;
292     t_app             *app;
293     t_windata         *wd;
294     int                i, dx;
295
296     snew(app, 1);
297     th = XTextHeight(x11->font)+4;
298     InitWin(&(app->wd), 0, 0, MAXDEG+6, MAXDEG+6+th+6, 0, "Ramachandran Movie");
299     dx           = app->wd.width/ebutNR;
300     app->wd.self = XCreateSimpleWindow(x11->disp, x11->root, app->wd.x, app->wd.y,
301                                        app->wd.width, app->wd.height,
302                                        app->wd.bwidth, x11->fg, x11->bg);
303     x11->RegisterCallback(x11, app->wd.self, x11->root, mainCallBack, app);
304     x11->SetInputMask(x11, app->wd.self, StructureNotifyMask);
305     hints.flags = 0;
306     pm          = XCreatePixmapFromBitmapData(x11->disp, x11->root, (char *)rama_bits,
307                                               rama_width, rama_height, WHITE, BLACK, 1);
308     XSetStandardProperties(x11->disp, app->wd.self, app->wd.text,
309                            "Rama", pm, argv, argc, &hints);
310     x11->RegisterCallback(x11, app->wd.self, x11->root, appCallBack, app);
311     x11->SetInputMask(x11, app->wd.self, ButtonPressMask | ExposureMask |
312                       StructureNotifyMask);
313
314     app->xr = init_xrama(x11, app->wd.self, th+6, app);
315     for (i = 0; (i < ebutNR); i++)
316     {
317         wd = &(app->but[i]);
318         InitWin(wd, i*dx+2, 2, dx-4, th, 1, but_nm[i]);
319         wd->self = XCreateSimpleWindow(x11->disp, app->wd.self,
320                                        wd->x, wd->y, wd->width, wd->height,
321                                        wd->bwidth, x11->fg, x11->bg);
322         x11->RegisterCallback(x11, wd->self, app->wd.self, appCallBack, app);
323         x11->SetInputMask(x11, wd->self, ButtonPressMask | ExposureMask);
324     }
325     return app;
326 }
327
328 static void mk_gly(t_app *app)
329 {
330     int i;
331
332     snew(app->bIsGly, app->xr->npp);
333     for (i = 0; (i < app->xr->npp); i++)
334     {
335         if (strstr(app->xr->pp[i].label, "GLY") != NULL)
336         {
337             app->bIsGly[i] = TRUE;
338         }
339     }
340 }
341
342 int main(int argc, char *argv[])
343 {
344     const char  *desc[] = {
345         "[TT]g_xrama[tt] shows a Ramachandran movie, that is, it shows",
346         "the Phi/Psi angles as a function of time in an X-Window.[PAR]"
347         "Static Phi/Psi plots for printing can be made with [TT]g_rama[tt].[PAR]",
348         "Some of the more common X command line options can be used:[BR]",
349         "[TT]-bg[tt], [TT]-fg[tt] change colors, [TT]-font fontname[tt], changes the font."
350     };
351
352     output_env_t oenv;
353     t_x11       *x11;
354     t_topology  *ramatop;
355     t_app       *app;
356     t_filenm     fnm[] = {
357         { efTRX, "-f", NULL, ffREAD },
358         { efTPX, NULL, NULL, ffREAD }
359     };
360 #define NFILE asize(fnm)
361
362     CopyRight(stderr, argv[0]);
363     parse_common_args(&argc, argv, PCA_CAN_TIME, NFILE, fnm, 0, NULL,
364                       asize(desc), desc, 0, NULL, &oenv);
365
366
367     if ((x11 = GetX11(&argc, argv)) == NULL)
368     {
369         fprintf(stderr, "Can't open display, set your DISPLAY environment variable\n");
370         exit(1);
371     }
372     XSetForeground(x11->disp, x11->gc, x11->fg);
373     app = init_app(x11, argc, argv);
374
375     ramatop = init_rama(oenv, ftp2fn(efTRX, NFILE, fnm), ftp2fn(efTPX, NFILE, fnm),
376                         app->xr, 3);
377     mk_gly(app);
378
379     XMapWindow(x11->disp, app->wd.self);
380     XMapSubwindows(x11->disp, app->wd.self);
381     x11->MainLoop(x11);
382     x11->CleanUp(x11);
383
384     thanx(stderr);
385
386     return 0;
387 }