0004 - OOP Buttons, with and without Arguments
NB: In any given example file, to find the code that’s new since the previous example, look for a comment like this:
I’ve defined two classes in this example. The first one,
TestRigWindow, you’ve already seen in the OOP version of the base code file. The second one, we’ll talk about in a moment.
General Principles of OOP
Not all of them; that would be far beyond the scope of this blog post (or the blog in its entirety) so I’ll just say this…
OOP is about code organization. It’s about cutting back on bugs or at least having a better idea of where to look for them. No system of this kind is perfect, but that’s life. A class is meant to encapsulate specific data as well as any functions that act upon that data. The point is to protect the data from outside influences that might screw things up.
With that in mind, let’s look at the new class.
The MyButton Class
And here it is:
MyButton isn’t the name I’d use for production. It would be more descriptive of the data/actions involved, but for this example, it’ll do.
Since we don’t have any data, we only need to encapsulate actions and a constructor. All the constructor does is call its parent class while passing along its label text, and then hook up the signal we’re going to track for this button.
The other function,
buttonAction() stands in for whatever function might work on whatever data we might use… if we had data. All it does is write a line to the shell so we know it’s there and working.
But if We Pass an Argument…
Here’s the second version of this class:
You’ll notice the signal hook-up is different this time around. Even though we’re in scope, we need to use a delegate so we can pass an argument along to
buttonAction(). Also note that there’s another way of phrasing this hook-up and it’s commented out so as not to confuse things. But either way will work. The second way uses an underscore as a placeholder of sorts. It’s effectively saying: pass the originator along as a first argument and the compiler is smart enough to figure out that, in this case, the originator is a GTK
And everything else should be obvious. (If not, email me.)
And that’s it for this time. Next time we’ll look at how to open a window of a specific size. 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