1e0d44c88b90ccfc6b9d1b1399d42c735faace1d
[alexxy/gromacs.git] / src / ngmx / filter.c
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-2004, The GROMACS development team,
6  * check out http://www.gromacs.org for more information.
7  * Copyright (c) 2012, by the GROMACS development team, led by
8  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9  * others, as listed in the AUTHORS file in the top-level source
10  * directory and at http://www.gromacs.org.
11  *
12  * GROMACS is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation; either version 2.1
15  * of the License, or (at your option) any later version.
16  *
17  * GROMACS is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with GROMACS; if not, see
24  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
26  *
27  * If you want to redistribute modifications to GROMACS, please
28  * consider that scientific software is very special. Version
29  * control is crucial - bugs must be traceable. We will be happy to
30  * consider code for inclusion in the official distribution, but
31  * derived work must not be called official GROMACS. Details are found
32  * in the README & COPYING files - if they are missing, get the
33  * official version at http://www.gromacs.org.
34  *
35  * To help us fund GROMACS development, we humbly ask that you cite
36  * the research papers on the package. Check out http://www.gromacs.org.
37  */
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42 #include <string.h>
43 #include "sysstuff.h"
44 #include "futil.h"
45 #include "smalloc.h"
46 #include "macros.h"
47 #include "index.h"
48 #include "xdlghi.h"
49 #include "dialogs.h"
50 #include "index.h"
51 #include "gmx_fatal.h"
52
53 t_filter *init_filter(t_atoms *atoms, const char *fn, int natom_trx)
54 {
55   t_filter *f;
56   int      g,i;
57
58   snew(f,1);
59   if (fn != NULL)
60     f->grps=init_index(fn,&f->grpnames);
61   else {
62     snew(f->grps,1);
63     snew(f->grps->index,1);
64     analyse(atoms,f->grps,&f->grpnames,FALSE,FALSE);
65   }
66   snew(f->bDisable,f->grps->nr);
67   for(g=0; g<f->grps->nr; g++)
68     for(i=f->grps->index[g]; i<f->grps->index[g+1] && !f->bDisable[g]; i++)
69       f->bDisable[g] = (f->grps->a[i] >= natom_trx);
70   
71   snew(f->bShow,f->grps->nr);
72
73   return f;
74 }
75
76 static void FilterCB(t_x11 *x11,int dlg_mess,int item_id,
77                      char *set,void *data)
78 {
79   int      nset;
80   t_filter *f;
81   t_gmx    *gmx;
82   t_dlg    *dlg;
83
84   gmx=(t_gmx *)data;
85   dlg=gmx->dlgs[edFilter];
86   f=gmx->filter;
87
88 #ifdef DEBUG
89   printf("item_id: %d, set: %s\n",item_id,set);
90 #endif
91   switch (dlg_mess) {
92   case DLG_SET:
93     if (set) 
94       if (sscanf(set,"%d",&nset)==1)
95         f->bShow[nset]=!f->bShow[nset];
96     break;
97   case DLG_EXIT:
98     HideDlg(dlg);
99     write_gmx(x11,gmx,IDDOFILTER);
100     break;
101   }
102 }
103
104 t_dlg *select_filter(t_x11 *x11,t_gmx *gmx)
105 {
106   static const char *title="Group";
107   static const char *dummy="\"FALSE\"";
108   static const char *ok="\"Ok\"";
109   FILE   *tmp;
110   t_dlg  *dlg;
111   char   tmpfile[STRLEN];
112   int    i,j,k,len,tlen,ht,ncol,nrow,x0;
113
114   len=strlen(title);
115   for(i=0; (i<(int)gmx->filter->grps->nr); i++)
116     len=max(len,(int)strlen(gmx->filter->grpnames[i]));
117   len+=2;
118
119   ncol=1+(gmx->filter->grps->nr / 15);
120   nrow=gmx->filter->grps->nr/ncol;
121   if (nrow*ncol < gmx->filter->grps->nr)
122     nrow++;
123   if (ncol > 1) {
124     ht=1+(nrow+1)*2+3;
125   }
126   else {
127     ht=1+(gmx->filter->grps->nr+1)*2+3;
128   }
129   strcpy(tmpfile,"filterXXXXXX");
130   gmx_tmpnam(tmpfile);
131 #ifdef DEBUG
132   fprintf(stderr,"file: %s\n",tmpfile);
133 #endif
134   if ((tmp = fopen(tmpfile,"w")) == NULL) {
135     sprintf(tmpfile,"%ctmp%cfilterXXXXXX",DIR_SEPARATOR,DIR_SEPARATOR);
136     gmx_tmpnam(tmpfile);
137     if ((tmp = fopen(tmpfile,"w")) == NULL) 
138       gmx_fatal(FARGS,"Can not open tmp file %s",tmpfile);
139   }
140   tlen=1+ncol*(1+len);
141   fprintf(tmp,"grid %d %d {\n\n",tlen,ht);
142
143   for(k=j=0,x0=1; (j<ncol); j++,x0+=len+1) {
144     fprintf(tmp,"group \"%s-%d\" %d 1 %d %d {\n",title,j+1,x0,len,ht-5);
145     for(i=0; (i<nrow) && (k<gmx->filter->grps->nr); i++,k++)
146       if (!gmx->filter->bDisable[k])
147         fprintf(tmp,"checkbox \"%s\" \"%d\" %s %s %s\n",
148                 gmx->filter->grpnames[k],k,dummy,dummy,dummy);
149       else
150         fprintf(tmp,"statictext { \"  %s\" } \"%d\" %s %s %s\n",
151                 gmx->filter->grpnames[k],k,dummy,dummy,dummy);
152     fprintf(tmp,"}\n\n");
153   }
154   fprintf(tmp,"simple 1 %d %d 2 {\n",ht-3,tlen-2);
155   fprintf(tmp,"defbutton %s %s %s %s %s\n",ok,ok,dummy,dummy,dummy);
156   fprintf(tmp,"}\n\n}\n");
157   fclose(tmp);
158
159   dlg=ReadDlg(x11,gmx->wd->self,title,x11->fg,x11->bg,tmpfile,
160               0,0,TRUE,FALSE,FilterCB,gmx);
161   
162   remove(tmpfile);
163
164   return dlg;
165 }