hex
' sob.menu.v=new
[if]
 
   0 value si-help-window
   0 value si-help-list
   0 value si-help-edit

   : cb-help-close
      si-help-window   sob=delete
      0 to si-help-window                 \ used to show that the WINDOW is deleted
   ;

   \ this is linked to the standard text output routine of the CLI interpreter
   \ each time a CR is sent by the normal CLI  TYPE or EMIT commands, then the 
   \ last line of text is passed to this definition
   \ See command   CATCH_ON and CATCH_OFF

   : cb-a-line
      si-help-list sob.item=add drop 
  ;

   \ each time a CR is pressed in the HELP EDIT field this callback will
   \ execute.
   \ It redirects the CLI TYPE and EMIT commands to pass their text
   \ to    cb-a-line
   \ Then it passes a text string to the CLI interpreter that results
   \ in the wanted help information being sent via TYPE and/or EMIT 
   \ to the display.
   \ Once the string is processed, we reset TYPE and EMIT to the
   \ normal output handling.

   : cb-edit-cr  ( key-char -- )
      drop                              \ do not need the key character
      si-help-list  sob=empty           \ empty the LIST field
      ['] cb-a-line    catch_on         \ redirect TYPE and EMIT
      pad 0 
      s"   help* "     join"            \ create first part of text string

      0  sob.callback@id sob@line       \ get text from EDIT field
      
      \ following code ensures that only the leading text string is
      \ handled. If this was NOT done and the user entered say
      \  fred  2 3 * . cr           
      \ in the EDIT field then the actual text passed to EVALUATE would be
      \  help*  fred  2 3 * . cr
      \ The following forces this to be reduced to
      \  help*  fred

      bl trim-leading  bl trim-trailing
      2dup 0
      ?DO
         dup @char bl =
         IF
            drop drop i 1 leave 
         THEN
         char+
         sizeof(char)
      +LOOP
      drop 

      join"                             \ add EDIT text to first part of text
      2dup type cr
      evaluate                          \ sent text string to interpreter
      catch_off                         \ reset TYPE and EMIT to normal
   ;

   : cb-help
      si-help-window                   \ does the HELP window already exist
      IF                               \ YES, so display it
         1 si-help-window sob=show      
      ELSE                             \ NO, so create it
         s" Help Window" sob.window=new  to si-help-window
 

         ['] cb-help-close                \ link windows   CLOSE button
         si-help-window   sob.callback=xt1 \ to a callback handler

         sob.con.row drop
            s" help for what ?" 0 sob.label=new drop  

            \ now make an edit field for user to enter the HELP word in to
            s"                            "   \ define width of next SOB
            0                                 \ NO callback
            sob.edit=new to si-help-edit          \ create an EDIT SOB

            [']  cb-edit-cr   si-help-edit    \ link to a CALLBACK
            sob.key.cr=xt                     \ when CR key pressed in EDIT field

            si-help-edit   sob=empty          \ empty initial EDIT field    

         sob.con=close

         sob.list>scroll.v sob.list>border \ define characteristics of next  
         sob.list>scroll.h sob.list>height \ created LIST box
         10                                \ say 10 lines as default height
         s" Enter here the name of the text to be found"
         0 sob.list=new  to si-help-list       \ make the LIST box
      THEN

      si-help-window    sob=shrink         \ shrink the HELP window to fit its contents
   ;

[then]

\ ==============================================
\ This code defines the BROWSE and LAUNCH capability
\ If you do not want it then either cut it out
\ or set the follow    1   to a  0

1 [if]

: BrowseAndLaunch"
   s" RipeForth Browse and Launch"
;

\ ==============================================

: date"
   s" 2017-04-08"
;
\ ==============================================
: crlf"
   0x0d char>"
   0x0a char>" join"
;
\ ==============================================
variable FileNamelen

variable FileName
64 allot

: CopyToFileName ( c-addr1 , u -- c-addr2 , u )
  FilenameLen !
  FileName
  cmove

   FileName
   FilenameLen @
;

\ ==============================================

-1 value si-myedit-win
-1 value si-myedit-box
-1 value si-title

variable fname-len

0 value Eopfile 
 
BrowseAndLaunch"   sob.window=new  to si-myedit-win
0 si-myedit-win sob=show
 
 
: default-name
  s" UnnamedRIPE.txt"
;

: cb-new
  default-name si-title  sob=title
  si-myedit-box   sob=empty
;

: write-to-file

 

  -1 0 DO
  
   i si-myedit-box sob@line  
   dup
   IF

      2dup sizeof(char) *  + sizeof(char) - @char  0x0d =
      IF
        1 - 
      THEN
 

      Eopfile  write-cline    If  ." Write-line error "   errno .dec cr exit   then
   ELSE
     drop
     LEAVE
   THEN
  LOOP
  
  Eopfile  close-file drop drop   
  
 
;

: cb-save
  default-name 
  fam=wo
  open-file 
  IF 
    drop
  ELSE
    to Eopfile 
    write-to-file
  THEN
;
  
: cb-save-as
  sys.savesel 
  dup 0= IF 2drop exit THEN

  fam=wo
  open-file drop to Eopfile 
  write-to-file
;

: cb-Execute

	app@cwd   	 
	s" \"    		join"
	default-name 		join"
   
 

   fam=wo
   s" , ccs=utf-8"  join"
 

   open-file 
   IF   drop  ." Could not open the temporary file "  errno .dec  cr  exit THEN
      
   to Eopfile 
   write-to-file

	s" s"
	[char] " char>" 	join"
	s"  "    		join"
	app@cwd   		join"
	s" \"    		join"
	default-name 		join"
	[char] "  char>"	join"
  
 	s"  included "    	join"

	spawn drop
;

: cb-exec-drop-file
   dnd.drop@count  1 = 
   IF  
   ELSE 
      ." Invalid number of Drag and Drop files" cr
      exit 
   THEN

   0 dnd.drop@data file@exists
   0=
   IF
      ." Drag and Drop specified file does not exist" cr      
      exit       
   THEN

	s" s"

	[char] " char>" 	join"
	s"  "    		join"
   0 dnd.drop@data    join"
	[char] "  char>"	join"
  
 	s"  included "    	join"

 	spawn drop
;

: read-to-box
  si-myedit-box   sob=empty

  BEGIN
    pad  256t Eopfile read-cline   drop   
    IF
      pad swap  
      0x0a char>"  join"  si-myedit-box sob.item=add drop 0
    ELSE
      drop  1
    THEN 
  UNTIL
  Eopfile  close-file drop
;

: open-read ( c-addr , u - )

  2dup si-title  sob=title
  fam=ro
  fam>cli
  open-file 
  IF
    drop
  ELSE
    to Eopfile  
    read-to-box
  THEN
;

: cb-open
  sys.filesel
  dup 0= IF 2drop exit THEN
  open-read 
;

: open-default
  default-name
  open-read 
;
 
: cb-edit-drop-file
   dnd.drop@count  1 = 
   IF  
   ELSE 
      ." Invalid number of Drag and Drop files" cr
      exit 
   THEN

   0 dnd.drop@data file@exists
   0=
   IF
      ." Drag and Drop specified file does not exist" cr      
      exit       
   THEN
   
   0 dnd.drop@data open-read 
;

: cb-exec-drop-file
   dnd.drop@count  1 = 
   IF  
   ELSE 
      ." Invalid number of Drag and Drop files" cr
      exit 
   THEN

   0 dnd.drop@data file@exists
   0=
   IF
      ." Drag and Drop specified file does not exist" cr      
      exit       
   THEN

	s" s"

	[char] " char>" 	join"
	s"  "    		join"
   0 dnd.drop@data    join"
	[char] "  char>"	join"
  
 	s"  included "    	join"

 	spawn drop
  0 dnd.drop@data open-read 
;

\ ==============================================
: cb-help1

   s" A simple text file browser with a limited edit and launch ability."
   crlf" join"
   date" join"

   BrowseAndLaunch"
   msg.box.ok  drop  
;
\ ==============================================

: cb-clear
  si-myedit-box   sob=empty
;

: cb-undo
  si-myedit-box   sob.edit=undo
;

: cb-reload
  si-myedit-win  sob@title
  open-read
;

\ ==============================================

   si-myedit-win   sob.menu.bar=new                         \ make a menu bar for the CLI screen

   dup s" File"  sob.menu.h=new                             \ add a horizontal menu

      dup  s" New"       ' cb-new      sob.menu.v=new drop  \ add a menu item with callback
      dup  s" Open"      ' cb-open     sob.menu.v=new drop  \ add a menu item with callback
      dup  s" Save"      ' cb-save     sob.menu.v=new drop  \ add a menu item with callback
      dup  s" Save As"   ' cb-save-as  sob.menu.v=new drop  \ add a menu item with callback
      dup  s" Exit"      ' bye         sob.menu.v=new drop  \ add a menu item with callback
      drop 

   dup s" Help" sob.menu.h=new                                           \ add a horizontal menu
      dup  s" help"           ' cb-help     sob.menu.v=new drop          \ add a menu item with callback
      dup  s" About"          ' cb-help1    sob.menu.v=new drop          \ add a menu item with callback
      dup  s" RipeTech"       '  about      sob.menu.v=new drop          \ add a menu item with callback
      drop  
         
   drop

\ ==============================================
   sob.con.col.w drop
   
      s"   "
      0
      sob.edit=new to si-title
      1 si-title sob=ro

      sob.con.row.w drop

         s" Exec dropped File"  0 sob.button.push=new    drop
               ' cb-exec-drop-file   
               15t   
               sob@last.id
               sob.callback=xt.drop 
        
         s" Edit dropped File"  0 sob.button.push=new    drop
               ' cb-edit-drop-file   
               15t   
               sob@last.id
               sob.callback=xt.drop 
         
         s" Reload"   ' cb-reload sob.button.push=new    drop
         s" Undo"     ' cb-undo sob.button.push=new    drop
         s" Launch"     ' cb-Execute sob.button.push=new    drop
         s" Clear"       ' cb-clear   sob.button.push=new    drop
         
      sob.con=close
      
   sob.con=close   

\ ==============================================

20t
pad 128t bl fill.char
pad 80t 
sob.edit.multi=new   to si-myedit-box

si-myedit-box   sob=empty

\ ==============================================

 
  open-default

  si-myedit-win  sob=shrink
 
\ ===================================================================================================
\ call hide-me  when the top right X on the GUI window is clicked
 
' bye     si-myedit-win       sob.callback=xt1

\ on error ENABLE the normal CLI window
0 on-error=xt

\ HIDE the window 
  0 si-myedit-win sob=show
 
\ ===================================================================================================
\ This is the end of the BROWSE and LAUNCH capability
[then]

hex
' sob.menu.v=new
[if]

   : cb-included 
     s" Text (*.txt)"  0x0 char>" join"  s" *.txt" join"
     0x0 char>" join" 0x0 char>" join" sys.filesel=filter
     sys.filesel 
     0 0  sys.filesel=filter
     dup IF included ELSE 2drop THEN
   ;

   : cb-editor 
  		  1 si-myedit-win sob=show
        0 sob@cli sob=show
   ;

   : cb-move-to-exe-dir  
      path@invocation sys.filesel=dir
   ;
 
   sob@cli   sob.menu.bar=new    \ make a menu bar for the CLI screen

   dup s" File" sob.menu.h=new                   \ add a horizontal menu

      dup  s" Included"              ' cb-included sob.menu.v=new drop   \ add a menu item with callback
     
      dup  s" Browse and Launch"     ' cb-editor sob.menu.v=new drop     \ add a menu item with callback
   
      dup  s" Move to .EXE dir"      '  cb-move-to-exe-dir  sob.menu.v=new drop           \ add a menu item with callback
      
      dup  s" Exit"       ' bye sob.menu.v=new drop           \ add a menu item with callback

      drop    

   : cb-depth-check   
     ." Depth " depth .dec cr 
   ;

   dup s" Tools" sob.menu.h=new                         \ add a horizontal menu
      dup  s" Depth Check"    ' cb-depth-check   sob.menu.v=new drop  \ add a menu item with callback
      drop           

   dup s" Help" sob.menu.h=new                          \ add a horizontal menu
   
      dup  s" Help"    ' cb-help   sob.menu.v=new drop  \ add a menu item with callback   
      dup  s" About"   '  about    sob.menu.v=new drop  \ add a menu item with callback
      drop           
      
   drop

[THEN]

'  cb-move-to-exe-dir  
[IF]
   cb-move-to-exe-dir
[then]

sob@gui   sob.con=open
