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 */
49 /* added missing prototypes */
50 extern void SFtextChanged();
51 extern int SFgetDir(SFDir *);
52 extern void SFdrawLists(int);
53 extern void SFdrawList(int, int);
54 extern void SFclearList(int, int);
55 extern void SFmotionList(Widget, int, XMotionEvent*);
68 int SFbuttonPressed = 0;
70 static int SFdoNotTouchDirPtr = 0;
72 static int SFdoNotTouchVorigin = 0;
74 static SFDir SFrootDir, SFhomeDir;
76 static SFLogin *SFlogins;
78 static int SFtwiddle = 0;
80 void SFsetText(char *path);
90 if (strcmp(path, SFcurrentDir)) {
93 (void) strncpy(SFcurrentDir, path, MAXPATHLEN);
109 for (j = dir->nEntries - 1; j >= 0; j--) {
110 if (dir->entries[j].shown != dir->entries[j].real) {
111 XtFree(dir->entries[j].shown);
113 XtFree(dir->entries[j].real);
116 XtFree((char *) dir->entries);
129 *s1 = strcpy(XtMalloc((unsigned) (strlen(s2) + 1)), s2);
137 char *cannotOpen = "<cannot open> ";
139 dir->entries = (SFEntry *) XtMalloc(sizeof(SFEntry));
140 dir->entries[0].statDone = 1;
141 SFstrdup(&dir->entries[0].real, cannotOpen);
142 dir->entries[0].shown = dir->entries[0].real;
144 dir->nChars = strlen(cannotOpen);
148 #ifdef SEL_FILE_IGNORE_CASE
151 register char *p, *q;
154 register char c1, c2;
171 while ((--n >= 0) && (c1 == c2)) {
173 return strncmp(psave, qsave, nsave);
186 return strncmp(psave, qsave, nsave);
191 #endif /* def SEL_FILE_IGNORE_CASE */
200 text.length = strlen(path);
202 text.format = FMT8BIT;
204 XawTextReplace(selFileField, 0, strlen(SFtextBuffer), &text);
205 XawTextSetInsertionPoint(selFileField, strlen(SFtextBuffer));
211 SFreplaceText(dir, str)
219 if (str[len - 1] == '/') {
220 (void) strcat(SFcurrentPath, str);
222 (void) strncat(SFcurrentPath, str, len - 1);
224 if (strncmp(SFcurrentPath, SFstartDir, strlen(SFstartDir))) {
225 SFsetText(SFcurrentPath);
227 SFsetText(&(SFcurrentPath[strlen(SFstartDir)]));
240 char *name, *growing;
242 SFEntry *entry, *max;
246 dir = &(SFdirs[SFdirEnd - 1]);
248 if (dir->beginSelection == -1) {
250 SFreplaceText(dir, str);
253 } else if (dir->beginSelection == dir->endSelection) {
254 SFreplaceText(dir, dir->entries[dir->beginSelection].shown);
258 max = &(dir->entries[dir->endSelection + 1]);
260 name = dir->entries[dir->beginSelection].shown;
261 SFstrdup(&growing, name);
265 entry = &(dir->entries[dir->beginSelection]);
266 while (entry < max) {
267 if (cmp = strncmp(growing, entry->shown, len)) {
276 * SFreplaceText() expects filename
278 growing[len - 2] = ' ';
280 growing[len - 1] = 0;
281 SFreplaceText(dir, growing);
290 register int i, last, max;
291 register char *name, save;
299 if (str[len - 1] == ' ') {
302 } else if (str[len - 1] == '/') {
308 entries = dir->entries;
312 name = entries[i].shown;
313 last = strlen(name) - 1;
317 #ifdef SEL_FILE_IGNORE_CASE
318 result = SFstrncmp(str, name, len);
319 #else /* def SEL_FILE_IGNORE_CASE */
320 result = strncmp(str, name, len);
321 #endif /* def SEL_FILE_IGNORE_CASE */
331 name = entries[i].shown;
332 last = strlen(name) - 1;
336 #ifdef SEL_FILE_IGNORE_CASE
337 result = SFstrncmp(str, name, len);
338 #else /* def SEL_FILE_IGNORE_CASE */
339 result = strncmp(str, name, len);
340 #endif /* def SEL_FILE_IGNORE_CASE */
352 (dir->beginSelection != begin) ||
353 (dir->endSelection != end - 1)
356 dir->beginSelection = begin;
357 if (str[strlen(str) - 1] == '/') {
358 dir->endSelection = begin;
360 dir->endSelection = end - 1;
364 if (dir->beginSelection != -1) {
366 dir->beginSelection = -1;
367 dir->endSelection = -1;
372 SFdoNotTouchVorigin ||
373 ((begin > dir->vOrigin) && (end < dir->vOrigin + SFlistSize))
375 SFdoNotTouchVorigin = 0;
380 if (i > max - SFlistSize) {
381 i = max - SFlistSize;
387 if (dir->vOrigin != i) {
400 dir = &(SFdirs[SFdirEnd - 1]);
401 if (dir->beginSelection != -1) {
404 dir->beginSelection = -1;
405 dir->endSelection = -1;
410 SFcompareLogins(p, q)
413 return strcmp(p->name, q->name);
422 SFEntry *entries = NULL;
429 entries = (SFEntry *) XtMalloc(sizeof(SFEntry));
430 SFlogins = (SFLogin *) XtMalloc(sizeof(SFLogin));
431 entries[0].real = XtMalloc(3);
432 (void) strcpy(entries[0].real, "~");
433 entries[0].shown = entries[0].real;
434 entries[0].statDone = 1;
435 SFlogins[0].name = "";
436 pw = getpwuid((int) getuid());
437 SFstrdup(&SFlogins[0].dir, pw ? pw->pw_dir : "/");
443 while ((pw = getpwent()) && (*(pw->pw_name))) {
446 entries = (SFEntry *) XtRealloc(
448 (unsigned) (alloc * sizeof(SFEntry))
450 SFlogins = (SFLogin *) XtRealloc(
452 (unsigned) (alloc * sizeof(SFLogin))
455 len = strlen(pw->pw_name);
456 entries[i].real = XtMalloc((unsigned) (len + 3));
457 (void) strcat(strcpy(entries[i].real, "~"),
459 entries[i].shown = entries[i].real;
460 entries[i].statDone = 1;
461 if (len > maxChars) {
464 SFstrdup(&SFlogins[i].name, pw->pw_name);
465 SFstrdup(&SFlogins[i].dir, pw->pw_dir);
469 SFhomeDir.dir = XtMalloc(1) ;
470 SFhomeDir.dir[0] = 0 ;
471 SFhomeDir.path = SFcurrentPath ;
472 SFhomeDir.entries = entries ;
473 SFhomeDir.nEntries = i ;
474 SFhomeDir.vOrigin = 0 ; /* :-) */
475 SFhomeDir.nChars = maxChars + 2 ;
476 SFhomeDir.hOrigin = 0 ;
477 SFhomeDir.changed = 1 ;
478 SFhomeDir.beginSelection = -1 ;
479 SFhomeDir.endSelection = -1 ;
481 qsort((char *) entries, (size_t)i, sizeof(SFEntry), SFcompareEntries);
482 qsort((char *) SFlogins, (size_t)i, sizeof(SFLogin), SFcompareLogins);
484 for (i--; i >= 0; i--) {
485 (void) strcat(entries[i].real, "/");
491 SFfindHomeDir(begin, end)
501 for (i = SFhomeDir.nEntries - 1; i >= 0; i--) {
502 if (!strcmp(SFhomeDir.entries[i].real, begin)) {
504 SFstrdup(&theRest, end);
505 (void) strcat(strcat(strncpy(SFcurrentPath,SFlogins[i].dir,
509 SFsetText(SFcurrentPath);
524 static int wasTwiddle = 0;
528 int SFdirPtrSave, SFdirEndSave;
532 SFdirs = (SFDir *) XtMalloc((alloc = 10) * sizeof(SFDir));
534 SFstrdup(&dir->dir, "/");
536 (void) SFgetDir(dir);
537 for (j = 1; j < alloc; j++) {
538 SFdirs[j].dir = NULL;
540 dir->path = SFcurrentPath + 1;
544 dir->beginSelection = -1;
545 dir->endSelection = -1;
546 SFhomeDir.dir = NULL;
549 SFdirEndSave = SFdirEnd;
552 SFdirPtrSave = SFdirPtr;
557 if (SFcurrentPath[0] == '~') {
562 if (!SFhomeDir.dir) {
569 SFdoNotTouchDirPtr = 1;
578 end = SFcurrentPath + 1;
586 while (*end++ == '/') {
591 while ((*end) && (*end++ != '/')) {
594 if ((end - SFcurrentPath <= SFtextPos) && (*(end - 1) == '/')) {
601 if (*(end - 1) == '/') {
605 if (SFfindHomeDir(begin, end)) {
613 SFdirs = (SFDir *) XtRealloc(
615 (unsigned) ((alloc *= 2) *
618 for (j = alloc / 2; j < alloc; j++) {
619 SFdirs[j].dir = NULL;
626 strcmp(dir->dir, begin)
632 SFstrdup(&dir->dir, begin);
637 dir->beginSelection = -1;
638 dir->endSelection = -1;
639 (void) SFfindFile(dir - 1, begin);
641 SFchdir(SFcurrentPath) ||
644 SFunreadableDir(dir);
653 if (SFfindFile(&(SFdirs[SFdirEnd-1]), begin)) {
662 if ((end == SFcurrentPath + 1) && (!SFtwiddle)) {
666 for (i = SFdirEnd; i < alloc; i++) {
672 if (SFdoNotTouchDirPtr) {
675 SFdirPtr = SFdirEnd - 1;
680 SFdirPtr = SFdirPtrSave;
682 SFdoNotTouchDirPtr = 0;
685 if ((SFdirPtr != SFdirPtrSave) || (SFdirEnd != SFdirEndSave)) {
686 XawScrollbarSetThumb(
688 (float) (((double) SFdirPtr) / SFdirEnd),
689 (float) (((double) ((SFdirEnd < NR) ? SFdirEnd : NR)) /
694 if (SFdirPtr != SFdirPtrSave) {
695 SFdrawLists(SF_DO_SCROLL);
697 for (i = 0; i < NR; i++) {
698 if (SFdirPtr + i < SFdirEnd) {
699 if (SFdirs[SFdirPtr + i].changed) {
700 SFdirs[SFdirPtr + i].changed = 0;
701 SFdrawList(i, SF_DO_SCROLL);
704 SFclearList(i, SF_DO_SCROLL);
713 SFbuttonPressList(w, n, event)
716 XButtonPressedEvent *event;
719 if(event->button == Button4) dir = -2; // kludge to indicate relative motion
720 if(event->button == Button5) dir = -1;
721 if(dir) SFvSliderMovedCallback(w, n, dir); else
727 SFbuttonReleaseList(w, n, event)
730 XButtonReleasedEvent *event;
733 static int lastClick;
735 if(event->button == Button4 || event->button == Button5) return; // [HGM] mouse wheel does not select
738 if (SFcurrentInvert[n] != -1) {
740 SFdoNotTouchDirPtr = 1;
742 SFdoNotTouchVorigin = 1;
743 dir = &(SFdirs[SFdirPtr + n]);
746 dir->entries[dir->vOrigin + SFcurrentInvert[n]].shown
748 SFmotionList(w, n, (XMotionEvent *) event);
749 if(lastClick == 256*n + SFcurrentInvert[n]) SFstatus = SEL_FILE_OK; // [HGM] double click implies OK
751 lastClick = 256*n + SFcurrentInvert[n];
763 (!stat(".", &statBuf)) &&
764 (statBuf.st_mtime != dir->mtime)
768 * If the pointer is currently in the window that we are about
769 * to update, we must warp it to prevent the user from
770 * accidentally selecting the wrong file.
772 if (SFcurrentInvert[n] != -1) {
776 XtWindow(selFileLists[n]),
786 for (i = dir->nEntries - 1; i >= 0; i--) {
787 if (dir->entries[i].shown != dir->entries[i].real) {
788 XtFree(dir->entries[i].shown);
790 XtFree(dir->entries[i].real);
792 XtFree((char *) dir->entries);
794 SFunreadableDir(dir);
796 if (dir->vOrigin > dir->nEntries - SFlistSize) {
797 dir->vOrigin = dir->nEntries - SFlistSize;
799 if (dir->vOrigin < 0) {
802 if (dir->hOrigin > dir->nChars - SFcharsPerEntry) {
803 dir->hOrigin = dir->nChars - SFcharsPerEntry;
805 if (dir->hOrigin < 0) {
808 dir->beginSelection = -1;
809 dir->endSelection = -1;
810 SFdoNotTouchVorigin = 1;
811 if ((dir + 1)->dir) {
812 (void) SFfindFile(dir, (dir + 1)->dir);
814 (void) SFfindFile(dir, dir->path);
817 if (!SFworkProcAdded) {
818 (void) XtAppAddWorkProc(SFapp, SFworkProc, NULL);
828 /* Return a single character describing what kind of file STATBUF is. */
832 struct stat *statBuf;
834 if (S_ISDIR (statBuf->st_mode)) {
836 } else if (S_ISREG (statBuf->st_mode)) {
837 return S_ISXXX (statBuf->st_mode) ? '*' : ' ';
839 } else if (S_ISSOCK (statBuf->st_mode)) {
841 #endif /* S_ISSOCK */
862 to = dir->vOrigin + SFlistSize;
863 if (to > dir->nEntries) {
867 for (i = from; i < to; i++) {
868 str = dir->entries[i].real;
869 last = strlen(str) - 1;
872 if (stat(str, &statBuf)) {
875 new = SFstatChar(&statBuf);
887 SFdirModTimer(cl, id)
896 if ((!SFtwiddle) && (SFdirPtr < SFdirEnd)) {
898 if ((n > NR-1) || (SFdirPtr + n >= SFdirEnd)) {
901 if ((f > NR-1) || (SFdirPtr + f >= SFdirEnd)) {
905 dir = &(SFdirs[SFdirPtr + n]);
908 if (SFchdir(SFcurrentPath)) {
920 SFcheckDir(n, dir) ||
921 ((f == n) && SFcheckFiles(dir))
923 SFdrawList(n, SF_DO_SCROLL);
928 SFdirModTimerId = XtAppAddTimeOut(SFapp, (unsigned long) 1000,
929 SFdirModTimer, (XtPointer) NULL);