home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / alt / toolkits / xview / 1158 < prev    next >
Encoding:
Text File  |  1992-12-29  |  12.9 KB  |  570 lines

  1. Path: sparky!uunet!usc!zaphod.mps.ohio-state.edu!swrinde!cs.utexas.edu!news
  2. From: cpg@cs.utexas.edu (Carlos M. Puchol)
  3. Newsgroups: alt.toolkits.xview
  4. Subject: Re: HELP! Find all files of a directory in Xview
  5. Date: 29 Dec 1992 04:44:11 -0600
  6. Organization: Department of Computer Sciences, UT Austin
  7. Lines: 558
  8. Message-ID: <lk0avrINN228@thor.cs.utexas.edu>
  9. References: <1992Dec24.055841.14790@dec8.ncku.edu.tw>
  10. NNTP-Posting-Host: thor.cs.utexas.edu
  11.  
  12. In article <1992Dec24.055841.14790@dec8.ncku.edu.tw> wangbs@casd1.iie.ncku writes:
  13. >
  14. >   Hi, all:
  15. >       How can I program to find all files of a directory in Xview. I want to
  16. >   it like the "Open" option of "File" pop-up menu in Turbo C. In Turbo C, 
  17. >   you can select the Open option to list many files that you can select.
  18. >   Since I am not good at Xview, it is better to have a example code to show
  19. >   me.   HELP!!!
  20. >
  21. >               Thanks for any response!!!
  22. >                                           wangbs@casd1.iie.ncku.edu.tw
  23. >                                           wangbs@locust.iie.ncku.edu.tw 
  24.  
  25. I picked this from the comp.sys.sun.misc newsgroup some time ago :-)
  26.  
  27. Good luck!
  28.  
  29.  
  30. Path: cs.utexas.edu!wupost!uunet!mcsun!sun4nl!ictser!weegink
  31. From: weegink@ictser.UUCP (Han Weegink)
  32. Newsgroups: comp.sys.sun.misc
  33. Subject: Re: XView file browser?
  34. Message-ID: <1214@ictser.UUCP>
  35. Date: 4 Mar 92 14:01:13 GMT
  36. References: <1992Feb27.050602.8979@ge-dab.GE.COM>
  37. Lines: 496
  38.  
  39. vita@gloucester.dab.ge.com (Mark Vita) writes:
  40.  
  41. >Hello all,
  42.  
  43. >Does anyone have XView code for a Macintosh-style file browser that you'd
  44. >be willing to give away?  I.e., a widget that lets the user choose a file
  45. >by browsing through a scrolling list of files, navigating through the
  46. >directory structure, etc., like SFGetFile on the Mac.  I remember seeing
  47. >several implementations back in my SunView days, so I figure there must be
  48. >something around for XView...
  49.  
  50. >Thanks in advance...
  51.  
  52. >--
  53. >Mark Vita                                  vita@sunny.dab.ge.com
  54. >Advanced Engineering
  55. >GE Simulation and Control Systems
  56. >Daytona Beach, FL
  57.  
  58.  
  59.  
  60. Re: XView file browser ?
  61.  
  62.  
  63. I picked up some sources from the net a while ago. Don't remember who originally
  64. posted them, but thanks anyway. They contain some (minor) bugs but show you how
  65. it could be done.
  66.  
  67. The example consists of two scrolling lists, one presenting the directories,
  68. the other the files. You can 'change directory' by selecting an entry in the
  69. directory list. You can also select files in the other list. The selection can
  70. be passed to a routine, e.g. for loading the file.
  71.  
  72. I rewrote some parts to solve some of the problems, but my version is too 
  73. specific to post it on the net.
  74. Therefore, this posting contains the original sources.
  75. However, let me know if you encounter serious problems.
  76.  
  77. I have included the source file and the makefile below, hope this is what
  78. you want,
  79.  
  80.  
  81. Han Weegink
  82.  
  83.  
  84. ---------------------------------------------------------------------------
  85. IIIII  CCCCC  TTTTTTT  Han Weegink                                info only
  86.   I   C     C    T     ICT Automatisering Deventer BV
  87.   I   C          T     Aerospace Group        Holland
  88.   I   C          T     E-mail:    weegink@ict.nl
  89.   I   C     C    T     Telefax:   +31 (0)5700 21362
  90. IIIII  CCCCC     T     TelePhone: +31 (0)5700 33888
  91. ---------------------------------------------------------------------------
  92.  
  93. #! /bin/sh
  94. # This is a shell archive.  Remove anything before this line, then unpack
  95. # it by saving it into a file and typing "sh file".  To overwrite existing
  96. # files, type "sh file -c".  You can also feed this as standard input via
  97. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  98. # will see the following message at the end:
  99. #        "End of shell archive."
  100. # Contents:  Makefile dir_list.c
  101. # Wrapped by cpg@thor on Tue Dec 29 04:43:26 1992
  102. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  103. if test -f 'Makefile' -a "${1}" != "-c" ; then 
  104.   echo shar: Will not clobber existing file \"'Makefile'\"
  105. else
  106. echo shar: Extracting \"'Makefile'\" \(586 characters\)
  107. sed "s/^X//" >'Makefile' <<'END_OF_FILE'
  108. X# -------------------- make file ----------------------------
  109. X#
  110. X#    Makefile for the dirlist example program
  111. X#
  112. X
  113. XINCLUDE     = -I${OPENWINHOME}/include
  114. X
  115. X#
  116. X# If you want to compile for debugging, change "-O" to "-g"
  117. X#
  118. X
  119. XCFLAGS        = ${INCLUDE} -g
  120. X
  121. X# if you want special to pass special loader options to ld, set
  122. X# LDFLAGS= ...
  123. X#
  124. X
  125. XXVIEW_LIBS     = -L${OPENWINHOME}/lib \
  126. X          -lxview -lolgx -lX11
  127. X
  128. XCFILES        = dir_list.c 
  129. X       
  130. XOBJS        = dir_list
  131. X
  132. Xall:  ${OBJS}
  133. X
  134. X${OBJS}: $$@.c $$@.o 
  135. X    ${CC} ${CFLAGS} ${LDFLAGS} -o $@ $@.o ${XVIEW_LIBS}
  136. X
  137. X#---------------------- end of makefile --------------------------
  138. X
  139. END_OF_FILE
  140. if test 586 -ne `wc -c <'Makefile'`; then
  141.     echo shar: \"'Makefile'\" unpacked with wrong size!
  142. fi
  143. # end of 'Makefile'
  144. fi
  145. if test -f 'dir_list.c' -a "${1}" != "-c" ; then 
  146.   echo shar: Will not clobber existing file \"'dir_list.c'\"
  147. else
  148. echo shar: Extracting \"'dir_list.c'\" \(7548 characters\)
  149. sed "s/^X//" >'dir_list.c' <<'END_OF_FILE'
  150. X/*
  151. X * dirlist.c
  152. X * This program creates two scrolling lists, one with directories and one
  153. X * with files.  It allows the user to search through directories for files
  154. X * and then select a file to "open".
  155. X */
  156. X
  157. X#include <stdio.h>
  158. X#include <stdlib.h>
  159. X#include <xview/xview.h>
  160. X#include <xview/panel.h>
  161. X#include <dirent.h>
  162. X#include <sys/stat.h>
  163. X#include <sys/types.h>
  164. X
  165. X#define DESELECT 0
  166. X#define SELECT 1
  167. X#define ENTRY_KEY 5877
  168. X#define NULL_STR ""
  169. X
  170. XFrame frame;
  171. XPanel panel;
  172. XPanel_item pi_dir, pi_file, ti_file, mi_dir, bi_open;
  173. XDIR *dirp;
  174. Xstruct dirent *entry;
  175. Xchar *current;
  176. X
  177. Xint num_files, num_dirs;
  178. X
  179. Xint Open();
  180. Xint Cancel();
  181. Xvoid setup_list();
  182. Xvoid dir_proc();
  183. Xvoid file_proc();
  184. Xint check_read();
  185. X
  186. Xmain(argc, argv)
  187. Xint argc;
  188. Xchar *argv[];
  189. X
  190. X{
  191. X  char *tmpname;
  192. X
  193. X/*
  194. X * Start in the HOME directory
  195. X */
  196. X
  197. X  if (argc<1)
  198. X      tmpname = getenv("HOME");
  199. X  else
  200. X    tmpname=argv[1];
  201. X  current = (char *)malloc(strlen(tmpname)+1);
  202. X  memset(current, '\0', strlen(tmpname)+1);
  203. X  strcat(current, tmpname);
  204. X  free(tmpname);
  205. X
  206. X  xv_init(XV_INIT_ARGC_PTR_ARGV, &argc, argv, NULL);
  207. X
  208. X  frame = (Frame)xv_create(NULL, FRAME,
  209. X    FRAME_LABEL,        "File Open",
  210. X    NULL);
  211. X
  212. X  panel = (Panel)xv_create(frame, PANEL, NULL);
  213. X
  214. X  mi_dir = (Panel_item)xv_create(panel, PANEL_MESSAGE,
  215. X    PANEL_LABEL_STRING,        current,
  216. X    PANEL_LABEL_BOLD,        TRUE,
  217. X    NULL);
  218. X
  219. X  ti_file = (Panel_item)xv_create(panel, PANEL_TEXT,
  220. X    PANEL_LABEL_STRING,     "File:",
  221. X    PANEL_VALUE_DISPLAY_LENGTH,    50,
  222. X    PANEL_VALUE_STORED_LENGTH,    256,
  223. X    PANEL_NEXT_ROW,        -1,
  224. X    PANEL_VALUE,        NULL_STR,
  225. X    NULL);
  226. X
  227. X  pi_dir = (Panel_item)xv_create(panel, PANEL_LIST,
  228. X    PANEL_LABEL_STRING,        "Directories:",
  229. X    PANEL_LIST_DISPLAY_ROWS,    10,
  230. X    PANEL_CHOOSE_ONE,        TRUE,
  231. X    PANEL_CHOOSE_NONE,          TRUE,
  232. X    PANEL_READ_ONLY,        TRUE,
  233. X    PANEL_NOTIFY_PROC,        dir_proc,
  234. X    PANEL_NEXT_ROW,        -1,
  235. X    PANEL_LIST_WIDTH,        150,
  236. X    PANEL_LAYOUT,        PANEL_VERTICAL,
  237. X    NULL);
  238. X
  239. X  pi_file = (Panel_item)xv_create(panel, PANEL_LIST,
  240. X    PANEL_LABEL_STRING,        "Files:",
  241. X    PANEL_LIST_DISPLAY_ROWS,    10,
  242. X    PANEL_CHOOSE_ONE,        TRUE,
  243. X    PANEL_CHOOSE_NONE,        TRUE,
  244. X    PANEL_READ_ONLY,        TRUE,
  245. X    PANEL_NOTIFY_PROC,        file_proc,
  246. X    PANEL_LIST_WIDTH,        150,
  247. X    PANEL_LAYOUT,        PANEL_VERTICAL,
  248. X    NULL);
  249. X
  250. X  bi_open = (Panel_item)xv_create(panel, PANEL_BUTTON,
  251. X    PANEL_LABEL_STRING,        "Open",
  252. X    PANEL_NOTIFY_PROC,        Open,
  253. X    XV_X,            370,
  254. X    XV_Y,            73,
  255. X    NULL);
  256. X
  257. X  (void)xv_create(panel, PANEL_BUTTON,
  258. X    PANEL_LABEL_STRING,        "Cancel",
  259. X    PANEL_NOTIFY_PROC,        Cancel,
  260. X    XV_X,            367,
  261. X    XV_Y,            103,
  262. X    NULL);
  263. X
  264. X  if (check_read(current))
  265. X    setup_list();
  266. X
  267. X  window_fit(panel);
  268. X  window_fit(frame);
  269. X
  270. X  xv_main_loop(frame);
  271. X}
  272. X
  273. X
  274. X/*
  275. X * Re-read the directory entries for the scrolling lists
  276. X */
  277. X
  278. Xvoid setup_list()
  279. X
  280. X{
  281. X  char *tmpname;
  282. X  int i;
  283. X
  284. X  xv_set(ti_file,
  285. X    PANEL_VALUE,    NULL_STR,
  286. X    NULL);
  287. X
  288. X  xv_set(bi_open,
  289. X    XV_SHOW, FALSE,
  290. X    NULL);
  291. X
  292. X  xv_set(mi_dir,
  293. X    PANEL_LABEL_STRING,    current,
  294. X    NULL);
  295. X
  296. X/*
  297. X * Hide the scroll lists while re-computing the lists.  Saves on re-draw
  298. X * time.
  299. X */
  300. X
  301. X  xv_set(pi_dir,
  302. X    XV_SHOW,        FALSE,
  303. X    NULL);
  304. X
  305. X  xv_set(pi_file,
  306. X    XV_SHOW,        FALSE,
  307. X    NULL);
  308. X
  309. X/*
  310. X * First delete the old entries
  311. X */
  312. X
  313. X  for (i = num_dirs - 1; i >= 0; i--)
  314. X    xv_set(pi_dir, PANEL_LIST_DELETE, i, NULL);
  315. X
  316. X  for (i = num_files - 1; i >= 0; i--)
  317. X    xv_set(pi_file, PANEL_LIST_DELETE, i, NULL);
  318. X
  319. X  num_files = 0;
  320. X  num_dirs = 0;
  321. X
  322. X/*
  323. X * Now read the new entries
  324. X */
  325. X
  326. X  dirp = opendir(current);
  327. X
  328. X  while ((entry = readdir(dirp)) != NULL) {
  329. X    tmpname = (char *)malloc(strlen(current) + entry->d_namlen + 2);
  330. X    memset(tmpname, '\0', strlen(current) + entry->d_namlen + 2);
  331. X    strcat(tmpname, current);
  332. X    strcat(tmpname, "/");
  333. X    strcat(tmpname, entry->d_name);
  334. X    if (dir_test(tmpname)) {
  335. X
  336. X/*
  337. X * A directory
  338. X */
  339. X
  340. X      if (strcmp(entry->d_name, ".")) {
  341. X        xv_set(pi_dir,
  342. X          PANEL_LIST_INSERT,    num_dirs,
  343. X          PANEL_LIST_STRING,    num_dirs, entry->d_name,
  344. X          NULL);
  345. X        num_dirs++;
  346. X      }
  347. X    }
  348. X    else {
  349. X
  350. X/*
  351. X * A file
  352. X */
  353. X
  354. X      if (strncmp(entry->d_name, ".", 1)) {
  355. X        xv_set(pi_file,
  356. X      PANEL_LIST_INSERT,    num_files,
  357. X      PANEL_LIST_STRING,    num_files, entry->d_name,
  358. X      NULL);
  359. X        num_files++;
  360. X      }
  361. X    }
  362. X    free(tmpname);
  363. X  }
  364. X
  365. X/*
  366. X * Done, show the new scroll panels
  367. X */
  368. X
  369. X  xv_set(pi_dir,
  370. X    XV_SHOW,        TRUE,
  371. X    NULL);
  372. X  xv_set(pi_file,
  373. X    XV_SHOW,        TRUE,
  374. X    NULL);
  375. X
  376. X  closedir(dirp);
  377. X}
  378. X
  379. X
  380. X/*
  381. X * Handle the selection of a directory entry in the scroll list
  382. X */
  383. X
  384. Xvoid dir_proc(item, string, client_data, op, event)
  385. XPanel_item item;
  386. Xchar *string;
  387. Xcaddr_t client_data;
  388. XPanel_list_op op;
  389. XEvent *event;
  390. X
  391. X{
  392. X  char *oldcurrent;
  393. X  int i;
  394. X
  395. X/*
  396. X * Make sure the user selected and didn't drag.  Dragging may cause unwanted
  397. X * changes
  398. X */
  399. X
  400. X  if (op == SELECT && event_id(event) != LOC_DRAG &&
  401. X
  402. X/*
  403. X * Make sure we can read the directory before making any changes
  404. X */
  405. X
  406. X      check_read(current, string)) {
  407. X
  408. X      oldcurrent = (char *)malloc(strlen(current)+1);
  409. X      memset(oldcurrent, '\0', strlen(current)+1);
  410. X      strcat(oldcurrent, current);
  411. X      free(current);
  412. X
  413. X      if (strcmp(string, "..")) {
  414. X
  415. X/*
  416. X * Add the new directory to the "current" directory string
  417. X */
  418. X
  419. X        current = (char *)malloc(strlen(oldcurrent) + strlen(string) + 2);
  420. X        memset(current, '\0', strlen(oldcurrent) + strlen(string) + 2);
  421. X        strcat(current, oldcurrent);
  422. X        if (strlen(oldcurrent) > 1)
  423. X          strcat(current, "/");
  424. X        strcat(current, string);
  425. X      }
  426. X      else {
  427. X
  428. X/*
  429. X * Go back one directory
  430. X */
  431. X
  432. X        i = strlen(oldcurrent);
  433. X
  434. X        while(oldcurrent[i] != '/') {
  435. X      oldcurrent[i] = '\0';
  436. X      i--;
  437. X        }
  438. X
  439. X/*
  440. X * Tried to go back from the root directory?
  441. X */
  442. X
  443. X        if (i < 1) {
  444. X      oldcurrent[0] = '/';
  445. X      oldcurrent[1] = '\0';
  446. X      i = 1;
  447. X        }
  448. X        else
  449. X          oldcurrent[i] = '\0';
  450. X
  451. X        current = (char *)malloc(i);
  452. X        memset(current, '\0', i);
  453. X        strcat(current, oldcurrent);
  454. X      }
  455. X
  456. X      free(oldcurrent);
  457. X
  458. X      setup_list();
  459. X  }
  460. X}
  461. X
  462. X
  463. X/*
  464. X * Handle a selection on the file list
  465. X */
  466. X
  467. Xvoid file_proc(item, string, client_data, op, event)
  468. XPanel_item item;
  469. Xchar *string;
  470. Xcaddr_t client_data;
  471. XPanel_list_op op;
  472. XEvent *event;
  473. X
  474. X{
  475. X  if (op == SELECT) {
  476. X    xv_set(ti_file,
  477. X      PANEL_VALUE,    string,
  478. X      NULL);
  479. X    xv_set(bi_open,
  480. X      XV_SHOW,        TRUE,
  481. X      NULL);
  482. X  }
  483. X  else if (op == DESELECT) {
  484. X    xv_set(ti_file,
  485. X      PANEL_VALUE,    NULL_STR,
  486. X      NULL);
  487. X    xv_set(bi_open,
  488. X      XV_SHOW,        FALSE,
  489. X      NULL);
  490. X  }
  491. X}
  492. X
  493. X
  494. X/*
  495. X * Acknowledge an Open button press
  496. X */
  497. X
  498. Xint Open(item, event)
  499. XPanel_item item;
  500. XEvent *event;
  501. X
  502. X{
  503. X  if (strcmp(NULL_STR, (char *)xv_get(ti_file, PANEL_VALUE))) {
  504. X    printf("Open: %s\n", xv_get(ti_file, PANEL_VALUE));
  505. X    xv_destroy_safe(frame);
  506. X  }
  507. X}
  508. X
  509. Xint Cancel(item, event)
  510. XPanel_item item;
  511. XEvent *event;
  512. X
  513. X{
  514. X  xv_destroy_safe(frame);
  515. X}
  516. X
  517. X
  518. X/*
  519. X * This routine tests to see if a file name is a directory entry
  520. X */
  521. X
  522. Xdir_test(filename)
  523. Xchar *filename;
  524. X
  525. X{
  526. X  struct stat statbuf;
  527. X
  528. X  stat(filename, &statbuf);
  529. X  return(S_ISDIR(statbuf.st_mode));
  530. X}
  531. X
  532. X
  533. X/*
  534. X * Check to see if the directory is readable
  535. X */
  536. X
  537. Xint check_read(oldpath, newdir)
  538. Xchar *oldpath, *newdir;
  539. X
  540. X{
  541. X  char *both;
  542. X  DIR *test_open;
  543. X
  544. X  both = (char *)malloc(strlen(oldpath) + strlen(newdir) + 2);
  545. X  memset(both, '\0', strlen(oldpath) + strlen(newdir) + 2);
  546. X  strcat(both, oldpath);
  547. X  strcat(both, "/");
  548. X  strcat(both, newdir);
  549. X
  550. X  if (!(test_open = opendir(both))) {
  551. X    free(both);
  552. X    return 0;
  553. X  }
  554. X  else {
  555. X    free(both);
  556. X    closedir(test_open);
  557. X    return 1;
  558. X  }
  559. X}
  560. X
  561. X/*---------------- end of dirlist.c ------------------------------------------*/
  562. END_OF_FILE
  563. if test 7548 -ne `wc -c <'dir_list.c'`; then
  564.     echo shar: \"'dir_list.c'\" unpacked with wrong size!
  565. fi
  566. # end of 'dir_list.c'
  567. fi
  568. echo shar: End of shell archive.
  569. exit 0
  570.