2 // This file is part of the aMule Project.
4 // Copyright (c) 2004-2008 Angel Vidal ( kry@amule.org )
5 // Copyright (c) 2004-2008 aMule Team ( admin@amule.org / http://www.amule.org )
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
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.
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
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
)
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
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);
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
);
93 bool CMuleNotebook::DeleteAllPages()
98 while ( GetPageCount() ) {
99 result
&= DeletePage( 0 );
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
) {
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
) {
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
);
153 m_popup_widget
->AddPendingEvent( evt
);
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
))
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
) )
188 void CMuleNotebook::MouseClick(wxMouseEvent
&event
)
191 if (GetImageList() == NULL
) {
192 // This Mulenotebook has no images on tabs, so nothing to do.
198 event
.GetPosition(&xpos
, &ypos
);
201 int tab
= HitTest(wxPoint(xpos
,ypos
),&flags
);
203 if ((tab
!= -1) && (flags
== wxNB_HITTEST_ONICON
)) {
204 // User did click on a 'x'
207 // Is not a 'x'. Send this event up.
213 void CMuleNotebook::MouseMotion(wxMouseEvent
&event
)
216 if (GetImageList() == NULL
) {
217 // This Mulenotebook has no images on tabs, so nothing to do.
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
) {
230 if ((tab
!= -1) && (flags
== wxNB_HITTEST_ONICON
)) {
231 // Mouse is over a 'x'
232 SetPageImage(tab
, 1);
234 // Is not a 'x'. Send this event up.
240 // File_checked_for_headers