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