Remove repeat sfree(state->nosehoover_xi)
[alexxy/gromacs.git] / src / ngmx / ngmx.cpp
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 <ctype.h>
40 #include <string.h>
41
42 #include "sysstuff.h"
43 #include "macros.h"
44 #include "smalloc.h"
45 #include "gmx_fatal.h"
46 #include "typedefs.h"
47 #include "string2.h"
48 #include "statutil.h"
49 #include "Xstuff.h"
50 #include "gromacs.bm"
51 #include "copyrite.h"
52 #include "confio.h"
53 #include "dialogs.h"
54 #include "writeps.h"
55 #include "molps.h"
56 #include "nmol.h"
57 #include "tpxio.h"
58
59 #include "gromacs/commandline/cmdlinemodulemanager.h"
60
61 /* Forward declarations: I Don't want all that init shit here */
62 void init_gmx(t_x11 *x11, char *program, int nfile, t_filenm fnm[],
63               const output_env_t oenv);
64
65 static void dump_it(t_manager *man)
66 {
67     t_psdata ps;
68
69     ps = ps_open("ngmx.ps", 0, 0, man->molw->wd.width, man->molw->wd.height);
70     ps_draw_mol(ps, man);
71     ps_close(ps);
72 }
73
74 static void done_gmx(t_x11 *x11, t_gmx *gmx)
75 {
76     done_logo(x11, gmx->logo);
77     done_pd(x11, gmx->pd);
78     done_man(x11, gmx->man);
79     done_dlgs(gmx);
80     x11->UnRegisterCallback(x11, gmx->wd->self);
81 }
82
83 static void move_gmx(t_x11 *x11, t_gmx *gmx, int width, int height,
84                      gmx_bool bSizePD)
85 {
86     int y0, wl, hl;
87 #ifdef DEBUG
88     fprintf(stderr, "Move gmx %dx%d\n", width, height);
89 #endif
90     y0 = XTextHeight(x11->font);
91     /* Resize PD-Menu */
92     if (bSizePD)
93     {
94         XResizeWindow(x11->disp, gmx->pd->wd.self, width, y0);
95     }
96
97     XMoveWindow(x11->disp, gmx->man->wd.self, 0, y0+1);
98     XResizeWindow(x11->disp, gmx->man->wd.self, width, height-y0-1);
99
100     wl = gmx->logo->wd.width;
101     hl = gmx->logo->wd.height;
102     XMoveWindow(x11->disp, gmx->logo->wd.self, (width-wl)/2, (height-y0-hl)/2);
103 }
104
105 static gmx_bool HandleClient(t_x11 *x11, int ID, t_gmx *gmx)
106 {
107     t_pulldown *pd;
108
109     pd = gmx->pd;
110
111     switch (ID)
112     {
113         /* File Menu */
114         case IDDUMPWIN:
115             write_gmx(x11, gmx, IDDODUMP);
116             break;
117         case IDDODUMP:
118             if (gmx->man->bAnimate)
119             {
120                 hide_but(x11, gmx->man->vbox);
121             }
122             dump_it(gmx->man);
123             if (gmx->man->bAnimate)
124             {
125                 show_but(x11, gmx->man->vbox);
126             }
127             break;
128         case IDCLOSE:
129         case IDIMPORT:
130         case IDEXPORT:
131             ShowDlg(gmx->dlgs[edExport]);
132             break;
133         case IDDOEXPORT:
134             write_sto_conf(gmx->confout, *gmx->man->top.name,
135                            &(gmx->man->top.atoms),
136                            gmx->man->x, NULL, gmx->man->molw->ePBC, gmx->man->box);
137             break;
138         case IDQUIT:
139             show_mb(gmx, emQuit);
140             break;
141         case IDTERM:
142             done_gmx(x11, gmx);
143             return TRUE;
144
145         /* Edit Menu */
146         case IDEDITTOP:
147             edit_file("topol.gmx");
148             break;
149         case IDEDITCOORDS:
150             edit_file("confin.gmx");
151             break;
152         case IDEDITPARAMS:
153             edit_file("mdparin.gmx");
154             break;
155
156         /* Display Menu */
157         case IDFILTER:
158             if (gmx->filter)
159             {
160                 ShowDlg(gmx->dlgs[edFilter]);
161             }
162             break;
163         case IDDOFILTER:
164             do_filter(x11, gmx->man, gmx->filter);
165             break;
166         case IDANIMATE:
167             check_pd_item(pd, IDANIMATE, toggle_animate(x11, gmx->man));
168             break;
169         case IDLABELSOFF:
170             no_labels(x11, gmx->man);
171             break;
172         case IDRESETVIEW:
173             reset_view(gmx->man->view);
174             ExposeWin(x11->disp, gmx->man->molw->wd.self);
175             break;
176         case IDPHOTO:
177             show_mb(gmx, emNotImplemented);
178             break;
179         case IDBONDOPTS:
180             ShowDlg(gmx->dlgs[edBonds]);
181             break;
182         case IDTHIN:
183             set_bond_type(x11, gmx->man->molw, eBThin);
184             break;
185         case IDFAT:
186             set_bond_type(x11, gmx->man->molw, eBFat);
187             break;
188         case IDVERYFAT:
189             set_bond_type(x11, gmx->man->molw, eBVeryFat);
190             break;
191         case IDBALLS:
192             set_bond_type(x11, gmx->man->molw, eBSpheres);
193             break;
194         case IDNOBOX:
195             set_box_type(x11, gmx->man->molw, esbNone);
196             break;
197         case IDRECTBOX:
198             set_box_type(x11, gmx->man->molw, esbRect);
199             break;
200         case IDTRIBOX:
201             set_box_type(x11, gmx->man->molw, esbTri);
202             break;
203         case IDTOBOX:
204             set_box_type(x11, gmx->man->molw, esbTrunc);
205             break;
206
207         /* Analysis Menu */
208         case IDBOND:
209         case IDANGLE:
210         case IDDIH:
211         case IDRMS:
212         case IDRDF:
213         case IDENERGIES:
214         case IDCORR:
215             show_mb(gmx, emNotImplemented);
216             break;
217
218         /* Help Menu */
219         case IDHELP:
220             show_mb(gmx, emHelp);
221             break;
222         case IDABOUT:
223             show_logo(x11, gmx->logo);
224             break;
225
226         default:
227             break;
228     }
229     return FALSE;
230 }
231
232 static gmx_bool MainCallBack(t_x11 *x11, XEvent *event, Window w, void *data)
233 {
234     t_gmx    *gmx;
235     int       nsel, width, height;
236     gmx_bool  result;
237
238     result = FALSE;
239     gmx    = (t_gmx *)data;
240     switch (event->type)
241     {
242         case ButtonRelease:
243             hide_pd(x11, gmx->pd);
244             break;
245         case ConfigureNotify:
246             width  = event->xconfigure.width;
247             height = event->xconfigure.height;
248             if ((width != gmx->wd->width) || (height != gmx->wd->height))
249             {
250                 move_gmx(x11, gmx, width, height, TRUE);
251             }
252             break;
253         case ClientMessage:
254             hide_pd(x11, gmx->pd);
255             nsel   = event->xclient.data.l[0];
256             result = HandleClient(x11, nsel, gmx);
257             break;
258         default:
259             break;
260     }
261     return result;
262 }
263
264 int gmx_ngmx(int argc, char *argv[])
265 {
266     const char  *desc[] = {
267         "[TT]ngmx[tt] is the GROMACS trajectory viewer. This program reads a",
268         "trajectory file, a run input file and an index file and plots a",
269         "3D structure of your molecule on your standard X Window",
270         "screen. No need for a high end graphics workstation, it even",
271         "works on Monochrome screens.[PAR]",
272         "The following features have been implemented:",
273         "3D view, rotation, translation and scaling of your molecule(s),",
274         "labels on atoms, animation of trajectories,",
275         "hardcopy in PostScript format, user defined atom-filters",
276         "runs on MIT-X (real X), open windows and motif,",
277         "user friendly menus, option to remove periodicity, option to",
278         "show computational box.[PAR]",
279         "Some of the more common X command line options can be used: ",
280         "[TT]-bg[tt], [TT]-fg[tt] change colors, [TT]-font fontname[tt] changes the font."
281     };
282     const char  *bugs[] = {
283         "Balls option does not work",
284         "Some times dumps core without a good reason"
285     };
286
287     output_env_t oenv;
288     t_x11       *x11;
289     t_filenm     fnm[] = {
290         { efTRX, "-f", NULL, ffREAD },
291         { efTPX, NULL, NULL, ffREAD },
292         { efNDX, NULL, NULL, ffOPTRD }
293     };
294 #define NFILE asize(fnm)
295
296     if (!parse_common_args(&argc, argv, PCA_CAN_TIME, NFILE, fnm,
297                            0, NULL, asize(desc), desc, asize(bugs), bugs, &oenv))
298     {
299         return 0;
300     }
301
302     if ((x11 = GetX11(&argc, argv)) == NULL)
303     {
304         fprintf(stderr, "Can't connect to X Server.\n"
305                 "Check your DISPLAY environment variable\n");
306         exit(1);
307     }
308     init_gmx(x11, argv[0], NFILE, fnm, oenv);
309
310     x11->MainLoop(x11);
311     x11->CleanUp(x11);
312
313     return 0;
314 }
315
316 int main(int argc, char *argv[])
317 {
318     return gmx::CommandLineModuleManager::runAsMainCMain(argc, argv, &gmx_ngmx);
319 }
320
321 static t_mentry  FileMenu[] = {
322     { 0,  IDEXPORT,   FALSE,  "Export..." },
323     { 0,  IDDUMPWIN,  FALSE,  "Print"     },
324     { 0,  IDQUIT,     FALSE,  "Quit"      }
325 };
326
327 static t_mentry  DispMenu[] = {
328     { 0,  IDFILTER,   FALSE,  "Filter..." },
329     { 0,  IDANIMATE,  FALSE,  "Animate"   },
330     { 0,  IDLABELSOFF,    FALSE,  "Labels Off"},
331     { 0,  IDRESETVIEW,    FALSE,  "Reset View"},
332     { 0,  IDBONDOPTS,     FALSE,  "Options..."}
333 };
334
335 static t_mentry  HelpMenu[] = {
336     { 0,  IDHELP,     FALSE,  "Help"             },
337     { 0,  IDABOUT,    FALSE,  "About Gromacs..." }
338 };
339
340 static t_mentry *gmx_pd[] = { FileMenu, DispMenu, HelpMenu };
341
342 #define MSIZE asize(gmx_pd)
343
344 static int         gmx_pd_size[MSIZE] = {
345     asize(FileMenu), asize(DispMenu), asize(HelpMenu)
346 };
347
348 static const char *MenuTitle[MSIZE] = {
349     "File", "Display", "Help"
350 };
351
352 void init_gmx(t_x11 *x11, char *program, int nfile, t_filenm fnm[],
353               const output_env_t oenv)
354 {
355     Pixmap                pm;
356     t_gmx                *gmx;
357     XSizeHints            hints;
358     int                   w0, h0;
359     int                   natom, natom_trx;
360     t_topology            top;
361     int                   ePBC;
362     matrix                box;
363     t_trxframe            fr;
364     t_trxstatus          *status;
365     char                  quote[256];
366
367     snew(gmx, 1);
368     snew(gmx->wd, 1);
369
370     ePBC = read_tpx_top(ftp2fn(efTPX, nfile, fnm),
371                         NULL, box, &natom, NULL, NULL, NULL, &top);
372
373     read_first_frame(oenv, &status, ftp2fn(efTRX, nfile, fnm), &fr, TRX_DONT_SKIP);
374     close_trx(status);
375     natom_trx = fr.natoms;
376
377     /* Creates a simple window */
378     w0 = DisplayWidth(x11->disp, x11->screen)-132;
379     h0 = DisplayHeight(x11->disp, x11->screen)-140;
380     bromacs(quote, 255);
381     InitWin(gmx->wd, 0, 0, w0, h0, 3, quote);
382     gmx->wd->self = XCreateSimpleWindow(x11->disp, x11->root,
383                                         gmx->wd->x, gmx->wd->y,
384                                         gmx->wd->width, gmx->wd->height,
385                                         gmx->wd->bwidth, WHITE, BLACK);
386     pm = XCreatePixmapFromBitmapData(x11->disp, x11->root,
387                                      (char *)gromacs_bits, gromacs_width,
388                                      gromacs_height,
389                                      WHITE, BLACK, 1);
390     hints.flags      = PMinSize;
391     hints.min_width  = 2*EWIDTH+40;
392     hints.min_height = EHEIGHT+LDHEIGHT+LEGHEIGHT+40;
393     XSetStandardProperties(x11->disp, gmx->wd->self, gmx->wd->text, program,
394                            pm, NULL, 0, &hints);
395
396     x11->RegisterCallback(x11, gmx->wd->self, x11->root, MainCallBack, gmx);
397     x11->SetInputMask(x11, gmx->wd->self,
398                       ButtonPressMask     | ButtonReleaseMask |
399                       OwnerGrabButtonMask | ExposureMask      |
400                       StructureNotifyMask);
401
402     /* The order of creating windows is important here! */
403     /* Manager */
404     gmx->man  = init_man(x11, gmx->wd->self, 0, 0, 1, 1, WHITE, BLACK, ePBC, box, oenv);
405     gmx->logo = init_logo(x11, gmx->wd->self, FALSE);
406
407     /* Now put all windows in the proper place */
408     move_gmx(x11, gmx, w0, h0, FALSE);
409
410     XMapWindow(x11->disp, gmx->wd->self);
411     map_man(x11, gmx->man);
412
413     /* Pull Down menu */
414     gmx->pd = init_pd(x11, gmx->wd->self, gmx->wd->width,
415                       XTextHeight(x11->font), x11->fg, x11->bg,
416                       MSIZE, gmx_pd_size, gmx_pd, MenuTitle);
417
418     /* Dialogs & Filters */
419
420     gmx->filter = init_filter(&(top.atoms), ftp2fn_null(efNDX, nfile, fnm),
421                               natom_trx);
422
423     init_dlgs(x11, gmx);
424
425     /* Now do file shit */
426     set_file(x11, gmx->man, ftp2fn(efTRX, nfile, fnm), ftp2fn(efTPX, nfile, fnm));
427
428     /*show_logo(x11,gmx->logo);*/
429
430     ShowDlg(gmx->dlgs[edFilter]);
431 }