Search code examples
c++wxwidgets

Move assignment operator for class with dynamic bindings


I want to implement move assignment operator for my class, which will be also moving all my dynamic bindings. I works well, until I try to inherit from wxEvtHandler. I played a lot of time with it and can't find the solution when it will be working well with inheritance from wxEvtHandler too. Please, help.

UI class:

class FrameUi : public wxFrame
{
public:
    FrameUi(wxWindow* parent)
        : wxFrame(parent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize)
    {
        m_panel = new wxPanel(this);

        wxBoxSizer* mainS = new wxBoxSizer(wxVERTICAL);
        m_button = new wxButton(m_panel, wxID_ANY);
        mainS->Add(m_button, 0, wxALL);

        m_panel->SetSizer(mainS);

        wxBoxSizer* m_panelSizer = new wxBoxSizer(wxVERTICAL);
        m_panelSizer->Add(m_panel, 1, wxEXPAND, 5);

        SetSizer(m_panelSizer);
    }

    wxPanel* m_panel;
    wxButton* m_button;
};

Controller class:

class Frame
    : public wxEvtHandler // Without inheriting from it, binding work on new object
{
public:
    Frame()
        : m_ui(0)
    { }

    Frame(wxWindow* parent)
        : m_ui(new FrameUi(parent))
    { 
        m_ui->m_panel->Bind(wxEVT_LEFT_DOWN, &Frame::onLeftDown, this);
        m_ui->m_button->Bind(wxEVT_BUTTON, &Frame::onButton, this);
    }

    Frame(const Frame&) = delete;
    Frame& operator=(const Frame&) = delete;

    Frame& operator=(Frame&& rhs)
    {
        if (&rhs != this)
        {
            m_ui = rhs.m_ui;
            rhs.m_ui = nullptr;
        }
        return *this;
    }

    void onLeftDown(wxMouseEvent& event) { wxLogDebug("Left Down"); }

    void onButton(wxCommandEvent&) { wxLogDebug("Pressed"); }

    FrameUi* m_ui;
};

Main window class:

class MainWnd : public wxFrame
{
public:
    MainWnd()
        : wxFrame(NULL, wxID_ANY, wxEmptyString)
    {
        wxBoxSizer* s = new wxBoxSizer(wxVERTICAL);
        SetSizer(s);
    }

    Frame frame;
};

Application entrance point:

class WxguiApp
    : public wxApp
{
public:
    bool OnInit() override
    {
        MainWnd* mainWnd = new MainWnd();
        mainWnd->Show();
        SetTopWindow(mainWnd);

        mainWnd->frame = Frame(mainWnd); // If comment inheritance from wxEvtHandler, binding will work well
        mainWnd->frame.m_ui->Show();

        return true;
    }
};

IMPLEMENT_APP(WxguiApp);

Solution

  • wxEvtHandler is not movable, so you can't make a class derived from it movable. The simplest solution is, of course, just not inherit from it.