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