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,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.
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.
47 #include "gromacs/utility/cstringutil.h"
48 #include "gromacs/utility/fatalerror.h"
49 #include "gromacs/utility/smalloc.h"
55 static t_dlgitem* newitem(void)
64 /*****************************
66 * Window Procedures and helpful functions
68 ****************************/
69 static void ShowCaret(t_x11* x11, t_dlgitem* dlgitem)
73 if (dlgitem->type == edlgET)
77 et = &(dlgitem->u.edittext);
78 x = XTextWidth(x11->font, dlgitem->win.text, std::strlen(dlgitem->win.text)) + XCARET
79 + XTextWidth(x11->font, (char*)&(et->buf[et->strbegin]), et->pos);
80 y1 = (dlgitem->win.height - XTextHeight(x11->font)) / 2;
81 y2 = (dlgitem->win.height - y1);
83 XDrawLine(x11->disp, dlgitem->win.self, x11->gc, x - XCARET, y1, x + XCARET, y1);
84 XDrawLine(x11->disp, dlgitem->win.self, x11->gc, x, y1, x, y2);
85 XDrawLine(x11->disp, dlgitem->win.self, x11->gc, x - XCARET, y2, x + XCARET, y2);
89 static void HideCaret(t_x11* x11, t_dlgitem* dlgitem)
91 XSetForeground(x11->disp, x11->gc, x11->bg);
92 ShowCaret(x11, dlgitem);
93 XSetForeground(x11->disp, x11->gc, x11->fg);
96 static int DefWndProc(t_x11* x11, t_dlgitem* dlgitem, XEvent* event)
98 XComposeStatus status;
103 std::printf("DefWndProc\n");
110 if (HelpPressed(event))
116 XLookupString(&(event->xkey), c, BUFSIZE, &keysym, &status);
117 if ((keysym == XK_Return) || (keysym == XK_KP_Enter))
124 dlgitem->win.bFocus = true;
125 ShowCaret(x11, dlgitem);
126 /* LightBorder(x11->disp,dlgitem->win.self,x11->fg); */
129 dlgitem->win.bFocus = false;
130 HideCaret(x11, dlgitem);
131 /* LightBorder(x11->disp,dlgitem->win.self,x11->bg); */
133 default: XBell(x11->disp, 50);
138 static int WndProcBN(t_x11* x11, t_dlgitem* dlgitem, XEvent* event)
143 if (dlgitem->type != edlgBN)
145 gmx_incons("button processing");
147 win = &(dlgitem->win);
148 w = XTextWidth(x11->font, win->text, std::strlen(win->text));
149 x = (win->width - w) / 2;
150 th = XTextHeight(x11->font) + OFFS_Y;
154 RectWin(x11->disp, x11->gc, win, x11->fg);
155 TextInRect(x11, win->self, win->text, 0, 0, win->width, th, eXCenter, eYCenter);
157 case ButtonPress: return BNPRESSED;
158 case EnterNotify: XDrawLine(x11->disp, win->self, x11->gc, x - 1, th, x + w, th); break;
160 XSetForeground(x11->disp, x11->gc, x11->bg);
161 XDrawLine(x11->disp, win->self, x11->gc, x - 1, th, x + w, th);
162 XSetForeground(x11->disp, x11->gc, x11->fg);
164 default: return DefWndProc(x11, dlgitem, event);
169 static int WndProcRB(t_x11* x11, t_dlgitem* dlgitem, XEvent* event)
175 if (dlgitem->type != edlgRB)
177 gmx_incons("radiobutton processing");
179 rb = &(dlgitem->u.radiobutton);
180 win = &(dlgitem->win);
182 rad = win->height / 3;
188 XClearArea(x11->disp, win->self, x - rad, y - rad, x + rad, y + rad, False);
192 XFillCircle(x11->disp, win->self, x11->gc, x, y, rad);
194 XDrawCircle(x11->disp, win->self, x11->gc, x, y, rad);
196 TextInRect(x11, win->self, win->text, x, 0, win->width - x, win->height, eXLeft, eYCenter);
203 XBell(x11->disp, 50);
206 case LeaveNotify: break;
207 default: return DefWndProc(x11, dlgitem, event);
212 static int WndProcGB(t_x11* x11, t_dlgitem* dlgitem, XEvent* event)
217 if (dlgitem->type != edlgGB)
219 gmx_incons("gb processing");
221 win = &(dlgitem->win);
223 x = XTextWidth(x11->font, win->text, std::strlen(win->text));
224 y = XTextHeight(x11->font);
228 XSetForeground(x11->disp, x11->gc, x11->fg);
229 XDrawRoundRect(x11->disp, win->self, x11->gc, 0, y / 2, win->width - 1,
230 win->height - y / 2 - 1);
231 XClearArea(x11->disp, win->self, OFFS_X, 0, x + OFFS_X, y, False);
232 TextInRect(x11, win->self, win->text, 2 * OFFS_X, 0, x, y, eXCenter, eYCenter);
235 case LeaveNotify: break;
236 default: return DefWndProc(x11, dlgitem, event);
241 static int WndProcCB(t_x11* x11, t_dlgitem* dlgitem, XEvent* event)
247 if (dlgitem->type != edlgCB)
249 gmx_incons("check box processing");
251 cb = &(dlgitem->u.checkbox);
252 win = &(dlgitem->win);
261 XSetForeground(x11->disp, x11->gc, x11->fg);
262 XClearArea(x11->disp, win->self, x, y, w, h, False);
263 XDrawRectangle(x11->disp, win->self, x11->gc, x, y, w, h);
266 XDrawLine(x11->disp, win->self, x11->gc, x, y, x + w, y + h);
267 XDrawLine(x11->disp, win->self, x11->gc, x + w, y, x, y + h);
270 TextInRect(x11, win->self, win->text, x, 0, win->width - x, win->height, eXLeft, eYCenter);
272 case ButtonPress: cb->bChecked = !cb->bChecked; return CBPRESSED;
274 case LeaveNotify: break;
275 default: return DefWndProc(x11, dlgitem, event);
280 static int WndProcST(t_x11* x11, t_dlgitem* dlgitem, XEvent* event)
286 if (dlgitem->type != edlgST)
288 gmx_incons("st processing");
290 st = &(dlgitem->u.statictext);
291 win = &(dlgitem->win);
296 dy = XTextHeight(x11->font) + OFFS_Y;
297 for (i = 0; (i < st->nlines); i++)
299 TextInRect(x11, win->self, st->lines[i], 0, OFFS_Y + i * dy, win->width, dy, eXLeft,
303 default: return DefWndProc(x11, dlgitem, event);
308 static bool insert(char* s, char c, int* pos)
315 /* +1 for zero termination */
316 for (i = sl + 1; (i > *pos); i--)
327 static bool my_backspace(char* s, int* pos)
332 if ((sl > 0) && ((*pos) > 0))
334 for (i = *pos - 1; (i < sl); i++)
338 (*pos) = std::max(0, (*pos) - 1);
344 static bool my_delete(char* s, int* pos)
349 if ((sl > 0) && ((*pos) < sl))
351 for (i = *pos; (i < sl); i++)
360 static int WndProcET(t_x11* x11, t_dlgitem* dlgitem, XEvent* event)
365 char c[BUFSIZE + 1], *bp;
368 int xp, xtitle, ewidth;
370 if (dlgitem->type != edlgET)
372 gmx_incons("st processing");
374 et = &(dlgitem->u.edittext);
375 win = &(dlgitem->win);
377 /* Copy string part that is visible into screen buffer */
378 for (i = 0; (i < et->buflen); i++)
380 scrbuf[i] = et->buf[i + et->strbegin];
387 XSetForeground(x11->disp, x11->gc, x11->fg);
388 xtitle = XTextWidth(x11->font, win->text, std::strlen(win->text));
389 ewidth = win->width - xtitle;
390 TextInRect(x11, win->self, win->text, 0, 0, xtitle - 1, win->height, eXLeft, eYCenter);
391 XClearArea(x11->disp, win->self, xtitle, 0, ewidth + XCARET, win->height, False);
392 TextInRect(x11, win->self, scrbuf, xtitle + XCARET, 0, ewidth, win->height, eXLeft, eYCenter);
394 std::printf("Expose\n");
398 ShowCaret(x11, dlgitem);
402 /* Calculate new position for caret */
403 et->pos = std::strlen(et->buf);
404 bp = gmx_strdup(et->buf);
405 xp = event->xbutton.x - XTextWidth(x11->font, win->text, std::strlen(win->text)) - XCARET;
406 while ((et->pos > 0) && (XTextWidth(x11->font, bp, std::strlen(bp)) > xp))
415 /* Check for HelpKey */
416 if (HelpPressed(event))
418 return DefWndProc(x11, dlgitem, event);
420 XLookupString(&(event->xkey), c, BUFSIZE, &keysym, nullptr);
422 std::printf("Keysym: %x\n", keysym);
427 if (my_delete(et->buf, &(et->pos)))
434 XBell(x11->disp, 50);
438 if (my_backspace(et->buf, &(et->pos)))
445 XBell(x11->disp, 50);
449 case XK_Return: return ENTERPRESSED;
456 if (strlen(et->buf) <= (unsigned int)et->buflen)
458 et->pos = std::strlen(et->buf);
462 et->pos = et->buflen;
463 et->strbegin = std::strlen(et->buf) - et->buflen;
468 et->pos = std::max(0, et->pos - 1);
469 et->strbegin = std::min(et->strbegin, et->pos);
473 if ((et->pos < et->buflen) && (et->strbegin + et->buflen > (int)strlen(et->buf)))
477 else if ((et->buflen < (int)strlen(et->buf))
478 && (et->strbegin < (int)strlen(et->buf) - et->buflen))
491 if (insert(et->buf, c[0], &(et->pos)))
497 XBell(x11->disp, 50);
503 HideCaret(x11, dlgitem);
506 et->bChanged = false;
509 default: return DefWndProc(x11, dlgitem, event);
514 /*****************************
516 * Routines to create dialog items, all items have an id
517 * which you can use to extract info. It is possible to have
518 * multiple items with the same id but it may then not be possible
519 * to extract information.
520 * All routines take the position relative to the parent dlg
521 * and the size and border width.
522 * If the width and height are set to zero initially, they will
523 * be calculated and set by the routine. With the dlgitem manipulation
524 * routines listed below, the application can then move the items around
525 * on the dlg box, and if wished resize them.
527 ****************************/
529 CreateButton(t_x11* x11, const char* szLab, bool bDef, t_id id, t_id groupid, int x0, int y0, int w, int h, int bw)
537 h = XTextHeight(x11->font) + 2 * OFFS_Y;
541 w = XTextWidth(x11->font, szLab, std::strlen(szLab)) + 2 * OFFS_X;
545 snew(lab, std::strlen(szLab) + 7); /* 6 for >> << and 1 for \0 */
546 std::sprintf(lab, ">> %s <<", szLab);
550 lab = gmx_strdup(szLab);
552 InitWin(&(dlgitem->win), x0, y0, w, h, bw, szLab);
555 dlgitem->GroupID = groupid;
556 dlgitem->type = edlgBN;
557 dlgitem->u.button.bDefault = bDef;
558 dlgitem->WndProc = WndProcBN;
564 CreateRadioButton(t_x11* x11, const char* szLab, bool bSet, t_id id, t_id groupid, int x0, int y0, int w, int h, int bw)
571 h = XTextHeight(x11->font) + OFFS_Y;
575 w = XTextWidth(x11->font, szLab, std::strlen(szLab)) + OFFS_X + h;
577 InitWin(&(dlgitem->win), x0, y0, w, h, bw, szLab);
579 dlgitem->GroupID = groupid;
580 dlgitem->type = edlgRB;
581 dlgitem->u.radiobutton.bSelect = bSet;
582 dlgitem->WndProc = WndProcRB;
588 CreateGroupBox(t_x11* x11, const char* szLab, t_id id, int nitems, t_id items[], int x0, int y0, int w, int h, int bw)
595 h = XTextHeight(x11->font) + OFFS_Y;
599 w = XTextWidth(x11->font, szLab, std::strlen(szLab)) + 2 * OFFS_X;
601 InitWin(&(dlgitem->win), x0, y0, w, h, bw, szLab);
602 dlgitem->GroupID = id;
604 dlgitem->type = edlgGB;
605 dlgitem->u.groupbox.nitems = nitems;
606 snew(dlgitem->u.groupbox.item, nitems);
607 std::memcpy((char*)dlgitem->u.groupbox.item, (char*)items, nitems * sizeof(items[0]));
608 dlgitem->WndProc = WndProcGB;
613 t_dlgitem* CreateCheckBox(t_x11* x11,
615 bool bCheckedInitial,
629 h = XTextHeight(x11->font) + OFFS_Y;
633 w = XTextWidth(x11->font, szLab, std::strlen(szLab)) + OFFS_X + h;
635 InitWin(&(dlgitem->win), x0, y0, w, h, bw, szLab);
637 dlgitem->GroupID = groupid;
638 dlgitem->type = edlgCB;
639 dlgitem->u.checkbox.bChecked = bCheckedInitial;
640 dlgitem->WndProc = WndProcCB;
645 t_dlgitem* CreatePixmap(Pixmap pm, t_id id, t_id /*groupid*/, int x0, int y0, int w, int h, int bw)
650 InitWin(&(dlgitem->win), x0, y0, w, h, bw, nullptr);
652 dlgitem->type = edlgPM;
653 dlgitem->u.pixmap.pm = pm;
654 dlgitem->WndProc = DefWndProc;
659 t_dlgitem* CreateStaticText(t_x11* x11,
661 const char* const* lines,
676 h = (XTextHeight(x11->font) + OFFS_Y) * nlines + OFFS_Y;
680 for (i = 0; (i < nlines); i++)
682 w = std::max(w, XTextWidth(x11->font, lines[i], std::strlen(lines[i])));
686 InitWin(&(dlgitem->win), x0, y0, w, h, bw, nullptr);
688 dlgitem->GroupID = groupid;
689 dlgitem->type = edlgST;
690 dlgitem->u.statictext.nlines = nlines;
691 snew(dlgitem->u.statictext.lines, nlines);
692 for (i = 0; (i < nlines); i++)
694 dlgitem->u.statictext.lines[i] = gmx_strdup(lines[i]);
696 dlgitem->WndProc = WndProcST;
701 t_dlgitem* CreateEditText(t_x11* x11,
719 h = XTextHeight(x11->font) + OFFS_Y;
725 snew(test, screenbuf);
726 std::memset(test, 'w', screenbuf);
727 w = XTextWidth(x11->font, test, screenbuf)
728 + XTextWidth(x11->font, title, std::strlen(title)) + 2 * XCARET + 2 * OFFS_X;
731 InitWin(&(dlgitem->win), x0, y0, w, h, bw, title);
733 dlgitem->GroupID = groupid;
734 dlgitem->type = edlgET;
735 et = &(dlgitem->u.edittext);
736 snew(et->buf, STRLEN);
737 std::strcpy(et->buf, buf);
738 et->buflen = screenbuf;
740 et->bChanged = false;
741 dlgitem->WndProc = WndProcET;
746 #define SC(src) (strlen(src) ? gmx_strdup(src) : NULL)
748 void SetDlgitemOpts(t_dlgitem* dlgitem, bool bUseMon, char* set, char* get, char* help)
750 dlgitem->bUseMon = bUseMon;
751 dlgitem->set = SC(set);
752 dlgitem->get = SC(get);
753 dlgitem->help = SC(help);
755 std::printf("Help is: '%s'\n", dlgitem->help);