Tagged files with gromacs 3.0 header and added some license info
[alexxy/gromacs.git] / src / ngmx / filter.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_filter_c = "$Id$";
37 #include <string.h>
38 #include "sysstuff.h"
39 #include "futil.h"
40 #include "smalloc.h"
41 #include "macros.h"
42 #include "rdgroup.h"
43 #include "xdlghi.h"
44 #include "dialogs.h"
45 #include "index.h"
46
47 t_filter *init_filter(t_atoms *atoms, char *fn, int natom_trx)
48 {
49   t_filter *f;
50   int      g,i;
51
52   snew(f,1);
53   if (fn != NULL)
54     f->grps=init_index(fn,&f->grpnames);
55   else {
56     snew(f->grps,1);
57     snew(f->grps->index,1);
58     analyse(atoms,f->grps,&f->grpnames,FALSE,FALSE);
59   }
60   snew(f->bDisable,f->grps->nr);
61   for(g=0; g<f->grps->nr; g++)
62     for(i=f->grps->index[g]; i<f->grps->index[g+1] && !f->bDisable[g]; i++)
63       f->bDisable[g] = (f->grps->a[i] >= natom_trx);
64   
65   snew(f->bShow,f->grps->nr);
66
67   return f;
68 }
69
70 static void FilterCB(t_x11 *x11,int dlg_mess,int item_id,
71                      char *set,void *data)
72 {
73   int      nset;
74   t_filter *f;
75   t_gmx    *gmx;
76   t_dlg    *dlg;
77
78   gmx=(t_gmx *)data;
79   dlg=gmx->dlgs[edFilter];
80   f=gmx->filter;
81
82 #ifdef DEBUG
83   printf("item_id: %d, set: %s\n",item_id,set);
84 #endif
85   switch (dlg_mess) {
86   case DLG_SET:
87     if (set) 
88       if (sscanf(set,"%d",&nset)==1)
89         f->bShow[nset]=!f->bShow[nset];
90     break;
91   case DLG_EXIT:
92     HideDlg(dlg);
93     write_gmx(x11,gmx,IDDOFILTER);
94     break;
95   }
96 }
97
98 t_dlg *select_filter(t_x11 *x11,t_gmx *gmx)
99 {
100   static char *title="Group";
101   static char *dummy="\"FALSE\"";
102   static char *ok="\"Ok\"";
103   FILE   *tmp;
104   t_dlg  *dlg;
105   char   tmpfile[L_tmpnam];
106   int    i,j,k,len,tlen,ht,ncol,nrow,x0;
107
108   len=strlen(title);
109   for(i=0; (i<(int)gmx->filter->grps->nr); i++)
110     len=max(len,(int)strlen(gmx->filter->grpnames[i]));
111   len+=2;
112
113   ncol=1+(gmx->filter->grps->nr / 15);
114   nrow=gmx->filter->grps->nr/ncol;
115   if (nrow*ncol < gmx->filter->grps->nr)
116     nrow++;
117   if (ncol > 1) {
118     ht=1+(nrow+1)*2+3;
119   }
120   else {
121     ht=1+(gmx->filter->grps->nr+1)*2+3;
122   }
123   tmpnam(tmpfile);
124 #ifdef DEBUG
125   fprintf(stderr,"file: %s\n",tmpfile);
126 #endif
127   tmp=fopen(tmpfile,"w");
128   tlen=1+ncol*(1+len);
129   fprintf(tmp,"grid %d %d {\n\n",tlen,ht);
130
131   for(k=j=0,x0=1; (j<ncol); j++,x0+=len+1) {
132     fprintf(tmp,"group \"%s-%d\" %d 1 %d %d {\n",title,j+1,x0,len,ht-5);
133     for(i=0; (i<nrow) && (k<gmx->filter->grps->nr); i++,k++)
134       if (!gmx->filter->bDisable[k])
135         fprintf(tmp,"checkbox \"%s\" \"%d\" %s %s %s\n",
136                 gmx->filter->grpnames[k],k,dummy,dummy,dummy);
137       else
138         fprintf(tmp,"statictext { \"  %s\" } \"%d\" %s %s %s\n",
139                 gmx->filter->grpnames[k],k,dummy,dummy,dummy);
140     fprintf(tmp,"}\n\n");
141   }
142   fprintf(tmp,"simple 1 %d %d 2 {\n",ht-3,tlen-2);
143   fprintf(tmp,"defbutton %s %s %s %s %s\n",ok,ok,dummy,dummy,dummy);
144   fprintf(tmp,"}\n\n}\n");
145   fclose(tmp);
146
147   dlg=ReadDlg(x11,gmx->wd->self,title,x11->fg,x11->bg,tmpfile,
148               0,0,TRUE,FALSE,FilterCB,gmx);
149   
150   remove(tmpfile);
151
152   return dlg;
153 }