Improve speed of category tab title updates
[amule.git] / src / MuleNotebook.cpp
blob6a821a53a2976c64cd33583a4f7b6bafda393420
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2004-2008 Angel Vidal ( kry@amule.org )
5 // Copyright (c) 2004-2008 aMule Team ( admin@amule.org / http://www.amule.org )
6 //
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
9 // respective authors.
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include <wx/menu.h>
27 #include <wx/intl.h>
29 #include "MuleNotebook.h" // Interface declarations
31 #include <common/MenuIDs.h>
33 DEFINE_LOCAL_EVENT_TYPE(wxEVT_COMMAND_MULENOTEBOOK_PAGE_CLOSING)
34 DEFINE_LOCAL_EVENT_TYPE(wxEVT_COMMAND_MULENOTEBOOK_ALL_PAGES_CLOSED)
36 BEGIN_EVENT_TABLE(CMuleNotebook, wxNotebook)
37 EVT_RIGHT_DOWN(CMuleNotebook::OnRMButton)
39 EVT_MENU(MP_CLOSE_TAB, CMuleNotebook::OnPopupClose)
40 EVT_MENU(MP_CLOSE_ALL_TABS, CMuleNotebook::OnPopupCloseAll)
41 EVT_MENU(MP_CLOSE_OTHER_TABS, CMuleNotebook::OnPopupCloseOthers)
43 // Madcat - tab closing engine
44 EVT_LEFT_UP(CMuleNotebook::OnMouseLeftRelease)
45 EVT_MOTION(CMuleNotebook::OnMouseMotion)
46 END_EVENT_TABLE()
48 CMuleNotebook::CMuleNotebook( wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style, const wxString& name )
49 : wxNotebook(parent, id, pos, size, style, name)
51 m_popup_enable = true;
52 m_popup_widget = NULL;
56 CMuleNotebook::~CMuleNotebook()
58 // Ensure that all notifications gets sent
59 DeleteAllPages();
63 bool CMuleNotebook::DeletePage(int nPage)
65 wxCHECK_MSG((nPage >= 0) && (nPage < (int)GetPageCount()), false,
66 wxT("Trying to delete invalid page-index in CMuleNotebook::DeletePage"));
68 // Send out close event
69 wxNotebookEvent evt( wxEVT_COMMAND_MULENOTEBOOK_PAGE_CLOSING, GetId(), nPage );
70 evt.SetEventObject(this);
71 ProcessEvent( evt );
73 // and finally remove the actual page
74 bool result = wxNotebook::DeletePage( nPage );
76 // Ensure a valid selection
77 if ( GetPageCount() && (int)GetSelection() >= (int)GetPageCount() ) {
78 SetSelection( GetPageCount() - 1 );
81 // Send a page change event to work around wx problem when newly selected page
82 // is identical with deleted page (wx sends a page change event during deletion,
83 // but the control is still the one to be deleted at that moment).
84 if (GetPageCount()) {
85 // Select the tab that took the place of the one we just deleted.
86 size_t page = nPage;
87 // Except if we deleted the last one - then select the one that is last now.
88 if (page == GetPageCount()) {
89 page--;
91 wxNotebookEvent event( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, GetId(), page );
92 event.SetEventObject(this);
93 ProcessEvent( event );
94 } else {
95 // Send an event when no pages are left open
96 wxNotebookEvent event( wxEVT_COMMAND_MULENOTEBOOK_ALL_PAGES_CLOSED, GetId() );
97 event.SetEventObject(this);
98 ProcessEvent( event );
101 return result;
105 bool CMuleNotebook::DeleteAllPages()
107 Freeze();
109 bool result = true;
110 while ( GetPageCount() ) {
111 result &= DeletePage( 0 );
114 Thaw();
116 return result;
120 void CMuleNotebook::EnablePopup( bool enable )
122 m_popup_enable = enable;
126 void CMuleNotebook::SetPopupHandler( wxWindow* widget )
128 m_popup_widget = widget;
132 //#warning wxMac does not support selection by right-clicking on tabs!
133 void CMuleNotebook::OnRMButton(wxMouseEvent& event)
135 // Cases where we shouldn't be showing a popup-menu.
136 if ( !GetPageCount() || !m_popup_enable ) {
137 event.Skip();
138 return;
142 // For some reason, gtk1 does a rather poor job when using the HitTest
143 wxPoint eventPoint = event.GetPosition();
145 int tab = HitTest(eventPoint);
146 if (tab != wxNOT_FOUND) {
147 SetSelection(tab);
148 } else {
149 event.Skip();
150 return;
153 // Should we send the event to a specific widget?
154 if ( m_popup_widget ) {
155 wxMouseEvent evt = event;
157 // Map the coordinates onto the parent
158 wxPoint point = evt.GetPosition();
159 point = ClientToScreen( point );
160 point = m_popup_widget->ScreenToClient( point );
162 evt.m_x = point.x;
163 evt.m_y = point.y;
165 m_popup_widget->GetEventHandler()->AddPendingEvent( evt );
166 } else {
167 wxMenu menu(_("Close"));
168 menu.Append(MP_CLOSE_TAB, wxString(_("Close tab")));
169 menu.Append(MP_CLOSE_ALL_TABS, wxString(_("Close all tabs")));
170 menu.Append(MP_CLOSE_OTHER_TABS, wxString(_("Close other tabs")));
172 PopupMenu( &menu, event.GetPosition() );
177 void CMuleNotebook::OnPopupClose(wxCommandEvent& WXUNUSED(evt))
179 DeletePage( GetSelection() );
183 void CMuleNotebook::OnPopupCloseAll(wxCommandEvent& WXUNUSED(evt))
185 DeleteAllPages();
189 void CMuleNotebook::OnPopupCloseOthers(wxCommandEvent& WXUNUSED(evt))
191 wxNotebookPage* current = GetPage( GetSelection() );
193 for ( int i = GetPageCount() - 1; i >= 0; i-- ) {
194 if ( current != GetPage( i ) )
195 DeletePage( i );
200 void CMuleNotebook::OnMouseLeftRelease(wxMouseEvent &event)
203 if (GetImageList() == NULL) {
204 // This Mulenotebook has no images on tabs, so nothing to do.
205 event.Skip();
206 return;
209 long xpos, ypos;
210 event.GetPosition(&xpos, &ypos);
212 long flags = 0;
213 int tab = HitTest(wxPoint(xpos,ypos),&flags);
215 if ((tab != -1) && (flags == wxNB_HITTEST_ONICON)) {
216 // User did click on a 'x'
217 DeletePage(tab);
218 } else {
219 // Is not a 'x'. Send this event up.
220 event.Skip();
225 void CMuleNotebook::OnMouseMotion(wxMouseEvent &event)
228 if (GetImageList() == NULL) {
229 // This Mulenotebook has no images on tabs, so nothing to do.
230 event.Skip();
231 return;
234 long flags = 0;
235 int tab = HitTest(wxPoint(event.m_x,event.m_y),&flags);
237 // Clear the highlight for all tabs.
238 for (int i=0;i<(int)GetPageCount();++i) {
239 SetPageImage(i, 0);
242 if ((tab != -1) && (flags == wxNB_HITTEST_ONICON)) {
243 // Mouse is over a 'x'
244 SetPageImage(tab, 1);
245 } else {
246 // Is not a 'x'. Send this event up.
247 event.Skip();
252 // File_checked_for_headers