On 22-12-2018 13:34, Ron Tarrant wrote:
I've run into something that has me scratching my head. The following code, based on a number of similar examples, doesn't work. I'm starting to think I can only hook an event to a button if it's subclassed.
Can anyone set me straight?
......
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.
import gtk.MainWindow;
import gtk.Main;
import gtk.Button;
import gtk.Widget;
import gdk.Event;
import std.stdio;
import std.functional;
void main(string[] args)
{
Main.init(args);
MainWindow myAppWin = new MainWindow("Test Rig");
Button myButt = new Button();
myButt.setLabel("My Butt");
myButt.addOnButtonRelease(toDelegate(&quitApp));
myAppWin.add(myButt);
myAppWin.addOnDestroy(delegate void(Widget w) { quitApp(null, null); });
myAppWin.showAll();
Main.run();
} // main()
bool quitApp(Event event, Widget widget)
{
writeln("Quit, eh");
Main.quit();
return(true);
} // quitApp()