Upstream tarball 20080928
[amule.git] / src / MuleNotebook.cpp
blob8dae6b302aba857dd51abf1189037ef94463bdf0
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_DOWN(CMuleNotebook::MouseClick)
45 EVT_LEFT_DCLICK(CMuleNotebook::MouseClick)
46 EVT_MOTION(CMuleNotebook::MouseMotion)
47 END_EVENT_TABLE()
49 CMuleNotebook::CMuleNotebook( wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style, const wxString& name )
50 : wxNotebook(parent, id, pos, size, style, name)
52 m_popup_enable = true;
53 m_popup_widget = NULL;
57 CMuleNotebook::~CMuleNotebook()
59 // Ensure that all notifications gets sent
60 DeleteAllPages();
64 bool CMuleNotebook::DeletePage(int nPage)
66 wxCHECK_MSG((nPage >= 0) && (nPage < (int)GetPageCount()), false,
67 wxT("Trying to delete invalid page-index in CMuleNotebook::DeletePage"));
69 // Send out close event
70 wxNotebookEvent evt( wxEVT_COMMAND_MULENOTEBOOK_PAGE_CLOSING, GetId(), nPage );
71 evt.SetEventObject(this);
72 ProcessEvent( evt );
74 // and finally remove the actual page
75 bool result = wxNotebook::DeletePage( nPage );
77 // Ensure a valid selection
78 if ( GetPageCount() && (int)GetSelection() >= (int)GetPageCount() ) {
79 SetSelection( GetPageCount() - 1 );
82 // Send an event when no pages are left open
83 if ( !GetPageCount() ) {
84 wxNotebookEvent event( wxEVT_COMMAND_MULENOTEBOOK_ALL_PAGES_CLOSED, GetId() );
85 event.SetEventObject(this);
86 ProcessEvent( event );
89 return result;
93 bool CMuleNotebook::DeleteAllPages()
95 Freeze();
97 bool result = true;
98 while ( GetPageCount() ) {
99 result &= DeletePage( 0 );
102 Thaw();
104 return result;
108 void CMuleNotebook::EnablePopup( bool enable )
110 m_popup_enable = enable;
114 void CMuleNotebook::SetPopupHandler( wxWindow* widget )
116 m_popup_widget = widget;
120 //#warning wxMac does not support selection by right-clicking on tabs!
121 void CMuleNotebook::OnRMButton(wxMouseEvent& event)
123 // Cases where we shouldn't be showing a popup-menu.
124 if ( !GetPageCount() || !m_popup_enable ) {
125 event.Skip();
126 return;
130 // For some reason, gtk1 does a rather poor job when using the HitTest
131 wxPoint eventPoint = event.GetPosition();
133 int tab = HitTest(eventPoint);
134 if (tab != wxNOT_FOUND) {
135 SetSelection(tab);
136 } else {
137 event.Skip();
138 return;
141 // Should we send the event to a specific widget?
142 if ( m_popup_widget ) {
143 wxMouseEvent evt = event;
145 // Map the coordinates onto the parent
146 wxPoint point = evt.GetPosition();
147 point = ClientToScreen( point );
148 point = m_popup_widget->ScreenToClient( point );
150 evt.m_x = point.x;
151 evt.m_y = point.y;
153 m_popup_widget->AddPendingEvent( evt );
154 } else {
155 wxMenu menu(_("Close"));
156 menu.Append(MP_CLOSE_TAB, wxString(_("Close tab")));
157 menu.Append(MP_CLOSE_ALL_TABS, wxString(_("Close all tabs")));
158 menu.Append(MP_CLOSE_OTHER_TABS, wxString(_("Close other tabs")));
160 PopupMenu( &menu, event.GetPosition() );
165 void CMuleNotebook::OnPopupClose(wxCommandEvent& WXUNUSED(evt))
167 DeletePage( GetSelection() );
171 void CMuleNotebook::OnPopupCloseAll(wxCommandEvent& WXUNUSED(evt))
173 DeleteAllPages();
177 void CMuleNotebook::OnPopupCloseOthers(wxCommandEvent& WXUNUSED(evt))
179 wxNotebookPage* current = GetPage( GetSelection() );
181 for ( int i = GetPageCount() - 1; i >= 0; i-- ) {
182 if ( current != GetPage( i ) )
183 DeletePage( i );
188 void CMuleNotebook::MouseClick(wxMouseEvent &event)
191 if (GetImageList() == NULL) {
192 // This Mulenotebook has no images on tabs, so nothing to do.
193 event.Skip();
194 return;
197 long xpos, ypos;
198 event.GetPosition(&xpos, &ypos);
200 long flags = 0;
201 int tab = HitTest(wxPoint(xpos,ypos),&flags);
203 if ((tab != -1) && (flags == wxNB_HITTEST_ONICON)) {
204 // User did click on a 'x'
205 DeletePage(tab);
206 } else {
207 // Is not a 'x'. Send this event up.
208 event.Skip();
213 void CMuleNotebook::MouseMotion(wxMouseEvent &event)
216 if (GetImageList() == NULL) {
217 // This Mulenotebook has no images on tabs, so nothing to do.
218 event.Skip();
219 return;
222 long flags = 0;
223 int tab = HitTest(wxPoint(event.m_x,event.m_y),&flags);
225 // Clear the highlight for all tabs.
226 for (int i=0;i<(int)GetPageCount();++i) {
227 SetPageImage(i, 0);
230 if ((tab != -1) && (flags == wxNB_HITTEST_ONICON)) {
231 // Mouse is over a 'x'
232 SetPageImage(tab, 1);
233 } else {
234 // Is not a 'x'. Send this event up.
235 event.Skip();
240 // File_checked_for_headers