Upstream tarball 9931
[amule.git] / src / MuleNotebook.cpp
blob2450da6ac57cdec9168e51c707fef99d0092f4fe
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 wxNotebookEvent event( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, GetId(), nPage );
86 event.SetEventObject(this);
87 ProcessEvent( event );
88 } else {
89 // Send an event when no pages are left open
90 wxNotebookEvent event( wxEVT_COMMAND_MULENOTEBOOK_ALL_PAGES_CLOSED, GetId() );
91 event.SetEventObject(this);
92 ProcessEvent( event );
95 return result;
99 bool CMuleNotebook::DeleteAllPages()
101 Freeze();
103 bool result = true;
104 while ( GetPageCount() ) {
105 result &= DeletePage( 0 );
108 Thaw();
110 return result;
114 void CMuleNotebook::EnablePopup( bool enable )
116 m_popup_enable = enable;
120 void CMuleNotebook::SetPopupHandler( wxWindow* widget )
122 m_popup_widget = widget;
126 //#warning wxMac does not support selection by right-clicking on tabs!
127 void CMuleNotebook::OnRMButton(wxMouseEvent& event)
129 // Cases where we shouldn't be showing a popup-menu.
130 if ( !GetPageCount() || !m_popup_enable ) {
131 event.Skip();
132 return;
136 // For some reason, gtk1 does a rather poor job when using the HitTest
137 wxPoint eventPoint = event.GetPosition();
139 int tab = HitTest(eventPoint);
140 if (tab != wxNOT_FOUND) {
141 SetSelection(tab);
142 } else {
143 event.Skip();
144 return;
147 // Should we send the event to a specific widget?
148 if ( m_popup_widget ) {
149 wxMouseEvent evt = event;
151 // Map the coordinates onto the parent
152 wxPoint point = evt.GetPosition();
153 point = ClientToScreen( point );
154 point = m_popup_widget->ScreenToClient( point );
156 evt.m_x = point.x;
157 evt.m_y = point.y;
159 m_popup_widget->GetEventHandler()->AddPendingEvent( evt );
160 } else {
161 wxMenu menu(_("Close"));
162 menu.Append(MP_CLOSE_TAB, wxString(_("Close tab")));
163 menu.Append(MP_CLOSE_ALL_TABS, wxString(_("Close all tabs")));
164 menu.Append(MP_CLOSE_OTHER_TABS, wxString(_("Close other tabs")));
166 PopupMenu( &menu, event.GetPosition() );
171 void CMuleNotebook::OnPopupClose(wxCommandEvent& WXUNUSED(evt))
173 DeletePage( GetSelection() );
177 void CMuleNotebook::OnPopupCloseAll(wxCommandEvent& WXUNUSED(evt))
179 DeleteAllPages();
183 void CMuleNotebook::OnPopupCloseOthers(wxCommandEvent& WXUNUSED(evt))
185 wxNotebookPage* current = GetPage( GetSelection() );
187 for ( int i = GetPageCount() - 1; i >= 0; i-- ) {
188 if ( current != GetPage( i ) )
189 DeletePage( i );
194 void CMuleNotebook::OnMouseLeftRelease(wxMouseEvent &event)
197 if (GetImageList() == NULL) {
198 // This Mulenotebook has no images on tabs, so nothing to do.
199 event.Skip();
200 return;
203 long xpos, ypos;
204 event.GetPosition(&xpos, &ypos);
206 long flags = 0;
207 int tab = HitTest(wxPoint(xpos,ypos),&flags);
209 if ((tab != -1) && (flags == wxNB_HITTEST_ONICON)) {
210 // User did click on a 'x'
211 DeletePage(tab);
212 } else {
213 // Is not a 'x'. Send this event up.
214 event.Skip();
219 void CMuleNotebook::OnMouseMotion(wxMouseEvent &event)
222 if (GetImageList() == NULL) {
223 // This Mulenotebook has no images on tabs, so nothing to do.
224 event.Skip();
225 return;
228 long flags = 0;
229 int tab = HitTest(wxPoint(event.m_x,event.m_y),&flags);
231 // Clear the highlight for all tabs.
232 for (int i=0;i<(int)GetPageCount();++i) {
233 SetPageImage(i, 0);
236 if ((tab != -1) && (flags == wxNB_HITTEST_ONICON)) {
237 // Mouse is over a 'x'
238 SetPageImage(tab, 1);
239 } else {
240 // Is not a 'x'. Send this event up.
241 event.Skip();
246 // File_checked_for_headers