Tagged files with gromacs 3.0 header and added some license info
[alexxy/gromacs.git] / src / ngmx / fgrid.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  * Giving Russians Opium May Alter Current Situation
35  */
36 static char *SRCID_fgrid_c = "$Id$";
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <ctype.h>
41 #include "string2.h"
42 #include "smalloc.h"
43 #include "fgrid.h"
44 #include "futil.h"
45
46 static char *type[] = { 
47   "button", "radiobuttons", "groupbox", "checkbox",
48   "pixmap", "statictext",   "edittext", "defbutton"
49   };
50
51 void ReadDlgError(char *infile,eDLGERR err,char *s,char *file,int line)
52 {
53   fprintf(stderr,"Error: ");
54   switch(err) {
55   case eNOVALS:
56     fprintf(stderr,"Not enough values for %s",s);
57     break;
58   case eGRIDEXP:
59     fprintf(stderr,"'grid' expected instead of %s",s);
60     break;
61   case eACCOEXP:
62     fprintf(stderr,"'{' expected instead of %s",s);
63     break;
64   case eACCCEXP:
65     fprintf(stderr,"'}' expected instead of %s",s);
66     break;
67   case eGRPEXP:
68     fprintf(stderr,"'group' expected instead of %s",s);
69     break;
70   case eITEMEXP:
71     fprintf(stderr,"item expected instead of %s",s);
72     break;
73   case eSAMEPOINT:
74     fprintf(stderr,"grid point for %s already in use",s);
75     break;
76   case eTOOWIDE:
77     fprintf(stderr,"grid too wide for %s",s);
78     break;
79   case eTOOHIGH:
80     fprintf(stderr,"grid too high for %s",s);
81     break;
82   case eQUOTE:
83     fprintf(stderr,"quote expected instead of %s",s);
84     break;
85   default:
86     fprintf(stderr,"????");
87     break;
88   }
89   fprintf(stderr," in file %s\n",infile);
90   fprintf(stderr,"C-File: %s, line: %d\n",file,line);
91   exit(1);
92 }
93
94 #define ReadDlgErr(in,er,es) ReadDlgError(in,er,es,__FILE__,__LINE__)
95
96 static void GetBuf(FILE *in, char *buf)
97 {
98   fscanf(in,"%s",buf);
99 }
100
101 static void ReadAccOpen(char *infile, FILE *in)
102 {
103   char buf[STRLEN];
104   
105   GetBuf(in,buf);
106   if (strcmp(buf,"{")!=0)
107     ReadDlgErr(infile,eACCOEXP,buf);
108 }
109
110 static void ReadAccClose(char *infile, FILE *in)
111 {
112   char buf[STRLEN];
113   
114   GetBuf(in,buf);
115   if (strcmp(buf,"}")!=0)
116     ReadDlgErr(infile,eACCCEXP,buf);
117 }
118
119 void ReadQuoteString(char *infile, FILE *in, char *buf)
120 {
121   char c[2];
122   int i=0;
123   
124   /* Read until first quote */
125   while ((c[0]=fgetc(in))!='"') 
126     if (!isspace(c[0])) {
127       c[1]='\0';
128       ReadDlgErr(infile,eQUOTE,c);
129     }
130   /* Read until second quote */
131   while ((c[0]=fgetc(in))!='"')
132     buf[i++]=c[0];
133   buf[i]='\0';
134 }
135
136 static void ReadQuoteStringOrAccClose(FILE *in, char *buf)
137 {
138   char c;
139   int i=0;
140   
141   /* Read until first quote */
142   do {
143     c=fgetc(in);
144     if (c=='}') {
145       buf[0]=c;
146       buf[1]='\0';
147       return;
148     }
149   } while (c != '"');
150   
151   /* Read until second quote */
152   while ((c=fgetc(in))!='"')
153     buf[i++]=c;
154   buf[i]='\0';
155 }
156
157 static bool bNotAccClose(char *buf)
158 {
159   return (strcmp(buf,"}")!=0);
160 }
161
162 static t_fitem *NewFItem(void)
163 {
164   t_fitem *fitem;
165   
166   snew(fitem,1);
167   fitem->nname=0;
168   fitem->name=NULL;
169   fitem->set=NULL;
170   fitem->get=NULL;
171   fitem->def=NULL;
172   fitem->help=NULL;
173
174   return fitem;
175 }
176
177 static t_fsimple *NewFSimple(void)
178 {
179   t_fsimple *fsimple;
180   
181   snew(fsimple,1);
182   
183   return fsimple;
184 }
185
186 static void AddFItemName(t_fitem *fitem, char *name)
187 {
188   srenew(fitem->name,++fitem->nname);
189   fitem->name[fitem->nname-1]=strdup(name);
190 }
191
192 static t_fgroup *NewFGroup(void)
193 {
194   t_fgroup *fgroup;
195   
196   snew(fgroup,1);
197   fgroup->name=NULL;
198   fgroup->nfitem=0;
199   fgroup->fitem=NULL;
200   
201   return fgroup;
202 }
203
204 static void AddFGroupFItem(t_fgroup *fgroup, t_fitem *fitem)
205 {
206   srenew(fgroup->fitem,++fgroup->nfitem);
207   fgroup->fitem[fgroup->nfitem-1]=fitem;
208 }
209
210 static t_fgroup *AddFGridFGroup(t_fgrid *fgrid)
211 {
212   srenew(fgrid->fgroup,++fgrid->nfgroup);
213   fgrid->fgroup[fgrid->nfgroup-1]=NewFGroup();
214   return fgrid->fgroup[fgrid->nfgroup-1];
215 }
216
217 static t_fsimple *AddFGridFSimple(t_fgrid *fgrid)
218 {
219   srenew(fgrid->fsimple,++fgrid->nfsimple);
220   fgrid->fsimple[fgrid->nfsimple-1]=NewFSimple();
221   return fgrid->fsimple[fgrid->nfsimple-1];
222 }
223
224 static t_fgrid *NewFGrid(void)
225 {
226   t_fgrid *fgrid;
227   
228   snew(fgrid,1);
229   fgrid->w=0;
230   fgrid->h=0;
231   fgrid->nfgroup=0;
232   fgrid->fgroup=NULL;
233   fgrid->nfsimple=0;
234   fgrid->fsimple=NULL;
235   
236   return fgrid;
237 }
238
239 static void DoneFItem(t_fitem *fitem)
240 {
241   int i;
242   
243   for(i=0; (i<fitem->nname); i++)
244     sfree(fitem->name[i]);
245   sfree(fitem->name);
246   sfree(fitem->set);
247   sfree(fitem->get);
248   sfree(fitem->def);
249   sfree(fitem->help);
250 }
251
252 static void DoneFGroup(t_fgroup *fgroup)
253 {
254   int i;
255   
256   sfree(fgroup->name);
257   for(i=0; (i<fgroup->nfitem); i++)
258     DoneFItem(fgroup->fitem[i]);
259   sfree(fgroup->fitem);
260 }
261
262 static void DoneFSimple(t_fsimple *fsimple)
263 {
264   DoneFItem(fsimple->fitem);
265   sfree(fsimple->fitem);
266 }
267
268 void DoneFGrid(t_fgrid *fgrid)
269 {
270   int i;
271   
272   for(i=0; (i<fgrid->nfgroup); i++) 
273     DoneFGroup(fgrid->fgroup[i]);
274   sfree(fgrid->fgroup);
275   for(i=0; (i<fgrid->nfsimple); i++) 
276     DoneFSimple(fgrid->fsimple[i]);
277   sfree(fgrid->fsimple);
278 }
279
280 static t_fitem *ScanFItem(char *infile, FILE *in, char *buf)
281 {
282   char set[STRLEN],get[STRLEN],help[STRLEN],def[STRLEN];
283   edlgitem edlg;
284   t_fitem *fitem;
285   
286   fitem=NewFItem();
287   
288   for(edlg=(edlgitem)0; (edlg<edlgNR+1); edlg++)
289     if (strcmp(buf,type[edlg])==0)
290       break;
291   if (edlg==edlgNR) {
292     /* Special case */
293     edlg=edlgBN;
294     fitem->bDef=TRUE;
295   }
296   if (edlg==edlgNR+1) {
297     ReadDlgErr(infile,eITEMEXP,buf);
298   }
299   
300   fitem->edlg=edlg;
301   switch (edlg) {
302   case edlgBN:
303   case edlgCB:
304   case edlgET:
305     ReadQuoteString(infile,in,buf);
306     AddFItemName(fitem,buf);
307     break;
308   case edlgST:
309   case edlgRB:
310     ReadAccOpen(infile,in);
311     ReadQuoteStringOrAccClose(in,buf);
312     while (bNotAccClose(buf)) {
313       AddFItemName(fitem,buf);
314       ReadQuoteStringOrAccClose(in,buf);
315     }
316     break;
317   case edlgPM:
318   case edlgGB:
319     ReadDlgErr(infile,eITEMEXP,type[edlg]);
320     break;
321   default:
322     break;
323   }
324   ReadQuoteString(infile,in,set);
325   ReadQuoteString(infile,in,get);
326   ReadQuoteString(infile,in,def);
327   ReadQuoteString(infile,in,help);
328   fitem->set=strdup(set);
329   fitem->get=strdup(get);
330   fitem->def=strdup(def);
331   fitem->help=strdup(help);
332   
333   return fitem;
334 }
335
336 t_fgrid *FGridFromFile(char *infile)
337 {
338   FILE *in;
339   char buf[STRLEN];
340   char *gmxlib;
341   char newinfile[STRLEN];
342   
343   t_fgrid   *fgrid;
344   t_fgroup  *fgroup;
345   t_fsimple *fsimple;
346   int       gridx,gridy;  
347   
348   in = libopen(infile);
349   GetBuf(in,buf);
350   if (strcmp(buf,"grid")!=0)
351     ReadDlgErr(infile,eGRIDEXP,buf);
352   fgrid=NewFGrid();
353   if ((fscanf(in,"%d%d",&gridx,&gridy))!=2)
354     ReadDlgErr(infile,eNOVALS,"grid w,h");
355   fgrid->w=gridx;
356   fgrid->h=gridy;
357   ReadAccOpen(infile,in);
358   GetBuf(in,buf);
359   while (bNotAccClose(buf)) {
360     if (strcmp(buf,"group")==0) {
361       fgroup=AddFGridFGroup(fgrid);
362       ReadQuoteString(infile,in,buf);
363       fgroup->name=strdup(buf);
364       if ((fscanf(in,"%d%d%d%d",&fgroup->x,&fgroup->y,&fgroup->w,&fgroup->h))!=4)
365         ReadDlgErr(infile,eNOVALS,"group x,y,w,h");
366       if (fgroup->x+fgroup->w > gridx)
367         ReadDlgErr(infile,eTOOWIDE,buf);
368       if (fgroup->y+fgroup->h > gridy)
369         ReadDlgErr(infile,eTOOHIGH,buf);
370       ReadAccOpen(infile,in);
371       GetBuf(in,buf);
372       while (bNotAccClose(buf)) {
373         AddFGroupFItem(fgroup,ScanFItem(infile,in,buf));
374         GetBuf(in,buf);
375       }
376     }
377     else if (strcmp(buf,"simple")==0) {
378       fsimple=AddFGridFSimple(fgrid);
379       if ((fscanf(in,"%d%d%d%d",&fsimple->x,&fsimple->y,&fsimple->w,&fsimple->h))!=4)
380         ReadDlgErr(infile,eNOVALS,"simple x,y,w,h");
381       if (fsimple->x+fsimple->w > gridx)
382         ReadDlgErr(infile,eTOOWIDE,"simple");
383       if (fsimple->y+fsimple->h > gridy)
384         ReadDlgErr(infile,eTOOHIGH,"simple");
385       ReadAccOpen(infile,in);
386       GetBuf(in,buf);
387       fsimple->fitem=ScanFItem(infile,in,buf);
388       ReadAccClose(infile,in);
389     }
390     GetBuf(in,buf);
391   }
392   fclose(in);
393   
394   return fgrid;
395 }
396
397 static void DumpFItem(t_fitem *fitem)
398 {
399   int i;
400   
401   printf("  type: %s, set: '%s', get: '%s', def: '%s', help: '%s'\n  {",
402          type[fitem->edlg],fitem->set,fitem->get,fitem->def,fitem->help);
403   for(i=0; (i<fitem->nname); i++)
404     printf("  '%s'",fitem->name[i]);
405   printf("  }\n");
406 }
407
408 static void DumpFSimple(t_fsimple *fsimple)
409 {
410   printf("Simple %dx%d at %d,%d\n",fsimple->w,fsimple->h,fsimple->x,fsimple->y);
411   DumpFItem(fsimple->fitem);
412 }
413
414 static void DumpFGroup(t_fgroup *fgroup)
415 {
416   int i;
417   
418   printf("Group %dx%d at %d,%d\n",fgroup->w,fgroup->h,fgroup->x,fgroup->y);
419   for(i=0; (i<fgroup->nfitem); i++)
420     DumpFItem(fgroup->fitem[i]);
421 }
422
423 void DumpFGrid(t_fgrid *fgrid)
424 {
425   int i;
426   
427   printf("Grid %dx%d\n",fgrid->w,fgrid->h);
428   for(i=0; (i<fgrid->nfgroup); i++)
429     DumpFGroup(fgrid->fgroup[i]);
430   for(i=0; (i<fgrid->nfsimple); i++)
431     DumpFSimple(fgrid->fsimple[i]);
432 }