what does id, lastId do int functions(Bind, Connect)

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
language1446
In need of some credit
In need of some credit
Posts: 2
Joined: Wed Sep 30, 2020 1:33 pm

what does id, lastId do int functions(Bind, Connect)

Post by language1446 » Wed Sep 30, 2020 2:02 pm

Hi. I'm new to wxWdigets and trying to understand Bind(), Connect()
problem i have now is I don't understand what id and lastId is for.

id The first ID of the identifier range to be associated with the event handler.
lastId The last ID of the identifier range to be associated with the event handler.

My understating is that any objects whose ID is between id ~ lastId is affected by the event_type ?
for example if there is Bind() like below,
Bind(wxEVT_COMMAND_BUTTON_CLICKED, &Button::OnQuit, MY_ID, wxID_EXIT);
then any objects ID between MY_ID ~ wxID_EXIT is affected by wxEVT_COMMAND_BUTTON_CLICKED.
But I guess this is not right.

So can someone please explain how id and lastId is used??
and one more, even this is an address, how can it be used as id?

thanks.

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2676
Joined: Sun Jan 03, 2010 5:45 pm

Re: what does id, lastId do int functions(Bind, Connect)

Post by PB » Wed Sep 30, 2020 3:32 pm

As you correctly deduced, it means that the event handler will handle all events with a given event type where event id value is between id and lastId (inclusive).
I.e.,

Code: Select all

Bind(wxEVT_BUTTON, &MyDialog::OnButton, this, 10000, 20000);
would be called for a button click for any button in MyDialog with an id between 10,000 and 20,000. Having something like this is sometimes useful so that you do not have to create many separate event handlers when they are not actually needed and the task can be done in a common event handler based on the event id (e.g., calculator buttons with numbers where they all need to just insert their number into an edit control).

Here is a simple compilable and fully working example

Code: Select all

#include <wx/wx.h>

class MyDialog : public wxDialog
{
public:
    MyDialog() : wxDialog(nullptr, wxID_ANY, "Test")
    {
        static const int mainSizerColumnCount = 5;
        auto mainSizer = new wxGridSizer(mainSizerColumnCount);

        for ( int i = 0; i < ButtonIdCount; ++i )
            mainSizer->Add(new wxButton(this, ButtonIdFirst + i, wxString::Format("Button %d", i)));

        Bind(wxEVT_BUTTON, &MyDialog::OnButton, this, ButtonIdFirst, ButtonIdFirst + ButtonIdCount);

        SetSizerAndFit(mainSizer);
    }
private:
    static const int ButtonIdFirst = wxID_HIGHEST + 1;
    static const int ButtonIdCount = 50;

    void OnButton(wxCommandEvent& evt)
    {
        wxLogMessage("%d", evt.GetId() - ButtonIdFirst);
    }
};

class MyApp : public wxApp
{
public:
    bool OnInit() override
    {
        MyDialog().ShowModal();
        return false;
    }

}; wxIMPLEMENT_APP(MyApp);

Post Reply