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, 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.
52 #include "gromacs/utility/cstringutil.h"
53 #include "gromacs/utility/fatalerror.h"
54 #include "gromacs/utility/smalloc.h"
58 static t_dlgitem *newitem(void)
67 /*****************************
69 * Window Procedures and helpful functions
71 ****************************/
72 static void ShowCaret(t_x11 *x11, t_dlgitem *dlgitem)
76 if (dlgitem->type == edlgET)
80 et = &(dlgitem->u.edittext);
81 x = XTextWidth(x11->font, dlgitem->win.text, strlen(dlgitem->win.text))+XCARET+
82 XTextWidth(x11->font, (char*) &(et->buf[et->strbegin]), et->pos);
83 y1 = (dlgitem->win.height-XTextHeight(x11->font))/2;
84 y2 = (dlgitem->win.height-y1);
86 XDrawLine(x11->disp, dlgitem->win.self, x11->gc, x-XCARET, y1, x+XCARET, y1);
87 XDrawLine(x11->disp, dlgitem->win.self, x11->gc, x, y1, x, y2);
88 XDrawLine(x11->disp, dlgitem->win.self, x11->gc, x-XCARET, y2, x+XCARET, y2);
92 static void HideCaret(t_x11 *x11, t_dlgitem *dlgitem)
94 XSetForeground(x11->disp, x11->gc, x11->bg);
95 ShowCaret(x11, dlgitem);
96 XSetForeground(x11->disp, x11->gc, x11->fg);
99 static int DefWndProc(t_x11 *x11, t_dlgitem *dlgitem, XEvent *event)
101 XComposeStatus status;
106 printf("DefWndProc\n");
113 if (HelpPressed(event))
119 XLookupString(&(event->xkey), c, BUFSIZE, &keysym, &status);
120 if ((keysym == XK_Return) || (keysym == XK_KP_Enter))
127 dlgitem->win.bFocus = true;
128 ShowCaret(x11, dlgitem);
129 /* LightBorder(x11->disp,dlgitem->win.self,x11->fg); */
132 dlgitem->win.bFocus = false;
133 HideCaret(x11, dlgitem);
134 /* LightBorder(x11->disp,dlgitem->win.self,x11->bg); */
137 XBell(x11->disp, 50);
142 static int WndProcBN(t_x11 *x11, t_dlgitem *dlgitem, XEvent *event)
147 if (dlgitem->type != edlgBN)
149 gmx_incons("button processing");
151 win = &(dlgitem->win);
152 w = XTextWidth(x11->font, win->text, strlen(win->text));
153 x = (win->width-w)/2;
154 th = XTextHeight(x11->font)+OFFS_Y;
158 RectWin(x11->disp, x11->gc, win, x11->fg);
159 TextInRect(x11, win->self, win->text, 0, 0, win->width, th, eXCenter, eYCenter);
164 XDrawLine(x11->disp, win->self, x11->gc, x-1, th, x+w, th);
167 XSetForeground(x11->disp, x11->gc, x11->bg);
168 XDrawLine(x11->disp, win->self, x11->gc, x-1, th, x+w, th);
169 XSetForeground(x11->disp, x11->gc, x11->fg);
172 return DefWndProc(x11, dlgitem, event);
177 static int WndProcRB(t_x11 *x11, t_dlgitem *dlgitem, XEvent *event)
183 if (dlgitem->type != edlgRB)
185 gmx_incons("radiobutton processing");
187 rb = &(dlgitem->u.radiobutton);
188 win = &(dlgitem->win);
196 XClearArea(x11->disp, win->self, x-rad, y-rad, x+rad, y+rad, False);
200 XFillCircle(x11->disp, win->self, x11->gc, x, y, rad);
202 XDrawCircle(x11->disp, win->self, x11->gc, x, y, rad);
204 TextInRect(x11, win->self, win->text, x, 0, win->width-x, win->height,
212 XBell(x11->disp, 50);
218 return DefWndProc(x11, dlgitem, event);
223 static int WndProcGB(t_x11 *x11, t_dlgitem *dlgitem, XEvent *event)
228 if (dlgitem->type != edlgGB)
230 gmx_incons("gb processing");
232 win = &(dlgitem->win);
234 x = XTextWidth(x11->font, win->text, strlen(win->text));
235 y = XTextHeight(x11->font);
239 XSetForeground(x11->disp, x11->gc, x11->fg);
240 XDrawRoundRect(x11->disp, win->self, x11->gc, 0, y/2,
241 win->width-1, win->height-y/2-1);
242 XClearArea(x11->disp, win->self, OFFS_X, 0, x+OFFS_X, y, False);
243 TextInRect(x11, win->self, win->text, 2*OFFS_X, 0, x, y, eXCenter, eYCenter);
249 return DefWndProc(x11, dlgitem, event);
254 static int WndProcCB(t_x11 *x11, t_dlgitem *dlgitem, XEvent *event)
260 if (dlgitem->type != edlgCB)
262 gmx_incons("check box processing");
264 cb = &(dlgitem->u.checkbox);
265 win = &(dlgitem->win);
274 XSetForeground(x11->disp, x11->gc, x11->fg);
275 XClearArea(x11->disp, win->self, x, y, w, h, False);
276 XDrawRectangle(x11->disp, win->self, x11->gc, x, y, w, h);
279 XDrawLine(x11->disp, win->self, x11->gc, x, y, x+w, y+h);
280 XDrawLine(x11->disp, win->self, x11->gc, x+w, y, x, y+h);
283 TextInRect(x11, win->self, win->text, x, 0, win->width-x, win->height,
287 cb->bChecked = !cb->bChecked;
293 return DefWndProc(x11, dlgitem, event);
298 static int WndProcST(t_x11 *x11, t_dlgitem *dlgitem, XEvent *event)
304 if (dlgitem->type != edlgST)
306 gmx_incons("st processing");
308 st = &(dlgitem->u.statictext);
309 win = &(dlgitem->win);
314 dy = XTextHeight(x11->font)+OFFS_Y;
315 for (i = 0; (i < st->nlines); i++)
317 TextInRect(x11, win->self, st->lines[i],
318 0, OFFS_Y+i*dy, win->width, dy, eXLeft, eYCenter);
322 return DefWndProc(x11, dlgitem, event);
327 static bool insert(char *s, char c, int *pos)
334 /* +1 for zero termination */
335 for (i = sl+1; (i > *pos); i--)
346 static bool my_backspace(char *s, int *pos)
351 if ((sl > 0) && ((*pos) > 0))
353 for (i = *pos-1; (i < sl); i++)
357 (*pos) = std::max(0, (*pos)-1);
363 static bool my_delete(char *s, int *pos)
368 if ((sl > 0) && ((*pos) < sl))
370 for (i = *pos; (i < sl); i++)
379 static int WndProcET(t_x11 *x11, t_dlgitem *dlgitem, XEvent *event)
384 char c[BUFSIZE+1], *bp;
387 int xp, xtitle, ewidth;
389 if (dlgitem->type != edlgET)
391 gmx_incons("st processing");
393 et = &(dlgitem->u.edittext);
394 win = &(dlgitem->win);
396 /* Copy string part that is visible into screen buffer */
397 for (i = 0; (i < et->buflen); i++)
399 scrbuf[i] = et->buf[i+et->strbegin];
406 XSetForeground(x11->disp, x11->gc, x11->fg);
407 xtitle = XTextWidth(x11->font, win->text, strlen(win->text));
408 ewidth = win->width-xtitle;
409 TextInRect(x11, win->self, win->text,
410 0, 0, xtitle-1, win->height, eXLeft, eYCenter);
411 XClearArea(x11->disp, win->self, xtitle, 0, ewidth+XCARET, win->height, False);
412 TextInRect(x11, win->self, scrbuf,
413 xtitle+XCARET, 0, ewidth, win->height, eXLeft, eYCenter);
419 ShowCaret(x11, dlgitem);
423 /* Calculate new position for caret */
424 et->pos = strlen(et->buf);
425 bp = strdup(et->buf);
426 xp = event->xbutton.x-XTextWidth(x11->font, win->text, strlen(win->text))-
428 while ((et->pos > 0) && (XTextWidth(x11->font, bp, strlen(bp)) > xp))
437 /* Check for HelpKey */
438 if (HelpPressed(event))
440 return DefWndProc(x11, dlgitem, event);
442 XLookupString(&(event->xkey), c, BUFSIZE, &keysym, NULL);
444 printf("Keysym: %x\n", keysym);
449 if (my_delete(et->buf, &(et->pos)))
456 XBell(x11->disp, 50);
460 if (my_backspace(et->buf, &(et->pos)))
467 XBell(x11->disp, 50);
479 if (strlen(et->buf) <= (unsigned int)et->buflen)
481 et->pos = strlen(et->buf);
485 et->pos = et->buflen;
486 et->strbegin = strlen(et->buf)-et->buflen;
491 et->pos = std::max(0, et->pos-1);
492 et->strbegin = std::min(et->strbegin, et->pos);
496 if ((et->pos < et->buflen) &&
497 (et->strbegin+et->buflen > (int)strlen(et->buf)))
501 else if ((et->buflen < (int)strlen(et->buf)) &&
502 (et->strbegin < (int)strlen(et->buf)-et->buflen))
515 if (insert(et->buf, c[0], &(et->pos)))
521 XBell(x11->disp, 50);
527 HideCaret(x11, dlgitem);
530 et->bChanged = false;
534 return DefWndProc(x11, dlgitem, event);
539 /*****************************
541 * Routines to create dialog items, all items have an id
542 * which you can use to extract info. It is possible to have
543 * multiple items with the same id but it may then not be possible
544 * to extract information.
545 * All routines take the position relative to the parent dlg
546 * and the size and border width.
547 * If the width and height are set to zero initially, they will
548 * be calculated and set by the routine. With the dlgitem manipulation
549 * routines listed below, the application can then move the items around
550 * on the dlg box, and if wished resize them.
552 ****************************/
553 t_dlgitem *CreateButton(t_x11 *x11,
554 const char *szLab, bool bDef, t_id id, t_id groupid,
555 int x0, int y0, int w, int h, int bw)
563 h = XTextHeight(x11->font)+2*OFFS_Y;
567 w = XTextWidth(x11->font, szLab, strlen(szLab))+2*OFFS_X;
571 snew(lab, strlen(szLab)+7); /* 6 for >> << and 1 for \0 */
572 sprintf(lab, ">> %s <<", szLab);
578 InitWin(&(dlgitem->win), x0, y0, w, h, bw, szLab);
581 dlgitem->GroupID = groupid;
582 dlgitem->type = edlgBN;
583 dlgitem->u.button.bDefault = bDef;
584 dlgitem->WndProc = WndProcBN;
589 t_dlgitem *CreateRadioButton(t_x11 *x11,
590 const char *szLab, bool bSet, t_id id,
592 int x0, int y0, int w, int h, int bw)
599 h = XTextHeight(x11->font)+OFFS_Y;
603 w = XTextWidth(x11->font, szLab, strlen(szLab))+OFFS_X+h;
605 InitWin(&(dlgitem->win), x0, y0, w, h, bw, szLab);
607 dlgitem->GroupID = groupid;
608 dlgitem->type = edlgRB;
609 dlgitem->u.radiobutton.bSelect = bSet;
610 dlgitem->WndProc = WndProcRB;
615 t_dlgitem *CreateGroupBox(t_x11 *x11,
616 const char *szLab, t_id id,
617 int nitems, t_id items[],
618 int x0, int y0, int w, int h, int bw)
625 h = XTextHeight(x11->font)+OFFS_Y;
629 w = XTextWidth(x11->font, szLab, strlen(szLab))+2*OFFS_X;
631 InitWin(&(dlgitem->win), x0, y0, w, h, bw, szLab);
632 dlgitem->GroupID = id;
634 dlgitem->type = edlgGB;
635 dlgitem->u.groupbox.nitems = nitems;
636 snew(dlgitem->u.groupbox.item, nitems);
637 memcpy((char *)dlgitem->u.groupbox.item, (char *)items,
638 nitems*sizeof(items[0]));
639 dlgitem->WndProc = WndProcGB;
644 t_dlgitem *CreateCheckBox(t_x11 *x11,
645 const char *szLab, bool bCheckedInitial, t_id id,
647 int x0, int y0, int w, int h, int bw)
654 h = XTextHeight(x11->font)+OFFS_Y;
658 w = XTextWidth(x11->font, szLab, strlen(szLab))+OFFS_X+h;
660 InitWin(&(dlgitem->win), x0, y0, w, h, bw, szLab);
662 dlgitem->GroupID = groupid;
663 dlgitem->type = edlgCB;
664 dlgitem->u.checkbox.bChecked = bCheckedInitial;
665 dlgitem->WndProc = WndProcCB;
670 t_dlgitem *CreatePixmap(Pixmap pm, t_id id,
671 t_id /*groupid*/, int x0, int y0, int w, int h, int bw)
676 InitWin(&(dlgitem->win), x0, y0, w, h, bw, NULL);
678 dlgitem->type = edlgPM;
679 dlgitem->u.pixmap.pm = pm;
680 dlgitem->WndProc = DefWndProc;
685 t_dlgitem *CreateStaticText(t_x11 *x11,
686 int nlines, const char * const *lines, t_id id,
688 int x0, int y0, int w, int h, int bw)
696 h = (XTextHeight(x11->font)+OFFS_Y)*nlines+OFFS_Y;
700 for (i = 0; (i < nlines); i++)
702 w = std::max(w, XTextWidth(x11->font, lines[i], strlen(lines[i])));
706 InitWin(&(dlgitem->win), x0, y0, w, h, bw, NULL);
708 dlgitem->GroupID = groupid;
709 dlgitem->type = edlgST;
710 dlgitem->u.statictext.nlines = nlines;
711 snew(dlgitem->u.statictext.lines, nlines);
712 for (i = 0; (i < nlines); i++)
714 dlgitem->u.statictext.lines[i] = strdup(lines[i]);
716 dlgitem->WndProc = WndProcST;
721 t_dlgitem *CreateEditText(t_x11 *x11,
723 int screenbuf, char *buf, t_id id, t_id groupid,
724 int x0, int y0, int w, int h, int bw)
732 h = XTextHeight(x11->font)+OFFS_Y;
738 snew(test, screenbuf);
739 memset(test, 'w', screenbuf);
740 w = XTextWidth(x11->font, test, screenbuf)+
741 XTextWidth(x11->font, title, strlen(title))+
745 InitWin(&(dlgitem->win), x0, y0, w, h, bw, title);
747 dlgitem->GroupID = groupid;
748 dlgitem->type = edlgET;
749 et = &(dlgitem->u.edittext);
750 snew(et->buf, STRLEN);
751 strcpy(et->buf, buf);
752 et->buflen = screenbuf;
754 et->bChanged = false;
755 dlgitem->WndProc = WndProcET;
760 #define SC(src) (strlen(src) ? strdup(src) : NULL)
762 void SetDlgitemOpts(t_dlgitem *dlgitem, bool bUseMon,
763 char *set, char *get, char *help)
765 dlgitem->bUseMon = bUseMon;
766 dlgitem->set = SC(set);
767 dlgitem->get = SC(get);
768 dlgitem->help = SC(help);
770 printf("Help is: '%s'\n", dlgitem->help);