Re: NEWT :: selecting an item from a listbox

new topic     » goto parent     » topic index » view thread      » older message » newer message
K_D_R said...

I have been having a rough time trying to "capture" an item selected from a newt listbox form.

Consider example6.ex: The value assigned to listboxState appears to be an arbitrarily assigned pointer. But how can that value be used to reference the element in the sequence to which it refers?

The documentation states that "Each entry in a listbox is a ordered pair of the text which should be displayed for that item and a key, which is a void * that uniquely identifies that listbox item. Many applications pass integers in as keys, but using arbitrary pointers makes many applications significantly easier to code." But how exactly does an application pass an integer in as a key? http://gnewt.sourceforge.net/tutorial-4.html#ss4.12

Any assistance will be deeply appreciated.

The "arbitrary pointer" referred to in the documentation is simply the result of a call to allocate(). So it would seem that you could poke() any value you'd like into this pointer and pass it into newtListboxAppendEntry(), then get it back with a call to newtListboxGetCurrent(). I'm just coding from memory, but this should work just fine...

-- create a simple list of things 
sequence fruits = { "Apples", "Bananas", "Oranges" } 
 
-- create a new list box 
atom listbox = newtListbox( 2, 2, 5, NULL ) 
 
-- loop through the items 
for index = 1 to length( fruits ) do 
 
    -- allocate some memory 
    atom ptr = allocate( 4, 1 ) -- 1 = automatically free 
 
    -- store the index of this item 
    poke4( ptr, index ) 
 
    -- add the item to the list box 
    newtListboxAppendEntry( listbox, fruits[index], ptr ) 
 
end for 
 
--// run the form here //-- 
 
-- get the selected "item" (our pointer above) 
atom ptr = newtListboxGetCurrent( listbox ) 
 
-- fetch the index from the pointer 
integer index = peek4s( ptr ) 
 
-- display the result 
printf( 1, "you selected: %s\n", {fruits[index]} ) 

-Greg

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu