Enable missing declarations warning
[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,2015,2017, 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 "gmxpre.h"
38
39 #include "view.h"
40
41 #include "config.h"
42
43 #include <cstdio>
44
45 #include <string>
46
47 #include "gromacs/commandline/pargs.h"
48 #include "gromacs/fileio/confio.h"
49 #include "gromacs/fileio/oenv.h"
50 #include "gromacs/fileio/tpxio.h"
51 #include "gromacs/trajectory/trajectoryframe.h"
52 #include "gromacs/utility/arraysize.h"
53 #include "gromacs/utility/coolstuff.h"
54 #include "gromacs/utility/fatalerror.h"
55 #include "gromacs/utility/smalloc.h"
56
57 #if GMX_X11
58
59 #include "gromacs/fileio/writeps.h"
60
61 #include "Xstuff.h"
62 #include "dialogs.h"
63 #include "gromacs.bm"
64 #include "molps.h"
65 #include "nmol.h"
66 #include "xutil.h"
67
68 static void dump_it(t_manager *man)
69 {
70     t_psdata ps;
71
72     ps = ps_open("view.ps", 0, 0, man->molw->wd.width, man->molw->wd.height);
73     ps_draw_mol(ps, man);
74     ps_close(ps);
75 }
76
77 static void done_gmx(t_x11 *x11, t_gmx *gmx)
78 {
79     done_logo(x11, gmx->logo);
80     done_pd(x11, gmx->pd);
81     done_man(x11, gmx->man);
82     done_dlgs(gmx);
83     x11->UnRegisterCallback(x11, gmx->wd->self);
84 }
85
86 static void move_gmx(t_x11 *x11, t_gmx *gmx, int width, int height,
87                      bool bSizePD)
88 {
89     int y0, wl, hl;
90 #ifdef DEBUG
91     std::fprintf(stderr, "Move gmx %dx%d\n", width, height);
92 #endif
93     y0 = XTextHeight(x11->font);
94     /* Resize PD-Menu */
95     if (bSizePD)
96     {
97         XResizeWindow(x11->disp, gmx->pd->wd.self, width, y0);
98     }
99
100     XMoveWindow(x11->disp, gmx->man->wd.self, 0, y0+1);
101     XResizeWindow(x11->disp, gmx->man->wd.self, width, height-y0-1);
102
103     wl = gmx->logo->wd.width;
104     hl = gmx->logo->wd.height;
105     XMoveWindow(x11->disp, gmx->logo->wd.self, (width-wl)/2, (height-y0-hl)/2);
106 }
107
108 static bool HandleClient(t_x11 *x11, int ID, t_gmx *gmx)
109 {
110     t_pulldown *pd;
111
112     pd = gmx->pd;
113
114     switch (ID)
115     {
116         /* File Menu */
117         case IDDUMPWIN:
118             write_gmx(x11, gmx, IDDODUMP);
119             break;
120         case IDDODUMP:
121             if (gmx->man->bAnimate)
122             {
123                 hide_but(x11, gmx->man->vbox);
124             }
125             dump_it(gmx->man);
126             if (gmx->man->bAnimate)
127             {
128                 show_but(x11, gmx->man->vbox);
129             }
130             break;
131         case IDCLOSE:
132         case IDIMPORT:
133         case IDEXPORT:
134             ShowDlg(gmx->dlgs[edExport]);
135             break;
136         case IDDOEXPORT:
137             write_sto_conf(gmx->confout, *gmx->man->top.name,
138                            &(gmx->man->top.atoms),
139                            gmx->man->x, nullptr, gmx->man->molw->ePBC, gmx->man->box);
140             break;
141         case IDQUIT:
142             show_mb(gmx, emQuit);
143             break;
144         case IDTERM:
145             done_gmx(x11, gmx);
146             return true;
147
148         /* Edit Menu */
149         case IDEDITTOP:
150             edit_file("topol.gmx");
151             break;
152         case IDEDITCOORDS:
153             edit_file("confin.gmx");
154             break;
155         case IDEDITPARAMS:
156             edit_file("mdparin.gmx");
157             break;
158
159         /* Display Menu */
160         case IDFILTER:
161             if (gmx->filter)
162             {
163                 ShowDlg(gmx->dlgs[edFilter]);
164             }
165             break;
166         case IDDOFILTER:
167             do_filter(x11, gmx->man, gmx->filter);
168             break;
169         case IDANIMATE:
170             check_pd_item(pd, IDANIMATE, toggle_animate(x11, gmx->man));
171             break;
172         case IDLABELSOFF:
173             no_labels(x11, gmx->man);
174             break;
175         case IDRESETVIEW:
176             reset_view(gmx->man->view);
177             ExposeWin(x11->disp, gmx->man->molw->wd.self);
178             break;
179         case IDPHOTO:
180             show_mb(gmx, emNotImplemented);
181             break;
182         case IDBONDOPTS:
183             ShowDlg(gmx->dlgs[edBonds]);
184             break;
185         case IDTHIN:
186             set_bond_type(x11, gmx->man->molw, eBThin);
187             break;
188         case IDFAT:
189             set_bond_type(x11, gmx->man->molw, eBFat);
190             break;
191         case IDVERYFAT:
192             set_bond_type(x11, gmx->man->molw, eBVeryFat);
193             break;
194         case IDBALLS:
195             set_bond_type(x11, gmx->man->molw, eBSpheres);
196             break;
197         case IDNOBOX:
198             set_box_type(x11, gmx->man->molw, esbNone);
199             break;
200         case IDRECTBOX:
201             set_box_type(x11, gmx->man->molw, esbRect);
202             break;
203         case IDTRIBOX:
204             set_box_type(x11, gmx->man->molw, esbTri);
205             break;
206         case IDTOBOX:
207             set_box_type(x11, gmx->man->molw, esbTrunc);
208             break;
209
210         /* Analysis Menu */
211         case IDBOND:
212         case IDANGLE:
213         case IDDIH:
214         case IDRMS:
215         case IDRDF:
216         case IDENERGIES:
217         case IDCORR:
218             show_mb(gmx, emNotImplemented);
219             break;
220
221         /* Help Menu */
222         case IDHELP:
223             show_mb(gmx, emHelp);
224             break;
225         case IDABOUT:
226             show_logo(x11, gmx->logo);
227             break;
228
229         default:
230             break;
231     }
232     return false;
233 }
234
235 static bool MainCallBack(t_x11 *x11, XEvent *event, Window /*w*/, void *data)
236 {
237     t_gmx    *gmx;
238     int       nsel, width, height;
239     bool      result;
240
241     result = false;
242     gmx    = (t_gmx *)data;
243     switch (event->type)
244     {
245         case ButtonRelease:
246             hide_pd(x11, gmx->pd);
247             break;
248         case ConfigureNotify:
249             width  = event->xconfigure.width;
250             height = event->xconfigure.height;
251             if ((width != gmx->wd->width) || (height != gmx->wd->height))
252             {
253                 move_gmx(x11, gmx, width, height, true);
254             }
255             break;
256         case ClientMessage:
257             hide_pd(x11, gmx->pd);
258             nsel   = event->xclient.data.l[0];
259             result = HandleClient(x11, nsel, gmx);
260             break;
261         default:
262             break;
263     }
264     return result;
265 }
266
267 static t_mentry  FileMenu[] = {
268     { 0,  IDEXPORT,   false,  "Export..." },
269     { 0,  IDDUMPWIN,  false,  "Print"     },
270     { 0,  IDQUIT,     false,  "Quit"      }
271 };
272
273 static t_mentry  DispMenu[] = {
274     { 0,  IDFILTER,   false,  "Filter..." },
275     { 0,  IDANIMATE,  false,  "Animate"   },
276     { 0,  IDLABELSOFF,    false,  "Labels Off"},
277     { 0,  IDRESETVIEW,    false,  "Reset View"},
278     { 0,  IDBONDOPTS,     false,  "Options..."}
279 };
280
281 static t_mentry  HelpMenu[] = {
282     { 0,  IDHELP,     false,  "Help"             },
283     { 0,  IDABOUT,    false,  "About GROMACS..." }
284 };
285
286 static t_mentry *gmx_pd[] = { FileMenu, DispMenu, HelpMenu };
287
288 #define MSIZE asize(gmx_pd)
289
290 static int         gmx_pd_size[MSIZE] = {
291     asize(FileMenu), asize(DispMenu), asize(HelpMenu)
292 };
293
294 static const char *MenuTitle[MSIZE] = {
295     "File", "Display", "Help"
296 };
297
298 static void init_gmx(t_x11 *x11, char *program, int nfile, t_filenm fnm[],
299                      gmx_output_env_t *oenv)
300 {
301     Pixmap                pm;
302     t_gmx                *gmx;
303     XSizeHints            hints;
304     int                   w0, h0;
305     int                   natom, natom_trx;
306     t_topology            top;
307     int                   ePBC;
308     matrix                box;
309     t_trxframe            fr;
310     t_trxstatus          *status;
311
312     snew(gmx, 1);
313     snew(gmx->wd, 1);
314
315     ePBC = read_tpx_top(ftp2fn(efTPR, nfile, fnm),
316                         nullptr, box, &natom, nullptr, nullptr, &top);
317
318     read_first_frame(oenv, &status, ftp2fn(efTRX, nfile, fnm), &fr, TRX_DONT_SKIP);
319     close_trx(status);
320     natom_trx = fr.natoms;
321
322     /* Creates a simple window */
323     w0 = DisplayWidth(x11->disp, x11->screen)-132;
324     h0 = DisplayHeight(x11->disp, x11->screen)-140;
325     InitWin(gmx->wd, 0, 0, w0, h0, 3, gmx::bromacs().c_str());
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, nullptr, 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 operations */
370     set_file(x11, gmx->man, ftp2fn(efTRX, nfile, fnm), ftp2fn(efTPR, 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     gmx_output_env_t *oenv;
400     t_filenm          fnm[] = {
401         { efTRX, "-f", nullptr, ffREAD },
402         { efTPR, nullptr, nullptr, ffREAD },
403         { efNDX, nullptr, nullptr, ffOPTRD }
404     };
405 #define NFILE asize(fnm)
406
407     if (parse_common_args(&argc, argv, PCA_CAN_TIME, NFILE, fnm,
408                           0, nullptr, asize(desc), desc, asize(bugs), bugs, &oenv))
409     {
410 #if !GMX_X11
411         std::fprintf(stderr, "Compiled without X-Windows - can not run viewer.\n");
412 #else
413         t_x11 *x11;
414
415         if ((x11 = GetX11(&argc, argv)) == nullptr)
416         {
417             std::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 }