On Sun, 23 Dec 2018 13:09:39 +0100, Mike Wey wrote:
On 22-12-2018 13:34, Ron Tarrant wrote:
The main issue is that the quitApp function is a static function so
taking it's address will return a function pointer and not a delegate.There are a few ways to go about fixing that:
- use std.functional.toDelegate to turn it into a delegate.
- Put the function inside main as a nested function, it will be a
delegate in that case.
- Wrap the call in a anonymous delegate as with addOnDestroy.
- Use gobject.Signals.connect which also accepts function pointers.
There were two smaller issues with the example:
- The return type of quitApp should be bool.
- Missing std.stdio import.
Thanks for all that, Mike. After posting my question, I kept on digging and figured out the difference between the two types of callback. I think I'm now well on my way toward understanding this stuff:
import std.stdio;
import gtk.MainWindow;
import gtk.Main;
import gtk.Widget;
import gtk.Layout;
import gtk.Button;
import gdk.Event;
void main(string[] args)
{
Main.init(args);
TestRigWindow myTestRig = new TestRigWindow("Test Rig");
myTestRig.showAll();
Main.run();
} // main()
class TestRigWindow : MainWindow
{
this(string title)
{
// window
super(title);
addOnDestroy(delegate void(Widget w) { quitApp(); } );
auto myButton = new MyButt("Button Name");
auto myOtherButton = new MyOtherButt("Other Button Name");
// layout
auto myLayout = new MyLayout(myButton, myOtherButton);
add(myLayout);
} // this() CONSTRUCTOR
void quitApp()
{
writeln("Bye.");
Main.quit();
} // quitApp()
} // class myAppWindow
class MyLayout : Layout
{
this(MyButt myButton, MyOtherButt otherButton)
{
super(null, null);
put(myButton, 10, 20);
put(otherButton, 10, 60);
} // this()
} // class MyLayout
class MyButt : Button
{
this(string labelText)
{
super(labelText);
addOnButtonRelease(&doSomething);
} // this()
bool doSomething(Event e, Widget w)
{
writeln("Something was done.");
return(true);
} // doSomething()
} // class MyButt
class MyOtherButt : Button
{
this(string labelText)
{
super(labelText);
string message = "Something other than that was done.";
addOnClicked(delegate void(_) { doSomething(message); } );
} // this()
void doSomething(string messageText)
{
writeln(messageText);
} // doSomething()
} // class MyButt