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