wxNotebook Tab context menu Topic is solved

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
sethjackson
Super wx Problem Solver
Super wx Problem Solver
Posts: 396
Joined: Wed Oct 05, 2005 1:19 am

wxNotebook Tab context menu

Post by sethjackson »

Hi will wxNotebook ever have context menu support?

Something like this.

Code: Select all

EVT_NOTEBOOK_CONTEXT_MENU(...)
I think it is possible (Think Firefox. Maybe they owner-draw the tabs though)....
upCASE
Moderator
Moderator
Posts: 3176
Joined: Mon Aug 30, 2004 6:55 am
Location: Germany, Cologne

Re: wxNotebook Tab context menu

Post by upCASE »

sethjackson wrote:Hi will wxNotebook ever have context menu support?
An easy way to do this would be using a mouse event for the notebook. Connect an event handler like

Code: Select all

m_notebook->Connect( wxID_ANY,  wxEVT_RIGHT_UP ,wxMouseEventHandler(MyFrame::OnRightClick),NULL,this );
An create a wxMenu in the handler and call wxWindow::PopupMenu(). You'll have to figure out the active tab by yourself though.
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4

"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda
sethjackson
Super wx Problem Solver
Super wx Problem Solver
Posts: 396
Joined: Wed Oct 05, 2005 1:19 am

Re: wxNotebook Tab context menu

Post by sethjackson »

upCASE wrote:
sethjackson wrote:Hi will wxNotebook ever have context menu support?
An easy way to do this would be using a mouse event for the notebook. Connect an event handler like

Code: Select all

m_notebook->Connect( wxID_ANY,  wxEVT_RIGHT_UP ,wxMouseEventHandler(MyFrame::OnRightClick),NULL,this );
An create a wxMenu in the handler and call wxWindow::PopupMenu(). You'll have to figure out the active tab by yourself though.
Ok. Thanks. Will something like the above ever be added to wx though? It shouldn't be too hard....
sethjackson
Super wx Problem Solver
Super wx Problem Solver
Posts: 396
Joined: Wed Oct 05, 2005 1:19 am

Post by sethjackson »

This is what I meant. My question was if this would ever be added to wx with some type of wxNotebookEvent......

Image
eranif
Moderator
Moderator
Posts: 610
Joined: Tue Nov 29, 2005 7:10 pm
Location: Israel

Post by eranif »

You can use wxFlatNotebook, which contains this functionality and more.

to achieve this, you can use:
EVT_FLATNOTEBOOK_CONTEXT_MENU

or, if your menu is the same for all tabs, you can use this API function:
m_notebook->SetRightClickMenu(wxMenu*).

Eran
IDE: CodeLite + wxCrafter
OS: All
https://wxcrafter.codelite.org
https://codelite.org
sethjackson
Super wx Problem Solver
Super wx Problem Solver
Posts: 396
Joined: Wed Oct 05, 2005 1:19 am

Post by sethjackson »

Yes I know, but I want to use wxNotebook in my app. Not that I have anything against wxFNB because wxFNB is a nice peice of software..... Will the regular wxNotebook ever have this though? Also I'm really not sure why wxNotebook doesn't have this functionality in the first place. :?:
lowjoel
Part Of The Furniture
Part Of The Furniture
Posts: 1511
Joined: Sun Jun 19, 2005 11:37 am
Location: Singapore
Contact:

Post by lowjoel »

IMO firefox isn't the best example, because firefox draws most of its UI on its own but wx uses native controls.

-Joel
sethjackson
Super wx Problem Solver
Super wx Problem Solver
Posts: 396
Joined: Wed Oct 05, 2005 1:19 am

Post by sethjackson »

lowjoel wrote:IMO firefox isn't the best example, because firefox draws most of its UI on its own but wx uses native controls.

-Joel
Yes, but I think it gets the point across...... I'm just wondering if something like a context menu when right clicking on a tab will ever be supported in wx?
benedicte
wxWorld Domination!
wxWorld Domination!
Posts: 1409
Joined: Wed Jan 19, 2005 3:44 pm
Location: Paris, France

Re: wxNotebook Tab context menu

Post by benedicte »

sethjackson wrote:Hi will wxNotebook ever have context menu support?

Something like this.

Code: Select all

EVT_NOTEBOOK_CONTEXT_MENU(...)
I think it is possible (Think Firefox. Maybe they owner-draw the tabs though)....
Handle the EVT_MOUSE_EVENTS event:

Code: Select all

void MyNotebook::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) 
            { 
                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_RIGHT_DOWN) 
        { 
	m_rightClickedTabID = HitTest(event.GetPosition());
	wxMenu *menu = popup_menu you created the way you need (C++ code/XRC...);
	PopupMenu (menu, event.GetPosition());
	delete menu;
        } 
    } 
    
    event.Skip(); 
} 

// in MyNotebook class, declare the following members
//    int m_TabID, m_rightClickedTabID; 
//    wxCoord m_X,m_Y;

In the same class, you should handle events sent by the popup menu (such as "tab_close").
To do this, set the tab identifier in the event and pop it to the "upper" event handler:

Code: Select all

	event.SetExtraLong(1 + m_rightClickedTabID);
	event.Skip();
sethjackson
Super wx Problem Solver
Super wx Problem Solver
Posts: 396
Joined: Wed Oct 05, 2005 1:19 am

Post by sethjackson »

Hey benedicte you must read my mind..... :shock: I was going to do something pretty close to your code. I think I will close the thread with your answer. Thanks for the input everyone. :D
Post Reply