Whitespace and typo fixes
[amule.git] / src / MuleNotebook.cpp
blobff3ba294e8707ab553c61596b44e0c5e5f08f63d
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2004-2011 Angel Vidal ( kry@amule.org )
5 // Copyright (c) 2004-2011 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.
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::OnMouseButtonRelease)
45 EVT_MIDDLE_UP(CMuleNotebook::OnMouseButtonRelease)
46 EVT_MOTION(CMuleNotebook::OnMouseMotion)
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 a page change event to work around wx problem when newly selected page
83 // is identical with deleted page (wx sends a page change event during deletion,
84 // but the control is still the one to be deleted at that moment).
85 if (GetPageCount()) {
86 // Select the tab that took the place of the one we just deleted.
87 size_t page = nPage;
88 // Except if we deleted the last one - then select the one that is last now.
89 if (page == GetPageCount()) {
90 page--;
92 wxNotebookEvent event( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, GetId(), page );
93 event.SetEventObject(this);
94 ProcessEvent( event );
95 } else {
96 // Send an event when no pages are left open
97 wxNotebookEvent event( wxEVT_COMMAND_MULENOTEBOOK_ALL_PAGES_CLOSED, GetId() );
98 event.SetEventObject(this);
99 ProcessEvent( event );
102 return result;
106 bool CMuleNotebook::DeleteAllPages()
108 Freeze();
110 bool result = true;
111 while ( GetPageCount() ) {
112 result &= DeletePage( 0 );
115 Thaw();
117 return result;
121 void CMuleNotebook::EnablePopup( bool enable )
123 m_popup_enable = enable;
127 void CMuleNotebook::SetPopupHandler( wxWindow* widget )
129 m_popup_widget = widget;
133 //#warning wxMac does not support selection by right-clicking on tabs!
134 void CMuleNotebook::OnRMButton(wxMouseEvent& event)
136 // Cases where we shouldn't be showing a popup-menu.
137 if ( !GetPageCount() || !m_popup_enable ) {
138 event.Skip();
139 return;
143 // For some reason, gtk1 does a rather poor job when using the HitTest
144 wxPoint eventPoint = event.GetPosition();
146 int tab = HitTest(eventPoint);
147 if (tab != wxNOT_FOUND) {
148 SetSelection(tab);
149 } else {
150 event.Skip();
151 return;
154 // Should we send the event to a specific widget?
155 if ( m_popup_widget ) {
156 wxMouseEvent evt = event;
158 // Map the coordinates onto the parent
159 wxPoint point = evt.GetPosition();
160 point = ClientToScreen( point );
161 point = m_popup_widget->ScreenToClient( point );
163 evt.m_x = point.x;
164 evt.m_y = point.y;
166 m_popup_widget->GetEventHandler()->AddPendingEvent( evt );
167 } else {
168 wxMenu menu(_("Close"));
169 menu.Append(MP_CLOSE_TAB, wxString(_("Close tab")));
170 menu.Append(MP_CLOSE_ALL_TABS, wxString(_("Close all tabs")));
171 menu.Append(MP_CLOSE_OTHER_TABS, wxString(_("Close other tabs")));
173 PopupMenu( &menu, event.GetPosition() );
178 void CMuleNotebook::OnPopupClose(wxCommandEvent& WXUNUSED(evt))
180 DeletePage( GetSelection() );
184 void CMuleNotebook::OnPopupCloseAll(wxCommandEvent& WXUNUSED(evt))
186 DeleteAllPages();
190 void CMuleNotebook::OnPopupCloseOthers(wxCommandEvent& WXUNUSED(evt))
192 wxNotebookPage* current = GetPage( GetSelection() );
194 for ( int i = GetPageCount() - 1; i >= 0; i-- ) {
195 if ( current != GetPage( i ) )
196 DeletePage( i );
201 void CMuleNotebook::OnMouseButtonRelease(wxMouseEvent &event)
204 if (GetImageList() == NULL) {
205 // This Mulenotebook has no images on tabs, so nothing to do.
206 event.Skip();
207 return;
210 long xpos, ypos;
211 event.GetPosition(&xpos, &ypos);
213 long flags = 0;
214 int tab = HitTest(wxPoint(xpos,ypos),&flags);
216 if ((tab != -1) && (((flags == wxNB_HITTEST_ONICON) && event.LeftUp()) ||
217 ((flags == wxNB_HITTEST_ONLABEL) && event.MiddleUp()))) {
218 // User did click on a 'x' or middle click on the label
219 DeletePage(tab);
220 } else {
221 // Is not a 'x'. Send this event up.
222 event.Skip();
227 void CMuleNotebook::OnMouseMotion(wxMouseEvent &event)
230 if (GetImageList() == NULL) {
231 // This Mulenotebook has no images on tabs, so nothing to do.
232 event.Skip();
233 return;
236 long flags = 0;
237 int tab = HitTest(wxPoint(event.m_x,event.m_y),&flags);
239 // Clear the highlight for all tabs.
240 for (int i=0;i<(int)GetPageCount();++i) {
241 SetPageImage(i, 0);
244 if ((tab != -1) && (flags == wxNB_HITTEST_ONICON)) {
245 // Mouse is over a 'x'
246 SetPageImage(tab, 1);
247 } else {
248 // Is not a 'x'. Send this event up.
249 event.Skip();
254 // File_checked_for_headers