Murabıt Akdoğan
Murabıt Akdoğan

Reputation: 61

How to detect double click on wxAuiNotebook tab at wxWidgets?

I am trying to detect a double-click on a tab in wxAuiNotebook in wxWidgets (C++). However, I can't find any event in the wxAuiNotebook event system that corresponds to this action.

In wxPython, there is an event named wxEVT_AUINOTEBOOK_TAB_DCLICK which belongs to the class wx.lib.agw.aui.auibook.AuiNotebook and this event handles double-clicks on notebook tabs, but I need something similar for wxWidgets in C++.

For better understanding, this is an event that I want to run when I double-click on one of the tabs shown in the picture.

enter image description here

I have tried detecting the double-click by using auiNotebook->HitTest(position) with the position obtained from the mouse event when the double-click happens, but it doesn't seem to work. Is there any way to achieve this in wxWidgets?

How can I implement this functionality or is there an existing event that I might be missing?

Edit: I've managed to get click event but still not work properly You can see the problem

void SuperNotebook::OnPageChanged(wxAuiNotebookEvent& event)
{
    wxSize notebookSize = GetSize();
    int width = notebookSize.GetWidth();
    int height = notebookSize.GetHeight();

    int step = 10;

    for (int x = 0; x < width; x += step)
    {
        for (int y = 0; y < height; y += step)
        {
            wxPoint point(x, y);
            wxAuiTabCtrl* tabCtrl = GetTabCtrlFromPoint(point);

            if (tabCtrl)
            {
                tabCtrl->Bind(wxEVT_LEFT_DCLICK, &SuperNotebook::OnTabDoubleClick, this);
            }
        }
    }

    event.Skip();
}


void SuperNotebook::OnTabDoubleClick(wxMouseEvent& event)
{
    wxPoint pos = event.GetPosition();

    console->Print(wxString::Format("Clicked On: X: % d, Y : % d", pos.x, pos.y), MessageType::Information);


    int tabIndex = HitTest(pos);
    if (tabIndex != wxNOT_FOUND)
    {
        wxString tabName = GetPageText(tabIndex);
        console->Print(wxString::Format("Tab name: %s\n", tabName), MessageType::Good);
    }
}

Upvotes: 0

Views: 89

Answers (1)

Murabıt Akdoğan
Murabıt Akdoğan

Reputation: 61

There isn't an event that detects tab clicks. I handled this in a rather unconventional way. First, I derived a new class from the wxAuiGenericTabArt class and overrode the DrawTab method and added those lines inside that method:

//------------------For tab double-click event------------------//
wxPoint mousePos = wxGetMousePosition();
wxPoint relativeMousePos = wnd->ScreenToClient(mousePos);

if (in_rect.Contains(relativeMousePos)) {
    if (wxGetMouseState().LeftIsDown()) {
        m_notebook->SetSelectedPage(&page);
    }
}
else {
    m_notebook->SetSelectedPage(nullptr);
}

Here, m_notebook is a custom class I derived from wxAuiNotebook that includes the SetSelectedPage method. Then, in the class derived from wxAuiNotebook, I used Bind to associate the wxEVT_LEFT_DCLICK event with the TabCtrls within methods handling events like OnPageChange or OnPageChanging. Afterward, I managed to capture which tab was double-clicked as follows:

void CustomNotebook::OnTabDoubleClick(wxMouseEvent& event) 
{
    wxPoint pos = event.GetPosition();

    int tabIndex = HitTest(pos);
    if (tabIndex != wxNOT_FOUND)
    {
        if (selectedPage)
        {
            //In here I can get
        }
    }
}

It's important to note that this is a complex approach and the best solution until a better one is found.

Upvotes: 0

Related Questions