Debian Codegraph @

texk/xdvik/gui/sfSelFile.c

raw
1 /*
2 * Copyright 1989 Software Research Associates, Inc., Tokyo, Japan
3 *
4 * Permission to use, copy, modify, and distribute this software and its
5 * documentation for any purpose and without fee is hereby granted, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of Software Research Associates not be used
9 * in advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. Software Research Associates
11 * makes no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 * SOFTWARE RESEARCH ASSOCIATES DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
16 * IN NO EVENT SHALL SOFTWARE RESEARCH ASSOCIATES BE LIABLE FOR ANY SPECIAL,
17 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
18 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
19 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20 * PERFORMANCE OF THIS SOFTWARE.
21 *
22 * Author: Erik M. van der Poel
23 * Software Research Associates, Inc., Tokyo, Japan
24 * [email protected]
25 */
26
27 #include "xdvi-config.h"
28
29 #include "xdvi.h"
30 #include "dvi-init.h" /* for dviErrFlagT */
31 #include "message-window.h"
32
33 #include "kpathsea/c-stat.h"
34
35 #include <string.h>
36
37 #include "sfDir.h"
38 #include "sfPath.h"
39 #include "sfDraw.h"
40 #include "sfSelFile.h"
41 #include "util.h"
42 #include "xlwradio.h"
43 #include "x_util.h"
44
45 #include <ctype.h>
46 #include "kpathsea/c-fopen.h"
47 #include "kpathsea/c-stat.h"
48
49 #if !defined(MOTIF) /* entire file */
50
51 #include <errno.h>
52
53 #ifdef X_NOT_STDC_ENV
54 extern int errno;
55 #endif
56
57 #if HAVE_XKB_BELL_EXT
58 # include <X11/XKBlib.h>
59 # define sfBell(display, window, percent) \
60 XkbBell(display, window, percent, (Atom) None)
61 #else
62 # define sfBell(display, window, percent) XBell(display, percent)
63 #endif
64
65 #define SEL_FILE_CANCEL -1
66 #define SEL_FILE_OK 0
67 #define SEL_FILE_NULL 1
68 #define SEL_FILE_TEXT 2
69
70 /*
71 * Author's address:
72 *
73 * [email protected]
74 * OR
75 * erik%[email protected]
76 * OR
77 * erik%[email protected]
78 * OR
79 * try junet instead of co.jp
80 * OR
81 * Erik M. van der Poel
82 * Software Research Associates, Inc.
83 * 1-1-1 Hirakawa-cho, Chiyoda-ku
84 * Tokyo 102 Japan. TEL +81-3-234-2692
85 */
86
87 #include <sys/param.h>
88 #include <X11/cursorfont.h>
89 #include <X11/Intrinsic.h>
90 #include <X11/StringDefs.h>
91 #include <X11/Composite.h>
92 #include <X11/Shell.h>
93 #include <X11/Xaw/Paned.h>
94 #include <X11/Xaw/Text.h>
95 #include <X11/Xaw/AsciiText.h>
96 #include <X11/Xaw/Form.h>
97 #include <X11/Xaw/Command.h>
98 #include <X11/Xaw/Scrollbar.h>
99 #include <X11/Xaw/Label.h>
100 #include <X11/Xaw/Cardinals.h>
101
102
103 #ifndef MAXPATHLEN
104 #define MAXPATHLEN 1024
105 #endif /* ndef MAXPATHLEN */
106
107 /* global vars for communication with sfDraw.c */
108 char SFstartDir[MAXPATHLEN], SFcurrentPath[MAXPATHLEN], SFcurrentDir[MAXPATHLEN];
109 Widget selFileField, selFileForm, selFileHScroll, selFileHScrolls[3], selFileLists[3], selFileVScrolls[3];
110 Display *SFdisplay;
111 Pixel SFfore, SFback;
112 XSegment SFsegs[2], SFcompletionSegs[2];
113 XawTextPosition SFtextPos;
114 int SFupperX, SFlowerY, SFupperY;
115 int SFtextX, SFtextYoffset;
116 int SFentryWidth, SFentryHeight;
117 int SFlineToTextH = 3;
118 int SFlineToTextV = 3;
119 int SFbesideText = 3;
120 int SFaboveAndBelowText = 2;
121 int SFcharsPerEntry = 15;
122 int SFlistSize = 10;
123 int SFworkProcAdded = 0;
124 XtAppContext SFapp;
125 int SFpathScrollWidth, SFvScrollHeight, SFhScrollWidth;
126 char SFtextBuffer[MAXPATHLEN];
127 XtIntervalId SFdirModTimerId;
128 int (*SFfunc) (char *entryReal, char **entryShown, struct stat *statBuf);
129
130 static int SFstatus = SEL_FILE_NULL;
131
132 static Widget selFile, selFileCancel, selFileOK, selFilePrompt;
133 /* For file filter. */
134 static Widget selFileLabel, selFileMask, selFileHide;
135
136 #define MASKWIDTH 16
137 static char fileMask[MASKWIDTH + 2] = "*.dvi";
138
139 static Atom SFwmDeleteWindow;
140
141 static char *oneLineTextEditTranslations =
142 "<Key>Return: redraw-display()\n"
143 "Ctrl<Key>M: redraw-display()\n";
144
145 #if !defined (HAVE_STRERROR) && !defined (strerror)
146 static char *
147 strerror(int errnum)
148 {
149 return 0 < errnum && errnum <= sys_nerr
150 ? sys_errlist[errnum] : "Unknown system error";
151 }
152 #endif /* not HAVE_STRERROR && not strerror */
153
154 void
155 raise_file_selector(void)
156 {
157 if (selFile != NULL && XtIsManaged(selFile)) {
158 sfBell(DISP, XtWindow(selFile), 10);
159 XRaiseWindow(DISP, XtWindow(selFile));
160 return;
161 }
162 }
163
164 static void
165 SFexposeList(Widget w, XtPointer n, XEvent *event, Boolean *cont)
166 {
167 UNUSED(w);
168 UNUSED(cont);
169
170 if ((event->type == NoExpose) || event->xexpose.count) {
171 return;
172 }
173
174 SFdrawList((ptrdiff_t)n, SF_DO_NOT_SCROLL);
175 }
176
177
178 static void
179 cb_open_new_window(Widget w, XtPointer client_data, XtPointer call_data)
180 {
181 Boolean set;
182
183 UNUSED(client_data);
184 UNUSED(call_data);
185
186 XtVaGetValues(w, XtNstate, &set, NULL);
187
188 if (set)
189 resource.filesel_open_new_window = True;
190 else
191 resource.filesel_open_new_window = False;
192 store_preference(NULL, "fileselOpenNewWindow", "%d", resource.filesel_open_new_window);
193 }
194
195 static void
196 SFmodVerifyCallback(Widget w, XtPointer client_data, XEvent *event, Boolean *cont)
197 {
198 char buf[2];
199
200 UNUSED(w);
201 UNUSED(client_data);
202 UNUSED(cont);
203
204 if ((XLookupString(&(event->xkey), buf, 2, NULL, NULL) == 1) &&
205 ((*buf) == '\r')) {
206 SFstatus = SEL_FILE_OK;
207 }
208 else {
209 SFstatus = SEL_FILE_TEXT;
210 }
211 }
212
213 static void
214 SFokCallback(Widget w, XtPointer cl, XtPointer cd)
215 {
216 UNUSED(w);
217 UNUSED(cl);
218 UNUSED(cd);
219
220 SFstatus = SEL_FILE_OK;
221 }
222
223 static XtCallbackRec SFokSelect[] = {
224 {SFokCallback, (XtPointer) NULL},
225 {NULL, (XtPointer) NULL},
226 };
227
228 static void
229 SFcancelCallback(Widget w, XtPointer cl, XtPointer cd)
230 {
231 UNUSED(w);
232 UNUSED(cl);
233 UNUSED(cd);
234
235 SFstatus = SEL_FILE_CANCEL;
236 }
237
238 static XtCallbackRec SFcancelSelect[] = {
239 {SFcancelCallback, (XtPointer) NULL},
240 {NULL, (XtPointer) NULL},
241 };
242
243 static void
244 SFdismissAction(Widget w, XEvent *event, String *params, Cardinal *num_params)
245 {
246 UNUSED(w);
247 UNUSED(params);
248 UNUSED(num_params);
249
250 if (event->type == ClientMessage && (unsigned)(event->xclient.data.l[0]) != SFwmDeleteWindow)
251 return;
252
253 SFstatus = SEL_FILE_CANCEL;
254 }
255
256 static char *wmDeleteWindowTranslation = "<Message>WM_PROTOCOLS: SelFileDismiss()\n";
257
258 static XtActionsRec sf_actions[] = {
259 {"SelFileDismiss", SFdismissAction},
260 };
261
262 /* Don't show files that don't get through the filter. */
263
264 /* return 1 if file is masked (mask does not match filename), 0 otherwise */
265 static int
266 maskFile(const char *mask, char *filename)
267 {
268 int c, c1;
269
270 while ((c = *mask++)) {
271 if (c == '*') {
272 while ((c1 = *mask++)) {
273 if (c1 != '*') {
274 if (!(*filename))
275 return 1;
276 if (c1 != '?') {
277 while ((filename = strchr(filename, c1))) {
278 if (!maskFile(mask, ++filename))
279 return 0;
280 }
281 return 1;
282 }
283 else
284 filename++;
285 }
286 }
287 return 0;
288 }
289 if (c == '?') {
290 if (!*filename)
291 return 1;
292 }
293 else if (c != *filename)
294 return 1;
295 filename++;
296 }
297 return (*filename) ? 1 : 0;
298 }
299
300 Boolean hideFlag = False;
301 static int
302 showEntry(char *entryReal, char **entryShown, struct stat *statBuf)
303 {
304 UNUSED(entryShown);
305
306 if ((hideFlag && entryReal[0] == '.') || (!S_ISDIR(statBuf->st_mode) && maskFile(fileMask, entryReal)))
307 return 0;
308 entryReal[strlen(entryReal)] = SFstatChar(statBuf);
309 return 1;
310 }
311
312 static void
313 maskChanged(Widget w, XtPointer client_data, XEvent *call_data, Boolean *cont)
314 {
315 char buf[2];
316 SFDir *dir;
317
318 UNUSED(w);
319 UNUSED(client_data);
320 UNUSED(cont);
321
322 if ((XLookupString((XKeyPressedEvent *)call_data, buf, 2, NULL, NULL) == 1) && ((*buf) == '\r')) {
323 for (dir = &(SFdirs[SFdirEnd - 1]); dir >= SFdirs; dir--)
324 *(dir->dir) = 0; /* force a re-read */
325 SFupdatePath();
326 }
327 }
328
329 static void
330 hideFiles(Widget w, XtPointer client_data, XtPointer call_data)
331 {
332 SFDir *dir;
333 SFEntry *entry;
334
335 UNUSED(client_data);
336 UNUSED(call_data);
337
338 hideFlag = !hideFlag;
339 if (hideFlag) {
340 XtVaSetValues(w, XtNlabel, "hidden", NULL);
341 for (dir = &(SFdirs[SFdirEnd - 1]); dir >= SFdirs; dir--) {
342 if (!(dir->nEntries))
343 continue;
344 dir->vOrigin = 0;
345 for (entry = &(dir->entries[dir->nEntries - 1]);
346 entry >= dir->entries; entry--)
347 entry->statDone = 0;
348 SFdrawLists(SF_DO_SCROLL);
349 }
350 }
351 else {
352 XtVaSetValues(w, XtNlabel, "shown", NULL);
353 for (dir = &(SFdirs[SFdirEnd - 1]); dir >= SFdirs; dir--)
354 *(dir->dir) = 0; /* force a re-read */
355 SFupdatePath();
356 }
357 }
358
359
360 static Widget
361 SFcreateWidgets(Widget parent, struct filesel_callback *callback)
362 {
363 Widget open_menu = NULL;
364 Cardinal i, n;
365 int listWidth, listHeight;
366 int listSpacing = 10;
367 int scrollThickness = 15;
368 int hScrollX, hScrollY;
369 int vScrollX, vScrollY;
370 Cursor xtermCursor, sbRightArrowCursor, arrowCursor;
371 Arg arglist[20];
372 Widget paned, box;
373
374 i = 0;
375 XtSetArg(arglist[i], XtNtransientFor, parent); i++;
376 XtSetArg(arglist[i], XtNtitle, callback->title); i++;
377
378 selFile = XtAppCreateShell("xdviSelFile", "XdviSelFile",
379 transientShellWidgetClass, SFdisplay,
380 arglist, i);
381
382 /* Add WM_DELETE_WINDOW protocol */
383 XtAppAddActions(XtWidgetToApplicationContext(selFile),
384 sf_actions, XtNumber(sf_actions));
385 XtOverrideTranslations(selFile,
386 XtParseTranslationTable(wmDeleteWindowTranslation));
387
388 paned = XtVaCreateManagedWidget("paned", panedWidgetClass, selFile, NULL);
389
390 i = 0;
391 XtSetArg(arglist[i], XtNdefaultDistance, 6); i++;
392
393 selFileForm = XtCreateManagedWidget("selFileForm",
394 formWidgetClass, paned, arglist, i);
395
396 i = 0;
397 XtSetArg(arglist[i], XtNlabel, callback->prompt); i++;
398 XtSetArg(arglist[i], XtNresizable, True); i++;
399 XtSetArg(arglist[i], XtNtop, XtChainTop); i++;
400 XtSetArg(arglist[i], XtNbottom, XtChainTop); i++;
401 XtSetArg(arglist[i], XtNleft, XtChainLeft); i++;
402 XtSetArg(arglist[i], XtNright, XtChainLeft); i++;
403 XtSetArg(arglist[i], XtNborderWidth, 0); i++;
404 XtSetArg(arglist[i], XtNvertDistance, 20); i++;
405
406 selFilePrompt = XtCreateManagedWidget("selFilePrompt",
407 labelWidgetClass, selFileForm,
408 arglist, i);
409
410 #if 1
411 i = 0;
412 XtSetArg(arglist[i], XtNforeground, &SFfore); i++;
413 XtSetArg(arglist[i], XtNbackground, &SFback); i++;
414 XtGetValues(selFilePrompt, arglist, i);
415 #endif
416
417 SFinitFont();
418
419 SFentryWidth = SFbesideText + SFcharsPerEntry * SFcharWidth + SFbesideText;
420 SFentryHeight = SFaboveAndBelowText + SFcharHeight + SFaboveAndBelowText;
421
422 listWidth = SFlineToTextH + SFentryWidth + SFlineToTextH + 1 +
423 scrollThickness;
424 listHeight = SFlineToTextV + SFentryHeight + SFlineToTextV + 1 +
425 SFlineToTextV + SFlistSize * SFentryHeight +
426 SFlineToTextV + 1 + scrollThickness;
427
428 SFpathScrollWidth = 3 * listWidth + 2 * listSpacing + 4;
429
430 hScrollX = -1;
431 hScrollY = SFlineToTextV + SFentryHeight + SFlineToTextV + 1 +
432 SFlineToTextV + SFlistSize * SFentryHeight + SFlineToTextV;
433 SFhScrollWidth = SFlineToTextH + SFentryWidth + SFlineToTextH;
434
435 vScrollX = SFlineToTextH + SFentryWidth + SFlineToTextH;
436 vScrollY = SFlineToTextV + SFentryHeight + SFlineToTextV;
437 SFvScrollHeight = SFlineToTextV + SFlistSize * SFentryHeight +
438 SFlineToTextV;
439
440 SFupperX = SFlineToTextH + SFentryWidth + SFlineToTextH - 1;
441 SFlowerY = SFlineToTextV + SFentryHeight + SFlineToTextV + 1 +
442 SFlineToTextV;
443 SFupperY = SFlineToTextV + SFentryHeight + SFlineToTextV + 1 +
444 SFlineToTextV + SFlistSize * SFentryHeight - 1;
445
446 SFtextX = SFlineToTextH + SFbesideText;
447 SFtextYoffset = SFlowerY + SFaboveAndBelowText + SFcharAscent;
448
449 SFsegs[0].x1 = 0;
450 SFsegs[0].y1 = vScrollY;
451 SFsegs[0].x2 = vScrollX - 1;
452 SFsegs[0].y2 = vScrollY;
453 SFsegs[1].x1 = vScrollX;
454 SFsegs[1].y1 = 0;
455 SFsegs[1].x2 = vScrollX;
456 SFsegs[1].y2 = vScrollY - 1;
457
458 SFcompletionSegs[0].x1 = SFcompletionSegs[0].x2 = SFlineToTextH;
459 SFcompletionSegs[1].x1 = SFcompletionSegs[1].x2 =
460 SFlineToTextH + SFentryWidth - 1;
461
462 i = 0;
463 XtSetArg(arglist[i], XtNwidth,
464 3 * listWidth + 2 * listSpacing + 4); i++;
465 XtSetArg(arglist[i], XtNfromVert, selFilePrompt); i++;
466 XtSetArg(arglist[i], XtNresizable, True); i++;
467 XtSetArg(arglist[i], XtNtop, XtChainTop); i++;
468 XtSetArg(arglist[i], XtNbottom, XtChainTop); i++;
469 XtSetArg(arglist[i], XtNleft, XtChainLeft); i++;
470 XtSetArg(arglist[i], XtNright, XtChainLeft); i++;
471 XtSetArg(arglist[i], XtNstring, SFtextBuffer); i++;
472 XtSetArg(arglist[i], XtNlength, MAXPATHLEN); i++;
473 XtSetArg(arglist[i], XtNeditType, XawtextEdit); i++;
474 XtSetArg(arglist[i], XtNwrap, XawtextWrapWord); i++;
475 XtSetArg(arglist[i], XtNresize, XawtextResizeHeight); i++;
476 XtSetArg(arglist[i], XtNuseStringInPlace, True); i++;
477 XtSetArg(arglist[i], XtNvertDistance, 5); i++;
478
479 selFileField = XtCreateManagedWidget("selFileField",
480 asciiTextWidgetClass, selFileForm,
481 arglist, i);
482
483 XtOverrideTranslations(selFileField,
484 XtParseTranslationTable
485 (oneLineTextEditTranslations));
486 /* XtSetKeyboardFocus(selFileForm, selFileField);
487 need focus for selFileMask widget to set the filter */
488
489 i = 0;
490 XtSetArg(arglist[i], XtNorientation, XtorientHorizontal); i++;
491 XtSetArg(arglist[i], XtNwidth, SFpathScrollWidth); i++;
492 XtSetArg(arglist[i], XtNheight, scrollThickness); i++;
493 XtSetArg(arglist[i], XtNfromVert, selFileField); i++;
494 XtSetArg(arglist[i], XtNvertDistance, 30); i++;
495 XtSetArg(arglist[i], XtNtop, XtChainTop); i++;
496 XtSetArg(arglist[i], XtNbottom, XtChainTop); i++;
497 XtSetArg(arglist[i], XtNleft, XtChainLeft); i++;
498 XtSetArg(arglist[i], XtNright, XtChainLeft); i++;
499
500 selFileHScroll = XtCreateManagedWidget("selFileHScroll",
501 scrollbarWidgetClass, selFileForm,
502 arglist, i);
503
504 XtAddCallback(selFileHScroll, XtNjumpProc,
505 (XtCallbackProc)SFpathSliderMovedCallback, (XtPointer) NULL);
506 XtAddCallback(selFileHScroll, XtNscrollProc,
507 (XtCallbackProc)SFpathAreaSelectedCallback, (XtPointer) NULL);
508
509 i = 0;
510 XtSetArg(arglist[i], XtNwidth, listWidth); i++;
511 XtSetArg(arglist[i], XtNheight, listHeight); i++;
512 XtSetArg(arglist[i], XtNfromVert, selFileHScroll); i++;
513 XtSetArg(arglist[i], XtNvertDistance, 10); i++;
514 XtSetArg(arglist[i], XtNtop, XtChainTop); i++;
515 XtSetArg(arglist[i], XtNbottom, XtChainTop); i++;
516 XtSetArg(arglist[i], XtNleft, XtChainLeft); i++;
517 XtSetArg(arglist[i], XtNright, XtChainLeft); i++;
518
519 selFileLists[0] = XtCreateManagedWidget("selFileList1",
520 compositeWidgetClass, selFileForm,
521 arglist, i);
522
523 i = 0;
524 XtSetArg(arglist[i], XtNwidth, listWidth); i++;
525 XtSetArg(arglist[i], XtNheight, listHeight); i++;
526 XtSetArg(arglist[i], XtNfromHoriz, selFileLists[0]); i++;
527 XtSetArg(arglist[i], XtNfromVert, selFileHScroll); i++;
528 XtSetArg(arglist[i], XtNhorizDistance, listSpacing); i++;
529 XtSetArg(arglist[i], XtNvertDistance, 10); i++;
530 XtSetArg(arglist[i], XtNtop, XtChainTop); i++;
531 XtSetArg(arglist[i], XtNbottom, XtChainTop); i++;
532 XtSetArg(arglist[i], XtNleft, XtChainLeft); i++;
533 XtSetArg(arglist[i], XtNright, XtChainLeft); i++;
534
535 selFileLists[1] = XtCreateManagedWidget("selFileList2",
536 compositeWidgetClass, selFileForm,
537 arglist, i);
538
539 i = 0;
540 XtSetArg(arglist[i], XtNwidth, listWidth); i++;
541 XtSetArg(arglist[i], XtNheight, listHeight); i++;
542 XtSetArg(arglist[i], XtNfromHoriz, selFileLists[1]); i++;
543 XtSetArg(arglist[i], XtNfromVert, selFileHScroll); i++;
544 XtSetArg(arglist[i], XtNhorizDistance, listSpacing); i++;
545 XtSetArg(arglist[i], XtNvertDistance, 10); i++;
546 XtSetArg(arglist[i], XtNtop, XtChainTop); i++;
547 XtSetArg(arglist[i], XtNbottom, XtChainTop); i++;
548 XtSetArg(arglist[i], XtNleft, XtChainLeft); i++;
549 XtSetArg(arglist[i], XtNright, XtChainLeft); i++;
550
551 selFileLists[2] = XtCreateManagedWidget("selFileList3",
552 compositeWidgetClass, selFileForm,
553 arglist, i);
554
555 for (n = 0; n < 3; n++) {
556
557 i = 0;
558 XtSetArg(arglist[i], XtNx, vScrollX); i++;
559 XtSetArg(arglist[i], XtNy, vScrollY); i++;
560 XtSetArg(arglist[i], XtNwidth, scrollThickness); i++;
561 XtSetArg(arglist[i], XtNheight, SFvScrollHeight); i++;
562
563 selFileVScrolls[n] = XtCreateManagedWidget("selFileVScroll",
564 scrollbarWidgetClass,
565 selFileLists[n], arglist, i);
566
567 XtAddCallback(selFileVScrolls[n], XtNjumpProc,
568 (XtCallbackProc)SFvFloatSliderMovedCallback, cast_int_to_XtPointer(n));
569 XtAddCallback(selFileVScrolls[n], XtNscrollProc,
570 (XtCallbackProc)SFvAreaSelectedCallback, cast_int_to_XtPointer(n));
571
572 i = 0;
573 XtSetArg(arglist[i], XtNorientation, XtorientHorizontal); i++;
574 XtSetArg(arglist[i], XtNx, hScrollX); i++;
575 XtSetArg(arglist[i], XtNy, hScrollY); i++;
576 XtSetArg(arglist[i], XtNwidth, SFhScrollWidth); i++;
577 XtSetArg(arglist[i], XtNheight, scrollThickness); i++;
578
579 selFileHScrolls[n] = XtCreateManagedWidget("selFileHScroll",
580 scrollbarWidgetClass,
581 selFileLists[n], arglist, i);
582
583 XtAddCallback(selFileHScrolls[n], XtNjumpProc,
584 (XtCallbackProc)SFhSliderMovedCallback, cast_int_to_XtPointer(n));
585 XtAddCallback(selFileHScrolls[n], XtNscrollProc,
586 (XtCallbackProc)SFhAreaSelectedCallback, cast_int_to_XtPointer(n));
587 }
588
589 /* When opening a DVI file, offer to open in new window */
590 if (callback->must_exist) {
591 open_menu = XtVaCreateManagedWidget(Xdvi_NEW_WINDOW_RADIO_NAME,
592 #ifdef XAW
593 radioWidgetClass,
594 #else
595 toggleWidgetClass,
596 #endif
597 selFileForm,
598 #ifdef XAW
599 XtNisRadio, False,
600 #endif
601 XtNfromVert, selFileLists[0],
602 XtNvertDistance, 30,
603 XtNhighlightThickness, 1,
604 XtNborderWidth, 0,
605 XtNlabel, "Open file in new window",
606 XtNstate, resource.filesel_open_new_window,
607 NULL);
608 XtAddCallback(open_menu, XtNcallback, cb_open_new_window, (XtPointer)NULL);
609 }
610
611 /* Do the file filter stuff. */
612 selFileLabel = XtVaCreateManagedWidget("selFileLabel",
613 labelWidgetClass, selFileForm,
614 XtNfromVert, callback->must_exist ? open_menu : selFileLists[0],
615 XtNvertDistance, callback->must_exist ? 10 : 30,
616 /* XtNfromHoriz, selFileCancel, */
617 /* XtNhorizDistance, 60, */
618 XtNlabel, "File Mask:",
619 XtNborderWidth, 0,
620 XtNtop, XtChainTop,
621 XtNbottom, XtChainTop, NULL);
622
623 selFileMask = XtVaCreateManagedWidget("selFileMask",
624 asciiTextWidgetClass, selFileForm,
625 XtNwidth, MASKWIDTH / 2 * SFcharWidth,
626 XtNfromVert, callback->must_exist ? open_menu : selFileLists[0],
627 XtNvertDistance, callback->must_exist ? 10 : 30,
628 XtNfromHoriz, selFileLabel,
629 XtNhorizDistance, 0,
630 XtNtop, XtChainTop,
631 XtNbottom, XtChainTop,
632 XtNstring, fileMask,
633 XtNlength, MASKWIDTH,
634 XtNeditType, XawtextEdit,
635 XtNwrap, XawtextWrapNever,
636 XtNuseStringInPlace, True, NULL);
637
638 for (i = 0; i < 3; i++)
639 XtSetKeyboardFocus(selFileLists[i], selFileField);
640
641 XtOverrideTranslations(selFileMask,
642 XtParseTranslationTable
643 (oneLineTextEditTranslations));
644
645 XtAddEventHandler(selFileMask, KeyPressMask, False,
646 (XtEventHandler)maskChanged, (XtPointer) NULL);
647
648 selFileLabel = XtVaCreateManagedWidget("selFileLabel",
649 labelWidgetClass, selFileForm,
650 XtNfromVert, callback->must_exist ? open_menu : selFileLists[0],
651 XtNvertDistance, callback->must_exist ? 10 : 30,
652 XtNfromHoriz, selFileMask,
653 XtNhorizDistance, 40,
654 XtNlabel, "Dot files are:",
655 XtNborderWidth, 0,
656 XtNtop, XtChainTop,
657 XtNbottom, XtChainTop, NULL);
658
659 selFileHide = XtVaCreateManagedWidget("selFileHide",
660 commandWidgetClass, selFileForm,
661 /* XtNwidth, 7 * SFcharWidth, */
662 XtNfromVert, callback->must_exist ? open_menu : selFileLists[0],
663 XtNvertDistance, callback->must_exist ? 10 : 30,
664 XtNfromHoriz, selFileLabel,
665 XtNhorizDistance, 2,
666 XtNlabel, hideFlag ? "hidden" : "shown",
667 /* XtNborderWidth, 1, */
668 XtNtop, XtChainTop,
669 /* XtNjustify, XtJustifyLeft, */
670 XtNbottom, XtChainTop,
671 NULL);
672 XtAddCallback(selFileHide, XtNcallback, (XtCallbackProc)hideFiles, NULL);
673
674 box = XtVaCreateManagedWidget("box", formWidgetClass,
675 paned,
676 XtNshowGrip, False,
677 XtNdefaultDistance, 6,
678 XtNskipAdjust, True,
679 XtNaccelerators, G_accels_cr,
680 NULL);
681 selFileOK = XtVaCreateManagedWidget("selFileOK", commandWidgetClass,
682 box,
683 XtNlabel, callback->ok,
684 XtNcallback, SFokSelect,
685 XtNtop, XtChainTop,
686 XtNbottom, XtChainBottom,
687 XtNleft, XtChainLeft,
688 XtNright, XtChainLeft,
689 NULL);
690 selFileCancel = XtVaCreateManagedWidget("selFileCancel", commandWidgetClass,
691 box,
692 XtNlabel, callback->cancel,
693 XtNcallback, SFcancelSelect,
694 /* XtNborderColor, SFfore, */
695 XtNfromHoriz, selFileOK,
696 XtNbottom, XtChainBottom,
697 XtNleft, XtChainRight,
698 XtNright, XtChainRight,
699 NULL);
700
701 XtSetMappedWhenManaged(selFile, False);
702 XtRealizeWidget(selFile);
703
704 /* Add WM_DELETE_WINDOW protocol */
705 SFwmDeleteWindow = XInternAtom(SFdisplay, "WM_DELETE_WINDOW", False);
706 XSetWMProtocols(SFdisplay, XtWindow(selFile), &SFwmDeleteWindow, 1);
707
708 SFcreateGC();
709
710 xtermCursor = XCreateFontCursor(SFdisplay, XC_xterm);
711
712 sbRightArrowCursor = XCreateFontCursor(SFdisplay, XC_sb_right_arrow);
713 arrowCursor = XCreateFontCursor(SFdisplay, XC_left_ptr);
714
715 XDefineCursor(SFdisplay, XtWindow(selFileForm), arrowCursor);
716 XDefineCursor(SFdisplay, XtWindow(selFileField), xtermCursor);
717
718 for (n = 0; n < 3; n++) {
719 XDefineCursor(SFdisplay, XtWindow(selFileLists[n]), sbRightArrowCursor);
720 }
721 XDefineCursor(SFdisplay, XtWindow(selFileOK), arrowCursor);
722 XDefineCursor(SFdisplay, XtWindow(selFileCancel), arrowCursor);
723
724 for (n = 0; n < 3; n++) {
725 XtAddEventHandler(selFileLists[n], ExposureMask, True,
726 (XtEventHandler)SFexposeList, cast_int_to_XtPointer(n));
727 XtAddEventHandler(selFileLists[n], EnterWindowMask, False,
728 (XtEventHandler)SFenterList, cast_int_to_XtPointer(n));
729 XtAddEventHandler(selFileLists[n], LeaveWindowMask, False,
730 (XtEventHandler)SFleaveList, cast_int_to_XtPointer(n));
731 XtAddEventHandler(selFileLists[n], PointerMotionMask, False,
732 (XtEventHandler)SFmotionList, cast_int_to_XtPointer(n));
733 XtAddEventHandler(selFileLists[n], ButtonPressMask, False,
734 (XtEventHandler)SFbuttonPressList, cast_int_to_XtPointer(n));
735 XtAddEventHandler(selFileLists[n], ButtonReleaseMask, False,
736 (XtEventHandler)SFbuttonReleaseList, cast_int_to_XtPointer(n));
737 }
738
739 XtAddEventHandler(selFileField, KeyPressMask, False,
740 (XtEventHandler)SFmodVerifyCallback, (XtPointer) NULL);
741
742 SFapp = XtWidgetToApplicationContext(selFile);
743 return selFile;
744 }
745
746 /* position widget under the cursor */
747 void
748 SFpositionWidget(Widget w)
749 {
750 Arg args[3];
751 Cardinal num_args;
752 Dimension width, height, b_width;
753 int x, y, max_x, max_y;
754 Window root, child;
755 int dummyx, dummyy;
756 unsigned int dummymask;
757
758 XQueryPointer(XtDisplay(w), XtWindow(w), &root, &child, &x, &y,
759 &dummyx, &dummyy, &dummymask);
760 num_args = 0;
761 XtSetArg(args[num_args], XtNwidth, &width);
762 num_args++;
763 XtSetArg(args[num_args], XtNheight, &height);
764 num_args++;
765 XtSetArg(args[num_args], XtNborderWidth, &b_width);
766 num_args++;
767 XtGetValues(w, args, num_args);
768
769 width += 2 * b_width;
770 height += 2 * b_width;
771
772 x -= ((Position) width / 2);
773 if (x < 0)
774 x = 0;
775 if (x > (max_x = (Position) (XtScreen(w)->width - width)))
776 x = max_x;
777
778 y -= ((Position) height / 2);
779 if (y < 0)
780 y = 0;
781 if (y > (max_y = (Position) (XtScreen(w)->height - height)))
782 y = max_y;
783
784 num_args = 0;
785 XtSetArg(args[num_args], XtNx, x);
786 num_args++;
787 XtSetArg(args[num_args], XtNy, y);
788 num_args++;
789 XtSetValues(w, args, num_args);
790 }
791
792
793 FILE *
794 SFopenFile(const char *name, const char *mode, const char *prompt, const char *failed)
795 {
796 Arg args[1];
797 FILE *fp;
798
799 UNUSED(args);
800 UNUSED(prompt);
801 UNUSED(failed);
802
803 SFchdir(SFstartDir);
804 errno = 0;
805 if (!name || *name == 0 || (fp = XFOPEN(name, mode)) == NULL) {
806 sfBell(DISP,
807 (selFile != NULL && XtIsManaged(selFile))
808 ? XtWindow(selFile) : (Window) NULL, 0);
809 return NULL;
810 }
811 return fp;
812 }
813
814 void
815 SFtextChanged(void)
816 {
817 if ((SFtextBuffer[0] == '/') || (SFtextBuffer[0] == '~')) {
818 (void)strcpy(SFcurrentPath, SFtextBuffer);
819
820 SFtextPos = XawTextGetInsertionPoint(selFileField);
821 }
822 else {
823 (void)strcat(strcpy(SFcurrentPath, SFstartDir), SFtextBuffer);
824
825 SFtextPos = XawTextGetInsertionPoint(selFileField) + strlen(SFstartDir);
826 }
827
828 if (!SFworkProcAdded) {
829 (void)XtAppAddWorkProc(SFapp, (XtWorkProc)SFworkProc, NULL);
830 SFworkProcAdded = 1;
831 }
832
833 SFupdatePath();
834 }
835
836 static void
837 SFprepareToReturn(void)
838 {
839 SFstatus = SEL_FILE_NULL;
840 /* XtRemoveGrab(selFile); */
841 XtUnmapWidget(selFile);
842
843 if (SFdirModTimerId) {
844 XtRemoveTimeOut(SFdirModTimerId);
845 SFdirModTimerId = 0;
846 }
847 if (SFchdir(SFstartDir)) {
848 XtAppError(SFapp, "XsraSelFile: can't return to current directory");
849 }
850 }
851
852 Widget
853 XsraSelFile(Widget parent, struct filesel_callback *callback)
854 {
855 SFdisplay = XtDisplay(parent);
856 return SFcreateWidgets(parent, callback);
857 }
858
859 void
860 XsraSelFilePopup(struct filesel_callback *callback)
861 {
862 Cardinal i;
863 Arg arglist[20];
864 XEvent event;
865
866 if (XtIsManaged(callback->shell)) {
867 sfBell(DISP, XtWindow(callback->shell), 10);
868 XRaiseWindow(DISP, XtWindow(callback->shell));
869 return;
870 }
871
872 if (!callback->prompt)
873 callback->prompt = "Pathname:";
874
875 if (!callback->title)
876 callback->title = "xdvik: select filename";
877
878 if (!callback->ok)
879 callback->ok = "OK";
880
881 if (!callback->cancel)
882 callback->cancel = "Cancel";
883
884 /* if (!callback->browse_fname) */
885 /* callback->browse_fname = xt_strdup(xgetcwd()); */
886
887 i = 0;
888 XtSetArg(arglist[i], XtNlabel, callback->prompt); i++;
889 XtSetValues(selFilePrompt, arglist, i);
890
891 i = 0;
892 XtSetArg(arglist[i], XtNlabel, callback->ok); i++;
893 XtSetValues(selFileOK, arglist, i);
894
895 i = 0;
896 XtSetArg(arglist[i], XtNtitle, callback->title); i++;
897 XtSetValues(selFile, arglist, i);
898
899 i = 0;
900 XtSetArg(arglist[i], XtNlabel, callback->cancel); i++;
901 XtSetValues(selFileCancel, arglist, i);
902
903
904 SFpositionWidget(selFile);
905 XtMapWidget(selFile);
906
907 {
908 char *cwd = xgetcwd();
909 strcpy(SFstartDir, cwd);
910 free(cwd);
911 }
912 if (SFstartDir[0] == 0) {
913 XtAppError(SFapp, "XsraSelFile: can't get current directory");
914 }
915 (void)strcat(SFstartDir, "/");
916 (void)strcpy(SFcurrentDir, SFstartDir);
917
918 if (callback->init_path) {
919 if (callback->init_path[0] == '/') {
920 (void)strcpy(SFcurrentPath, callback->init_path);
921 if (strncmp(SFcurrentPath, SFstartDir, strlen(SFstartDir))) {
922 SFsetText(SFcurrentPath);
923 }
924 else {
925 SFsetText(&(SFcurrentPath[strlen(SFstartDir)]));
926 }
927 }
928 else {
929 (void)strcat(strcpy(SFcurrentPath, SFstartDir), callback->init_path);
930 SFsetText(&(SFcurrentPath[strlen(SFstartDir)]));
931 }
932 }
933 else {
934 SFsetText(SFcurrentDir);
935 (void)strcpy(SFcurrentPath, SFstartDir);
936 }
937
938 SFfunc = showEntry;
939
940 SFtextChanged();
941
942 /* don't grab the pointer so that warning popups still work */
943 /* XtAddGrab(selFile, True, True); */
944
945 SFdirModTimerId = XtAppAddTimeOut(SFapp, 1200UL,
946 SFdirModTimer, (XtPointer) NULL);
947
948 if (strcmp(fileMask, callback->filemask) != 0) { /* if mask changed */
949 SFDir *dir;
950 strncpy(fileMask, callback->filemask, MASKWIDTH);
951 XtVaSetValues(selFileMask, XtNstring, fileMask, NULL);
952 for (dir = &(SFdirs[SFdirEnd - 1]); dir >= SFdirs; dir--)
953 *(dir->dir) = 0; /* force a re-read */
954 SFupdatePath();
955 }
956
957 while (1) {
958 XtAppNextEvent(SFapp, &event);
959 switch (event.type) {
960 Widget w;
961 #if 0 /* DON'T do this, it may send the X server into a busy loop if the File selector
962 is positioned over a window that is `on top' by default */
963 case Expose:
964 if (!raise_message_windows())
965 raise_file_selector();
966 break;
967 #endif
968 case KeyPress:
969 case ButtonPress:
970 /* ignore keypress inside the main window (and beep to warn) */
971 w = XtWindowToWidget(DISP, event.xany.window);
972 while ((w != NULL) && (w != selFile)) {
973 /* exception: message windows */
974 if (is_message_window(w))
975 break;
976 w = XtParent(w);
977 }
978 if (w == NULL || w == globals.widgets.top_level) {
979 sfBell(DISP, event.xany.window, 0);
980 continue;
981 }
982 break;
983 }
984
985 XtDispatchEvent(&event);
986
987 switch (SFstatus) {
988 case SEL_FILE_TEXT:
989 SFstatus = SEL_FILE_NULL;
990 SFtextChanged();
991 break;
992 case SEL_FILE_OK:
993 if (callback->must_exist) {
994 FILE *tmp_fp = XFOPEN(SFtextBuffer, "r");
995 dviErrFlagT errflag = NO_ERROR;
996 if (tmp_fp == NULL) {
997 popup_message(selFile,
998 MSG_ERR, NULL, "Could not open %s: %s.\n",
999 SFtextBuffer, strerror(errno));
1000 SFstatus = SEL_FILE_NULL;
1001 break;
1002 }
1003 else if (!process_preamble(tmp_fp, &errflag)
1004 || !find_postamble(tmp_fp, &errflag)
1005 || !read_postamble(tmp_fp, &errflag, False)) {
1006 popup_message(selFile,
1007 MSG_ERR, NULL, "Error opening %s:\n%s.",
1008 SFtextBuffer, get_dvi_error(errflag));
1009 fclose(tmp_fp);
1010 SFstatus = SEL_FILE_NULL;
1011 break;
1012 }
1013 else { /* file is OK */
1014 fclose(tmp_fp);
1015 SFprepareToReturn();
1016 callback->func_ptr(SFtextBuffer, callback->data);
1017 return;
1018 /* return xstrdup(SFtextBuffer); */
1019 }
1020 }
1021 else {
1022 SFprepareToReturn();
1023 callback->func_ptr(SFtextBuffer, callback->data);
1024 return;
1025 /* return xstrdup(SFtextBuffer); */
1026 }
1027 case SEL_FILE_CANCEL:
1028 SFprepareToReturn();
1029 if (callback->exit_on_cancel)
1030 exit(0);
1031 return;
1032 /* return NULL; */
1033 case SEL_FILE_NULL:
1034 break;
1035 }
1036 }
1037 }
1038
1039 #else
1040 /* silence `empty compilation unit' warnings */
1041 static void bar(void); static void foo(void) { bar(); } static void bar(void) { foo(); }
1042 #endif /* !defined(MOTIF) */