-- ...then added a button and an SLE to "fill" the listbox.
-- Now MOVE and RE_SIZE the silly button to add another,
-- just to add a new feature.

include win32lib.ew

constant
    Win   = create( Window, "List +++", 0, Default, Default, 160, 190, 0 ),
    List1 = create( List, "", Win, 10, 10, 120, 58, LBS_NOINTEGRALHEIGHT ),    
    Label1 = create(RText, "", Win, 10, 75, 120, 20, 0),
    Sle1    = create( EditText, "", Win, 10, 96, 110, 20, 0 ),
    Button1 = create( PushButton, "ADD", Win, 10, 126, 60, 20, 0 ),
    Button2 = create( PushButton, "SAVE", Win, 75, 126, 60, 20, 0 )
--  ^^ here's our new SAVE button.

atom Handle -- declare 2 variables we use for file handling.
object line

-- the following 2 lines check to see if we have a file,
-- and creates that file if it does not exist,
-- or your program will crash when it tries to "read" from nothing !

Handle=open("mydata.txt","a") -- mydata.txt is our "human" readable file.
close(Handle)

procedure onLoad_Win( integer self, integer event, sequence params )
  -- add items to the listbox, from a file, this time !
  
  addItem(List1, " " ) -- better do this, or setIndex()
                       -- crashes with an empty list.
  Handle=open("mydata.txt","r")      -- open the file,
  while 1 do                         -- and get each line,
    line=gets(Handle)
      if atom(line) then exit end if -- until end of file.
    line=line[1..length(line)-1]     -- remove the line-feed we add below,
    addItem( List1, line )           -- and "park" it in the listbox.
  end while
  close( Handle )
  setIndex(List1,getCount(List1))    --point to the last item.
  setFocus(Sle1)
end procedure

procedure show_it(integer here)
sequence this
this=getItem(List1,here)
setText(Label1, this)
end procedure

procedure onChange_List1( integer self, integer event, sequence params )
atom where
where=getIndex(List1)
show_it(where)
end procedure

procedure onClick_Button1( integer self, integer event, sequence params )
	sequence list_entry   list_entry = getText( Sle1 )
	
	if length(list_entry) > 0 then addItem( List1, list_entry ) end if
	setText(Sle1, {} )
	setFocus(Sle1)
	
end procedure

procedure onClick_Button2( integer self, integer event, sequence params )
-- here's our new feature to save our simple database to a file.
  integer listlen
  listlen = getCount( List1 )          -- first, how many lines are in it ?
  Handle = open( "mydata.txt","w")
  for i = 2 to listlen do              -- note we don't save
                                       -- the first 'dummy' line.
    line = getItem( List1, i ) & {10}  -- add a linefeed, so we can read it.
    puts( Handle,line )                -- write the line to the file.
  end for
  close( Handle )  -- it's very important to close anything we've opened.
end procedure

setHandler( Win, w32HOpen, routine_id( "onLoad_Win" ) )
setHandler( List1, w32HChange, routine_id("onChange_List") )
setHandler( Button1, w32HClick, routine_id("onClick_Button1") )
setHandler( Button2, w32HClick, routine_id("onClick_Button2") )
-- ^^ our latest event handler.

WinMain( Win,Normal )

-- end --
