-- lists4.exw: by Ad Rienks
-- Added a delete routine, contributed by Ferlin Scarborough
-- with lot's of 'trickery', so put yer thinkin caps on!

include win32lib.ew

constant
  Win     = create(Window, "List ++++", 0, Default, Default, 160, 225, 0),
  List1   = create(List, "", Win, 10, 10, 130, 58, LBS_NOINTEGRALHEIGHT),    
  Label1  = create(LText, "", Win, 10, 75, 130, 20, 0),
  Sle1    = create(EditText, "", Win, 10, 96, 130, 20, 0),
  btnAdd  = create(PushButton, "Add", Win, 10, 126, 60, 25, 0),
  btnSave = create(PushButton, "Save", Win, 80, 126, 60, 25, 0),
  btnDel  = create(PushButton, "Delete", Win, 10, 156, 60, 25, 0)
  --  ^^ and the still newer Delete button.
    
atom Handle -- declare 2 variables we use for file handling.
object line

procedure onLoad_Win( integer self, integer event, sequence params )
-- from a file, this time, with a difference !    
-- Note that we use a completely different way to check for a file's
-- existence here. Since (Handle) becomes -1 if the file does not
-- exist, it will skip the 'read' entirely if this is true.
integer count
  Handle = open("mydata2.txt", "r")     -- open the file,
  if Handle != -1 then                  -- if file succesfully opened,
    while 1 do                          -- get each line,
        line = gets(Handle)
        if atom(line) then              -- until end of file.
            exit
        end if 
        line = line[1..length(line)-1]  -- remove line-feed we add below,
          if length(line)=12 then
            line = line & ' '
          end if
        addItem(List1, line)            -- and "park" it in the listbox.
    end while
    close(Handle)
  end if
  -- check if the list is not empty
  -- Note that, because of a new check here, we no longer need
  -- a 'dummy' line in the list.
  count = getCount(List1)                     -- get the lists length
  if count >= 0 then
      setIndex(List1, count)                  -- point to the last item.
      setText(Label1, getItem(List1, count))  -- show it also.
  end if
  setFocus(Sle1)
end procedure   -- onLoad_Win

procedure onChange_List1( integer self, integer event, sequence params )
-- list item will be shown in the static line.
integer what
    what = getIndex(List1)
    if what >= 0 then
        setText(Label1, getItem(List1, what))
    end if
end procedure   -- onChange_List1

procedure onClick_btnAdd( integer self, integer event, sequence params )
integer junk
sequence list_entry   list_entry = getText(Sle1)
  if length(list_entry) = 0 then
      junk = message_box("Type some text into the box first!",
                         "Empty list_entry!", MB_TASKMODAL + MB_ICONWARNING)
  else
      if length(list_entry) = 12 then    -- this is how we catch the bug,
          list_entry = list_entry & ' '  -- until a better solution is found.
      end if
      addItem(List1, list_entry)
      setText(Label1, list_entry)
      setText(Sle1, {})
      setIndex(List1, getCount(List1) )
  end if
  setFocus(Sle1)
end procedure   -- onClick_BtnAdd

procedure onClick_btnSave( 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 ?
  if listlen > 0 then                 -- no use to save an empty database.
    Handle = open("mydata2.txt", "w")
    for i = 1 to listlen do
      line = getItem(List1,i)
      if length(line) = 13 then
        if equal(line[ 13 ], ' ') then  -- if 13th char. is a space,
          line = line[1..12]            -- it's our 'fix', so remove it
        end if 
      end if
      line = line & '\n'        -- add a line-feed so we can read it, and
      puts(Handle, line)        -- write the line to the file.
    end for
    close(Handle)        
  end if
end procedure   -- onClick_btnSave

procedure onClick_btnDel( integer self, integer event, sequence params )
sequence lineitem
integer itemnum, junk, counter
  if getCount(List1) > 0 then  -- skip empty list
    itemnum = getIndex(List1)
    lineitem = getItem(List1, itemnum)
    junk = message_box("Are you sure you want to DELETE\n" & lineitem,
                       "Confirm Delete of Item!",
                       MB_ICONQUESTION + MB_YESNO + MB_TASKMODAL)
    if junk = IDYES then
        junk = deleteItem(List1, itemnum)
        counter = getCount(List1)
        if counter > 0 then
          setIndex(List1, counter)
            setText(Label1, getItem(List1, counter))
        else
            setText(Label1, {})
        end if
    end if
  end if
  setFocus(Sle1)
end procedure   -- onClick_btnDel

setHandler( Win, w32HOpen, routine_id( "onLoad_Win" ) )
setHandler( List1, w32HChange, routine_id("onChange_List") )
setHandler( btnAdd, w32HClick, routine_id("onClick_btnAdd") )
setHandler( btnSave, w32HClick, routine_id("onClick_btnSave") )
setHandler( btnDel, w32HClick, routine_id("onClick_btnDel") )
-- ^^ our latest event handler.

WinMain(Win,Normal)

-- end --
