Sort includes outside src/gromacs
[alexxy/gromacs.git] / src / programs / view / filter.cpp
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-2013, The GROMACS development team.
6  * Copyright (c) 2013,2014, 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.
10  *
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.
15  *
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.
20  *
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.
25  *
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.
33  *
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.
36  */
37 #include "gmxpre.h"
38
39 #include <string.h>
40
41 #include <algorithm>
42
43 #include "gromacs/legacyheaders/macros.h"
44 #include "gromacs/topology/index.h"
45 #include "gromacs/utility/fatalerror.h"
46 #include "gromacs/utility/futil.h"
47 #include "gromacs/utility/smalloc.h"
48
49 #include "dialogs.h"
50 #include "xdlghi.h"
51
52 t_filter *init_filter(t_atoms *atoms, const char *fn, int natom_trx)
53 {
54     t_filter *f;
55     int       g, i;
56
57     snew(f, 1);
58     if (fn != NULL)
59     {
60         f->grps = init_index(fn, &f->grpnames);
61     }
62     else
63     {
64         snew(f->grps, 1);
65         snew(f->grps->index, 1);
66         analyse(atoms, f->grps, &f->grpnames, false, false);
67     }
68     snew(f->bDisable, f->grps->nr);
69     for (g = 0; g < f->grps->nr; g++)
70     {
71         for (i = f->grps->index[g]; i < f->grps->index[g+1] && !f->bDisable[g]; i++)
72         {
73             f->bDisable[g] = (f->grps->a[i] >= natom_trx);
74         }
75     }
76
77     snew(f->bShow, f->grps->nr);
78
79     return f;
80 }
81
82 static void FilterCB(t_x11 *x11, int dlg_mess, int /*item_id*/,
83                      char *set, void *data)
84 {
85     int       nset;
86     t_filter *f;
87     t_gmx    *gmx;
88     t_dlg    *dlg;
89
90     gmx = (t_gmx *)data;
91     dlg = gmx->dlgs[edFilter];
92     f   = gmx->filter;
93
94 #ifdef DEBUG
95     printf("item_id: %d, set: %s\n", item_id, set);
96 #endif
97     switch (dlg_mess)
98     {
99         case DLG_SET:
100             if (set)
101             {
102                 if (sscanf(set, "%10d", &nset) == 1)
103                 {
104                     f->bShow[nset] = !f->bShow[nset];
105                 }
106             }
107             break;
108         case DLG_EXIT:
109             HideDlg(dlg);
110             write_gmx(x11, gmx, IDDOFILTER);
111             break;
112     }
113 }
114
115 t_dlg *select_filter(t_x11 *x11, t_gmx *gmx)
116 {
117     static const char *title = "Group";
118     static const char *dummy = "\"FALSE\"";
119     static const char *ok    = "\"Ok\"";
120     FILE              *tmp;
121     t_dlg             *dlg;
122     char               tmpfile[STRLEN];
123     int                i, j, k, len, tlen, ht, ncol, nrow, x0;
124
125     len = strlen(title);
126     for (i = 0; (i < (int)gmx->filter->grps->nr); i++)
127     {
128         len = std::max(len, (int)strlen(gmx->filter->grpnames[i]));
129     }
130     len += 2;
131
132     ncol = 1+(gmx->filter->grps->nr / 15);
133     nrow = gmx->filter->grps->nr/ncol;
134     if (nrow*ncol < gmx->filter->grps->nr)
135     {
136         nrow++;
137     }
138     if (ncol > 1)
139     {
140         ht = 1+(nrow+1)*2+3;
141     }
142     else
143     {
144         ht = 1+(gmx->filter->grps->nr+1)*2+3;
145     }
146     strcpy(tmpfile, "filterXXXXXX");
147     gmx_tmpnam(tmpfile);
148 #ifdef DEBUG
149     fprintf(stderr, "file: %s\n", tmpfile);
150 #endif
151     if ((tmp = fopen(tmpfile, "w")) == NULL)
152     {
153         sprintf(tmpfile, "%ctmp%cfilterXXXXXX", DIR_SEPARATOR, DIR_SEPARATOR);
154         gmx_tmpnam(tmpfile);
155         if ((tmp = fopen(tmpfile, "w")) == NULL)
156         {
157             gmx_fatal(FARGS, "Can not open tmp file %s", tmpfile);
158         }
159     }
160     tlen = 1+ncol*(1+len);
161     fprintf(tmp, "grid %d %d {\n\n", tlen, ht);
162
163     for (k = j = 0, x0 = 1; (j < ncol); j++, x0 += len+1)
164     {
165         fprintf(tmp, "group \"%s-%d\" %d 1 %d %d {\n", title, j+1, x0, len, ht-5);
166         for (i = 0; (i < nrow) && (k < gmx->filter->grps->nr); i++, k++)
167         {
168             if (!gmx->filter->bDisable[k])
169             {
170                 fprintf(tmp, "checkbox \"%s\" \"%d\" %s %s %s\n",
171                         gmx->filter->grpnames[k], k, dummy, dummy, dummy);
172             }
173             else
174             {
175                 fprintf(tmp, "statictext { \"  %s\" } \"%d\" %s %s %s\n",
176                         gmx->filter->grpnames[k], k, dummy, dummy, dummy);
177             }
178         }
179         fprintf(tmp, "}\n\n");
180     }
181     fprintf(tmp, "simple 1 %d %d 2 {\n", ht-3, tlen-2);
182     fprintf(tmp, "defbutton %s %s %s %s %s\n", ok, ok, dummy, dummy, dummy);
183     fprintf(tmp, "}\n\n}\n");
184     fclose(tmp);
185
186     dlg = ReadDlg(x11, gmx->wd->self, title, tmpfile,
187                   0, 0, true, false, FilterCB, gmx);
188
189     remove(tmpfile);
190
191     return dlg;
192 }