Search code examples
c++visual-studiowxwidgetsvisual-studio-debuggingpdb-files

How to destroy wxWidgets Popup ONLY on clicking button inside it?


I have created a popup using "wxPopupTransientWindow" and added some text and a button. Now I want to destroy this popup only on Clicking this particular button. Right now, Popup will get destroyed if click anywhere outside it. Here is what I have tried so far.

SimpleTransientPopup::SimpleTransientPopup(wxWindow *parent, bool scrolled) : wxPopupTransientWindow(parent, wxFRAME_SHAPED)
{

    m_bGotItClick = false;
    m_panel = new wxScrolledWindow(this,-1,wxDefaultPosition,wxDefaultSize);
    m_panel->SetBackgroundColour(*wxYELLOW);

    wxStaticText *text = new wxStaticText( m_panel, wxID_ANY,
                          "This panel will guide you stepwise through the workflow\n");

    wxPanel *pBottomPanel = new wxPanel(m_panel, -1);
    m_button = new wxButton(pBottomPanel, Minimal_PopupButton, "Got it!",wxDefaultPosition, wxDefaultSize);
    m_link = new wxHyperlinkCtrl(pBottomPanel, Minimal_PopupLink, _("Hide these tips"), _(""), wxDefaultPosition, wxDefaultSize, wxHL_ALIGN_LEFT);
    m_link->SetFont(wxFont(10, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_ITALIC, wxFONTWEIGHT_NORMAL, true));
    m_link->SetNormalColour(*wxLIGHT_GREY);
    m_link->SetForegroundColour(*wxLIGHT_GREY);
    wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
    wxBoxSizer *bottomSizer = new wxBoxSizer(wxHORIZONTAL);
    
    bottomSizer->Add(m_link,0,wxALL,20);
    bottomSizer->AddStretchSpacer();
    bottomSizer->Add(m_button, 0, wxALL, 20);
    
    pBottomPanel->SetAutoLayout(true);
    pBottomPanel->SetSizer(bottomSizer);
    bottomSizer->SetSizeHints(pBottomPanel);
    bottomSizer->Fit(pBottomPanel);

    topSizer->Add(text, 0, wxEXPAND | wxLEFT | wxRIGHT | wxTOP, 20);
    topSizer->Add(pBottomPanel,0,wxEXPAND);
    
    m_panel->SetSizer( topSizer );
    // Use the fitting size for the panel if we don't need scrollbars.
    topSizer->Fit(m_panel);

    SetClientSize(m_panel->GetSize());
    wxTipKind tipKind = wxTipKind_Auto;
    const int offsetY = SetTipShapeAndSize(tipKind, GetBestSize());
    if (offsetY > 0)
    {
        // Offset our contents by the tip height to make it appear in the
        // main rectangle.
        topSizer->PrependSpacer(offsetY);
    }
    m_panel->Fit();
}

I am calling this OnDismiss function on clicking the button.

void SimpleTransientPopup::OnDismiss()
{
    if (m_bGotItClick) {
        wxLogMessage("%p SimpleTransientPopup::OnDismiss", this);
        wxPopupTransientWindow::OnDismiss();
        m_bGotItClick = false;
    }
}

PS: I am new to wxWidgets


Solution

  • By overriding "wxPopupTransientWindow::Dismiss()" I was able to close the popup when button is clicked.

    void SimpleTransientPopup::Dismiss()
    {
        if (m_bGotItClick) {
            wxLogMessage("%p SimpleTransientPopup::Dismiss", this);
            wxPopupTransientWindow::Dismiss();
            m_bGotItClick = false;
        }
    }