Old Listener interfaces


Welcome

What is gtkD
Screenshots
sourceforge Page
Contacts

Downloads

gtkD Windows
gtkD Linux

Developers

Tutorial
Diffs to GTK
Events
Class Hierarchy
Class Index
Class List
OpenGL

Links

Site pages
Links
Acknowledgments

Warning:
The event implementation will probably change to use delegates instead of listener interfaces
see the discussion at started at
Listeners interfaces or delegates?

Description

Any object can receive any event providing that it implements the correspondent listener and requests to receive it.
all the listener interfaces declarations are on the event.Dispatcher module
so we can have some thing like:

How to create a listener

First lets look at a listener interface declaration - All the listeners are defined on the module event.Dispatcher.
(I'm trying to convince doxygen to document event.Dispatcher but it's not easy)

//
// Single mouse click on widget that support mouse click.
// Examples are buttons and menu items.
//
public: interface ClickedListener
{
    // defines only one method to implement
    // this will be called on the implementing class when the clicked event is generated
    void clickedPerformed(ObjectG objectG, char [] action);
}

We need a class that listens to mouse clicked actions

class MyMenuListener : ClickedListener
{
    void clickedPerformed(ObjectG obj, char [] action)
    {
        switch ( action )
        {
            case "File.Exit":
                // do something here
            break;
            case "Help.About":
                // do something different here
            break;
            default:
                // oops : unknown action - do nothing maybe the user didn't notice
            break;
        }
    }
}

Instantiating the listener class

    MyMenuListener myMenuListener = new MyMenuListener;

creating the a menu item (later it has to be added to a menu)

    MenuItem item = new MenuItem("Exit");

get this application dispatcher;

    Dispatcher dispatcher = Dispatcher.getDispather()

request that myMenuListener receives event of type Clicked with action code "File.Exit"

    dispatcher.addClickedListener(myMenuListener,item,"File.Exit");

But gtkD can simplify your life, if you already have a menu object
then the last four steps can be replaced by:

    menu.append(new MenuItem(myMenuListener,"Exit","File.Exit"));

Complete example from a real gtkD class

Here is the example of MainWindow.
MainWindow is simply a Window that listens to window events.

note that the MainWindow will stop the main GUI event cycle with

GtkD.stop();    // effectively ends the GUI main event cycle closing all graphics elements

making the following statement irrelevant

return false;   // returning true would keep the window open (if the main event cycle is still running)


SourceForge.net Logo DSource.org Logo digitalmars.com Logo