Merge branch release-5-1
[gromacs/AngularHB.git] / src / programs / view / dialogs.cpp
blob050394cad21d1414062371d17d23364f1cbd6a1d
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 "dialogs.h"
41 #include "config.h"
43 #include <cstdio>
44 #include <cstdlib>
45 #include <cstring>
47 #ifdef HAVE_UNISTD_H
48 #include <unistd.h> // for fork()
49 #endif
51 #include "gromacs/mdtypes/md_enums.h"
52 #include "gromacs/utility/arraysize.h"
53 #include "gromacs/utility/cstringutil.h"
54 #include "gromacs/utility/dir_separator.h"
55 #include "gromacs/utility/fatalerror.h"
56 #include "gromacs/utility/futil.h"
57 #include "gromacs/utility/smalloc.h"
59 #include "manager.h"
60 #include "nmol.h"
61 #include "x11.h"
62 #include "xdlghi.h"
63 #include "xmb.h"
65 #define MBFLAGS /* MB_APPLMODAL | */ MB_DONTSHOW
67 void write_gmx(t_x11 *x11, t_gmx *gmx, int mess)
69 XEvent letter;
71 letter.type = ClientMessage;
72 letter.xclient.display = x11->disp;
73 letter.xclient.window = gmx->wd->self;
74 letter.xclient.message_type = 0;
75 letter.xclient.format = 32;
76 letter.xclient.data.l[0] = mess;
77 letter.xclient.data.l[1] = Button1;
78 XSendEvent(x11->disp, letter.xclient.window, True, 0, &letter);
81 static void shell_comm(const char *title, const char *script, int nsleep)
83 FILE *tfil;
84 char command[STRLEN];
85 char tmp[32];
87 std::strcpy(tmp, "dialogXXXXXX");
88 tfil = gmx_fopen_temporary(tmp);
90 fprintf(tfil, "%s\n", script);
91 fprintf(tfil, "sleep %d\n", nsleep);
92 gmx_ffclose(tfil);
94 std::sprintf(command, "xterm -title %s -e sh %s", title, tmp);
95 #ifdef DEBUG
96 std::fprintf(stderr, "command: %s\n", command);
97 #endif
99 if (0 != std::system(command))
101 gmx_fatal(FARGS, "Failed to execute command: %s", command);
104 #ifdef DEBUG
105 unlink(tmp)
106 #endif
109 void show_mb(t_gmx *gmx, int mb)
111 if (mb >= 0 && mb < emNR)
113 gmx->which_mb = mb;
114 ShowDlg(gmx->mboxes[mb]);
118 static void hide_mb(t_gmx *gmx)
120 if (gmx->which_mb >= 0 && gmx->which_mb < emNR)
122 HideDlg(gmx->mboxes[gmx->which_mb]);
123 gmx->which_mb = -1;
127 static void MBCallback(t_x11 * /*x11*/, int dlg_mess, int /*item_id*/,
128 char * /*set*/, void *data)
130 t_gmx *gmx;
132 gmx = (t_gmx *)data;
133 if (dlg_mess == DLG_EXIT)
135 hide_mb(gmx);
139 static t_dlg *about_mb(t_x11 *x11, t_gmx *gmx)
141 const char *lines[] = {
142 " G R O M A C S",
143 " Machine for Simulating Chemistry",
144 " Copyright (c) 1992-2013",
145 " Berk Hess, David van der Spoel, Erik Lindahl",
146 " and many collaborators!"
149 return MessageBox(x11, gmx->wd->self, gmx->wd->text,
150 asize(lines), lines, MB_OK | MB_ICONGMX | MBFLAGS,
151 MBCallback, gmx);
154 static void QuitCB(t_x11 *x11, int dlg_mess, int /*item_id*/,
155 char *set, void *data)
157 t_gmx *gmx;
158 gmx = (t_gmx *)data;
160 hide_mb(gmx);
161 if (dlg_mess == DLG_EXIT)
163 if (gmx_strcasecmp("yes", set) == 0)
165 write_gmx(x11, gmx, IDTERM);
170 static t_dlg *quit_mb(t_x11 *x11, t_gmx *gmx)
172 const char *lines[] = {
173 " Do you really want to Quit ?"
176 return MessageBox(x11, gmx->wd->self, gmx->wd->text,
177 asize(lines), lines,
178 MB_YESNO | MB_ICONSTOP | MBFLAGS,
179 QuitCB, gmx);
182 static t_dlg *help_mb(t_x11 *x11, t_gmx *gmx)
184 const char *lines[] = {
185 " Help will soon be added"
188 return MessageBox(x11, gmx->wd->self, gmx->wd->text,
189 asize(lines), lines,
190 MB_OK | MB_ICONINFORMATION | MBFLAGS,
191 MBCallback, gmx);
194 static t_dlg *ni_mb(t_x11 *x11, t_gmx *gmx)
196 const char *lines[] = {
197 " This feature has not been",
198 " implemented yet."
201 return MessageBox(x11, gmx->wd->self, gmx->wd->text,
202 asize(lines), lines,
203 MB_OK | MB_ICONEXCLAMATION | MBFLAGS,
204 MBCallback, gmx);
207 enum {
208 eExE, eExGrom, eExPdb, eExConf, eExNR
211 static void ExportCB(t_x11 *x11, int dlg_mess, int item_id,
212 char *set, void *data)
214 bool bOk;
215 t_gmx *gmx;
216 t_dlg *dlg;
218 gmx = (t_gmx *)data;
219 dlg = gmx->dlgs[edExport];
220 switch (dlg_mess)
222 case DLG_SET:
223 switch (item_id)
225 case eExGrom:
226 gmx->ExpMode = eExpGromos;
227 break;
228 case eExPdb:
229 gmx->ExpMode = eExpPDB;
230 break;
231 default:
232 break;
234 #ifdef DEBUG
235 std::fprintf(stderr, "exportcb: item_id=%d\n", item_id);
236 #endif
237 break;
238 case DLG_EXIT:
239 if ((bOk = gmx_strcasecmp("ok", set)) == 0)
241 std::strcpy(gmx->confout, EditText(dlg, eExConf));
243 HideDlg(dlg);
244 if (bOk)
246 write_gmx(x11, gmx, IDDOEXPORT);
248 break;
252 enum {
253 eg0, egTOPOL, egCONFIN, egPARAM, eg1, eg1PROC, eg32PROC
256 static void Extract(t_dlg *dlg, int ID, char *buf)
258 char *et;
260 et = EditText(dlg, ID);
261 if (et)
263 std::strcpy(buf, et);
267 enum bond_set {
268 ebShowH = 11, ebDPlus, ebRMPBC, ebCue, ebSkip, ebWait
271 static void BondsCB(t_x11 *x11, int dlg_mess, int item_id,
272 char *set, void *data)
274 static int ebond = -1;
275 static int ebox = -1;
276 bool bOk, bBond = false;
277 int nskip, nwait;
278 t_gmx *gmx;
279 char *endptr;
281 gmx = (t_gmx *)data;
282 if (ebond == -1)
284 ebond = gmx->man->molw->bond_type;
285 ebox = gmx->man->molw->boxtype;
287 switch (dlg_mess)
289 case DLG_SET:
290 if (item_id <= eBNR)
292 ebond = item_id-1;
293 bBond = false;
295 else if (item_id <= eBNR+esbNR+1)
297 ebox = item_id-eBNR-2;
298 bBond = true;
300 else
303 #define DO_NOT(b) (b) = (!(b))
305 switch (item_id)
307 case ebShowH:
308 toggle_hydrogen(x11, gmx->man->molw);
309 break;
310 case ebDPlus:
311 DO_NOT(gmx->man->bPlus);
312 #ifdef DEBUG
313 std::fprintf(stderr, "gmx->man->bPlus=%s\n",
314 gmx->man->bPlus ? "true" : "false");
315 #endif
316 break;
317 case ebRMPBC:
318 toggle_pbc(gmx->man);
319 break;
320 case ebCue:
321 DO_NOT(gmx->man->bSort);
322 #ifdef DEBUG
323 std::fprintf(stderr, "gmx->man->bSort=%s\n",
324 gmx->man->bSort ? "true" : "false");
325 #endif
326 break;
327 case ebSkip:
328 nskip = std::strtol(set, &endptr, 10);
329 if (endptr != set)
331 #ifdef DEBUG
332 std::fprintf(stderr, "nskip: %d frames\n", nskip);
333 #endif
334 if (nskip >= 0)
336 gmx->man->nSkip = nskip;
339 break;
340 case ebWait:
341 nwait = std::strtol(set, &endptr, 10);
342 if (endptr != set)
344 #ifdef DEBUG
345 std::fprintf(stderr, "wait: %d ms\n", nwait);
346 #endif
347 if (nwait >= 0)
349 gmx->man->nWait = nwait;
352 default:
353 #ifdef DEBUG
354 std::fprintf(stderr, "item_id: %d, set: %s\n", item_id, set);
355 #endif
356 break;
359 break;
360 case DLG_EXIT:
361 bOk = (gmx_strcasecmp("ok", set) == 0);
362 HideDlg(gmx->dlgs[edBonds]);
363 if (bOk)
365 if (bBond)
367 switch (ebond)
369 case eBThin:
370 write_gmx(x11, gmx, IDTHIN);
371 break;
372 case eBFat:
373 write_gmx(x11, gmx, IDFAT);
374 break;
375 case eBVeryFat:
376 write_gmx(x11, gmx, IDVERYFAT);
377 break;
378 case eBSpheres:
379 write_gmx(x11, gmx, IDBALLS);
380 break;
381 default:
382 gmx_fatal(FARGS, "Invalid bond type %d at %s, %d",
383 ebond, __FILE__, __LINE__);
386 else
388 switch (ebox)
390 case esbNone:
391 write_gmx(x11, gmx, IDNOBOX);
392 break;
393 case esbRect:
394 write_gmx(x11, gmx, IDRECTBOX);
395 break;
396 case esbTri:
397 write_gmx(x11, gmx, IDTRIBOX);
398 break;
399 case esbTrunc:
400 write_gmx(x11, gmx, IDTOBOX);
401 break;
402 default:
403 gmx_fatal(FARGS, "Invalid box type %d at %s, %d",
404 ebox, __FILE__, __LINE__);
408 break;
412 enum {
413 esFUNCT = 1, esBSHOW, esINFIL, esINDEXFIL, esLSQ, esSHOW, esPLOTFIL
416 static bool in_set(int i, int n, int set[])
418 int j;
419 for (j = 0; (j < n); j++)
421 if (set[j] == i)
423 return true;
426 return false;
429 typedef t_dlg *t_mmb (t_x11 *x11, t_gmx *gmx);
431 typedef struct {
432 const char *dlgfile;
433 DlgCallback *cb;
434 } t_dlginit;
436 void init_dlgs(t_x11 *x11, t_gmx *gmx)
438 static t_dlginit di[] = {
439 { "export.dlg", ExportCB },
440 { "bonds.dlg", BondsCB }
442 static t_mmb *mi[emNR] = { quit_mb, help_mb, about_mb, ni_mb };
443 unsigned int i;
445 snew(gmx->dlgs, edNR);
446 for (i = 0; (i < asize(di)); i++)
448 gmx->dlgs[i] = ReadDlg(x11, gmx->wd->self, di[i].dlgfile,
449 di[i].dlgfile,
450 0, 0, true, false, di[i].cb, gmx);
453 gmx->dlgs[edFilter] = select_filter(x11, gmx);
455 snew(gmx->mboxes, emNR);
456 for (i = 0; (i < emNR); i++)
458 gmx->mboxes[i] = mi[i](x11, gmx);
460 gmx->which_mb = -1;
463 void done_dlgs(t_gmx *gmx)
465 int i;
467 for (i = 0; (i < edNR); i++)
469 FreeDlg(gmx->dlgs[i]);
471 for (i = 0; (i < emNR); i++)
473 FreeDlg(gmx->mboxes[i]);
477 void edit_file(const char *fn)
479 if (fork() == 0)
481 char script[256];
483 std::sprintf(script, "vi %s", fn);
484 shell_comm(fn, script, 0);
485 std::exit(0);