2
* Motif Tools Library, Version 3.1
5
* Written by David Flanagan.
6
* Copyright (c) 1992-2001 by David Flanagan.
7
* All Rights Reserved. See the file COPYRIGHT for details.
8
* This is open source software. See the file LICENSE for details.
9
* There is no warranty for this software. See NO_WARRANTY for details.
12
* Revision 1.1.1.1 2001/07/18 11:06:01 root
15
* Revision 1.1 2001/06/12 15:00:21 andre
16
* AA-2001-06-12-0: replaced Xmt212 by Xmt310
17
* (http://sourceforge.net/projects/motiftools) with
18
* our xmt212 patches applied
24
#include <Xmt/ScreenP.h>
25
#include <Xmt/DialogsP.h>
26
#include <Xmt/Converters.h>
27
#include <Xm/DialogS.h>
28
#include <Xm/SelectioB.h>
33
#if NeedFunctionPrototypes
34
static void NoMatchCallback(Widget w, XtPointer tag, XtPointer data)
36
static void NoMatchCallback(w, tag, data)
42
XmtDisplayError(w, XmtNO_MATCH_DIALOG,
43
XmtLocalize2(w, XmtNO_MATCH_MESSAGE,
44
"XmtAskForItem", "noMatch"));
47
#if NeedFunctionPrototypes
48
static void CreateItemDialog(XmtPerScreenInfo *info)
50
static void CreateItemDialog(info)
51
XmtPerScreenInfo *info;
59
/* create the dialog shell */
61
XtSetArg(args[i], XmNallowShellResize, True); i++;
62
dshell = XmCreateDialogShell(info->topmost_shell,
63
XmtITEM_DIALOG_SHELL_NAME, args, i);
66
* create the dialog box itself.
67
* Use WORK_AREA type so we don't get an apply button.
70
XtSetArg(args[i], XmNdialogStyle, XmDIALOG_FULL_APPLICATION_MODAL); i++;
71
XtSetArg(args[i], XmNdialogType, XmDIALOG_WORK_AREA); i++;
72
XtSetArg(args[i], XmNautoUnmanage, False); i++;
73
XtSetArg(args[i], XmNdefaultPosition, False); i++;
74
dialog = XmCreateSelectionBox(dshell, XmtITEM_DIALOG_NAME, args, i);
76
/* add callbacks on all the dialog buttons */
77
XtAddCallback(dialog, XmNokCallback, _XmtOkCallback, (XtPointer)info);
78
XtAddCallback(dialog, XmNcancelCallback,
79
_XmtCancelCallback, (XtPointer)info);
80
XtAddCallback(XmSelectionBoxGetChild(dialog, XmDIALOG_HELP_BUTTON),
81
XmNactivateCallback, _XmtHelpCallback, (XtPointer)info);
83
/* add a callback for f.close */
84
XmtAddDeleteCallback(dshell, XmDO_NOTHING,
85
_XmtCancelCallback, (XtPointer)info);
88
* If XmNmustMatch is True, the selection box does its own checking.
89
* We only need to add a callback to handle the no match case.
91
XtAddCallback(dialog, XmNnoMatchCallback, NoMatchCallback,(XtPointer)info);
93
/* cache the dialog in the PerScreenInfo structure */
94
info->item_dialog = dialog;
106
static XtResource resources[] = {
107
{XmtNmessage, XmtCMessage, XtRString,
108
sizeof(String), XtOffsetOf(ItemDialogData, message),
110
{XmtNtitle, XmtCTitle, XtRString,
111
sizeof(String), XtOffsetOf(ItemDialogData, title),
113
{XmtNlistTitle, XmtCListTitle, XtRString,
114
sizeof(String), XtOffsetOf(ItemDialogData, list_title),
116
{XmtNitems, XmtCItems, XmtRStringList,
117
sizeof(String *), XtOffsetOf(ItemDialogData, items),
119
{XmtNhelpText, XmtCHelpText, XtRString,
120
sizeof(String), XtOffsetOf(ItemDialogData, help_text),
122
{XmtNvisibleItems, XmtCVisibleItems, XtRInt,
123
sizeof(int), XtOffsetOf(ItemDialogData, visible_items),
124
XtRImmediate, (XtPointer)8}
128
#if NeedFunctionPrototypes
129
static Boolean GetItem(Widget w, StringConst dialog_name,
130
StringConst prompt_default,
131
StringConst list_title_default, String *items_default,
132
int num_items_default, Boolean must_match,
133
String buffer_return, int buffer_len,
134
int *int_return, StringConst help_text_default,
135
Boolean return_number)
137
static Boolean GetItem(w, dialog_name, prompt_default, list_title_default,
138
items_default, num_items_default, must_match,
139
buffer_return, buffer_len, int_return,
140
help_text_default, return_number)
142
StringConst dialog_name;
143
StringConst prompt_default;
144
StringConst list_title_default;
145
String *items_default;
146
int num_items_default;
148
String buffer_return;
151
StringConst help_text_default;
152
Boolean return_number;
155
Widget shell = XmtGetShell(w);
156
XmtPerScreenInfo *info = XmtGetPerScreenInfo(shell);
160
static String localized_title;
161
XmString message, title, list_title;
162
XmString *item_labels;
163
XmString default_item_label;
164
XmString selected_item_label;
165
String selected_item;
168
static Boolean registered;
170
/* make sure the shell we pop up over is not a menu shell! */
171
while(XtIsOverrideShell(shell)) shell = XmtGetShell(XtParent(shell));
173
/* localize the default title the first time through */
174
if (!localized_title)
175
localized_title = XmtLocalize2(w, XmtITEM_DIALOG_TITLE_DEFAULT,
176
"XmtAskForItem", "title");
178
/* if there's no cached dialog, create one. */
179
if (info->item_dialog == NULL) CreateItemDialog(info);
181
/* if this dialog has a name, look up its resources */
182
if (dialog_name != NULL) {
184
XmtRegisterStringListConverter();
187
resources[0].default_addr = (XtPointer) prompt_default;
188
resources[1].default_addr = (XtPointer) localized_title;
189
resources[2].default_addr = (XtPointer) list_title_default;
190
resources[3].default_addr = (XtPointer) items_default;
191
resources[4].default_addr = (XtPointer) help_text_default;
192
resources[5].default_addr = (XtPointer) 8;
193
XtGetSubresources(shell, (XtPointer)&data,
194
(String)dialog_name, XmtCItemDialog,
195
resources, XtNumber(resources),
198
else { /* otherwise use arguments as defaults */
199
data.message = (String) prompt_default;
200
data.title = localized_title;
201
data.list_title = (String)list_title_default;
202
data.items = items_default;
203
data.help_text = (String) help_text_default;
204
data.visible_items = 8; /* no argument for this one */
207
/* create the XmStrings */
208
message = XmtCreateLocalizedXmString(w, data.message);
209
title = XmtCreateLocalizedXmString(w, data.title);
210
list_title = XmtCreateLocalizedXmString(w, data.list_title);
213
* If we got an items string from the resource db,
214
* it is NULL-terminated, and we need to count the number
215
* of items. Otherwise we can just use num_items_default.
217
if (data.items != items_default) {
218
for(num_items=0; data.items[num_items]; num_items++);
221
num_items = num_items_default;
224
/* convert the list items to XmStrings */
225
item_labels = (XmString *) XtMalloc(num_items * sizeof(XmString));
226
for(i = 0; i < num_items; i++)
227
item_labels[i] = XmtCreateLocalizedXmString(w, data.items[i]);
230
default_item_label = item_labels[*int_return];
231
else if (buffer_return && *buffer_return)
232
default_item_label = XmtCreateLocalizedXmString(w, buffer_return);
234
default_item_label = item_labels[0];
236
/* set resources on the dialog */
237
XtVaSetValues(info->item_dialog,
238
XmNselectionLabelString, message,
239
XmNdialogTitle, title,
240
XmNlistLabelString, list_title,
241
XmNlistItems, item_labels,
242
XmNlistItemCount, num_items,
243
XmNlistVisibleItemCount,
244
(num_items < data.visible_items)
245
?((num_items>0)?num_items:1):data.visible_items,
246
XmNmustMatch, must_match,
247
XmNtextString, default_item_label,
251
* Be sure that the item selected in the list widget is the
252
* same as the item displayed in the text widget. This does
253
* not happen just by setting XmNtextString, and since we reuse
254
* this dialog, this is important to reset each time.
256
XmListDeselectAllItems(XmSelectionBoxGetChild(info->item_dialog,
258
XmListSelectItem(XmSelectionBoxGetChild(info->item_dialog, XmDIALOG_LIST),
259
default_item_label, False);
262
#if 0 /* this is a great idea, but it doesn't work */
264
/* if there are no items in the list, unmanage list and title */
265
list_w = XmSelectionBoxGetChild(info->item_dialog, XmDIALOG_LIST);
266
list_label_w = XmSelectionBoxGetChild(info->item_dialog,
267
XmDIALOG_LIST_LABEL);
268
if (num_items == 0) {
269
XtUnmanageChild(list_w);
270
XtUnmanageChild(list_label_w);
273
XtManageChild(list_w);
274
XtManageChild(list_label_w);
279
* if there is help text, make the button sensitive,
280
* and put help text where the callback procedure can find it.
282
help_w = XmSelectionBoxGetChild(info->item_dialog, XmDIALOG_HELP_BUTTON);
283
if ((data.help_text != NULL) && (data.help_text[0] != '\0')) {
284
XtSetSensitive(help_w, True);
285
info->help_text = data.help_text;
288
XtSetSensitive(help_w, False);
289
info->help_text = NULL;
292
/* destroy the XmStrings */
293
XmStringFree(message);
295
XmStringFree(list_title);
296
for(i = 0; i < num_items; i++)
297
XmStringFree(item_labels[i]);
298
XtFree((char *)item_labels);
299
if (!return_number && buffer_return && *buffer_return)
300
XmStringFree(default_item_label);
302
/* Tell the dialog who it is transient for */
303
XtVaSetValues(XtParent(info->item_dialog), XtNtransientFor, shell, NULL);
305
/* position, set initial focus, and pop up the dialog */
306
XmtDialogPosition(info->item_dialog, shell);
307
text_widget = XmSelectionBoxGetChild(info->item_dialog, XmDIALOG_TEXT);
308
XmtSetInitialFocus(info->item_dialog, text_widget);
309
XmTextSetInsertionPosition(text_widget,XmTextGetLastPosition(text_widget));
310
XtManageChild(info->item_dialog);
313
* Enter a recursive event loop.
314
* The callback registered on the okay and cancel buttons when
315
* this dialog was created will cause info->button to change
316
* when one of those buttons is pressed.
318
info->blocked = True;
319
XmtBlock(shell, &info->blocked);
321
/* pop down the dialog */
322
XtUnmanageChild(info->item_dialog);
324
/* make sure what is underneath gets cleaned up
325
* (the calling routine might act on the user's returned
326
* input and not handle events for awhile.)
328
XSync(XtDisplay(info->item_dialog), 0);
329
XmUpdateDisplay(info->item_dialog);
332
* if the user clicked Ok, figure out the selected string
333
* and set the return values.
335
if (info->button == XmtOkButton) {
336
XtVaGetValues(info->item_dialog,
337
XmNtextString, &selected_item_label, NULL);
338
XmStringGetLtoR(selected_item_label, XmSTRING_DEFAULT_CHARSET,
340
XmStringFree(selected_item_label);
342
/* put the string in the buffer */
343
if (!return_number) {
344
strncpy(buffer_return, selected_item, buffer_len-1);
345
buffer_return[buffer_len-1] = '\0';
347
else { /* or figure out the item number */
348
for(i = 0; i < num_items-1; i++)
349
if (strcmp(selected_item, data.items[i]) == 0) break;
352
XtFree(selected_item);
355
/* if user clicked Cancel, return False, else True */
356
if (info->button == XmtCancelButton) return False;
362
#if NeedFunctionPrototypes
363
Boolean XmtAskForItem(Widget w, StringConst dialog_name,
364
StringConst prompt_default,
365
StringConst list_title_default,
366
String *items_default,
367
int num_items_default, XmtWideBoolean must_match,
368
String buffer_return, int buffer_len,
369
StringConst help_text_default)
371
Boolean XmtAskForItem(w, dialog_name, prompt_default, list_title_default,
372
items_default, num_items_default, must_match,
373
buffer_return, buffer_len, help_text_default)
375
StringConst dialog_name;
376
StringConst prompt_default;
377
StringConst list_title_default;
378
String *items_default;
379
int num_items_default;
381
String buffer_return;
383
StringConst help_text_default;
386
return GetItem(w, dialog_name, prompt_default, list_title_default,
387
items_default, num_items_default, must_match,
388
buffer_return, buffer_len, NULL, help_text_default, False);
392
#if NeedFunctionPrototypes
393
Boolean XmtAskForItemNumber(Widget w, StringConst dialog_name,
394
StringConst prompt_default,
395
StringConst list_title_default,
396
String *items_default,
397
int num_items_default, int *value_return,
398
StringConst help_text_default)
400
Boolean XmtAskForItemNumber(w, dialog_name, prompt_default, list_title_default,
401
items_default, num_items_default, value_return,
404
StringConst dialog_name;
405
StringConst prompt_default;
406
StringConst list_title_default;
407
String *items_default;
408
int num_items_default;
410
StringConst help_text_default;
413
return GetItem(w, dialog_name, prompt_default, list_title_default,
414
items_default, num_items_default, True, NULL, 0,
415
value_return, help_text_default, True);