~ubuntu-branches/ubuntu/utopic/gridengine/utopic

« back to all changes in this revision

Viewing changes to source/3rdparty/qmon/Xmt310/Xmt/AskForItem.c

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2008-06-25 22:36:13 UTC
  • Revision ID: james.westby@ubuntu.com-20080625223613-tvd9xlhuoct9kyhm
Tags: upstream-6.2~beta2
ImportĀ upstreamĀ versionĀ 6.2~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
 * Motif Tools Library, Version 3.1
 
3
 * $Id$
 
4
 * 
 
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.
 
10
 *
 
11
 * $Log$
 
12
 * Revision 1.1.1.1  2001/07/18 11:06:01  root
 
13
 * Initial checkin.
 
14
 *
 
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
 
19
 *
 
20
 *
 
21
 */
 
22
 
 
23
#include <Xmt/Xmt.h>
 
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>
 
29
#include <Xm/Text.h>
 
30
#include <Xm/List.h>
 
31
 
 
32
/* ARGSUSED */
 
33
#if NeedFunctionPrototypes
 
34
static void NoMatchCallback(Widget w, XtPointer tag, XtPointer data)
 
35
#else
 
36
static void NoMatchCallback(w, tag, data)
 
37
Widget w;
 
38
XtPointer tag;
 
39
XtPointer data;
 
40
#endif
 
41
{
 
42
    XmtDisplayError(w, XmtNO_MATCH_DIALOG,
 
43
                    XmtLocalize2(w, XmtNO_MATCH_MESSAGE,
 
44
                                 "XmtAskForItem", "noMatch"));
 
45
}
 
46
 
 
47
#if NeedFunctionPrototypes
 
48
static void CreateItemDialog(XmtPerScreenInfo *info)
 
49
#else
 
50
static void CreateItemDialog(info)
 
51
XmtPerScreenInfo *info;
 
52
#endif
 
53
{
 
54
    Widget dshell;
 
55
    Widget dialog;
 
56
    Arg args[5];
 
57
    int i;
 
58
 
 
59
    /* create the dialog shell */
 
60
    i = 0;
 
61
    XtSetArg(args[i], XmNallowShellResize, True); i++;
 
62
    dshell = XmCreateDialogShell(info->topmost_shell,
 
63
                                 XmtITEM_DIALOG_SHELL_NAME, args, i);
 
64
 
 
65
    /*
 
66
     * create the dialog box itself.
 
67
     * Use WORK_AREA type so we don't get an apply button.
 
68
     */
 
69
    i = 0;
 
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);
 
75
 
 
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);
 
82
 
 
83
    /* add a callback for f.close */
 
84
    XmtAddDeleteCallback(dshell, XmDO_NOTHING,
 
85
                         _XmtCancelCallback, (XtPointer)info);
 
86
 
 
87
    /*
 
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.
 
90
     */
 
91
    XtAddCallback(dialog, XmNnoMatchCallback, NoMatchCallback,(XtPointer)info);
 
92
    
 
93
    /* cache the dialog in the PerScreenInfo structure */
 
94
    info->item_dialog = dialog;
 
95
}
 
96
 
 
97
typedef struct {
 
98
    String message;
 
99
    String title;
 
100
    String list_title;
 
101
    String *items;
 
102
    String help_text;
 
103
    int visible_items;
 
104
} ItemDialogData;
 
105
 
 
106
static XtResource resources[] = {
 
107
{XmtNmessage, XmtCMessage, XtRString,
 
108
     sizeof(String), XtOffsetOf(ItemDialogData, message),
 
109
     XtRString, NULL},
 
110
{XmtNtitle, XmtCTitle, XtRString,
 
111
     sizeof(String), XtOffsetOf(ItemDialogData, title),
 
112
     XtRString, NULL},
 
113
{XmtNlistTitle, XmtCListTitle, XtRString,
 
114
     sizeof(String), XtOffsetOf(ItemDialogData, list_title),
 
115
     XtRString, NULL},
 
116
{XmtNitems, XmtCItems, XmtRStringList,
 
117
     sizeof(String *), XtOffsetOf(ItemDialogData, items),
 
118
     XtRImmediate, NULL},
 
119
{XmtNhelpText, XmtCHelpText, XtRString,
 
120
     sizeof(String), XtOffsetOf(ItemDialogData, help_text),
 
121
     XtRString, NULL},
 
122
{XmtNvisibleItems, XmtCVisibleItems, XtRInt,
 
123
     sizeof(int), XtOffsetOf(ItemDialogData, visible_items),
 
124
     XtRImmediate, (XtPointer)8}
 
125
};
 
126
 
 
127
 
 
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)
 
136
#else
 
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)
 
141
Widget w;
 
142
StringConst dialog_name;
 
143
StringConst prompt_default;
 
144
StringConst list_title_default;
 
145
String *items_default;
 
146
int num_items_default;
 
147
Boolean must_match;
 
148
String buffer_return;
 
149
int buffer_len;
 
150
int *int_return;
 
151
StringConst help_text_default;
 
152
Boolean return_number;
 
153
#endif
 
154
{
 
155
    Widget shell = XmtGetShell(w);
 
156
    XmtPerScreenInfo *info = XmtGetPerScreenInfo(shell);
 
157
    ItemDialogData data;
 
158
    Widget help_w;
 
159
    int num_items;
 
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;
 
166
    Widget text_widget;
 
167
    int i;
 
168
    static Boolean registered;
 
169
 
 
170
    /* make sure the shell we pop up over is not a menu shell! */
 
171
    while(XtIsOverrideShell(shell)) shell = XmtGetShell(XtParent(shell));
 
172
 
 
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");
 
177
 
 
178
    /* if there's no cached dialog, create one. */
 
179
    if (info->item_dialog == NULL) CreateItemDialog(info);
 
180
 
 
181
    /* if this dialog has a name, look up its resources */
 
182
    if (dialog_name != NULL) {
 
183
        if (!registered) {
 
184
            XmtRegisterStringListConverter();
 
185
            registered = True;
 
186
        }
 
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),
 
196
                          NULL, 0);
 
197
    }
 
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 */
 
205
    }
 
206
 
 
207
    /* create the XmStrings */
 
208
    message = XmtCreateLocalizedXmString(w, data.message);
 
209
    title = XmtCreateLocalizedXmString(w, data.title);
 
210
    list_title = XmtCreateLocalizedXmString(w, data.list_title);
 
211
 
 
212
    /*
 
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.
 
216
     */
 
217
    if (data.items != items_default) {
 
218
        for(num_items=0; data.items[num_items]; num_items++);
 
219
    }
 
220
    else {
 
221
        num_items = num_items_default;
 
222
    }
 
223
 
 
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]);
 
228
 
 
229
    if (return_number)
 
230
        default_item_label = item_labels[*int_return];
 
231
    else if (buffer_return && *buffer_return)
 
232
        default_item_label = XmtCreateLocalizedXmString(w, buffer_return);
 
233
    else
 
234
        default_item_label = item_labels[0];
 
235
 
 
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,
 
248
                  NULL);
 
249
 
 
250
    /*
 
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.
 
255
     */
 
256
    XmListDeselectAllItems(XmSelectionBoxGetChild(info->item_dialog,
 
257
                                                  XmDIALOG_LIST));
 
258
    XmListSelectItem(XmSelectionBoxGetChild(info->item_dialog, XmDIALOG_LIST),
 
259
                     default_item_label, False);
 
260
 
 
261
 
 
262
#if 0  /* this is a great idea, but it doesn't work */
 
263
 
 
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);
 
271
    }
 
272
    else {
 
273
        XtManageChild(list_w);
 
274
        XtManageChild(list_label_w);
 
275
    }
 
276
#endif
 
277
    
 
278
    /*
 
279
     * if there is help text, make the button sensitive,
 
280
     * and put help text where the callback procedure can find it.
 
281
     */
 
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;
 
286
    }
 
287
    else {
 
288
        XtSetSensitive(help_w, False);
 
289
        info->help_text = NULL;
 
290
    }
 
291
 
 
292
    /* destroy the XmStrings  */
 
293
    XmStringFree(message);
 
294
    XmStringFree(title);
 
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);
 
301
    
 
302
    /* Tell the dialog who it is transient for */
 
303
    XtVaSetValues(XtParent(info->item_dialog), XtNtransientFor, shell, NULL);
 
304
 
 
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);
 
311
 
 
312
    /*
 
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.
 
317
     */
 
318
    info->blocked = True;
 
319
    XmtBlock(shell, &info->blocked);
 
320
 
 
321
    /* pop down the dialog */
 
322
    XtUnmanageChild(info->item_dialog);
 
323
 
 
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.)
 
327
     */
 
328
    XSync(XtDisplay(info->item_dialog), 0);
 
329
    XmUpdateDisplay(info->item_dialog);
 
330
 
 
331
    /*
 
332
     * if the user clicked Ok, figure out the selected string
 
333
     * and set the return values.
 
334
     */
 
335
    if (info->button == XmtOkButton) {
 
336
        XtVaGetValues(info->item_dialog,
 
337
                      XmNtextString, &selected_item_label, NULL);
 
338
        XmStringGetLtoR(selected_item_label, XmSTRING_DEFAULT_CHARSET,
 
339
                        &selected_item);
 
340
        XmStringFree(selected_item_label);
 
341
 
 
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';
 
346
        }
 
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;
 
350
            *int_return = i;
 
351
        }
 
352
        XtFree(selected_item);
 
353
    }
 
354
 
 
355
    /* if user clicked Cancel, return False, else True */
 
356
    if (info->button == XmtCancelButton) return False;
 
357
    else return True;
 
358
}
 
359
 
 
360
 
 
361
 
 
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)
 
370
#else
 
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)
 
374
Widget w;
 
375
StringConst dialog_name;
 
376
StringConst prompt_default;
 
377
StringConst list_title_default;
 
378
String *items_default;
 
379
int num_items_default;
 
380
int must_match;
 
381
String buffer_return;
 
382
int buffer_len;
 
383
StringConst help_text_default;
 
384
#endif
 
385
{
 
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);
 
389
}
 
390
 
 
391
 
 
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)
 
399
#else
 
400
Boolean XmtAskForItemNumber(w, dialog_name, prompt_default, list_title_default,
 
401
                            items_default, num_items_default, value_return,
 
402
                            help_text_default)
 
403
Widget w;
 
404
StringConst dialog_name;
 
405
StringConst prompt_default;
 
406
StringConst list_title_default;
 
407
String *items_default;
 
408
int num_items_default;
 
409
int *value_return;
 
410
StringConst help_text_default;
 
411
#endif
 
412
{
 
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);
 
416
}