--select an item from the list by left-double-click, or ENTER key, or
--by closing the list thru the picturebutton.
--As written, this defaults to the '(setIndex)ed' list item.

include win32lib.ew
without warning

integer flag
flag=0

-- here's the data for our 'combo' dropdown.
sequence list
list={"Abc", "Easy", "as", "123",
 "This one is supposed to be extremely long and run out of the combobox.",
 "more", "and more", "and another", "take that!", "ok", "enough already"}

--all our windows,
-- with the SortedList() window longer than the EditText() window.
constant
Win      = create( Window, "Combo emulation", 0, 10, 10, 440, 190, 0 ),
edittext = create( EditText, "", Win, 5, 10, 160, 21, ES_READONLY ),
picture  = create( PictureButton, "", Win, 161, 10, 21, 21, 0 ),
yourlist = create( SortedList, "", Win, 6, 30, 420, 100, 0 ),
exitbtn  = create( PushButton, "Exit", Win, 40, 50, 100, 25, 0 ),
selected = create( LText, "selected = ", Win, 5, 130, 440, 20, 0 )

setBitmap(picture,".\\Bin\\arrow.bmp")

for i = 1 to length (list) do
  addItem (yourlist, list[i])
end for
setIndex (yourlist, 4)

integer sel
sequence line

procedure show_list( integer self, integer event, sequence params )
if flag=0 then
  setVisible(exitbtn,False)
  setVisible(yourlist,True)
  setFocus(yourlist)  flag=1
else
  setVisible(yourlist,False)
  setVisible(exitbtn,True)
  sel = getIndex(yourlist)
  if sel >= 1 then
    line = getItem(yourlist, sel)
    setText(edittext,line)
    setText(selected,"selected = " & line)
  end if
  setFocus(edittext)
  flag = 0
end if
end procedure

procedure show( integer self, integer event, sequence params )
  show_list( self, event, params )
end procedure

procedure proceed( integer self, integer event, sequence params )
  sel = getIndex(yourlist)
  if sel >= 1 then
    line = getItem(yourlist, sel)
    setText(edittext,line)
    setText(selected,"selected = " & line)
    flag = 1
    show_list( self, event, params )
  end if
end procedure

procedure update( integer self, integer event, sequence params )
  sel = getIndex(yourlist)
  if sel >= 1 then
    line = getItem(yourlist, sel)
    setText(edittext,line)
  end if
end procedure

procedure hide_list( integer self, integer event, sequence params )
  proceed( self, event, params )
  setVisible(yourlist,False)
end procedure


--procedure onchange_list(atom event,atom wParam,atom lParam) -- old param line
procedure onchange_list( integer self, integer event, sequence params )
-- A bit uncertain if this is ok
  integer wParam = params[1]
  event = params[1]  -- cheat old code
  
  if event = WM_KEYDOWN and wParam = VK_RETURN then proceed( self, event, params) end if
  if event = WM_LBUTTONDBLCLK then proceed( self, event, params ) end if
  
end procedure

procedure close_prog( integer self, integer event, sequence params )
  closeWindow(Win)
end procedure

setHandler( Win, w32HOpen, routine_id("hide_list") )
setHandler( picture, w32HClick, routine_id("show_list") )
setHandler( picture, w32HKeyDown, routine_id("show") )
setHandler( yourlist, w32HEvent, routine_id("onchange_list") )
setHandler( yourlist, w32HChange, routine_id("update") )
setHandler( exitbtn, w32HClick, routine_id("close_prog") )

WinMain(Win,Normal)