Re: NEWT :: selecting an item from a listbox
- Posted by K_D_R May 12, 2013
- 1702 views
Here I have attempted to make a bit more generic eunewt listbox include file.
-- -- listbox.e -- Kenneth Rhodes -- wolfmanjacques@gmail.com -- simple newt-euphoria list box demo -- -- Newt for Euphoria by Greg Haberek -- https://bitbucket.org/ghaberek/eunewt/downloads/eunewt.zip -- -- http://gnewt.sourceforge.net/tutorial.html#toc1 -- -- Demonstrates how to pass an integer "key" (sequence index) to the -- newt application which may be used to access a list box selection, -- instead of allowing the application to set an arbitrary pointer -- to the value selected. -- -- Comments illustrate the alternate method of using pointers. include edx/newt.e sequence list = {} sequence selection= " " integer centered_window_width, centered_window_height, listbox_left_margin, listbox_top_margin, listbox_height, button_left_margin, button_top_margin, ll centered_window_height = 18 centered_window_width = 45 listbox_height = 10 listbox_left_margin = 2 listbox_top_margin = 2 button_left_margin = 5 button_top_margin = centered_window_height -3 procedure main() newtComponent form, listbox, buttonSubmit -- not needed, if NEWT_FLAG_RETURNEXIT flag is used newt:Init() newt:Cls() newt:CenteredWindow( centered_window_width, centered_window_height, "Arrow up/down/right/Enter to Submit" ) -- create a new list box listbox = newt:Listbox( listbox_left_margin, listbox_top_margin, listbox_height, NEWT_FLAG_SCROLL -- NEWT_FLAG_RETURNEXIT -- does not require submit button, scrolling slider -- is not shown ) -- loop through the items in the list sequence -- add the items to the list box, assigning the -- items sequence index as the "key", which would -- by default be a pointer automatically assigned by -- the application for index = 1 to length(list) do newt:ListboxAppendEntry( listbox, -- newt component list[index], -- text item index --<==== key, an integer, the sequence item index -- will now be returned, instead of a -- pointer assigned by the application ) end for -- not needed if NEWT_FLAG_RETURNEXIT flag is used buttonSubmit = newt:Button( button_left_margin, button_top_margin, "Submit" ) form = newt:Form( NULL, NULL, 0 ) newt:FormAddComponents( form, { listbox, buttonSubmit } ) -- newt:FormAddComponents( form, { listbox}) -- if NEWT_FLAG_RETURNEXIT flag is used --// run the form here //-- newt:FormRun(form) -- get the selected "item" atom index = newt:ListboxGetCurrent( listbox ) newt:FormDestroy(form) newt:Finished() selection = sprintf("%s", {list[index]}) end procedure public function listbox( sequence items ) list = items ll = length(list) for i = 1 to ll do if length(list[i])>centered_window_width then centered_window_width = length(list[i]) + 6 end if end for main() return selection end function