Don't use POSIX fnmatch() for pattern matching.
[gromacs/qmmm-gamess-us.git] / src / ngmx / filter.c
blobabd55530d7949892cec5479896e5d120ff5c4e73
1 /*
2 *
3 * This source code is part of
4 *
5 * G R O M A C S
6 *
7 * GROningen MAchine for Chemical Simulations
8 *
9 * VERSION 3.2.0
10 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12 * Copyright (c) 2001-2004, The GROMACS development team,
13 * check out http://www.gromacs.org for more information.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * If you want to redistribute modifications, please consider that
21 * scientific software is very special. Version control is crucial -
22 * bugs must be traceable. We will be happy to consider code for
23 * inclusion in the official distribution, but derived work must not
24 * be called official GROMACS. Details are found in the README & COPYING
25 * files - if they are missing, get the official version at www.gromacs.org.
27 * To help us fund GROMACS development, we humbly ask that you cite
28 * the papers on the package - you can find them in the top README file.
30 * For more info, check our website at http://www.gromacs.org
32 * And Hey:
33 * Gyas ROwers Mature At Cryogenic Speed
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
39 #include <string.h>
40 #include "sysstuff.h"
41 #include "futil.h"
42 #include "smalloc.h"
43 #include "macros.h"
44 #include "index.h"
45 #include "xdlghi.h"
46 #include "dialogs.h"
47 #include "index.h"
49 t_filter *init_filter(t_atoms *atoms, const char *fn, int natom_trx)
51 t_filter *f;
52 int g,i;
54 snew(f,1);
55 if (fn != NULL)
56 f->grps=init_index(fn,&f->grpnames);
57 else {
58 snew(f->grps,1);
59 snew(f->grps->index,1);
60 analyse(atoms,f->grps,&f->grpnames,FALSE,FALSE);
62 snew(f->bDisable,f->grps->nr);
63 for(g=0; g<f->grps->nr; g++)
64 for(i=f->grps->index[g]; i<f->grps->index[g+1] && !f->bDisable[g]; i++)
65 f->bDisable[g] = (f->grps->a[i] >= natom_trx);
67 snew(f->bShow,f->grps->nr);
69 return f;
72 static void FilterCB(t_x11 *x11,int dlg_mess,int item_id,
73 char *set,void *data)
75 int nset;
76 t_filter *f;
77 t_gmx *gmx;
78 t_dlg *dlg;
80 gmx=(t_gmx *)data;
81 dlg=gmx->dlgs[edFilter];
82 f=gmx->filter;
84 #ifdef DEBUG
85 printf("item_id: %d, set: %s\n",item_id,set);
86 #endif
87 switch (dlg_mess) {
88 case DLG_SET:
89 if (set)
90 if (sscanf(set,"%d",&nset)==1)
91 f->bShow[nset]=!f->bShow[nset];
92 break;
93 case DLG_EXIT:
94 HideDlg(dlg);
95 write_gmx(x11,gmx,IDDOFILTER);
96 break;
100 t_dlg *select_filter(t_x11 *x11,t_gmx *gmx)
102 static const char *title="Group";
103 static const char *dummy="\"FALSE\"";
104 static const char *ok="\"Ok\"";
105 FILE *tmp;
106 t_dlg *dlg;
107 char tmpfile[STRLEN];
108 int i,j,k,len,tlen,ht,ncol,nrow,x0;
110 len=strlen(title);
111 for(i=0; (i<(int)gmx->filter->grps->nr); i++)
112 len=max(len,(int)strlen(gmx->filter->grpnames[i]));
113 len+=2;
115 ncol=1+(gmx->filter->grps->nr / 15);
116 nrow=gmx->filter->grps->nr/ncol;
117 if (nrow*ncol < gmx->filter->grps->nr)
118 nrow++;
119 if (ncol > 1) {
120 ht=1+(nrow+1)*2+3;
122 else {
123 ht=1+(gmx->filter->grps->nr+1)*2+3;
125 strcpy(tmpfile,"filterXXXXXX");
126 gmx_tmpnam(tmpfile);
127 #ifdef DEBUG
128 fprintf(stderr,"file: %s\n",tmpfile);
129 #endif
130 if ((tmp = fopen(tmpfile,"w")) == NULL) {
131 sprintf(tmpfile,"%ctmp%cfilterXXXXXX",DIR_SEPARATOR,DIR_SEPARATOR);
132 gmx_tmpnam(tmpfile);
133 if ((tmp = fopen(tmpfile,"w")) == NULL)
134 gmx_fatal(FARGS,"Can not open tmp file %s",tmpfile);
136 tlen=1+ncol*(1+len);
137 fprintf(tmp,"grid %d %d {\n\n",tlen,ht);
139 for(k=j=0,x0=1; (j<ncol); j++,x0+=len+1) {
140 fprintf(tmp,"group \"%s-%d\" %d 1 %d %d {\n",title,j+1,x0,len,ht-5);
141 for(i=0; (i<nrow) && (k<gmx->filter->grps->nr); i++,k++)
142 if (!gmx->filter->bDisable[k])
143 fprintf(tmp,"checkbox \"%s\" \"%d\" %s %s %s\n",
144 gmx->filter->grpnames[k],k,dummy,dummy,dummy);
145 else
146 fprintf(tmp,"statictext { \" %s\" } \"%d\" %s %s %s\n",
147 gmx->filter->grpnames[k],k,dummy,dummy,dummy);
148 fprintf(tmp,"}\n\n");
150 fprintf(tmp,"simple 1 %d %d 2 {\n",ht-3,tlen-2);
151 fprintf(tmp,"defbutton %s %s %s %s %s\n",ok,ok,dummy,dummy,dummy);
152 fprintf(tmp,"}\n\n}\n");
153 fclose(tmp);
155 dlg=ReadDlg(x11,gmx->wd->self,title,x11->fg,x11->bg,tmpfile,
156 0,0,TRUE,FALSE,FilterCB,gmx);
158 remove(tmpfile);
160 return dlg;