cnjinhao / nana

a modern C++ GUI library
https://nana.acemind.cn
Boost Software License 1.0
2.29k stars 331 forks source link

attaching tags to widgets to distinguish widgets in event handlers #668

Open SourceOfDeath opened 1 year ago

SourceOfDeath commented 1 year ago

let's say widgets are stored in a container like that: vector<vector< unique_ptr<textbox> >> _someContainer; registering an event handler: _someContainer[var1][var2]->events().text_changed([](const nana::arg_textbox& texev)->void { //I can get a widget self texev.widget......; //but how can I get the indexes var1 and var2? })

In Delphi VCL (and Lazarus LCL) all widgets has .tag field of type integer. Does NANA have a mechanism of a similar purpose? Maybe use the tooltip string to store special information? Inherit from the desired class and add your own fields? I don't want to look through the entire container and compare pointers to find the desired widget. It looks silly. It is not advisable to perform cumbersome calculations in a frequently called handler. Does NANA have ready tools?

ashbob999 commented 1 year ago

You could just add _someContainer, var1and var2 to the capture group of the lambda.

vector<vector< unique_ptr<textbox> >> _someContainer;
_someContainer[var1][var2]->events().text_changed([&_someContainer, var1, var2](const nana::arg_textbox& texev)->void {
    // now you have access to _someContainer, var1 and var2
    const auto& wdg = _someContainer[var1][var2];
});