Upstream tarball 9445
[amule.git] / src / MuleNotebook.cpp
blob0fea9c9a908b459a87cc013e60ad1cbea6dbd55f
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 an event when no pages are left open
82 if ( !GetPageCount() ) {
83 wxNotebookEvent event( wxEVT_COMMAND_MULENOTEBOOK_ALL_PAGES_CLOSED, GetId() );
84 event.SetEventObject(this);
85 ProcessEvent( event );
88 return result;
92 bool CMuleNotebook::DeleteAllPages()
94 Freeze();
96 bool result = true;
97 while ( GetPageCount() ) {
98 result &= DeletePage( 0 );
101 Thaw();
103 return result;
107 void CMuleNotebook::EnablePopup( bool enable )
109 m_popup_enable = enable;
113 void CMuleNotebook::SetPopupHandler( wxWindow* widget )
115 m_popup_widget = widget;
119 //#warning wxMac does not support selection by right-clicking on tabs!
120 void CMuleNotebook::OnRMButton(wxMouseEvent& event)
122 // Cases where we shouldn't be showing a popup-menu.
123 if ( !GetPageCount() || !m_popup_enable ) {
124 event.Skip();
125 return;
129 // For some reason, gtk1 does a rather poor job when using the HitTest
130 wxPoint eventPoint = event.GetPosition();
132 int tab = HitTest(eventPoint);
133 if (tab != wxNOT_FOUND) {
134 SetSelection(tab);
135 } else {
136 event.Skip();
137 return;
140 // Should we send the event to a specific widget?
141 if ( m_popup_widget ) {
142 wxMouseEvent evt = event;
144 // Map the coordinates onto the parent
145 wxPoint point = evt.GetPosition();
146 point = ClientToScreen( point );
147 point = m_popup_widget->ScreenToClient( point );
149 evt.m_x = point.x;
150 evt.m_y = point.y;
152 m_popup_widget->AddPendingEvent( evt );
153 } else {
154 wxMenu menu(_("Close"));
155 menu.Append(MP_CLOSE_TAB, wxString(_("Close tab")));
156 menu.Append(MP_CLOSE_ALL_TABS, wxString(_("Close all tabs")));
157 menu.Append(MP_CLOSE_OTHER_TABS, wxString(_("Close other tabs")));
159 PopupMenu( &menu, event.GetPosition() );
164 void CMuleNotebook::OnPopupClose(wxCommandEvent& WXUNUSED(evt))
166 DeletePage( GetSelection() );
170 void CMuleNotebook::OnPopupCloseAll(wxCommandEvent& WXUNUSED(evt))
172 DeleteAllPages();
176 void CMuleNotebook::OnPopupCloseOthers(wxCommandEvent& WXUNUSED(evt))
178 wxNotebookPage* current = GetPage( GetSelection() );
180 for ( int i = GetPageCount() - 1; i >= 0; i-- ) {
181 if ( current != GetPage( i ) )
182 DeletePage( i );
187 void CMuleNotebook::OnMouseLeftRelease(wxMouseEvent &event)
190 if (GetImageList() == NULL) {
191 // This Mulenotebook has no images on tabs, so nothing to do.
192 event.Skip();
193 return;
196 long xpos, ypos;
197 event.GetPosition(&xpos, &ypos);
199 long flags = 0;
200 int tab = HitTest(wxPoint(xpos,ypos),&flags);
202 if ((tab != -1) && (flags == wxNB_HITTEST_ONICON)) {
203 // User did click on a 'x'
204 DeletePage(tab);
205 } else {
206 // Is not a 'x'. Send this event up.
207 event.Skip();
212 void CMuleNotebook::OnMouseMotion(wxMouseEvent &event)
215 if (GetImageList() == NULL) {
216 // This Mulenotebook has no images on tabs, so nothing to do.
217 event.Skip();
218 return;
221 long flags = 0;
222 int tab = HitTest(wxPoint(event.m_x,event.m_y),&flags);
224 // Clear the highlight for all tabs.
225 for (int i=0;i<(int)GetPageCount();++i) {
226 SetPageImage(i, 0);
229 if ((tab != -1) && (flags == wxNB_HITTEST_ONICON)) {
230 // Mouse is over a 'x'
231 SetPageImage(tab, 1);
232 } else {
233 // Is not a 'x'. Send this event up.
234 event.Skip();
239 // File_checked_for_headers