Apply clang-format to source tree
[alexxy/gromacs.git] / src / programs / view / dialogs.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,2015,2016,2017,2019, 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 "dialogs.h"
40
41 #include "config.h"
42
43 #include <cstdio>
44 #include <cstdlib>
45 #include <cstring>
46
47 #ifdef HAVE_UNISTD_H
48 #    include <unistd.h> // for fork()
49 #endif
50
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"
58
59 #include "manager.h"
60 #include "nmol.h"
61 #include "x11.h"
62 #include "xdlghi.h"
63 #include "xmb.h"
64
65 #define MBFLAGS /* MB_APPLMODAL | */ MB_DONTSHOW
66
67 void write_gmx(t_x11* x11, t_gmx* gmx, int mess)
68 {
69     XEvent letter;
70
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);
79 }
80
81 static void shell_comm(const char* title, const char* script, int nsleep)
82 {
83     FILE* tfil;
84     char  command[STRLEN];
85     char  tmp[32];
86
87     std::strcpy(tmp, "dialogXXXXXX");
88     tfil = gmx_fopen_temporary(tmp);
89
90     fprintf(tfil, "%s\n", script);
91     fprintf(tfil, "sleep %d\n", nsleep);
92     gmx_ffclose(tfil);
93
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
98
99     if (0 != std::system(command))
100     {
101         gmx_fatal(FARGS, "Failed to execute command: %s", command);
102     }
103
104 #ifdef DEBUG
105     unlink(tmp)
106 #endif
107 }
108
109 void show_mb(t_gmx* gmx, int mb)
110 {
111     if (mb >= 0 && mb < emNR)
112     {
113         gmx->which_mb = mb;
114         ShowDlg(gmx->mboxes[mb]);
115     }
116 }
117
118 static void hide_mb(t_gmx* gmx)
119 {
120     if (gmx->which_mb >= 0 && gmx->which_mb < emNR)
121     {
122         HideDlg(gmx->mboxes[gmx->which_mb]);
123         gmx->which_mb = -1;
124     }
125 }
126
127 static void MBCallback(t_x11* /*x11*/, int dlg_mess, int /*item_id*/, char* /*set*/, void* data)
128 {
129     t_gmx* gmx;
130
131     gmx = static_cast<t_gmx*>(data);
132     if (dlg_mess == DLG_EXIT)
133     {
134         hide_mb(gmx);
135     }
136 }
137
138 static t_dlg* about_mb(t_x11* x11, t_gmx* gmx)
139 {
140     const char* lines[] = { "         G R O M A C S", " Machine for Simulating Chemistry",
141                             "       Copyright (c) 1992-2013",
142                             "  Berk Hess, David van der Spoel, Erik Lindahl",
143                             "        and many collaborators!" };
144
145     return MessageBox(x11, gmx->wd->self, gmx->wd->text, asize(lines), lines,
146                       MB_OK | MB_ICONGMX | MBFLAGS, MBCallback, gmx);
147 }
148
149 static void QuitCB(t_x11* x11, int dlg_mess, int /*item_id*/, char* set, void* data)
150 {
151     t_gmx* gmx;
152     gmx = static_cast<t_gmx*>(data);
153
154     hide_mb(gmx);
155     if (dlg_mess == DLG_EXIT)
156     {
157         if (gmx_strcasecmp("yes", set) == 0)
158         {
159             write_gmx(x11, gmx, IDTERM);
160         }
161     }
162 }
163
164 static t_dlg* quit_mb(t_x11* x11, t_gmx* gmx)
165 {
166     const char* lines[] = { " Do you really want to Quit ?" };
167
168     return MessageBox(x11, gmx->wd->self, gmx->wd->text, asize(lines), lines,
169                       MB_YESNO | MB_ICONSTOP | MBFLAGS, QuitCB, gmx);
170 }
171
172 static t_dlg* help_mb(t_x11* x11, t_gmx* gmx)
173 {
174     const char* lines[] = { " Help will soon be added" };
175
176     return MessageBox(x11, gmx->wd->self, gmx->wd->text, asize(lines), lines,
177                       MB_OK | MB_ICONINFORMATION | MBFLAGS, MBCallback, gmx);
178 }
179
180 static t_dlg* ni_mb(t_x11* x11, t_gmx* gmx)
181 {
182     const char* lines[] = { " This feature has not been", " implemented yet." };
183
184     return MessageBox(x11, gmx->wd->self, gmx->wd->text, asize(lines), lines,
185                       MB_OK | MB_ICONEXCLAMATION | MBFLAGS, MBCallback, gmx);
186 }
187
188 enum
189 {
190     eExE,
191     eExGrom,
192     eExPdb,
193     eExConf,
194     eExNR
195 };
196
197 static void ExportCB(t_x11* x11, int dlg_mess, int item_id, char* set, void* data)
198 {
199     bool   bOk;
200     t_gmx* gmx;
201     t_dlg* dlg;
202
203     gmx = static_cast<t_gmx*>(data);
204     dlg = gmx->dlgs[edExport];
205     switch (dlg_mess)
206     {
207         case DLG_SET:
208             switch (item_id)
209             {
210                 case eExGrom: gmx->ExpMode = eExpGromos; break;
211                 case eExPdb: gmx->ExpMode = eExpPDB; break;
212                 default: break;
213             }
214 #ifdef DEBUG
215             std::fprintf(stderr, "exportcb: item_id=%d\n", item_id);
216 #endif
217             break;
218         case DLG_EXIT:
219             if ((bOk = gmx_strcasecmp("ok", set)) == 0)
220             {
221                 std::strcpy(gmx->confout, EditText(dlg, eExConf));
222             }
223             HideDlg(dlg);
224             if (bOk)
225             {
226                 write_gmx(x11, gmx, IDDOEXPORT);
227             }
228             break;
229     }
230 }
231
232 enum
233 {
234     eg0,
235     egTOPOL,
236     egCONFIN,
237     egPARAM,
238     eg1,
239     eg1PROC,
240     eg32PROC
241 };
242
243 enum bond_set
244 {
245     ebShowH = 11,
246     ebDPlus,
247     ebRMPBC,
248     ebCue,
249     ebSkip,
250     ebWait
251 };
252
253 static void BondsCB(t_x11* x11, int dlg_mess, int item_id, char* set, void* data)
254 {
255     static int ebond = -1;
256     static int ebox  = -1;
257     bool       bOk, bBond = false;
258     int        nskip, nwait;
259     t_gmx*     gmx;
260     char*      endptr;
261
262     gmx = static_cast<t_gmx*>(data);
263     if (ebond == -1)
264     {
265         ebond = gmx->man->molw->bond_type;
266         ebox  = gmx->man->molw->boxtype;
267     }
268     switch (dlg_mess)
269     {
270         case DLG_SET:
271             if (item_id <= eBNR)
272             {
273                 ebond = item_id - 1;
274                 bBond = false;
275             }
276             else if (item_id <= eBNR + esbNR + 1)
277             {
278                 ebox  = item_id - eBNR - 2;
279                 bBond = true;
280             }
281             else
282             {
283
284 #define DO_NOT(b) (b) = (!(b))
285
286                 switch (item_id)
287                 {
288                     case ebShowH: toggle_hydrogen(x11, gmx->man->molw); break;
289                     case ebDPlus: DO_NOT(gmx->man->bPlus);
290 #ifdef DEBUG
291                         std::fprintf(stderr, "gmx->man->bPlus=%s\n", gmx->man->bPlus ? "true" : "false");
292 #endif
293                         break;
294                     case ebRMPBC: toggle_pbc(gmx->man); break;
295                     case ebCue: DO_NOT(gmx->man->bSort);
296 #ifdef DEBUG
297                         std::fprintf(stderr, "gmx->man->bSort=%s\n", gmx->man->bSort ? "true" : "false");
298 #endif
299                         break;
300                     case ebSkip:
301                         nskip = std::strtol(set, &endptr, 10);
302                         if (endptr != set)
303                         {
304 #ifdef DEBUG
305                             std::fprintf(stderr, "nskip: %d frames\n", nskip);
306 #endif
307                             if (nskip >= 0)
308                             {
309                                 gmx->man->nSkip = nskip;
310                             }
311                         }
312                         break;
313                     case ebWait:
314                         nwait = std::strtol(set, &endptr, 10);
315                         if (endptr != set)
316                         {
317 #ifdef DEBUG
318                             std::fprintf(stderr, "wait: %d ms\n", nwait);
319 #endif
320                             if (nwait >= 0)
321                             {
322                                 gmx->man->nWait = nwait;
323                             }
324                         }
325                     default:
326 #ifdef DEBUG
327                         std::fprintf(stderr, "item_id: %d, set: %s\n", item_id, set);
328 #endif
329                         break;
330                 }
331             }
332             break;
333         case DLG_EXIT:
334             bOk = (gmx_strcasecmp("ok", set) == 0);
335             HideDlg(gmx->dlgs[edBonds]);
336             if (bOk)
337             {
338                 if (bBond)
339                 {
340                     switch (ebond)
341                     {
342                         case eBThin: write_gmx(x11, gmx, IDTHIN); break;
343                         case eBFat: write_gmx(x11, gmx, IDFAT); break;
344                         case eBVeryFat: write_gmx(x11, gmx, IDVERYFAT); break;
345                         case eBSpheres: write_gmx(x11, gmx, IDBALLS); break;
346                         default:
347                             gmx_fatal(FARGS, "Invalid bond type %d at %s, %d", ebond, __FILE__, __LINE__);
348                     }
349                 }
350                 else
351                 {
352                     switch (ebox)
353                     {
354                         case esbNone: write_gmx(x11, gmx, IDNOBOX); break;
355                         case esbRect: write_gmx(x11, gmx, IDRECTBOX); break;
356                         case esbTri: write_gmx(x11, gmx, IDTRIBOX); break;
357                         case esbTrunc: write_gmx(x11, gmx, IDTOBOX); break;
358                         default:
359                             gmx_fatal(FARGS, "Invalid box type %d at %s, %d", ebox, __FILE__, __LINE__);
360                     }
361                 }
362             }
363             break;
364     }
365 }
366
367 enum
368 {
369     esFUNCT = 1,
370     esBSHOW,
371     esINFIL,
372     esINDEXFIL,
373     esLSQ,
374     esSHOW,
375     esPLOTFIL
376 };
377
378 typedef t_dlg* t_mmb(t_x11* x11, t_gmx* gmx);
379
380 typedef struct
381 {
382     const char*  dlgfile;
383     DlgCallback* cb;
384 } t_dlginit;
385
386 void init_dlgs(t_x11* x11, t_gmx* gmx)
387 {
388     static t_dlginit di[]     = { { "export.dlg", ExportCB }, { "bonds.dlg", BondsCB } };
389     static t_mmb*    mi[emNR] = { quit_mb, help_mb, about_mb, ni_mb };
390
391     snew(gmx->dlgs, edNR);
392     for (int i = 0; (i < asize(di)); i++)
393     {
394         gmx->dlgs[i] = ReadDlg(x11, gmx->wd->self, di[i].dlgfile, di[i].dlgfile, 0, 0, true, false,
395                                di[i].cb, gmx);
396     }
397
398     gmx->dlgs[edFilter] = select_filter(x11, gmx);
399
400     snew(gmx->mboxes, emNR);
401     for (int i = 0; (i < emNR); i++)
402     {
403         gmx->mboxes[i] = mi[i](x11, gmx);
404     }
405     gmx->which_mb = -1;
406 }
407
408 void done_dlgs(t_gmx* gmx)
409 {
410     int i;
411
412     for (i = 0; (i < edNR); i++)
413     {
414         FreeDlg(gmx->dlgs[i]);
415     }
416     for (i = 0; (i < emNR); i++)
417     {
418         FreeDlg(gmx->mboxes[i]);
419     }
420 }
421
422 void edit_file(const char* fn)
423 {
424     if (fork() == 0)
425     {
426         char script[256];
427
428         std::sprintf(script, "vi  %s", fn);
429         shell_comm(fn, script, 0);
430         std::exit(0);
431     }
432 }