G4C

; This GUI is similar to the Workbench "Execute command" GUI, but more.
; There is a textin gadget for entering commands, buttons for choosing
; files, changing the directory, and opening a newshell.

; More important, it can also send ARexx commands to the port shown
; in the other (bottom right) textin gadget (which can also be changed)

; USE :
; Guiload guis:tools/cli.gc <commandline> <mode>    - or
; GuiOpen cli.gc <commandline> <mode>


WINBIG 48 34 554 39 "Enter commands :"
WinSmall 0 -1 422 30
WinType  11110010
BOX 0 0 554 39 out BUTTON

; ------------------------------------------------------------------
;               System events
; ------------------------------------------------------------------

xOnLoad cl_Main cl_Mode
	if $cl_Mode < ' '
	   cl_Mode = G4C
	endif
	cl_Port = $$G4C.port    ; Port name (can also sendrexx to ourselves)
	guiopen cli.gc

	xonRELOAD cl_Main cl_Mode
	guiopen cli.gc

xOnOpen
	setwintitle cli.gc '$$CURRENT_DIR'
	Update cli.gc 1 $cl_Main        ; show whatever the command line is 
	SetGad cli.gc 1 ON              ; to get the cursor in the gadget immediately
	; update cycler to mode given
	docase $cl_Mode
	   case = G4C
	        update cli.gc 10 0
	        break
	   case = CLI
	        update cli.gc 10 1
	        break
	   case = RUN
	        update cli.gc 10 2
	        break
	   case = EXEC
	        update cli.gc 10 3
	        break
	endcase
	stack = $$g4c.stack
	update cli.gc 12 $stack

xOnRMB
	status

; ------------------------------------------------------------------
;===============> Main text input gadget
; ------------------------------------------------------------------

XTEXTIN 10 5 513 14 "" cl_Main "" 512
	gadid 1
	gadhelp 'Enter Command line'
	gosub cli.gc DoCommand

XBUTTON 525 5 20 14 C
	gadhelp 'Clear Command line'
	cl_Main = ""
	update cli.gc 1 ""

; ------------------------------------------------------------------
;========> Cycler for choosing ARexx/CLI/RUN modes
; ------------------------------------------------------------------

XCYCLER 10 20 80 14 "" cl_Mode
	GadID 10 ; we may want to change it from elsewhere
	gadhelp 'Choose action to take on command line'
	CSTR ARexx     G4C
	CSTR CLI       CLI
	CSTR RUN       RUN
	CSTR Exec      EXEC


; ------------------------------------------------------------------
;========> GO button (to execute the command)
; ------------------------------------------------------------------

XBUTTON 90 20 40 14 "GO!"
	gadhelp 'Execute action shown in Cycler'
	gosub cli.gc DoCommand


; ------------------------------------------------------------------
;=======> button for file requester for commands
; ------------------------------------------------------------------

XBUTTON 135 20 65 14 "Cmd.."
	gadhelp 'Choose a Command'
	cl_cmd = ""
	REQFILE -1 -1 300 -60 "Choose Files" LOAD cl_cmd  sys:c
	if $cl_cmd > ""
	   cl_temp = $cl_Main
	   cl_Main = '$cl_cmd '
	   appvar cl_Main $cl_temp
	   update cli.gc 1 $cl_Main
	   delvar cl_temp
	endif

; ------------------------------------------------------------------
;========> Button for File requester for filenames
; ------------------------------------------------------------------

XBUTTON 200 20 65 14 "Files.."
	gadhelp 'Choose a File as an argument'
	cl_File = ""
	REQFILE -1 -1 300 -60 "Choose Files" MULTI cl_File  $$CURRENT_DIR
	if $cl_File > ""
	   appvar cl_Main $cl_File
	   update cli.gc 1 $cl_Main
	endif


; ------------------------------------------------------------------
;========> Button to open a shell
; ------------------------------------------------------------------

XBUTTON 265 20 40 14 Cli
	gadhelp 'Open a Shell'
	CLI 'Newshell "con:0/150/640/100/NewShell/CLOSE"'


; ------------------------------------------------------------------
;========> Button to change directory
; ------------------------------------------------------------------

XBUTTON 305 20 40 14 CD
	setvar cl_Dir ""
	gadhelp "Change Gui4Cli's current directory"
	REQFILE -1 -1 300 -60 "Choose directory" DIR cl_Dir ""
	if $cl_Dir > ""
	   CD $cl_Dir
	   SetWinTitle cli.gc '$cl_Dir'
	endif

; ------------------------------------------------------------------
;========> Set stack size
; ------------------------------------------------------------------

XTEXTIN 365 20 55 14 "" stack 4000 12
	gadid 12
	gadhelp 'Sets the STACK size for CLI or RUN'
	setstack $stack

XBUTTON 350 20 15 14 "<"
	gadhelp 'Decrease stack size'
	if $$g4c.stack > 1500             ; minimum stack size
	   stack == $$g4c.stack - 1024
	endif
	update cli.gc 12 $stack
	setstack $stack

XBUTTON 420 20 15 14 ">"
	gadhelp 'Increase stack size'
	stack == $$g4c.stack + 1024
	update cli.gc 12 $stack
	setstack $stack

; ------------------------------------------------------------------
;========> TEXTin gadget to get port name for sendrexx command
; ------------------------------------------------------------------

XTEXTIN 440 20 105 14 "" cl_Port "Gui4Cli" 40
	GadID 2
	gadhelp 'Port name for sending ARexx commands'


; ------------------------------------------------------------------
;========> ROUTINE to execute command
; ------------------------------------------------------------------

xROUTINE DoCommand
	docase $cl_Mode
	case  = G4C
	        SendRexx $cl_Port $cl_Main
	        break
	case  = CLI
	        CLI $cl_Main
	        break
	case  = RUN
	        RUN $cl_Main
	        break
	case  = EXEC
	        CLI 'execute $cl_Main'
	        break
	endcase
	if $$RETCODE = 0
	   SetWinTitle cli.gc "OK - $$G4C.DIR"
	else
	   SetWinTitle cli.gc "* ERROR * $$RETCODE $$REXXRET"
	endif


; ------------------------------------------------------------------
;=========> before commands are executed, we restore the window title
; ------------------------------------------------------------------

xBEFORE
	SetWinTitle cli.gc '$$CURRENT_DIR'


