Fix crash with wxWidgets 2.9
[amule.git] / src / SharedFilesWnd.cpp
blob4da2e3fa4f227ab7a81594765078f158703b0ad6
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002-2008 Merkur ( devs@emule-project.net / http://www.emule-project.net )
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/config.h>
27 #include <wx/gauge.h> // Do_not_auto_remove (win32)
29 #include "SharedFilesWnd.h" // Interface declarations
30 #include "SharedFilesCtrl.h"
31 #include "SharedFilePeersListCtrl.h"
32 #include "muuli_wdr.h" // Needed for ID_SHFILELIST
33 #include "KnownFileList.h" // Needed for CKnownFileList
34 #include "KnownFile.h" // Needed for CKnownFile
35 #include "amule.h" // Needed for theApp
38 BEGIN_EVENT_TABLE(CSharedFilesWnd, wxPanel)
39 EVT_LIST_ITEM_SELECTED( ID_SHFILELIST, CSharedFilesWnd::OnItemSelectionChanged )
40 EVT_LIST_ITEM_DESELECTED( ID_SHFILELIST, CSharedFilesWnd::OnItemSelectionChanged )
41 EVT_BUTTON( ID_BTNRELSHARED, CSharedFilesWnd::OnBtnReloadShared )
42 EVT_BUTTON(ID_SHAREDCLIENTTOGGLE, CSharedFilesWnd::OnToggleClientList)
44 EVT_SPLITTER_SASH_POS_CHANGING(ID_SHARESSPLATTER, CSharedFilesWnd::OnSashPositionChanging)
45 END_EVENT_TABLE()
47 CSharedFilesWnd::CSharedFilesWnd( wxWindow* pParent )
48 : wxPanel(pParent, -1)
50 wxSizer* content = sharedfilesDlg(this, true);
51 content->Show(this, true);
53 m_bar_requests = CastChild( wxT("popbar"), wxGauge );
54 m_bar_accepted = CastChild( wxT("popbarAccept"), wxGauge );
55 m_bar_transfer = CastChild( wxT("popbarTrans"), wxGauge );
56 sharedfilesctrl = CastChild( wxT("sharedFilesCt"), CSharedFilesCtrl );
57 peerslistctrl = CastChild( ID_SHAREDCLIENTLIST, CSharedFilePeersListCtrl );
58 wxASSERT(sharedfilesctrl);
59 wxASSERT(peerslistctrl);
60 m_prepared = false;
62 m_splitter = 0;
64 wxConfigBase *config = wxConfigBase::Get();
66 // Check if the clientlist is hidden
67 bool show = true;
68 config->Read( wxT("/GUI/SharedWnd/ShowClientList"), &show, true );
69 peerslistctrl->SetShowing(show);
70 // Load the last used splitter position
71 m_splitter = config->Read( wxT("/GUI/SharedWnd/Splitter"), 463l );
75 CSharedFilesWnd::~CSharedFilesWnd()
77 wxConfigBase *config = wxConfigBase::Get();
79 if ( !peerslistctrl->GetShowing() ) {
80 // Save the splitter position
81 config->Write( wxT("/GUI/SharedWnd/Splitter"), m_splitter );
83 // Save the visible status of the list
84 config->Write( wxT("/GUI/SharedWnd/ShowClientList"), false );
85 } else {
86 wxSplitterWindow* splitter = CastChild( wxT("sharedsplitterWnd"), wxSplitterWindow );
88 // Save the splitter position
89 config->Write( wxT("/GUI/SharedWnd/Splitter"), splitter->GetSashPosition() );
91 // Save the visible status of the list
92 config->Write( wxT("/GUI/SharedWnd/ShowClientList"), true );
97 void CSharedFilesWnd::SelectionUpdated()
99 if (!sharedfilesctrl->IsSorting()) {
100 uint64 lTransferred = theApp->knownfiles->transferred;
101 uint32 lAccepted = theApp->knownfiles->accepted;
102 uint32 lRequested = theApp->knownfiles->requested;
103 m_bar_requests->SetRange( lRequested );
104 m_bar_accepted->SetRange( lAccepted );
105 m_bar_transfer->SetRange( lTransferred / 1024 );
107 CKnownFileVector fileVector;
109 if ( !sharedfilesctrl->GetSelectedItemCount() ) {
110 // Requests
111 m_bar_requests->SetValue( 0 );
112 CastChild(IDC_SREQUESTED, wxStaticText)->SetLabel( wxT("- / -") );
114 // Accepted requets
115 m_bar_accepted->SetValue( 0 );
116 CastChild(IDC_SACCEPTED, wxStaticText)->SetLabel( wxT("- / -") );
118 // Transferred
119 m_bar_transfer->SetValue( 0 );
120 CastChild(IDC_STRANSFERRED, wxStaticText)->SetLabel( wxT("- / -") );
122 } else {
123 // Create a total statistic for the selected item(s)
124 uint32 session_requests = 0;
125 uint32 session_accepted = 0;
126 uint64 session_transferred = 0;
127 uint32 all_requests = 0;
128 uint32 all_accepted = 0;
129 uint64 all_transferred = 0;
131 long index = -1;
133 fileVector.reserve(sharedfilesctrl->GetSelectedItemCount());
135 while ( (index = sharedfilesctrl->GetNextItem( index, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED )) != -1) {
136 if ( sharedfilesctrl->GetItemState( index, wxLIST_STATE_SELECTED ) ) {
137 CKnownFile* file = (CKnownFile*)sharedfilesctrl->GetItemData( index );
138 wxASSERT(file);
140 session_requests += file->statistic.GetRequests();
141 session_accepted += file->statistic.GetAccepts();
142 session_transferred += file->statistic.GetTransferred();
144 all_requests += file->statistic.GetAllTimeRequests();
145 all_accepted += file->statistic.GetAllTimeAccepts();
146 all_transferred += file->statistic.GetAllTimeTransferred();
148 fileVector.push_back(file);
152 std::sort(fileVector.begin(), fileVector.end());
154 // Store text lengths, and layout() when the texts have grown
155 static uint32 lReq = 0, lAcc = 0, lTrans = 0;
156 // Requests
157 session_requests = session_requests > lRequested ? lRequested : session_requests;
158 m_bar_requests->SetValue( session_requests );
159 wxString labelReq = CFormat(wxT("%d / %d")) % session_requests % all_requests;
160 CastChild(IDC_SREQUESTED, wxStaticText)->SetLabel(labelReq);
162 // Accepted requets
163 session_accepted = session_accepted > lAccepted ? lAccepted : session_accepted;
164 m_bar_accepted->SetValue( session_accepted );
165 wxString labelAcc = CFormat(wxT("%d / %d")) % session_accepted % all_accepted;
166 CastChild(IDC_SACCEPTED, wxStaticText)->SetLabel(labelAcc);
168 // Transferred
169 session_transferred = session_transferred > lTransferred ? lTransferred : session_transferred;
170 m_bar_transfer->SetValue( session_transferred / 1024 );
171 wxString labelTrans = CastItoXBytes( session_transferred ) + wxT(" / ") + CastItoXBytes( all_transferred );
172 CastChild(IDC_STRANSFERRED, wxStaticText)->SetLabel(labelTrans);
174 if (labelReq.Len() > lReq || labelAcc.Len() > lAcc || labelTrans.Len() > lTrans) {
175 lReq = labelReq.Len();
176 lAcc = labelAcc.Len();
177 lTrans = labelTrans.Len();
178 s_sharedfilespeerHeader->Layout();
182 this->peerslistctrl->ShowSources(fileVector);
184 Refresh();
185 Layout();
190 void CSharedFilesWnd::OnBtnReloadShared( wxCommandEvent& WXUNUSED(evt) )
192 theApp->sharedfiles->Reload();
193 #ifndef CLIENT_GUI
194 // remote gui will update display when data is back
195 SelectionUpdated();
196 #endif
200 void CSharedFilesWnd::OnItemSelectionChanged(wxListEvent& evt)
202 SelectionUpdated();
204 evt.Skip();
208 void CSharedFilesWnd::RemoveAllSharedFiles() {
209 sharedfilesctrl->DeleteAllItems();
210 sharedfilesctrl->ShowFilesCount();
211 peerslistctrl->DeleteAllItems();
212 SelectionUpdated();
215 void CSharedFilesWnd::Prepare()
217 if (m_prepared) {
218 return;
220 m_prepared = true;
221 wxSplitterWindow* splitter = CastChild( wxT("sharedsplitterWnd"), wxSplitterWindow );
222 int height = splitter->GetSize().GetHeight();
223 int header_height = s_sharedfilespeerHeader->GetSize().GetHeight();
225 if ( m_splitter ) {
226 // Some sanity checking
227 if ( m_splitter < 90 ) {
228 m_splitter = 90;
229 } else if ( m_splitter > height - header_height * 2 ) {
230 m_splitter = height - header_height * 2;
232 splitter->SetSashPosition( m_splitter );
233 m_splitter = 0;
236 if ( !peerslistctrl->GetShowing() ) {
237 // use a toggle event to close it (calculate size, change button)
238 peerslistctrl->SetShowing( true ); // so it will be toggled to false
239 wxCommandEvent evt1;
240 OnToggleClientList( evt1 );
244 void CSharedFilesWnd::OnToggleClientList(wxCommandEvent& WXUNUSED(evt))
246 wxSplitterWindow* splitter = CastChild( wxT("sharedsplitterWnd"), wxSplitterWindow );
247 wxBitmapButton* button = CastChild( ID_SHAREDCLIENTTOGGLE, wxBitmapButton );
249 if ( !peerslistctrl->GetShowing() ) {
250 splitter->SetSashPosition( m_splitter );
251 m_splitter = 0;
253 peerslistctrl->SetShowing( true );
255 button->SetBitmapLabel( amuleDlgImages( 10 ) );
256 button->SetBitmapFocus( amuleDlgImages( 10 ) );
257 button->SetBitmapSelected( amuleDlgImages( 10 ) );
258 button->SetBitmapHover( amuleDlgImages( 10 ) );
259 } else {
260 peerslistctrl->SetShowing( false );
262 m_splitter = splitter->GetSashPosition();
264 // Add the height of the listctrl to the top-window
265 int height = peerslistctrl->GetSize().GetHeight()
266 + splitter->GetWindow1()->GetSize().GetHeight();
268 splitter->SetSashPosition( height );
270 button->SetBitmapLabel( amuleDlgImages( 11 ) );
271 button->SetBitmapFocus( amuleDlgImages( 11 ) );
272 button->SetBitmapSelected( amuleDlgImages( 11 ) );
273 button->SetBitmapHover( amuleDlgImages( 11 ) );
277 void CSharedFilesWnd::OnSashPositionChanging(wxSplitterEvent& evt)
279 if ( evt.GetSashPosition() < 90 ) {
280 evt.SetSashPosition( 90 );
281 } else {
282 wxSplitterWindow* splitter = wxStaticCast( evt.GetEventObject(), wxSplitterWindow);
283 wxCHECK_RET(splitter, wxT("ERROR: NULL splitter in CSharedFilesWnd::OnSashPositionChanging"));
285 int height = splitter->GetSize().GetHeight();
286 int header_height = s_sharedfilespeerHeader->GetSize().GetHeight();
287 int mousey = wxGetMousePosition().y - splitter->GetScreenRect().GetTop();
289 if ( !peerslistctrl->GetShowing() ) {
290 // lower window hidden
291 if ( height - mousey < header_height * 2 ) {
292 // no moving down if already hidden
293 evt.Veto();
294 } else {
295 // show it
296 m_splitter = mousey; // prevent jumping if it was minimized and is then shown by dragging the sash
297 wxCommandEvent evt1;
298 OnToggleClientList( evt1 );
300 } else {
301 // lower window showing
302 if ( height - mousey < header_height * 2 ) {
303 // hide it
304 wxCommandEvent evt1;
305 OnToggleClientList( evt1 );
306 } else {
307 // normal resize
308 // If several events queue up, setting the sash to the current mouse position
309 // will speed up things and make sash moving more smoothly.
310 evt.SetSashPosition( mousey );
315 // File_checked_for_headers