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 by the GROMACS development team.
7  * Copyright (c) 2019,2020, by the GROMACS development team, led by
8  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9  * and including many others, as listed in the AUTHORS file in the
10  * top-level source directory and at http://www.gromacs.org.
11  *
12  * GROMACS is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation; either version 2.1
15  * of the License, or (at your option) any later version.
16  *
17  * GROMACS is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with GROMACS; if not, see
24  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
26  *
27  * If you want to redistribute modifications to GROMACS, please
28  * consider that scientific software is very special. Version
29  * control is crucial - bugs must be traceable. We will be happy to
30  * consider code for inclusion in the official distribution, but
31  * derived work must not be called official GROMACS. Details are found
32  * in the README & COPYING files - if they are missing, get the
33  * official version at http://www.gromacs.org.
34  *
35  * To help us fund GROMACS development, we humbly ask that you cite
36  * the research papers on the package. Check out http://www.gromacs.org.
37  */
38 #include "gmxpre.h"
39 
40 #include "dialogs.h"
41 
42 #include "config.h"
43 
44 #include <cstdio>
45 #include <cstdlib>
46 #include <cstring>
47 
48 #ifdef HAVE_UNISTD_H
49 #    include <unistd.h> // for fork()
50 #endif
51 
52 #include "gromacs/mdtypes/md_enums.h"
53 #include "gromacs/utility/arraysize.h"
54 #include "gromacs/utility/cstringutil.h"
55 #include "gromacs/utility/dir_separator.h"
56 #include "gromacs/utility/fatalerror.h"
57 #include "gromacs/utility/futil.h"
58 #include "gromacs/utility/smalloc.h"
59 
60 #include "manager.h"
61 #include "nmol.h"
62 #include "x11.h"
63 #include "xdlghi.h"
64 #include "xmb.h"
65 
66 #define MBFLAGS /* MB_APPLMODAL | */ MB_DONTSHOW
67 
write_gmx(t_x11 * x11,t_gmx * gmx,int mess)68 void write_gmx(t_x11* x11, t_gmx* gmx, int mess)
69 {
70     XEvent letter;
71 
72     letter.type                 = ClientMessage;
73     letter.xclient.display      = x11->disp;
74     letter.xclient.window       = gmx->wd->self;
75     letter.xclient.message_type = 0;
76     letter.xclient.format       = 32;
77     letter.xclient.data.l[0]    = mess;
78     letter.xclient.data.l[1]    = Button1;
79     XSendEvent(x11->disp, letter.xclient.window, True, 0, &letter);
80 }
81 
shell_comm(const char * title,const char * script,int nsleep)82 static void shell_comm(const char* title, const char* script, int nsleep)
83 {
84     FILE* tfil;
85     char  command[STRLEN];
86     char  tmp[32];
87 
88     std::strcpy(tmp, "dialogXXXXXX");
89     tfil = gmx_fopen_temporary(tmp);
90 
91     fprintf(tfil, "%s\n", script);
92     fprintf(tfil, "sleep %d\n", nsleep);
93     gmx_ffclose(tfil);
94 
95     std::sprintf(command, "xterm -title %s -e sh %s", title, tmp);
96 #ifdef DEBUG
97     std::fprintf(stderr, "command: %s\n", command);
98 #endif
99 
100     if (0 != std::system(command))
101     {
102         gmx_fatal(FARGS, "Failed to execute command: %s", command);
103     }
104 
105 #ifdef DEBUG
106     unlink(tmp)
107 #endif
108 }
109 
show_mb(t_gmx * gmx,int mb)110 void show_mb(t_gmx* gmx, int mb)
111 {
112     if (mb >= 0 && mb < emNR)
113     {
114         gmx->which_mb = mb;
115         ShowDlg(gmx->mboxes[mb]);
116     }
117 }
118 
hide_mb(t_gmx * gmx)119 static void hide_mb(t_gmx* gmx)
120 {
121     if (gmx->which_mb >= 0 && gmx->which_mb < emNR)
122     {
123         HideDlg(gmx->mboxes[gmx->which_mb]);
124         gmx->which_mb = -1;
125     }
126 }
127 
MBCallback(t_x11 *,int dlg_mess,int,char *,void * data)128 static void MBCallback(t_x11* /*x11*/, int dlg_mess, int /*item_id*/, char* /*set*/, void* data)
129 {
130     t_gmx* gmx;
131 
132     gmx = static_cast<t_gmx*>(data);
133     if (dlg_mess == DLG_EXIT)
134     {
135         hide_mb(gmx);
136     }
137 }
138 
about_mb(t_x11 * x11,t_gmx * gmx)139 static t_dlg* about_mb(t_x11* x11, t_gmx* gmx)
140 {
141     const char* lines[] = { "         G R O M A C S", " Machine for Simulating Chemistry",
142                             "       Copyright (c) 1992-2013",
143                             "  Berk Hess, David van der Spoel, Erik Lindahl",
144                             "        and many collaborators!" };
145 
146     return MessageBox(x11, gmx->wd->self, gmx->wd->text, asize(lines), lines,
147                       MB_OK | MB_ICONGMX | MBFLAGS, MBCallback, gmx);
148 }
149 
QuitCB(t_x11 * x11,int dlg_mess,int,char * set,void * data)150 static void QuitCB(t_x11* x11, int dlg_mess, int /*item_id*/, char* set, void* data)
151 {
152     t_gmx* gmx;
153     gmx = static_cast<t_gmx*>(data);
154 
155     hide_mb(gmx);
156     if (dlg_mess == DLG_EXIT)
157     {
158         if (gmx_strcasecmp("yes", set) == 0)
159         {
160             write_gmx(x11, gmx, IDTERM);
161         }
162     }
163 }
164 
quit_mb(t_x11 * x11,t_gmx * gmx)165 static t_dlg* quit_mb(t_x11* x11, t_gmx* gmx)
166 {
167     const char* lines[] = { " Do you really want to Quit ?" };
168 
169     return MessageBox(x11, gmx->wd->self, gmx->wd->text, asize(lines), lines,
170                       MB_YESNO | MB_ICONSTOP | MBFLAGS, QuitCB, gmx);
171 }
172 
help_mb(t_x11 * x11,t_gmx * gmx)173 static t_dlg* help_mb(t_x11* x11, t_gmx* gmx)
174 {
175     const char* lines[] = { " Help will soon be added" };
176 
177     return MessageBox(x11, gmx->wd->self, gmx->wd->text, asize(lines), lines,
178                       MB_OK | MB_ICONINFORMATION | MBFLAGS, MBCallback, gmx);
179 }
180 
ni_mb(t_x11 * x11,t_gmx * gmx)181 static t_dlg* ni_mb(t_x11* x11, t_gmx* gmx)
182 {
183     const char* lines[] = { " This feature has not been", " implemented yet." };
184 
185     return MessageBox(x11, gmx->wd->self, gmx->wd->text, asize(lines), lines,
186                       MB_OK | MB_ICONEXCLAMATION | MBFLAGS, MBCallback, gmx);
187 }
188 
189 enum
190 {
191     eExE,
192     eExGrom,
193     eExPdb,
194     eExConf,
195     eExNR
196 };
197 
ExportCB(t_x11 * x11,int dlg_mess,int item_id,char * set,void * data)198 static void ExportCB(t_x11* x11, int dlg_mess, int item_id, char* set, void* data)
199 {
200     bool   bOk;
201     t_gmx* gmx;
202     t_dlg* dlg;
203 
204     gmx = static_cast<t_gmx*>(data);
205     dlg = gmx->dlgs[edExport];
206     switch (dlg_mess)
207     {
208         case DLG_SET:
209             switch (item_id)
210             {
211                 case eExGrom: gmx->ExpMode = eExpGromos; break;
212                 case eExPdb: gmx->ExpMode = eExpPDB; break;
213                 default: break;
214             }
215 #ifdef DEBUG
216             std::fprintf(stderr, "exportcb: item_id=%d\n", item_id);
217 #endif
218             break;
219         case DLG_EXIT:
220             if ((bOk = gmx_strcasecmp("ok", set)) == 0)
221             {
222                 std::strcpy(gmx->confout, EditText(dlg, eExConf));
223             }
224             HideDlg(dlg);
225             if (bOk)
226             {
227                 write_gmx(x11, gmx, IDDOEXPORT);
228             }
229             break;
230     }
231 }
232 
233 enum
234 {
235     eg0,
236     egTOPOL,
237     egCONFIN,
238     egPARAM,
239     eg1,
240     eg1PROC,
241     eg32PROC
242 };
243 
244 enum bond_set
245 {
246     ebShowH = 11,
247     ebDPlus,
248     ebRMPBC,
249     ebCue,
250     ebSkip,
251     ebWait
252 };
253 
BondsCB(t_x11 * x11,int dlg_mess,int item_id,char * set,void * data)254 static void BondsCB(t_x11* x11, int dlg_mess, int item_id, char* set, void* data)
255 {
256     static int ebond = -1;
257     static int ebox  = -1;
258     bool       bOk, bBond = false;
259     int        nskip, nwait;
260     t_gmx*     gmx;
261     char*      endptr;
262 
263     gmx = static_cast<t_gmx*>(data);
264     if (ebond == -1)
265     {
266         ebond = gmx->man->molw->bond_type;
267         ebox  = gmx->man->molw->boxtype;
268     }
269     switch (dlg_mess)
270     {
271         case DLG_SET:
272             if (item_id <= eBNR)
273             {
274                 ebond = item_id - 1;
275                 bBond = false;
276             }
277             else if (item_id <= eBNR + esbNR + 1)
278             {
279                 ebox  = item_id - eBNR - 2;
280                 bBond = true;
281             }
282             else
283             {
284 
285 #define DO_NOT(b) (b) = (!(b))
286 
287                 switch (item_id)
288                 {
289                     case ebShowH: toggle_hydrogen(x11, gmx->man->molw); break;
290                     case ebDPlus: DO_NOT(gmx->man->bPlus);
291 #ifdef DEBUG
292                         std::fprintf(stderr, "gmx->man->bPlus=%s\n", gmx->man->bPlus ? "true" : "false");
293 #endif
294                         break;
295                     case ebRMPBC: toggle_pbc(gmx->man); break;
296                     case ebCue: DO_NOT(gmx->man->bSort);
297 #ifdef DEBUG
298                         std::fprintf(stderr, "gmx->man->bSort=%s\n", gmx->man->bSort ? "true" : "false");
299 #endif
300                         break;
301                     case ebSkip:
302                         nskip = std::strtol(set, &endptr, 10);
303                         if (endptr != set)
304                         {
305 #ifdef DEBUG
306                             std::fprintf(stderr, "nskip: %d frames\n", nskip);
307 #endif
308                             if (nskip >= 0)
309                             {
310                                 gmx->man->nSkip = nskip;
311                             }
312                         }
313                         break;
314                     case ebWait:
315                         nwait = std::strtol(set, &endptr, 10);
316                         if (endptr != set)
317                         {
318 #ifdef DEBUG
319                             std::fprintf(stderr, "wait: %d ms\n", nwait);
320 #endif
321                             if (nwait >= 0)
322                             {
323                                 gmx->man->nWait = nwait;
324                             }
325                         }
326                     default:
327 #ifdef DEBUG
328                         std::fprintf(stderr, "item_id: %d, set: %s\n", item_id, set);
329 #endif
330                         break;
331                 }
332             }
333             break;
334         case DLG_EXIT:
335             bOk = (gmx_strcasecmp("ok", set) == 0);
336             HideDlg(gmx->dlgs[edBonds]);
337             if (bOk)
338             {
339                 if (bBond)
340                 {
341                     switch (ebond)
342                     {
343                         case eBThin: write_gmx(x11, gmx, IDTHIN); break;
344                         case eBFat: write_gmx(x11, gmx, IDFAT); break;
345                         case eBVeryFat: write_gmx(x11, gmx, IDVERYFAT); break;
346                         case eBSpheres: write_gmx(x11, gmx, IDBALLS); break;
347                         default:
348                             gmx_fatal(FARGS, "Invalid bond type %d at %s, %d", ebond, __FILE__, __LINE__);
349                     }
350                 }
351                 else
352                 {
353                     switch (ebox)
354                     {
355                         case esbNone: write_gmx(x11, gmx, IDNOBOX); break;
356                         case esbRect: write_gmx(x11, gmx, IDRECTBOX); break;
357                         case esbTri: write_gmx(x11, gmx, IDTRIBOX); break;
358                         case esbTrunc: write_gmx(x11, gmx, IDTOBOX); break;
359                         default:
360                             gmx_fatal(FARGS, "Invalid box type %d at %s, %d", ebox, __FILE__, __LINE__);
361                     }
362                 }
363             }
364             break;
365     }
366 }
367 
368 enum
369 {
370     esFUNCT = 1,
371     esBSHOW,
372     esINFIL,
373     esINDEXFIL,
374     esLSQ,
375     esSHOW,
376     esPLOTFIL
377 };
378 
379 typedef t_dlg* t_mmb(t_x11* x11, t_gmx* gmx);
380 
381 typedef struct
382 {
383     const char*  dlgfile;
384     DlgCallback* cb;
385 } t_dlginit;
386 
init_dlgs(t_x11 * x11,t_gmx * gmx)387 void init_dlgs(t_x11* x11, t_gmx* gmx)
388 {
389     static t_dlginit di[]     = { { "export.dlg", ExportCB }, { "bonds.dlg", BondsCB } };
390     static t_mmb*    mi[emNR] = { quit_mb, help_mb, about_mb, ni_mb };
391 
392     snew(gmx->dlgs, edNR);
393     for (int i = 0; (i < asize(di)); i++)
394     {
395         gmx->dlgs[i] = ReadDlg(x11, gmx->wd->self, di[i].dlgfile, di[i].dlgfile, 0, 0, true, false,
396                                di[i].cb, gmx);
397     }
398 
399     gmx->dlgs[edFilter] = select_filter(x11, gmx);
400 
401     snew(gmx->mboxes, emNR);
402     for (int i = 0; (i < emNR); i++)
403     {
404         gmx->mboxes[i] = mi[i](x11, gmx);
405     }
406     gmx->which_mb = -1;
407 }
408 
done_dlgs(t_gmx * gmx)409 void done_dlgs(t_gmx* gmx)
410 {
411     int i;
412 
413     for (i = 0; (i < edNR); i++)
414     {
415         FreeDlg(gmx->dlgs[i]);
416     }
417     for (i = 0; (i < emNR); i++)
418     {
419         FreeDlg(gmx->mboxes[i]);
420     }
421 }
422 
edit_file(const char * fn)423 void edit_file(const char* fn)
424 {
425     if (fork() == 0)
426     {
427         char script[256];
428 
429         std::sprintf(script, "vi  %s", fn);
430         shell_comm(fn, script, 0);
431         std::exit(0);
432     }
433 }
434