Improve speed of category tab title updates
[amule.git] / src / SearchListCtrl.cpp
blob5ce32756f0a21f68fb58bd7a72993f6cdef05def
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 "SearchListCtrl.h" // Interface declarations
28 #include <common/MenuIDs.h>
30 #include "amule.h" // Needed for theApp
31 #include "KnownFileList.h" // Needed for CKnownFileList
32 #include "SearchList.h" // Needed for CSearchFile
33 #include "SearchDlg.h" // Needed for CSearchDlg
34 #include "amuleDlg.h" // Needed for CamuleDlg
35 #include "muuli_wdr.h" // Needed for clientImages
36 #include "Preferences.h" // Needed for thePrefs
37 #include "GuiEvents.h" // Needed for CoreNotify_Search_Add_Download
38 #include "MuleColour.h"
40 BEGIN_EVENT_TABLE(CSearchListCtrl, CMuleListCtrl)
41 EVT_LIST_ITEM_RIGHT_CLICK(-1, CSearchListCtrl::OnRightClick)
42 EVT_LIST_COL_CLICK( -1, CSearchListCtrl::OnColumnLClick)
43 EVT_LIST_COL_END_DRAG( -1, CSearchListCtrl::OnColumnResize)
45 EVT_MENU( MP_GETED2KLINK, CSearchListCtrl::OnPopupGetUrl)
46 EVT_MENU( MP_RAZORSTATS, CSearchListCtrl::OnRazorStatsCheck)
47 EVT_MENU( MP_SEARCHRELATED, CSearchListCtrl::OnRelatedSearch)
48 EVT_MENU( MP_MARK_AS_KNOWN, CSearchListCtrl::OnMarkAsKnown)
49 EVT_MENU( MP_RESUME, CSearchListCtrl::OnPopupDownload)
50 EVT_MENU_RANGE( MP_ASSIGNCAT, MP_ASSIGNCAT + 99, CSearchListCtrl::OnPopupDownload )
52 EVT_LIST_ITEM_ACTIVATED( -1, CSearchListCtrl::OnItemActivated)
53 END_EVENT_TABLE()
56 std::list<CSearchListCtrl*> CSearchListCtrl::s_lists;
59 enum SearchListColumns {
60 ID_SEARCH_COL_NAME = 0,
61 ID_SEARCH_COL_SIZE,
62 ID_SEARCH_COL_SOURCES,
63 ID_SEARCH_COL_TYPE,
64 ID_SEARCH_COL_FILEID,
65 ID_SEARCH_COL_STATUS
69 CSearchListCtrl::CSearchListCtrl(
70 wxWindow *parent,
71 wxWindowID winid,
72 const wxPoint &pos,
73 const wxSize &size,
74 long style,
75 const wxValidator &validator,
76 const wxString &name)
78 CMuleListCtrl(parent, winid, pos, size, style | wxLC_OWNERDRAW, validator, name),
79 m_filterKnown(false),
80 m_invert(false),
81 m_filterEnabled(false)
83 // Setting the sorter function.
84 SetSortFunc( SortProc );
86 InsertColumn( ID_SEARCH_COL_NAME, _("File Name"), wxLIST_FORMAT_LEFT, 500, wxT("N") );
87 InsertColumn( ID_SEARCH_COL_SIZE, _("Size"), wxLIST_FORMAT_LEFT, 100, wxT("Z") );
88 InsertColumn( ID_SEARCH_COL_SOURCES, _("Sources"), wxLIST_FORMAT_LEFT, 50, wxT("u") );
89 InsertColumn( ID_SEARCH_COL_TYPE, _("Type"), wxLIST_FORMAT_LEFT, 65, wxT("Y") );
90 InsertColumn( ID_SEARCH_COL_FILEID, _("FileID"), wxLIST_FORMAT_LEFT, 280, wxT("I") );
91 InsertColumn( ID_SEARCH_COL_STATUS, _("Status"), wxLIST_FORMAT_LEFT, 100, wxT("S") );
93 m_nResultsID = 0;
95 // Only load settings for first list, otherwise sync with current lists
96 if ( s_lists.empty() ) {
97 // Set the name to enable loading of settings
98 SetTableName( wxT("Search") );
100 LoadSettings();
102 // Unset the name to avoid the settings getting saved every time a list is closed
103 SetTableName( wxEmptyString );
104 } else {
105 // Sync this list with one of the others
106 SyncLists( s_lists.front(), this );
109 // Add the list so that it will be synced with the other lists
110 s_lists.push_back( this );
114 wxString CSearchListCtrl::GetOldColumnOrder() const
116 return wxT("N,Z,u,Y,I,S");
120 CSearchListCtrl::~CSearchListCtrl()
122 std::list<CSearchListCtrl*>::iterator it = std::find( s_lists.begin(), s_lists.end(), this );
124 if ( it != s_lists.end() )
125 s_lists.erase( it );
127 // We only save the settings if the last list was closed
128 if ( s_lists.empty() ) {
129 // In order to get the settings saved, we need to set the name
130 SetTableName( wxT("Search") );
135 void CSearchListCtrl::AddResult(CSearchFile* toshow)
137 wxCHECK_RET(toshow->GetSearchID() == m_nResultsID, wxT("Wrong search-id for result-list"));
139 const wxUIntPtr toshowdata = reinterpret_cast<wxUIntPtr>(toshow);
141 // Check if the result should be shown
142 if (FindItem(-1, toshowdata) != -1) {
143 return;
144 } else if (toshow->GetParent() && !toshow->GetParent()->ShowChildren()) {
145 return;
146 } else if (!IsFiltered(toshow)) {
147 if (toshow->HasChildren() && toshow->ShowChildren()) {
148 // Only filter the parent if none of the children are shown.
149 bool foundChild = false;
150 const CSearchResultList& children = toshow->GetChildren();
151 for (size_t i = 0; i < children.size(); ++i) {
152 if (IsFiltered(children.at(i))) {
153 foundChild = true;
154 break;
158 if (!foundChild) {
159 // No children left, and the parent is filtered.
160 m_filteredOut.push_back(toshow);
161 return;
163 } else {
164 m_filteredOut.push_back(toshow);
165 return;
169 // Insert the item before the item found by the search
170 uint32 newid = InsertItem(GetInsertPos(toshowdata), toshow->GetFileName().GetPrintable());
172 // Sanity checks to ensure that results/children are properly positioned.
173 #ifdef __WXDEBUG__
175 CSearchFile* parent = toshow->GetParent();
177 if (newid > 0) {
178 CSearchFile* before = (CSearchFile*)GetItemData(newid - 1);
179 wxASSERT(before);
180 if (parent) {
181 wxASSERT((before->GetParent() == parent) || (before == parent));
182 } else {
183 wxASSERT(before->GetParent() != toshow);
187 if ((int)newid < GetItemCount() - 1) {
188 CSearchFile* after = (CSearchFile*)GetItemData(newid + 1);
189 wxASSERT(after);
190 if (parent) {
191 wxASSERT((after->GetParent() == parent) || (!after->GetParent()));
192 } else {
193 wxASSERT((after->GetParent() == toshow) || (!after->GetParent()));
197 #endif
199 SetItemPtrData(newid, toshowdata);
201 // Filesize
202 SetItem(newid, ID_SEARCH_COL_SIZE, CastItoXBytes( toshow->GetFileSize() ) );
204 // Source count
205 wxString temp = CFormat(wxT("%d")) % toshow->GetSourceCount();
206 if (toshow->GetCompleteSourceCount()) {
207 temp += CFormat(wxT(" (%d)")) % toshow->GetCompleteSourceCount();
209 if (toshow->GetClientsCount()) {
210 temp += CFormat(wxT(" [%d]")) % toshow->GetClientsCount();
212 #if defined(__DEBUG__) && !defined(CLIENT_GUI)
213 if (toshow->GetKadPublishInfo() == 0) {
214 temp += wxT(" | -");
215 } else {
216 temp += CFormat(wxT(" | N:%u, P:%u, T:%0.2f"))
217 % ((toshow->GetKadPublishInfo() & 0xFF000000) >> 24)
218 % ((toshow->GetKadPublishInfo() & 0x00FF0000) >> 16)
219 % ((toshow->GetKadPublishInfo() & 0x0000FFFF) / 100.0);
221 #endif
222 SetItem( newid, ID_SEARCH_COL_SOURCES, temp );
224 // File-type
225 SetItem( newid, ID_SEARCH_COL_TYPE, GetFiletypeByName( toshow->GetFileName() ) );
227 // File-hash
228 SetItem(newid, ID_SEARCH_COL_FILEID, toshow->GetFileHash().Encode() );
230 // File status
231 SetItem(newid, ID_SEARCH_COL_STATUS, DetermineStatusPrintable(toshow));
233 // Set the color of the item
234 UpdateItemColor( newid );
238 void CSearchListCtrl::RemoveResult(CSearchFile* toremove)
240 ShowChildren(toremove, false);
242 long index = FindItem(-1, reinterpret_cast<wxUIntPtr>(toremove));
243 if (index != -1) {
244 DeleteItem(index);
245 } else {
246 ResultList::iterator it = std::find(m_filteredOut.begin(), m_filteredOut.end(), toremove);
247 if ( it != m_filteredOut.end()) {
248 m_filteredOut.erase(it);
254 void CSearchListCtrl::UpdateResult(CSearchFile* toupdate)
256 long index = FindItem(-1, reinterpret_cast<wxUIntPtr>(toupdate));
257 if (index != -1) {
258 // Update the filename, which may be changed in case of multiple variants.
259 SetItem(index, ID_SEARCH_COL_NAME, toupdate->GetFileName().GetPrintable());
261 wxString temp = CFormat(wxT("%d")) % toupdate->GetSourceCount();
262 if (toupdate->GetCompleteSourceCount()) {
263 temp += CFormat(wxT(" (%d)")) % toupdate->GetCompleteSourceCount();
265 if (toupdate->GetClientsCount()) {
266 temp += CFormat(wxT(" [%d]")) % toupdate->GetClientsCount();
268 #if defined(__DEBUG__) && !defined(CLIENT_GUI)
269 if (toupdate->GetKadPublishInfo() == 0) {
270 temp += wxT(" | -");
271 } else {
272 temp += CFormat(wxT(" | N:%u, P:%u, T:%0.2f"))
273 % ((toupdate->GetKadPublishInfo() & 0xFF000000) >> 24)
274 % ((toupdate->GetKadPublishInfo() & 0x00FF0000) >> 16)
275 % ((toupdate->GetKadPublishInfo() & 0x0000FFFF) / 100.0);
277 #endif
278 SetItem(index, ID_SEARCH_COL_SOURCES, temp);
280 SetItem(index, ID_SEARCH_COL_STATUS, DetermineStatusPrintable(toupdate));
282 UpdateItemColor(index);
284 // Deletions of items causes rather large amount of flicker, so to
285 // avoid this, we resort the list to ensure correct ordering.
286 if (!IsItemSorted(index)) {
287 SortList();
293 void CSearchListCtrl::UpdateItemColor(long index)
295 wxListItem item;
296 item.SetId( index );
297 item.SetColumn( ID_SEARCH_COL_SIZE );
298 item.SetMask(
299 wxLIST_MASK_STATE |
300 wxLIST_MASK_TEXT |
301 wxLIST_MASK_IMAGE |
302 wxLIST_MASK_DATA |
303 wxLIST_MASK_WIDTH |
304 wxLIST_MASK_FORMAT);
306 if (GetItem(item)) {
307 CMuleColour newcol(wxSYS_COLOUR_WINDOWTEXT);
309 CSearchFile* file = (CSearchFile*)GetItemData(index);
311 int red = newcol.Red();
312 int green = newcol.Green();
313 int blue = newcol.Blue();
315 switch (file->GetDownloadStatus()) {
316 case CSearchFile::DOWNLOADED:
317 // File has already been downloaded. Mark as green.
318 green = 255;
319 break;
320 case CSearchFile::QUEUED:
321 // File is downloading.
322 case CSearchFile::QUEUEDCANCELED:
323 // File is downloading and has been canceled before.
324 // Mark as red
325 red = 255;
326 break;
327 case CSearchFile::CANCELED:
328 // File has been canceled. Mark as magenta.
329 red = 255;
330 blue = 255;
331 break;
332 default:
333 // File is new, colour after number of files
334 blue += file->GetSourceCount() * 5;
335 if ( blue > 255 ) {
336 blue = 255;
340 // don't forget to set the item data back...
341 wxListItem newitem;
342 newitem.SetId( index );
343 newitem.SetTextColour( wxColour( red, green, blue ) );
344 SetItem( newitem );
349 void CSearchListCtrl::ShowResults( long ResultsID )
351 DeleteAllItems();
352 m_nResultsID = ResultsID;
353 if (ResultsID) {
354 const CSearchResultList& list = theApp->searchlist->GetSearchResults(ResultsID);
355 for (unsigned int i = 0; i < list.size(); ++i) {
356 AddResult( list[i] );
362 wxUIntPtr CSearchListCtrl::GetSearchId()
364 return m_nResultsID;
368 void CSearchListCtrl::SetFilter(const wxString& regExp, bool invert, bool filterKnown)
370 if (regExp.IsEmpty()) {
371 // Show everything
372 m_filterText = wxT(".*");
373 } else {
374 m_filterText = regExp;
377 m_filter.Compile(m_filterText, wxRE_DEFAULT | wxRE_ICASE);
378 m_filterKnown = filterKnown;
379 m_invert = invert;
381 if (m_filterEnabled) {
382 // Swap the list of filtered results so we can freely add new items to the list
383 ResultList curFiltered;
384 std::swap(curFiltered, m_filteredOut);
386 // Filter items already on the list
387 for (int i = 0; i < GetItemCount();) {
388 CSearchFile* file = (CSearchFile*)GetItemData(i);
390 if (IsFiltered(file)) {
391 ++i;
392 } else {
393 m_filteredOut.push_back(file);
394 DeleteItem(i);
398 // Check the previously filtered items.
399 ResultList::iterator it = curFiltered.begin();
400 for (; it != curFiltered.end(); ++it) {
401 if (IsFiltered(*it)) {
402 AddResult(*it);
403 } else {
404 m_filteredOut.push_back(*it);
411 void CSearchListCtrl::EnableFiltering(bool enabled)
413 if (enabled != m_filterEnabled) {
414 m_filterEnabled = enabled;
416 if (enabled) {
417 SetFilter(m_filterText, m_invert, m_filterKnown);
418 } else {
419 ResultList::iterator it = m_filteredOut.begin();
420 for (; it != m_filteredOut.end(); ++it) {
421 AddResult(*it);
424 m_filteredOut.clear();
430 size_t CSearchListCtrl::GetHiddenItemCount() const
432 return m_filteredOut.size();
436 bool CSearchListCtrl::IsFiltered(const CSearchFile* file)
438 // By default, everything is displayed
439 bool result = true;
441 if (m_filterEnabled && m_filter.IsValid()) {
442 result = m_filter.Matches(file->GetFileName().GetPrintable());
443 result = ((result && !m_invert) || (!result && m_invert));
444 if (result && m_filterKnown) {
445 result = file->GetDownloadStatus() == CSearchFile::NEW;
449 return result;
453 int CSearchListCtrl::SortProc(wxUIntPtr item1, wxUIntPtr item2, long sortData)
455 CSearchFile* file1 = (CSearchFile*)item1;
456 CSearchFile* file2 = (CSearchFile*)item2;
458 // Modifies the result, 1 for ascending, -1 for decending
459 int modifier = (sortData & CMuleListCtrl::SORT_DES) ? -1 : 1;
460 bool alternate = (sortData & CMuleListCtrl::SORT_ALT) != 0;
462 // Decide if which should files we should sort by.
463 wxUIntPtr parent1 = reinterpret_cast<wxUIntPtr>(file1->GetParent());
464 wxUIntPtr parent2 = reinterpret_cast<wxUIntPtr>(file2->GetParent());
465 wxUIntPtr filePtr1 = reinterpret_cast<wxUIntPtr>(file1);
466 wxUIntPtr filePtr2 = reinterpret_cast<wxUIntPtr>(file2);
467 if (parent1 && parent2) {
468 if (parent1 != parent2) {
469 return SortProc(parent1, parent2, sortData);
471 } else if (parent1) {
472 if (parent1 == filePtr2) {
473 return 1;
474 } else {
475 return SortProc(parent1, filePtr2, sortData);
477 } else if (parent2) {
478 if (parent2 == filePtr1) {
479 return -1;
480 } else {
481 return SortProc(filePtr1, parent2, sortData);
485 int result = 0;
486 switch (sortData & CMuleListCtrl::COLUMN_MASK) {
487 // Sort by filename
488 case ID_SEARCH_COL_NAME:
489 result = CmpAny(file1->GetFileName(), file2->GetFileName());
490 break;
492 // Sort file-size
493 case ID_SEARCH_COL_SIZE:
494 result = CmpAny( file1->GetFileSize(), file2->GetFileSize() );
495 break;
497 // Sort by sources
498 case ID_SEARCH_COL_SOURCES: {
499 int cmp = CmpAny( file1->GetSourceCount(), file2->GetSourceCount() );
500 int cmp2 = CmpAny( file1->GetCompleteSourceCount(), file2->GetCompleteSourceCount() );
502 if ( alternate ) {
503 // Swap criterias
504 int temp = cmp2;
505 cmp2 = cmp;
506 cmp = temp;
509 if ( cmp == 0 ) {
510 cmp = cmp2;
513 result = cmp;
514 break;
517 // Sort by file-types
518 case ID_SEARCH_COL_TYPE: {
519 result = GetFiletypeByName(file1->GetFileName()).Cmp(GetFiletypeByName(file2->GetFileName()));
520 if (result == 0) {
521 // Same file-type, sort by extension
522 result = CmpAny(file1->GetFileName().GetExt(), file2->GetFileName().GetExt());
525 break;
528 // Sort by file-hash
529 case ID_SEARCH_COL_FILEID:
530 result = CmpAny(file2->GetFileHash(), file1->GetFileHash());
531 break;
533 // Sort by file status
534 case ID_SEARCH_COL_STATUS:
535 result = CmpAny(DetermineStatusPrintable(file2), DetermineStatusPrintable(file1));
536 break;
539 return modifier * result;
543 void CSearchListCtrl::SyncLists( CSearchListCtrl* src, CSearchListCtrl* dst )
545 wxCHECK_RET(src && dst, wxT("NULL argument in SyncLists"));
547 // Column widths
548 for ( int i = 0; i < src->GetColumnCount(); i++ ) {
549 // We do this check since just setting the width causes a redraw
550 if ( dst->GetColumnWidth( i ) != src->GetColumnWidth( i ) ) {
551 dst->SetColumnWidth( i, src->GetColumnWidth( i ) );
555 // Sync sorting
556 unsigned column = src->GetSortColumn();
557 unsigned order = src->GetSortOrder();
558 if (column != dst->GetSortColumn() || order != dst->GetSortOrder()) {
559 dst->SetSorting(column, order);
564 void CSearchListCtrl::SyncOtherLists(CSearchListCtrl *src)
566 std::list<CSearchListCtrl*>::iterator it;
568 for (it = s_lists.begin(); it != s_lists.end(); ++it) {
569 if ((*it) != src) {
570 SyncLists( src, *it );
576 void CSearchListCtrl::OnRightClick(wxListEvent& event)
578 CheckSelection(event);
580 if (GetSelectedItemCount()) {
581 // Create the popup-menu
582 wxMenu menu(_("File"));
583 menu.Append(MP_RESUME, _("Download"));
585 wxMenu* cats = new wxMenu(_("Category"));
586 cats->Append(MP_ASSIGNCAT, _("Main"));
587 for (unsigned i = 1; i < theApp->glob_prefs->GetCatCount(); i++) {
588 cats->Append(MP_ASSIGNCAT + i,
589 theApp->glob_prefs->GetCategory(i)->title);
592 menu.Append(MP_MENU_CATS, _("Download in category"), cats);
593 menu.AppendSeparator();
594 /* Commented out while it's gone
595 menu.Append(MP_RAZORSTATS, _("Get Razorback 2's stats for this file"));
596 menu.AppendSeparator();
598 menu.Append(MP_SEARCHRELATED, _("Search related files (eD2k, local server)"));
599 menu.AppendSeparator();
601 //#warning Uncomment this here to test the MP_MARK_AS_KNOWN feature. Beware! You are on your own here, this might break "known.met"
602 #if 0
603 menu.Append(MP_MARK_AS_KNOWN, _("Mark as known file"));
604 menu.AppendSeparator();
605 #endif
607 menu.Append(MP_GETED2KLINK, _("Copy eD2k link to clipboard"));
609 // These should only be enabled for single-selections
610 bool enable = (GetSelectedItemCount() == 1);
611 menu.Enable(MP_GETED2KLINK, enable);
612 menu.Enable(MP_MENU_CATS, (theApp->glob_prefs->GetCatCount() > 1));
614 PopupMenu(&menu, event.GetPoint());
615 } else {
616 event.Skip();
621 void CSearchListCtrl::OnColumnLClick( wxListEvent& event )
623 // Let the real event handler do its work first
624 CMuleListCtrl::OnColumnLClick( event );
626 SyncOtherLists( this );
630 void CSearchListCtrl::OnColumnResize( wxListEvent& WXUNUSED(event) )
632 SyncOtherLists( this );
636 void CSearchListCtrl::OnPopupGetUrl( wxCommandEvent& WXUNUSED(event) )
638 wxString URIs;
640 long index = GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
642 while (index != -1) {
643 CSearchFile* file = (CSearchFile*)GetItemData( index );
645 URIs += theApp->CreateED2kLink( file ) + wxT("\n");
647 index = GetNextItem( index, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
650 if (!URIs.IsEmpty()) {
651 theApp->CopyTextToClipboard( URIs.RemoveLast() );
656 void CSearchListCtrl::OnRazorStatsCheck( wxCommandEvent& WXUNUSED(event) )
658 int item = GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
659 if (item == -1) {
660 return;
663 CSearchFile* file = (CSearchFile*)GetItemData( item );
664 theApp->amuledlg->LaunchUrl(wxT("http://stats.razorback2.com/ed2khistory?ed2k=") + file->GetFileHash().Encode());
668 void CSearchListCtrl::OnRelatedSearch( wxCommandEvent& WXUNUSED(event) )
670 int item = GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
671 if (item == -1) {
672 return;
675 CSearchFile* file = (CSearchFile*)GetItemData( item );
676 theApp->searchlist->StopSearch(true);
677 theApp->amuledlg->m_searchwnd->ResetControls();
678 CastByID( IDC_SEARCHNAME, theApp->amuledlg->m_searchwnd, wxTextCtrl )->
679 SetValue(wxT("related::") + file->GetFileHash().Encode());
680 theApp->amuledlg->m_searchwnd->StartNewSearch();
684 void CSearchListCtrl::OnMarkAsKnown( wxCommandEvent& WXUNUSED(event) )
686 #ifndef CLIENT_GUI
687 long index = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
688 while (index > -1) {
689 CSearchFile *searchFile = (CSearchFile *)GetItemData(index);
690 CKnownFile *knownFile(new CKnownFile(*searchFile));
691 theApp->knownfiles->SafeAddKFile(knownFile);
692 index = GetNextItem(index, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
694 #endif
698 void CSearchListCtrl::OnPopupDownload(wxCommandEvent& event)
700 if (event.GetId() == MP_RESUME) {
701 // Via the "Download" menu-item, use category specified in drop-down menu
702 DownloadSelected();
703 } else {
704 // Via an "Download in category" item
705 DownloadSelected(event.GetId() - MP_ASSIGNCAT);
710 void CSearchListCtrl::OnItemActivated(wxListEvent& event)
712 CSearchFile* file = ((CSearchFile*)GetItemData(event.GetIndex()));
713 if (file->HasChildren()) {
714 ShowChildren(file, !file->ShowChildren());
715 } else {
716 DownloadSelected();
721 bool CSearchListCtrl::AltSortAllowed(unsigned column) const
723 switch (column) {
724 case ID_SEARCH_COL_SOURCES:
725 return true;
726 default:
727 return false;
732 void CSearchListCtrl::DownloadSelected(int category)
734 FindWindowById(IDC_SDOWNLOAD)->Enable(FALSE);
736 // Either the "Download" menu-item, the download-button, double-click or enter
737 if (category == -1) {
738 // Defaults to main category
739 category = 0;
741 if (CastByID(IDC_EXTENDEDSEARCHCHECK, NULL, wxCheckBox)->GetValue()) {
742 category = CastByID(ID_AUTOCATASSIGN, NULL, wxChoice)->GetSelection();
746 // Process all selections
747 long index = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
748 while (index > -1) {
749 CSearchFile* file = (CSearchFile*)GetItemData(index);
750 CoreNotify_Search_Add_Download(file, category);
751 index = GetNextItem(index, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
753 // Listcontrol gets updated by notification when download is started
757 const wxBrush& GetBrush(wxSystemColour index)
759 return CMuleColour(index).GetBrush();
763 void CSearchListCtrl::OnDrawItem(
764 int item, wxDC* dc, const wxRect& rect, const wxRect& rectHL, bool highlighted)
766 CSearchFile* file = (CSearchFile*)GetItemData(item);
768 // Define text-color and background
769 if (highlighted) {
770 if (GetFocus()) {
771 dc->SetBackground(GetBrush(wxSYS_COLOUR_HIGHLIGHT));
772 dc->SetTextForeground(CMuleColour(wxSYS_COLOUR_HIGHLIGHTTEXT));
773 } else {
774 dc->SetBackground(GetBrush(wxSYS_COLOUR_BTNSHADOW));
775 dc->SetTextForeground(CMuleColour(wxSYS_COLOUR_HIGHLIGHTTEXT));
777 } else {
778 dc->SetBackground(GetBrush(wxSYS_COLOUR_LISTBOX));
779 dc->SetTextForeground(CMuleColour(wxSYS_COLOUR_WINDOWTEXT));
782 // Define the border of the drawn area
783 if (highlighted) {
784 dc->SetPen(*(wxThePenList->FindOrCreatePen(CMuleColour(dc->GetBackground().GetColour()).Blend(65), 1, wxSOLID)));
785 } else {
786 dc->SetPen(*wxTRANSPARENT_PEN);
787 dc->SetTextForeground(GetItemTextColour(item));
790 // Clear the background, not done automatically since the drawing is buffered.
791 dc->SetBrush( dc->GetBackground() );
792 dc->DrawRectangle( rectHL.x, rectHL.y, rectHL.width, rectHL.height );
794 // Various constant values we use
795 const int iTextOffset = ( rect.GetHeight() - dc->GetCharHeight() ) / 2;
796 const int iOffset = 4;
797 const int treeOffset = 11;
798 const int treeCenter = 6;
799 bool tree_show = false;
801 wxRect cur_rec(iOffset, rect.y, 0, rect.height );
802 for (int i = 0; i < GetColumnCount(); i++) {
803 wxListItem listitem;
804 GetColumn(i, listitem);
806 if ( listitem.GetWidth() > 0 ) {
807 cur_rec.width = listitem.GetWidth() - 2*iOffset;
809 // Make a copy of the current rectangle so we can apply specific tweaks
810 wxRect target_rec = cur_rec;
812 // will ensure that text is about in the middle ;)
813 target_rec.y += iTextOffset;
815 if (i == 0) {
816 if (file->HasChildren() || file->GetParent()) {
817 tree_show = (listitem.GetWidth() > 0);
818 target_rec.x += treeOffset;
819 target_rec.width -= treeOffset;
821 // Children are indented a bit
822 if (file->GetParent()) {
823 target_rec.x += 4;
824 target_rec.width -= 4;
828 // Check if the rating icon should be drawn
829 if (file->HasRating()) {
830 int image = Client_InvalidRating_Smiley + file->UserRating() - 1;
832 int imgWidth = 16;
834 theApp->amuledlg->m_imagelist.Draw(image, *dc, target_rec.GetX(),
835 target_rec.GetY() - 1, wxIMAGELIST_DRAW_TRANSPARENT);
837 // Move the text past the icon.
838 target_rec.x += imgWidth + 4;
839 target_rec.width -= imgWidth + 4;
843 wxListItem cellitem;
844 cellitem.SetColumn(i);
845 cellitem.SetId(item);
847 // Force clipper (clip 2 px more than the rectangle from the right side)
848 wxDCClipper clipper(*dc, target_rec.x, target_rec.y, target_rec.width - 2, target_rec.height);
850 if (GetItem(cellitem)) {
851 dc->DrawText(cellitem.GetText(), target_rec.GetX(), target_rec.GetY());
852 } else {
853 dc->DrawText(wxT("GetItem failed!"), target_rec.GetX(), target_rec.GetY());
856 // Increment to the next column
857 cur_rec.x += listitem.GetWidth();
861 // Draw tree last so it draws over selected and focus (looks better)
862 if (tree_show) {
863 // Gather some information
864 const bool notLast = (item + 1 < GetItemCount());
865 const bool notFirst = (item != 0);
866 const bool hasNext = notLast && ((CSearchFile*)GetItemData(item + 1))->GetParent();
867 const int middle = cur_rec.y + ( cur_rec.height + 1 ) / 2;
869 // Set up a new pen for drawing the tree
870 dc->SetPen( *(wxThePenList->FindOrCreatePen(dc->GetTextForeground(), 1, wxSOLID)) );
872 if (file->GetParent()) {
873 // Draw the line to the filename
874 dc->DrawLine(treeCenter, middle, treeOffset + 4, middle);
876 // Draw the line to the child node
877 if (hasNext) {
878 dc->DrawLine(treeCenter, middle, treeCenter, cur_rec.y + cur_rec.height + 1);
881 // Draw the line back up to parent node
882 if (notFirst) {
883 dc->DrawLine(treeCenter, middle, treeCenter, cur_rec.y - 1);
885 } else if (file->HasChildren()) {
886 if (file->ShowChildren()) {
887 // Draw empty circle
888 dc->SetBrush(*wxTRANSPARENT_BRUSH);
889 } else {
890 dc->SetBrush(*(wxTheBrushList->FindOrCreateBrush(GetItemTextColour(item))));
893 dc->DrawCircle( treeCenter, middle, 3 );
895 // Draw the line to the child node if there are any children
896 if (hasNext && file->ShowChildren()) {
897 dc->DrawLine(treeCenter, middle + 3, treeCenter, cur_rec.y + cur_rec.height + 1);
902 // Sanity checks to ensure that results/children are properly positioned.
903 #ifdef __WXDEBUG__
905 CSearchFile* parent = file->GetParent();
907 if (item > 0) {
908 CSearchFile* before = (CSearchFile*)GetItemData(item - 1);
909 wxASSERT(before);
910 if (parent) {
911 wxASSERT((before->GetParent() == parent) || (before == parent));
912 } else {
913 wxASSERT(before->GetParent() != file);
917 if (item < GetItemCount() - 1) {
918 CSearchFile* after = (CSearchFile*)GetItemData(item + 1);
919 wxASSERT(after);
920 if (parent) {
921 wxASSERT((after->GetParent() == parent) || (!after->GetParent()));
922 } else {
923 wxASSERT((after->GetParent() == file) || (!after->GetParent()));
927 #endif
931 void CSearchListCtrl::ShowChildren(CSearchFile* file, bool show)
933 Freeze();
935 file->SetShowChildren(show);
937 const CSearchResultList& results = file->GetChildren();
938 for (size_t i = 0; i < results.size(); ++i) {
939 if (show) {
940 AddResult(results[i]);
941 } else {
942 RemoveResult(results[i]);
946 Thaw();
950 wxString CSearchListCtrl::GetTTSText(unsigned item) const
952 return GetItemText(item);
956 wxString CSearchListCtrl::DetermineStatusPrintable(CSearchFile *toshow)
958 switch (toshow->GetDownloadStatus()) {
959 case CSearchFile::DOWNLOADED:
960 // File has already been downloaded.
961 return _("Downloaded");
962 case CSearchFile::QUEUED:
963 // File is downloading.
964 case CSearchFile::QUEUEDCANCELED:
965 // File is downloading and has been canceled before.
966 return _("Queued");
967 case CSearchFile::CANCELED:
968 // File has been canceled.
969 return _("Canceled");
970 default:
971 // File is new.
972 return _("New");
975 // File_checked_for_headers