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.
48 #include "gromacs/legacyheaders/macros.h"
49 #include "gromacs/utility/cstringutil.h"
50 #include "gromacs/utility/fatalerror.h"
51 #include "gromacs/utility/smalloc.h"
57 static t_dlgitem *newitem(void)
66 /*****************************
68 * Window Procedures and helpful functions
70 ****************************/
71 static void ShowCaret(t_x11 *x11, t_dlgitem *dlgitem)
75 if (dlgitem->type == edlgET)
79 et = &(dlgitem->u.edittext);
80 x = XTextWidth(x11->font, dlgitem->win.text, strlen(dlgitem->win.text))+XCARET+
81 XTextWidth(x11->font, (char*) &(et->buf[et->strbegin]), et->pos);
82 y1 = (dlgitem->win.height-XTextHeight(x11->font))/2;
83 y2 = (dlgitem->win.height-y1);
85 XDrawLine(x11->disp, dlgitem->win.self, x11->gc, x-XCARET, y1, x+XCARET, y1);
86 XDrawLine(x11->disp, dlgitem->win.self, x11->gc, x, y1, x, y2);
87 XDrawLine(x11->disp, dlgitem->win.self, x11->gc, x-XCARET, y2, x+XCARET, y2);
91 static void HideCaret(t_x11 *x11, t_dlgitem *dlgitem)
93 XSetForeground(x11->disp, x11->gc, x11->bg);
94 ShowCaret(x11, dlgitem);
95 XSetForeground(x11->disp, x11->gc, x11->fg);
98 static int DefWndProc(t_x11 *x11, t_dlgitem *dlgitem, XEvent *event)
100 XComposeStatus status;
105 printf("DefWndProc\n");
112 if (HelpPressed(event))
118 XLookupString(&(event->xkey), c, BUFSIZE, &keysym, &status);
119 if ((keysym == XK_Return) || (keysym == XK_KP_Enter))
126 dlgitem->win.bFocus = true;
127 ShowCaret(x11, dlgitem);
128 /* LightBorder(x11->disp,dlgitem->win.self,x11->fg); */
131 dlgitem->win.bFocus = false;
132 HideCaret(x11, dlgitem);
133 /* LightBorder(x11->disp,dlgitem->win.self,x11->bg); */
136 XBell(x11->disp, 50);
141 static int WndProcBN(t_x11 *x11, t_dlgitem *dlgitem, XEvent *event)
146 if (dlgitem->type != edlgBN)
148 gmx_incons("button processing");
150 win = &(dlgitem->win);
151 w = XTextWidth(x11->font, win->text, strlen(win->text));
152 x = (win->width-w)/2;
153 th = XTextHeight(x11->font)+OFFS_Y;
157 RectWin(x11->disp, x11->gc, win, x11->fg);
158 TextInRect(x11, win->self, win->text, 0, 0, win->width, th, eXCenter, eYCenter);
163 XDrawLine(x11->disp, win->self, x11->gc, x-1, th, x+w, th);
166 XSetForeground(x11->disp, x11->gc, x11->bg);
167 XDrawLine(x11->disp, win->self, x11->gc, x-1, th, x+w, th);
168 XSetForeground(x11->disp, x11->gc, x11->fg);
171 return DefWndProc(x11, dlgitem, event);
176 static int WndProcRB(t_x11 *x11, t_dlgitem *dlgitem, XEvent *event)
182 if (dlgitem->type != edlgRB)
184 gmx_incons("radiobutton processing");
186 rb = &(dlgitem->u.radiobutton);
187 win = &(dlgitem->win);
195 XClearArea(x11->disp, win->self, x-rad, y-rad, x+rad, y+rad, False);
199 XFillCircle(x11->disp, win->self, x11->gc, x, y, rad);
201 XDrawCircle(x11->disp, win->self, x11->gc, x, y, rad);
203 TextInRect(x11, win->self, win->text, x, 0, win->width-x, win->height,
211 XBell(x11->disp, 50);
217 return DefWndProc(x11, dlgitem, event);
222 static int WndProcGB(t_x11 *x11, t_dlgitem *dlgitem, XEvent *event)
227 if (dlgitem->type != edlgGB)
229 gmx_incons("gb processing");
231 win = &(dlgitem->win);
233 x = XTextWidth(x11->font, win->text, strlen(win->text));
234 y = XTextHeight(x11->font);
238 XSetForeground(x11->disp, x11->gc, x11->fg);
239 XDrawRoundRect(x11->disp, win->self, x11->gc, 0, y/2,
240 win->width-1, win->height-y/2-1);
241 XClearArea(x11->disp, win->self, OFFS_X, 0, x+OFFS_X, y, False);
242 TextInRect(x11, win->self, win->text, 2*OFFS_X, 0, x, y, eXCenter, eYCenter);
248 return DefWndProc(x11, dlgitem, event);
253 static int WndProcCB(t_x11 *x11, t_dlgitem *dlgitem, XEvent *event)
259 if (dlgitem->type != edlgCB)
261 gmx_incons("check box processing");
263 cb = &(dlgitem->u.checkbox);
264 win = &(dlgitem->win);
273 XSetForeground(x11->disp, x11->gc, x11->fg);
274 XClearArea(x11->disp, win->self, x, y, w, h, False);
275 XDrawRectangle(x11->disp, win->self, x11->gc, x, y, w, h);
278 XDrawLine(x11->disp, win->self, x11->gc, x, y, x+w, y+h);
279 XDrawLine(x11->disp, win->self, x11->gc, x+w, y, x, y+h);
282 TextInRect(x11, win->self, win->text, x, 0, win->width-x, win->height,
286 cb->bChecked = !cb->bChecked;
292 return DefWndProc(x11, dlgitem, event);
297 static int WndProcST(t_x11 *x11, t_dlgitem *dlgitem, XEvent *event)
303 if (dlgitem->type != edlgST)
305 gmx_incons("st processing");
307 st = &(dlgitem->u.statictext);
308 win = &(dlgitem->win);
313 dy = XTextHeight(x11->font)+OFFS_Y;
314 for (i = 0; (i < st->nlines); i++)
316 TextInRect(x11, win->self, st->lines[i],
317 0, OFFS_Y+i*dy, win->width, dy, eXLeft, eYCenter);
321 return DefWndProc(x11, dlgitem, event);
326 static bool insert(char *s, char c, int *pos)
333 /* +1 for zero termination */
334 for (i = sl+1; (i > *pos); i--)
345 static bool my_backspace(char *s, int *pos)
350 if ((sl > 0) && ((*pos) > 0))
352 for (i = *pos-1; (i < sl); i++)
356 (*pos) = std::max(0, (*pos)-1);
362 static bool my_delete(char *s, int *pos)
367 if ((sl > 0) && ((*pos) < sl))
369 for (i = *pos; (i < sl); i++)
378 static int WndProcET(t_x11 *x11, t_dlgitem *dlgitem, XEvent *event)
383 char c[BUFSIZE+1], *bp;
386 int xp, xtitle, ewidth;
388 if (dlgitem->type != edlgET)
390 gmx_incons("st processing");
392 et = &(dlgitem->u.edittext);
393 win = &(dlgitem->win);
395 /* Copy string part that is visible into screen buffer */
396 for (i = 0; (i < et->buflen); i++)
398 scrbuf[i] = et->buf[i+et->strbegin];
405 XSetForeground(x11->disp, x11->gc, x11->fg);
406 xtitle = XTextWidth(x11->font, win->text, strlen(win->text));
407 ewidth = win->width-xtitle;
408 TextInRect(x11, win->self, win->text,
409 0, 0, xtitle-1, win->height, eXLeft, eYCenter);
410 XClearArea(x11->disp, win->self, xtitle, 0, ewidth+XCARET, win->height, False);
411 TextInRect(x11, win->self, scrbuf,
412 xtitle+XCARET, 0, ewidth, win->height, eXLeft, eYCenter);
418 ShowCaret(x11, dlgitem);
422 /* Calculate new position for caret */
423 et->pos = strlen(et->buf);
424 bp = gmx_strdup(et->buf);
425 xp = event->xbutton.x-XTextWidth(x11->font, win->text, strlen(win->text))-
427 while ((et->pos > 0) && (XTextWidth(x11->font, bp, strlen(bp)) > xp))
436 /* Check for HelpKey */
437 if (HelpPressed(event))
439 return DefWndProc(x11, dlgitem, event);
441 XLookupString(&(event->xkey), c, BUFSIZE, &keysym, NULL);
443 printf("Keysym: %x\n", keysym);
448 if (my_delete(et->buf, &(et->pos)))
455 XBell(x11->disp, 50);
459 if (my_backspace(et->buf, &(et->pos)))
466 XBell(x11->disp, 50);
478 if (strlen(et->buf) <= (unsigned int)et->buflen)
480 et->pos = strlen(et->buf);
484 et->pos = et->buflen;
485 et->strbegin = strlen(et->buf)-et->buflen;
490 et->pos = std::max(0, et->pos-1);
491 et->strbegin = std::min(et->strbegin, et->pos);
495 if ((et->pos < et->buflen) &&
496 (et->strbegin+et->buflen > (int)strlen(et->buf)))
500 else if ((et->buflen < (int)strlen(et->buf)) &&
501 (et->strbegin < (int)strlen(et->buf)-et->buflen))
514 if (insert(et->buf, c[0], &(et->pos)))
520 XBell(x11->disp, 50);
526 HideCaret(x11, dlgitem);
529 et->bChanged = false;
533 return DefWndProc(x11, dlgitem, event);
538 /*****************************
540 * Routines to create dialog items, all items have an id
541 * which you can use to extract info. It is possible to have
542 * multiple items with the same id but it may then not be possible
543 * to extract information.
544 * All routines take the position relative to the parent dlg
545 * and the size and border width.
546 * If the width and height are set to zero initially, they will
547 * be calculated and set by the routine. With the dlgitem manipulation
548 * routines listed below, the application can then move the items around
549 * on the dlg box, and if wished resize them.
551 ****************************/
552 t_dlgitem *CreateButton(t_x11 *x11,
553 const char *szLab, bool bDef, t_id id, t_id groupid,
554 int x0, int y0, int w, int h, int bw)
562 h = XTextHeight(x11->font)+2*OFFS_Y;
566 w = XTextWidth(x11->font, szLab, strlen(szLab))+2*OFFS_X;
570 snew(lab, strlen(szLab)+7); /* 6 for >> << and 1 for \0 */
571 sprintf(lab, ">> %s <<", szLab);
575 lab = gmx_strdup(szLab);
577 InitWin(&(dlgitem->win), x0, y0, w, h, bw, szLab);
580 dlgitem->GroupID = groupid;
581 dlgitem->type = edlgBN;
582 dlgitem->u.button.bDefault = bDef;
583 dlgitem->WndProc = WndProcBN;
588 t_dlgitem *CreateRadioButton(t_x11 *x11,
589 const char *szLab, bool bSet, t_id id,
591 int x0, int y0, int w, int h, int bw)
598 h = XTextHeight(x11->font)+OFFS_Y;
602 w = XTextWidth(x11->font, szLab, strlen(szLab))+OFFS_X+h;
604 InitWin(&(dlgitem->win), x0, y0, w, h, bw, szLab);
606 dlgitem->GroupID = groupid;
607 dlgitem->type = edlgRB;
608 dlgitem->u.radiobutton.bSelect = bSet;
609 dlgitem->WndProc = WndProcRB;
614 t_dlgitem *CreateGroupBox(t_x11 *x11,
615 const char *szLab, t_id id,
616 int nitems, t_id items[],
617 int x0, int y0, int w, int h, int bw)
624 h = XTextHeight(x11->font)+OFFS_Y;
628 w = XTextWidth(x11->font, szLab, strlen(szLab))+2*OFFS_X;
630 InitWin(&(dlgitem->win), x0, y0, w, h, bw, szLab);
631 dlgitem->GroupID = id;
633 dlgitem->type = edlgGB;
634 dlgitem->u.groupbox.nitems = nitems;
635 snew(dlgitem->u.groupbox.item, nitems);
636 memcpy((char *)dlgitem->u.groupbox.item, (char *)items,
637 nitems*sizeof(items[0]));
638 dlgitem->WndProc = WndProcGB;
643 t_dlgitem *CreateCheckBox(t_x11 *x11,
644 const char *szLab, bool bCheckedInitial, t_id id,
646 int x0, int y0, int w, int h, int bw)
653 h = XTextHeight(x11->font)+OFFS_Y;
657 w = XTextWidth(x11->font, szLab, strlen(szLab))+OFFS_X+h;
659 InitWin(&(dlgitem->win), x0, y0, w, h, bw, szLab);
661 dlgitem->GroupID = groupid;
662 dlgitem->type = edlgCB;
663 dlgitem->u.checkbox.bChecked = bCheckedInitial;
664 dlgitem->WndProc = WndProcCB;
669 t_dlgitem *CreatePixmap(Pixmap pm, t_id id,
670 t_id /*groupid*/, int x0, int y0, int w, int h, int bw)
675 InitWin(&(dlgitem->win), x0, y0, w, h, bw, NULL);
677 dlgitem->type = edlgPM;
678 dlgitem->u.pixmap.pm = pm;
679 dlgitem->WndProc = DefWndProc;
684 t_dlgitem *CreateStaticText(t_x11 *x11,
685 int nlines, const char * const *lines, t_id id,
687 int x0, int y0, int w, int h, int bw)
695 h = (XTextHeight(x11->font)+OFFS_Y)*nlines+OFFS_Y;
699 for (i = 0; (i < nlines); i++)
701 w = std::max(w, XTextWidth(x11->font, lines[i], strlen(lines[i])));
705 InitWin(&(dlgitem->win), x0, y0, w, h, bw, NULL);
707 dlgitem->GroupID = groupid;
708 dlgitem->type = edlgST;
709 dlgitem->u.statictext.nlines = nlines;
710 snew(dlgitem->u.statictext.lines, nlines);
711 for (i = 0; (i < nlines); i++)
713 dlgitem->u.statictext.lines[i] = gmx_strdup(lines[i]);
715 dlgitem->WndProc = WndProcST;
720 t_dlgitem *CreateEditText(t_x11 *x11,
722 int screenbuf, char *buf, t_id id, t_id groupid,
723 int x0, int y0, int w, int h, int bw)
731 h = XTextHeight(x11->font)+OFFS_Y;
737 snew(test, screenbuf);
738 memset(test, 'w', screenbuf);
739 w = XTextWidth(x11->font, test, screenbuf)+
740 XTextWidth(x11->font, title, strlen(title))+
744 InitWin(&(dlgitem->win), x0, y0, w, h, bw, title);
746 dlgitem->GroupID = groupid;
747 dlgitem->type = edlgET;
748 et = &(dlgitem->u.edittext);
749 snew(et->buf, STRLEN);
750 strcpy(et->buf, buf);
751 et->buflen = screenbuf;
753 et->bChanged = false;
754 dlgitem->WndProc = WndProcET;
759 #define SC(src) (strlen(src) ? gmx_strdup(src) : NULL)
761 void SetDlgitemOpts(t_dlgitem *dlgitem, bool bUseMon,
762 char *set, char *get, char *help)
764 dlgitem->bUseMon = bUseMon;
765 dlgitem->set = SC(set);
766 dlgitem->get = SC(get);
767 dlgitem->help = SC(help);
769 printf("Help is: '%s'\n", dlgitem->help);