-- a simple addressbook example, part 6, by Wolf/Jan./99
-- Feb.4, added 'speedy' e_entry with many new onKeyPress[] handlers.

include win32lib.ew
without warning

constant
  MyWin   = create(Window, "AddressBook 6", 0, 0, 0, 230, 400, 0),
  widget  = create(Group, "", MyWin, 5, 8, 214, 87, 0),
  Labelx  = create(CText, "", MyWin, 15, 10, 191, 20, 0),
  List1   = create(SortedList, "", MyWin, 11, 36, 200, 60, 0),    
  Label1  = create(LText, "", MyWin, 10, 100, 210, 18, 0),
  Label2  = create(RText, "Last:", MyWin, 10, 133, 65, 20, 0),
  Sle1    = create(EditText, "", MyWin, 80, 130, 130, 22, 0),
  Label3  = create(RText, "First:", MyWin, 10, 155, 65, 20, 0),
  Sle2    = create(EditText, "", MyWin, 80, 152, 130, 22, 0),
  Label4  = create(RText, "Address:", MyWin, 10, 177, 65, 20, 0),
  Sle3    = create(EditText, "", MyWin, 80, 174, 130, 22, 0),
  Label5  = create(RText, "Street:", MyWin, 10, 199, 65, 20, 0),
  Sle4    = create(EditText, "", MyWin, 80, 196, 130, 22, 0),
  Label6  = create(RText, "City:", MyWin, 10, 221, 65, 20, 0),
  Sle5    = create(EditText, "", MyWin, 80, 218, 130, 22, 0),
  Label7  = create(RText, "State:", MyWin, 10, 243, 65, 20, 0),
  Sle6    = create(EditText, "", MyWin, 80, 240, 80, 22, 0),
  Label8  = create(RText, "Zip:", MyWin, 10, 265, 65, 20, 0),
  Sle7    = create(EditText, "", MyWin, 80, 262, 80, 22, 0),
  Label9  = create(RText, "Phone:", MyWin, 10, 287, 65, 20, 0),
  Sle8    = create(EditText, "", MyWin, 80, 284, 130, 22, 0),

  MenuTop   = create(Menu, "&File", MyWin, 0, 0, 0, 0, 0),
  MenuSave  = create(MenuItem, "&Save", MenuTop, 0, 0, 0, 0, 0),
  MenuPrint = create(MenuItem,"&Print", 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),
  MenuThank = create(Menu, "Thanks", MyWin, 0, 0, 0, 0, 0),
  MenuTo    = create(MenuItem, "To..", MenuThank, 0, 0, 0, 0, 0),

  btnNew  = create(PushButton, "New", MyWin, 10, 322, 40, 25, 0),
  btnAdd  = create(PushButton, "Add", MyWin, 55, 322, 40, 25, 0),
  btnSave = create(PushButton, "Save", MyWin, 100, 322, 50, 25, 0),
  btnDel  = create(PushButton, "Delete", MyWin, 155, 322, 60, 25, 0),

-- give us a little bitmap picture.
  Bitmap1 = create( Bitmap, "", MyWin, 171, 246, 32, 32, 0 )
-- and a different font for 'item x of y' display
  setFont( Labelx, "Times New Roman", 12, 2 )

atom Handle
object line, line2, e_entry
integer  rgbColor, listlen, itemnum, index, junk, isnew, haschanged

  isnew=0  haschanged=0


-- from DaJaRo's threed.ew code,
-- draws a nice 3D frame.

procedure Threed(integer Win_id, sequence xys,sequence hilo,integer width )
  integer x1,y1,x2,y2
    x1=1
    y1=2
    x2=3
    y2=4
    setPenColor( Win_id, hilo[2] )
  for i = 0 to width do
    drawLine( Win_id, xys[x1]-i, xys[y1]-i, xys[x2]+i, xys[y1]-i )
    drawLine( Win_id, xys[x1]-i, xys[y1]-i, xys[x1]-i, xys[y2]+i )
  end for

  setPenColor( Win_id, hilo[1] )
  for i = 0 to width do
    drawLine( Win_id, xys[x1]-i, xys[y2]+i, xys[x2]+i, xys[y2]+i )
    drawLine( Win_id, xys[x2]+i, xys[y1]-i, xys[x2]+i, xys[y2]+i )
  end for
end procedure

procedure PaintWin_id(integer Win_id)
  sequence xys, hilo
  integer width
  integer x,y
    x=40
    y=28
    width  = 4
    xys    = {5,125,220-width,316-width}
    hilo   = { rgb(240,240,240),rgb(75,75,75) }
    Threed(Win_id, xys, hilo, width ) -- frame e_entry form
    width=2
    hilo   = { rgb(240,240,240),rgb(75,75,75) }
    xys    = {13,9,210-width,32-width}
    Threed(Win_id, xys, hilo, width ) -- frame text window
  setPenColor(MyWin,Black)
  drawLine(MyWin,0,1,228,1)           -- draw line below menu
  end procedure

procedure onPaint_MainWindow( integer self, integer event, sequence params )
  PaintWin_id(MyWin)
end procedure

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()
  isnew=1  -- set since we have new item
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.
  sequence Sstring
    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))
    --**
    junk=getIndex(List1)
    listlen = getCount(List1)
    line= sprintf("%d", junk)
    line2= sprintf("%d", listlen)
    Sstring="showing item " & line & " of " & line2
    setText(Labelx,Sstring)
  end procedure

procedure onLoad_MyWin( integer self, integer event, sequence params )
  PaintWin_id(MyWin)                  -- draw our fancy borders.
  setBitmap( Bitmap1, ".\\Bin\\race2.bmp" )   -- draw our bitmap.
-- 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.
  junk = getIndex(List1)
  if junk >= 1 then
    line=getItem(List1, 0)
    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)
        show_parsed() --**
      end if
    end for
  end if
  setFocus(List1)
end procedure

procedure onClick_btnAdd( integer self, integer event, sequence params )
  haschanged=1
  junk=getIndex(List1)
  if isnew=0 then
-- this deletes an old item so the newly edited item can be added.
    junk = deleteItem(List1, junk)
  end if 
  isnew=0
  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
  setFocus(btnNew)
end procedure

procedure onClick_btnSave( integer self, integer event, sequence params )
-- save our database to a file.
  haschanged=0
  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 )
  if getCount(List1) > 0 then  -- is list empty?
    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
        haschanged=1
        junk = deleteItem(List1, itemnum)
        listlen = getCount(List1)
        if listlen > 0 then
          if itemnum > 1 then
            setIndex(List1, itemnum-1)
            line=getItem(List1,itemnum-1)
          else
            line=getItem(List1,1)
            setIndex(List1,1)
          end if
        else
          line=", , , , , , , , "
          setText(Label1, {})
        end if
        show_parsed()
      end if          
  end if
  if listlen > 0 then
    setFocus(List1)
  else
    setFocus(Sle1)
  end if
end procedure

-- from pr123.exw in lesson Extra_02 --

include printws.ew

atom myphdc, j, k, charheight, width, hor, ver, column2
sequence mypr, textinfo

procedure print_setup(sequence filename)
  mypr=DefaultPrinterName()         -- get default printer name
  myphdc=GetPrinterDC(mypr)         -- create a print 'handle'
  textinfo=GetTextMetrics(myphdc)   -- get printer 'font' info
  charheight=textinfo[1]+1          -- character height+1
  width=textinfo[2]                 -- character width
  hor=GetDeviceCaps(myphdc,8)       -- page width
  column2=floor(hor/2)              -- location for our second column
  ver=GetDeviceCaps(myphdc,10)      -- page length
  j=StartDoc(myphdc, filename)      -- start document with 'dummy' filename
  j=StartPage(myphdc)               -- start a new page
end procedure

procedure end_print()
  j=EndPage(myphdc)                 -- eject page,
  j=EndDoc(myphdc)                  -- close 'file',
  DeleteDC(myphdc)                  -- and clean up.
end procedure

procedure print_data()
  k=charheight*4                    -- some empty space at the top
  listlen = getCount(List1)         -- how many lines are in it ?
  if listlen > 0 then               -- can't print an empty list.
    for l=1 to listlen by 2 do
      line=getItem(List1,l)
      show_parsed()                 -- fill our sle's for each item.
-- print the first column of the first line.
      line=getText(Sle2) & " " & getText(Sle1)
      pTextOut(myphdc,0,k,line )    -- send to print spooler

      if listlen > l then
-- print the second column of the first line.
        line=getItem(List1,l+1)     -- go to next data item
        show_parsed()
        line=getText(Sle2) & " " & getText(Sle1)
        pTextOut(myphdc,column2,k,line )
      end if

-- return to previous data item
-- and print the second line of the first column.
      line=getItem(List1,l)
      show_parsed()
      k=k+charheight                -- increment line count
      line=getText(Sle3)
      if length(line) > 0 then 
        line=getText(Sle3) & ", " & getText(Sle4)
      else line=getText(Sle4)       -- skip empty sle
      end if
      pTextOut(myphdc,0,k,line)

      if listlen > l then
-- print the second column of the second line.
        line=getItem(List1,l+1)
        show_parsed()
        line=getText(Sle3)
        if length(line) > 0 then 
          line=getText(Sle3) & ", " & getText(Sle4)
        else line=getText(Sle4)     -- skip empty sle
        end if
        pTextOut(myphdc,column2,k,line)
      end if

-- return to previous data item
-- and print the third line of the first column.
      line=getItem(List1,l)
      show_parsed()
      k=k+charheight
      line=getText(Sle5)
      if length(line) > 0 then
        line=getText(Sle5) & ", " & getText(Sle6) & "   " & getText(Sle7)
      else line=getText(Sle6) & "   " & getText(Sle7)
      end if
      pTextOut(myphdc,0,k,line)

      if listlen > l then
-- print the second column of the third line.
        line=getItem(List1,l+1)
        show_parsed()
        line=getText(Sle5)
        if length(line) > 0 then 
          line=getText(Sle5) & ", " & getText(Sle6) & "   " & getText(Sle7)
        else line=getText(Sle6) & "   " & getText(Sle7)  -- skip empty sle
        end if
        pTextOut(myphdc,column2,k,line)
      end if

      k=k+charheight*3              -- increment and skip 2 lines.
      if k > ver-charheight*6 then  -- close to bottom?
        j=EndPage(myphdc)           -- if so, start a new page.
        j=StartPage(myphdc)         -- next page
        k=charheight*4              -- reset line count
      end if
    end for
    line=getItem(List1,1)
    show_parsed()                   -- correct our display
  end if
end procedure

procedure onMenu_MenuPrint( integer self, integer event, sequence params )
  print_setup("mydata3.txt")
  print_data()
  end_print()    
end procedure

procedure onMenu_Exit( integer self, integer event, sequence params )
  if haschanged=1 then
    junk = message_box("You have made changes.\n" &
    "Would you like to save them?",
    "Confirm Saving CHANGES!",
    MB_ICONQUESTION + MB_YESNO + MB_TASKMODAL)
    if junk = IDYES then
      onClick_btnSave( self, event, params )
    end if
  end if
  haschanged=0
  closeWindow(MyWin)
end procedure

procedure onKey_btnNew( integer self, integer event, sequence params )
  integer key = params[1]
  if key=13 then onClick_btnNew( self, event, params ) else end if
end procedure
setHandler( btnNew, w32HKeyDown, routine_id("onKey_btnNew") )

procedure onKey_btnAdd( integer self, integer event, sequence params )
  integer key = params[1]
  if key=13 then
    onClick_btnAdd( self, event, params )
    setFocus(btnNew)
  else end if
end procedure
setHandler( btnAdd, w32HKeyDown, routine_id("onKey_btnAdd") )

procedure onKey_btnSave( integer self, integer event, sequence params )
  integer key = params[1]
  if key=13 then onClick_btnSave( self, event, params ) else end if
end procedure
setHandler( btnSave, w32HKeyDown, routine_id("onKey_btnSave") )

procedure onKey_btnDel( integer self, integer event, sequence params )
  integer key = params[1]
  if key=13 then onClick_btnDel( self, event, params ) else end if
end procedure
setHandler( btnDel, w32HKeyDown, routine_id("onKey_btnDel") )

procedure onMenu_MenuTo( integer self, integer event, sequence params )
  junk = message_box( " Thanks to Robert Craig for Euphoria,\n" &
                      " David Cuny for the win32lib 'wrapper',\n" &
                      " and DAJARO for the 'threed' routines.",
                      "to all... from Wolf",#2000)
end procedure

-- 'speedy' e_entry section
procedure onKey_Sle1( integer self, integer event, sequence params )
  integer key = params[1]
  if key=13 then setFocus(Sle2) else end if
end procedure
setHandler( Sle1, w32HKeyDown, routine_id("onKey_Sle1") )

procedure onKey_Sle2( integer self, integer event, sequence params )
  integer key = params[1]
  if key=13 then setFocus(Sle3) else end if
end procedure
setHandler( Sle2, w32HKeyDown, routine_id("onKey_Sle2") )

procedure onKey_Sle3( integer self, integer event, sequence params )
  integer key = params[1]
  if key=13 then setFocus(Sle4) else end if
end procedure
setHandler( Sle3, w32HKeyDown, routine_id("onKey_Sle3") )

procedure onKey_Sle4( integer self, integer event, sequence params )
  integer key = params[1]
  if key=13 then setFocus(Sle5) else end if
end procedure
setHandler( Sle4, w32HKeyDown, routine_id("onKey_Sle4") )

procedure onKey_Sle5( integer self, integer event, sequence params )
  integer key = params[1]
  if key=13 then setFocus(Sle6) else end if
end procedure
setHandler( Sle5, w32HKeyDown, routine_id("onKey_Sle5") )

procedure onKey_Sle6( integer self, integer event, sequence params )
  integer key = params[1]
  if key=13 then setFocus(Sle7) else end if
end procedure
setHandler( Sle6, w32HKeyDown, routine_id("onKey_Sle6") )

procedure onKey_Sle7( integer self, integer event, sequence params )
  integer key = params[1]
  if key=13 then setFocus(Sle8) else end if
end procedure
setHandler( Sle7, w32HKeyDown, routine_id("onKey_Sle7") )

procedure onKey_Sle8( integer self, integer event, sequence params )
  integer key = params[1]
  if key=13 then setFocus(btnAdd) else end if
end procedure
setHandler( Sle8, w32HKeyDown, routine_id("onKey_Sle8") )

procedure onDestroy_MyWin( integer self, integer event, sequence params )
  if haschanged=1 then
    junk = message_box("You have made changes.\n" &
                       "Would you like to save them?",
                       "ADDRESSBOOK6 data CHANGED !",
                       MB_ICONWARNING + MB_YESNO + MB_TASKMODAL)
    if junk = IDYES then
         onClick_btnSave( self, event, params )
    end if
  end if
end procedure

setHandler( MyWin, w32HPaint, routine_id("onPaint_MainWindow") )
setHandler( MenuPrint, w32HClick, routine_id("onMenu_MenuPrint") )
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") )
setHandler( MenuTo, w32HClick, routine_id("onMenu_MenuTo") )
setHandler( MyWin, w32HDestroy, routine_id("onDestroy_MyWin") )


WinMain(MyWin,Normal)
-- end --
