gtkdubuntu-14.04dmdgtkd

(dlang, GtkD) Using menubar


I copied the code in this site(https://sites.google.com/site/gtkdtutorial/#chapter2_2) and compiled it by dmd2.

import gtk.MainWindow;
import gtk.Box;
import gtk.Main;
import gtk.Menu;
import gtk.MenuBar;
import gtk.MenuItem;
import gtk.Widget;
import gdk.Event;

void main(string[] args)
{
    Main.init(args);
    MainWindow win = new MainWindow("MenuBar Example");
    win.setDefaultSize(250, 200);

    MenuBar menuBar = new MenuBar();  
    menuBar.append(new FileMenuItem());

    Box box = new Box(Orientation.VERTICAL, 10);
    box.packStart(menuBar, false, false, 0);

    win.add(box);
    win.showAll();
    Main.run();
}

class FileMenuItem : MenuItem
{
    Menu fileMenu;
    MenuItem exitMenuItem;

    this()
    {
        super("File");
        fileMenu = new Menu();

        exitMenuItem = new MenuItem("Exit");
        exitMenuItem.addOnButtonRelease(&exit);
        fileMenu.append(exitMenuItem);

        setSubmenu(fileMenu);
    }

    bool exit(Event event, Widget widget)
    {
        Main.quit();
        return true;
    }
}

The window was correctly shown but it doesn't die when I click [Exit] MenuItem. I'm confused. Any ideas?

Environment: Ubuntu 14.04 LTS


Solution

  • button-release-event (addOnButtonRelease() in GtkD) is the wrong signal to connect to for GtkMenuItem. It's a low-level GDK event; that is, an abstraction over the raw event produced by the window system when the user lets go of a mouse button. It's intended for custom event handling, like if you were using a GtkDrawingArea.

    Instead, you want the activate signal (addOnActivate() in GtkD).