e9458330423cc7f92016538d93a71d8ab7e617d6
[alexxy/gromacs.git] / src / ngmx / xstat.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 2.0
11  * 
12  * Copyright (c) 1991-1999
13  * BIOSON Research Institute, Dept. of Biophysical Chemistry
14  * University of Groningen, The Netherlands
15  * 
16  * Please refer to:
17  * GROMACS: A message-passing parallel molecular dynamics implementation
18  * H.J.C. Berendsen, D. van der Spoel and R. van Drunen
19  * Comp. Phys. Comm. 91, 43-56 (1995)
20  * 
21  * Also check out our WWW page:
22  * http://md.chem.rug.nl/~gmx
23  * or e-mail to:
24  * gromacs@chem.rug.nl
25  * 
26  * And Hey:
27  * Great Red Oystrich Makes All Chemists Sane
28  */
29 static char *SRCID_xstat_c = "$Id$";
30
31 #include "sysstuff.h"
32 #include "smalloc.h"
33 #include "x11.h"
34 #include "string2.h"
35 #include "macros.h"
36 #include "fgrid.h"
37 #include "futil.h"
38 #include "xdlg.h"
39 #include "xdlghi.h"
40
41 typedef struct {
42   int     nopt,nAppl;
43   char  **name;
44   char  **description;
45   char  **dlgfile;
46   t_dlg  *dlg;
47   t_dlg  *appl;
48 } t_data;
49
50 static void ApplCallback(t_x11 *x11,int dlg_mess,int item_id,
51                          char *set,void *dta)
52 {
53   t_data    *data;
54   t_dlg     *dlg;
55   t_dlgitem *item;
56   int       i;
57   char      doit[1024];
58
59   data=(t_data *)dta;
60   dlg=data->appl;
61
62   fprintf(stderr,"item_id: %d (%s)\n",item_id,set);
63   if (strcasecmp(set,"OK") == 0) {
64     /* Doit */
65     sprintf(doit,
66             "xterm -geometry +100+100 -n %s"
67             " -title \"GROMACS: %s\" -e nice %s ",
68             data->name[data->nAppl],
69             data->name[data->nAppl],
70             data->name[data->nAppl]);
71     for(i=0; (i<dlg->nitem); i++) {
72       item=dlg->dlgitem[i];
73       switch (item->type) {
74       case edlgRB:
75         strcat(doit,item->set);
76         strcat(doit," ");
77         break;
78       case edlgCB:
79         if (item->u.checkbox.bChecked)
80           strcat(doit,item->set);
81         strcat(doit," ");
82         break;
83       case edlgET:
84         if (strlen(item->u.edittext.buf) > 0) {
85           strcat(doit,item->set);
86           strcat(doit," ");
87           strcat(doit,item->u.edittext.buf);
88           strcat(doit," ");
89         }
90         break;
91       default:
92         fprintf(stderr,"Type: %d\n",item->type);
93       }
94     }
95     strcat(doit," &");
96     fprintf(stderr,"Going to exec: '%s'\n",doit);
97     system(doit);
98     HideDlg(data->appl);
99   }
100   else if (strcasecmp(set,"Cancel") == 0) {
101     data->nAppl = -1;
102     HideDlg(data->appl);
103   }
104
105
106 static void Callback(t_x11 *x11,int dlg_mess,int item_id,
107                      char *set,void *dta)
108 {
109   t_data *data=(t_data *)dta;
110
111   if (item_id == data->nopt)  {
112     fprintf(stderr,"Doei...\n");
113     exit(0);
114   }
115   else {
116     fprintf(stderr,"%d: %s\n",item_id,data->description[item_id]);
117     if (data->nAppl != -1)
118       HideDlg(data->appl);
119     data->nAppl=item_id;
120     data->appl=ReadDlg(x11,0,data->name[item_id],
121                        BLACK,LIGHTGREY,data->dlgfile[item_id],
122                        50,50,FALSE,FALSE,ApplCallback,data);
123     ShowDlg(data->appl);
124   }
125 }
126
127 static void read_opts(t_data *data)
128 {
129   FILE *in;
130   char fn[STRLEN],buf[STRLEN];
131   int  i,n;
132
133   sprintf(fn,"xstat.dat");
134   in=libopen(fn);
135   fscanf(in,"%d",&n);
136   data->nopt=n;
137   snew(data->name,data->nopt);
138   snew(data->description,data->nopt);
139   snew(data->dlgfile,data->nopt);
140
141   for(i=0; (i<data->nopt); i++) {
142     ReadQuoteString(fn,in,buf);
143     data->name[i] = strdup(buf);
144     ReadQuoteString(fn,in,buf);
145     data->description[i] = strdup(buf);
146     ReadQuoteString(fn,in,buf);
147     data->dlgfile[i] = strdup(buf);
148   }
149   fclose(in);
150 }
151
152 static void add_opts(t_x11 *x11,t_data *data)
153 {
154   t_dlgitem *but;
155   int        i,y0,w;
156
157   y0=OFFS_Y;
158   for(i=0; (i<data->nopt); i++) {
159     but=CreateButton(x11,data->description[i],FALSE,
160                      (t_id)i,(t_id)0,
161                      OFFS_X,y0,0,0,1);
162     AddDlgItem(data->dlg,but);
163     y0+=but->win.height+OFFS_Y;
164   }
165   but=CreateButton(x11,"Quit",TRUE,(t_id)data->nopt,(t_id)0,
166                    OFFS_X,y0,0,0,1);
167   AddDlgItem(data->dlg,but);
168   y0+=but->win.height+OFFS_Y;
169   
170   w=0;
171   for(i=0; (i<=data->nopt); i++)
172     w=max(w,QueryDlgItemW(data->dlg,i));
173   w+=2*OFFS_X;
174   for(i=0; (i<=data->nopt); i++)
175     SetDlgItemSize(data->dlg,i,w,0);
176   SetDlgSize(data->dlg,w+2*OFFS_X,y0,TRUE);
177 }
178
179 void main(int argc,char *argv[])
180 {
181   t_x11  *x11;
182   t_data data;
183
184   /* Initiate X and data */
185   if ((x11=GetX11(&argc,argv))==NULL) {
186     fprintf(stderr,"Can't open DISPLAY\n");
187     exit(1);
188   }
189   read_opts(&data);
190   data.dlg=CreateDlg(x11,0,argv[0],0,0,0,0,0,BLACK,LIGHTGREY,Callback,&data);
191   add_opts(x11,&data);
192   data.nAppl=-1;
193
194   ShowDlg(data.dlg);
195   x11->MainLoop(x11);
196   HideDlg(data.dlg);
197 }