File: dialogs.c

package info (click to toggle)
xrn 9.02-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,784 kB
  • ctags: 3,047
  • sloc: ansic: 24,689; makefile: 2,240; yacc: 888; sh: 274; lex: 92; perl: 35; awk: 31; csh: 13
file content (366 lines) | stat: -rw-r--r-- 9,626 bytes parent folder | download
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

#if !defined(lint) && !defined(SABER) && !defined(GCC_WALL)
static char XRNrcsid[] = "$Id: dialogs.c,v 1.23 1998/07/05 14:39:13 jik Exp $";
/* Modified 2/20/92 dbrooks@osf.org to clean up dialog layout */
#endif

/*
 * xrn - an X-based NNTP news reader
 *
 * Copyright (c) 1988-1993, Ellen M. Sentovich and Rick L. Spickelmier.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of the University of California not
 * be used in advertising or publicity pertaining to distribution of 
 * the software without specific, written prior permission.  The University
 * of California makes no representations about the suitability of this
 * software for any purpose.  It is provided "as is" without express or
 * implied warranty.
 *
 * THE UNIVERSITY OF CALIFORNIA DISCLAIMS ALL WARRANTIES WITH REGARD TO 
 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND 
 * FITNESS, IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE FOR
 * ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * dialogs.c: create simple moded dialog boxes
 */

#include "copyright.h"
#include "config.h"
#include "utils.h"
#include "mesg_strings.h"
#include <X11/Xos.h>
#include <X11/StringDefs.h>
#include <X11/Intrinsic.h>
#include <X11/Shell.h>

#include <X11/Xaw/Command.h>
#include <X11/Xaw/Dialog.h>
#include <X11/Xaw/Text.h>

#include "xthelper.h"
#include "xmisc.h"
#include "xrn.h"
#include "dialogs.h"

#ifdef XRN_USE_STDARG
#include <stdarg.h>
#else
#include <varargs.h>
#endif

/*
 * find the closest ancestor of w which is a shell
 */
static Widget GetAncestorShell _ARGUMENTS((Widget));

static Widget GetAncestorShell(w)
    Widget w;
{
    while (w && (! XtIsShell(w)))
	w = XtParent(w);

    return (w);
}

/*
 * create a simple moded dialog box
 */
Widget CreateDialog(parent, title, textField, args, count)
    Widget parent;           /* parent window                         */
    char *title;             /* title of the dialog box               */
    char *textField;	     /* default text field                    */
    struct DialogArg *args;  /* description of the buttons            */
    unsigned int count;      /* number of buttons                     */
{
    Widget popup, dialog;
    Arg dargs[4];
    int cnt = 0;
    int i;
    Widget bb WALL(= 0);
    static Arg shellArgs[] = {
	{XtNallowShellResize, (XtArgVal) True},
	{XtNinput, (XtArgVal) True},
        {XtNtransientFor, (XtArgVal) NULL},
    };
    Widget typein;
    char *t = XtNewString(title), *p;

    p = t;
    while ((p = strchr(p, '\t')))
      *p = ' ';

    XtSetArg(shellArgs[2], XtNtransientFor, GetAncestorShell(parent));
    /* override does not get titlebar, transient does */
    popup = XtCreatePopupShell("popup", transientShellWidgetClass, parent,
			       shellArgs, XtNumber(shellArgs));
    
    /* create the dialog box */
    XtSetArg(dargs[cnt], XtNvalue, textField); cnt++;
    XtSetArg(dargs[cnt], XtNlabel, t); cnt++;
    XtSetArg(dargs[cnt], XtNinput, True); cnt++;
    dialog = XtCreateManagedWidget("dialog", dialogWidgetClass, popup, dargs, cnt);

    /* add the buttons */
    XtFree(t);
    for (i = 0; i < count; i++) {
	Arg bargs[2];
	static XtCallbackRec callbacks[] = {
	    {CBbusyCursor, NULL},
	    {NULL, NULL},
	    {CBunbusyCursor, NULL},
	    {NULL, NULL},
	};

	callbacks[1].callback = args[i].handler;
	callbacks[1].closure = args[i].data;

	XtSetArg(bargs[0], XtNlabel, args[i].buttonName);
	XtSetArg(bargs[1], XtNcallback, callbacks);
	if (i == count - 1) {
	    bb  = XtCreateManagedWidget("default", commandWidgetClass,
					dialog, bargs, XtNumber(bargs));
	} else {
	     XtCreateManagedWidget("command", commandWidgetClass,
				   dialog, bargs, XtNumber(bargs));
	}
    }

    if ((typein = XtNameToWidget(dialog, "value")) != 0) {
	XtSetKeyboardFocus(dialog, typein);
	XtSetKeyboardFocus(TopLevel, typein);
    }
	
    makeDefaultButton(bb);

    XtRealizeWidget(popup);

#ifndef ACCELERATORBUG
    XtInstallAccelerators(dialog, bb);
    XtInstallAccelerators(popup, bb);
#endif
    if (typein != 0) {
	XtInstallAccelerators(typein, bb);
	if (textField) {
	    /* force the text field to be 'big enough' */
	    XtCallActionProc(typein, "beginning-of-line", 0, 0, 0);
	}
    }
    return(popup);
}

#ifdef DECFOCUSPATCH
static void FocusPopUp _ARGUMENTS((Widget, XtPointer, XEvent *));

static void FocusPopUp(popup, data, event)
    Widget popup;
    XtPointer data;
    XEvent *event;
{
    if (event->type == MapNotify) {
	XSetInputFocus(XtDisplay(popup), XtWindow(popup),
		     RevertToPointerRoot,  CurrentTime);
	XtRemoveEventHandler(popup, XtAllEvents, True, FocusPopUp, 0);
    }
    return;
}
#endif

void PopUpDialog(popup)
    Widget popup;
{
    xthCenterWidgetOverCursor(popup);
    XtPopup(popup, XtGrabExclusive);
#ifdef DECFOCUSPATCH
    XtAddEventHandler(popup, StructureNotifyMask, False, FocusPopUp, 0);
#endif
    return;
}

/*
 * pop down the dialog (do not destroy, it will be used again)
 */
void PopDownDialog(dialog)
    Widget dialog;
{
    XtPopdown(dialog);
    XtDestroyWidget(dialog);
    return;
}

char * GetDialogValue(popup)
    Widget popup;
{
  
    return XawDialogGetValueString(XtNameToWidget(popup, "dialog"));
}

/*
 * simple confirmation box
 */

static int retVal;

static void cbHandler _ARGUMENTS((Widget, XtPointer, XtPointer));

/* ARGSUSED */
static void cbHandler(widget, client_data, call_data)
    Widget widget;
    XtPointer client_data;
    XtPointer call_data;
{
    retVal = (int) client_data;
    return;
}

/*
 * pop up a confirmation box and return either 'XRN_CB_ABORT' or
 * 'XRN_CB_CONTINUE'.  Either or both of the button1 and button2
 * arguments can be null; they default to "no" and "yes".  The second
 * button is the default.
 *
 * Always returns XRN_CB_ABORT if X isn't up (according to the
 * XRN_X_UP bit in the XRNState variable).
 */
int ConfirmationBox(
		    _ANSIDECL(Widget,	parent),
		    _ANSIDECL(char *,	message),
		    _ANSIDECL(char *,	button1),
		    _ANSIDECL(char *,	button2),
		    _ANSIDECL(Boolean,	continue_first)
		    )
     _KNRDECL(Widget,	parent)
     _KNRDECL(char *,	message)
     _KNRDECL(char *,	button1)
     _KNRDECL(char *,	button2)
     _KNRDECL(Boolean,	continue_first)
{
  int retval;

  if (! (XRNState & XRN_X_UP))
    return XRN_CB_ABORT;

  retval = ChoiceBox(parent, message, 2,
		     button1 ? button1 : NO_STRING,
		     button2 ? button2 : YES_STRING);

  if (continue_first)
    return ((retval == 1) ? XRN_CB_CONTINUE : XRN_CB_ABORT);
  else
    return ((retval == 1) ? XRN_CB_ABORT : XRN_CB_CONTINUE);
}

/*
 *
 * Pop up a box with an arbitrary number of buttons, and return the
 * number of the button that was selected (starting the count at 1).
 * The last button in the array is the default, and will be selected
 * if the user hits return instead of clicking on one of the buttons.
 */
#ifdef XRN_USE_STDARG
int ChoiceBox(Widget parent, char *message, int count, ...)
#else
int ChoiceBox(parent, message, count, va_alist)
     Widget parent;
     char *message;
     int count;
     va_dcl
#endif /* XRN_USE_STDARG */
{
  struct DialogArg *dialog_args;
  XEvent ev;
  Widget widget;
  XtAppContext app = XtWidgetToApplicationContext(parent);
  int i;
  va_list args;

  dialog_args = (struct DialogArg *) XtMalloc(sizeof(*dialog_args) * count);

#ifdef XRN_USE_STDARG
  va_start(args, count);
#else
  va_start(args);
#endif

  for (i = 0; i < count; i++) {
    dialog_args[i].buttonName = va_arg(args, char *);
    dialog_args[i].handler = cbHandler;
    dialog_args[i].data = (XtPointer) (i + 1);
  }

  va_end(args);

  retVal = -1;

  widget = CreateDialog(parent, message, DIALOG_NOTEXT, dialog_args, count);

  PopUpDialog(widget);

  for (;;) {
    XtAppNextEvent(app, &ev);
    (void) MyDispatchEvent(&ev);
    if (retVal != -1) {
      PopDownDialog(widget);
      XtFree((char *) dialog_args);
      return(retVal);
    }
  }
}

static int password_result;
static char *dialog_password;

static void passwordHandler _ARGUMENTS((Widget, XtPointer,
					XtPointer));

static void passwordHandler(widget, client_data, call_data)
     Widget widget;
     XtPointer client_data, call_data;
{
  Widget dialog = XtParent(XtParent(widget));

  password_result = (int) client_data;
  if (password_result == XRN_CB_CONTINUE) {
    dialog_password = GetDialogValue(dialog);
    dialog_password = XtNewString(dialog_password);
  }
  PopDownDialog(dialog);
  return;
}

String PasswordBox(widget, prompt)
     Widget widget;
     String prompt;
{
  Widget dialog;
  static struct DialogArg args[] = {
    {ABORT_STRING, passwordHandler, (XtPointer) XRN_CB_ABORT},
    {DOIT_STRING, passwordHandler, (XtPointer) XRN_CB_CONTINUE},
  };

  password_result = -1;

  dialog = CreateDialog(TopLevel, prompt, DIALOG_TEXT, args, XtNumber(args));
  XtVaSetValues(XtNameToWidget(dialog, "dialog.value"), XtNecho, 0, 0);
  PopUpDialog(dialog);

  while (password_result < 0) {
    XEvent ev;

    XtAppNextEvent(TopContext, &ev);
    MyDispatchEvent(&ev);
  }

  if (password_result == XRN_CB_CONTINUE)
    return dialog_password;
  else
    return 0;
}