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