Upstream tarball 10101
[amule.git] / src / SearchListCtrl.cpp
blobf891fcb26eac502206c05f509e12c675158f394d
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 = wxString::Format(wxT("%d"), toshow->GetSourceCount());
206 if (toshow->GetCompleteSourceCount()) {
207 temp += wxString::Format(wxT(" (%d)"), toshow->GetCompleteSourceCount());
209 if (toshow->GetClientsCount()) {
210 temp += wxString::Format(wxT(" [%d]"), toshow->GetClientsCount());
212 #if defined(__DEBUG__) && !defined(CLIENT_GUI)
213 if (toshow->GetKadPublishInfo() == 0) {
214 temp += wxT(" | -");
215 } else {
216 temp += wxString::Format(wxT(" | N:%u, P:%u, T:%0.2f"), (toshow->GetKadPublishInfo() & 0xFF000000) >> 24, (toshow->GetKadPublishInfo() & 0x00FF0000) >> 16, (toshow->GetKadPublishInfo() & 0x0000FFFF) / 100.0);
218 #endif
219 SetItem( newid, ID_SEARCH_COL_SOURCES, temp );
221 // File-type
222 SetItem( newid, ID_SEARCH_COL_TYPE, GetFiletypeByName( toshow->GetFileName() ) );
224 // File-hash
225 SetItem(newid, ID_SEARCH_COL_FILEID, toshow->GetFileHash().Encode() );
227 // File status
228 SetItem(newid, ID_SEARCH_COL_STATUS, DetermineStatusPrintable(toshow));
230 // Set the color of the item
231 UpdateItemColor( newid );
235 void CSearchListCtrl::RemoveResult(CSearchFile* toremove)
237 ShowChildren(toremove, false);
239 long index = FindItem(-1, reinterpret_cast<wxUIntPtr>(toremove));
240 if (index != -1) {
241 DeleteItem(index);
242 } else {
243 ResultList::iterator it = std::find(m_filteredOut.begin(), m_filteredOut.end(), toremove);
244 if ( it != m_filteredOut.end()) {
245 m_filteredOut.erase(it);
251 void CSearchListCtrl::UpdateResult(CSearchFile* toupdate)
253 long index = FindItem(-1, reinterpret_cast<wxUIntPtr>(toupdate));
254 if (index != -1) {
255 // Update the filename, which may be changed in case of multiple variants.
256 SetItem(index, ID_SEARCH_COL_NAME, toupdate->GetFileName().GetPrintable());
258 wxString temp = wxString::Format(wxT("%d"), toupdate->GetSourceCount());
259 if (toupdate->GetCompleteSourceCount()) {
260 temp += wxString::Format(wxT(" (%d)"), toupdate->GetCompleteSourceCount());
262 if (toupdate->GetClientsCount()) {
263 temp += wxString::Format(wxT(" [%d]"), toupdate->GetClientsCount());
265 #if defined(__DEBUG__) && !defined(CLIENT_GUI)
266 if (toupdate->GetKadPublishInfo() == 0) {
267 temp += wxT(" | -");
268 } else {
269 temp += wxString::Format(wxT(" | N:%u, P:%u, T:%0.2f"), (toupdate->GetKadPublishInfo() & 0xFF000000) >> 24, (toupdate->GetKadPublishInfo() & 0x00FF0000) >> 16, (toupdate->GetKadPublishInfo() & 0x0000FFFF) / 100.0);
271 #endif
272 SetItem(index, ID_SEARCH_COL_SOURCES, temp);
274 SetItem(index, ID_SEARCH_COL_STATUS, DetermineStatusPrintable(toupdate));
276 UpdateItemColor(index);
278 // Deletions of items causes rather large amount of flicker, so to
279 // avoid this, we resort the list to ensure correct ordering.
280 if (!IsItemSorted(index)) {
281 SortList();
287 void CSearchListCtrl::UpdateItemColor( long index )
289 wxListItem item;
290 item.SetId( index );
291 item.SetColumn( ID_SEARCH_COL_SIZE );
292 item.SetMask( wxLIST_MASK_STATE|wxLIST_MASK_TEXT|wxLIST_MASK_IMAGE|wxLIST_MASK_DATA|wxLIST_MASK_WIDTH|wxLIST_MASK_FORMAT );
294 if ( GetItem(item) ) {
295 CMuleColour newcol(wxSYS_COLOUR_WINDOWTEXT);
297 CSearchFile* file = (CSearchFile*)GetItemData(index);
299 int red = newcol.Red();
300 int green = newcol.Green();
301 int blue = newcol.Blue();
303 switch (file->GetDownloadStatus()) {
304 case CSearchFile::DOWNLOADED: // File has already been downloaded. Mark as green.
305 green = 255;
306 break;
307 case CSearchFile::QUEUED: // File is downloading.
308 case CSearchFile::QUEUEDCANCELED: // File is downloading and has been canceled before.
309 // Mark as red
310 red = 255;
311 break;
312 case CSearchFile::CANCELED: // File has been canceled. Mark as magenta.
313 red = 255;
314 blue = 255;
315 break;
316 default: // File is new, colour after number of files
317 blue += file->GetSourceCount() * 5;
318 if ( blue > 255 ) {
319 blue = 255;
323 // don't forget to set the item data back...
324 wxListItem newitem;
325 newitem.SetId( index );
326 newitem.SetTextColour( wxColour( red, green, blue ) );
327 SetItem( newitem );
332 void CSearchListCtrl::ShowResults( long ResultsID )
334 DeleteAllItems();
335 m_nResultsID = ResultsID;
336 if (ResultsID) {
337 const CSearchResultList& list = theApp->searchlist->GetSearchResults(ResultsID);
338 for (unsigned int i = 0; i < list.size(); ++i) {
339 AddResult( list[i] );
345 wxUIntPtr CSearchListCtrl::GetSearchId()
347 return m_nResultsID;
351 void CSearchListCtrl::SetFilter(const wxString& regExp, bool invert, bool filterKnown)
353 if (regExp.IsEmpty()) {
354 // Show everything
355 m_filterText = wxT(".*");
356 } else {
357 m_filterText = regExp;
360 m_filter.Compile(m_filterText, wxRE_DEFAULT | wxRE_ICASE);
361 m_filterKnown = filterKnown;
362 m_invert = invert;
364 if (m_filterEnabled) {
365 // Swap the list of filtered results so we can freely add new items to the list
366 ResultList curFiltered;
367 std::swap(curFiltered, m_filteredOut);
369 // Filter items already on the list
370 for (int i = 0; i < GetItemCount();) {
371 CSearchFile* file = (CSearchFile*)GetItemData(i);
373 if (IsFiltered(file)) {
374 ++i;
375 } else {
376 m_filteredOut.push_back(file);
377 DeleteItem(i);
381 // Check the previously filtered items.
382 ResultList::iterator it = curFiltered.begin();
383 for (; it != curFiltered.end(); ++it) {
384 if (IsFiltered(*it)) {
385 AddResult(*it);
386 } else {
387 m_filteredOut.push_back(*it);
394 void CSearchListCtrl::EnableFiltering(bool enabled)
396 if (enabled != m_filterEnabled) {
397 m_filterEnabled = enabled;
399 if (enabled) {
400 SetFilter(m_filterText, m_invert, m_filterKnown);
401 } else {
402 ResultList::iterator it = m_filteredOut.begin();
403 for (; it != m_filteredOut.end(); ++it) {
404 AddResult(*it);
407 m_filteredOut.clear();
413 size_t CSearchListCtrl::GetHiddenItemCount() const
415 return m_filteredOut.size();
419 bool CSearchListCtrl::IsFiltered(const CSearchFile* file)
421 // By default, everything is displayed
422 bool result = true;
424 if (m_filterEnabled && m_filter.IsValid()) {
425 result = m_filter.Matches(file->GetFileName().GetPrintable());
426 result = ((result && !m_invert) || (!result && m_invert));
428 if (result && m_filterKnown) {
429 result = file->GetDownloadStatus() == CSearchFile::NEW;
433 return result;
437 int CSearchListCtrl::SortProc(wxUIntPtr item1, wxUIntPtr item2, long sortData)
439 CSearchFile* file1 = (CSearchFile*)item1;
440 CSearchFile* file2 = (CSearchFile*)item2;
442 // Modifies the result, 1 for ascending, -1 for decending
443 int modifier = (sortData & CMuleListCtrl::SORT_DES) ? -1 : 1;
444 bool alternate = (sortData & CMuleListCtrl::SORT_ALT) != 0;
446 // Decide if which should files we should sort by.
447 wxUIntPtr parent1 = reinterpret_cast<wxUIntPtr>(file1->GetParent());
448 wxUIntPtr parent2 = reinterpret_cast<wxUIntPtr>(file2->GetParent());
449 wxUIntPtr filePtr1 = reinterpret_cast<wxUIntPtr>(file1);
450 wxUIntPtr filePtr2 = reinterpret_cast<wxUIntPtr>(file2);
451 if (parent1 && parent2) {
452 if (parent1 != parent2) {
453 return SortProc(parent1, parent2, sortData);
455 } else if (parent1) {
456 if (parent1 == filePtr2) {
457 return 1;
458 } else {
459 return SortProc(parent1, filePtr2, sortData);
461 } else if (parent2) {
462 if (parent2 == filePtr1) {
463 return -1;
464 } else {
465 return SortProc(filePtr1, parent2, sortData);
469 int result = 0;
470 switch (sortData & CMuleListCtrl::COLUMN_MASK) {
471 // Sort by filename
472 case ID_SEARCH_COL_NAME:
473 result = CmpAny(file1->GetFileName(), file2->GetFileName());
474 break;
476 // Sort file-size
477 case ID_SEARCH_COL_SIZE:
478 result = CmpAny( file1->GetFileSize(), file2->GetFileSize() );
479 break;
481 // Sort by sources
482 case ID_SEARCH_COL_SOURCES: {
483 int cmp = CmpAny( file1->GetSourceCount(), file2->GetSourceCount() );
484 int cmp2 = CmpAny( file1->GetCompleteSourceCount(), file2->GetCompleteSourceCount() );
486 if ( alternate ) {
487 // Swap criterias
488 int temp = cmp2;
489 cmp2 = cmp;
490 cmp = temp;
493 if ( cmp == 0 ) {
494 cmp = cmp2;
497 result = cmp;
498 break;
501 // Sort by file-types
502 case ID_SEARCH_COL_TYPE: {
503 result = GetFiletypeByName(file1->GetFileName()).Cmp(GetFiletypeByName(file2->GetFileName()));
504 if (result == 0) {
505 // Same file-type, sort by extension
506 result = CmpAny(file1->GetFileName().GetExt(), file2->GetFileName().GetExt());
509 break;
512 // Sort by file-hash
513 case ID_SEARCH_COL_FILEID:
514 result = CmpAny(file2->GetFileHash(), file1->GetFileHash());
515 break;
517 // Sort by file status
518 case ID_SEARCH_COL_STATUS:
519 result = CmpAny(DetermineStatusPrintable(file2), DetermineStatusPrintable(file1));
520 break;
523 return modifier * result;
527 void CSearchListCtrl::SyncLists( CSearchListCtrl* src, CSearchListCtrl* dst )
529 wxCHECK_RET(src && dst, wxT("NULL argument in SyncLists"));
531 // Column widths
532 for ( int i = 0; i < src->GetColumnCount(); i++ ) {
533 // We do this check since just setting the width causes a redraw
534 if ( dst->GetColumnWidth( i ) != src->GetColumnWidth( i ) ) {
535 dst->SetColumnWidth( i, src->GetColumnWidth( i ) );
539 // Sync sorting
540 unsigned column = src->GetSortColumn();
541 unsigned order = src->GetSortOrder();
542 if (column != dst->GetSortColumn() || order != dst->GetSortOrder()) {
543 dst->SetSorting(column, order);
548 void CSearchListCtrl::SyncOtherLists( CSearchListCtrl* src )
550 std::list<CSearchListCtrl*>::iterator it;
552 for ( it = s_lists.begin(); it != s_lists.end(); ++it ) {
553 if ( (*it) != src ) {
554 SyncLists( src, *it );
560 void CSearchListCtrl::OnRightClick(wxListEvent& event)
562 CheckSelection(event);
564 if (GetSelectedItemCount()) {
565 // Create the popup-menu
566 wxMenu menu(_("File"));
567 menu.Append(MP_RESUME, _("Download"));
569 wxMenu* cats = new wxMenu(_("Category"));
570 cats->Append(MP_ASSIGNCAT, _("Main"));
571 for (unsigned i = 1; i < theApp->glob_prefs->GetCatCount(); i++) {
572 cats->Append(MP_ASSIGNCAT + i,
573 theApp->glob_prefs->GetCategory(i)->title);
576 menu.Append(MP_MENU_CATS, _("Download in category"), cats);
577 menu.AppendSeparator();
578 /* Commented out while it's gone
579 menu.Append(MP_RAZORSTATS, _("Get Razorback 2's stats for this file"));
580 menu.AppendSeparator();
582 menu.Append(MP_SEARCHRELATED, _("Search related files (eD2k, local server)"));
583 menu.AppendSeparator();
585 //#warning Uncomment this here to test the MP_MARK_AS_KNOWN feature. Beware! You are on your own here, this might break "known.met"
586 #if 0
587 menu.Append(MP_MARK_AS_KNOWN, _("Mark as known file"));
588 menu.AppendSeparator();
589 #endif
591 menu.Append(MP_GETED2KLINK, _("Copy eD2k link to clipboard"));
593 // These should only be enabled for single-selections
594 bool enable = (GetSelectedItemCount() == 1);
595 menu.Enable(MP_GETED2KLINK, enable);
596 menu.Enable(MP_MENU_CATS, (theApp->glob_prefs->GetCatCount() > 1));
598 PopupMenu(&menu, event.GetPoint());
599 } else {
600 event.Skip();
605 void CSearchListCtrl::OnColumnLClick( wxListEvent& event )
607 // Let the real event handler do its work first
608 CMuleListCtrl::OnColumnLClick( event );
610 SyncOtherLists( this );
614 void CSearchListCtrl::OnColumnResize( wxListEvent& WXUNUSED(event) )
616 SyncOtherLists( this );
620 void CSearchListCtrl::OnPopupGetUrl( wxCommandEvent& WXUNUSED(event) )
622 wxString URIs;
624 long index = GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
626 while( index != -1 ) {
627 CSearchFile* file = (CSearchFile*)GetItemData( index );
629 URIs += theApp->CreateED2kLink( file ) + wxT("\n");
631 index = GetNextItem( index, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
634 if ( !URIs.IsEmpty() ) {
635 theApp->CopyTextToClipboard( URIs.RemoveLast() );
640 void CSearchListCtrl::OnRazorStatsCheck( wxCommandEvent& WXUNUSED(event) )
642 int item = GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
643 if ( item == -1 ) {
644 return;
647 CSearchFile* file = (CSearchFile*)GetItemData( item );
648 theApp->amuledlg->LaunchUrl(wxT("http://stats.razorback2.com/ed2khistory?ed2k=") + file->GetFileHash().Encode());
652 void CSearchListCtrl::OnRelatedSearch( wxCommandEvent& WXUNUSED(event) )
654 int item = GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
655 if ( item == -1 ) {
656 return;
659 CSearchFile* file = (CSearchFile*)GetItemData( item );
660 theApp->searchlist->StopGlobalSearch();
661 theApp->amuledlg->m_searchwnd->ResetControls();
662 CastByID( IDC_SEARCHNAME, theApp->amuledlg->m_searchwnd, wxTextCtrl )->
663 SetValue(wxT("related::") + file->GetFileHash().Encode());
664 theApp->amuledlg->m_searchwnd->StartNewSearch();
668 void CSearchListCtrl::OnMarkAsKnown( wxCommandEvent& WXUNUSED(event) )
670 #ifndef CLIENT_GUI
671 long index = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
672 while (index > -1) {
673 CSearchFile *searchFile = (CSearchFile *)GetItemData(index);
674 CKnownFile *knownFile(new CKnownFile(*searchFile));
675 theApp->knownfiles->SafeAddKFile(knownFile);
676 index = GetNextItem(index, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
678 #endif
682 void CSearchListCtrl::OnPopupDownload(wxCommandEvent& event)
684 if (event.GetId() == MP_RESUME) {
685 // Via the "Download" menu-item, use category specified in drop-down menu
686 DownloadSelected();
687 } else {
688 // Via an "Download in category" item
689 DownloadSelected(event.GetId() - MP_ASSIGNCAT);
694 void CSearchListCtrl::OnItemActivated(wxListEvent& event)
696 CSearchFile* file = ((CSearchFile*)GetItemData(event.GetIndex()));
697 if (file->HasChildren()) {
698 ShowChildren(file, !file->ShowChildren());
699 } else {
700 DownloadSelected();
705 bool CSearchListCtrl::AltSortAllowed(unsigned column) const
707 switch ( column ) {
708 case ID_SEARCH_COL_SOURCES:
709 return true;
711 default:
712 return false;
717 void CSearchListCtrl::DownloadSelected(int category)
719 FindWindowById(IDC_SDOWNLOAD)->Enable(FALSE);
721 // Either the "Download" menu-item, the download-button, double-click or enter
722 if (category == -1) {
723 // Defaults to main category
724 category = 0;
726 if (CastByID(IDC_EXTENDEDSEARCHCHECK, NULL, wxCheckBox)->GetValue()) {
727 category = CastByID(ID_AUTOCATASSIGN, NULL, wxChoice)->GetSelection();
731 // Process all selections
732 long index = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
733 while (index > -1) {
734 CSearchFile* file = (CSearchFile*)GetItemData(index);
735 CoreNotify_Search_Add_Download(file, category);
736 index = GetNextItem(index, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
738 // Listcontrol gets updated by notification when download is started
742 const wxBrush& GetBrush(wxSystemColour index)
744 return CMuleColour(index).GetBrush();
748 void CSearchListCtrl::OnDrawItem(
749 int item, wxDC* dc, const wxRect& rect, const wxRect& rectHL, bool highlighted)
751 CSearchFile* file = (CSearchFile*)GetItemData(item);
753 // Define text-color and background
754 if (highlighted) {
755 if (GetFocus()) {
756 dc->SetBackground(GetBrush(wxSYS_COLOUR_HIGHLIGHT));
757 dc->SetTextForeground(CMuleColour(wxSYS_COLOUR_HIGHLIGHTTEXT));
758 } else {
759 dc->SetBackground(GetBrush(wxSYS_COLOUR_BTNSHADOW));
760 dc->SetTextForeground(CMuleColour(wxSYS_COLOUR_HIGHLIGHTTEXT));
762 } else {
763 dc->SetBackground(GetBrush(wxSYS_COLOUR_LISTBOX));
764 dc->SetTextForeground(CMuleColour(wxSYS_COLOUR_WINDOWTEXT));
767 // Define the border of the drawn area
768 if ( highlighted ) {
769 dc->SetPen(*(wxThePenList->FindOrCreatePen(CMuleColour(dc->GetBackground().GetColour()).Blend(65), 1, wxSOLID)));
770 } else {
771 dc->SetPen(*wxTRANSPARENT_PEN);
772 dc->SetTextForeground(GetItemTextColour(item));
775 // Clear the background, not done automatically since the drawing is buffered.
776 dc->SetBrush( dc->GetBackground() );
777 dc->DrawRectangle( rectHL.x, rectHL.y, rectHL.width, rectHL.height );
779 // Various constant values we use
780 const int iTextOffset = ( rect.GetHeight() - dc->GetCharHeight() ) / 2;
781 const int iOffset = 4;
782 const int treeOffset = 11;
783 const int treeCenter = 6;
784 bool tree_show = false;
786 wxRect cur_rec(iOffset, rect.y, 0, rect.height );
787 for (int i = 0; i < GetColumnCount(); i++) {
788 wxListItem listitem;
789 GetColumn(i, listitem);
791 if ( listitem.GetWidth() > 0 ) {
792 cur_rec.width = listitem.GetWidth() - 2*iOffset;
794 // Make a copy of the current rectangle so we can apply specific tweaks
795 wxRect target_rec = cur_rec;
797 // will ensure that text is about in the middle ;)
798 target_rec.y += iTextOffset;
800 if (i == 0) {
801 if (file->HasChildren() || file->GetParent()) {
802 tree_show = (listitem.GetWidth() > 0);
803 target_rec.x += treeOffset;
804 target_rec.width -= treeOffset;
806 // Children are indented a bit
807 if (file->GetParent()) {
808 target_rec.x += 4;
809 target_rec.width -= 4;
813 // Check if the rating icon should be drawn
814 if (file->HasRating()) {
815 int image = Client_InvalidRating_Smiley + file->UserRating() - 1;
817 int imgWidth = 16;
819 theApp->amuledlg->m_imagelist.Draw(image, *dc, target_rec.GetX(),
820 target_rec.GetY() - 1, wxIMAGELIST_DRAW_TRANSPARENT);
822 // Move the text past the icon.
823 target_rec.x += imgWidth + 4;
824 target_rec.width -= imgWidth + 4;
828 wxListItem cellitem;
829 cellitem.SetColumn(i);
830 cellitem.SetId(item);
832 // Force clipper (clip 2 px more than the rectangle from the right side)
833 wxDCClipper clipper(*dc, target_rec.x, target_rec.y, target_rec.width - 2, target_rec.height);
835 if (GetItem(cellitem)) {
836 dc->DrawText(cellitem.GetText(), target_rec.GetX(), target_rec.GetY());
837 } else {
838 dc->DrawText(wxT("GetItem failed!"), target_rec.GetX(), target_rec.GetY());
841 // Increment to the next column
842 cur_rec.x += listitem.GetWidth();
846 // Draw tree last so it draws over selected and focus (looks better)
847 if ( tree_show ) {
848 // Gather some information
849 const bool notLast = (item + 1 < GetItemCount());
850 const bool notFirst = (item != 0);
851 const bool hasNext = notLast && ((CSearchFile*)GetItemData(item + 1))->GetParent();
852 const int middle = cur_rec.y + ( cur_rec.height + 1 ) / 2;
854 // Set up a new pen for drawing the tree
855 dc->SetPen( *(wxThePenList->FindOrCreatePen(dc->GetTextForeground(), 1, wxSOLID)) );
857 if (file->GetParent()) {
858 // Draw the line to the filename
859 dc->DrawLine(treeCenter, middle, treeOffset + 4, middle);
861 // Draw the line to the child node
862 if (hasNext) {
863 dc->DrawLine(treeCenter, middle, treeCenter, cur_rec.y + cur_rec.height + 1);
866 // Draw the line back up to parent node
867 if (notFirst) {
868 dc->DrawLine(treeCenter, middle, treeCenter, cur_rec.y - 1);
870 } else if (file->HasChildren()) {
871 if (file->ShowChildren()) {
872 // Draw empty circle
873 dc->SetBrush(*wxTRANSPARENT_BRUSH);
874 } else {
875 dc->SetBrush(*(wxTheBrushList->FindOrCreateBrush(GetItemTextColour(item))));
878 dc->DrawCircle( treeCenter, middle, 3 );
880 // Draw the line to the child node if there are any children
881 if (hasNext && file->ShowChildren()) {
882 dc->DrawLine(treeCenter, middle + 3, treeCenter, cur_rec.y + cur_rec.height + 1);
887 // Sanity checks to ensure that results/children are properly positioned.
888 #ifdef __WXDEBUG__
890 CSearchFile* parent = file->GetParent();
892 if (item > 0) {
893 CSearchFile* before = (CSearchFile*)GetItemData(item - 1);
894 wxASSERT(before);
895 if (parent) {
896 wxASSERT((before->GetParent() == parent) || (before == parent));
897 } else {
898 wxASSERT(before->GetParent() != file);
902 if (item < GetItemCount() - 1) {
903 CSearchFile* after = (CSearchFile*)GetItemData(item + 1);
904 wxASSERT(after);
905 if (parent) {
906 wxASSERT((after->GetParent() == parent) || (!after->GetParent()));
907 } else {
908 wxASSERT((after->GetParent() == file) || (!after->GetParent()));
912 #endif
916 void CSearchListCtrl::ShowChildren(CSearchFile* file, bool show)
918 Freeze();
920 file->SetShowChildren(show);
922 const CSearchResultList& results = file->GetChildren();
923 for (size_t i = 0; i < results.size(); ++i) {
924 if (show) {
925 AddResult(results[i]);
926 } else {
927 RemoveResult(results[i]);
931 Thaw();
935 wxString CSearchListCtrl::GetTTSText(unsigned item) const
937 return GetItemText(item);
941 wxString CSearchListCtrl::DetermineStatusPrintable(CSearchFile *toshow)
943 switch (toshow->GetDownloadStatus()) {
944 case CSearchFile::DOWNLOADED: return _("Downloaded"); // File has already been downloaded.
945 case CSearchFile::QUEUED: // File is downloading.
946 case CSearchFile::QUEUEDCANCELED: return _("Queued"); // File is downloading and has been canceled before.
947 case CSearchFile::CANCELED: return _("Canceled"); // File has been canceled.
948 default: return _("New"); // File is new.
951 // File_checked_for_headers