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