Replace AI, AJ, .. and C0, C1, C2 macros with functions
[gromacs/AngularHB.git] / src / programs / view / dialogs.cpp
blob4f69d7a4a81b6cd0438b339d60813c7e8367de37
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 "config.h"
41 #include <stdlib.h>
42 #ifdef HAVE_UNISTD_H
43 #include <unistd.h> // for fork()
44 #endif
46 #include "dialogs.h"
48 #include "gromacs/legacyheaders/macros.h"
49 #include "gromacs/legacyheaders/names.h"
50 #include "gromacs/utility/cstringutil.h"
51 #include "gromacs/utility/dir_separator.h"
52 #include "gromacs/utility/fatalerror.h"
53 #include "gromacs/utility/futil.h"
54 #include "gromacs/utility/smalloc.h"
56 #include "manager.h"
57 #include "nmol.h"
58 #include "x11.h"
59 #include "xdlghi.h"
60 #include "xmb.h"
62 #define MBFLAGS /* MB_APPLMODAL | */ MB_DONTSHOW
64 void write_gmx(t_x11 *x11, t_gmx *gmx, int mess)
66 XEvent letter;
68 letter.type = ClientMessage;
69 letter.xclient.display = x11->disp;
70 letter.xclient.window = gmx->wd->self;
71 letter.xclient.message_type = 0;
72 letter.xclient.format = 32;
73 letter.xclient.data.l[0] = mess;
74 letter.xclient.data.l[1] = Button1;
75 XSendEvent(x11->disp, letter.xclient.window, True, 0, &letter);
78 static void shell_comm(const char *title, const char *script, int nsleep)
80 FILE *tfil;
81 char command[STRLEN];
82 char tmp[32];
84 strcpy(tmp, "dialogXXXXXX");
85 gmx_tmpnam(tmp);
87 if ((tfil = fopen(tmp, "w")) == NULL)
89 sprintf(tmp, "%ctmp%cdialogXXXXXX", DIR_SEPARATOR, DIR_SEPARATOR);
90 gmx_tmpnam(tmp);
92 else
94 fclose(tfil);
96 if ((tfil = fopen(tmp, "w")) == NULL)
98 gmx_fatal(FARGS, "Can not open tmp file %s", tmp);
101 fprintf(tfil, "%s\n", script);
102 fprintf(tfil, "sleep %d\n", nsleep);
103 fclose(tfil);
105 sprintf(command, "xterm -title %s -e sh %s", title, tmp);
106 #ifdef DEBUG
107 fprintf(stderr, "command: %s\n", command);
108 #endif
110 if (0 != system(command))
112 gmx_fatal(FARGS, "Failed to execute command: %s", command);
115 #ifdef DEBUG
116 unlink(tmp)
117 #endif
120 void show_mb(t_gmx *gmx, int mb)
122 if (mb >= 0 && mb < emNR)
124 gmx->which_mb = mb;
125 ShowDlg(gmx->mboxes[mb]);
129 static void hide_mb(t_gmx *gmx)
131 if (gmx->which_mb >= 0 && gmx->which_mb < emNR)
133 HideDlg(gmx->mboxes[gmx->which_mb]);
134 gmx->which_mb = -1;
138 static void MBCallback(t_x11 * /*x11*/, int dlg_mess, int /*item_id*/,
139 char * /*set*/, void *data)
141 t_gmx *gmx;
143 gmx = (t_gmx *)data;
144 if (dlg_mess == DLG_EXIT)
146 hide_mb(gmx);
150 static t_dlg *about_mb(t_x11 *x11, t_gmx *gmx)
152 const char *lines[] = {
153 " G R O M A C S",
154 " Machine for Simulating Chemistry",
155 " Copyright (c) 1992-2013",
156 " Berk Hess, David van der Spoel, Erik Lindahl",
157 " and many collaborators!"
160 return MessageBox(x11, gmx->wd->self, gmx->wd->text,
161 asize(lines), lines, MB_OK | MB_ICONGMX | MBFLAGS,
162 MBCallback, gmx);
165 static void QuitCB(t_x11 *x11, int dlg_mess, int /*item_id*/,
166 char *set, void *data)
168 t_gmx *gmx;
169 gmx = (t_gmx *)data;
171 hide_mb(gmx);
172 if (dlg_mess == DLG_EXIT)
174 if (gmx_strcasecmp("yes", set) == 0)
176 write_gmx(x11, gmx, IDTERM);
181 static t_dlg *quit_mb(t_x11 *x11, t_gmx *gmx)
183 const char *lines[] = {
184 " Do you really want to Quit ?"
187 return MessageBox(x11, gmx->wd->self, gmx->wd->text,
188 asize(lines), lines,
189 MB_YESNO | MB_ICONSTOP | MBFLAGS,
190 QuitCB, gmx);
193 static t_dlg *help_mb(t_x11 *x11, t_gmx *gmx)
195 const char *lines[] = {
196 " Help will soon be added"
199 return MessageBox(x11, gmx->wd->self, gmx->wd->text,
200 asize(lines), lines,
201 MB_OK | MB_ICONINFORMATION | MBFLAGS,
202 MBCallback, gmx);
205 static t_dlg *ni_mb(t_x11 *x11, t_gmx *gmx)
207 const char *lines[] = {
208 " This feature has not been",
209 " implemented yet."
212 return MessageBox(x11, gmx->wd->self, gmx->wd->text,
213 asize(lines), lines,
214 MB_OK | MB_ICONEXCLAMATION | MBFLAGS,
215 MBCallback, gmx);
218 enum {
219 eExE, eExGrom, eExPdb, eExConf, eExNR
222 static void ExportCB(t_x11 *x11, int dlg_mess, int item_id,
223 char *set, void *data)
225 bool bOk;
226 t_gmx *gmx;
227 t_dlg *dlg;
229 gmx = (t_gmx *)data;
230 dlg = gmx->dlgs[edExport];
231 switch (dlg_mess)
233 case DLG_SET:
234 switch (item_id)
236 case eExGrom:
237 gmx->ExpMode = eExpGromos;
238 break;
239 case eExPdb:
240 gmx->ExpMode = eExpPDB;
241 break;
242 default:
243 break;
245 #ifdef DEBUG
246 fprintf(stderr, "exportcb: item_id=%d\n", item_id);
247 #endif
248 break;
249 case DLG_EXIT:
250 if ((bOk = gmx_strcasecmp("ok", set)) == 0)
252 strcpy(gmx->confout, EditText(dlg, eExConf));
254 HideDlg(dlg);
255 if (bOk)
257 write_gmx(x11, gmx, IDDOEXPORT);
259 break;
263 enum {
264 eg0, egTOPOL, egCONFIN, egPARAM, eg1, eg1PROC, eg32PROC
267 static void Extract(t_dlg *dlg, int ID, char *buf)
269 char *et;
271 et = EditText(dlg, ID);
272 if (et)
274 strcpy(buf, et);
278 enum bond_set {
279 ebShowH = 11, ebDPlus, ebRMPBC, ebCue, ebSkip, ebWait
282 static void BondsCB(t_x11 *x11, int dlg_mess, int item_id,
283 char *set, void *data)
285 static int ebond = -1;
286 static int ebox = -1;
287 bool bOk, bBond = false;
288 int nskip, nwait;
289 t_gmx *gmx;
290 char *endptr;
292 gmx = (t_gmx *)data;
293 if (ebond == -1)
295 ebond = gmx->man->molw->bond_type;
296 ebox = gmx->man->molw->boxtype;
298 switch (dlg_mess)
300 case DLG_SET:
301 if (item_id <= eBNR)
303 ebond = item_id-1;
304 bBond = false;
306 else if (item_id <= eBNR+esbNR+1)
308 ebox = item_id-eBNR-2;
309 bBond = true;
311 else
314 #define DO_NOT(b) (b) = (!(b))
316 switch (item_id)
318 case ebShowH:
319 toggle_hydrogen(x11, gmx->man->molw);
320 break;
321 case ebDPlus:
322 DO_NOT(gmx->man->bPlus);
323 #ifdef DEBUG
324 fprintf(stderr, "gmx->man->bPlus=%s\n",
325 gmx->man->bPlus ? "true" : "false");
326 #endif
327 break;
328 case ebRMPBC:
329 toggle_pbc(gmx->man);
330 break;
331 case ebCue:
332 DO_NOT(gmx->man->bSort);
333 #ifdef DEBUG
334 fprintf(stderr, "gmx->man->bSort=%s\n",
335 gmx->man->bSort ? "true" : "false");
336 #endif
337 break;
338 case ebSkip:
339 nskip = strtol(set, &endptr, 10);
340 if (endptr != set)
342 #ifdef DEBUG
343 fprintf(stderr, "nskip: %d frames\n", nskip);
344 #endif
345 if (nskip >= 0)
347 gmx->man->nSkip = nskip;
350 break;
351 case ebWait:
352 nwait = strtol(set, &endptr, 10);
353 if (endptr != set)
355 #ifdef DEBUG
356 fprintf(stderr, "wait: %d ms\n", nwait);
357 #endif
358 if (nwait >= 0)
360 gmx->man->nWait = nwait;
363 default:
364 #ifdef DEBUG
365 fprintf(stderr, "item_id: %d, set: %s\n", item_id, set);
366 #endif
367 break;
370 break;
371 case DLG_EXIT:
372 bOk = (gmx_strcasecmp("ok", set) == 0);
373 HideDlg(gmx->dlgs[edBonds]);
374 if (bOk)
376 if (bBond)
378 switch (ebond)
380 case eBThin:
381 write_gmx(x11, gmx, IDTHIN);
382 break;
383 case eBFat:
384 write_gmx(x11, gmx, IDFAT);
385 break;
386 case eBVeryFat:
387 write_gmx(x11, gmx, IDVERYFAT);
388 break;
389 case eBSpheres:
390 write_gmx(x11, gmx, IDBALLS);
391 break;
392 default:
393 gmx_fatal(FARGS, "Invalid bond type %d at %s, %d",
394 ebond, __FILE__, __LINE__);
397 else
399 switch (ebox)
401 case esbNone:
402 write_gmx(x11, gmx, IDNOBOX);
403 break;
404 case esbRect:
405 write_gmx(x11, gmx, IDRECTBOX);
406 break;
407 case esbTri:
408 write_gmx(x11, gmx, IDTRIBOX);
409 break;
410 case esbTrunc:
411 write_gmx(x11, gmx, IDTOBOX);
412 break;
413 default:
414 gmx_fatal(FARGS, "Invalid box type %d at %s, %d",
415 ebox, __FILE__, __LINE__);
419 break;
423 enum {
424 esFUNCT = 1, esBSHOW, esINFIL, esINDEXFIL, esLSQ, esSHOW, esPLOTFIL
427 static bool in_set(int i, int n, int set[])
429 int j;
430 for (j = 0; (j < n); j++)
432 if (set[j] == i)
434 return true;
437 return false;
440 typedef t_dlg *t_mmb (t_x11 *x11, t_gmx *gmx);
442 typedef struct {
443 const char *dlgfile;
444 DlgCallback *cb;
445 } t_dlginit;
447 void init_dlgs(t_x11 *x11, t_gmx *gmx)
449 static t_dlginit di[] = {
450 { "export.dlg", ExportCB },
451 { "bonds.dlg", BondsCB }
453 static t_mmb *mi[emNR] = { quit_mb, help_mb, about_mb, ni_mb };
454 unsigned int i;
456 snew(gmx->dlgs, edNR);
457 for (i = 0; (i < asize(di)); i++)
459 gmx->dlgs[i] = ReadDlg(x11, gmx->wd->self, di[i].dlgfile,
460 di[i].dlgfile,
461 0, 0, true, false, di[i].cb, gmx);
464 gmx->dlgs[edFilter] = select_filter(x11, gmx);
466 snew(gmx->mboxes, emNR);
467 for (i = 0; (i < emNR); i++)
469 gmx->mboxes[i] = mi[i](x11, gmx);
471 gmx->which_mb = -1;
474 void done_dlgs(t_gmx *gmx)
476 int i;
478 for (i = 0; (i < edNR); i++)
480 FreeDlg(gmx->dlgs[i]);
482 for (i = 0; (i < emNR); i++)
484 FreeDlg(gmx->mboxes[i]);
488 void edit_file(const char *fn)
490 if (fork() == 0)
492 char script[256];
494 sprintf(script, "vi %s", fn);
495 shell_comm(fn, script, 0);
496 exit(0);