G4C

; This is a Full blown File Manager.

; I know it's difficult to follow specially since it consists
; of many GUIs which open-close on their own, but.. hey, it's
; a tough world out there..

; The main variable used is "lv_file", which is the name of the
; variable where both listviews will store the files selected.

; Get the full archive of Gui4Cli (Aminet:Dev/Gui/Gui4Cli.lha),
; read the guide and construct the file manager of your dreams! 


WINBIG 100 -10 248 229 Devices
WinType 11110001
resinfo 8 640 256
; nofontsense

xOnLoad
	fm_mode = 1
	setgad Dir.gc 2       HIDE       ; hide the gadgets of the
	setgad Dir.gc 201/210 HIDE
	setgad Dir.gc 10      HIDE
	guiopen Dir.gc
	guiload :Dir.g                     ; load the helper guis
	guiload :dir.g2
	guiload :dir.popfav
	guiload guis:tools/Prefs/players   ; the default viewers
	favmode.1 = 0
	favmode.2 = 0

xonreload
	guiopen dir.gc

xonquit
	guiquit Dir.g        ; quit our other guis also
	guiquit Dir.g2
	guiquit Dir.g3
	guiquit dir.g4
	guiquit dir.pop2
	guiquit cli.gc
	guiquit dir.prot
	guiquit dir.user
	guiquit dir.popfav
	guiquit getfiletype
	guiquit viewfile    ; it will quit viewfile2

xOnOpen
	SetGad dir.gc 20 OFF

xOnClose 
	SetGad dir.gc 20 ON


xAppIcon 0 0 :dir.gc DIR.gc *lv_app OFF
	GadID 20
	GuiOpen dir.gc
	GuiScreen dir.gc FRONT
	if $*lv_app > " "
	   gosub dir.gc appdir 
	endif


xAppWindow  *lv_app                ; The FM is also an AppWindow
	gosub dir.gc appdir             ; going to the dir of the icon..


xroutine appdir                    ; cd to the appitem's dir
	ifexists DIR $*lv_app           ; if it's a dir,vol,dev or asn
	orifexists DOS $*lv_app
	    dir  = $*lv_app
	    file = ''
	elseifexists file $*lv_app      ; if it's a file
	    extract *lv_app PATH dir
	    extract *lv_app FILE file
	endif
	lvuse dir.gc 1
	lvdir #$dir
	lvfind $file
	setwintitle dir.gc '$dir'
	delvar *lv_app

xOnRMB
	if $favmode.1 > 0
	   favid = 1
	   guiopen dir.popfav
	elseif $favmode.2 > 0
	   favid = 2
	   guiopen dir.popfav
	else
	   guiopen Dir.g     ; This has the copy, delete etc functions
	endif


; ===================================================================
;     The directory listviews
; ===================================================================

; Note they both have Hooks to display the current dir to the user.

;---------> the left side

XLISTVIEW 0 0 230 228 "" lv_file "" 10 DIR
	gadid 1
	Attr resize 0012	
	GadFont #mono 8 000        ; CHANGE THIS TO THE FONT YOU LIKE!
	; LVColors   2 1 3 2    ; uncomment this line for genlocking
	; attr dirstyle 2031       ; uncomment for 3D dirs

	if $favmode.1 = 1
	   cutvar lv_file cut word -1 lv_file
	   extract lv_file clean lv_file
	   if $lv_file == TITLE
	      stop
	   endif
	   favmode.1  = 0
	   lvmode DIR
	   lvdir #$lv_file
	   setwintitle Dir.gc '$lv_file'
	else
	   guiopen Dir.g2
	endif

xLVDirHook 1
	setwintitle Dir.gc '$$lv.dir'   ; show it in the wintitle

;----------> the right side

XLISTVIEW 248 16 230 212 "" lv_file "" 10 DIR
	gadid 2
	attr resize 1012
	GadFont #mono 8 000        ; CHANGE THIS TO THE FONT YOU LIKE!
	; LVColors   2 1 3 2    ; uncomment this line for genlocking
	if $favmode.2 = 1
	   cutvar lv_file cut word -1 lv_file
	   extract lv_file clean lv_file
	   if $lv_file == TITLE
	      stop
	   endif
	   favmode.2  = 0
	   lvmode DIR
	   lvdir #$lv_file
	   update Dir.gc 10 $lv_file
	else
	   guiopen Dir.g2
	endif

xLVDirHook 2
	update Dir.gc 10 $$lv.dir       ; show it in xTextIn gadget below

XTEXTIN 248 0 230 16 "" lvdir2 "" 140
	gadid 10
	attr resize 1010
	GadFont topaz.font 8 000
	lvuse dir.gc 2
	LVDir #$lvdir2   ; you can change the dir from here

; ===================================================================
;     Disk in/out events
; ===================================================================

xondiskin
	gosub dir.gc diskrefresh

xondiskout
	gosub dir.gc diskrefresh

xroutine diskrefresh
	lvuse dir.gc 1
	if $$lv.dir = ''     ; refresh if it's a device list
	   lvdir disks
	endif
	lvuse dir.gc 2
	if $$lv.dir = ''
	   lvdir disks
	endif

; ===================================================================
;     LEFT - Listview control buttons
; ===================================================================

;--------> for the left side (source)

XBUTTON 231 0 16 13 P
	GadID 101
	attr resize 1010
	lvuse dir.gc 1
	LVDir parent
	lv_dir1 = $$LV_DIR
	if $$lv.dir = ''
		setwintitle Dir.gc 'Devices'
	else
		setwintitle Dir.gc '$$lv.dir'
	endif

XBUTTON 231 13 16 13 R
	GadID 102
	attr resize 1010
	lvuse dir.gc 1
	LVDir Root
	lv_dir1 = $$LV_DIR
	setwintitle Dir.gc '$$lv.dir'

XBUTTON 231 26 16 13 D
	GadID 103
	attr resize 1010
	lvuse dir.gc 1
	LVDir Disks
	lv_dir1 = ""

XBUTTON 231 39 16 13 A
	GadID 104
	attr resize 1010
	lvuse dir.gc 1
	LVDir all

XBUTTON 231 52 16 13 N
	GadID 105
	attr resize 1010
	lvuse dir.gc 1
	LVDir None

XBUTTON 231 65 16 13 F
	GadID 106
	attr resize 1010
	lvuse dir.gc 1
	lvmode MULTI
	lvchange guis:tools/fav/DirDef
	favmode.1 = 1
	favid = 1

XBUTTON 231 78 16 13 +      ; add to favourites
	GadID 107
	attr resize 1010
	lvuse dir.gc 1
	gosub dir.gc addfav

XBUTTON 231 189 16 13 R
	GadID 108
	attr resize 1210
	lvuse dir.gc 1
	lvdir Refresh

XBUTTON 231 202 16 13 <       ; move list left
	GadID 109
	attr resize 1210
	lvuse dir.gc 1
	lvmove -10

XBUTTON 231 215 16 13 >
	GadID 110
	attr resize 1210
	lvuse dir.gc 1
	lvmove 10

; ===================================================================
;  	RIGHT - Listview control buttons
;		- give gadID so we set them on/off
; ===================================================================

XBUTTON 479 0 16 13 P
	GadID 201
	attr resize 1010
	lvuse dir.gc 2
	LVDir parent
	setvar lv_dir2 $$LV.DIR
	update Dir.gc 10 $$lv.dir

XBUTTON 479 13 16 13 R
	gadid 202
	attr resize 1010
	lvuse dir.gc 2
	LVDir Root
	setvar lv_dir2 $$LV.DIR
	update Dir.gc 10 $$lv.dir

XBUTTON 479 26 16 13 D
	gadid 203
	attr resize 1010
	lvuse dir.gc 2
	LVDir Disks
	setvar lv_dir2 ""
	update Dir.gc 10 ""

XBUTTON 479 39 16 13 A
	gadid 204
	attr resize 1010
	lvuse dir.gc 2
	LVDir All

XBUTTON 479 52 16 13 N
	gadid 205
	attr resize 1010
	lvuse dir.gc 2
	LVDir None

XBUTTON 479 65 16 13 F
	gadid 206
	attr resize 1010
	lvuse dir.gc 2
	lvmode MULTI
	lvchange guis:tools/fav/DirDef
	favmode.2 = 1
	favid = 2

XBUTTON 479 78 16 13 +      ; add to favourites
	gadid 207
	attr resize 1010
	lvuse dir.gc 2
	gosub dir.gc addfav

XBUTTON 479 189 16 13 R
	gadid 208
	attr resize 1210
	lvuse dir.gc 2
	lvdir Refresh

XBUTTON 479 202 16 13 <    ; move list left
	GadID 209
	attr resize 1210
	lvuse dir.gc 2
	lvmove -10

XBUTTON 479 215 16 13 >    ; move list right
	GadID 210
	attr resize 1210
	lvuse dir.gc 2
	lvmove 10

; ===================================================================
; 		MIDDLE buttons
; ===================================================================

;---------> open full file manager

XBUTTON 231 107 16 11 >
	gadid 11
	attr resize 1010
	if $fm_mode = 1
	   gosub Dir.gc OpenWindow
	else
	   gosub Dir.gc CloseWindow
	endif


;--------> button to switch left <-> right lvs

XBUTTON 231 96 16 11 x
	gadid 12
	attr resize 1010
	lvuse dir.gc 1
	tempdir1 = $$lv.dir
	lvuse dir.gc 2
	tempdir2 = $$lv.dir
	lvdir #$tempdir1
	lvuse dir.gc 1
	lvdir #$tempdir2
	setwintitle Dir.gc '$tempdir2' ; and their dir displays
	update Dir.gc 10 $tempdir1
	delvar tempdir#?


;--------> same directories

XBUTTON 231 118 16 11 =
	gadid 13
	attr resize 1010
	if $$lv.gui == dir.gc        ; check that the internal vars point to us
	   id  = $$lv.id
	   dir = $$lv.dir
	   if $id = 1
	      lvuse dir.gc 2 
	      lvdir #$dir
	      update Dir.gc 10 $dir
	   else
	      lvuse dir.gc 1
	      lvdir #$dir
	      setwintitle Dir.gc '$dir'
	   endif
	endif

; ===================================================================
; 		ROUTINES
; ===================================================================

xRoutine OpenWindow     ; Routine to enlarge the window
   fm_mode = 2
   setgad Dir.gc 2       SHOW
   setgad Dir.gc 201/210 SHOW
   setgad Dir.gc 10      SHOW
   changearg Dir.gc 11 4 <
   changegad Dir.gc 0 -1 -1 496 229 ""
   redraw Dir.gc

xRoutine CloseWindow    ; make it small again
   fm_mode = 1
   setgad Dir.gc 2       HIDE
   setgad Dir.gc 201/210 HIDE
   setgad Dir.gc 10      HIDE
   changegad Dir.gc 0 -1 -1 248 229 ""
   changearg Dir.gc 11 4 >
   redraw Dir.gc

xroutine addfav         ; add a path to the favourites
	; see first if path exists
	favdir = $$lv.dir
	cli 'guis:c/fsearch >env:.addfav guis:tools/fav/DirDef txt=$favdir'
	if $.addfav > " "
	   ; path already exists
	else
	   ; get dir name as identifier name
	   extract favdir file favname
	   ; append entry to the favourites file
	   append guis:tools/fav/DirDef '\n$favname\t\t\t\t$favdir'
	endif
	delvar .addfav


