Remove g_highway and vbox.c.
[alexxy/gromacs.git] / src / ngmx / ngmx.cpp
1 /*
2  *
3  *                This source code is part of
4  *
5  *                 G   R   O   M   A   C   S
6  *
7  *          GROningen MAchine for Chemical Simulations
8  *
9  *                        VERSION 3.2.0
10  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12  * Copyright (c) 2001-2004, The GROMACS development team,
13  * check out http://www.gromacs.org for more information.
14
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * If you want to redistribute modifications, please consider that
21  * scientific software is very special. Version control is crucial -
22  * bugs must be traceable. We will be happy to consider code for
23  * inclusion in the official distribution, but derived work must not
24  * be called official GROMACS. Details are found in the README & COPYING
25  * files - if they are missing, get the official version at www.gromacs.org.
26  *
27  * To help us fund GROMACS development, we humbly ask that you cite
28  * the papers on the package - you can find them in the top README file.
29  *
30  * For more info, check our website at http://www.gromacs.org
31  *
32  * And Hey:
33  * Gyas ROwers Mature At Cryogenic Speed
34  */
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #include <ctype.h>
40 #include <string.h>
41
42 #include "sysstuff.h"
43 #include "macros.h"
44 #include "smalloc.h"
45 #include "gmx_fatal.h"
46 #include "typedefs.h"
47 #include "string2.h"
48 #include "statutil.h"
49 #include "Xstuff.h"
50 #include "gromacs.bm"
51 #include "copyrite.h"
52 #include "confio.h"
53 #include "dialogs.h"
54 #include "writeps.h"
55 #include "molps.h"
56 #include "nmol.h"
57 #include "tpxio.h"
58
59 /* Forward declarations: I Don't want all that init shit here */
60 void init_gmx(t_x11 *x11, char *program, int nfile, t_filenm fnm[],
61               const output_env_t oenv);
62
63 static void dump_xw(char *dispname, Window w, char *fn)
64 {
65     char comm[256];
66
67     sprintf(comm, "xwd -id %d -display %s > %s", (int)w, dispname, fn);
68
69 #ifdef GMX_NO_SYSTEM
70     printf("Warning-- No calls to system(3) supported on this platform.");
71     printf("Warning-- Skipping execution of 'system(\"%s\")'.", buf);
72 #else
73     (void)system(comm);
74 #endif
75 }
76
77 static void dump_it(t_manager *man)
78 {
79     t_psdata ps;
80
81     ps = ps_open("ngmx.ps", 0, 0, man->molw->wd.width, man->molw->wd.height);
82     ps_draw_mol(ps, man);
83     ps_close(ps);
84 }
85
86 static void done_gmx(t_x11 *x11, t_gmx *gmx)
87 {
88     done_logo(x11, gmx->logo);
89     done_pd(x11, gmx->pd);
90     done_man(x11, gmx->man);
91     done_dlgs(gmx);
92     x11->UnRegisterCallback(x11, gmx->wd->self);
93 }
94
95 static void move_gmx(t_x11 *x11, t_gmx *gmx, int width, int height,
96                      gmx_bool bSizePD)
97 {
98     int y0, wl, hl;
99 #ifdef DEBUG
100     fprintf(stderr, "Move gmx %dx%d\n", width, height);
101 #endif
102     y0 = XTextHeight(x11->font);
103     /* Resize PD-Menu */
104     if (bSizePD)
105     {
106         XResizeWindow(x11->disp, gmx->pd->wd.self, width, y0);
107     }
108
109     XMoveWindow(x11->disp, gmx->man->wd.self, 0, y0+1);
110     XResizeWindow(x11->disp, gmx->man->wd.self, width, height-y0-1);
111
112     wl = gmx->logo->wd.width;
113     hl = gmx->logo->wd.height;
114     XMoveWindow(x11->disp, gmx->logo->wd.self, (width-wl)/2, (height-y0-hl)/2);
115 }
116
117 static gmx_bool HandleClient(t_x11 *x11, int ID, t_gmx *gmx)
118 {
119     t_pulldown *pd;
120
121     pd = gmx->pd;
122
123     switch (ID)
124     {
125         /* File Menu */
126         case IDDUMPWIN:
127             write_gmx(x11, gmx, IDDODUMP);
128             break;
129         case IDDODUMP:
130             if (gmx->man->bAnimate)
131             {
132                 hide_but(x11, gmx->man->vbox);
133             }
134             dump_it(gmx->man);
135             if (gmx->man->bAnimate)
136             {
137                 show_but(x11, gmx->man->vbox);
138             }
139             break;
140         case IDCLOSE:
141         case IDIMPORT:
142         case IDEXPORT:
143             ShowDlg(gmx->dlgs[edExport]);
144             break;
145         case IDDOEXPORT:
146             write_sto_conf(gmx->confout, *gmx->man->top.name,
147                            &(gmx->man->top.atoms),
148                            gmx->man->x, NULL, gmx->man->molw->ePBC, gmx->man->box);
149             break;
150         case IDQUIT:
151             show_mb(gmx, emQuit);
152             break;
153         case IDTERM:
154             done_gmx(x11, gmx);
155             return TRUE;
156
157         /* Edit Menu */
158         case IDEDITTOP:
159             edit_file("topol.gmx");
160             break;
161         case IDEDITCOORDS:
162             edit_file("confin.gmx");
163             break;
164         case IDEDITPARAMS:
165             edit_file("mdparin.gmx");
166             break;
167
168         /* Display Menu */
169         case IDFILTER:
170             if (gmx->filter)
171             {
172                 ShowDlg(gmx->dlgs[edFilter]);
173             }
174             break;
175         case IDDOFILTER:
176             do_filter(x11, gmx->man, gmx->filter);
177             break;
178         case IDANIMATE:
179             check_pd_item(pd, IDANIMATE, toggle_animate(x11, gmx->man));
180             break;
181         case IDLABELSOFF:
182             no_labels(x11, gmx->man);
183             break;
184         case IDRESETVIEW:
185             reset_view(gmx->man->view);
186             ExposeWin(x11->disp, gmx->man->molw->wd.self);
187             break;
188         case IDPHOTO:
189             show_mb(gmx, emNotImplemented);
190             break;
191         case IDBONDOPTS:
192             ShowDlg(gmx->dlgs[edBonds]);
193             break;
194         case IDTHIN:
195             set_bond_type(x11, gmx->man->molw, eBThin);
196             break;
197         case IDFAT:
198             set_bond_type(x11, gmx->man->molw, eBFat);
199             break;
200         case IDVERYFAT:
201             set_bond_type(x11, gmx->man->molw, eBVeryFat);
202             break;
203         case IDBALLS:
204             set_bond_type(x11, gmx->man->molw, eBSpheres);
205             break;
206         case IDNOBOX:
207             set_box_type(x11, gmx->man->molw, esbNone);
208             break;
209         case IDRECTBOX:
210             set_box_type(x11, gmx->man->molw, esbRect);
211             break;
212         case IDTRIBOX:
213             set_box_type(x11, gmx->man->molw, esbTri);
214             break;
215         case IDTOBOX:
216             set_box_type(x11, gmx->man->molw, esbTrunc);
217             break;
218
219         /* Analysis Menu */
220         case IDBOND:
221         case IDANGLE:
222         case IDDIH:
223         case IDRMS:
224         case IDRDF:
225         case IDENERGIES:
226         case IDCORR:
227             show_mb(gmx, emNotImplemented);
228             break;
229
230         /* Help Menu */
231         case IDHELP:
232             show_mb(gmx, emHelp);
233             break;
234         case IDABOUT:
235             show_logo(x11, gmx->logo);
236             break;
237
238         default:
239             break;
240     }
241     return FALSE;
242 }
243
244 static gmx_bool MainCallBack(t_x11 *x11, XEvent *event, Window w, void *data)
245 {
246     t_gmx    *gmx;
247     int       nsel, width, height;
248     gmx_bool  result;
249
250     result = FALSE;
251     gmx    = (t_gmx *)data;
252     switch (event->type)
253     {
254         case ButtonRelease:
255             hide_pd(x11, gmx->pd);
256             break;
257         case ConfigureNotify:
258             width  = event->xconfigure.width;
259             height = event->xconfigure.height;
260             if ((width != gmx->wd->width) || (height != gmx->wd->height))
261             {
262                 move_gmx(x11, gmx, width, height, TRUE);
263             }
264             break;
265         case ClientMessage:
266             hide_pd(x11, gmx->pd);
267             nsel   = event->xclient.data.l[0];
268             result = HandleClient(x11, nsel, gmx);
269             break;
270         default:
271             break;
272     }
273     return result;
274 }
275
276 int main(int argc, char *argv[])
277 {
278     const char  *desc[] = {
279         "[TT]ngmx[tt] is the GROMACS trajectory viewer. This program reads a",
280         "trajectory file, a run input file and an index file and plots a",
281         "3D structure of your molecule on your standard X Window",
282         "screen. No need for a high end graphics workstation, it even",
283         "works on Monochrome screens.[PAR]",
284         "The following features have been implemented:",
285         "3D view, rotation, translation and scaling of your molecule(s),",
286         "labels on atoms, animation of trajectories,",
287         "hardcopy in PostScript format, user defined atom-filters",
288         "runs on MIT-X (real X), open windows and motif,",
289         "user friendly menus, option to remove periodicity, option to",
290         "show computational box.[PAR]",
291         "Some of the more common X command line options can be used: ",
292         "[TT]-bg[tt], [TT]-fg[tt] change colors, [TT]-font fontname[tt] changes the font."
293     };
294     const char  *bugs[] = {
295         "Balls option does not work",
296         "Some times dumps core without a good reason"
297     };
298
299     output_env_t oenv;
300     t_x11       *x11;
301     t_filenm     fnm[] = {
302         { efTRX, "-f", NULL, ffREAD },
303         { efTPX, NULL, NULL, ffREAD },
304         { efNDX, NULL, NULL, ffOPTRD }
305     };
306 #define NFILE asize(fnm)
307
308     CopyRight(stderr, argv[0]);
309     parse_common_args(&argc, argv, PCA_CAN_TIME, NFILE, fnm,
310                       0, NULL, asize(desc), desc, asize(bugs), bugs, &oenv);
311
312     if ((x11 = GetX11(&argc, argv)) == NULL)
313     {
314         fprintf(stderr, "Can't connect to X Server.\n"
315                 "Check your DISPLAY environment variable\n");
316         exit(1);
317     }
318     init_gmx(x11, argv[0], NFILE, fnm, oenv);
319
320     x11->MainLoop(x11);
321     x11->CleanUp(x11);
322
323     gmx_thanx(stderr);
324
325     return 0;
326 }
327
328 static t_mentry  FileMenu[] = {
329     { 0,  IDEXPORT,   FALSE,  "Export..." },
330     { 0,  IDDUMPWIN,  FALSE,  "Print"     },
331     { 0,  IDQUIT,     FALSE,  "Quit"      }
332 };
333
334 static t_mentry  DispMenu[] = {
335     { 0,  IDFILTER,   FALSE,  "Filter..." },
336     { 0,  IDANIMATE,  FALSE,  "Animate"   },
337     { 0,  IDLABELSOFF,    FALSE,  "Labels Off"},
338     { 0,  IDRESETVIEW,    FALSE,  "Reset View"},
339     { 0,  IDBONDOPTS,     FALSE,  "Options..."}
340 };
341
342 static t_mentry  HelpMenu[] = {
343     { 0,  IDHELP,     FALSE,  "Help"             },
344     { 0,  IDABOUT,    FALSE,  "About Gromacs..." }
345 };
346
347 static t_mentry *gmx_pd[] = { FileMenu, DispMenu, HelpMenu };
348
349 #define MSIZE asize(gmx_pd)
350
351 static int         gmx_pd_size[MSIZE] = {
352     asize(FileMenu), asize(DispMenu), asize(HelpMenu)
353 };
354
355 static const char *MenuTitle[MSIZE] = {
356     "File", "Display", "Help"
357 };
358
359 void init_gmx(t_x11 *x11, char *program, int nfile, t_filenm fnm[],
360               const output_env_t oenv)
361 {
362     Pixmap                pm;
363     t_gmx                *gmx;
364     XSizeHints            hints;
365     int                   w0, h0;
366     int                   natom, natom_trx;
367     t_topology            top;
368     int                   ePBC;
369     matrix                box;
370     t_trxframe            fr;
371     t_trxstatus          *status;
372     char                  quote[256];
373
374     snew(gmx, 1);
375     snew(gmx->wd, 1);
376
377     ePBC = read_tpx_top(ftp2fn(efTPX, nfile, fnm),
378                         NULL, box, &natom, NULL, NULL, NULL, &top);
379
380     read_first_frame(oenv, &status, ftp2fn(efTRX, nfile, fnm), &fr, TRX_DONT_SKIP);
381     close_trx(status);
382     natom_trx = fr.natoms;
383
384     /* Creates a simple window */
385     w0 = DisplayWidth(x11->disp, x11->screen)-132;
386     h0 = DisplayHeight(x11->disp, x11->screen)-140;
387     bromacs(quote, 255);
388     InitWin(gmx->wd, 0, 0, w0, h0, 3, quote);
389     gmx->wd->self = XCreateSimpleWindow(x11->disp, x11->root,
390                                         gmx->wd->x, gmx->wd->y,
391                                         gmx->wd->width, gmx->wd->height,
392                                         gmx->wd->bwidth, WHITE, BLACK);
393     pm = XCreatePixmapFromBitmapData(x11->disp, x11->root,
394                                      (char *)gromacs_bits, gromacs_width,
395                                      gromacs_height,
396                                      WHITE, BLACK, 1);
397     hints.flags      = PMinSize;
398     hints.min_width  = 2*EWIDTH+40;
399     hints.min_height = EHEIGHT+LDHEIGHT+LEGHEIGHT+40;
400     XSetStandardProperties(x11->disp, gmx->wd->self, gmx->wd->text, program,
401                            pm, NULL, 0, &hints);
402
403     x11->RegisterCallback(x11, gmx->wd->self, x11->root, MainCallBack, gmx);
404     x11->SetInputMask(x11, gmx->wd->self,
405                       ButtonPressMask     | ButtonReleaseMask |
406                       OwnerGrabButtonMask | ExposureMask      |
407                       StructureNotifyMask);
408
409     /* The order of creating windows is important here! */
410     /* Manager */
411     gmx->man  = init_man(x11, gmx->wd->self, 0, 0, 1, 1, WHITE, BLACK, ePBC, box, oenv);
412     gmx->logo = init_logo(x11, gmx->wd->self, FALSE);
413
414     /* Now put all windows in the proper place */
415     move_gmx(x11, gmx, w0, h0, FALSE);
416
417     XMapWindow(x11->disp, gmx->wd->self);
418     map_man(x11, gmx->man);
419
420     /* Pull Down menu */
421     gmx->pd = init_pd(x11, gmx->wd->self, gmx->wd->width,
422                       XTextHeight(x11->font), x11->fg, x11->bg,
423                       MSIZE, gmx_pd_size, gmx_pd, MenuTitle);
424
425     /* Dialogs & Filters */
426
427     gmx->filter = init_filter(&(top.atoms), ftp2fn_null(efNDX, nfile, fnm),
428                               natom_trx);
429
430     init_dlgs(x11, gmx);
431
432     /* Now do file shit */
433     set_file(x11, gmx->man, ftp2fn(efTRX, nfile, fnm), ftp2fn(efTPX, nfile, fnm));
434
435     /*show_logo(x11,gmx->logo);*/
436
437     ShowDlg(gmx->dlgs[edFilter]);
438 }