G4C   

; ---------------------------------------------------------------
; FSearch.rep - GUI for searching the files found with
; the fsearch.gc gui - uses the REP command which must be
; in c: or guis:c.
; - Usage :
;   GuiLoad :fsearch.rep FromText ToText
; ---------------------------------------------------------------

WINBIG 129 75 400 77 'Replace in selected files'
WinType 11110001
winonwin fsearch.gc 15 40
BOX 0 0 0 0 OUT ICONDROP

; ----------------- General events

xOnLoad from
	to    = ""
	savemode = OVER
	ci = ""
	update #this 1 $from
	guiopen #this
	setgad #this 2 ON
	prog = "REP"
	extract prog fullpath prog
	if $prog = ''
		ezreq 'The CLI command REP was not\nfound in your path.\nI need it..' OK ''
		guiquit #this
		stop
	endif		
	CLI 'resident $prog pure add'

xOnReload from
	guiopen #this

xOnClose
	guiquit #this
	run 'resident Rep remove'

; ---------------- text-in gadgets for entering..

XTEXTIN 80 5 310 16 'Replace:' from "" 200	; string to Rep
	gadid 1
	setgad #this 2 on   ; activate next textin


XTEXTIN 81 24 310 16 'With:' to "" 200		; Rep with this
	gadid 2

; ---------------- Do we want to add extensions to the files ?

XCYCLER 9 42 190 14 "" savemode
	gadid 3
	cstr 'OVERWRITE Files'    OVER
	cstr 'Add .rep extension' EXT

; ---------------- case insensitive search ?

XCYCLER 201 42 190 14 "" ci
	gadid 6
	cstr 'Case Sensitive'    ""
	cstr 'Case Insensitive'  I

; ---------------- A text display, telling you what's going on.

TEXT 9 57 310 14 Waiting.. 100 BOX
	gadid 4

; ---------------------------------------------------------------
; ---------------- Start converting..
; ---------------------------------------------------------------

XBUTTON 321 57 70 14 Start
	gadid 5
	local count
	count = 0

	if $from = ""
	   ezreq 'No "Replace:" defined!' OK ""
	   stop
	endif
	gosub #this disable

	lvuse fsearch.gc 101

	gosub fsearch.gc NextFile
	while $fsearch.gc/resfile > ''

		extract fsearch.gc/resfile UNQUOTE fsearch.gc/resfile
		dest = $fsearch.gc/resfile			; prepare destination filename
		if $savemode = EXT
		   appvar  dest .rep
		   dest = '$dest'
		endif

		update #this 4 'REP: $fsearch.gc/resfile'

		set deeptrans off
		CLI 'Rep \"$fsearch.gc/resfile\" \"$from\" \"$to\" \"$dest\" $ci'
		set deeptrans on
		++count

		gosub fsearch.gc NextFile
	endwhile

   gosub #this enable
	update #this 4 'Processed $count files.'

; ---------------------------------------------------------------
; --------------- Routines to disables & enable guis
; ---------------------------------------------------------------

xRoutine disable
	setwintitle #this 'Press ESCAPE to abort!'
	guiwindow dir.gc wait
	setgad #this 1/6 off
	
xRoutine enable
	guiwindow dir.gc resume
	setwintitle #this 'Replace in selected files'
	setgad #this 1/6 on

xOnFail
	gosub #this enable

