Merge release-5-0 into master
[alexxy/gromacs.git] / src / programs / view / fgrid.cpp
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-2013, The GROMACS development team.
6  * Copyright (c) 2013,2014, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 #include <ctype.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45
46 #include "fgrid.h"
47
48 #include "gromacs/utility/cstringutil.h"
49 #include "gromacs/utility/futil.h"
50 #include "gromacs/utility/smalloc.h"
51
52 static const char *type[] = {
53     "button", "radiobuttons", "groupbox", "checkbox",
54     "pixmap", "statictext",   "edittext", "defbutton"
55 };
56
57 void ReadDlgError(const char *infile, eDLGERR err, const char *s,
58                   const char *file, int line)
59 {
60     fprintf(stderr, "Error: ");
61     switch (err)
62     {
63         case eNOVALS:
64             fprintf(stderr, "Not enough values for %s", s);
65             break;
66         case eGRIDEXP:
67             fprintf(stderr, "'grid' expected instead of %s", s);
68             break;
69         case eACCOEXP:
70             fprintf(stderr, "'{' expected instead of %s", s);
71             break;
72         case eACCCEXP:
73             fprintf(stderr, "'}' expected instead of %s", s);
74             break;
75         case eGRPEXP:
76             fprintf(stderr, "'group' expected instead of %s", s);
77             break;
78         case eITEMEXP:
79             fprintf(stderr, "item expected instead of %s", s);
80             break;
81         case eSAMEPOINT:
82             fprintf(stderr, "grid point for %s already in use", s);
83             break;
84         case eTOOWIDE:
85             fprintf(stderr, "grid too wide for %s", s);
86             break;
87         case eTOOHIGH:
88             fprintf(stderr, "grid too high for %s", s);
89             break;
90         case eQUOTE:
91             fprintf(stderr, "quote expected instead of %s", s);
92             break;
93         default:
94             fprintf(stderr, "????");
95             break;
96     }
97     fprintf(stderr, " in file %s\n", infile);
98     fprintf(stderr, "source file: %s, line: %d\n", file, line);
99     exit(1);
100 }
101
102 #define ReadDlgErr(in, er, es) ReadDlgError(in, er, es, __FILE__, __LINE__)
103
104 static void ReadAccOpen(const char *infile, FILE *in)
105 {
106     char buf[STRLEN];
107
108     fscanf(in, "%4s", buf);
109     if (strcmp(buf, "{") != 0)
110     {
111         ReadDlgErr(infile, eACCOEXP, buf);
112     }
113 }
114
115 static void ReadAccClose(const char *infile, FILE *in)
116 {
117     char buf[STRLEN];
118
119     fscanf(in, "%4s", buf);
120     if (strcmp(buf, "}") != 0)
121     {
122         ReadDlgErr(infile, eACCCEXP, buf);
123     }
124 }
125
126 void ReadQuoteString(const char *infile, FILE *in, char *buf)
127 {
128     char c[2];
129     int  i = 0;
130
131     /* Read until first quote */
132     while ((c[0] = fgetc(in)) != '"')
133     {
134         if (!isspace(c[0]))
135         {
136             c[1] = '\0';
137             ReadDlgErr(infile, eQUOTE, c);
138         }
139     }
140     /* Read until second quote */
141     while ((c[0] = fgetc(in)) != '"')
142     {
143         buf[i++] = c[0];
144     }
145     buf[i] = '\0';
146 }
147
148 static void ReadQuoteStringOrAccClose(FILE *in, char *buf)
149 {
150     char c;
151     int  i = 0;
152
153     /* Read until first quote */
154     do
155     {
156         c = fgetc(in);
157         if (c == '}')
158         {
159             buf[0] = c;
160             buf[1] = '\0';
161             return;
162         }
163     }
164     while (c != '"');
165
166     /* Read until second quote */
167     while ((c = fgetc(in)) != '"')
168     {
169         buf[i++] = c;
170     }
171     buf[i] = '\0';
172 }
173
174 static bool bNotAccClose(const char *buf)
175 {
176     return (strcmp(buf, "}") != 0);
177 }
178
179 static t_fitem *NewFItem(void)
180 {
181     t_fitem *fitem;
182
183     snew(fitem, 1);
184     fitem->nname = 0;
185     fitem->name  = NULL;
186     fitem->set   = NULL;
187     fitem->get   = NULL;
188     fitem->def   = NULL;
189     fitem->help  = NULL;
190
191     return fitem;
192 }
193
194 static t_fsimple *NewFSimple(void)
195 {
196     t_fsimple *fsimple;
197
198     snew(fsimple, 1);
199
200     return fsimple;
201 }
202
203 static void AddFItemName(t_fitem *fitem, char *name)
204 {
205     srenew(fitem->name, ++fitem->nname);
206     fitem->name[fitem->nname-1] = strdup(name);
207 }
208
209 static t_fgroup *NewFGroup(void)
210 {
211     t_fgroup *fgroup;
212
213     snew(fgroup, 1);
214     fgroup->name   = NULL;
215     fgroup->nfitem = 0;
216     fgroup->fitem  = NULL;
217
218     return fgroup;
219 }
220
221 static void AddFGroupFItem(t_fgroup *fgroup, t_fitem *fitem)
222 {
223     srenew(fgroup->fitem, ++fgroup->nfitem);
224     fgroup->fitem[fgroup->nfitem-1] = fitem;
225 }
226
227 static t_fgroup *AddFGridFGroup(t_fgrid *fgrid)
228 {
229     srenew(fgrid->fgroup, ++fgrid->nfgroup);
230     fgrid->fgroup[fgrid->nfgroup-1] = NewFGroup();
231     return fgrid->fgroup[fgrid->nfgroup-1];
232 }
233
234 static t_fsimple *AddFGridFSimple(t_fgrid *fgrid)
235 {
236     srenew(fgrid->fsimple, ++fgrid->nfsimple);
237     fgrid->fsimple[fgrid->nfsimple-1] = NewFSimple();
238     return fgrid->fsimple[fgrid->nfsimple-1];
239 }
240
241 static t_fgrid *NewFGrid(void)
242 {
243     t_fgrid *fgrid;
244
245     snew(fgrid, 1);
246     fgrid->w        = 0;
247     fgrid->h        = 0;
248     fgrid->nfgroup  = 0;
249     fgrid->fgroup   = NULL;
250     fgrid->nfsimple = 0;
251     fgrid->fsimple  = NULL;
252
253     return fgrid;
254 }
255
256 static void DoneFItem(t_fitem *fitem)
257 {
258     int i;
259
260     for (i = 0; (i < fitem->nname); i++)
261     {
262         sfree(fitem->name[i]);
263     }
264     sfree(fitem->name);
265     sfree(fitem->set);
266     sfree(fitem->get);
267     sfree(fitem->def);
268     sfree(fitem->help);
269 }
270
271 static void DoneFGroup(t_fgroup *fgroup)
272 {
273     int i;
274
275     sfree(fgroup->name);
276     for (i = 0; (i < fgroup->nfitem); i++)
277     {
278         DoneFItem(fgroup->fitem[i]);
279     }
280     sfree(fgroup->fitem);
281 }
282
283 static void DoneFSimple(t_fsimple *fsimple)
284 {
285     DoneFItem(fsimple->fitem);
286     sfree(fsimple->fitem);
287 }
288
289 void DoneFGrid(t_fgrid *fgrid)
290 {
291     int i;
292
293     for (i = 0; (i < fgrid->nfgroup); i++)
294     {
295         DoneFGroup(fgrid->fgroup[i]);
296     }
297     sfree(fgrid->fgroup);
298     for (i = 0; (i < fgrid->nfsimple); i++)
299     {
300         DoneFSimple(fgrid->fsimple[i]);
301     }
302     sfree(fgrid->fsimple);
303 }
304
305 static t_fitem *ScanFItem(const char *infile, FILE *in, char *buf)
306 {
307     char     set[STRLEN], get[STRLEN], help[STRLEN], def[STRLEN];
308     int      edlg;
309     t_fitem *fitem;
310
311     fitem = NewFItem();
312
313     for (edlg = 0; (edlg < edlgNR+1); edlg++)
314     {
315         if (strcmp(buf, type[edlg]) == 0)
316         {
317             break;
318         }
319     }
320     if (edlg == edlgNR)
321     {
322         /* Special case */
323         edlg        = edlgBN;
324         fitem->bDef = true;
325     }
326     if (edlg == edlgNR+1)
327     {
328         ReadDlgErr(infile, eITEMEXP, buf);
329     }
330
331     fitem->edlg = (edlgitem)edlg;
332     switch (edlg)
333     {
334         case edlgBN:
335         case edlgCB:
336         case edlgET:
337             ReadQuoteString(infile, in, buf);
338             AddFItemName(fitem, buf);
339             break;
340         case edlgST:
341         case edlgRB:
342             ReadAccOpen(infile, in);
343             ReadQuoteStringOrAccClose(in, buf);
344             while (bNotAccClose(buf))
345             {
346                 AddFItemName(fitem, buf);
347                 ReadQuoteStringOrAccClose(in, buf);
348             }
349             break;
350         case edlgPM:
351         case edlgGB:
352             ReadDlgErr(infile, eITEMEXP, type[edlg]);
353             break;
354         default:
355             break;
356     }
357     ReadQuoteString(infile, in, set);
358     ReadQuoteString(infile, in, get);
359     ReadQuoteString(infile, in, def);
360     ReadQuoteString(infile, in, help);
361     fitem->set  = strdup(set);
362     fitem->get  = strdup(get);
363     fitem->def  = strdup(def);
364     fitem->help = strdup(help);
365
366     return fitem;
367 }
368
369 t_fgrid *FGridFromFile(const char *infile)
370 {
371     FILE      *in;
372     char       buf[STRLEN];
373
374     t_fgrid   *fgrid;
375     t_fgroup  *fgroup;
376     t_fsimple *fsimple;
377     int        gridx, gridy;
378
379     in = libopen(infile);
380     fscanf(in, "%6s", buf);
381     if (strcmp(buf, "grid") != 0)
382     {
383         ReadDlgErr(infile, eGRIDEXP, buf);
384     }
385     fgrid = NewFGrid();
386     if ((fscanf(in, "%5d%5d", &gridx, &gridy)) != 2)
387     {
388         ReadDlgErr(infile, eNOVALS, "grid w,h");
389     }
390     fgrid->w = gridx;
391     fgrid->h = gridy;
392     ReadAccOpen(infile, in);
393     fscanf(in, "%15s", buf);
394     while (bNotAccClose(buf))
395     {
396         if (strcmp(buf, "group") == 0)
397         {
398             fgroup = AddFGridFGroup(fgrid);
399             ReadQuoteString(infile, in, buf);
400             fgroup->name = strdup(buf);
401             if ((fscanf(in, "%5d%5d%5d%5d", &fgroup->x, &fgroup->y, &fgroup->w, &fgroup->h)) != 4)
402             {
403                 ReadDlgErr(infile, eNOVALS, "group x,y,w,h");
404             }
405             if (fgroup->x+fgroup->w > gridx)
406             {
407                 ReadDlgErr(infile, eTOOWIDE, buf);
408             }
409             if (fgroup->y+fgroup->h > gridy)
410             {
411                 ReadDlgErr(infile, eTOOHIGH, buf);
412             }
413             ReadAccOpen(infile, in);
414             fscanf(in, "%15s", buf);
415             while (bNotAccClose(buf))
416             {
417                 AddFGroupFItem(fgroup, ScanFItem(infile, in, buf));
418                 fscanf(in, "%15s", buf);
419             }
420         }
421         else if (strcmp(buf, "simple") == 0)
422         {
423             fsimple = AddFGridFSimple(fgrid);
424             if ((fscanf(in, "%5d%5d%5d%5d", &fsimple->x, &fsimple->y, &fsimple->w, &fsimple->h)) != 4)
425             {
426                 ReadDlgErr(infile, eNOVALS, "simple x,y,w,h");
427             }
428             if (fsimple->x+fsimple->w > gridx)
429             {
430                 ReadDlgErr(infile, eTOOWIDE, "simple");
431             }
432             if (fsimple->y+fsimple->h > gridy)
433             {
434                 ReadDlgErr(infile, eTOOHIGH, "simple");
435             }
436             ReadAccOpen(infile, in);
437             fscanf(in, "%15s", buf);
438             fsimple->fitem = ScanFItem(infile, in, buf);
439             ReadAccClose(infile, in);
440         }
441         fscanf(in, "%15s", buf);
442     }
443     gmx_ffclose(in);
444
445     return fgrid;
446 }
447
448 static void DumpFItem(t_fitem *fitem)
449 {
450     int i;
451
452     printf("  type: %s, set: '%s', get: '%s', def: '%s', help: '%s'\n  {",
453            type[fitem->edlg], fitem->set, fitem->get, fitem->def, fitem->help);
454     for (i = 0; (i < fitem->nname); i++)
455     {
456         printf("  '%s'", fitem->name[i]);
457     }
458     printf("  }\n");
459 }
460
461 static void DumpFSimple(t_fsimple *fsimple)
462 {
463     printf("Simple %dx%d at %d,%d\n", fsimple->w, fsimple->h, fsimple->x, fsimple->y);
464     DumpFItem(fsimple->fitem);
465 }
466
467 static void DumpFGroup(t_fgroup *fgroup)
468 {
469     int i;
470
471     printf("Group %dx%d at %d,%d\n", fgroup->w, fgroup->h, fgroup->x, fgroup->y);
472     for (i = 0; (i < fgroup->nfitem); i++)
473     {
474         DumpFItem(fgroup->fitem[i]);
475     }
476 }
477
478 void DumpFGrid(t_fgrid *fgrid)
479 {
480     int i;
481
482     printf("Grid %dx%d\n", fgrid->w, fgrid->h);
483     for (i = 0; (i < fgrid->nfgroup); i++)
484     {
485         DumpFGroup(fgrid->fgroup[i]);
486     }
487     for (i = 0; (i < fgrid->nfsimple); i++)
488     {
489         DumpFSimple(fgrid->fsimple[i]);
490     }
491 }