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