81b9dd33c376a59b36f46399059935441528773c
[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 2.0
11  * 
12  * Copyright (c) 1991-1999
13  * BIOSON Research Institute, Dept. of Biophysical Chemistry
14  * University of Groningen, The Netherlands
15  * 
16  * Please refer to:
17  * GROMACS: A message-passing parallel molecular dynamics implementation
18  * H.J.C. Berendsen, D. van der Spoel and R. van Drunen
19  * Comp. Phys. Comm. 91, 43-56 (1995)
20  * 
21  * Also check out our WWW page:
22  * http://md.chem.rug.nl/~gmx
23  * or e-mail to:
24  * gromacs@chem.rug.nl
25  * 
26  * And Hey:
27  * Great Red Oystrich Makes All Chemists Sane
28  */
29 static char *SRCID_filter_c = "$Id$";
30
31 #include <string.h>
32 #include "sysstuff.h"
33 #include "futil.h"
34 #include "smalloc.h"
35 #include "macros.h"
36 #include "rdgroup.h"
37 #include "xdlghi.h"
38 #include "dialogs.h"
39 #include "index.h"
40
41 t_filter *init_filter(t_atoms *atoms, char *fn, int natom_trx)
42 {
43   t_filter *f;
44   int      g,i;
45
46   snew(f,1);
47   if (fn != NULL)
48     f->grps=init_index(fn,&f->grpnames);
49   else {
50     snew(f->grps,1);
51     snew(f->grps->index,1);
52     analyse(atoms,f->grps,&f->grpnames,FALSE,FALSE);
53   }
54   snew(f->bDisable,f->grps->nr);
55   for(g=0; g<f->grps->nr; g++)
56     for(i=f->grps->index[g]; i<f->grps->index[g+1] && !f->bDisable[g]; i++)
57       f->bDisable[g] = (f->grps->a[i] >= natom_trx);
58   
59   snew(f->bShow,f->grps->nr);
60
61   return f;
62 }
63
64 static void FilterCB(t_x11 *x11,int dlg_mess,int item_id,
65                      char *set,void *data)
66 {
67   int      nset;
68   t_filter *f;
69   t_gmx    *gmx;
70   t_dlg    *dlg;
71
72   gmx=(t_gmx *)data;
73   dlg=gmx->dlgs[edFilter];
74   f=gmx->filter;
75
76 #ifdef DEBUG
77   printf("item_id: %d, set: %s\n",item_id,set);
78 #endif
79   switch (dlg_mess) {
80   case DLG_SET:
81     if (set) 
82       if (sscanf(set,"%d",&nset)==1)
83         f->bShow[nset]=!f->bShow[nset];
84     break;
85   case DLG_EXIT:
86     HideDlg(dlg);
87     write_gmx(x11,gmx,IDDOFILTER);
88     break;
89   }
90 }
91
92 t_dlg *select_filter(t_x11 *x11,t_gmx *gmx)
93 {
94   static char *title="Group";
95   static char *dummy="\"FALSE\"";
96   static char *ok="\"Ok\"";
97   FILE   *tmp;
98   t_dlg  *dlg;
99   char   tmpfile[L_tmpnam];
100   int    i,j,k,len,tlen,ht,ncol,nrow,x0;
101
102   len=strlen(title);
103   for(i=0; (i<(int)gmx->filter->grps->nr); i++)
104     len=max(len,(int)strlen(gmx->filter->grpnames[i]));
105   len+=2;
106
107   ncol=1+(gmx->filter->grps->nr / 15);
108   nrow=gmx->filter->grps->nr/ncol;
109   if (nrow*ncol < gmx->filter->grps->nr)
110     nrow++;
111   if (ncol > 1) {
112     ht=1+(nrow+1)*2+3;
113   }
114   else {
115     ht=1+(gmx->filter->grps->nr+1)*2+3;
116   }
117   tmpnam(tmpfile);
118 #ifdef DEBUG
119   fprintf(stderr,"file: %s\n",tmpfile);
120 #endif
121   tmp=fopen(tmpfile,"w");
122   tlen=1+ncol*(1+len);
123   fprintf(tmp,"grid %d %d {\n\n",tlen,ht);
124
125   for(k=j=0,x0=1; (j<ncol); j++,x0+=len+1) {
126     fprintf(tmp,"group \"%s-%d\" %d 1 %d %d {\n",title,j+1,x0,len,ht-5);
127     for(i=0; (i<nrow) && (k<gmx->filter->grps->nr); i++,k++)
128       if (!gmx->filter->bDisable[k])
129         fprintf(tmp,"checkbox \"%s\" \"%d\" %s %s %s\n",
130                 gmx->filter->grpnames[k],k,dummy,dummy,dummy);
131       else
132         fprintf(tmp,"statictext { \"  %s\" } \"%d\" %s %s %s\n",
133                 gmx->filter->grpnames[k],k,dummy,dummy,dummy);
134     fprintf(tmp,"}\n\n");
135   }
136   fprintf(tmp,"simple 1 %d %d 2 {\n",ht-3,tlen-2);
137   fprintf(tmp,"defbutton %s %s %s %s %s\n",ok,ok,dummy,dummy,dummy);
138   fprintf(tmp,"}\n\n}\n");
139   fclose(tmp);
140
141   dlg=ReadDlg(x11,gmx->wd->self,title,x11->fg,x11->bg,tmpfile,
142               0,0,TRUE,FALSE,FilterCB,gmx);
143   
144   remove(tmpfile);
145
146   return dlg;
147 }