2 * Exports and types from dialog.c.
6 * This will come in handy for generic control handlers. Anyone
7 * knows how to make this more portable, let me know :-)
9 #define ATOFFSET(data, offset) ( (void *) ( (char *)(data) + (offset) ) )
12 * This is the big union which defines a single control, of any
16 * - _All_ pointers in this structure are expected to point to
17 * dynamically allocated things, unless otherwise indicated.
18 * - `char' fields giving keyboard shortcuts are expected to be
19 * NO_SHORTCUT if no shortcut is desired for a particular control.
20 * - The `label' field can often be NULL, which will cause the
21 * control to not have a label at all. This doesn't apply to
22 * checkboxes and push buttons, in which the label is not
23 * separate from the control.
26 #define NO_SHORTCUT '\0'
29 CTRL_TEXT, /* just a static line of text */
30 CTRL_EDITBOX, /* label plus edit box */
31 CTRL_RADIO, /* label plus radio buttons */
32 CTRL_CHECKBOX, /* checkbox (contains own label) */
33 CTRL_BUTTON, /* simple push button (no label) */
34 CTRL_LISTBOX, /* label plus list box */
35 CTRL_COLUMNS, /* divide window into columns */
36 CTRL_FILESELECT, /* label plus filename selector */
37 CTRL_FONTSELECT, /* label plus font selector */
38 CTRL_TABDELAY /* see `tabdelay' below */
42 * Many controls have `intorptr' unions for storing user data,
43 * since the user might reasonably want to store either an integer
44 * or a void * pointer. Here I define a union, and two convenience
45 * functions to create that union from actual integers or pointers.
47 * The convenience functions are declared as inline if possible.
48 * Otherwise, they're declared here and defined when this header is
49 * included with DEFINE_INTORPTR_FNS defined. This is a total pain,
52 typedef union { void *p; int i; } intorptr;
59 #if defined DEFINE_INTORPTR_FNS || defined INLINE
65 PREFIX intorptr I(int i) { intorptr ret; ret.i = i; return ret; }
66 PREFIX intorptr P(void *p) { intorptr ret; ret.p = p; return ret; }
71 * Each control has an `int' field specifying which columns it
72 * occupies in a multi-column part of the dialog box. These macros
73 * pack and unpack that field.
75 * If a control belongs in exactly one column, just specifying the
76 * column number is perfectly adequate.
78 #define COLUMN_FIELD(start, span) ( (((span)-1) << 16) + (start) )
79 #define COLUMN_START(field) ( (field) & 0xFFFF )
80 #define COLUMN_SPAN(field) ( (((field) >> 16) & 0xFFFF) + 1 )
85 * The number of event types is being deliberately kept small, on
86 * the grounds that not all platforms might be able to report a
87 * large number of subtle events. We have:
88 * - the special REFRESH event, called when a control's value
90 * - the ACTION event, called when the user does something that
91 * positively requests action (double-clicking a list box item,
92 * or pushing a push-button)
93 * - the VALCHANGE event, called when the user alters the setting
94 * of the control in a way that is usually considered to alter
95 * the underlying data (toggling a checkbox or radio button,
96 * moving the items around in a drag-list, editing an edit
98 * - the SELCHANGE event, called when the user alters the setting
99 * of the control in a more minor way (changing the selected
100 * item in a list box).
101 * - the CALLBACK event, which happens after the handler routine
102 * has requested a subdialog (file selector, font selector,
103 * colour selector) and it has come back with information.
112 typedef void (*handler_fn)(union control *ctrl, void *dlg,
113 void *data, int event);
115 #define STANDARD_PREFIX \
120 handler_fn handler; \
126 * The first possibility in this union is the generic header
127 * shared by all the structures, which we are therefore allowed
128 * to access through any one of them.
133 * Every control except CTRL_COLUMNS has _some_ sort of
134 * label. By putting it in the `generic' union as well as
135 * everywhere else, we avoid having to have an irritating
136 * switch statement when we go through and deallocate all
137 * the memory in a config-box structure.
139 * Yes, this does mean that any non-NULL value in this
140 * field is expected to be dynamically allocated and
143 * For CTRL_COLUMNS, this field MUST be NULL.
147 * If `tabdelay' is non-zero, it indicates that this
148 * particular control should not yet appear in the tab
149 * order. A subsequent CTRL_TABDELAY entry will place it.
153 * Indicate which column(s) this control occupies. This can
154 * be unpacked into starting column and column span by the
155 * COLUMN macros above.
159 * Most controls need to provide a function which gets
160 * called when that control's setting is changed, or when
161 * the control's setting needs initialising.
163 * The `data' parameter points to the writable data being
164 * modified as a result of the configuration activity; for
165 * example, the PuTTY `Conf' structure, although not
168 * The `dlg' parameter is passed back to the platform-
169 * specific routines to read and write the actual control
174 * Almost all of the above functions will find it useful to
175 * be able to store a piece of `void *' or `int' data.
179 * For any control, we also allow the storage of a piece of
180 * data for use by context-sensitive help. For example, on
181 * Windows you can click the magic question mark and then
182 * click a control, and help for that control should spring
183 * up. Hence, here is a slot in which to store per-control
184 * data that a particular platform-specific driver can use
185 * to ensure it brings up the right piece of help text.
198 char shortcut; /* keyboard shortcut */
200 * Percentage of the dialog-box width used by the edit box.
201 * If this is set to 100, the label is on its own line;
202 * otherwise the label is on the same line as the box
206 int password; /* details of input are hidden */
208 * A special case of the edit box is the combo box, which
209 * has a drop-down list built in. (Note that a _non_-
210 * editable drop-down list is done as a special case of a
213 * Don't try setting has_list and password on the same
214 * control; front ends are not required to support that
219 * Edit boxes tend to need two items of context, so here's
227 * `shortcut' here is a single keyboard shortcut which is
228 * expected to select the whole group of radio buttons. It
229 * can be NO_SHORTCUT if required, and there is also a way
230 * to place individual shortcuts on each button; see below.
234 * There are separate fields for `ncolumns' and `nbuttons'
235 * for several reasons.
237 * Firstly, we sometimes want the last of a set of buttons
238 * to have a longer label than the rest; we achieve this by
239 * setting `ncolumns' higher than `nbuttons', and the
240 * layout code is expected to understand that the final
241 * button should be given all the remaining space on the
242 * line. This sounds like a ludicrously specific special
243 * case (if we're doing this sort of thing, why not have
244 * the general ability to have a particular button span
245 * more than one column whether it's the last one or not?)
246 * but actually it's reasonably common for the sort of
247 * three-way control you get a lot of in PuTTY: `yes'
248 * versus `no' versus `some more complex way to decide'.
250 * Secondly, setting `nbuttons' higher than `ncolumns' lets
251 * us have more than one line of radio buttons for a single
252 * setting. A very important special case of this is
253 * setting `ncolumns' to 1, so that each button is on its
259 * This points to a dynamically allocated array of `char *'
260 * pointers, each of which points to a dynamically
263 char **buttons; /* `nbuttons' button labels */
265 * This points to a dynamically allocated array of `char'
266 * giving the individual keyboard shortcuts for each radio
267 * button. The array may be NULL if none are required.
269 char *shortcuts; /* `nbuttons' shortcuts; may be NULL */
271 * This points to a dynamically allocated array of
272 * intorptr, giving helpful data for each button.
274 intorptr *buttondata; /* `nbuttons' entries; may be NULL */
284 * At least Windows has the concept of a `default push
285 * button', which gets implicitly pressed when you hit
286 * Return even if it doesn't have the input focus.
290 * Also, the reverse of this: a default cancel-type button,
291 * which is implicitly pressed when you hit Escape.
297 char shortcut; /* keyboard shortcut */
299 * Height of the list box, in approximate number of lines.
300 * If this is zero, the list is a drop-down list.
302 int height; /* height in lines */
304 * If this is set, the list elements can be reordered by
305 * the user (by drag-and-drop or by Up and Down buttons,
306 * whatever the per-platform implementation feels
307 * comfortable with). This is not guaranteed to work on a
308 * drop-down list, so don't try it!
312 * If this is non-zero, the list can have more than one
313 * element selected at a time. This is not guaranteed to
314 * work on a drop-down list, so don't try it!
316 * Different non-zero values request slightly different
317 * types of multi-selection (this may well be meaningful
318 * only in GTK, so everyone else can ignore it if they
319 * want). 1 means the list box expects to have individual
320 * items selected, whereas 2 means it expects the user to
321 * want to select a large contiguous range at a time.
325 * Percentage of the dialog-box width used by the list box.
326 * If this is set to 100, the label is on its own line;
327 * otherwise the label is on the same line as the box
328 * itself. Setting this to anything other than 100 is not
329 * guaranteed to work on a _non_-drop-down list, so don't
334 * Some list boxes contain strings that contain tab
335 * characters. If `ncols' is greater than 0, then
336 * `percentages' is expected to be non-zero and to contain
337 * the respective widths of `ncols' columns, which together
338 * will exactly fit the width of the list box. Otherwise
339 * `percentages' must be NULL.
341 * There should never be more than one column in a
342 * drop-down list (one with height==0), because front ends
343 * may have to implement it as a special case of an
344 * editable combo box.
346 int ncols; /* number of columns */
347 int *percentages; /* % width of each column */
349 * Flag which can be set to FALSE to suppress the horizontal
350 * scroll bar if a list box entry goes off the right-hand
359 * `filter' dictates what type of files will be selected by
360 * default; for example, when selecting private key files
361 * the file selector would do well to only show .PPK files
362 * (on those systems where this is the chosen extension).
364 * The precise contents of `filter' are platform-defined,
365 * unfortunately. The special value NULL means `all files'
366 * and is always a valid fallback.
368 * Unlike almost all strings in this structure, this value
369 * is NOT expected to require freeing (although of course
370 * you can always use ctrl_alloc if you do need to create
371 * one on the fly). This is because the likely mode of use
372 * is to define string constants in a platform-specific
373 * header file, and directly reference those. Or worse, a
374 * particular platform might choose to cast integers into
375 * this pointer type...
379 * Some systems like to know whether a file selector is
380 * choosing a file to read or one to write (and possibly
385 * On at least some platforms, the file selector is a
386 * separate dialog box, and contains a user-settable title.
388 * This value _is_ expected to require freeing.
393 /* In this variant, `label' MUST be NULL. */
395 int ncols; /* number of columns */
396 int *percentages; /* % width of each column */
398 * Every time this control type appears, exactly one of
399 * `ncols' and the previous number of columns MUST be one.
400 * Attempting to allow a seamless transition from a four-
401 * to a five-column layout, for example, would be way more
402 * trouble than it was worth. If you must lay things out
403 * like that, define eight unevenly sized columns and use
404 * column-spanning a lot. But better still, just don't.
406 * `percentages' may be NULL if ncols==1, to save space.
415 #undef STANDARD_PREFIX
418 * `controlset' is a container holding an array of `union control'
419 * structures, together with a panel name and a title for the whole
420 * set. In Windows and any similar-looking GUI, each `controlset'
421 * in the config will be a container box within a panel.
423 * Special case: if `boxname' is NULL, the control set gives an
424 * overall title for an entire panel of controls.
427 char *pathname; /* panel path, e.g. "SSH/Tunnels" */
428 char *boxname; /* internal short name of controlset */
429 char *boxtitle; /* title of container box */
430 int ncolumns; /* current no. of columns at bottom */
431 int ncontrols; /* number of `union control' in array */
432 int ctrlsize; /* allocated size of array */
433 union control **ctrls; /* actual array */
436 typedef void (*ctrl_freefn_t)(void *); /* used by ctrl_alloc_with_free */
439 * This is the container structure which holds a complete set of
443 int nctrlsets; /* number of ctrlsets */
444 int ctrlsetsize; /* ctrlset size */
445 struct controlset **ctrlsets; /* actual array of ctrlsets */
448 void **frees; /* array of aux data areas to free */
449 ctrl_freefn_t *freefuncs; /* parallel array of free functions */
452 struct controlbox *ctrl_new_box(void);
453 void ctrl_free_box(struct controlbox *);
456 * Standard functions used for populating a controlbox structure.
459 /* Set up a panel title. */
460 struct controlset *ctrl_settitle(struct controlbox *,
461 const char *path, const char *title);
462 /* Retrieve a pointer to a controlset, creating it if absent. */
463 struct controlset *ctrl_getset(struct controlbox *, const char *path,
464 const char *name, const char *boxtitle);
465 void ctrl_free_set(struct controlset *);
467 void ctrl_free(union control *);
470 * This function works like `malloc', but the memory it returns
471 * will be automatically freed when the controlbox is freed. Note
472 * that a controlbox is a dialog-box _template_, not an instance,
473 * and so data allocated through this function is better not used
474 * to hold modifiable per-instance things. It's mostly here for
475 * allocating structures to be passed as control handler params.
477 * ctrl_alloc_with_free also allows you to provide a function to free
478 * the structure, in case there are other dynamically allocated bits
479 * and pieces dangling off it.
481 void *ctrl_alloc(struct controlbox *b, size_t size);
482 void *ctrl_alloc_with_free(struct controlbox *b, size_t size,
483 ctrl_freefn_t freefunc);
486 * Individual routines to create `union control' structures in a controlset.
488 * Most of these routines allow the most common fields to be set
489 * directly, and put default values in the rest. Each one returns a
490 * pointer to the `union control' it created, so that final tweaks
494 /* `ncolumns' is followed by that many percentages, as integers. */
495 union control *ctrl_columns(struct controlset *, int ncolumns, ...);
496 union control *ctrl_editbox(struct controlset *, const char *label,
497 char shortcut, int percentage, intorptr helpctx,
499 intorptr context, intorptr context2);
500 union control *ctrl_combobox(struct controlset *, const char *label,
501 char shortcut, int percentage, intorptr helpctx,
503 intorptr context, intorptr context2);
505 * `ncolumns' is followed by (alternately) radio button titles and
506 * intorptrs, until a NULL in place of a title string is seen. Each
507 * title is expected to be followed by a shortcut _iff_ `shortcut'
510 union control *ctrl_radiobuttons(struct controlset *, const char *label,
511 char shortcut, int ncolumns, intorptr helpctx,
512 handler_fn handler, intorptr context, ...);
513 union control *ctrl_pushbutton(struct controlset *, const char *label,
514 char shortcut, intorptr helpctx,
515 handler_fn handler, intorptr context);
516 union control *ctrl_listbox(struct controlset *, const char *label,
517 char shortcut, intorptr helpctx,
518 handler_fn handler, intorptr context);
519 union control *ctrl_droplist(struct controlset *, const char *label,
520 char shortcut, int percentage, intorptr helpctx,
521 handler_fn handler, intorptr context);
522 union control *ctrl_draglist(struct controlset *, const char *label,
523 char shortcut, intorptr helpctx,
524 handler_fn handler, intorptr context);
525 union control *ctrl_filesel(struct controlset *, const char *label,
526 char shortcut, const char *filter, int write,
527 const char *title, intorptr helpctx,
528 handler_fn handler, intorptr context);
529 union control *ctrl_fontsel(struct controlset *, const char *label,
530 char shortcut, intorptr helpctx,
531 handler_fn handler, intorptr context);
532 union control *ctrl_text(struct controlset *, const char *text,
534 union control *ctrl_checkbox(struct controlset *, const char *label,
535 char shortcut, intorptr helpctx,
536 handler_fn handler, intorptr context);
537 union control *ctrl_tabdelay(struct controlset *, union control *);
540 * Routines the platform-independent dialog code can call to read
541 * and write the values of controls.
543 void dlg_radiobutton_set(union control *ctrl, void *dlg, int whichbutton);
544 int dlg_radiobutton_get(union control *ctrl, void *dlg);
545 void dlg_checkbox_set(union control *ctrl, void *dlg, int checked);
546 int dlg_checkbox_get(union control *ctrl, void *dlg);
547 void dlg_editbox_set(union control *ctrl, void *dlg, char const *text);
548 char *dlg_editbox_get(union control *ctrl, void *dlg); /* result must be freed by caller */
549 /* The `listbox' functions can also apply to combo boxes. */
550 void dlg_listbox_clear(union control *ctrl, void *dlg);
551 void dlg_listbox_del(union control *ctrl, void *dlg, int index);
552 void dlg_listbox_add(union control *ctrl, void *dlg, char const *text);
554 * Each listbox entry may have a numeric id associated with it.
555 * Note that some front ends only permit a string to be stored at
556 * each position, which means that _if_ you put two identical
557 * strings in any listbox then you MUST not assign them different
558 * IDs and expect to get meaningful results back.
560 void dlg_listbox_addwithid(union control *ctrl, void *dlg,
561 char const *text, int id);
562 int dlg_listbox_getid(union control *ctrl, void *dlg, int index);
563 /* dlg_listbox_index returns <0 if no single element is selected. */
564 int dlg_listbox_index(union control *ctrl, void *dlg);
565 int dlg_listbox_issel(union control *ctrl, void *dlg, int index);
566 void dlg_listbox_select(union control *ctrl, void *dlg, int index);
567 void dlg_text_set(union control *ctrl, void *dlg, char const *text);
568 void dlg_filesel_set(union control *ctrl, void *dlg, Filename *fn);
569 Filename *dlg_filesel_get(union control *ctrl, void *dlg);
570 void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec *fn);
571 FontSpec *dlg_fontsel_get(union control *ctrl, void *dlg);
573 * Bracketing a large set of updates in these two functions will
574 * cause the front end (if possible) to delay updating the screen
575 * until it's all complete, thus avoiding flicker.
577 void dlg_update_start(union control *ctrl, void *dlg);
578 void dlg_update_done(union control *ctrl, void *dlg);
580 * Set input focus into a particular control.
582 void dlg_set_focus(union control *ctrl, void *dlg);
584 * Change the label text on a control.
586 void dlg_label_change(union control *ctrl, void *dlg, char const *text);
588 * Return the `ctrl' structure for the most recent control that had
589 * the input focus apart from the one mentioned. This is NOT
590 * GUARANTEED to work on all platforms, so don't base any critical
591 * functionality on it!
593 union control *dlg_last_focused(union control *ctrl, void *dlg);
595 * During event processing, you might well want to give an error
596 * indication to the user. dlg_beep() is a quick and easy generic
597 * error; dlg_error() puts up a message-box or equivalent.
599 void dlg_beep(void *dlg);
600 void dlg_error_msg(void *dlg, const char *msg);
602 * This function signals to the front end that the dialog's
603 * processing is completed, and passes an integer value (typically
606 void dlg_end(void *dlg, int value);
609 * Routines to manage a (per-platform) colour selector.
610 * dlg_coloursel_start() is called in an event handler, and
611 * schedules the running of a colour selector after the event
612 * handler returns. The colour selector will send EVENT_CALLBACK to
613 * the control that spawned it, when it's finished;
614 * dlg_coloursel_results() fetches the results, as integers from 0
615 * to 255; it returns nonzero on success, or zero if the colour
616 * selector was dismissed by hitting Cancel or similar.
618 * dlg_coloursel_start() accepts an RGB triple which is used to
619 * initialise the colour selector to its starting value.
621 void dlg_coloursel_start(union control *ctrl, void *dlg,
622 int r, int g, int b);
623 int dlg_coloursel_results(union control *ctrl, void *dlg,
624 int *r, int *g, int *b);
627 * This routine is used by the platform-independent code to
628 * indicate that the value of a particular control is likely to
629 * have changed. It triggers a call of the handler for that control
630 * with `event' set to EVENT_REFRESH.
632 * If `ctrl' is NULL, _all_ controls in the dialog get refreshed
633 * (for loading or saving entire sets of settings).
635 void dlg_refresh(union control *ctrl, void *dlg);
638 * Standard helper functions for reading a controlbox structure.
642 * Find the index of next controlset in a controlbox for a given
643 * path, or -1 if no such controlset exists. If -1 is passed as
644 * input, finds the first. Intended usage is something like
646 * for (index=-1; (index=ctrl_find_path(ctrlbox, index, path)) >= 0 ;) {
647 * ... process this controlset ...
650 int ctrl_find_path(struct controlbox *b, const char *path, int index);
651 int ctrl_path_elements(const char *path);
652 /* Return the number of matching path elements at the starts of p1 and p2,
653 * or INT_MAX if the paths are identical. */
654 int ctrl_path_compare(const char *p1, const char *p2);