Tagged files with gromacs 3.0 header and added some license info
[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 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  * Good gRace! Old Maple Actually Chews Slate
35  */
36 static char *SRCID_xstat_c = "$Id$";
37 #include "sysstuff.h"
38 #include "smalloc.h"
39 #include "x11.h"
40 #include "string2.h"
41 #include "macros.h"
42 #include "fgrid.h"
43 #include "futil.h"
44 #include "xdlg.h"
45 #include "xdlghi.h"
46
47 typedef struct {
48   int     nopt,nAppl;
49   char  **name;
50   char  **description;
51   char  **dlgfile;
52   t_dlg  *dlg;
53   t_dlg  *appl;
54 } t_data;
55
56 static void ApplCallback(t_x11 *x11,int dlg_mess,int item_id,
57                          char *set,void *dta)
58 {
59   t_data    *data;
60   t_dlg     *dlg;
61   t_dlgitem *item;
62   int       i;
63   char      doit[1024];
64
65   data=(t_data *)dta;
66   dlg=data->appl;
67
68   fprintf(stderr,"item_id: %d (%s)\n",item_id,set);
69   if (strcasecmp(set,"OK") == 0) {
70     /* Doit */
71     sprintf(doit,
72             "xterm -geometry +100+100 -n %s"
73             " -title \"GROMACS: %s\" -e nice %s ",
74             data->name[data->nAppl],
75             data->name[data->nAppl],
76             data->name[data->nAppl]);
77     for(i=0; (i<dlg->nitem); i++) {
78       item=dlg->dlgitem[i];
79       switch (item->type) {
80       case edlgRB:
81         strcat(doit,item->set);
82         strcat(doit," ");
83         break;
84       case edlgCB:
85         if (item->u.checkbox.bChecked)
86           strcat(doit,item->set);
87         strcat(doit," ");
88         break;
89       case edlgET:
90         if (strlen(item->u.edittext.buf) > 0) {
91           strcat(doit,item->set);
92           strcat(doit," ");
93           strcat(doit,item->u.edittext.buf);
94           strcat(doit," ");
95         }
96         break;
97       default:
98         fprintf(stderr,"Type: %d\n",item->type);
99       }
100     }
101     strcat(doit," &");
102     fprintf(stderr,"Going to exec: '%s'\n",doit);
103     system(doit);
104     HideDlg(data->appl);
105   }
106   else if (strcasecmp(set,"Cancel") == 0) {
107     data->nAppl = -1;
108     HideDlg(data->appl);
109   }
110
111
112 static void Callback(t_x11 *x11,int dlg_mess,int item_id,
113                      char *set,void *dta)
114 {
115   t_data *data=(t_data *)dta;
116
117   if (item_id == data->nopt)  {
118     fprintf(stderr,"Doei...\n");
119     exit(0);
120   }
121   else {
122     fprintf(stderr,"%d: %s\n",item_id,data->description[item_id]);
123     if (data->nAppl != -1)
124       HideDlg(data->appl);
125     data->nAppl=item_id;
126     data->appl=ReadDlg(x11,0,data->name[item_id],
127                        BLACK,LIGHTGREY,data->dlgfile[item_id],
128                        50,50,FALSE,FALSE,ApplCallback,data);
129     ShowDlg(data->appl);
130   }
131 }
132
133 static void read_opts(t_data *data)
134 {
135   FILE *in;
136   char fn[STRLEN],buf[STRLEN];
137   int  i,n;
138
139   sprintf(fn,"xstat.dat");
140   in=libopen(fn);
141   fscanf(in,"%d",&n);
142   data->nopt=n;
143   snew(data->name,data->nopt);
144   snew(data->description,data->nopt);
145   snew(data->dlgfile,data->nopt);
146
147   for(i=0; (i<data->nopt); i++) {
148     ReadQuoteString(fn,in,buf);
149     data->name[i] = strdup(buf);
150     ReadQuoteString(fn,in,buf);
151     data->description[i] = strdup(buf);
152     ReadQuoteString(fn,in,buf);
153     data->dlgfile[i] = strdup(buf);
154   }
155   fclose(in);
156 }
157
158 static void add_opts(t_x11 *x11,t_data *data)
159 {
160   t_dlgitem *but;
161   int        i,y0,w;
162
163   y0=OFFS_Y;
164   for(i=0; (i<data->nopt); i++) {
165     but=CreateButton(x11,data->description[i],FALSE,
166                      (t_id)i,(t_id)0,
167                      OFFS_X,y0,0,0,1);
168     AddDlgItem(data->dlg,but);
169     y0+=but->win.height+OFFS_Y;
170   }
171   but=CreateButton(x11,"Quit",TRUE,(t_id)data->nopt,(t_id)0,
172                    OFFS_X,y0,0,0,1);
173   AddDlgItem(data->dlg,but);
174   y0+=but->win.height+OFFS_Y;
175   
176   w=0;
177   for(i=0; (i<=data->nopt); i++)
178     w=max(w,QueryDlgItemW(data->dlg,i));
179   w+=2*OFFS_X;
180   for(i=0; (i<=data->nopt); i++)
181     SetDlgItemSize(data->dlg,i,w,0);
182   SetDlgSize(data->dlg,w+2*OFFS_X,y0,TRUE);
183 }
184
185 void main(int argc,char *argv[])
186 {
187   t_x11  *x11;
188   t_data data;
189
190   /* Initiate X and data */
191   if ((x11=GetX11(&argc,argv))==NULL) {
192     fprintf(stderr,"Can't open DISPLAY\n");
193     exit(1);
194   }
195   read_opts(&data);
196   data.dlg=CreateDlg(x11,0,argv[0],0,0,0,0,0,BLACK,LIGHTGREY,Callback,&data);
197   add_opts(x11,&data);
198   data.nAppl=-1;
199
200   ShowDlg(data.dlg);
201   x11->MainLoop(x11);
202   HideDlg(data.dlg);
203 }