0028 – Menus III - CheckMenuItems
Today we’ll cover another type of
CheckMenuItem. This post is longer than usual because after doing the simple version, I wanted to do a multiple
CheckMenuItem version as well and that led to a whole can of worms dumped on my desktop. Anyway, let’s get on with it…
For the most part, this is very much the same as other
MenuItems. It’s created by calling for a new
CheckMenuItem and it’s appended to a
Menu in the same way, but where it strays from the norm is:
- you need to decide the default state of the
CheckBox, is it checked or not, and
- the callback needs to handle both checked and unchecked states because it’s triggered by toggling the
CheckBoxand *not *just by setting it to an active state.
The beauty of this last point is that we can set up the callback to perform two different actions, one when the
CheckBox is activated, another when it’s deactivated. (A similar behaviour is inherited by the
RadioMenuItem as we’ll see in the next blog post.)
The simplest way to write the callback is to check the state of the
getActive() and decide what to do from there:
While digging into this widget, I found myself unable to resist doing a more complex version with multiple
CheckMenuItems, so here we go…
Multiple CheckMenuItems and Tracking Their States
Before we look at the
The ObservedFeaturesList Object
This example spotlights an imaginary object and the features controlled with the
CheckMenuItems. Here’s the code to define this imaginary object:
This is a different approach than we’ve taken before. This class keeps track of:
- the number of features,
- their names, and
- their states (on or off, signified by true or false)
features associative array is the only part of the code we need to fiddle with when deciding which features are turned on by default, another reason why using a generic approach to coding other classes makes sense.
Also, the key names are used as labels when creating the
CheckMenuItems, so there’s another thing we only have to deal with here in this one place.
ObservedFeaturesList object is instantiated in…
Back to the Top
Jumping back to the
TextRigWindow class, we see some changes to our usual code:
Because all of the
CheckMenuItems need access to the
ObservedFeaturesList and I also wanted to duplicate
MenuItem action when the user clicks the window’s Close button, I created the observed object here and passed it down through the hierarchy.
The FileMenu Class
Here, because the
CheckMenuItems are all so similar, we can get away grabbing the names (
itemKey) from within a
foreach loop to create the items:
Note that when using an associative array with a
foreach loop, we need to name a pair of iterators, a
key and a
value, as well as the associative array even though we don’t actually use
itemValue. Within the loop, we do the standard stuff:
- grab the item’s name from
- create the
- add it to the local array of
- append it to the
Once we’re done with those, it’s back to adding the
exitItem in the usual way.
One last bit to look at…
The FeatureCheckMenuItem Class
There’s a little more going on here than with other types of
MenuItem. Here’s the code:
Because the individual
CheckMenuItems differ only in label text, we can get away with this being a generic class and deal with the naming step up in the
FileMenu class we just looked at. The constructor gets access to the
ObservedFeaturesList object like everyone else.
After calling the super-class constructor and setting a couple of local variables, the callback is attached and then we come to:
As mentioned above in the section on The
ObservedFeaturesList object, this function defers the decision about the
CheckMenuItem’s state to the observed list object.
Next time, we’ll do pretty much the same thing except with
RadioMenuItems. However, the mutual-exclusivity of
RadioMenuItems demands a different approach in a few areas.
Until then, happy coding, happy hunting, happy birthday, or happy New Year, whatever’s your poison.
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