0000: Introduction to GtkDcoding
Where All This is Coming From…
Ever since the heyday of 8-bit computers and Microsoft BASIC — which was available on almost every one of them — programmers have been pursuing the goal of cross-platform development… write once, compile for m/any. These days, there’s Java and Electron if you’re so inclined, but I prefer something that compiles to a native executable using a single programming language. When I found D, I realized I might just have found what I was looking for.
GtkD is a wrapper for GTK+, a GUI toolkit originally from the world of Linux, an almost-POSIX-compliant OS, but now available across a number of operating systems. That pretty much makes it perfect for my intentions. By learning how to use a single language compiler (dmd2) I can build and run applications on Windows, Linux, Mac, and FreeBSD. With GTK+ thrown in, we’re looking at a single-code-base solution to cross-platform development.
I’ve been writing code in more than a dozen languages since 1985 and I’ve also worked with GTK+ in some of those languages, but I’m coming into this as a D rookie. This blog is my exploration of D and GtkD as I work out how to code various widgets and build a thorough understanding of it all.
The third language I learned back in the day was C and I fell in love with its power and elegance. Along the way, I also grew fond of curly braces—although I can’t say why. D, being a descendant of C, drives right up my street and since D incorporates a handful of paradigms, it’s even more appealing:
- parallel, and
- concurrent, but…
I’m a sucker for object-oriented program development, so I’ll be concentrating on that for the most part. I’ll start with a few imperative examples, but then it’ll be OOP all the way.
D and GtkD both lend themselves well to an object-oriented approach to coding. The imperative examples will get you heading in that direction if it’s where you want to go, but I much prefer a full-on OOP approach.
Widgets: For the sake of clarity, each example will demonstrate just one widget unless it’s something that needs extra widgets to work.
All code is formatted in this way:
- import statements are at the top of the file,
- all curly braces, opening and closing, have their own line,
- except where delegates are defined,
- lots of white space to make things easy to read,
- variables are declared at the tops of functions or classes, and
- comments point out code not covered in previous examples.
What You’ll Need
First, a D compiler. I’m using dmd, the official reference compiler throughout. You can find it on the Downloads page of the dlang.org web site.
Second, you’ll want the latest GTK+ runtime. This as well as the GtkD.zip file can be downloaded from the official GtkD website (maintained by Mike Wey).
For now, I’m working on Windows and that’ll be reflected herein as well, although I’ve done a bit of preliminary work with FreeBSD, so I suspect all these examples will compile without trouble on any OS where D, GtkD, and a GTK+ runtime are available. Once I’m reoriented to the POSIX world, I’ll make sure everything works there, too. If you find any situation where you can’t compile and run these examples, please let me know.
Installation of the Development Environment
Install the reference compiler and the GTK+ runtime. Best to accept the default locations:
- The GTK+ runtime will install in
C:\Program Files\gtk-runtime, and
- The D compiler will install in
C:\Dalthough most of the action will be in
So go ahead and get those installed and I’ll wait here…
There are two more things we need to do before we can get down to it. Open the directory (folder, in Windows speak) where the
dmd binary lives. If you accepted the defaults, it’ll be in:
Find the file:
and open it in a text editor.
Look for the
DFLAGS variable and:
- type a space at the end of the line,
copy and paste (or type) this after the space, including the quotes:
Now you need to copy the GtkD wrapper files to where the compiler can find them:
- unzip GtkD-3.9.zip (the version numbers may differ by the time you read this, but that’s fine),
gtkd-3.9\generateddirectory, copy the
Thirdly, you’ll build and install the GtkD library…
Open a command prompt and make your way to where you unzipped
GtkD-3.9 (you’re in the right place if typing
dir shows you a file named
Build.d), and build the library with the command:
rdmd –m64 Build.d
unless you’re on a 32-bit OS, then use:
without the 64-bit flag,
Two .lib files will appear right there in the top level directory. Copy them to:
for a 32-bit OS, it’ll be:
And that should be that. You should be ready to dance the D-dance.
Base Test Rig
For now, you can copy this code (I highly suggest you type it out unless you’ve got an eidetic memory) and look for the compile instructions below:
Save this as
test_rig_imperative.d (or whatever you want, really) and compile it thusly:
dmd –de –w –m64 –Lgtkd.lib test_rig_imperative.d
After correcting typos so the compiler finishes, type the name without an extension (or you can include the .exe extension if you’re on Windows) to run it.
And that’s it for now. Next time, we’ll break down the test rig code so you know what’s going on in there. Then I’ll show you the same functionality in an OOP version of the test rig.
Until then, happy D-coding and may the widgets be with you.
If you'd like to leave a comment...
Until I get time to research and install a commenting system, I ask that you try one of these options:
- come on over to the D Language Forum and look for one of the gtkDcoding announcement posts,
- drop by the gtkD Forum,
- follow the link below to email me, or
- go to the gtkDcoding Facebook page.
You can also subscribe via RSS so you won't miss anything.
© Copyright 2019 Ron Tarrant