Sign up

What is the right way to send data to a button activate callback?

In C I have:

	move = gtk_menu_item_new_with_label (name);
	g_signal_connect(G_OBJECT(move), "activate", G_CALLBACK(MoveToNotepadCB), np);

In D I have tried:

auto move = new MenuItem (name);
		Signals.connectData(view, "activate", &selectNotepad, &selectNotepad, null, ConnectFlags.SWAPPED);

Re: What is the right way to send data to a button activate callback?

(continued)
With the above D code, I get the error:

none of the overloads of connectData are callable using argument types
because

void delegate(Notepad np)

does not match

extern (C) void function() cHandler

connectData seems to be a low level function since most of the parameters are extern(C)

Should I subclass every button? I'd prefer to do what I do in C, set the callback and give it data (an object) to operate on.

Re: What is the right way to send data to a button activate callback?

I found a way:

auto view = new RadioMenuItem (group, name);
view.addOnActivate(delegate void(MenuItem foo) {this.selectNotepad();});

This is a bit awkward because view is a RadioMenuItem but since addOnActivate is defined in MenuItem, that's what the delegate parameter must be. Why doesn't inheritance make them equivalent?

I also saw an example that does:

view.addOnActivate(delegate void(_) {this.selectNotepad();});

It works, but I don't understand why.

Re: What is the right way to send data to a button activate callback?

On 23-12-2018 21:33, Chris Bare wrote:

I found a way:

auto view = new RadioMenuItem (group, name);
view.addOnActivate(delegate void(MenuItem foo) {this.selectNotepad();});

This is a bit awkward because view is a RadioMenuItem but since addOnActivate is defined in MenuItem, that's what the delegate parameter must be. Why doesn't inheritance make them equivalent?

Most of the time with GtkD you would use a delegate which has access to
it's context.
There is currently no connectData overload for D as there is for
connect.

I also saw an example that does:

view.addOnActivate(delegate void(_) {this.selectNotepad();});

It works, but I don't understand why.

It works because of type inference by the compiler, _ is the parameter
name and the compiler infers it's type from the definition of
addOnActivate. You can also remove the delegate void part.