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