wxNotebook with Draggable & DoubleClickable Tab

If you have a cool piece of software to share, but you are not hosting it officially yet, please dump it in here. If you have code snippets that are useful, please donate!
Post Reply
phonxe
Earned a small fee
Earned a small fee
Posts: 18
Joined: Fri May 06, 2005 12:37 pm

wxNotebook with Draggable & DoubleClickable Tab

Post by phonxe » Sat Jul 23, 2005 11:32 am

Drag the Tab to change the position.
DoubleClick the Tab to remove the Tab/Page.

Tested under wxWidgets2.6.1 for MSWindows and Linux(wxGTK).
Change the cursor shape by wxSetCursor() only work under mswin.

wxMyNotebook only handle wxMouseEvent additionally, just use wxMyNotebook as usually as wxNotebook.

Code: Select all

class wxMyNotebook : public wxNotebook
{
private:
    DECLARE_EVENT_TABLE()
    
protected:
    // for Tab Dragging
    int m_TabID;
    wxCoord m_X,m_Y;
    void OnMouseEvent(wxMouseEvent& event);
    
public:
    wxMyNotebook(wxWindow *parent,
             wxWindowID id,
             const wxPoint& pos = wxDefaultPosition,
             const wxSize& size = wxDefaultSize,
             long style = 0,
             const wxString& name = wxNotebookNameStr)
             :wxNotebook(parent,id,pos,size,style,name), m_TabID(-1)
    {}
};

BEGIN_EVENT_TABLE(wxMyNotebook,wxNotebook)
    EVT_MOUSE_EVENTS(wxMyNotebook::OnMouseEvent)
END_EVENT_TABLE()

void wxMyNotebook::OnMouseEvent(wxMouseEvent& event)
{
    wxEventType eventType = event.GetEventType();
    
    if(m_TabID>=0)
    {
        if(eventType==wxEVT_LEFT_UP)
        {
            int id=HitTest(wxPoint(event.GetX(), event.GetY()));
            if(id>=0 && id!=m_TabID) 
            {
                // move [m_TabID] to [id]
                //wxGetApp().GetTopWindow()->SetTitle(wxString::Format(wxT("Drag %d->%d"), m_TabID, id));
                wxNotebookPage* page=GetPage(m_TabID);
                wxString text=GetPageText(m_TabID);
                int image=GetPageImage(m_TabID);
                
                RemovePage(m_TabID);
                InsertPage(id, page, text, true, image);
            }
            
            m_TabID=-1;
            wxSetCursor(wxCursor(wxCURSOR_ARROW));
        }
        else
        {
            wxCoord x=event.GetX();
            wxCoord y=event.GetY();
            int id=HitTest(wxPoint(x, y));
            if(id<0)
            {
                wxSetCursor(wxCursor(wxCURSOR_ARROW));
            }
            else if(x!=m_X || y!=m_Y)
            {
                wxSetCursor(wxCursor(wxCURSOR_HAND));
                m_X=m_Y=-1024;
            }
        }
    }
    else // m_TabID==-1, no dragging
    {
        if(eventType==wxEVT_LEFT_DOWN)
        {
            m_X=event.GetX();
            m_Y=event.GetY();
            m_TabID=HitTest(wxPoint(m_X, m_Y));
        }
        else if(eventType == wxEVT_LEFT_DCLICK)
        {
            // delete this page
            int id=HitTest(wxPoint(event.m_x, event.m_y));
            //wxGetApp().GetTopWindow()->SetTitle(wxString::Format(wxT("DCLICK %d"), id));
            DeletePage(id);
        }
    }
    
    event.Skip();
}

daddydave
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 214
Joined: Wed Jun 15, 2005 3:31 am
Location: United States
Contact:

Post by daddydave » Tue Jul 26, 2005 5:10 am

Haven't tried it, but sounds like it is sure to be a hit!

gururamnath
Moderator
Moderator
Posts: 466
Joined: Sat Sep 18, 2004 2:49 am
Location: California, USA

Re: wxNotebook with Draggable & DoubleClickable Tab

Post by gururamnath » Tue Jul 26, 2005 5:35 pm

There is a bounty for this feature given in the wxWidgets site. Have a look at it, you might get some money for this contribution.

-Guru Kathiresan

VonGodric
Earned some good credits
Earned some good credits
Posts: 103
Joined: Sun Jan 30, 2005 9:31 pm
Contact:

Post by VonGodric » Mon Aug 15, 2005 8:04 pm

Works perfectly!!!

Tnx :wink:

Post Reply