G4C

; $VER: FSearch.gc 2.0
; by D.Keletsekis June 1999

; GUI for the FSearch command
; You must have FSearch 2.0 in the "c:" or "guis:c" dirs

; This gui can be called as :
; GUILOAD fsearch.gc SearchPath Text2Find

; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
;									  Globals
;
; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

WINBIG 103 22 413 159 'FSearch.gc by D.Keletsekis'
winsmall -1 20 150 -1
WinType 11110001
resinfo 8 660 270

BOX 2 13 410 128 OUT button
	gadid 988 ; for gadredraw
	attr resize 0022

; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
;									 System events
;
; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

xOnLoad searchpath findtxt
	setgad FSearch.gc 101  hide
	setgad FSearch.gc 201/250 hide
	setgad FSearch.gc 300/350 hide
	setgad FSearch.gc 325/333 off   ; size
	setgad FSearch.gc 305/306 off   ; date
	; the default values
	file = '#?'
	subdirs = ALL
	case = ''
	strsearch = On
	output = gui
	format = 'VERBOSE'
	nobin = ''
	nopath = ''
	details = ''
	highlight = HL
	min_size = ''
	max_size = ''
	from = ''
	to = ''
	header = ''
	guimode = PAR
	GuiOpen FSearch.gc
	update fsearch.gc 2 $searchpath
	update fsearch.gc 7 $findtxt
	setgad fsearch.gc 7 on

	; run fsearch..
	extract fsearch.gc guipath fsprog
	joinfile $fsprog fsearch fsprog
	ifexists file $fsprog
		; nop
	elseifexists file c:fsearch
		fsprog = c:FSearch
	elseifexists file guis:c/fsearch
		fsprog = guis:c/fsearch
	else
		ezreq 'Could not find the FSearch binary\nYou *must* copy it to C:' 'OK' ''
	endif
	guiload :fsearch.rmb		; load pop-up guis
	guiload :fsearch.pop
	guiload :fsearch.ed		; gui to interface with editor (CEd in my case)

xOnClose
	GuiQuit FSearch.gc
	GuiQuit fsearch.ft		; filetypes gui
	GuiQuit fsearch.rmb
	GuiQuit fsearch.pop
	GuiQuit fsearch.ed
	guiquit fsearch.rep

xONRMB
	if $guimode = RES
		guiopen fsearch.rmb
	endif

xOnHelp OFF
	gadid 905
	gosub #this dohelp

xRoutine dohelp
	if '$$help.text' > ''
		setwintitle fsearch.gc '$$help.text'
	else
		setwintitle fsearch.gc 'No help here, but it should be obvious..'
	endif

; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
;									Gui Mode cycler
;
; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

; ---- a cycler to change the gui to its various modes

XCYCLER 2 0 188 13 "" guimode
	gadhelp 'Click to change the GUI mode - use "m" for shortcut'
	gadid 500
	attr resize 0010
	gadkey m       ; use 'm' to cycle it
	CStr "FSearch Parameters"	PAR
	CStr "FSearch Settings" SET
	cstr "Date, Size & Type"  DATE
	cstr "FSearch Results" RES
	gosub FSearch.gc guichange

xroutine guichange
	docase $guimode
		case = PAR
			setgad FSearch.gc 1/13	 show
			setgad FSearch.gc 21/24 show
			setgad FSearch.gc 101	 hide
			setgad FSearch.gc 300/350	hide
			setgad FSearch.gc 201/250	hide
			break
		case = SET
			setgad FSearch.gc 1/13	 hide
			setgad FSearch.gc 21/24 hide
			setgad FSearch.gc 101	 hide
			setgad FSearch.gc 300/350	hide
			setgad FSearch.gc 201/250	show
			break
		case = DATE
			setgad FSearch.gc 1/13		hide
			setgad FSearch.gc 21/24 hide
			setgad FSearch.gc 101		hide
			setgad FSearch.gc 201/250	hide
			setgad FSearch.gc 300/350	show
			break
		case = RES
			setgad FSearch.gc 1/13	 hide
			setgad FSearch.gc 21/24 hide
			setgad FSearch.gc 101	 show
			setgad FSearch.gc 300/350	hide
			setgad FSearch.gc 201/250	hide
			break
	endcase

	; redraw FSearch.gc
	; NEW - using PARTREDRAW - looks great eh?
	; partredraw FSearch.gc 4 15 405 124

	; redraw 1 pixel inside the box (top of file)
	gadredraw #this 988 988 988 988 1

	if $guimode != RES
		SetWintitle FSearch.gc	'FSearch by D. Keletsekis'
	endif


; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
;									Parameters
;
; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

; -------------------------- choose searchpath(s)

BOX 10 20 394 64 in button
	gadid 12

CTEXT 20 25 "Search in :" #screen 8 2 0 0001
	gadid 1

XTEXTIN 18 37 356 13 "" searchpath sys: 100
	gadhelp 'The path FSearch will start searching from'
	gadid 2
	setgad FSearch.gc 5 on			; set cursor to next textin gad

XBUTTON 376 37 18 13 "<"			; choose a searchpath
	gadhelp 'Choose a path from a requester'
	gadid 3
	ReqFile -1 -1 300 200 'Choose SearchPath:' DIR searchpath $searchpath
	update FSearch.gc 2 $searchpath

; ------------------------ filename pattern

CTEXT 21 54 "File name pattern :" #screen 8 2 0 0001
	gadid 4

XTEXTIN 18 65 356 13 "" file '#?' 100
	gadhelp 'Enter the filepattern - #? means all files'
	gadid 5
	setgad FSearch.gc 7 on			 ; set cursor to next textin gad

XBUTTON 376 65 18 13 "<"
	gadhelp 'Choose from various common patterns'
	gadid 4
	guiopen fsearch.ft :fsearch.pat 5

XCHECKBOX 370 24 25 9 "_Include sub-directories" subdirs ALL "" ON
	gadhelp 'Check to recurse through all subdirectories'
	gadid 11

; ---------------------- text search

CTEXT 22 99 "Find text :" #screen 8 2 0 0001
	gadid 6

XTEXTIN 18 111 377 13 "" findtxt '' 100
	gadhelp 'Enter text to find - (nothing = no text search)'
	gadid 7

BOX 9 92 395 37 IN BUTTON
	gadid 8

XCHECKBOX 366 97 25 9 "Case sensitive" case CS "" OFF
	gadhelp 'Check for Case Sensitive search'
	gadid 9


; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
;								Search results listview
;
; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

XLISTVIEW 4 15 406 124 "" resfile "" 10 MULTI
	gadhelp "Display search results - Click RMB for options"
	gadid 101
	attr resize 0022
	gadfont #mono 8 001

	; -------- If its a LINE - go to it..
 
	if $resfile[0][1] = ' '
	or $resfile[0][4] = '›33m'

		if $resfile[0][4] = '›33m'						; cut off highlighting
			cutvar resfile[4] cut word  1 resnum
		else
			cutvar resfile cut word 1 resnum			; get the line number
		endif
		extract resnum clean resnum
		cutvar resnum	cut char -1 ''
		while $$lv.rec[0][1] == '	'					; get the parent file
		or $$lv.rec[0][4] == '›33m'
		and $$lv.line > ''
			lvgo	prev
		endwhile
		resfile	= $$lv.rec
		if $resfile[0][4] = '›32m'	; cut off highlighting
			cutvar resfile[4] cut word 1	resfile
			cutvar resfile cut char -4 temp	  		; trim the csi
		endif

		gosub fsearch.ed LoadFile $fsearch.gc/resfile $fsearch.gc/resnum

	; ------------ If its a FILE - use popup window to deal with it..

	elseif $resfile[0][1] != '*'

		if $resfile[0][4] =	'›32m'	; cut off highlighting
			cutvar resfile[4] cut word 1 resfile
			cutvar resfile cut char -4 temp	  		; trim the csi
		endif
		guiopen fsearch.pop $resfile 1

	endif 


; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
;									 FSearch settings
;
; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

; ------- output mode

BOX 23 28 177 98 in button
	gadid 201

XRADIO 158 38 18 9 output 2
	gadhelp 'Choose where you want the output from FSearch to go to'
	gadid 202
	rstr 'Output to Gui'   gui
	RStr 'Output to Shell' cli

; ----------- Output format 

XRADIO 158 68 19 9 format 2
	gadhelp 'Says it all really..'
	gadid 203
	RStr	'Show text found' 'VERBOSE'
	RStr	'Filenames only'	''

; ----------- LINE switch

XRADIO 159 96 19 9 lineswitch 2
	gadhelp 'Show Line or Byte No of text found - Byte is faster!'
	gadid 203 ; same as above
	RSTR 'Show Byte No' ''
	RSTR 'Show Line No' LINE

; ------------ Settings - checkboxes

BOX 218 28 178 70 IN BUTTON
	gadid 208

XCHECKBOX 356 35 25 9 'Highlight results' highlight "HL" "" ON
	gadhelp 'Bask in the glory of colored ansi text'
	gadid 205

XCHECKBOX 356 50 25 9 "Check binary files" nobin "" "NOBIN" ON
	gadhelp 'Also search binary (non-text) files'
	gadid 211

XCHECKBOX 356 65 25 9 "Show File Path" nopath "" "NOPATH" ON
	gadhelp 'Print the full path of the files found'
	gadid 212

XCHECKBOX 356 79 25 9 "Show File Details" details "INFO" "" OFF
	gadhelp 'Also print file info - Disables REPLACE!'
	gadid 213

; ------- help & save settings

XCHECKBOX 391 1 20 11 'OnLine help' helpon "ON" "OFF" OFF
	gadhelp 'Start/Stop showing these help messages'
	attr resize 2000
	if $helpon = ON
		setgad #this 905 ON
		setgad fsearch.rmb 905 on
	else
		setwintitle #this 'FSearch.gc by D.Keletsekis'
		setgad #this 905 OFF
		setgad fsearch.rmb 905 off
	endif

XBUTTON 261 111 91 13 "Help.."
	gadhelp 'Load & display the FSearch.readme file'
	gadid 220
	update FSearch.gc 500 3        ; change gui to show results listview
	guimode = RES
	gosub FSearch.gc guichange
	lvuse #this 101
	lvchange :fsearch.readme

; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
;										  DO IT!
;
; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

; This is where the commandline is formed and launched. The name of
; the variable which holds it is 'options'


XBUTTON 20 143 91 13 "_Search"
	gadhelp 'Start the search!'
	gadid 501
	attr resize 1210
	if $searchpath < ' '
		setwintitle FSearch.gc 'Wrong parameters!'
		stop
	endif
	SetWintitle FSearch.gc Searching...

	; --- construct the command line

	options = '$searchpath '
	if $file != '#?'
	and $file > ' '
		appvar options 'PAT=$file	'
	endif
	if $findtxt > ' '
		appvar options 'TXT=\"$findtxt\" $case '
	endif
	if $size = SIZE
		if $min_size  > ' '
		and $min_size > 0
			min = $min_size
			if $min_type = mb
				min == $min *	1000
			endif
			appvar options 'MIN=$min '
		endif
		if $max_size  > ' '
		and $max_size > 0
			max = $max_size
			if $max_type = mb
				max == $max *	1000
			endif
			appvar options 'MAX=$max '
		endif
		if $min_size > $max_size
		and $max_size != ''
			ezreq 'Minimum size is more than\nMaximum size..' Ooops! ''
			stop
		endif
	endif
	if $date = DATED
		if $from_date > ' '
			appvar options 'FROM=$from_date '
		endif
		if $to_date > ' '
			appvar options 'TO=$to_date '
		endif
	endif
	extract header clean header
	if $header > ' '
	and $header != NONE
		appvar options 'HEADER=$header'
		appvar options '#? '
	endif
	appvar options '$format $nobin $nopath $details $highlight $subdirs $lineswitch'

	; disable gadgets
	setgad FSearch.gc 501/502 off

	; --- launch according to where it should output

	if $output = cli
		say '\n-------------- Search results :\n'
		launch 1 '$fsprog $options'
	else
		launch 2 '$fsprog >pipe:findres $options'
		update FSearch.gc 500 3        ; change gui to show results listview
		guimode = RES
		gosub FSearch.gc guichange
	endif

; ---------- xONRETURN commands - handle returns according to launches

; -- return from shell launch

xonreturn 1   ; to the shell
	say '-------------- Search finished.\n'
	setgad FSearch.gc 501/502 on
	SetWintitle FSearch.gc 'Search finished.'

; -- return from gui output mode launch

xonreturn 2   ; to gui or file
	lvuse FSearch.gc 101
	setgad FSearch.gc 501/502 on
	SetWintitle FSearch.gc 'Search finished.'

; -- the pipe event where the output is piped at

xpipe pipe:findres ON
	gadid 999
	lvuse FSearch.gc 101      ; whenever a line is available
	lvadd $$pipe				  ; append it to our listv

; -- We're all human..

xonfail
	setwintitle FSearch.gc '** Error **'
	setgad FSearch.gc 501/502 on


; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
;								 Clear & cancel buttons
;
; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

; --------------- The CLEAR button - reset all settings

XBUTTON 209 143 91 13 "C_lear"
	gadhelp 'Clear the current gadgets - will not change hidden gadgets'
	gadid 502
	attr resize 1210
	docase $guimode
	case =	PAR
		file	= #?
		update #this 5 $file
		findtxt = ''
		update #this 7 ''
		break
	case =	SET
		break
	case =	DATE
		date	= ANY
		update FSearch.gc 301 0
			from_date = ''
			update FSearch.gc 305 ''
			to_date	= ''
			update FSearch.gc 306 ''
			size	= ANY
			update FSearch.gc 321 0
			min_size =	''
			update FSearch.gc 325 ''
			max_size =	''
			update FSearch.gc 330 ''
			min_type =	kb
			update FSearch.gc 329 0
			max_type =	kb
			update FSearch.gc 333 0
			setgad FSearch.gc 325/333	off	; size
			setgad FSearch.gc 305/306	off	; date
			header = NONE
			update FSearch.gc 341 'All files'
			break
	case = RES
			lvuse FSearch.gc	101	; clear the lv
			lvclear
			break
	endcase

; --------------- The CANCEL button

XBUTTON 304 143 91 13 "_Cancel"
	gadhelp 'Abort the Search!'
	gadid 503
	attr resize 1210
	breaktask $fsprog C		 ; break fsearch
	setgad FSearch.gc 999 CLEAN	 ; stop & flush pipe output
	lvuse FSearch.gc 101
	lvadd "**** BREAK ****"


; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
;							  SET DATE, SIZE & TYPE
;
; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

; The surrounding boxes
BOX 13 17 387 47 in button
	gadid 335
BOX 13 74 387 41 in button
	gadid 336

; ----- Date

CTEXT 20 20 Date #screen 8 2 0 "0001"
	gadid 300

XRADIO 173 25 17 9 date 4
	gadhelp 'Use the from & to dates entered here'
	gadid 301
	RStr 'Any date'	 ANY
	RStr 'Files dated' DATED
	if $date = ANY
		setgad FSearch.gc 305/306 off
	else
		setgad FSearch.gc 305/306 on
	endif

XTEXTIN 302 31 77 12 "From:" from_date '' 100
	gadhelp 'Show files created/changed after this date'
	gadid 305
	setgad FSearch.gc 306 on

XTEXTIN 302 45 77 12 "To:" to_date '' 100
	gadhelp 'Show files created/changed before this date'
	gadid 306
	setgad FSearch.gc 305 on

; ----- Size

CTEXT 20 77 Size #screen 8 2 0 "0001"
	gadid 320

XRADIO 175 82 18 9 size 4
	gadhelp 'Use the file size settings entered here'
	gadid 321
	RStr 'Any size' ANY
	RStr 'Files sized' SIZE
	if $size = SIZE
		setgad FSearch.gc 325/333 on
	else
		setgad FSearch.gc 325/333 off
	endif

XTEXTIN 251 85 47 12 Over min_size "" 100
	gadhelp 'Show files larger than this'
	gadid 325

XBUTTON 300 85 15 12 "<"
	gadid 327
	if $min_size > 0
		counter min_size dec 1
		update FSearch.gc 325 $min_size
	endif

XBUTTON 318 85 15 12 ">"
	gadid 328
	counter min_size inc 1
	update FSearch.gc 325 $min_size

XCYCLER 334 85 52 12 "" min_type
	CStr Kb kb
	CStr Mb mb
	gadid 329

XTEXTIN 251 99 46 12 Under max_size "" 100
	gadhelp 'Show files smaller than this'
	gadid 330

XBUTTON 300 99 15 12 "<"
	gadid 331
	if $max_size	> 0
		counter max_size dec 1
		update FSearch.gc 330 $max_size
	endif

XBUTTON 318 99 15 12 ">"
	gadid 332
	counter max_size inc 1
	update FSearch.gc 330 $max_size

XCYCLER 334 99 52 12 "" max_type
	CStr Kb kb
	CStr Mb mb
	gadid 333

; ----- File type

CTEXT 28 122 Type #screen 8 2 0 0001
	gadid 340

TEXT 66 122 293 12 "All files" 80 BOX
	gadhelp 'Choose the type of file you want'
	gadid 341

XBUTTON 363 122 23 12 "<"
	gadhelp 'Choose a file type from a pop-up list'
	gadid 342
	guiopen fsearch.ft :fsearch.types 341


; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

;								ROUTINES

; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

xRoutine NextFile
	lvuse fsearch.gc 101
	lvmulti first	; 1st every time since we set item off at end

	; Skip all lines ..
 	while $resfile[0][1] = ' '		; normal line
	or $resfile[0][4] = '›33m'		; highlighted
	or $resfile[0][1] = '*'			; break
		lvmulti off
		lvmulti next
	endwhile

	if $$lv.line = ''					; end of list
		resfile = ''
	else									; cut off esc seq
		if $resfile[0][4] = '›32m'
			cutvar resfile cut char 4 ''
			cutvar resfile cut char -4 ''
		endif
		lvmulti off
	endif 


; ##################################################################
; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;
;									  FSearch.ft
;
;			 Gui with listview for choosing file header type
;
; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; ##################################################################

NEWFILE	fsearch.ft

WINBIG 245 109 179 116	''
WinType 00001000
winonmouse 100 50
; varpath FSearch.gc

xonopen list id
	lvuse fsearch.ft 1
	lvchange $list

xoninactive
	guiclose fsearch.ft

xonrmb
	guiclose fsearch.ft

; start off with empty listview - load the fsearch.types file in the
; fsearch.gc xonload event

XLISTVIEW 0 0 179 116  "" ftype '' 10 TXT
	gadid 1
	gadfont #mono 8 000
	guiclose fsearch.ft
	if $id = 341	; file type
		cutvar ftype cut word -1 fsearch.gc/header
		update FSearch.gc $id $ftype
	else		; search pattern
		cutvar ftype cut word -1 fsearch.gc/file
		extract fsearch.gc/file clean fsearch.gc/file
		update FSearch.gc $id $fsearch.gc/file
	endif





