-- a simple addressbook example, part 3, by Wolf/Jan./99

include win32lib.ew
without warning

constant
  MyWin   = create(Window, "AddressBook 3", 0, 0, 0, 230, 370, 0),
  List1   = create(SortedList, "", MyWin, 10, 10, 200, 60, 0),    
  Label1  = create(LText, "", MyWin, 10, 69, 210, 18, 0),
  Label2  = create(RText, "Last:", MyWin, 10, 92, 65, 20, 0),
  Sle1    = create(EditText, "", MyWin, 80, 89, 130, 22, 0),
  Label3  = create(RText, "First:", MyWin, 10, 117, 65, 20, 0),
  Sle2    = create(EditText, "", MyWin, 80, 114, 130, 22, 0),
  Label4  = create(RText, "Address:", MyWin, 10, 142, 65, 20, 0),
  Sle3    = create(EditText, "", MyWin, 80, 139, 130, 22, 0),
  Label5  = create(RText, "Street:", MyWin, 10, 167, 65, 20, 0),
  Sle4    = create(EditText, "", MyWin, 80, 164, 130, 22, 0),
  Label6  = create(RText, "City:", MyWin, 10, 192, 65, 20, 0),
  Sle5    = create(EditText, "", MyWin, 80, 189, 130, 22, 0),
  Label7  = create(RText, "State:", MyWin, 10, 217, 65, 20, 0),
  Sle6    = create(EditText, "", MyWin, 80, 214, 80, 22, 0),
  Label8  = create(RText, "Zip:", MyWin, 10, 242, 65, 20, 0),
  Sle7    = create(EditText, "", MyWin, 80, 239, 80, 22, 0),
  Label9  = create(RText, "Phone:", MyWin, 10, 267, 65, 20, 0),
  Sle8    = create(EditText, "", MyWin, 80, 264, 130, 22, 0),

  MenuTop   = create(Menu, "&File", MyWin, 0, 0, 0, 0, 0),
  MenuSave  = create(MenuItem, "&Save", MenuTop, 0, 0, 0, 0, 0),
  MenuExit  = create(MenuItem, "E&xit", MenuTop, 0, 0, 0, 0, 0),
  MenuDo    = create(Menu, "&Item", MyWin, 0, 0, 0, 0, 0),
  MenuNew   = create(MenuItem, "&New", MenuDo, 0, 0, 0, 0, 0),
  MenuAdd   = create(MenuItem, "&Add", MenuDo, 0, 0, 0, 0, 0),
  MenuDelete= create(MenuItem, "&Delete", MenuDo, 0, 0, 0, 0, 0),

  btnNew  = create(PushButton, "New", MyWin, 10, 290, 40, 25, 0),
  btnAdd  = create(PushButton, "Add", MyWin, 55, 290, 40, 25, 0),
  btnSave = create(PushButton, "Save", MyWin, 100, 290, 50, 25, 0),
  btnDel  = create(PushButton, "Delete", MyWin, 155, 290, 60, 25, 0)
    
-- set the background color
setWindowBackColor( MyWin, rgb(0,255,127) )

atom Handle -- declare variable we use for file handling.
integer listlen -- length of list
integer index -- index for parsing line
object line, line2, e_entry

procedure clear_all()
-- clear all the Sle's.
setText(Sle1,{})  setText(Sle2,{})
setText(Sle3,{})  setText(Sle4,{})
setText(Sle5,{})  setText(Sle6,{})
setText(Sle7,{})  setText(Sle8,{})
end procedure

procedure onClick_btnNew( integer self, integer event, sequence params )
setFocus(Sle1)
clear_all()
end procedure

function lTrim(sequence string)
  -- returns line segment up to comma/space combo.
  for i=1 to length(string) do
    if string[i] = ',' then
      if string[i+1] = ' ' then return string[1..i-1] end if
    end if
  end for
  return string
end function

procedure get_chunk()
index=length(line2)+3
if index < length(line) then
line=line[index..length(line)]
line2=lTrim(line)
else line2 = " "
end if
end procedure

procedure show_parsed()
-- removing the comma and space in the listbox line.
clear_all()
index=1
line2=lTrim(line)
setText(Sle1,line2)
get_chunk()
setText(Sle2,line2)
get_chunk()
setText(Sle3,line2)
get_chunk()
setText(Sle4,line2)
get_chunk()
setText(Sle5,line2)
get_chunk()
setText(Sle6,line2)
get_chunk()
setText(Sle7,line2)
get_chunk()
setText(Sle8,line2)
setText(Label1, getText(Sle2) & " " & getText(Sle1))
end procedure

procedure onLoad_MyWin( integer self, integer event, sequence params )
-- read from a file, IF it exists.    
  Handle = open("mydata3.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,
        addItem(List1, line)            -- and "park" it in the listbox.
    end while
    close(Handle)
  end if
  -- check if the list is not empty
  listlen = getCount(List1)               -- get the lists length
  if listlen > 0 then
      setIndex(List1, 1)                  -- point to the first item.
      line = getItem(List1,1)
      show_parsed()
  end if
  setFocus(List1)
end procedure

procedure onChange_List1( integer self, integer event, sequence params )
-- names will be shown in the static line in reverse order.
integer what
    what = getIndex(List1)
    if what >= 1 then
      line=getItem(List1, what)
      show_parsed()
    end if
end procedure

procedure re_index()
-- to point to the latest e_entry in the listbox.
  listlen = getCount(List1)         -- how many lines are in it ?
  if listlen > 0 then
      for i = 1 to listlen do
          line = getItem(List1,i)
            if compare(line,e_entry) = 0 then
              setIndex(List1, i)
            end if
      end for
  end if
setFocus(List1)
end procedure

procedure onClick_btnAdd( integer self, integer event, sequence params )
integer junk
  e_entry = getText(Sle1) & ", " & getText(Sle2) & ", " & getText(Sle3)
  e_entry=e_entry & ", " & getText(Sle4) & ", " & getText(Sle5) & ", "
  e_entry=e_entry & getText(Sle6) & ", " & getText(Sle7) & ", "
  e_entry=e_entry & getText(Sle8) & ", "
if length(e_entry) = 16 then
      junk = message_box("Type some text in first!",
                         "Empty e_entry!", MB_TASKMODAL + MB_ICONWARNING)
  else
      setFocus(List1)
      addItem(List1, e_entry)
      setText(Label1, getText(Sle2) & " " & getText(Sle1))
      re_index() -- routine to point to new item in sorted list
end if
end procedure

procedure onClick_btnSave( integer self, integer event, sequence params )
-- save our database to a file.
  listlen = getCount(List1)           -- first, how many lines are in it ?
  if listlen > 0 then                 -- no use to save an empty database.
      Handle = open("mydata3.txt", "w")
      for i = 1 to listlen do
          line = getItem(List1,i)
          line = line & '\n'          -- add a linefeed, so we can read it.
          puts(Handle, line)          -- write the line to the file.
      end for
      close(Handle)        
  end if
end procedure

procedure onClick_btnDel( integer self, integer event, sequence params )
integer itemnum, junk
  if getCount(List1) > 0 then  -- skip empty list
    itemnum = getIndex(List1)

    line = getItem(List1, itemnum)
    junk = message_box("Are you sure you want to DELETE\n" & line,
                       "Confirm Delete of Item!",
                       MB_ICONQUESTION + MB_YESNO + MB_TASKMODAL)
    if junk = IDYES then
      junk = deleteItem(List1, itemnum)
      listlen = getCount(List1)
      if listlen > 0 then
        if itemnum > 1 then
          setIndex(List1, itemnum-1)
        end if
      else
        setText(Label1, {})
      end if
      if listlen > 0 then
        if itemnum > 1 then
          line=getItem(List1,itemnum-1)
        else line=getItem(List1,1)
          setIndex(List1,1)
        end if
      end if
    end if
  setFocus(List1)
  show_parsed()
  end if
end procedure

procedure onMenu_Exit( integer self, integer event, sequence params )
    closeWindow(MyWin)
end procedure

setHandler( MyWin, w32HOpen, routine_id("onLoad_MyWin") )
setHandler( List1, w32HChange, routine_id("onChange_List1") )
setHandler( btnNew, w32HClick, routine_id("onClick_btnNew") )
setHandler( btnAdd, w32HClick, routine_id("onClick_btnAdd") )
setHandler( btnSave, w32HClick, routine_id("onClick_btnSave") )
setHandler( btnDel, w32HClick, routine_id("onClick_btnDel") )
setHandler( MenuExit, w32HClick, routine_id("onMenu_Exit") )
setHandler( MenuNew, w32HClick, routine_id("onClick_btnNew") )
setHandler( MenuAdd, w32HClick, routine_id("onClick_btnAdd") )
setHandler( MenuSave, w32HClick, routine_id("onClick_btnSave") )
setHandler( MenuDelete, w32HClick, routine_id("onClick_btnDel") )


WinMain(MyWin,Normal)

-- end --
