www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Handling CheckBox state changes in DLangUI

reply Daren Scot Wilson <darenw darenscotwilson.com> writes:
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))`
Dec 30 2022
next sibling parent reply brianush1 <brianush1 outlook.com> writes:
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);
Dec 30 2022
parent Daren Scot Wilson <darenw darenscotwilson.com> writes:
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 :)
Dec 30 2022
prev sibling parent torhu <torhu yahoo.com> writes:
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));
Jan 02 2023