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,2019,2020, 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 
dump_it(t_manager * man)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 
done_gmx(t_x11 * x11,t_gmx * gmx)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 
move_gmx(t_x11 * x11,t_gmx * gmx,int width,int height,bool bSizePD)86 static void move_gmx(t_x11* x11, t_gmx* gmx, int width, int height, bool bSizePD)
87 {
88     int y0, wl, hl;
89 #    ifdef DEBUG
90     std::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 
HandleClient(t_x11 * x11,int ID,t_gmx * gmx)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: write_gmx(x11, gmx, IDDODUMP); 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: ShowDlg(gmx->dlgs[edExport]); break;
131         case IDDOEXPORT:
132             write_sto_conf(gmx->confout, *gmx->man->top.name, &(gmx->man->top.atoms), gmx->man->x,
133                            nullptr, gmx->man->molw->pbcType, gmx->man->box);
134             break;
135         case IDQUIT: show_mb(gmx, emQuit); break;
136         case IDTERM: done_gmx(x11, gmx); return true;
137 
138         /* Edit Menu */
139         case IDEDITTOP: edit_file("topol.gmx"); break;
140         case IDEDITCOORDS: edit_file("confin.gmx"); break;
141         case IDEDITPARAMS: edit_file("mdparin.gmx"); break;
142 
143         /* Display Menu */
144         case IDFILTER:
145             if (gmx->filter)
146             {
147                 ShowDlg(gmx->dlgs[edFilter]);
148             }
149             break;
150         case IDDOFILTER: do_filter(x11, gmx->man, gmx->filter); break;
151         case IDANIMATE: check_pd_item(pd, IDANIMATE, toggle_animate(x11, gmx->man)); break;
152         case IDLABELSOFF: no_labels(x11, gmx->man); break;
153         case IDRESETVIEW:
154             reset_view(gmx->man->view);
155             ExposeWin(x11->disp, gmx->man->molw->wd.self);
156             break;
157         case IDPHOTO: show_mb(gmx, emNotImplemented); break;
158         case IDBONDOPTS: ShowDlg(gmx->dlgs[edBonds]); break;
159         case IDTHIN: set_bond_type(x11, gmx->man->molw, eBThin); break;
160         case IDFAT: set_bond_type(x11, gmx->man->molw, eBFat); break;
161         case IDVERYFAT: set_bond_type(x11, gmx->man->molw, eBVeryFat); break;
162         case IDBALLS: set_bond_type(x11, gmx->man->molw, eBSpheres); break;
163         case IDNOBOX: set_box_type(x11, gmx->man->molw, esbNone); break;
164         case IDRECTBOX: set_box_type(x11, gmx->man->molw, esbRect); break;
165         case IDTRIBOX: set_box_type(x11, gmx->man->molw, esbTri); break;
166         case IDTOBOX: set_box_type(x11, gmx->man->molw, esbTrunc); break;
167 
168         /* Analysis Menu */
169         case IDBOND:
170         case IDANGLE:
171         case IDDIH:
172         case IDRMS:
173         case IDRDF:
174         case IDENERGIES:
175         case IDCORR: show_mb(gmx, emNotImplemented); break;
176 
177         /* Help Menu */
178         case IDHELP: show_mb(gmx, emHelp); break;
179         case IDABOUT: show_logo(x11, gmx->logo); break;
180 
181         default: break;
182     }
183     return false;
184 }
185 
MainCallBack(t_x11 * x11,XEvent * event,Window,void * data)186 static bool MainCallBack(t_x11* x11, XEvent* event, Window /*w*/, void* data)
187 {
188     t_gmx* gmx;
189     int    nsel, width, height;
190     bool   result;
191 
192     result = false;
193     gmx    = (t_gmx*)data;
194     switch (event->type)
195     {
196         case ButtonRelease: hide_pd(x11, gmx->pd); break;
197         case ConfigureNotify:
198             width  = event->xconfigure.width;
199             height = event->xconfigure.height;
200             if ((width != gmx->wd->width) || (height != gmx->wd->height))
201             {
202                 move_gmx(x11, gmx, width, height, true);
203             }
204             break;
205         case ClientMessage:
206             hide_pd(x11, gmx->pd);
207             nsel   = event->xclient.data.l[0];
208             result = HandleClient(x11, nsel, gmx);
209             break;
210         default: break;
211     }
212     return result;
213 }
214 
215 static t_mentry FileMenu[] = { { 0, IDEXPORT, false, "Export..." },
216                                { 0, IDDUMPWIN, false, "Print" },
217                                { 0, IDQUIT, false, "Quit" } };
218 
219 static t_mentry DispMenu[] = { { 0, IDFILTER, false, "Filter..." },
220                                { 0, IDANIMATE, false, "Animate" },
221                                { 0, IDLABELSOFF, false, "Labels Off" },
222                                { 0, IDRESETVIEW, false, "Reset View" },
223                                { 0, IDBONDOPTS, false, "Options..." } };
224 
225 static t_mentry HelpMenu[] = { { 0, IDHELP, false, "Help" },
226                                { 0, IDABOUT, false, "About GROMACS..." } };
227 
228 static t_mentry* gmx_pd[] = { FileMenu, DispMenu, HelpMenu };
229 
230 #    define MSIZE asize(gmx_pd)
231 
232 static int gmx_pd_size[MSIZE] = { asize(FileMenu), asize(DispMenu), asize(HelpMenu) };
233 
234 static const char* MenuTitle[MSIZE] = { "File", "Display", "Help" };
235 
init_gmx(t_x11 * x11,char * program,int nfile,t_filenm fnm[],gmx_output_env_t * oenv)236 static void init_gmx(t_x11* x11, char* program, int nfile, t_filenm fnm[], gmx_output_env_t* oenv)
237 {
238     Pixmap       pm;
239     t_gmx*       gmx;
240     XSizeHints   hints;
241     int          w0, h0;
242     int          natom, natom_trx;
243     t_topology   top;
244     PbcType      pbcType;
245     matrix       box;
246     t_trxframe   fr;
247     t_trxstatus* status;
248 
249     snew(gmx, 1);
250     snew(gmx->wd, 1);
251 
252     pbcType = read_tpx_top(ftp2fn(efTPR, nfile, fnm), nullptr, box, &natom, nullptr, nullptr, &top);
253 
254     read_first_frame(oenv, &status, ftp2fn(efTRX, nfile, fnm), &fr, TRX_DONT_SKIP);
255     close_trx(status);
256     natom_trx = fr.natoms;
257 
258     /* Creates a simple window */
259     w0 = DisplayWidth(x11->disp, x11->screen) - 132;
260     h0 = DisplayHeight(x11->disp, x11->screen) - 140;
261     InitWin(gmx->wd, 0, 0, w0, h0, 3, gmx::bromacs().c_str());
262     gmx->wd->self = XCreateSimpleWindow(x11->disp, x11->root, gmx->wd->x, gmx->wd->y, gmx->wd->width,
263                                         gmx->wd->height, gmx->wd->bwidth, WHITE, BLACK);
264     pm = XCreatePixmapFromBitmapData(x11->disp, x11->root, (char*)gromacs_bits, gromacs_width,
265                                      gromacs_height, WHITE, BLACK, 1);
266     hints.flags      = PMinSize;
267     hints.min_width  = 2 * EWIDTH + 40;
268     hints.min_height = EHEIGHT + LDHEIGHT + LEGHEIGHT + 40;
269     XSetStandardProperties(x11->disp, gmx->wd->self, gmx->wd->text, program, pm, nullptr, 0, &hints);
270 
271     x11->RegisterCallback(x11, gmx->wd->self, x11->root, MainCallBack, gmx);
272     x11->SetInputMask(x11, gmx->wd->self,
273                       ButtonPressMask | ButtonReleaseMask | OwnerGrabButtonMask | ExposureMask
274                               | StructureNotifyMask);
275 
276     /* The order of creating windows is important here! */
277     /* Manager */
278     gmx->man  = init_man(x11, gmx->wd->self, 0, 0, 1, 1, WHITE, BLACK, pbcType, box, oenv);
279     gmx->logo = init_logo(x11, gmx->wd->self, false);
280 
281     /* Now put all windows in the proper place */
282     move_gmx(x11, gmx, w0, h0, false);
283 
284     XMapWindow(x11->disp, gmx->wd->self);
285     map_man(x11, gmx->man);
286 
287     /* Pull Down menu */
288     gmx->pd = init_pd(x11, gmx->wd->self, gmx->wd->width, x11->fg, x11->bg, MSIZE, gmx_pd_size,
289                       gmx_pd, MenuTitle);
290 
291     /* Dialogs & Filters */
292 
293     gmx->filter = init_filter(&(top.atoms), ftp2fn_null(efNDX, nfile, fnm), natom_trx);
294 
295     init_dlgs(x11, gmx);
296 
297     /* Now do file operations */
298     set_file(x11, gmx->man, ftp2fn(efTRX, nfile, fnm), ftp2fn(efTPR, nfile, fnm));
299 
300     ShowDlg(gmx->dlgs[edFilter]);
301 }
302 #endif
303 
gmx_view(int argc,char * argv[])304 int gmx_view(int argc, char* argv[])
305 {
306     const char* desc[] = {
307         "[THISMODULE] is the GROMACS trajectory viewer. This program reads a",
308         "trajectory file, a run input file and an index file and plots a",
309         "3D structure of your molecule on your standard X Window",
310         "screen. No need for a high end graphics workstation, it even",
311         "works on Monochrome screens.[PAR]",
312         "The following features have been implemented:",
313         "3D view, rotation, translation and scaling of your molecule(s),",
314         "labels on atoms, animation of trajectories,",
315         "hardcopy in PostScript format, user defined atom-filters",
316         "runs on MIT-X (real X), open windows and motif,",
317         "user friendly menus, option to remove periodicity, option to",
318         "show computational box.[PAR]",
319         "Some of the more common X command line options can be used: ",
320         "[TT]-bg[tt], [TT]-fg[tt] change colors, [TT]-font fontname[tt] changes the font."
321     };
322     const char* bugs[] = { "Balls option does not work",
323                            "Some times dumps core without a good reason" };
324 
325     gmx_output_env_t* oenv;
326     t_filenm          fnm[] = { { efTRX, "-f", nullptr, ffREAD },
327                        { efTPR, nullptr, nullptr, ffREAD },
328                        { efNDX, nullptr, nullptr, ffOPTRD } };
329 #define NFILE asize(fnm)
330 
331     if (parse_common_args(&argc, argv, PCA_CAN_TIME, NFILE, fnm, 0, nullptr, asize(desc), desc,
332                           asize(bugs), bugs, &oenv))
333     {
334 #if !GMX_X11
335         std::fprintf(stderr, "Compiled without X-Windows - can not run viewer.\n");
336 #else
337         t_x11* x11;
338 
339         if ((x11 = GetX11(&argc, argv)) == nullptr)
340         {
341             std::fprintf(stderr,
342                          "Can't connect to X Server.\n"
343                          "Check your DISPLAY environment variable\n");
344         }
345         else
346         {
347             init_gmx(x11, argv[0], NFILE, fnm, oenv);
348             x11->MainLoop(x11);
349             x11->CleanUp(x11);
350         }
351 #endif
352     }
353     return 0;
354 }
355