Merge release-4-6 into release-5-0
[alexxy/gromacs.git] / src / programs / view / view.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2013,2014, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 #include <stdio.h>
42 #include <string.h>
43
44 #include "sysstuff.h"
45 #include "macros.h"
46 #include "gromacs/utility/smalloc.h"
47 #include "gmx_fatal.h"
48 #include "typedefs.h"
49 #include "gromacs/commandline/pargs.h"
50 #include "copyrite.h"
51 #include "gromacs/fileio/confio.h"
52 #include "gromacs/fileio/tpxio.h"
53
54 #ifdef GMX_X11
55
56 #include "gromacs/fileio/writeps.h"
57
58 #include "Xstuff.h"
59 #include "gromacs.bm"
60 #include "xutil.h"
61 #include "dialogs.h"
62 #include "molps.h"
63 #include "nmol.h"
64
65 static void dump_it(t_manager *man)
66 {
67     t_psdata ps;
68
69     ps = ps_open("view.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                      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 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 bool MainCallBack(t_x11 *x11, XEvent *event, Window /*w*/, void *data)
233 {
234     t_gmx    *gmx;
235     int       nsel, width, height;
236     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 static t_mentry  FileMenu[] = {
265     { 0,  IDEXPORT,   false,  "Export..." },
266     { 0,  IDDUMPWIN,  false,  "Print"     },
267     { 0,  IDQUIT,     false,  "Quit"      }
268 };
269
270 static t_mentry  DispMenu[] = {
271     { 0,  IDFILTER,   false,  "Filter..." },
272     { 0,  IDANIMATE,  false,  "Animate"   },
273     { 0,  IDLABELSOFF,    false,  "Labels Off"},
274     { 0,  IDRESETVIEW,    false,  "Reset View"},
275     { 0,  IDBONDOPTS,     false,  "Options..."}
276 };
277
278 static t_mentry  HelpMenu[] = {
279     { 0,  IDHELP,     false,  "Help"             },
280     { 0,  IDABOUT,    false,  "About Gromacs..." }
281 };
282
283 static t_mentry *gmx_pd[] = { FileMenu, DispMenu, HelpMenu };
284
285 #define MSIZE asize(gmx_pd)
286
287 static int         gmx_pd_size[MSIZE] = {
288     asize(FileMenu), asize(DispMenu), asize(HelpMenu)
289 };
290
291 static const char *MenuTitle[MSIZE] = {
292     "File", "Display", "Help"
293 };
294
295 static void init_gmx(t_x11 *x11, char *program, int nfile, t_filenm fnm[],
296                      const output_env_t oenv)
297 {
298     Pixmap                pm;
299     t_gmx                *gmx;
300     XSizeHints            hints;
301     int                   w0, h0;
302     int                   natom, natom_trx;
303     t_topology            top;
304     int                   ePBC;
305     matrix                box;
306     t_trxframe            fr;
307     t_trxstatus          *status;
308     char                  quote[256];
309
310     snew(gmx, 1);
311     snew(gmx->wd, 1);
312
313     ePBC = read_tpx_top(ftp2fn(efTPX, nfile, fnm),
314                         NULL, box, &natom, NULL, NULL, NULL, &top);
315
316     read_first_frame(oenv, &status, ftp2fn(efTRX, nfile, fnm), &fr, TRX_DONT_SKIP);
317     close_trx(status);
318     natom_trx = fr.natoms;
319
320     /* Creates a simple window */
321     w0 = DisplayWidth(x11->disp, x11->screen)-132;
322     h0 = DisplayHeight(x11->disp, x11->screen)-140;
323     bromacs(quote, 255);
324     InitWin(gmx->wd, 0, 0, w0, h0, 3, quote);
325     gmx->wd->self = XCreateSimpleWindow(x11->disp, x11->root,
326                                         gmx->wd->x, gmx->wd->y,
327                                         gmx->wd->width, gmx->wd->height,
328                                         gmx->wd->bwidth, WHITE, BLACK);
329     pm = XCreatePixmapFromBitmapData(x11->disp, x11->root,
330                                      (char *)gromacs_bits, gromacs_width,
331                                      gromacs_height,
332                                      WHITE, BLACK, 1);
333     hints.flags      = PMinSize;
334     hints.min_width  = 2*EWIDTH+40;
335     hints.min_height = EHEIGHT+LDHEIGHT+LEGHEIGHT+40;
336     XSetStandardProperties(x11->disp, gmx->wd->self, gmx->wd->text, program,
337                            pm, NULL, 0, &hints);
338
339     x11->RegisterCallback(x11, gmx->wd->self, x11->root, MainCallBack, gmx);
340     x11->SetInputMask(x11, gmx->wd->self,
341                       ButtonPressMask     | ButtonReleaseMask |
342                       OwnerGrabButtonMask | ExposureMask      |
343                       StructureNotifyMask);
344
345     /* The order of creating windows is important here! */
346     /* Manager */
347     gmx->man  = init_man(x11, gmx->wd->self, 0, 0, 1, 1, WHITE, BLACK, ePBC, box, oenv);
348     gmx->logo = init_logo(x11, gmx->wd->self, false);
349
350     /* Now put all windows in the proper place */
351     move_gmx(x11, gmx, w0, h0, false);
352
353     XMapWindow(x11->disp, gmx->wd->self);
354     map_man(x11, gmx->man);
355
356     /* Pull Down menu */
357     gmx->pd = init_pd(x11, gmx->wd->self, gmx->wd->width,
358                       x11->fg, x11->bg,
359                       MSIZE, gmx_pd_size, gmx_pd, MenuTitle);
360
361     /* Dialogs & Filters */
362
363     gmx->filter = init_filter(&(top.atoms), ftp2fn_null(efNDX, nfile, fnm),
364                               natom_trx);
365
366     init_dlgs(x11, gmx);
367
368     /* Now do file operations */
369     set_file(x11, gmx->man, ftp2fn(efTRX, nfile, fnm), ftp2fn(efTPX, nfile, fnm));
370
371     ShowDlg(gmx->dlgs[edFilter]);
372 }
373 #endif
374
375 int gmx_view(int argc, char *argv[])
376 {
377     const char  *desc[] = {
378         "[THISMODULE] is the GROMACS trajectory viewer. This program reads a",
379         "trajectory file, a run input file and an index file and plots a",
380         "3D structure of your molecule on your standard X Window",
381         "screen. No need for a high end graphics workstation, it even",
382         "works on Monochrome screens.[PAR]",
383         "The following features have been implemented:",
384         "3D view, rotation, translation and scaling of your molecule(s),",
385         "labels on atoms, animation of trajectories,",
386         "hardcopy in PostScript format, user defined atom-filters",
387         "runs on MIT-X (real X), open windows and motif,",
388         "user friendly menus, option to remove periodicity, option to",
389         "show computational box.[PAR]",
390         "Some of the more common X command line options can be used: ",
391         "[TT]-bg[tt], [TT]-fg[tt] change colors, [TT]-font fontname[tt] changes the font."
392     };
393     const char  *bugs[] = {
394         "Balls option does not work",
395         "Some times dumps core without a good reason"
396     };
397
398     output_env_t oenv;
399     t_filenm     fnm[] = {
400         { efTRX, "-f", NULL, ffREAD },
401         { efTPX, NULL, NULL, ffREAD },
402         { efNDX, NULL, NULL, ffOPTRD }
403     };
404 #define NFILE asize(fnm)
405
406     if (parse_common_args(&argc, argv, PCA_CAN_TIME, NFILE, fnm,
407                           0, NULL, asize(desc), desc, asize(bugs), bugs, &oenv))
408     {
409 #ifndef GMX_X11
410         fprintf(stderr, "Compiled without X-Windows - can not run viewer.\n");
411 #else
412         t_x11 *x11;
413
414         if ((x11 = GetX11(&argc, argv)) == NULL)
415         {
416             fprintf(stderr, "Can't connect to X Server.\n"
417                     "Check your DISPLAY environment variable\n");
418         }
419         else
420         {
421             init_gmx(x11, argv[0], NFILE, fnm, oenv);
422             x11->MainLoop(x11);
423             x11->CleanUp(x11);
424         }
425 #endif
426     }
427     return 0;
428 }