2 * Copyright 1989 Software Research Associates, Inc., Tokyo, Japan
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.
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.
22 * Author: Erik M. van der Poel
23 * Software Research Associates, Inc., Tokyo, Japan
28 #include <stdlib.h> /* for qsort */
29 #include "../config.h"
31 #ifdef SEL_FILE_IGNORE_CASE
33 #endif /* def SEL_FILE_IGNORE_CASE */
39 #include <X11/Xaw/Scrollbar.h>
42 #define MAXPATHLEN 1024
43 #endif /* ndef MAXPATHLEN */
46 extern uid_t getuid();
47 #endif /* def HAS_DIRENT_H */
60 int SFbuttonPressed = 0;
62 static int SFdoNotTouchDirPtr = 0;
64 static int SFdoNotTouchVorigin = 0;
66 static SFDir SFrootDir, SFhomeDir;
68 static SFLogin *SFlogins;
70 static int SFtwiddle = 0;
72 void SFsetText(char *path);
82 if (strcmp(path, SFcurrentDir)) {
85 (void) strncpy(SFcurrentDir, path, MAXPATHLEN);
101 for (j = dir->nEntries - 1; j >= 0; j--) {
102 if (dir->entries[j].shown != dir->entries[j].real) {
103 XtFree(dir->entries[j].shown);
105 XtFree(dir->entries[j].real);
108 XtFree((char *) dir->entries);
121 *s1 = strcpy(XtMalloc((unsigned) (strlen(s2) + 1)), s2);
129 char *cannotOpen = "<cannot open> ";
131 dir->entries = (SFEntry *) XtMalloc(sizeof(SFEntry));
132 dir->entries[0].statDone = 1;
133 SFstrdup(&dir->entries[0].real, cannotOpen);
134 dir->entries[0].shown = dir->entries[0].real;
136 dir->nChars = strlen(cannotOpen);
140 #ifdef SEL_FILE_IGNORE_CASE
143 register char *p, *q;
146 register char c1, c2;
163 while ((--n >= 0) && (c1 == c2)) {
165 return strncmp(psave, qsave, nsave);
178 return strncmp(psave, qsave, nsave);
183 #endif /* def SEL_FILE_IGNORE_CASE */
192 text.length = strlen(path);
194 text.format = FMT8BIT;
196 XawTextReplace(selFileField, 0, strlen(SFtextBuffer), &text);
197 XawTextSetInsertionPoint(selFileField, strlen(SFtextBuffer));
203 SFreplaceText(dir, str)
211 if (str[len - 1] == '/') {
212 (void) strcat(SFcurrentPath, str);
214 (void) strncat(SFcurrentPath, str, len - 1);
216 if (strncmp(SFcurrentPath, SFstartDir, strlen(SFstartDir))) {
217 SFsetText(SFcurrentPath);
219 SFsetText(&(SFcurrentPath[strlen(SFstartDir)]));
232 char *name, *growing;
234 SFEntry *entry, *max;
238 dir = &(SFdirs[SFdirEnd - 1]);
240 if (dir->beginSelection == -1) {
242 SFreplaceText(dir, str);
245 } else if (dir->beginSelection == dir->endSelection) {
246 SFreplaceText(dir, dir->entries[dir->beginSelection].shown);
250 max = &(dir->entries[dir->endSelection + 1]);
252 name = dir->entries[dir->beginSelection].shown;
253 SFstrdup(&growing, name);
257 entry = &(dir->entries[dir->beginSelection]);
258 while (entry < max) {
259 if (cmp = strncmp(growing, entry->shown, len)) {
268 * SFreplaceText() expects filename
270 growing[len - 2] = ' ';
272 growing[len - 1] = 0;
273 SFreplaceText(dir, growing);
282 register int i, last, max;
283 register char *name, save;
291 if (str[len - 1] == ' ') {
294 } else if (str[len - 1] == '/') {
300 entries = dir->entries;
304 name = entries[i].shown;
305 last = strlen(name) - 1;
309 #ifdef SEL_FILE_IGNORE_CASE
310 result = SFstrncmp(str, name, len);
311 #else /* def SEL_FILE_IGNORE_CASE */
312 result = strncmp(str, name, len);
313 #endif /* def SEL_FILE_IGNORE_CASE */
323 name = entries[i].shown;
324 last = strlen(name) - 1;
328 #ifdef SEL_FILE_IGNORE_CASE
329 result = SFstrncmp(str, name, len);
330 #else /* def SEL_FILE_IGNORE_CASE */
331 result = strncmp(str, name, len);
332 #endif /* def SEL_FILE_IGNORE_CASE */
344 (dir->beginSelection != begin) ||
345 (dir->endSelection != end - 1)
348 dir->beginSelection = begin;
349 if (str[strlen(str) - 1] == '/') {
350 dir->endSelection = begin;
352 dir->endSelection = end - 1;
356 if (dir->beginSelection != -1) {
358 dir->beginSelection = -1;
359 dir->endSelection = -1;
364 SFdoNotTouchVorigin ||
365 ((begin > dir->vOrigin) && (end < dir->vOrigin + SFlistSize))
367 SFdoNotTouchVorigin = 0;
372 if (i > max - SFlistSize) {
373 i = max - SFlistSize;
379 if (dir->vOrigin != i) {
392 dir = &(SFdirs[SFdirEnd - 1]);
393 if (dir->beginSelection != -1) {
396 dir->beginSelection = -1;
397 dir->endSelection = -1;
402 SFcompareLogins(p, q)
405 return strcmp(p->name, q->name);
414 SFEntry *entries = NULL;
421 entries = (SFEntry *) XtMalloc(sizeof(SFEntry));
422 SFlogins = (SFLogin *) XtMalloc(sizeof(SFLogin));
423 entries[0].real = XtMalloc(3);
424 (void) strcpy(entries[0].real, "~");
425 entries[0].shown = entries[0].real;
426 entries[0].statDone = 1;
427 SFlogins[0].name = "";
428 pw = getpwuid((int) getuid());
429 SFstrdup(&SFlogins[0].dir, pw ? pw->pw_dir : "/");
435 while ((pw = getpwent()) && (*(pw->pw_name))) {
438 entries = (SFEntry *) XtRealloc(
440 (unsigned) (alloc * sizeof(SFEntry))
442 SFlogins = (SFLogin *) XtRealloc(
444 (unsigned) (alloc * sizeof(SFLogin))
447 len = strlen(pw->pw_name);
448 entries[i].real = XtMalloc((unsigned) (len + 3));
449 (void) strcat(strcpy(entries[i].real, "~"),
451 entries[i].shown = entries[i].real;
452 entries[i].statDone = 1;
453 if (len > maxChars) {
456 SFstrdup(&SFlogins[i].name, pw->pw_name);
457 SFstrdup(&SFlogins[i].dir, pw->pw_dir);
461 SFhomeDir.dir = XtMalloc(1) ;
462 SFhomeDir.dir[0] = 0 ;
463 SFhomeDir.path = SFcurrentPath ;
464 SFhomeDir.entries = entries ;
465 SFhomeDir.nEntries = i ;
466 SFhomeDir.vOrigin = 0 ; /* :-) */
467 SFhomeDir.nChars = maxChars + 2 ;
468 SFhomeDir.hOrigin = 0 ;
469 SFhomeDir.changed = 1 ;
470 SFhomeDir.beginSelection = -1 ;
471 SFhomeDir.endSelection = -1 ;
473 qsort((char *) entries, (size_t)i, sizeof(SFEntry), SFcompareEntries);
474 qsort((char *) SFlogins, (size_t)i, sizeof(SFLogin), SFcompareLogins);
476 for (i--; i >= 0; i--) {
477 (void) strcat(entries[i].real, "/");
483 SFfindHomeDir(begin, end)
493 for (i = SFhomeDir.nEntries - 1; i >= 0; i--) {
494 if (!strcmp(SFhomeDir.entries[i].real, begin)) {
496 SFstrdup(&theRest, end);
497 (void) strcat(strcat(strncpy(SFcurrentPath,SFlogins[i].dir,
501 SFsetText(SFcurrentPath);
516 static int wasTwiddle = 0;
520 int SFdirPtrSave, SFdirEndSave;
524 SFdirs = (SFDir *) XtMalloc((alloc = 10) * sizeof(SFDir));
526 SFstrdup(&dir->dir, "/");
528 (void) SFgetDir(dir);
529 for (j = 1; j < alloc; j++) {
530 SFdirs[j].dir = NULL;
532 dir->path = SFcurrentPath + 1;
536 dir->beginSelection = -1;
537 dir->endSelection = -1;
538 SFhomeDir.dir = NULL;
541 SFdirEndSave = SFdirEnd;
544 SFdirPtrSave = SFdirPtr;
549 if (SFcurrentPath[0] == '~') {
554 if (!SFhomeDir.dir) {
561 SFdoNotTouchDirPtr = 1;
570 end = SFcurrentPath + 1;
578 while (*end++ == '/') {
583 while ((*end) && (*end++ != '/')) {
586 if ((end - SFcurrentPath <= SFtextPos) && (*(end - 1) == '/')) {
593 if (*(end - 1) == '/') {
597 if (SFfindHomeDir(begin, end)) {
605 SFdirs = (SFDir *) XtRealloc(
607 (unsigned) ((alloc *= 2) *
610 for (j = alloc / 2; j < alloc; j++) {
611 SFdirs[j].dir = NULL;
618 strcmp(dir->dir, begin)
624 SFstrdup(&dir->dir, begin);
629 dir->beginSelection = -1;
630 dir->endSelection = -1;
631 (void) SFfindFile(dir - 1, begin);
633 SFchdir(SFcurrentPath) ||
636 SFunreadableDir(dir);
645 if (SFfindFile(&(SFdirs[SFdirEnd-1]), begin)) {
654 if ((end == SFcurrentPath + 1) && (!SFtwiddle)) {
658 for (i = SFdirEnd; i < alloc; i++) {
664 if (SFdoNotTouchDirPtr) {
667 SFdirPtr = SFdirEnd - 1;
672 SFdirPtr = SFdirPtrSave;
674 SFdoNotTouchDirPtr = 0;
677 if ((SFdirPtr != SFdirPtrSave) || (SFdirEnd != SFdirEndSave)) {
678 XawScrollbarSetThumb(
680 (float) (((double) SFdirPtr) / SFdirEnd),
681 (float) (((double) ((SFdirEnd < NR) ? SFdirEnd : NR)) /
686 if (SFdirPtr != SFdirPtrSave) {
687 SFdrawLists(SF_DO_SCROLL);
689 for (i = 0; i < NR; i++) {
690 if (SFdirPtr + i < SFdirEnd) {
691 if (SFdirs[SFdirPtr + i].changed) {
692 SFdirs[SFdirPtr + i].changed = 0;
693 SFdrawList(i, SF_DO_SCROLL);
696 SFclearList(i, SF_DO_SCROLL);
705 SFbuttonPressList(w, n, event)
708 XButtonPressedEvent *event;
711 if(event->button == Button4) dir = -2; // kludge to indicate relative motion
712 if(event->button == Button5) dir = -1;
713 if(dir) SFvSliderMovedCallback(w, n, dir); else
719 SFbuttonReleaseList(w, n, event)
722 XButtonReleasedEvent *event;
725 static int lastClick;
727 if(event->button == Button4 || event->button == Button5) return; // [HGM] mouse wheel does not select
730 if (SFcurrentInvert[n] != -1) {
732 SFdoNotTouchDirPtr = 1;
734 SFdoNotTouchVorigin = 1;
735 dir = &(SFdirs[SFdirPtr + n]);
738 dir->entries[dir->vOrigin + SFcurrentInvert[n]].shown
740 SFmotionList(w, n, (XMotionEvent *) event);
741 if(lastClick == 256*n + SFcurrentInvert[n]) SFstatus = SEL_FILE_OK; // [HGM] double click implies OK
743 lastClick = 256*n + SFcurrentInvert[n];
755 (!stat(".", &statBuf)) &&
756 (statBuf.st_mtime != dir->mtime)
760 * If the pointer is currently in the window that we are about
761 * to update, we must warp it to prevent the user from
762 * accidentally selecting the wrong file.
764 if (SFcurrentInvert[n] != -1) {
768 XtWindow(selFileLists[n]),
778 for (i = dir->nEntries - 1; i >= 0; i--) {
779 if (dir->entries[i].shown != dir->entries[i].real) {
780 XtFree(dir->entries[i].shown);
782 XtFree(dir->entries[i].real);
784 XtFree((char *) dir->entries);
786 SFunreadableDir(dir);
788 if (dir->vOrigin > dir->nEntries - SFlistSize) {
789 dir->vOrigin = dir->nEntries - SFlistSize;
791 if (dir->vOrigin < 0) {
794 if (dir->hOrigin > dir->nChars - SFcharsPerEntry) {
795 dir->hOrigin = dir->nChars - SFcharsPerEntry;
797 if (dir->hOrigin < 0) {
800 dir->beginSelection = -1;
801 dir->endSelection = -1;
802 SFdoNotTouchVorigin = 1;
803 if ((dir + 1)->dir) {
804 (void) SFfindFile(dir, (dir + 1)->dir);
806 (void) SFfindFile(dir, dir->path);
809 if (!SFworkProcAdded) {
810 (void) XtAppAddWorkProc(SFapp, SFworkProc, NULL);
820 /* Return a single character describing what kind of file STATBUF is. */
824 struct stat *statBuf;
826 if (S_ISDIR (statBuf->st_mode)) {
828 } else if (S_ISREG (statBuf->st_mode)) {
829 return S_ISXXX (statBuf->st_mode) ? '*' : ' ';
831 } else if (S_ISSOCK (statBuf->st_mode)) {
833 #endif /* S_ISSOCK */
854 to = dir->vOrigin + SFlistSize;
855 if (to > dir->nEntries) {
859 for (i = from; i < to; i++) {
860 str = dir->entries[i].real;
861 last = strlen(str) - 1;
864 if (stat(str, &statBuf)) {
867 new = SFstatChar(&statBuf);
879 SFdirModTimer(cl, id)
888 if ((!SFtwiddle) && (SFdirPtr < SFdirEnd)) {
890 if ((n > NR-1) || (SFdirPtr + n >= SFdirEnd)) {
893 if ((f > NR-1) || (SFdirPtr + f >= SFdirEnd)) {
897 dir = &(SFdirs[SFdirPtr + n]);
900 if (SFchdir(SFcurrentPath)) {
912 SFcheckDir(n, dir) ||
913 ((f == n) && SFcheckFiles(dir))
915 SFdrawList(n, SF_DO_SCROLL);
920 SFdirModTimerId = XtAppAddTimeOut(SFapp, (unsigned long) 1000,
921 SFdirModTimer, (XtPointer) NULL);