* Program: NEWFILR.PRG
* By: Grover Cox
* 11-12-92, Rev. 1.00
* This is a demo of a Pick List allowing multiple selections
* A Popup menu is created with bars of the menu showing data files in a 
* directory.  The pick list is similar to how the FoxPro FILER utility functions.
* This program lets you select the file type first(DBF & PRG/TXT only), then after making 
*   the file picks you can perform actions on the file(s) based on the type of files.
SET TALK OFF
SET ECHO OFF
CLEAR
SET PROCEDURE TO NEWFILR
CrntDrv='C'
CrntDir=CURDIR()
SET DEFAULT TO (CrntDir)
Skeleton='*.DBF'
DEFINE WINDOW FilUtil FROM 1,1 TO 20,70 TITLE ' New Filer ' SHADOW DOUBLE
ACTIVATE WINDOW FilUtil
*
* Show the current directory name
*
@ 1,1 SAY 'Current Drive:'
@ 1,16 SAY CrntDrv COLOR 'GR+/B'
@ 1,19 SAY 'Directory:'
@ 1,30 SAY CrntDir COLOR 'GR+/B'
*
* Get the File Skeleton to be displayed
*
@ 3,52 SAY 'Show Files Like'
FilLike=Skeleton
@ 4,53 GET FilLike FUNCTION '!' SIZE 1,12;
  MESSAGE 'Enter any kind of file skeleton. Only ONE is allowed'
READ
FilLike=TRIM(FilLike)
Skeleton=FilLike
WAIT WINDOW 'Reading files....' NOWAIT
*
* Put list of all files into FAry array
*
=ADIR(FAry,Skeleton,'DHS')
=ASORT(FAry)		&& Sort array by file name in alpha ascending order
NoRows=ALEN(FAry,1)	&& Get number of rows in the array
ArrowHd=CHR(16)		&& Use the left arrow head as a pick list mark char.
*
* Put the file array into the popup bars
*
DEFINE POPUP filer FROM 4,1 TO 15,48 MARGIN MULTI SCROLL;
 IN WINDOW FilUtil MARK ArrowHd
FOR x=1 TO NoRows
  DEFINE BAR x OF filer PROMPT PADR(FAry[x,1],12)+PADL(FAry[x,2],8);
  +' '+DTOC(FAry[x,3])+' '+FAry[x,4]+' '+FAry[x,5]
ENDFOR
ON SELECTION POPUP filer DO Factions
@ 3,4 SAY 'File'
@ 3,19 SAY 'Size'
@ 3,26 SAY 'Last Modified'
@ 3,42 SAY 'Attrib'
WAIT CLEAR
ACTIVATE POPUP filer
DEACTIVATE POPUP filer
RELEASE POPUP filer
DEACTIVATE WINDOW FilUtil
RELEASE WINDOW FilUtil
RELEASE FAry, NoRows
SET PROCEDURE TO
CLEAR
*SET DEFAULT TO C:\FOXPRO2\ADBUS
RETURN

PROCEDURE Factions
* Called by main program
* File Actions - select what you want to do with these files
IF LASTKEY() # 13	&& Insure last key pressed was the ENTER key
	RETURN	&& If last key WAS NOT the Enter key then return to pick list
ENDIF
DO CASE
  CASE RIGHT(FilLike,3)='DBF'		&& If DBF file type then do this
	DO DBFwork
  CASE RIGHT(FilLike,3)='PRG'		&& If PRG file type then do this
	DO ASCwork
  CASE RIGHT(FilLike,3)='TXT'		&& If TXT file type then do this
	DO ASCwork
ENDCASE
RETURN

PROCEDURE DBFwork
* Allow users to Pack or Index the file(s)
STORE .F. TO PakDBF, RndxDBF
@ 07,52 GET PakDBF FUNCTION '*C \<Pack DBF';
  MESSAGE 'Pack the selected files'
IF PakDBF		&& If PACK selected the select RndxDBF
	RndxDBF=.T.
ELSE
	RndxDBF=.F.
ENDIF
@ 09,52 GET RndxDBF FUNCTION '*C \<Reindex DBF' WHEN ! PakDBF;
  MESSAGE 'ReIndex the selected files'
STORE 1 TO choice  
@ 12,55 GET choice FUNCTION '*TV \!\<OK;\<Cancel' SIZE 1, 8;
  MESSAGE 'Select OK to start the process or CANCEL to quit now'
READ
IF choice=1
  FOR count=1 TO CNTBAR('filer')
	IF MRKBAR('filer',count)=.T.
		WAIT WINDOW NOWAIT 'Processing '+FAry[count,1]
		DatFile=TRIM(FAry[count,1])
		IF PakDBF
		   USE (DatFile) EXCLUSIVE
		   PACK
		   USE
		ELSE
		   IF RndxDBF
		      USE (DatFile) EXCLUSIVE
		      REINDEX
		      USE
		   ENDIF	&& Reindex
		ENDIF	&& Pack
	ENDIF	&& MRKBAR
  ENDFOR
ENDIF	&& Choice
WAIT CLEAR
@ 7,52 CLEAR TO 13,66
RETURN

PROCEDURE ASCwork
* Allow users to view/edit the file
STORE 1 TO ASCtask, choice
@ 07,52 GET ASCtask FUNCTION '*RNV \<View File;\<Edit File';
  MESSAGE 'Select View or Edit text of the selected file in a window'
@ 12,55 GET choice FUNCTION '*TV \!\<OK;\<Cancel' SIZE 1, 8;
  MESSAGE 'Select OK to start the process or CANCEL to quit now'
READ
IF ASCtask=1
   TxtTit=' View Text '
ELSE
   TxtTit=' Edit Text '
ENDIF
IF choice=1
  ON KEY LABEL F10 KEYBOARD CHR(23)
  FOR count=1 TO CNTBAR('filer')
	IF MRKBAR('filer',count)=.T.
		ASCFile=TRIM(FAry[count,1])
		DEFINE WINDOW text FROM 5,5 TO 20,75 SHADOW;
		    TITLE TxtTit FOOTER ' <F10> Exit '
		ACTIVATE WINDOW text
		DO CASE
		   CASE ASCtask=1
			 MODIFY COMMAND &ASCFile NOEDIT WINDOW text
		   CASE ASCtask=2
			 MODIFY COMMAND &ASCFile WINDOW text
		ENDCASE
		DEACTIVATE WINDOW text
		RELEASE WINDOW text
	ENDIF
  ENDFOR
  ON KEY LABEL F10
ENDIF	&& Choice
@ 7,52 CLEAR TO 13,66
RETURN
