Tagged files with gromacs 3.0 header and added some license info
[alexxy/gromacs.git] / src / ngmx / dialogs.c
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.0
11  * 
12  * Copyright (c) 1991-2001
13  * BIOSON Research Institute, Dept. of Biophysical Chemistry
14  * University of Groningen, The Netherlands
15  * 
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  * 
21  * If you want to redistribute modifications, please consider that
22  * scientific software is very special. Version control is crucial -
23  * bugs must be traceable. We will be happy to consider code for
24  * inclusion in the official distribution, but derived work must not
25  * be called official GROMACS. Details are found in the README & COPYING
26  * files - if they are missing, get the official version at www.gromacs.org.
27  * 
28  * To help us fund GROMACS development, we humbly ask that you cite
29  * the papers on the package - you can find them in the top README file.
30  * 
31  * Do check out http://www.gromacs.org , or mail us at gromacs@gromacs.org .
32  * 
33  * And Hey:
34  * Giving Russians Opium May Alter Current Situation
35  */
36 static char *SRCID_dialogs_c = "$Id$";
37 #include "smalloc.h"
38 #include "sysstuff.h"
39 #include "macros.h"
40 #include "string2.h"
41 #include "x11.h"
42 #include "xdlghi.h"
43 #include "xmb.h"
44 #include "dialogs.h"
45 #include "names.h"
46 #include "nmol.h"
47 #include "manager.h"
48
49 #define MBFLAGS /* MB_APPLMODAL | */ MB_DONTSHOW
50
51 void write_gmx(t_x11 *x11,t_gmx *gmx,int mess)
52 {
53   XEvent letter;
54
55   letter.type=ClientMessage;
56   letter.xclient.display=x11->disp;
57   letter.xclient.window=gmx->wd->self;
58   letter.xclient.message_type=0;
59   letter.xclient.format=32;
60   letter.xclient.data.l[0]=mess;
61   letter.xclient.data.l[1]=Button1;
62   XSendEvent(x11->disp,letter.xclient.window,True,0,&letter);
63 }
64
65 static void shell_comm(char *title,char *script,int nsleep)
66 {
67   FILE *tfil;
68   char command[STRLEN];
69   char *tmp;
70
71   tmp=tmpnam(NULL);
72   if ((tfil=fopen(tmp,"w"))==NULL) {
73     perror(tmp);
74     exit(1);
75   }
76   fprintf(tfil,"%s\n",script);
77   fprintf(tfil,"sleep %d\n",nsleep);
78   fclose(tfil);
79
80   sprintf(command,"xterm -title %s -e sh %s",title,tmp);
81 #ifdef DEBUG
82   fprintf(stderr,"command: %s\n",command);
83 #endif
84   system(command);
85 #ifdef DEBUG
86   unlink(tmp)
87 #endif
88 }
89
90 void show_mb(t_gmx *gmx,int mb)
91 {
92   if (mb >=0 && mb < emNR) {
93     gmx->which_mb=mb;
94     ShowDlg(gmx->mboxes[mb]);
95   }
96 }
97
98 static void hide_mb(t_gmx *gmx)
99 {
100   if (gmx->which_mb > 0 && gmx->which_mb < emNR)
101     HideDlg(gmx->mboxes[gmx->which_mb]);
102 }
103
104 static void MBCallback(t_x11 *x11,int dlg_mess,int item_id,
105                        char *set,void *data)
106 {
107   t_gmx *gmx;
108
109   gmx=(t_gmx *)data;
110   if (dlg_mess==DLG_EXIT) 
111     hide_mb(gmx);
112 }
113
114 static t_dlg *about_mb(t_x11 *x11,t_gmx *gmx)
115 {
116   static char *lines[]={
117     "         G R O M A C S",
118     " Machine for Simulating Chemistry",
119     "       Copyright (c) 1992-2000",
120     "  Dept. of Biophysical Chemistry",
121     "    University of Groningen"
122     };
123   
124   return MessageBox(x11,gmx->wd->self,gmx->wd->text,
125                     asize(lines),lines,MB_OK | MB_ICONGMX | MBFLAGS,
126                     MBCallback,gmx);
127 }
128
129 static void QuitCB(t_x11 *x11,int dlg_mess,int item_id,
130                    char *set,void *data)
131 {
132   t_gmx  *gmx;
133   gmx=(t_gmx *)data;
134
135   if (dlg_mess==DLG_EXIT) {
136     hide_mb(gmx);
137     if (strcasecmp("yes",set)==0) 
138       write_gmx(x11,gmx,IDTERM);
139   }
140 }
141
142 static t_dlg *quit_mb(t_x11 *x11,t_gmx *gmx)
143 {
144   static char *lines[]={
145     " Do you really want to Quit ?"
146     };
147
148   return MessageBox(x11,gmx->wd->self,gmx->wd->text,
149                     asize(lines),lines,
150                     MB_YESNO | MB_ICONSTOP | MBFLAGS,
151                     QuitCB,gmx);
152 }
153
154 static t_dlg *help_mb(t_x11 *x11,t_gmx *gmx)
155 {
156   static char *lines[]={
157     " Help will soon be added"
158     };
159   
160   return MessageBox(x11,gmx->wd->self,gmx->wd->text,
161                     asize(lines),lines,
162                     MB_OK | MB_ICONINFORMATION | MBFLAGS,
163                     MBCallback,gmx);
164 }
165
166 static t_dlg *ni_mb(t_x11 *x11,t_gmx *gmx)
167 {
168   static char *lines[]={
169     " This feature has not been",
170     " implemented yet."
171     };
172   
173   return MessageBox(x11,gmx->wd->self,gmx->wd->text,
174                     asize(lines),lines,
175                     MB_OK | MB_ICONEXCLAMATION | MBFLAGS,
176                     MBCallback,gmx);
177 }
178
179 enum { eExE, eExGrom, eExPdb, eExConf, eExNR };
180
181 static void ExportCB(t_x11 *x11,int dlg_mess,int item_id,
182                      char *set,void *data)
183 {
184   bool   bOk;
185   t_gmx  *gmx;
186   t_dlg  *dlg;
187
188   gmx=(t_gmx *)data;
189   dlg=gmx->dlgs[edExport];
190   switch (dlg_mess) {
191   case DLG_SET:
192     switch (item_id) {
193     case eExGrom:
194       gmx->ExpMode=eExpGromos;
195       break;
196     case eExPdb:
197       gmx->ExpMode=eExpPDB;
198       break;
199     default:
200       break;
201     }
202 #ifdef DEBUG
203     fprintf(stderr,"exportcb: item_id=%d\n",item_id);
204 #endif
205     break;
206   case DLG_EXIT:
207     if ((bOk=(strcasecmp("ok",set))==0)) 
208       strcpy(gmx->confout,EditText(dlg,eExConf));
209     HideDlg(dlg);
210     if (bOk)
211       write_gmx(x11,gmx,IDDOEXPORT);
212     break;
213   }
214 }
215
216 enum { eg0, egTOPOL, egCONFIN, egPARAM, eg1, eg1PROC, eg32PROC };
217
218 static void Extract(t_dlg *dlg,int ID,char *buf)
219 {
220   char *et;
221   
222   et=EditText(dlg,ID);
223   if (et)
224     strcpy(buf,et);
225 }
226
227 enum bond_set { ebShowH=11, ebDPlus, ebRMPBC, ebCue, ebSkip, ebWait };
228
229 static void BondsCB(t_x11 *x11,int dlg_mess,int item_id,
230                     char *set,void *data)
231 {
232   static int ebond=-1;
233   static int ebox=-1;
234   bool   bOk,bBond=FALSE;
235   int    nskip,nwait;
236   t_gmx  *gmx;
237
238   gmx=(t_gmx *)data;
239   if (ebond==-1) {
240     ebond = gmx->man->molw->bond_type;
241     ebox  = gmx->man->molw->boxtype;
242   }
243   switch (dlg_mess) {
244   case DLG_SET:
245     if (item_id <= eBNR) {
246       ebond=item_id-1;
247       bBond=FALSE;
248     }
249     else if (item_id <= eBNR+esbNR+1) {
250       ebox = item_id-eBNR-2;
251       bBond=TRUE;
252     }
253     else {
254
255 #define DO_NOT(b) (b) = (!(b))
256
257       switch (item_id) {
258       case ebShowH:
259         toggle_hydrogen(x11,gmx->man->molw);
260         break;
261       case ebDPlus:
262         DO_NOT(gmx->man->bPlus);
263 #ifdef DEBUG
264         fprintf(stderr,"gmx->man->bPlus=%s\n",bool_names[gmx->man->bPlus]);
265 #endif
266         break;
267         /*case ebSBox:
268         set_box_type(x11,gmx->man->molw,ebond);
269         break;*/
270       case ebRMPBC:
271         toggle_pbc(gmx->man);
272         break;
273       case ebCue:
274         DO_NOT(gmx->man->bSort);
275 #ifdef DEBUG
276         fprintf(stderr,"gmx->man->bSort=%s\n",bool_names[gmx->man->bSort]);
277 #endif
278         break;
279       case ebSkip:
280         sscanf(set,"%d",&nskip);
281 #ifdef DEBUG
282         fprintf(stderr,"nskip: %d frames\n",nskip);
283 #endif
284         if (nskip >= 0)
285           gmx->man->nSkip=nskip;
286         break;
287       case ebWait:
288         sscanf(set,"%d",&nwait);
289 #ifdef DEBUG
290         fprintf(stderr,"wait: %d ms\n",nwait);
291 #endif
292         if (nwait >= 0)
293           gmx->man->nWait=nwait;
294       default:
295 #ifdef DEBUG
296         fprintf(stderr,"item_id: %d, set: %s\n",item_id,set);
297 #endif
298         break;
299       }
300     }
301     break;
302   case DLG_EXIT:
303     bOk=(strcasecmp("ok",set)==0);
304     HideDlg(gmx->dlgs[edBonds]);
305     if (bOk) {
306       if (bBond) {
307         switch (ebond) {
308         case eBThin:
309           write_gmx(x11,gmx,IDTHIN);
310           break;
311         case eBFat:
312           write_gmx(x11,gmx,IDFAT);
313           break;
314         case eBVeryFat:
315           write_gmx(x11,gmx,IDVERYFAT);
316           break;
317         case eBSpheres:
318           write_gmx(x11,gmx,IDBALLS);
319           break;
320         default:
321           fatal_error(0,"Invalid bond type %d at %s, %d",
322                       ebond,__FILE__,__LINE__);
323         }
324       }
325       else {
326         switch(ebox) {
327         case esbNone:
328           write_gmx(x11,gmx,IDNOBOX);
329           break;
330         case esbRect:
331           write_gmx(x11,gmx,IDRECTBOX);
332           break;
333         case esbTri:
334           write_gmx(x11,gmx,IDTRIBOX);
335           break;
336         case esbTrunc:
337           write_gmx(x11,gmx,IDTOBOX);
338           break;
339         default:
340           fatal_error(0,"Invalid box type %d at %s, %d",
341                       ebox,__FILE__,__LINE__);
342         }
343       }
344     }
345     break;
346   }
347 }
348
349 enum { esFUNCT=1, esBSHOW, esINFIL, esINDEXFIL, esLSQ, esSHOW, esPLOTFIL };
350
351 static bool in_set(int i,int n,int set[])
352 {
353   int j;
354   for(j=0; (j<n); j++)
355     if (set[j]==i)
356       return TRUE;
357   return FALSE;
358 }
359
360 typedef t_dlg *t_mmb(t_x11 *x11,t_gmx *gmx);
361
362 typedef struct {
363   eDialogs    ed;
364   char        *dlgfile;
365   DlgCallback *cb;
366 } t_dlginit;
367
368 typedef struct {
369   eMBoxes     ed;
370   t_mmb       *mmb;
371   DlgCallback *cb;
372 } t_mbinit;
373
374 void init_dlgs(t_x11 *x11,t_gmx *gmx)
375 {
376   static t_dlginit di[] = {
377     { edExport,   "export.dlg",   ExportCB },
378     { edBonds,    "bonds.dlg",    BondsCB  }
379   };
380   static t_mbinit mi[emNR] = {
381     { emQuit,           quit_mb,        QuitCB     },
382     { emHelp,           help_mb,        MBCallback },
383     { emAbout,          about_mb,       MBCallback },
384     { emNotImplemented, ni_mb,          MBCallback }
385   };
386   int i;
387
388   snew(gmx->dlgs,edNR);
389   for(i=0; (i<asize(di)); i++)
390     gmx->dlgs[i]=ReadDlg(x11,gmx->wd->self,di[i].dlgfile,
391                          x11->fg,x11->bg,di[i].dlgfile, 
392                          0,0,TRUE,FALSE,di[i].cb,gmx);
393
394   gmx->dlgs[edFilter]=select_filter(x11,gmx);
395   
396   snew(gmx->mboxes,emNR);
397   for(i=0; (i<emNR); i++)
398     gmx->mboxes[i]=mi[i].mmb(x11,gmx);
399 }
400
401 void done_dlgs(t_gmx *gmx)
402 {
403   int i;
404
405   for(i=0; (i<edNR); i++)
406     FreeDlg(gmx->dlgs[i]);
407   for(i=0; (i<emNR); i++)
408     FreeDlg(gmx->mboxes[i]);
409 }
410
411 void edit_file(char *fn)
412 {
413   if (fork()==0) {
414     char script[256];
415
416     sprintf(script,"vi  %s",fn);
417     shell_comm(fn,script,0);
418     exit(0);
419   }
420 }