File: DialogF.c

package info (click to toggle)
nedit 1%3A5.5-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 7,136 kB
  • ctags: 6,956
  • sloc: ansic: 92,920; xml: 1,427; yacc: 621; makefile: 341; awk: 40; sh: 10
file content (650 lines) | stat: -rw-r--r-- 26,511 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
static const char CVSID[] = "$Id: DialogF.c,v 1.31 2004/08/01 10:06:12 yooden Exp $";
/*******************************************************************************
*                                                                              *
* DialogF -- modal dialog printf routine                                       *
*                                                                              *
* Copyright (C) 1999 Mark Edel                                                 *
*                                                                              *
* This is free software; you can redistribute it and/or modify it under the    *
* terms of the GNU General Public License as published by the Free Software    *
* Foundation; either version 2 of the License, or (at your option) any later   *
* version. In addition, you may distribute version of this program linked to   *
* Motif or Open Motif. See README for details.                                 *
*                                                                              *
* This software is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        *
* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License        *
* for more details.                                                               *
*                                                                                *
* You should have received a copy of the GNU General Public License along with *
* software; if not, write to the Free Software Foundation, Inc., 59 Temple     *
* Place, Suite 330, Boston, MA  02111-1307 USA                                       *
*                                                                               *
* Nirvana Text Editor                                                                   *
* April 26, 1991                                                               *
*                                                                               *
* Written by Joy Kyriakopulos                                                       *
*                                                                               *
*******************************************************************************/

#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif

#include "DialogF.h"
#include "misc.h"

#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>

#include <Xm/Xm.h>
#include <Xm/MessageB.h>
#include <Xm/DialogS.h>
#include <Xm/PushB.h>
#include <Xm/PushBG.h>
#include <Xm/SelectioB.h>
#include <X11/StringDefs.h>
#include <X11/Intrinsic.h>
#include <X11/keysym.h>

#ifdef HAVE_DEBUG_H
#include "../debug.h"
#endif

#define NUM_DIALOGS_SUPPORTED 6
#define NUM_BUTTONS_SUPPORTED 3		/* except prompt dialog */
#define NUM_BUTTONS_MAXPROMPT 4
#define MAX_TITLE_LEN 256

enum dialogBtnIndecies {OK_BTN, APPLY_BTN, CANCEL_BTN, HELP_BTN};

struct dfcallbackstruct {
    unsigned button;		/* button pressed by user		     */
    Boolean done_with_dialog;	/* set by callbacks; dialog can be destroyed */
    unsigned apply_up;		/* will = 1 when apply button managed	     */
    Boolean destroyed;		/* set when dialog is destroyed unexpectedly */
};

static char **PromptHistory = NULL;
static int NPromptHistoryItems = -1;

static void apply_callback (Widget w, struct dfcallbackstruct *client_data,
	caddr_t call_data);
static void help_callback (Widget w, struct dfcallbackstruct *client_data,
	caddr_t call_data);
static void cancel_callback (Widget w, struct dfcallbackstruct *client_data,
	caddr_t call_data);
static void ok_callback (Widget w, struct dfcallbackstruct *client_data,
	caddr_t call_data);
static void destroy_callback (Widget w, struct dfcallbackstruct *client_data,
	caddr_t call_data);
static void focusCB(Widget w, Widget dialog, caddr_t call_data);
static void addEscapeHandler(Widget dialog, struct dfcallbackstruct *df,
    	int whichBtn);
static void escapeHelpCB(Widget w, XtPointer callData, XEvent *event,
    	Boolean *cont);
static void escapeApplyCB(Widget w, XtPointer callData, XEvent *event,
    	Boolean *cont);
static void createMnemonics(Widget w);

/*******************************************************************************
* DialogF()                                                                    *
*                                                                              *
* function to put up modal versions of the XmCreate<type>Dialog functions      *
* (where <type> is Error, Information, Prompt, Question, Message, or Warning). *
* The purpose of this routine is to allow a printf-style dialog box to be      *
* invoked in-line without giving control back to the main loop. The message    *
* string can contain vsprintf specifications.                                  *
* DialogF displays the dialog in application-modal style, blocking the         *
* application and keeping the modal dialog as the top window until the user    *
* responds. DialogF accepts a variable number of arguments, so the calling     *
* routine needs to #include <stdarg.h>. The first button is automatically      *
* marked as the default button (activated when the user types Return,          *
* surrounded by a special outline), and any button named either Cancel, or     *
* Dismiss is marked as the cancel button (activated by the ESC key). Buttons   *
* marked Dismiss or Cancel are also triggered by close of dialog via the       *
* window close box. If there's no Cancel or Dismiss button, button 1 is        *
* invoked when the close box is pressed.                                       *
*                                                                              *
* Arguments:                                                                   *
*                                                                              *
* unsigned dialog_type  dialog type (e.g. DF_ERR for error dialog, refer to    *
*                       DialogF.h for dialog type values)                      *
* Widget parent         parent widget ID                                       *
* unsigned n            # of buttons to display; if set to 0, use defaults in  *
*                       XmCreate<type>Dialog; value in range 0 to              *
*                       NUM_BUTTONS_SUPPORTED (for prompt dialogs:             *
*                       NUM_BUTTONS_MAXPROMPT)                                 *
* char* title           dialog title                                           *
* char* msgstr          message string (may contain conversion specifications  *
*                       for vsprintf)                                          *
* char* input_string    if dialog type = DF_PROMPT, then: a character string   *
*                       array in which to put the string input by the user. Do *
*                       NOT include an input_string argument for other dialog  *
*                       types.                                                 *
* char* but_lbl         button label(s) for buttons requested (if n > 0, one   *
*                       but_lbl argument per button)                           *
* <anytype> <args>      arguments for vsprintf (if any)                        *
*                                                                              *
*                                                                              *
* Returns:                                                                     *
*                                                                              *
* button selected by user (i.e. 1, 2, or 3. or 4 for prompt)                   *
*                                                                              *
*                                                                              *
* Examples:                                                                    *
*                                                                              *
* but_pressed = DialogF (DF_QUES, toplevel, 3, "Direction?", "up",             *
*              "down", "other");                                               *
* but_pressed = DialogF (DF_ERR, toplevel, 1, "You can't do that!",            *
*              "Acknowledged");                                                *
* but_pressed = DialogF (DF_PROMPT, toplevel, 0, "New %s",                     *
*             new_sub_category, categories[i]);                                *
*/
unsigned DialogF(int dialog_type, Widget parent, unsigned n, const char* title,
        const char* msgstr, ...)                    /* variable # arguments */
{
    va_list var;

    Widget dialog, dialog_shell;
    unsigned dialog_num, prompt;
    XmString but_lbl_xms[NUM_BUTTONS_MAXPROMPT];
    XmString msgstr_xms, input_string_xms, titstr_xms;
    char msgstr_vsp[DF_MAX_MSG_LENGTH+1];
    char *but_lbl, *input_string = NULL, *input_string_ptr;
    int argcount, num_but_lbls = 0, i, but_index, cancel_index = -1;
    Arg args[256];
    char titleCopy[MAX_TITLE_LEN];
    
    struct dfcallbackstruct df;

    static int dialog_types[] = {		/* Supported dialog types */
		XmDIALOG_ERROR,
		XmDIALOG_INFORMATION,
		XmDIALOG_MESSAGE,
		XmDIALOG_QUESTION,
		XmDIALOG_WARNING,
		XmDIALOG_PROMPT
    };
    static char *dialog_name[] = {		/* Corresponding dialog names */
		"Error",
		"Information",
		"Message",
		"Question",
		"Warning",
		"Prompt"
    };

    static unsigned char selectionButton_id[] =
    {
        XmDIALOG_OK_BUTTON,
        XmDIALOG_APPLY_BUTTON,
        XmDIALOG_CANCEL_BUTTON,
        XmDIALOG_HELP_BUTTON
    };
    Cardinal N_SELECTION_BUTTONS = XtNumber(selectionButton_id);

    static unsigned char messageButton_id[] =
    {
        XmDIALOG_OK_BUTTON,
        XmDIALOG_CANCEL_BUTTON,
        XmDIALOG_HELP_BUTTON
    };
    Cardinal N_MESSAGE_BUTTONS = XtNumber(messageButton_id);

    static char *button_name[] = {		/* Motif button names */
		XmNokLabelString,
		XmNapplyLabelString,		/* button #2, if managed */
		XmNcancelLabelString,
		XmNhelpLabelString
    };

						/* Validate input parameters */
    if ((dialog_type > NUM_DIALOGS_SUPPORTED) || (dialog_type <= 0)) {
	printf ("\nError calling DialogF - Unsupported dialog type\n");
	return (0);
    }
    dialog_num = dialog_type - 1;
    prompt = (dialog_type == DF_PROMPT);
    if  ((!prompt && (n > NUM_BUTTONS_SUPPORTED)) ||
	  (prompt && (n > NUM_BUTTONS_MAXPROMPT))) {
	printf ("\nError calling DialogF - Too many buttons specified\n");
	return (0);
    }

    df.done_with_dialog = False;
    df.destroyed = False;

    va_start (var, msgstr);
    if (prompt) {		      /* Get where to put dialog input string */
	input_string = va_arg(var, char*);
    }
    if (n == NUM_BUTTONS_MAXPROMPT)
	df.apply_up = 1;		/* Apply button will be managed */
    else
    	df.apply_up = 0;		/* Apply button will not be managed */
    for (argcount = 0; argcount < (int)n; ++argcount) {   /* Set up button labels */
	but_lbl = va_arg(var, char *);
	but_lbl_xms[num_but_lbls] = XmStringCreateLtoR (but_lbl, 
		XmSTRING_DEFAULT_CHARSET);
	but_index = df.apply_up ? argcount : 
	    	((argcount == 0) ? argcount : argcount+1);
	XtSetArg (args[argcount], button_name[but_index], 
		but_lbl_xms[num_but_lbls++]);
	if (!strcmp(but_lbl, "Cancel") || !strcmp(but_lbl, "Dismiss"))
	    cancel_index = but_index;
    }
    if (n == 1)
       cancel_index = 0;

    /* Get & translate msg string. NOTE: the use of vsprintf is inherently
       dangerous because there is no way to control the length of the written
       string. vnsprintf isn't available on all platforms, unfortunately.
       Therefore we have to make sure that msgstr_vsp is large enough such
       that it cannot overflow under any circumstances (within the context
       of any DialogF call). A necessary condition is that it can at least
       hold one file name (MAXPATHLEN). Therefore, DF_MAX_MSG_LENGTH must be
       sufficiently larger than MAXPATHLEN.
    */
    vsprintf (msgstr_vsp, msgstr, var);
    va_end(var);
    
    strncpy(&titleCopy[0], title, MAX_TITLE_LEN);
    titleCopy[MAX_TITLE_LEN-1] = '\0';

    msgstr_xms = XmStringCreateLtoR(msgstr_vsp, XmSTRING_DEFAULT_CHARSET);
    titstr_xms = XmStringCreateLtoR(titleCopy, XmSTRING_DEFAULT_CHARSET);

    if (prompt) {				/* Prompt dialog */
	XtSetArg (args[argcount], XmNselectionLabelString, msgstr_xms);
		argcount++;
	XtSetArg (args[argcount], XmNdialogTitle, titstr_xms);
		argcount++;
	XtSetArg (args[argcount], XmNdialogStyle, XmDIALOG_FULL_APPLICATION_MODAL);
		argcount ++;

	dialog = CreatePromptDialog(parent, dialog_name[dialog_num], args,
			argcount);
	XtAddCallback (dialog, XmNokCallback, (XtCallbackProc)ok_callback,
		(char *)&df);
	XtAddCallback (dialog, XmNcancelCallback,
		(XtCallbackProc)cancel_callback, (char *)&df);
	XtAddCallback (dialog, XmNhelpCallback, (XtCallbackProc)help_callback,
		(char *)&df);
	XtAddCallback (dialog, XmNapplyCallback, (XtCallbackProc)apply_callback,
		(char *)&df);
	RemapDeleteKey(XmSelectionBoxGetChild(dialog, XmDIALOG_TEXT));

	/* Text area in prompt dialog should get focus, not ok button
	   since user enters text first.  To fix this, we need to turn
	   off the default button for the dialog, until after keyboard
	   focus has been established */
	XtVaSetValues(dialog, XmNdefaultButton, NULL, NULL);
    	XtAddCallback(XmSelectionBoxGetChild(dialog, XmDIALOG_TEXT),
    		XmNfocusCallback, (XtCallbackProc)focusCB, (char *)dialog);

	/* Limit the length of the text that can be entered in text field */
	XtVaSetValues(XmSelectionBoxGetChild(dialog, XmDIALOG_TEXT),
		XmNmaxLength, DF_MAX_PROMPT_LENGTH-1, NULL);
	
	/* Turn on the requested number of buttons in the dialog by
	   managing/unmanaging the button widgets */
	switch (n) {		/* number of buttons requested */
	case 0: case 3:
	    break;		/* or default of 3 buttons */
	case 1:
	    XtUnmanageChild (XmSelectionBoxGetChild (dialog,
		XmDIALOG_CANCEL_BUTTON) );
	case 2:
	    XtUnmanageChild (XmSelectionBoxGetChild (dialog,
		XmDIALOG_HELP_BUTTON) );
	    break;
	case 4:
	    XtManageChild (XmSelectionBoxGetChild (dialog,
		XmDIALOG_APPLY_BUTTON) );
	    df.apply_up = 1;		/* apply button managed */
	default:
	    break;
	}				/* end switch */

        /*  Set margin width to managed buttons.  */
        for (i = 0; i < N_SELECTION_BUTTONS; i++)
        {
            Widget button = XmSelectionBoxGetChild(dialog, selectionButton_id[i]);

            if (XtIsManaged(button))
            {
                XtVaSetValues(button, XmNmarginWidth, BUTTON_WIDTH_MARGIN, NULL);
            }
        }

    	/* If the button labeled cancel or dismiss is not the cancel button, or
    	   if there is no button labeled cancel or dismiss, redirect escape key
    	   events (this is necessary because the XmNcancelButton resource in
    	   the bulletin board widget class is blocked from being reset) */
    	if (cancel_index == -1)
    	    addEscapeHandler(dialog, NULL, 0);
    	else if (cancel_index != CANCEL_BTN)
    	    addEscapeHandler(dialog, &df, cancel_index);

    	/* Add a callback to the window manager close callback for the dialog */
    	AddMotifCloseCallback(XtParent(dialog),
    	    	(XtCallbackProc)(cancel_index == APPLY_BTN ? apply_callback :
    	    	(cancel_index == CANCEL_BTN ? cancel_callback :
    	    	(cancel_index == HELP_BTN ? help_callback : ok_callback))), &df);
 
        /* Also add a callback to detect unexpected destruction (eg, because
           the parent window is destroyed) */
        XtAddCallback(dialog, XmNdestroyCallback, 
            (XtCallbackProc)destroy_callback, &df);

	/* A previous call to SetDialogFPromptHistory can request that an
	   up-arrow history-recall mechanism be attached.  If so, do it here */
	if (NPromptHistoryItems != -1)
	    AddHistoryToTextWidget(XmSelectionBoxGetChild(dialog,XmDIALOG_TEXT),
		    &PromptHistory, &NPromptHistoryItems);
	
    	/* Pop up the dialog */
	ManageDialogCenteredOnPointer(dialog);
	
	/* Get the focus to the input string.  There is some timing problem
	   within Motif that requires this to be called several times */
	for (i=0; i<20; i++)
	    XmProcessTraversal(XmSelectionBoxGetChild(dialog, XmDIALOG_TEXT),
		    XmTRAVERSE_CURRENT);
	
	/* Wait for a response to the dialog */
	while (!df.done_with_dialog && !df.destroyed)
	    XtAppProcessEvent (XtWidgetToApplicationContext(dialog), XtIMAll);
        
        if (!df.destroyed) {
	    argcount = 0; /* Pass back string user entered */
	    XtSetArg (args[argcount], XmNtextString, &input_string_xms); argcount++;
	    XtGetValues (dialog, args, argcount);
	    XmStringGetLtoR (input_string_xms, XmSTRING_DEFAULT_CHARSET,
		&input_string_ptr);
	    strcpy (input_string, input_string_ptr);  /* This step is necessary */
	    XmStringFree(input_string_xms );
            XtFree(input_string_ptr);
             /* Important! Only intercept unexpected destroy events. */
	    XtRemoveCallback(dialog, XmNdestroyCallback, 
            	(XtCallbackProc)destroy_callback, &df);
	    XtDestroyWidget(dialog);
	}
	PromptHistory = NULL;
    	NPromptHistoryItems = -1;
    }						  /* End prompt dialog path */

    else {				/* MessageBox dialogs */
	XtSetArg (args[argcount], XmNmessageString, msgstr_xms); argcount++;

	XtSetArg (args[argcount], XmNdialogType, dialog_types[dialog_num]);
		argcount ++;
	XtSetArg (args[argcount], XmNdialogTitle, titstr_xms);
		argcount++;
	XtSetArg (args[argcount], XmNdialogStyle, XmDIALOG_FULL_APPLICATION_MODAL);
		argcount ++;

	dialog_shell = CreateDialogShell (parent, dialog_name[dialog_num],
			0, 0);
	dialog = XmCreateMessageBox (dialog_shell, "msg box", args, argcount);
        
	XtAddCallback (dialog, XmNokCallback, (XtCallbackProc)ok_callback,
		(char *)&df);
	XtAddCallback (dialog, XmNcancelCallback,
		(XtCallbackProc)cancel_callback, (char *)&df);
	XtAddCallback (dialog, XmNhelpCallback, (XtCallbackProc)help_callback,
		(char *)&df);

	/* Make extraneous buttons disappear */
	switch (n) {		/* n = number of buttons requested */
	case 0: case 3:
	    break;		/* default (0) gets you 3 buttons */
	case 1:
	    XtUnmanageChild (XmMessageBoxGetChild (dialog,
			XmDIALOG_CANCEL_BUTTON) );
	case 2:
	    XtUnmanageChild (XmMessageBoxGetChild (dialog,
			XmDIALOG_HELP_BUTTON) );
	    break;
	default:
	    break;
	}

        /*  Set margin width to managed buttons.  */
        for (i = 0; i < N_MESSAGE_BUTTONS; i++)
        {
            Widget button = XmMessageBoxGetChild(dialog, messageButton_id[i]);

            if (XtIsManaged(button))
            {
                XtVaSetValues(button, XmNmarginWidth, BUTTON_WIDTH_MARGIN, NULL);
            }
        }

        /* Try to create some sensible default mnemonics */
        createMnemonics(dialog_shell);
        AddDialogMnemonicHandler(dialog_shell, TRUE);

    	/* If the button labeled cancel or dismiss is not the cancel button, or
    	   if there is no button labeled cancel or dismiss, redirect escape key
    	   events (this is necessary because the XmNcancelButton resource in
    	   the bulletin board widget class is blocked from being reset) */
    	if (cancel_index == -1)
    	    addEscapeHandler(dialog, NULL, 0);
    	else if (cancel_index != CANCEL_BTN)
    	    addEscapeHandler(dialog, &df, cancel_index);

    	/* Add a callback to the window manager close callback for the dialog */
    	AddMotifCloseCallback(XtParent(dialog),
    	    	(XtCallbackProc)(cancel_index == APPLY_BTN ? apply_callback :
    	    	(cancel_index == CANCEL_BTN ? cancel_callback :
    	    	(cancel_index == HELP_BTN ? help_callback : ok_callback))),&df);
        
        /* Also add a callback to detect unexpected destruction (eg, because
           the parent window is destroyed) */
        XtAddCallback(dialog_shell, XmNdestroyCallback, 
	    (XtCallbackProc)destroy_callback, &df);

	/* Pop up the dialog, wait for response*/
	ManageDialogCenteredOnPointer(dialog);
	while (!df.done_with_dialog && !df.destroyed)
	    XtAppProcessEvent (XtWidgetToApplicationContext(dialog), XtIMAll);
	
	if (!df.destroyed) {
             /* Important! Only intercept unexpected destroy events. */
	    XtRemoveCallback(dialog_shell, XmNdestroyCallback, 
            	(XtCallbackProc)destroy_callback, &df);
	    XtDestroyWidget(dialog_shell);
        }
    }

    XmStringFree(msgstr_xms);
    XmStringFree(titstr_xms);
    for (i = 0; i < num_but_lbls; ++i)
	XmStringFree(but_lbl_xms[i]);
    
    /* If the dialog was destroyed unexpectedly, the button was not set yet,
       so we must set the index of the cancel button. */
    if (df.destroyed) {
	df.button = cancel_index == APPLY_BTN ? 2 :
        	(cancel_index == CANCEL_BTN ? 2 + df.apply_up :
                (cancel_index == HELP_BTN ? 3 + df.apply_up : 1));
    }
		
    df.apply_up = 0;			/* default is apply button unmanaged */

    return (df.button);
}

/*
** Add up-arrow history recall to the next DialogF(DF_PROMPT... call (see
** AddHistoryToTextWidget in misc.c).  This must be re-set before each call.
** calling DialogF with a dialog type of DF_PROMPT automatically resets
** this mode back to no-history-recall.
*/
void SetDialogFPromptHistory(char **historyList, int nItems)
{
    PromptHistory = historyList;
    NPromptHistoryItems = nItems;
}

static void ok_callback (Widget w, struct dfcallbackstruct *client_data,
	caddr_t call_data)
{
    client_data->done_with_dialog = True;
    client_data->button = 1;		/* Return Button number pressed */
}

static void cancel_callback (Widget w, struct dfcallbackstruct *client_data,
	caddr_t call_data)
{
    client_data->done_with_dialog = True;
    client_data->button = 2 + client_data->apply_up; /* =3 if apply button managed */
}

static void help_callback (Widget w, struct dfcallbackstruct *client_data,
	caddr_t call_data)
{
    client_data->done_with_dialog = True;
    client_data->button = 3 + client_data->apply_up; /* =4 if apply button managed */
}

static void apply_callback (Widget w, struct dfcallbackstruct *client_data,
	caddr_t call_data)
{
    client_data->done_with_dialog = True;
    client_data->button = 2;		/* Motif puts between OK and cancel */
}

static void destroy_callback (Widget w, struct dfcallbackstruct *client_data,
	caddr_t call_data)
{
    client_data->destroyed = True;
}

/*
** callback for returning default button status to the ok button once we're
** sure the text area in the prompt dialog has input focus.
*/
static void focusCB(Widget w, Widget dialog, caddr_t call_data)
{
    XtVaSetValues(dialog, XmNdefaultButton,
    	    XmSelectionBoxGetChild(dialog, XmDIALOG_OK_BUTTON), NULL);
}

/*
** Message and prompt dialogs hardwire the cancel button to the XmNcancelButton
** resource in the bulletin board dialog.  Since we rename the buttons, the
** cancel label may not be on the dialog's idea of the Cancel button.  The only
** way to make the accelerator for Cancel and Dismiss (the escape key) work
** correctly in this situation is to brutally catch and redirect the event.
** This routine redirects escape key events in the dialog to the callback for
** the button "whichBtn", passing it argument "df".  If "df" is NULL, simply
** block the event from reaching the dialog.
*/
static void addEscapeHandler(Widget dialog, struct dfcallbackstruct *df,
    	int whichBtn)
{
    XtAddEventHandler(dialog, KeyPressMask, False, whichBtn == APPLY_BTN ?
    	    escapeApplyCB : escapeHelpCB, (XtPointer)df);
    XtGrabKey(dialog, XKeysymToKeycode(XtDisplay(dialog), XK_Escape), 0,
	    True, GrabModeAsync, GrabModeAsync);
}

/*
** Event handler for escape key to redirect the event to the help button.
** Attached when cancel label appears on Help button.
*/
static void escapeHelpCB(Widget w, XtPointer callData, XEvent *event,
    	Boolean *cont)
{
    if (event->xkey.keycode != XKeysymToKeycode(XtDisplay(w), XK_Escape))
    	return;
    if (callData != NULL)
    	help_callback(w, (struct dfcallbackstruct *)callData, NULL);
    *cont = False;
}

/*
** Event handler for escape key to redirect event to the apply button.
** Attached when cancel label appears on Apply button.
*/
static void escapeApplyCB(Widget w, XtPointer callData, XEvent *event,
    	Boolean *cont)
{
    if (event->xkey.keycode != XKeysymToKeycode(XtDisplay(w), XK_Escape))
    	return;
    if (callData != NULL)
    	apply_callback(w, (struct dfcallbackstruct *)callData, NULL);
    *cont = False;
}

/*
** Only used by createMnemonics(Widget w)
*/
static void recurseCreateMnemonics(Widget w, Boolean *mnemonicUsed)
{
    WidgetList children;
    Cardinal   numChildren, i;

    XtVaGetValues(w,
                  XmNchildren,    &children,
                  XmNnumChildren, &numChildren,
                  NULL);

    for (i = 0; i < numChildren; i++)
    {
        Widget child = children[i];
        
        if (XtIsComposite(child))
        {
            recurseCreateMnemonics(child, mnemonicUsed);
        }
        else if (XtIsSubclass(child, xmPushButtonWidgetClass) ||
                 XtIsSubclass(child, xmPushButtonGadgetClass))
        {
            XmString xmslabel;
            char *label;
            int c;
            
            XtVaGetValues(child, XmNlabelString, &xmslabel, NULL);
            if (XmStringGetLtoR(xmslabel, XmSTRING_DEFAULT_CHARSET, &label))
            {
                /* Scan through the string to see if the label is already used */            
                int labelLen = strlen(label);
                for (c = 0; c < labelLen; c++)
                {
                    unsigned char lc = tolower((unsigned char)label[c]);

                    if (!mnemonicUsed[lc] && isalnum(lc))
                    {
                        mnemonicUsed[lc] = TRUE;
                        XtVaSetValues(child, XmNmnemonic, label[c], NULL);
                        break;
                    }
                }

                XtFree(label);
            }
            XmStringFree(xmslabel);
        }
    }
}

/*
** Automatically create mnemonics for a widget.  Traverse all it's
** children.  If the child is a push button, snag the first unused letter
** and make that the mnemonic.  This is useful for DialogF dialogs which
** can have arbitrary text in the buttons.
*/
static void createMnemonics(Widget w)
{
    Boolean mnemonicUsed[UCHAR_MAX + 1];
    
    memset(mnemonicUsed, FALSE, sizeof mnemonicUsed / sizeof *mnemonicUsed);
    recurseCreateMnemonics(w, mnemonicUsed);
}