Re: NEWT :: selecting an item from a listbox
- Posted by K_D_R May 11, 2013
- 1794 views
Hi Ken,
Here you've a working example using pointers as you first tried: http://openeuphoria.org/pastey/216.wc
Of course, your solution is the correct one, but to prove you could store a pointer as data I got this example working.
The problem was that you allocated memory and got a pointer to it but that memory was set to autofree. As the pointer is "forgotten" by Euphoria and passed to Newt, Euphoria deallocates it as thinks it's not used anymore. So the key was to allocate the memory and not free it until the end.
By the way, I've just realized my code has a memory leak since I only free the selected's item memory. So be sure to check that if you're going to store pointers as data in the listboxes :)
Cheers,
Guillermo
PS: Relevant documentation: http://openeuphoria.org/docs/std_machine.html#_5596_allocate_data
Automatically - If the cleanup parameter is non-zero, then the memory is returned when the variable that receives the address goes out of scope and is not referenced by anything else.
Thanks for the info, Guillermo!
I have come up with this little listbox.e include file. It works very well
with my word/command auto-complete routine. Also, if you feed it the results of dir(*.*), you have a quick and dirty file selector.
-- -- listbox.e -- -- simple newt-euphoria list box demo -- -- 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 -- include std/machine.e -- if you use "pointers" integer h = 4 sequence list = {} sequence selection= " " procedure main() newtComponent form, listbox, buttonSubmit newt:Init() newt:Cls() newt:CenteredWindow( 40, -- width 15, -- height "Press Return to Select Item" -- title ) -- create a new list box listbox = newt:Listbox( 2, -- left margin 2, -- top margin h, -- height -- NEWT_FLAG_SCROLL -- flag NEWT_FLAG_RETURNEXIT -- flag ) -- newt:ListboxAppendEntry( listbox, "", 0 ) -- loop through the items for index = 1 to length(list) do -- add the item to the list box newt:ListboxAppendEntry( listbox, list[index], -- text index --<==== key, integer will now be returned ) -- instead of pointer assigned end for -- by application. buttonSubmit = newt:Button( 5, 10, "Submit" ) form = newt:Form( NULL, NULL, 0 ) newt:FormAddComponents( form, { listbox, buttonSubmit } ) --// 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 main() return selection end function