Some compile fixes, not enough
[erwise] / Ui / UiSelectionBox.c
1 static char *rcsid = "$Id: UiSelectionBox.c,v 1.3 1992/03/26 18:13:50 kny Exp $";
2
3 #include "UiIncludes.h"
4
5
6 static Widget uicreatesbformdialog();
7 static Widget
8  uicreatesbfsbox(Widget formwdg,
9                   void (*callback) (char *nodename));
10 static Widget uicreatesbseparator(Widget formwdg, Widget fsboxwdg);
11 static Widget uicreatesbadd(Widget formwdg, Widget fsboxwdg,
12                              Widget separatorwdg);
13 static Widget uicreatesbdelete(Widget formwdg, Widget fsboxwdg);
14 static Widget uicreatesblist(Widget formwdg, Widget deletewdg,
15                               Widget separatorwdg);
16 static char *uifixselection(char *selection);
17
18 static void uiselectionboxokcb(Widget wdg, caddr_t callback,
19                                 XmPushButtonCallbackStruct * calldata);
20 static void uiselectionboxcancelcb(Widget wdg, caddr_t ignored,
21                                   XmPushButtonCallbackStruct * calldata);
22 static void uiselectionboxaddcb(Widget wdg, caddr_t ignored,
23                                  XmPushButtonCallbackStruct * calldata);
24 static void uiselectionboxdeletecb(Widget wdg, caddr_t ignored,
25                                   XmPushButtonCallbackStruct * calldata);
26 static void uiselectionboxclickcb(Widget wdg, caddr_t ignored,
27                                    XmListCallbackStruct * calldata);
28 static void uiselectionboxupdateconfig(void);
29
30
31 uiTopLevel_t uiTopLevel;
32 char **uiSelectionArray;
33
34
35 static int uidummy;
36
37
38 int UiDisplaySelectionBox(callback)
39 void (*callback) (char *nodename);
40 {
41     uiSelectionBoxGfx_t *sbgfx = &uiTopLevel.SBGfx;
42     XmString dummystr;
43     int i = 0;
44
45     if (sbgfx->FormWdg) {
46         XtMapWidget(XtParent(sbgfx->FormWdg));
47
48         return UI_OK;
49     }
50     sbgfx->FormWdg = uicreatesbformdialog();
51     sbgfx->FSBoxWdg = uicreatesbfsbox(sbgfx->FormWdg, callback);
52     sbgfx->SeparatorWdg = uicreatesbseparator(sbgfx->FormWdg, sbgfx->FSBoxWdg);
53     sbgfx->AddWdg = uicreatesbadd(sbgfx->FormWdg, sbgfx->FSBoxWdg,
54                                   sbgfx->SeparatorWdg);
55     sbgfx->DeleteWdg = uicreatesbdelete(sbgfx->FormWdg, sbgfx->FSBoxWdg);
56     sbgfx->ListWdg = uicreatesblist(sbgfx->FormWdg, sbgfx->DeleteWdg,
57                                     sbgfx->SeparatorWdg);
58
59     XtManageChild(sbgfx->FormWdg);
60     XtRealizeWidget(XtParent(sbgfx->FormWdg));
61
62     if (!uiSelectionArray) {
63         dummystr = XmStringCreateSimple(" ");
64         XmListAddItem(uiTopLevel.SBGfx.ListWdg, dummystr, 0);
65         XtSetSensitive(uiTopLevel.SBGfx.ListWdg, FALSE);
66         XmStringFree(dummystr);
67         uidummy = TRUE;
68     } else {
69         while (uiSelectionArray[i]) {
70             dummystr = XmStringCreateSimple(uiSelectionArray[i++]);
71             XmListAddItem(uiTopLevel.SBGfx.ListWdg, dummystr, 0);
72             XmStringFree(dummystr);
73         }
74         uidummy = FALSE;
75     }
76
77     return UI_OK;
78 }
79
80
81 static Widget
82  uicreatesbformdialog()
83 {
84     ArgList args;
85     Cardinal nargs;
86     Widget formwdg;
87     Widget topwdg;
88
89     topwdg = XtCreateApplicationShell("SelectionBox",
90                                       topLevelShellWidgetClass,
91                                       NULL, 0);
92
93     args = uiVaSetArgs(&nargs,
94                        XmNresizePolicy, XmRESIZE_NONE,
95                        XmNautoUnmanage, FALSE, NULL);
96     formwdg = XmCreateForm(topwdg, "SelectionBox",
97                            args, nargs);
98
99     return formwdg;
100 }
101
102
103 static Widget
104  uicreatesbfsbox(formwdg, callback)
105 Widget formwdg;
106 void (*callback) (char *nodename);
107 {
108     ArgList args;
109     Cardinal nargs;
110     Widget fsboxwdg, textwdg, okwdg, cancelwdg;
111
112     args = uiVaSetArgs(&nargs,
113                        XmNtopAttachment, XmATTACH_FORM,
114                        XmNtopOffset, 1,
115                        XmNleftAttachment, XmATTACH_FORM,
116                        XmNleftOffset, 1,
117                        XmNbottomOffset, 1,
118                        XmNbottomAttachment, XmATTACH_FORM, NULL);
119     fsboxwdg = XmCreateFileSelectionBox(formwdg, "FSBox",
120                                         args, nargs);
121     XtUnmanageChild(XmFileSelectionBoxGetChild(fsboxwdg,
122                                                XmDIALOG_HELP_BUTTON));
123
124     textwdg = XmFileSelectionBoxGetChild(fsboxwdg, XmDIALOG_TEXT);
125     XmTextSetString(textwdg, "");
126
127     okwdg = XmFileSelectionBoxGetChild(fsboxwdg, XmDIALOG_OK_BUTTON);
128     XtAddCallback(okwdg, XmNactivateCallback, uiselectionboxokcb,
129                   (caddr_t) callback);
130
131     cancelwdg = XmFileSelectionBoxGetChild(fsboxwdg, XmDIALOG_CANCEL_BUTTON);
132     XtAddCallback(cancelwdg, XmNactivateCallback, uiselectionboxcancelcb,
133                   (caddr_t) NULL);
134     XtManageChild(fsboxwdg);
135
136     return fsboxwdg;
137 }
138
139
140 static Widget
141  uicreatesbseparator(formwdg, fsboxwdg)
142 Widget formwdg;
143 Widget fsboxwdg;
144 {
145     ArgList args;
146     Cardinal nargs;
147     Widget separatorwdg;
148
149     args = uiVaSetArgs(&nargs,
150                        XmNorientation, XmVERTICAL,
151                        XmNleftAttachment, XmATTACH_WIDGET,
152                        XmNleftWidget, fsboxwdg,
153                        XmNleftOffset, 10,
154                        XmNtopAttachment, XmATTACH_FORM,
155                        XmNbottomAttachment, XmATTACH_FORM, NULL);
156     separatorwdg = XmCreateSeparatorGadget(formwdg, "Separator",
157                                            args, nargs);
158     XtManageChild(separatorwdg);
159
160     return separatorwdg;
161 }
162
163
164 static Widget
165  uicreatesbadd(formwdg, fsboxwdg, separatorwdg)
166 Widget formwdg;
167 Widget fsboxwdg;
168 Widget separatorwdg;
169 {
170     ArgList args;
171     Cardinal nargs;
172     Widget addwdg;
173
174     args = uiVaSetArgs(&nargs,
175                        XmNwidth, 70,
176                        XmNleftOffset, 20,
177                        XmNleftAttachment, XmATTACH_WIDGET,
178                        XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET,
179                        XmNbottomWidget, fsboxwdg,
180                        XmNbottomOffset, 20,
181                        XmNleftWidget, separatorwdg, NULL);
182     addwdg = XmCreatePushButtonGadget(formwdg, "Add",
183                                       args, nargs);
184     XtManageChild(addwdg);
185     XtAddCallback(addwdg, XmNactivateCallback, uiselectionboxaddcb,
186                   (caddr_t) NULL);
187
188     return addwdg;
189 }
190
191
192 static Widget
193  uicreatesbdelete(formwdg, fsboxwdg)
194 Widget formwdg;
195 Widget fsboxwdg;
196 {
197     ArgList args;
198     Cardinal nargs;
199     Widget deletewdg;
200
201     args = uiVaSetArgs(&nargs,
202                        XmNwidth, 70,
203                        XmNbottomAttachment, XmATTACH_OPPOSITE_WIDGET,
204                        XmNbottomWidget, fsboxwdg,
205                        XmNbottomOffset, 20,
206                        XmNrightOffset, 10,
207                        XmNrightAttachment, XmATTACH_FORM, NULL);
208     deletewdg = XmCreatePushButtonGadget(formwdg, "Delete",
209                                          args, nargs);
210     XtManageChild(deletewdg);
211     XtAddCallback(deletewdg, XmNactivateCallback, uiselectionboxdeletecb,
212                   (caddr_t) NULL);
213
214     return deletewdg;
215 }
216
217
218 static Widget
219  uicreatesblist(formwdg, deletewdg, separatorwdg)
220 Widget formwdg;
221 Widget deletewdg;
222 Widget separatorwdg;
223 {
224     ArgList args;
225     Cardinal nargs;
226     Widget listwdg;
227
228     args = uiVaSetArgs(&nargs,
229                        XmNvisibleItemCount, 15,
230                        XmNwidth, 200,
231                        XmNselectionPolicy, XmSINGLE_SELECT,
232                        XmNlistSizePolicy, XmCONSTANT,
233                        XmNscrollBarDisplayPolicy, XmSTATIC,
234                        XmNtopAttachment, XmATTACH_FORM,
235                        XmNtopOffset, 11,
236                        XmNbottomAttachment, XmATTACH_WIDGET,
237                        XmNbottomWidget, deletewdg,
238                        XmNbottomOffset, 20,
239                        XmNrightAttachment, XmATTACH_FORM,
240                        XmNrightOffset, 11,
241                        XmNleftOffset, 20,
242                        XmNleftAttachment, XmATTACH_WIDGET,
243                        XmNleftWidget, separatorwdg, NULL);
244     listwdg = XmCreateScrolledList(formwdg, "List",
245                                    args, nargs);
246     XtManageChild(listwdg);
247     XtAddCallback(listwdg, XmNdefaultActionCallback, uiselectionboxclickcb,
248                   (caddr_t) NULL);
249
250     return listwdg;
251 }
252
253
254 static char *
255  uifixselection(selection)
256 char *selection;
257 {
258     char *newselection;
259
260     if (!selection)
261         return selection;
262
263     if (index(selection, ':'))
264         return strdup(selection);
265
266     newselection = uiMalloc(strlen(selection) + strlen("file:") + 1);
267     strcpy(newselection, "file:");
268     strcat(newselection, selection);
269
270     return newselection;
271 }
272
273
274 static void uiselectionboxokcb(wdg, callback, calldata)
275 Widget wdg;
276 caddr_t callback;
277 XmPushButtonCallbackStruct *calldata;
278 {
279     Widget textwdg;
280     char *selection, *fixedselection;
281
282     textwdg = XmFileSelectionBoxGetChild(uiTopLevel.SBGfx.FSBoxWdg,
283                                          XmDIALOG_TEXT);
284     selection = XmTextGetString(textwdg);
285     fixedselection = uifixselection(selection);
286     XtUnmapWidget(XtParent(uiTopLevel.SBGfx.FormWdg));
287     (*(void (*) (char *nodename)) callback) (fixedselection);
288
289     XtFree(selection);
290     XtFree(fixedselection);
291 }
292
293
294 static void uiselectionboxcancelcb(wdg, ignored, calldata)
295 Widget wdg;
296 caddr_t ignored;
297 XmPushButtonCallbackStruct *calldata;
298 {
299     XtUnmapWidget(XtParent(uiTopLevel.SBGfx.FormWdg));
300 }
301
302
303 static void uiselectionboxaddcb(wdg, ignored, calldata)
304 Widget wdg;
305 caddr_t ignored;
306 XmPushButtonCallbackStruct *calldata;
307 {
308     Widget textwdg;
309     char *selection, *fixedselection;
310     XmString selectionstr;
311     int *poslist;
312     int poscount;
313
314     textwdg = XmFileSelectionBoxGetChild(uiTopLevel.SBGfx.FSBoxWdg,
315                                          XmDIALOG_TEXT);
316     selection = XmTextGetString(textwdg);
317     fixedselection = uifixselection(selection);
318
319     if (fixedselection && fixedselection[0]) {
320         selectionstr = XmStringCreateSimple(fixedselection);
321         if (uidummy) {
322             XmListDeletePos(uiTopLevel.SBGfx.ListWdg, 1);
323             XtSetSensitive(uiTopLevel.SBGfx.ListWdg, TRUE);
324             uidummy = FALSE;
325         }
326         if (XmListGetSelectedPos(uiTopLevel.SBGfx.ListWdg, &poslist,
327                                  &poscount)) {
328             XmListDeselectPos(uiTopLevel.SBGfx.ListWdg, poslist[0]);
329             XmListAddItem(uiTopLevel.SBGfx.ListWdg, selectionstr, poslist[0]);
330             XmListSelectPos(uiTopLevel.SBGfx.ListWdg, poslist[0] + 1, FALSE);
331             XtFree(poslist);
332         } else
333             XmListAddItem(uiTopLevel.SBGfx.ListWdg, selectionstr, 0);
334         XmStringFree(selectionstr);
335     }
336     XtFree(selection);
337     XtFree(fixedselection);
338
339     uiselectionboxupdateconfig();
340 }
341
342
343 static void uiselectionboxdeletecb(wdg, ignored, calldata)
344 Widget wdg;
345 caddr_t ignored;
346 XmPushButtonCallbackStruct *calldata;
347 {
348     int *poslist;
349     int poscount;
350     XmString dummystr;
351
352     /* We should have only one selected item */
353     if (XmListGetSelectedPos(uiTopLevel.SBGfx.ListWdg, &poslist, &poscount)) {
354         XmListDeletePos(uiTopLevel.SBGfx.ListWdg, poslist[0]);
355         XmListSelectPos(uiTopLevel.SBGfx.ListWdg, poslist[0], FALSE);
356         XtFree(poslist);
357     }
358     if (!uiGetArg(uiTopLevel.SBGfx.ListWdg, XmNitemCount)) {
359         dummystr = XmStringCreateSimple(" ");
360         XmListAddItem(uiTopLevel.SBGfx.ListWdg, dummystr, 0);
361         XmStringFree(dummystr);
362         XtSetSensitive(uiTopLevel.SBGfx.ListWdg, FALSE);
363         uidummy = TRUE;
364     }
365     uiselectionboxupdateconfig();
366 }
367
368
369 static void uiselectionboxclickcb(wdg, ignored, calldata)
370 Widget wdg;
371 caddr_t ignored;
372 XmListCallbackStruct *calldata;
373 {
374     Widget textwdg;
375     char *selection;
376
377     XmStringGetLtoR(calldata->item, XmSTRING_DEFAULT_CHARSET, &selection);
378     textwdg = XmFileSelectionBoxGetChild(uiTopLevel.SBGfx.FSBoxWdg,
379                                          XmDIALOG_TEXT);
380     XmTextSetString(textwdg, selection);
381     XtFree(selection);
382 }
383
384
385 static void uiselectionboxupdateconfig()
386 {
387     Widget listwdg = uiTopLevel.SBGfx.ListWdg;
388     int nitems, i;
389     char **items;
390     XmStringTable stringtable;
391     void *table;
392
393     nitems = uiGetArg(listwdg, XmNitemCount);
394     items = (char **) uiMalloc((nitems + 1) * sizeof(*items));
395
396     stringtable = (XmStringTable) uiGetArg(listwdg, XmNitems);
397     for (i = 0; i < nitems; i++)
398         XmStringGetLtoR(stringtable[i], XmSTRING_DEFAULT_CHARSET, &items[i]);
399     items[nitems] = (char *) NULL;
400
401     table = uiConfigPF((void *) NULL, C_DEFAULTS);
402     (void) uiConfigSetPF(table, C_DEFAULTSTABLE, (void *) items);
403 }