Search code examples
dtk-toolkit

How do I poll a set of RadioButton widgets in tkd (Tinker for the D language)


I've made a block of RadioButton widgets for my tkd interface, which work exactly as expected. However, the documentation for tkd's RadioButton doesn't show how one determines which of the buttons is pressed. Instead, the example code simply shows how to create an array of buttons that are linked together (i.e. so that only one can be selected at a time)

// The radio button group parent.
auto frame = new Frame()
    .pack();    
auto option1 = new RadioButton(frame, "Foo")
    .setSelectedValue("foo")
    .select()
    .pack();    
auto option2 = new RadioButton(frame, "Bar")
    .setSelectedValue("bar")
    .pack();

The CheckButton widget is a similar, and can be polled withe its associated isChecked() method, but nothing similar seems to be present for the RadioButton.

How do I check whether a given RadioButton has been selected?

Additionally, is there an easy way to check which member of an array of RadioButtons is selected, without iterating over the entire set myself?


Solution

  • After reading more documentation, it appears tkd implements this feature with a series of mixins.

    The RadioButton class implements the Value interface described here.

    Calling any of the associated methods will allow access to the state variable that is shared by the entire button array. E.g.

    option1.getValue(); // Returns "Foo" if option 1 is selected, "Bar" if option 2 is.
    option2.getValue(); // Same as above, since the value is shared.
    option2.setValue("bar"); //Change value, without changing the selected button.