gen2brain / iup-go

Cross-platform UI library with native controls

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Proper FLAT_ACTION callback for FlatList? Segfault using ListActionFunc

justinfx opened this issue · comments

I'm getting a segfault when I try to wire up a FlatList with the "FLAT_ACTION" and ListActionFunc. But if I switch to a regular List and use ACTION, it works correctly. The iup docs seems to indicate that the only difference is the callback name. What would be the correct way to apply this callback to FlatList?

You should use FlatActionFunc with "FLAT_ACTION".

Thanks for the fast reply!
The thing that threw me and made me not choose FlatActionFunc is that its signature is:

Go

func(ih Ihandle) int

C

int function (Ihandle *ih, char *text, int item, int state); [in C]

So is it expected that I need to query the selection state after receiving the handle?

Ah, sorry, you are right, you need ListActionFunc actually.

No worries. So that is my original issue. If I use FlatList with "FLAT_ACTION" and a ListActionFunc, I get a segfault.

What happens with regular List and ListActionFunc on "ACTION"? Can you provide a simple example so I can test it?

I think I understand, FLAT_ACTION should have a similar switch as ACTION here https://github.com/gen2brain/iup-go/blob/main/iup/bind_events.go#L127, but I need to check first where else is FLAT_ACTION used.

Ok, can you try now, should be fixed in 1e1aa5c? FlatActionFunc is added by mistake actually, but let it be for now.

It does work now with FlatList + FLAT_ACTION + FlatActionFunc.
However, with FlatActionFunc being the wrong signature and only handling the Ihandle arg, its not really easy to check the state of the action and know if it was because of selection or deselection, so you end up with multiple calls.

Did you try with ListActionFunc? That one should be used.

Yes I did. It still segfaults, here:

goText := C.GoString((*C.char)(text))

Fixed in 3af2161. I tried with the example below:

package main

import (
	"github.com/gen2brain/iup-go/iup"
)

func main() {
	iup.Open()
	defer iup.Close()

	list1 := iup.List()
	iup.SetAttribute(list1, "VALUE", "1")
	iup.SetAttribute(list1, "1", "Item 1 Text")
	iup.SetAttribute(list1, "2", "Item 2 Text")
	iup.SetAttribute(list1, "3", "Item 3 Text")
	iup.SetAttribute(list1, "TIP", "List 1")

	fr1 := iup.Frame(
		iup.Vbox(
			list1,
		),
	).SetAttribute("TITLE", "List")

	list2 := iup.FlatList()
	iup.SetAttribute(list2, "VALUE", "1")
	iup.SetAttribute(list2, "1", "Item 1 Text")
	iup.SetAttribute(list2, "2", "Item 2 Text")
	iup.SetAttribute(list2, "3", "Item 3 Text")
	iup.SetAttribute(list2, "TIP", "List 1")

	fr2 := iup.Frame(
		iup.Vbox(
			list2,
		),
	).SetAttribute("TITLE", "FlatList")

	vbox := iup.Vbox(
		fr1,
		fr2,
	)

	dlg := iup.Dialog(vbox).SetAttributes(`TITLE="List"`)

	iup.SetCallback(list1, "ACTION", iup.ListActionFunc(listCb))
	iup.SetCallback(list2, "FLAT_ACTION", iup.FlatListActionFunc(flatListCb))

	iup.Show(dlg)
	iup.MainLoop()
}

func listCb(ih iup.Ihandle, text string, item, state int) int {
	println("ACTION", text, item, state)
	return iup.DEFAULT
}

func flatListCb(ih iup.Ihandle, text string, item, state int) int {
	println("FLAT_ACTION", text, item, state)
	return iup.DEFAULT
}

Thanks for the fixes!