programs/view: move from C to C++ headers
[gromacs.git] / src / programs / view / filter.cpp
blob8a6f00ac41682d6e3b6308caa357ecee86008982
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2013, The GROMACS development team.
6 * Copyright (c) 2013,2014,2015, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
37 #include "gmxpre.h"
39 #include <cstdio>
40 #include <cstring>
42 #include <algorithm>
44 #include "gromacs/legacyheaders/macros.h"
45 #include "gromacs/topology/index.h"
46 #include "gromacs/utility/cstringutil.h"
47 #include "gromacs/utility/dir_separator.h"
48 #include "gromacs/utility/fatalerror.h"
49 #include "gromacs/utility/futil.h"
50 #include "gromacs/utility/smalloc.h"
52 #include "dialogs.h"
53 #include "xdlghi.h"
55 t_filter *init_filter(t_atoms *atoms, const char *fn, int natom_trx)
57 t_filter *f;
58 int g, i;
60 snew(f, 1);
61 if (fn != NULL)
63 f->grps = init_index(fn, &f->grpnames);
65 else
67 snew(f->grps, 1);
68 snew(f->grps->index, 1);
69 analyse(atoms, f->grps, &f->grpnames, false, false);
71 snew(f->bDisable, f->grps->nr);
72 for (g = 0; g < f->grps->nr; g++)
74 for (i = f->grps->index[g]; i < f->grps->index[g+1] && !f->bDisable[g]; i++)
76 f->bDisable[g] = (f->grps->a[i] >= natom_trx);
80 snew(f->bShow, f->grps->nr);
82 return f;
85 static void FilterCB(t_x11 *x11, int dlg_mess, int /*item_id*/,
86 char *set, void *data)
88 int nset;
89 t_filter *f;
90 t_gmx *gmx;
91 t_dlg *dlg;
93 gmx = (t_gmx *)data;
94 dlg = gmx->dlgs[edFilter];
95 f = gmx->filter;
97 #ifdef DEBUG
98 std::printf("item_id: %d, set: %s\n", item_id, set);
99 #endif
100 switch (dlg_mess)
102 case DLG_SET:
103 if (set)
105 if (sscanf(set, "%10d", &nset) == 1)
107 f->bShow[nset] = !f->bShow[nset];
110 break;
111 case DLG_EXIT:
112 HideDlg(dlg);
113 write_gmx(x11, gmx, IDDOFILTER);
114 break;
118 t_dlg *select_filter(t_x11 *x11, t_gmx *gmx)
120 static const char *title = "Group";
121 static const char *dummy = "\"FALSE\"";
122 static const char *ok = "\"Ok\"";
123 FILE *tmp;
124 t_dlg *dlg;
125 char tmpfile[STRLEN];
126 int i, j, k, len, tlen, ht, ncol, nrow, x0;
128 len = std::strlen(title);
129 for (i = 0; (i < (int)gmx->filter->grps->nr); i++)
131 len = std::max(len, static_cast<int>(std::strlen(gmx->filter->grpnames[i])));
133 len += 2;
135 ncol = 1+(gmx->filter->grps->nr / 15);
136 nrow = gmx->filter->grps->nr/ncol;
137 if (nrow*ncol < gmx->filter->grps->nr)
139 nrow++;
141 if (ncol > 1)
143 ht = 1+(nrow+1)*2+3;
145 else
147 ht = 1+(gmx->filter->grps->nr+1)*2+3;
149 std::strcpy(tmpfile, "filterXXXXXX");
150 gmx_tmpnam(tmpfile);
151 #ifdef DEBUG
152 std::fprintf(stderr, "file: %s\n", tmpfile);
153 #endif
154 if ((tmp = std::fopen(tmpfile, "w")) == NULL)
156 std::sprintf(tmpfile, "%ctmp%cfilterXXXXXX", DIR_SEPARATOR, DIR_SEPARATOR);
157 gmx_tmpnam(tmpfile);
158 if ((tmp = std::fopen(tmpfile, "w")) == NULL)
160 gmx_fatal(FARGS, "Can not open tmp file %s", tmpfile);
163 tlen = 1+ncol*(1+len);
164 std::fprintf(tmp, "grid %d %d {\n\n", tlen, ht);
166 for (k = j = 0, x0 = 1; (j < ncol); j++, x0 += len+1)
168 std::fprintf(tmp, "group \"%s-%d\" %d 1 %d %d {\n", title, j+1, x0, len, ht-5);
169 for (i = 0; (i < nrow) && (k < gmx->filter->grps->nr); i++, k++)
171 if (!gmx->filter->bDisable[k])
173 std::fprintf(tmp, "checkbox \"%s\" \"%d\" %s %s %s\n",
174 gmx->filter->grpnames[k], k, dummy, dummy, dummy);
176 else
178 std::fprintf(tmp, "statictext { \" %s\" } \"%d\" %s %s %s\n",
179 gmx->filter->grpnames[k], k, dummy, dummy, dummy);
182 std::fprintf(tmp, "}\n\n");
184 std::fprintf(tmp, "simple 1 %d %d 2 {\n", ht-3, tlen-2);
185 std::fprintf(tmp, "defbutton %s %s %s %s %s\n", ok, ok, dummy, dummy, dummy);
186 std::fprintf(tmp, "}\n\n}\n");
187 std::fclose(tmp);
189 dlg = ReadDlg(x11, gmx->wd->self, title, tmpfile,
190 0, 0, true, false, FilterCB, gmx);
192 std::remove(tmpfile);
194 return dlg;