c6245720ff7bfa461938bda7859c107a6f133fb1
[alexxy/gromacs.git] / src / ngmx / dialogs.c
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 "smalloc.h"
40 #include "sysstuff.h"
41 #include "macros.h"
42 #include "string2.h"
43 #include "x11.h"
44 #include "xdlghi.h"
45 #include "xmb.h"
46 #include "dialogs.h"
47 #include "names.h"
48 #include "nmol.h"
49 #include "manager.h"
50 #include "futil.h"
51 #include "gmx_fatal.h"
52
53 #define MBFLAGS /* MB_APPLMODAL | */ MB_DONTSHOW
54
55 void write_gmx(t_x11 *x11,t_gmx *gmx,int mess)
56 {
57   XEvent letter;
58
59   letter.type=ClientMessage;
60   letter.xclient.display=x11->disp;
61   letter.xclient.window=gmx->wd->self;
62   letter.xclient.message_type=0;
63   letter.xclient.format=32;
64   letter.xclient.data.l[0]=mess;
65   letter.xclient.data.l[1]=Button1;
66   XSendEvent(x11->disp,letter.xclient.window,True,0,&letter);
67 }
68
69 static void shell_comm(const char *title,const char *script,int nsleep)
70 {
71   FILE *tfil;
72   char command[STRLEN];
73   char tmp[32];
74
75   strcpy(tmp,"dialogXXXXXX");
76   gmx_tmpnam(tmp);
77   
78   if ((tfil = fopen(tmp,"w")) == NULL) {
79     sprintf(tmp,"%ctmp%cdialogXXXXXX",DIR_SEPARATOR,DIR_SEPARATOR);
80     gmx_tmpnam(tmp);
81   }
82   else
83   {
84       fclose(tfil);
85   }
86   if ((tfil = fopen(tmp,"w")) == NULL) 
87     gmx_fatal(FARGS,"Can not open tmp file %s",tmp);
88   
89   fprintf(tfil,"%s\n",script);
90   fprintf(tfil,"sleep %d\n",nsleep);
91   fclose(tfil);
92
93   sprintf(command,"xterm -title %s -e sh %s",title,tmp);
94 #ifdef DEBUG
95   fprintf(stderr,"command: %s\n",command);
96 #endif
97
98 #ifdef GMX_NO_SYSTEM
99   printf("Warning-- No calls to system(3) supported on this platform.");
100   printf("Warning-- Skipping execution of 'system(\"%s\")'.", buf);
101 #else
102   if(0 != system(command))
103   {
104       gmx_fatal(FARGS,"Failed to execute command: %s",command);
105   }
106 #endif
107
108 #ifdef DEBUG
109   unlink(tmp)
110 #endif
111 }
112
113 void show_mb(t_gmx *gmx,int mb)
114 {
115   if (mb >=0 && mb < emNR) {
116     gmx->which_mb=mb;
117     ShowDlg(gmx->mboxes[mb]);
118   }
119 }
120
121 static void hide_mb(t_gmx *gmx)
122 {
123   if (gmx->which_mb >= 0 && gmx->which_mb < emNR) {
124     HideDlg(gmx->mboxes[gmx->which_mb]);
125     gmx->which_mb = -1;
126   }
127 }
128
129 static void MBCallback(t_x11 *x11,int dlg_mess,int item_id,
130                        char *set,void *data)
131 {
132   t_gmx *gmx;
133
134   gmx=(t_gmx *)data;
135   if (dlg_mess==DLG_EXIT) 
136     hide_mb(gmx);
137 }
138
139 static t_dlg *about_mb(t_x11 *x11,t_gmx *gmx)
140 {
141   char *lines[]={
142     "         G R O M A C S",
143     " Machine for Simulating Chemistry",
144     "       Copyright (c) 1992-2000",
145     "  Dept. of Biophysical Chemistry",
146     "    University of Groningen"
147     };
148   
149   return MessageBox(x11,gmx->wd->self,gmx->wd->text,
150                     asize(lines),lines,MB_OK | MB_ICONGMX | MBFLAGS,
151                     MBCallback,gmx);
152 }
153
154 static void QuitCB(t_x11 *x11,int dlg_mess,int item_id,
155                    char *set,void *data)
156 {
157   t_gmx  *gmx;
158   gmx=(t_gmx *)data;
159
160   hide_mb(gmx);
161   if (dlg_mess==DLG_EXIT) {
162     if (gmx_strcasecmp("yes",set)==0) 
163       write_gmx(x11,gmx,IDTERM);
164   }
165 }
166
167 static t_dlg *quit_mb(t_x11 *x11,t_gmx *gmx)
168 {
169   char *lines[]={
170     " Do you really want to Quit ?"
171     };
172
173   return MessageBox(x11,gmx->wd->self,gmx->wd->text,
174                     asize(lines),lines,
175                     MB_YESNO | MB_ICONSTOP | MBFLAGS,
176                     QuitCB,gmx);
177 }
178
179 static t_dlg *help_mb(t_x11 *x11,t_gmx *gmx)
180 {
181   char *lines[]={
182     " Help will soon be added"
183     };
184   
185   return MessageBox(x11,gmx->wd->self,gmx->wd->text,
186                     asize(lines),lines,
187                     MB_OK | MB_ICONINFORMATION | MBFLAGS,
188                     MBCallback,gmx);
189 }
190
191 static t_dlg *ni_mb(t_x11 *x11,t_gmx *gmx)
192 {
193   char *lines[]={
194     " This feature has not been",
195     " implemented yet."
196     };
197   
198   return MessageBox(x11,gmx->wd->self,gmx->wd->text,
199                     asize(lines),lines,
200                     MB_OK | MB_ICONEXCLAMATION | MBFLAGS,
201                     MBCallback,gmx);
202 }
203
204 enum { eExE, eExGrom, eExPdb, eExConf, eExNR };
205
206 static void ExportCB(t_x11 *x11,int dlg_mess,int item_id,
207                      char *set,void *data)
208 {
209   gmx_bool   bOk;
210   t_gmx  *gmx;
211   t_dlg  *dlg;
212
213   gmx=(t_gmx *)data;
214   dlg=gmx->dlgs[edExport];
215   switch (dlg_mess) {
216   case DLG_SET:
217     switch (item_id) {
218     case eExGrom:
219       gmx->ExpMode=eExpGromos;
220       break;
221     case eExPdb:
222       gmx->ExpMode=eExpPDB;
223       break;
224     default:
225       break;
226     }
227 #ifdef DEBUG
228     fprintf(stderr,"exportcb: item_id=%d\n",item_id);
229 #endif
230     break;
231   case DLG_EXIT:
232     if ((bOk=gmx_strcasecmp("ok",set))==0)
233       strcpy(gmx->confout,EditText(dlg,eExConf));
234     HideDlg(dlg);
235     if (bOk)
236       write_gmx(x11,gmx,IDDOEXPORT);
237     break;
238   }
239 }
240
241 enum { eg0, egTOPOL, egCONFIN, egPARAM, eg1, eg1PROC, eg32PROC };
242
243 static void Extract(t_dlg *dlg,int ID,char *buf)
244 {
245   char *et;
246   
247   et=EditText(dlg,ID);
248   if (et)
249     strcpy(buf,et);
250 }
251
252 enum bond_set { ebShowH=11, ebDPlus, ebRMPBC, ebCue, ebSkip, ebWait };
253
254 static void BondsCB(t_x11 *x11,int dlg_mess,int item_id,
255                     char *set,void *data)
256 {
257   static int ebond=-1;
258   static int ebox=-1;
259   gmx_bool   bOk,bBond=FALSE;
260   int    nskip,nwait;
261   t_gmx  *gmx;
262
263   gmx=(t_gmx *)data;
264   if (ebond==-1) {
265     ebond = gmx->man->molw->bond_type;
266     ebox  = gmx->man->molw->boxtype;
267   }
268   switch (dlg_mess) {
269   case DLG_SET:
270     if (item_id <= eBNR) {
271       ebond=item_id-1;
272       bBond=FALSE;
273     }
274     else if (item_id <= eBNR+esbNR+1) {
275       ebox = item_id-eBNR-2;
276       bBond=TRUE;
277     }
278     else {
279
280 #define DO_NOT(b) (b) = (!(b))
281
282       switch (item_id) {
283       case ebShowH:
284         toggle_hydrogen(x11,gmx->man->molw);
285         break;
286       case ebDPlus:
287         DO_NOT(gmx->man->bPlus);
288 #ifdef DEBUG
289         fprintf(stderr,"gmx->man->bPlus=%s\n",gmx_bool_names[gmx->man->bPlus]);
290 #endif
291         break;
292         /*case ebSBox:
293         set_box_type(x11,gmx->man->molw,ebond);
294         break;*/
295       case ebRMPBC:
296         toggle_pbc(gmx->man);
297         break;
298       case ebCue:
299         DO_NOT(gmx->man->bSort);
300 #ifdef DEBUG
301         fprintf(stderr,"gmx->man->bSort=%s\n",gmx_bool_names[gmx->man->bSort]);
302 #endif
303         break;
304       case ebSkip:
305         sscanf(set,"%d",&nskip);
306 #ifdef DEBUG
307         fprintf(stderr,"nskip: %d frames\n",nskip);
308 #endif
309         if (nskip >= 0)
310           gmx->man->nSkip=nskip;
311         break;
312       case ebWait:
313         sscanf(set,"%d",&nwait);
314 #ifdef DEBUG
315         fprintf(stderr,"wait: %d ms\n",nwait);
316 #endif
317         if (nwait >= 0)
318           gmx->man->nWait=nwait;
319       default:
320 #ifdef DEBUG
321         fprintf(stderr,"item_id: %d, set: %s\n",item_id,set);
322 #endif
323         break;
324       }
325     }
326     break;
327   case DLG_EXIT:
328     bOk=(gmx_strcasecmp("ok",set)==0);
329     HideDlg(gmx->dlgs[edBonds]);
330     if (bOk) {
331       if (bBond) {
332         switch (ebond) {
333         case eBThin:
334           write_gmx(x11,gmx,IDTHIN);
335           break;
336         case eBFat:
337           write_gmx(x11,gmx,IDFAT);
338           break;
339         case eBVeryFat:
340           write_gmx(x11,gmx,IDVERYFAT);
341           break;
342         case eBSpheres:
343           write_gmx(x11,gmx,IDBALLS);
344           break;
345         default:
346           gmx_fatal(FARGS,"Invalid bond type %d at %s, %d",
347                       ebond,__FILE__,__LINE__);
348         }
349       }
350       else {
351         switch(ebox) {
352         case esbNone:
353           write_gmx(x11,gmx,IDNOBOX);
354           break;
355         case esbRect:
356           write_gmx(x11,gmx,IDRECTBOX);
357           break;
358         case esbTri:
359           write_gmx(x11,gmx,IDTRIBOX);
360           break;
361         case esbTrunc:
362           write_gmx(x11,gmx,IDTOBOX);
363           break;
364         default:
365           gmx_fatal(FARGS,"Invalid box type %d at %s, %d",
366                       ebox,__FILE__,__LINE__);
367         }
368       }
369     }
370     break;
371   }
372 }
373
374 enum { esFUNCT=1, esBSHOW, esINFIL, esINDEXFIL, esLSQ, esSHOW, esPLOTFIL };
375
376 static gmx_bool in_set(int i,int n,int set[])
377 {
378   int j;
379   for(j=0; (j<n); j++)
380     if (set[j]==i)
381       return TRUE;
382   return FALSE;
383 }
384
385 typedef t_dlg *t_mmb(t_x11 *x11,t_gmx *gmx);
386
387 typedef struct {
388   eDialogs    ed;
389   const char  *dlgfile;
390   DlgCallback *cb;
391 } t_dlginit;
392
393 typedef struct {
394   eMBoxes     ed;
395   t_mmb       *mmb;
396   DlgCallback *cb;
397 } t_mbinit;
398
399 void init_dlgs(t_x11 *x11,t_gmx *gmx)
400 {
401   static t_dlginit di[] = {
402     { edExport,   "export.dlg",   ExportCB },
403     { edBonds,    "bonds.dlg",    BondsCB  }
404   };
405   static t_mbinit mi[emNR] = {
406     { emQuit,           quit_mb,        QuitCB     },
407     { emHelp,           help_mb,        MBCallback },
408     { emAbout,          about_mb,       MBCallback },
409     { emNotImplemented, ni_mb,          MBCallback }
410   };
411   int i;
412
413   snew(gmx->dlgs,edNR);
414   for(i=0; (i<asize(di)); i++)
415     gmx->dlgs[i]=ReadDlg(x11,gmx->wd->self,di[i].dlgfile,
416                          x11->fg,x11->bg,di[i].dlgfile, 
417                          0,0,TRUE,FALSE,di[i].cb,gmx);
418
419   gmx->dlgs[edFilter]=select_filter(x11,gmx);
420   
421   snew(gmx->mboxes,emNR);
422   for(i=0; (i<emNR); i++)
423     gmx->mboxes[i]=mi[i].mmb(x11,gmx);
424   gmx->which_mb = -1;
425 }
426
427 void done_dlgs(t_gmx *gmx)
428 {
429   int i;
430
431   for(i=0; (i<edNR); i++)
432     FreeDlg(gmx->dlgs[i]);
433   for(i=0; (i<emNR); i++)
434     FreeDlg(gmx->mboxes[i]);
435 }
436
437 void edit_file(const char *fn)
438 {
439   if (fork()==0) {
440     char script[256];
441
442     sprintf(script,"vi  %s",fn);
443     shell_comm(fn,script,0);
444     exit(0);
445   }
446 }