2 * dialog.c - a reasonably platform-independent mechanism for
3 * describing dialog boxes.
11 #define DEFINE_INTORPTR_FNS
16 int ctrl_path_elements(char *path
)
20 if (*path
== '/') i
++;
26 /* Return the number of matching path elements at the starts of p1 and p2,
27 * or INT_MAX if the paths are identical. */
28 int ctrl_path_compare(char *p1
, char *p2
)
32 if ((*p1
== '/' || *p1
== '\0') &&
33 (*p2
== '/' || *p2
== '\0'))
34 i
++; /* a whole element matches, ooh */
36 return i
; /* mismatch */
39 return INT_MAX
; /* exact match */
42 struct controlbox
*ctrl_new_box(void)
44 struct controlbox
*ret
= snew(struct controlbox
);
46 ret
->nctrlsets
= ret
->ctrlsetsize
= 0;
48 ret
->nfrees
= ret
->freesize
= 0;
54 void ctrl_free_box(struct controlbox
*b
)
58 for (i
= 0; i
< b
->nctrlsets
; i
++) {
59 ctrl_free_set(b
->ctrlsets
[i
]);
61 for (i
= 0; i
< b
->nfrees
; i
++)
68 void ctrl_free_set(struct controlset
*s
)
75 for (i
= 0; i
< s
->ncontrols
; i
++) {
76 ctrl_free(s
->ctrls
[i
]);
83 * Find the index of first controlset in a controlbox for a given
84 * path. If that path doesn't exist, return the index where it
87 static int ctrl_find_set(struct controlbox
*b
, char *path
, int start
)
92 for (i
= 0; i
< b
->nctrlsets
; i
++) {
93 thisone
= ctrl_path_compare(path
, b
->ctrlsets
[i
]->pathname
);
95 * If `start' is true and there exists a controlset with
96 * exactly the path we've been given, we should return the
97 * index of the first such controlset we find. Otherwise,
98 * we should return the index of the first entry in which
99 * _fewer_ path elements match than they did last time.
101 if ((start
&& thisone
== INT_MAX
) || thisone
< last
)
105 return b
->nctrlsets
; /* insert at end */
109 * Find the index of next controlset in a controlbox for a given
110 * path, or -1 if no such controlset exists. If -1 is passed as
111 * input, finds the first.
113 int ctrl_find_path(struct controlbox
*b
, char *path
, int index
)
116 index
= ctrl_find_set(b
, path
, 1);
120 if (index
< b
->nctrlsets
&& !strcmp(path
, b
->ctrlsets
[index
]->pathname
))
126 /* Set up a panel title. */
127 struct controlset
*ctrl_settitle(struct controlbox
*b
,
128 char *path
, char *title
)
131 struct controlset
*s
= snew(struct controlset
);
132 int index
= ctrl_find_set(b
, path
, 1);
133 s
->pathname
= dupstr(path
);
135 s
->boxtitle
= dupstr(title
);
136 s
->ncontrols
= s
->ctrlsize
= 0;
137 s
->ncolumns
= 0; /* this is a title! */
139 if (b
->nctrlsets
>= b
->ctrlsetsize
) {
140 b
->ctrlsetsize
= b
->nctrlsets
+ 32;
141 b
->ctrlsets
= sresize(b
->ctrlsets
, b
->ctrlsetsize
,struct controlset
*);
143 if (index
< b
->nctrlsets
)
144 memmove(&b
->ctrlsets
[index
+1], &b
->ctrlsets
[index
],
145 (b
->nctrlsets
-index
) * sizeof(*b
->ctrlsets
));
146 b
->ctrlsets
[index
] = s
;
151 /* Retrieve a pointer to a controlset, creating it if absent. */
152 struct controlset
*ctrl_getset(struct controlbox
*b
,
153 char *path
, char *name
, char *boxtitle
)
155 struct controlset
*s
;
156 int index
= ctrl_find_set(b
, path
, 1);
157 while (index
< b
->nctrlsets
&&
158 !strcmp(b
->ctrlsets
[index
]->pathname
, path
)) {
159 if (b
->ctrlsets
[index
]->boxname
&&
160 !strcmp(b
->ctrlsets
[index
]->boxname
, name
))
161 return b
->ctrlsets
[index
];
164 s
= snew(struct controlset
);
165 s
->pathname
= dupstr(path
);
166 s
->boxname
= dupstr(name
);
167 s
->boxtitle
= boxtitle ?
dupstr(boxtitle
) : NULL
;
169 s
->ncontrols
= s
->ctrlsize
= 0;
171 if (b
->nctrlsets
>= b
->ctrlsetsize
) {
172 b
->ctrlsetsize
= b
->nctrlsets
+ 32;
173 b
->ctrlsets
= sresize(b
->ctrlsets
, b
->ctrlsetsize
,struct controlset
*);
175 if (index
< b
->nctrlsets
)
176 memmove(&b
->ctrlsets
[index
+1], &b
->ctrlsets
[index
],
177 (b
->nctrlsets
-index
) * sizeof(*b
->ctrlsets
));
178 b
->ctrlsets
[index
] = s
;
183 /* Allocate some private data in a controlbox. */
184 void *ctrl_alloc(struct controlbox
*b
, size_t size
)
188 * This is an internal allocation routine, so it's allowed to
189 * use smalloc directly.
192 if (b
->nfrees
>= b
->freesize
) {
193 b
->freesize
= b
->nfrees
+ 32;
194 b
->frees
= sresize(b
->frees
, b
->freesize
, void *);
196 b
->frees
[b
->nfrees
++] = p
;
200 static union control
*ctrl_new(struct controlset
*s
, int type
,
201 intorptr helpctx
, handler_fn handler
,
204 union control
*c
= snew(union control
);
205 if (s
->ncontrols
>= s
->ctrlsize
) {
206 s
->ctrlsize
= s
->ncontrols
+ 32;
207 s
->ctrls
= sresize(s
->ctrls
, s
->ctrlsize
, union control
*);
209 s
->ctrls
[s
->ncontrols
++] = c
;
211 * Fill in the standard fields.
213 c
->generic
.type
= type
;
214 c
->generic
.tabdelay
= 0;
215 c
->generic
.column
= COLUMN_FIELD(0, s
->ncolumns
);
216 c
->generic
.helpctx
= helpctx
;
217 c
->generic
.handler
= handler
;
218 c
->generic
.context
= context
;
219 c
->generic
.label
= NULL
;
223 /* `ncolumns' is followed by that many percentages, as integers. */
224 union control
*ctrl_columns(struct controlset
*s
, int ncolumns
, ...)
226 union control
*c
= ctrl_new(s
, CTRL_COLUMNS
, P(NULL
), NULL
, P(NULL
));
227 assert(s
->ncolumns
== 1 || ncolumns
== 1);
228 c
->columns
.ncols
= ncolumns
;
229 s
->ncolumns
= ncolumns
;
231 c
->columns
.percentages
= NULL
;
235 c
->columns
.percentages
= snewn(ncolumns
, int);
236 va_start(ap
, ncolumns
);
237 for (i
= 0; i
< ncolumns
; i
++)
238 c
->columns
.percentages
[i
] = va_arg(ap
, int);
244 union control
*ctrl_editbox(struct controlset
*s
, char *label
, char shortcut
,
246 intorptr helpctx
, handler_fn handler
,
247 intorptr context
, intorptr context2
)
249 union control
*c
= ctrl_new(s
, CTRL_EDITBOX
, helpctx
, handler
, context
);
250 c
->editbox
.label
= label ?
dupstr(label
) : NULL
;
251 c
->editbox
.shortcut
= shortcut
;
252 c
->editbox
.percentwidth
= percentage
;
253 c
->editbox
.password
= 0;
254 c
->editbox
.has_list
= 0;
255 c
->editbox
.context2
= context2
;
259 union control
*ctrl_combobox(struct controlset
*s
, char *label
, char shortcut
,
261 intorptr helpctx
, handler_fn handler
,
262 intorptr context
, intorptr context2
)
264 union control
*c
= ctrl_new(s
, CTRL_EDITBOX
, helpctx
, handler
, context
);
265 c
->editbox
.label
= label ?
dupstr(label
) : NULL
;
266 c
->editbox
.shortcut
= shortcut
;
267 c
->editbox
.percentwidth
= percentage
;
268 c
->editbox
.password
= 0;
269 c
->editbox
.has_list
= 1;
270 c
->editbox
.context2
= context2
;
275 * `ncolumns' is followed by (alternately) radio button titles and
276 * intorptrs, until a NULL in place of a title string is seen. Each
277 * title is expected to be followed by a shortcut _iff_ `shortcut'
280 union control
*ctrl_radiobuttons(struct controlset
*s
, char *label
,
281 char shortcut
, int ncolumns
, intorptr helpctx
,
282 handler_fn handler
, intorptr context
, ...)
286 union control
*c
= ctrl_new(s
, CTRL_RADIO
, helpctx
, handler
, context
);
287 c
->radio
.label
= label ?
dupstr(label
) : NULL
;
288 c
->radio
.shortcut
= shortcut
;
289 c
->radio
.ncolumns
= ncolumns
;
291 * Initial pass along variable argument list to count the
294 va_start(ap
, context
);
296 while (va_arg(ap
, char *) != NULL
) {
298 if (c
->radio
.shortcut
== NO_SHORTCUT
)
299 (void)va_arg(ap
, int); /* char promotes to int in arg lists */
300 (void)va_arg(ap
, intorptr
);
303 c
->radio
.nbuttons
= i
;
304 if (c
->radio
.shortcut
== NO_SHORTCUT
)
305 c
->radio
.shortcuts
= snewn(c
->radio
.nbuttons
, char);
307 c
->radio
.shortcuts
= NULL
;
308 c
->radio
.buttons
= snewn(c
->radio
.nbuttons
, char *);
309 c
->radio
.buttondata
= snewn(c
->radio
.nbuttons
, intorptr
);
311 * Second pass along variable argument list to actually fill in
314 va_start(ap
, context
);
315 for (i
= 0; i
< c
->radio
.nbuttons
; i
++) {
316 c
->radio
.buttons
[i
] = dupstr(va_arg(ap
, char *));
317 if (c
->radio
.shortcut
== NO_SHORTCUT
)
318 c
->radio
.shortcuts
[i
] = va_arg(ap
, int);
319 /* char promotes to int in arg lists */
320 c
->radio
.buttondata
[i
] = va_arg(ap
, intorptr
);
326 union control
*ctrl_pushbutton(struct controlset
*s
,char *label
,char shortcut
,
327 intorptr helpctx
, handler_fn handler
,
330 union control
*c
= ctrl_new(s
, CTRL_BUTTON
, helpctx
, handler
, context
);
331 c
->button
.label
= label ?
dupstr(label
) : NULL
;
332 c
->button
.shortcut
= shortcut
;
333 c
->button
.isdefault
= 0;
334 c
->button
.iscancel
= 0;
338 union control
*ctrl_listbox(struct controlset
*s
,char *label
,char shortcut
,
339 intorptr helpctx
, handler_fn handler
,
342 union control
*c
= ctrl_new(s
, CTRL_LISTBOX
, helpctx
, handler
, context
);
343 c
->listbox
.label
= label ?
dupstr(label
) : NULL
;
344 c
->listbox
.shortcut
= shortcut
;
345 c
->listbox
.height
= 5; /* *shrug* a plausible default */
346 c
->listbox
.draglist
= 0;
347 c
->listbox
.multisel
= 0;
348 c
->listbox
.percentwidth
= 100;
349 c
->listbox
.ncols
= 0;
350 c
->listbox
.percentages
= NULL
;
354 union control
*ctrl_droplist(struct controlset
*s
, char *label
, char shortcut
,
355 int percentage
, intorptr helpctx
,
356 handler_fn handler
, intorptr context
)
358 union control
*c
= ctrl_new(s
, CTRL_LISTBOX
, helpctx
, handler
, context
);
359 c
->listbox
.label
= label ?
dupstr(label
) : NULL
;
360 c
->listbox
.shortcut
= shortcut
;
361 c
->listbox
.height
= 0; /* means it's a drop-down list */
362 c
->listbox
.draglist
= 0;
363 c
->listbox
.multisel
= 0;
364 c
->listbox
.percentwidth
= percentage
;
368 union control
*ctrl_draglist(struct controlset
*s
,char *label
,char shortcut
,
369 intorptr helpctx
, handler_fn handler
,
372 union control
*c
= ctrl_new(s
, CTRL_LISTBOX
, helpctx
, handler
, context
);
373 c
->listbox
.label
= label ?
dupstr(label
) : NULL
;
374 c
->listbox
.shortcut
= shortcut
;
375 c
->listbox
.height
= 5; /* *shrug* a plausible default */
376 c
->listbox
.draglist
= 1;
377 c
->listbox
.multisel
= 0;
378 c
->listbox
.percentwidth
= 100;
382 union control
*ctrl_filesel(struct controlset
*s
,char *label
,char shortcut
,
383 char const *filter
, int write
, char *title
,
384 intorptr helpctx
, handler_fn handler
,
387 union control
*c
= ctrl_new(s
, CTRL_FILESELECT
, helpctx
, handler
, context
);
388 c
->fileselect
.label
= label ?
dupstr(label
) : NULL
;
389 c
->fileselect
.shortcut
= shortcut
;
390 c
->fileselect
.filter
= filter
;
391 c
->fileselect
.for_writing
= write
;
392 c
->fileselect
.title
= dupstr(title
);
396 union control
*ctrl_fontsel(struct controlset
*s
,char *label
,char shortcut
,
397 intorptr helpctx
, handler_fn handler
,
400 union control
*c
= ctrl_new(s
, CTRL_FONTSELECT
, helpctx
, handler
, context
);
401 c
->fontselect
.label
= label ?
dupstr(label
) : NULL
;
402 c
->fontselect
.shortcut
= shortcut
;
406 union control
*ctrl_tabdelay(struct controlset
*s
, union control
*ctrl
)
408 union control
*c
= ctrl_new(s
, CTRL_TABDELAY
, P(NULL
), NULL
, P(NULL
));
409 c
->tabdelay
.ctrl
= ctrl
;
413 union control
*ctrl_text(struct controlset
*s
, char *text
, intorptr helpctx
)
415 union control
*c
= ctrl_new(s
, CTRL_TEXT
, helpctx
, NULL
, P(NULL
));
416 c
->text
.label
= dupstr(text
);
420 union control
*ctrl_checkbox(struct controlset
*s
, char *label
, char shortcut
,
421 intorptr helpctx
, handler_fn handler
,
424 union control
*c
= ctrl_new(s
, CTRL_CHECKBOX
, helpctx
, handler
, context
);
425 c
->checkbox
.label
= label ?
dupstr(label
) : NULL
;
426 c
->checkbox
.shortcut
= shortcut
;
430 void ctrl_free(union control
*ctrl
)
434 sfree(ctrl
->generic
.label
);
435 switch (ctrl
->generic
.type
) {
437 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++)
438 sfree(ctrl
->radio
.buttons
[i
]);
439 sfree(ctrl
->radio
.buttons
);
440 sfree(ctrl
->radio
.shortcuts
);
441 sfree(ctrl
->radio
.buttondata
);
444 sfree(ctrl
->columns
.percentages
);
447 sfree(ctrl
->listbox
.percentages
);
449 case CTRL_FILESELECT
:
450 sfree(ctrl
->fileselect
.title
);
456 void dlg_stdradiobutton_handler(union control
*ctrl
, void *dlg
,
457 void *data
, int event
)
461 * For a standard radio button set, the context parameter gives
462 * offsetof(targetfield, Config), and the extra data per button
463 * gives the value the target field should take if that button
464 * is the one selected.
466 if (event
== EVENT_REFRESH
) {
467 for (button
= 0; button
< ctrl
->radio
.nbuttons
; button
++)
468 if (*(int *)ATOFFSET(data
, ctrl
->radio
.context
.i
) ==
469 ctrl
->radio
.buttondata
[button
].i
)
471 /* We expected that `break' to happen, in all circumstances. */
472 assert(button
< ctrl
->radio
.nbuttons
);
473 dlg_radiobutton_set(ctrl
, dlg
, button
);
474 } else if (event
== EVENT_VALCHANGE
) {
475 button
= dlg_radiobutton_get(ctrl
, dlg
);
476 assert(button
>= 0 && button
< ctrl
->radio
.nbuttons
);
477 *(int *)ATOFFSET(data
, ctrl
->radio
.context
.i
) =
478 ctrl
->radio
.buttondata
[button
].i
;
482 void dlg_stdcheckbox_handler(union control
*ctrl
, void *dlg
,
483 void *data
, int event
)
488 * For a standard checkbox, the context parameter gives
489 * offsetof(targetfield, Config), optionally ORed with
492 offset
= ctrl
->checkbox
.context
.i
;
493 if (offset
& CHECKBOX_INVERT
) {
494 offset
&= ~CHECKBOX_INVERT
;
500 * C lacks a logical XOR, so the following code uses the idiom
501 * (!a ^ !b) to obtain the logical XOR of a and b. (That is, 1
502 * iff exactly one of a and b is nonzero, otherwise 0.)
505 if (event
== EVENT_REFRESH
) {
506 dlg_checkbox_set(ctrl
,dlg
, (!*(int *)ATOFFSET(data
,offset
) ^ !invert
));
507 } else if (event
== EVENT_VALCHANGE
) {
508 *(int *)ATOFFSET(data
, offset
) = !dlg_checkbox_get(ctrl
,dlg
) ^ !invert
;
512 void dlg_stdeditbox_handler(union control
*ctrl
, void *dlg
,
513 void *data
, int event
)
516 * The standard edit-box handler expects the main `context'
517 * field to contain the `offsetof' a field in the structure
518 * pointed to by `data'. The secondary `context2' field
519 * indicates the type of this field:
521 * - if context2 > 0, the field is a char array and context2
523 * - if context2 == -1, the field is an int and the edit box
525 * - if context2 < -1, the field is an int and the edit box is
526 * _floating_, and (-context2) gives the scale. (E.g. if
527 * context2 == -1000, then typing 1.2 into the box will set
528 * the field to 1200.)
530 int offset
= ctrl
->editbox
.context
.i
;
531 int length
= ctrl
->editbox
.context2
.i
;
534 char *field
= (char *)ATOFFSET(data
, offset
);
535 if (event
== EVENT_REFRESH
) {
536 dlg_editbox_set(ctrl
, dlg
, field
);
537 } else if (event
== EVENT_VALCHANGE
) {
538 dlg_editbox_get(ctrl
, dlg
, field
, length
);
540 } else if (length
< 0) {
541 int *field
= (int *)ATOFFSET(data
, offset
);
543 if (event
== EVENT_REFRESH
) {
545 sprintf(data
, "%d", *field
);
547 sprintf(data
, "%g", (double)*field
/ (double)(-length
));
548 dlg_editbox_set(ctrl
, dlg
, data
);
549 } else if (event
== EVENT_VALCHANGE
) {
550 dlg_editbox_get(ctrl
, dlg
, data
, lenof(data
));
554 *field
= (int)((-length
) * atof(data
));
559 void dlg_stdfilesel_handler(union control
*ctrl
, void *dlg
,
560 void *data
, int event
)
563 * The standard file-selector handler expects the `context'
564 * field to contain the `offsetof' a Filename field in the
565 * structure pointed to by `data'.
567 int offset
= ctrl
->fileselect
.context
.i
;
569 if (event
== EVENT_REFRESH
) {
570 dlg_filesel_set(ctrl
, dlg
, *(Filename
*)ATOFFSET(data
, offset
));
571 } else if (event
== EVENT_VALCHANGE
) {
572 dlg_filesel_get(ctrl
, dlg
, (Filename
*)ATOFFSET(data
, offset
));
576 void dlg_stdfontsel_handler(union control
*ctrl
, void *dlg
,
577 void *data
, int event
)
580 * The standard file-selector handler expects the `context'
581 * field to contain the `offsetof' a FontSpec field in the
582 * structure pointed to by `data'.
584 int offset
= ctrl
->fontselect
.context
.i
;
586 if (event
== EVENT_REFRESH
) {
587 dlg_fontsel_set(ctrl
, dlg
, *(FontSpec
*)ATOFFSET(data
, offset
));
588 } else if (event
== EVENT_VALCHANGE
) {
589 dlg_fontsel_get(ctrl
, dlg
, (FontSpec
*)ATOFFSET(data
, offset
));