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,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 "xdlgitem.h"
40 
41 #include <cctype>
42 #include <cstdio>
43 #include <cstring>
44 
45 #include <algorithm>
46 
47 #include "gromacs/utility/cstringutil.h"
48 #include "gromacs/utility/fatalerror.h"
49 #include "gromacs/utility/smalloc.h"
50 
51 #include "Xstuff.h"
52 
53 #define BUFSIZE 16
54 
newitem(void)55 static t_dlgitem* newitem(void)
56 {
57     t_dlgitem* item;
58 
59     snew(item, 1);
60 
61     return item;
62 }
63 
64 /*****************************
65  *
66  * Window Procedures and helpful functions
67  *
68  ****************************/
ShowCaret(t_x11 * x11,t_dlgitem * dlgitem)69 static void ShowCaret(t_x11* x11, t_dlgitem* dlgitem)
70 {
71     t_edittext* et;
72 
73     if (dlgitem->type == edlgET)
74     {
75         int x, y1, y2;
76 
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);
82         y1--, y2++;
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);
86     }
87 }
88 
HideCaret(t_x11 * x11,t_dlgitem * dlgitem)89 static void HideCaret(t_x11* x11, t_dlgitem* dlgitem)
90 {
91     XSetForeground(x11->disp, x11->gc, x11->bg);
92     ShowCaret(x11, dlgitem);
93     XSetForeground(x11->disp, x11->gc, x11->fg);
94 }
95 
DefWndProc(t_x11 * x11,t_dlgitem * dlgitem,XEvent * event)96 static int DefWndProc(t_x11* x11, t_dlgitem* dlgitem, XEvent* event)
97 {
98     XComposeStatus status;
99     KeySym         keysym;
100     char           c[BUFSIZE + 1];
101 
102 #ifdef DEBUG
103     std::printf("DefWndProc\n");
104 #endif
105     switch (event->type)
106     {
107         case Expose:
108         case ButtonPress:
109         case KeyPress:
110             if (HelpPressed(event))
111             {
112                 return HELPPRESSED;
113             }
114             else
115             {
116                 XLookupString(&(event->xkey), c, BUFSIZE, &keysym, &status);
117                 if ((keysym == XK_Return) || (keysym == XK_KP_Enter))
118                 {
119                     return ENTERPRESSED;
120                 }
121             }
122             break;
123         case EnterNotify:
124             dlgitem->win.bFocus = true;
125             ShowCaret(x11, dlgitem);
126             /*    LightBorder(x11->disp,dlgitem->win.self,x11->fg); */
127             break;
128         case LeaveNotify:
129             dlgitem->win.bFocus = false;
130             HideCaret(x11, dlgitem);
131             /*    LightBorder(x11->disp,dlgitem->win.self,x11->bg); */
132             break;
133         default: XBell(x11->disp, 50);
134     }
135     return ITEMOK;
136 }
137 
WndProcBN(t_x11 * x11,t_dlgitem * dlgitem,XEvent * event)138 static int WndProcBN(t_x11* x11, t_dlgitem* dlgitem, XEvent* event)
139 {
140     t_windata* win;
141     int        x, w, th;
142 
143     if (dlgitem->type != edlgBN)
144     {
145         gmx_incons("button processing");
146     }
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;
151     switch (event->type)
152     {
153         case Expose:
154             RectWin(x11->disp, x11->gc, win, x11->fg);
155             TextInRect(x11, win->self, win->text, 0, 0, win->width, th, eXCenter, eYCenter);
156             break;
157         case ButtonPress: return BNPRESSED;
158         case EnterNotify: XDrawLine(x11->disp, win->self, x11->gc, x - 1, th, x + w, th); break;
159         case LeaveNotify:
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);
163             break;
164         default: return DefWndProc(x11, dlgitem, event);
165     }
166     return ITEMOK;
167 }
168 
WndProcRB(t_x11 * x11,t_dlgitem * dlgitem,XEvent * event)169 static int WndProcRB(t_x11* x11, t_dlgitem* dlgitem, XEvent* event)
170 {
171     t_radiobutton* rb;
172     t_windata*     win;
173     int            x, y, rad;
174 
175     if (dlgitem->type != edlgRB)
176     {
177         gmx_incons("radiobutton processing");
178     }
179     rb  = &(dlgitem->u.radiobutton);
180     win = &(dlgitem->win);
181 
182     rad = win->height / 3;
183     x   = rad;
184     y   = win->height / 2;
185     switch (event->type)
186     {
187         case Expose:
188             XClearArea(x11->disp, win->self, x - rad, y - rad, x + rad, y + rad, False);
189             if (rb->bSelect)
190             {
191                 /* Filled */
192                 XFillCircle(x11->disp, win->self, x11->gc, x, y, rad);
193             }
194             XDrawCircle(x11->disp, win->self, x11->gc, x, y, rad);
195             x += rad + OFFS_X;
196             TextInRect(x11, win->self, win->text, x, 0, win->width - x, win->height, eXLeft, eYCenter);
197             break;
198         case ButtonPress:
199             if (!rb->bSelect)
200             {
201                 return RBPRESSED;
202             }
203             XBell(x11->disp, 50);
204             break;
205         case EnterNotify:
206         case LeaveNotify: break;
207         default: return DefWndProc(x11, dlgitem, event);
208     }
209     return ITEMOK;
210 }
211 
WndProcGB(t_x11 * x11,t_dlgitem * dlgitem,XEvent * event)212 static int WndProcGB(t_x11* x11, t_dlgitem* dlgitem, XEvent* event)
213 {
214     t_windata* win;
215     int        x, y;
216 
217     if (dlgitem->type != edlgGB)
218     {
219         gmx_incons("gb processing");
220     }
221     win = &(dlgitem->win);
222 
223     x = XTextWidth(x11->font, win->text, std::strlen(win->text));
224     y = XTextHeight(x11->font);
225     switch (event->type)
226     {
227         case Expose:
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);
233             break;
234         case EnterNotify:
235         case LeaveNotify: break;
236         default: return DefWndProc(x11, dlgitem, event);
237     }
238     return ITEMOK;
239 }
240 
WndProcCB(t_x11 * x11,t_dlgitem * dlgitem,XEvent * event)241 static int WndProcCB(t_x11* x11, t_dlgitem* dlgitem, XEvent* event)
242 {
243     t_checkbox* cb;
244     t_windata*  win;
245     int         x, y, w, h;
246 
247     if (dlgitem->type != edlgCB)
248     {
249         gmx_incons("check box processing");
250     }
251     cb  = &(dlgitem->u.checkbox);
252     win = &(dlgitem->win);
253 
254     x = 0;
255     y = win->height / 7;
256     w = 5 * y;
257     h = 5 * y;
258     switch (event->type)
259     {
260         case Expose:
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);
264             if (cb->bChecked)
265             {
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);
268             }
269             x = w + OFFS_X;
270             TextInRect(x11, win->self, win->text, x, 0, win->width - x, win->height, eXLeft, eYCenter);
271             break;
272         case ButtonPress: cb->bChecked = !cb->bChecked; return CBPRESSED;
273         case EnterNotify:
274         case LeaveNotify: break;
275         default: return DefWndProc(x11, dlgitem, event);
276     }
277     return ITEMOK;
278 }
279 
WndProcST(t_x11 * x11,t_dlgitem * dlgitem,XEvent * event)280 static int WndProcST(t_x11* x11, t_dlgitem* dlgitem, XEvent* event)
281 {
282     t_statictext* st;
283     t_windata*    win;
284     int           i, dy;
285 
286     if (dlgitem->type != edlgST)
287     {
288         gmx_incons("st processing");
289     }
290     st  = &(dlgitem->u.statictext);
291     win = &(dlgitem->win);
292 
293     switch (event->type)
294     {
295         case Expose:
296             dy = XTextHeight(x11->font) + OFFS_Y;
297             for (i = 0; (i < st->nlines); i++)
298             {
299                 TextInRect(x11, win->self, st->lines[i], 0, OFFS_Y + i * dy, win->width, dy, eXLeft,
300                            eYCenter);
301             }
302             break;
303         default: return DefWndProc(x11, dlgitem, event);
304     }
305     return ITEMOK;
306 }
307 
insert(char * s,char c,int * pos)308 static bool insert(char* s, char c, int* pos)
309 {
310     int i, sl;
311 
312     if (isprint(c))
313     {
314         sl = std::strlen(s);
315         /* +1 for zero termination */
316         for (i = sl + 1; (i > *pos); i--)
317         {
318             s[i + 1] = s[i];
319         }
320         s[*pos] = c;
321         (*pos)++;
322         return true;
323     }
324     return false;
325 }
326 
my_backspace(char * s,int * pos)327 static bool my_backspace(char* s, int* pos)
328 {
329     int i, sl;
330 
331     sl = std::strlen(s);
332     if ((sl > 0) && ((*pos) > 0))
333     {
334         for (i = *pos - 1; (i < sl); i++)
335         {
336             s[i] = s[i + 1];
337         }
338         (*pos) = std::max(0, (*pos) - 1);
339         return true;
340     }
341     return false;
342 }
343 
my_delete(char * s,int * pos)344 static bool my_delete(char* s, int* pos)
345 {
346     int i, sl;
347 
348     sl = std::strlen(s);
349     if ((sl > 0) && ((*pos) < sl))
350     {
351         for (i = *pos; (i < sl); i++)
352         {
353             s[i] = s[i + 1];
354         }
355         return true;
356     }
357     return false;
358 }
359 
WndProcET(t_x11 * x11,t_dlgitem * dlgitem,XEvent * event)360 static int WndProcET(t_x11* x11, t_dlgitem* dlgitem, XEvent* event)
361 {
362     t_edittext* et;
363     t_windata*  win;
364     KeySym      keysym;
365     char        c[BUFSIZE + 1], *bp;
366     char        scrbuf[STRLEN];
367     int         i;
368     int         xp, xtitle, ewidth;
369 
370     if (dlgitem->type != edlgET)
371     {
372         gmx_incons("st processing");
373     }
374     et  = &(dlgitem->u.edittext);
375     win = &(dlgitem->win);
376 
377     /* Copy string part that is visible into screen buffer */
378     for (i = 0; (i < et->buflen); i++)
379     {
380         scrbuf[i] = et->buf[i + et->strbegin];
381     }
382     scrbuf[i] = '\0';
383 
384     switch (event->type)
385     {
386         case Expose:
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);
393 #ifdef DEBUG
394             std::printf("Expose\n");
395 #endif
396             if (win->bFocus)
397             {
398                 ShowCaret(x11, dlgitem);
399             }
400             break;
401         case ButtonPress:
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))
407             {
408                 et->pos--;
409                 bp[et->pos] = '\0';
410             }
411             sfree(bp);
412             et->bChanged = true;
413             return ETCHANGED;
414         case KeyPress:
415             /* Check for HelpKey */
416             if (HelpPressed(event))
417             {
418                 return DefWndProc(x11, dlgitem, event);
419             }
420             XLookupString(&(event->xkey), c, BUFSIZE, &keysym, nullptr);
421 #ifdef DEBUG
422             std::printf("Keysym: %x\n", keysym);
423 #endif
424             switch (keysym)
425             {
426                 case XK_Delete:
427                     if (my_delete(et->buf, &(et->pos)))
428                     {
429                         et->bChanged = true;
430                         return ETCHANGED;
431                     }
432                     else
433                     {
434                         XBell(x11->disp, 50);
435                     }
436                     break;
437                 case XK_BackSpace:
438                     if (my_backspace(et->buf, &(et->pos)))
439                     {
440                         et->bChanged = true;
441                         return ETCHANGED;
442                     }
443                     else
444                     {
445                         XBell(x11->disp, 50);
446                     }
447                     break;
448                 case XK_KP_Enter:
449                 case XK_Return: return ENTERPRESSED;
450                 case XK_Home:
451                     et->pos      = 0;
452                     et->strbegin = 0;
453                     et->bChanged = true;
454                     return ETCHANGED;
455                 case XK_End:
456                     if (strlen(et->buf) <= (unsigned int)et->buflen)
457                     {
458                         et->pos = std::strlen(et->buf);
459                     }
460                     else
461                     {
462                         et->pos      = et->buflen;
463                         et->strbegin = std::strlen(et->buf) - et->buflen;
464                     }
465                     et->bChanged = true;
466                     return ETCHANGED;
467                 case XK_Left:
468                     et->pos      = std::max(0, et->pos - 1);
469                     et->strbegin = std::min(et->strbegin, et->pos);
470                     et->bChanged = true;
471                     return ETCHANGED;
472                 case XK_Right:
473                     if ((et->pos < et->buflen) && (et->strbegin + et->buflen > (int)strlen(et->buf)))
474                     {
475                         et->pos++;
476                     }
477                     else if ((et->buflen < (int)strlen(et->buf))
478                              && (et->strbegin < (int)strlen(et->buf) - et->buflen))
479                     {
480                         et->strbegin++;
481                     }
482                     else
483                     {
484                         break;
485                     }
486                     et->bChanged = true;
487                     return ETCHANGED;
488                 default:
489                     if (keysym < 256)
490                     {
491                         if (insert(et->buf, c[0], &(et->pos)))
492                         {
493                             et->bChanged = true;
494                             return ETCHANGED;
495                         }
496                     }
497                     XBell(x11->disp, 50);
498                     break;
499             }
500             break;
501         case LeaveNotify:
502             win->bFocus = false;
503             HideCaret(x11, dlgitem);
504             if (et->bChanged)
505             {
506                 et->bChanged = false;
507             }
508             break;
509         default: return DefWndProc(x11, dlgitem, event);
510     }
511     return ITEMOK;
512 }
513 
514 /*****************************
515  *
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.
526  *
527  ****************************/
528 t_dlgitem*
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)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)
530 {
531     t_dlgitem* dlgitem;
532     char*      lab;
533 
534     dlgitem = newitem();
535     if (h == 0)
536     {
537         h = XTextHeight(x11->font) + 2 * OFFS_Y;
538     }
539     if (w == 0)
540     {
541         w = XTextWidth(x11->font, szLab, std::strlen(szLab)) + 2 * OFFS_X;
542     }
543     if (bDef)
544     {
545         snew(lab, std::strlen(szLab) + 7); /* 6 for >> << and 1 for \0 */
546         std::sprintf(lab, ">> %s <<", szLab);
547     }
548     else
549     {
550         lab = gmx_strdup(szLab);
551     }
552     InitWin(&(dlgitem->win), x0, y0, w, h, bw, szLab);
553     sfree(lab);
554     dlgitem->ID                = id;
555     dlgitem->GroupID           = groupid;
556     dlgitem->type              = edlgBN;
557     dlgitem->u.button.bDefault = bDef;
558     dlgitem->WndProc           = WndProcBN;
559 
560     return dlgitem;
561 }
562 
563 t_dlgitem*
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)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)
565 {
566     t_dlgitem* dlgitem;
567 
568     dlgitem = newitem();
569     if (h == 0)
570     {
571         h = XTextHeight(x11->font) + OFFS_Y;
572     }
573     if (w == 0)
574     {
575         w = XTextWidth(x11->font, szLab, std::strlen(szLab)) + OFFS_X + h;
576     }
577     InitWin(&(dlgitem->win), x0, y0, w, h, bw, szLab);
578     dlgitem->ID                    = id;
579     dlgitem->GroupID               = groupid;
580     dlgitem->type                  = edlgRB;
581     dlgitem->u.radiobutton.bSelect = bSet;
582     dlgitem->WndProc               = WndProcRB;
583 
584     return dlgitem;
585 }
586 
587 t_dlgitem*
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)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)
589 {
590     t_dlgitem* dlgitem;
591 
592     dlgitem = newitem();
593     if (h == 0)
594     {
595         h = XTextHeight(x11->font) + OFFS_Y;
596     }
597     if (w == 0)
598     {
599         w = XTextWidth(x11->font, szLab, std::strlen(szLab)) + 2 * OFFS_X;
600     }
601     InitWin(&(dlgitem->win), x0, y0, w, h, bw, szLab);
602     dlgitem->GroupID           = id;
603     dlgitem->ID                = 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;
609 
610     return dlgitem;
611 }
612 
CreateCheckBox(t_x11 * x11,const char * szLab,bool bCheckedInitial,t_id id,t_id groupid,int x0,int y0,int w,int h,int bw)613 t_dlgitem* CreateCheckBox(t_x11*      x11,
614                           const char* szLab,
615                           bool        bCheckedInitial,
616                           t_id        id,
617                           t_id        groupid,
618                           int         x0,
619                           int         y0,
620                           int         w,
621                           int         h,
622                           int         bw)
623 {
624     t_dlgitem* dlgitem;
625 
626     dlgitem = newitem();
627     if (h == 0)
628     {
629         h = XTextHeight(x11->font) + OFFS_Y;
630     }
631     if (w == 0)
632     {
633         w = XTextWidth(x11->font, szLab, std::strlen(szLab)) + OFFS_X + h;
634     }
635     InitWin(&(dlgitem->win), x0, y0, w, h, bw, szLab);
636     dlgitem->ID                  = id;
637     dlgitem->GroupID             = groupid;
638     dlgitem->type                = edlgCB;
639     dlgitem->u.checkbox.bChecked = bCheckedInitial;
640     dlgitem->WndProc             = WndProcCB;
641 
642     return dlgitem;
643 }
644 
CreatePixmap(Pixmap pm,t_id id,t_id,int x0,int y0,int w,int h,int bw)645 t_dlgitem* CreatePixmap(Pixmap pm, t_id id, t_id /*groupid*/, int x0, int y0, int w, int h, int bw)
646 {
647     t_dlgitem* dlgitem;
648 
649     dlgitem = newitem();
650     InitWin(&(dlgitem->win), x0, y0, w, h, bw, nullptr);
651     dlgitem->ID          = id;
652     dlgitem->type        = edlgPM;
653     dlgitem->u.pixmap.pm = pm;
654     dlgitem->WndProc     = DefWndProc;
655 
656     return dlgitem;
657 }
658 
CreateStaticText(t_x11 * x11,int nlines,const char * const * lines,t_id id,t_id groupid,int x0,int y0,int w,int h,int bw)659 t_dlgitem* CreateStaticText(t_x11*             x11,
660                             int                nlines,
661                             const char* const* lines,
662                             t_id               id,
663                             t_id               groupid,
664                             int                x0,
665                             int                y0,
666                             int                w,
667                             int                h,
668                             int                bw)
669 {
670     t_dlgitem* dlgitem;
671     int        i;
672 
673     dlgitem = newitem();
674     if (h == 0)
675     {
676         h = (XTextHeight(x11->font) + OFFS_Y) * nlines + OFFS_Y;
677     }
678     if (w == 0)
679     {
680         for (i = 0; (i < nlines); i++)
681         {
682             w = std::max(w, XTextWidth(x11->font, lines[i], std::strlen(lines[i])));
683         }
684         w += 2 * OFFS_X;
685     }
686     InitWin(&(dlgitem->win), x0, y0, w, h, bw, nullptr);
687     dlgitem->ID                  = id;
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++)
693     {
694         dlgitem->u.statictext.lines[i] = gmx_strdup(lines[i]);
695     }
696     dlgitem->WndProc = WndProcST;
697 
698     return dlgitem;
699 }
700 
CreateEditText(t_x11 * x11,const char * title,int screenbuf,char * buf,t_id id,t_id groupid,int x0,int y0,int w,int h,int bw)701 t_dlgitem* CreateEditText(t_x11*      x11,
702                           const char* title,
703                           int         screenbuf,
704                           char*       buf,
705                           t_id        id,
706                           t_id        groupid,
707                           int         x0,
708                           int         y0,
709                           int         w,
710                           int         h,
711                           int         bw)
712 {
713     t_dlgitem*  dlgitem;
714     t_edittext* et;
715 
716     dlgitem = newitem();
717     if (h == 0)
718     {
719         h = XTextHeight(x11->font) + OFFS_Y;
720     }
721     if (w == 0)
722     {
723         char* test;
724 
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;
729         sfree(test);
730     }
731     InitWin(&(dlgitem->win), x0, y0, w, h, bw, title);
732     dlgitem->ID      = id;
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;
739     et->strbegin     = 0;
740     et->bChanged     = false;
741     dlgitem->WndProc = WndProcET;
742 
743     return dlgitem;
744 }
745 
746 #define SC(src) (strlen(src) ? gmx_strdup(src) : NULL)
747 
SetDlgitemOpts(t_dlgitem * dlgitem,bool bUseMon,char * set,char * get,char * help)748 void SetDlgitemOpts(t_dlgitem* dlgitem, bool bUseMon, char* set, char* get, char* help)
749 {
750     dlgitem->bUseMon = bUseMon;
751     dlgitem->set     = SC(set);
752     dlgitem->get     = SC(get);
753     dlgitem->help    = SC(help);
754 #ifdef DEBUG
755     std::printf("Help is: '%s'\n", dlgitem->help);
756 #endif
757 }
758