linuxubuntu-14.04dgladegtkd

How To Show Hello World with Glade/GtkD and the D Programming Language


On Ubuntu Linux, I can use the Glade application to create a Hello World dialog. Now how do I get the D programming language to display it?


Solution

    1. Install the DMD compiler that compiles the D language on your Mac, Linux, or Windows computer. You can get more info about this here.
    2. Install the Glade interface designer on your Mac, Linux, or Windows computer. You can get Glade on Ubuntu Linux quite easy with sudo apt-get install glade, but more information about installing on the various other platforms is here.
    3. Install GTKd on your Mac, Linux, or Windows computer. This is not easy. You will need to start with the documentation at gtkd.org and then interact in the DLang.org Learn forum for more assistance if necessary.
    4. Open Glade and create a new window that has a label on it saying Hello World.

    Note that the tools palette in Glade shows a Window widget and an ApplicationWindow widget. Since we're not drawing any menus, ensure you're using the Window widget instead of the ApplicationWidget. If you fail to do that, you'll get warnings when running the application, talking about some missing menu calls.

           Save it as hello.glade.

    1. Open your hello.glade file in a text editor and look for a line that looks similar to this:

          <object class="GtkWindow" id="window1">

           Write down on a piece of paper that id attribute.

    1. Create a hello.d script in the same directory as this hello.glade file and alter the following contents, changing the window1 to whatever was your id you wrote down earlier.
    import gtk.Builder;
    import gtk.Main;
    import gtk.Widget;
    import gtk.Window;
    import std.stdio;
    
    int main (string[] args)
    {
        Main.init(args);
        Builder b = new Builder();
        b.addFromFile("hello.glade");
        Window w = cast(Window)b.getObject("window1");
        w.addOnHide( delegate void(Widget aux){ Main.quit(); } );
        w.showAll();
        Main.run();
        return 0;
    }
    
    1. Compiling is tricky. On Ubuntu Linux, I had to create a statement like the following. You may have to interact in the dlang.org Learn forums for your particular platform.
    # dmd hello.d `pkg-config --cflags --libs gtkd3`
    
    1. Once compiled, you can run your D executable to show the Hello World dialog. On Ubuntu Linux, I simply did:

           # ./hello

    1. Note that when you see the window and close it, you may receive some warnings on Linux. (On other platforms, this may vary.) I was receiving a warning about "Couldn't connect to accessibility bus - connection refused". The fix at least on Linux is to add this to your ~/.bashrc script at the bottom:

           export NO_AT_BRIDGE=1

           Now when you open the command prompt and run your compiled D command "hello" again,        it will not show that error.

           If you get errors regarding menus, then you used an ApplicationWindow widget instead of a        Window widget, and will need to switch that in Glade.

    Adding Buttons & Signals

    1. The way I do it is to click on a widget in Glade, click Signals, find the event I want to add, such as clicked, and then in the Handler column, type in a function. For example, on a button1, I would type onButton1Clicked. Save the file.

    2. Now, in your D source code, right after you create your Window object, add this code:

           b.connectSignals(null);

           ...where b is your Builder variable.

    1. In your D source code, add a function for this signal. For instance, I did:
    extern(C) void onButton1Clicked()
    {
        writeln("got here");
        Main.quit();
    }
    

           Note that in this case, the extern(C) is required.

    1. Recompile and run your application. You'll see that it automatically runs your new function for that button click.