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