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