Thread overview
Handling CheckBox state changes in DLangUI
Dec 31, 2022
Daren Scot Wilson
Dec 31, 2022
brianush1
Dec 31, 2022
Daren Scot Wilson
Jan 02, 2023
torhu
December 31, 2022

I'm writing a GUI program using dlangui. It has some checkboxes. I'm trying to figure out how to invoke a callback function when the user clicks the box. What are the valid ways of doing that?

I can copy from dlangide's source, where a delegate is defined in-line and assigned. That seems to work. But is that the only way?

    bool g_x = true;

    bool checkbox_b_clicked(Widget source, bool checked)
    {
      g_x = checked;
      if (checked) {
          writeln(checked);
      }
      return true;
    }


    auto check_a = new CheckBox("wantalt", "Alternating"d);
    auto check_b = new CheckBox("wantblinkb", "Blink(delg)"d);
    auto check_c = new CheckBox("wantblinkc", "Blink(direct)"d);

    check_a.checkChange = delegate(Widget w, bool checked) {
                 g_x=checked;
                 return true;
                 };
    check_b.checkChange = delegate(Widget w, bool checked) {
                 return checkbox_b_clicked(w,checked);
                 };
    check_c.checkChange = checkbox_b_clicked;
    check_c.checkChange = &checkbox_b_clicked;

The assignment to check_a is fine with the compiler.

For check_b, I try calling a function defined earlier. (Maybe in real life it's too complex to try having inline.) It was giving a compiler error until I realized I'm dumb, wasn't passing 'w' and 'checked' to it. Fixed, works fine now. Okay!

But what I think I should be able to do: assign checkbox_b_clicked directly to the .checkChange property of the checkbox, as shown for check_c. It doesn't work. Oh, I see an example where '&' is used - okay let's try that... nope!

The compiler errors I get are, for no '&' and with '&':

Error: function app.checkbox_b_clicked(Widget source, bool checked) is not callable using argument types ()

Error: none of the overloads of opAssign are callable using argument types (bool function(Widget source, bool checked))

December 31, 2022

On Saturday, 31 December 2022 at 02:40:49 UTC, Daren Scot Wilson wrote:

>

The compiler errors I get are, for no '&' and with '&':

Error: function app.checkbox_b_clicked(Widget source, bool checked) is not callable using argument types ()

Error: none of the overloads of opAssign are callable using argument types (bool function(Widget source, bool checked))

Try:

import std.functional : toDelegate;
check_c.checkChange = toDelegate(&checkbox_b_clicked);
December 31, 2022

On Saturday, 31 December 2022 at 03:05:45 UTC, brianush1 wrote:

>

On Saturday, 31 December 2022 at 02:40:49 UTC, Daren Scot Wilson wrote:

>

The compiler errors I get are, for no '&' and with '&':

Error: function app.checkbox_b_clicked(Widget source, bool checked) is not callable using argument types ()

Error: none of the overloads of opAssign are callable using argument types (bool function(Widget source, bool checked))

Try:

import std.functional : toDelegate;
check_c.checkChange = toDelegate(&checkbox_b_clicked);

That works :)

January 02, 2023

On Saturday, 31 December 2022 at 02:40:49 UTC, Daren Scot Wilson wrote:

>

The compiler errors I get are, for no '&' and with '&':

Error: function app.checkbox_b_clicked(Widget source, bool checked) is not callable using argument types ()

Error: none of the overloads of opAssign are callable using argument types (bool function(Widget source, bool checked))

If checkbox_b_clicked is a non-static nested function or non-static method, taking the address of it should result in a delegate, not a function pointer.

You can check what it is like this:

writeln(typeid(&checkbox_b_clicked));