Upstream tarball 20080708
[amule.git] / src / SearchListCtrl.cpp
blob666f0009b38acc7875559ae61a98b2028b0f0282
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 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 "DownloadQueue.h" // Needed for CDownloadQueue
32 #include "KnownFileList.h" // Needed for CKnownFileList
33 #include "PartFile.h" // Needed for CPartFile and CKnownFile
34 #include "SearchList.h" // Needed for CSearchFile
35 #include "SearchDlg.h" // Needed for CSearchDlg
36 #include "amuleDlg.h" // Needed for CamuleDlg
37 #include "muuli_wdr.h" // Needed for clientImages
38 #include "Preferences.h" // Needed for thePrefs
39 #include "GuiEvents.h" // Needed for CoreNotify_Search_Add_Download
40 #include "MuleColour.h"
42 BEGIN_EVENT_TABLE(CSearchListCtrl, CMuleListCtrl)
43 EVT_LIST_ITEM_RIGHT_CLICK(-1, CSearchListCtrl::OnRightClick)
44 EVT_LIST_COL_CLICK( -1, CSearchListCtrl::OnColumnLClick)
45 EVT_LIST_COL_END_DRAG( -1, CSearchListCtrl::OnColumnResize)
47 EVT_MENU( MP_GETED2KLINK, CSearchListCtrl::OnPopupGetUrl)
48 EVT_MENU( MP_RAZORSTATS, CSearchListCtrl::OnRazorStatsCheck)
49 EVT_MENU( MP_SEARCHRELATED, CSearchListCtrl::OnRelatedSearch)
50 EVT_MENU( MP_MARK_AS_KNOWN, CSearchListCtrl::OnMarkAsKnown)
51 EVT_MENU( MP_RESUME, CSearchListCtrl::OnPopupDownload)
52 EVT_MENU_RANGE( MP_ASSIGNCAT, MP_ASSIGNCAT + 99, CSearchListCtrl::OnPopupDownload )
54 EVT_LIST_ITEM_ACTIVATED( -1, CSearchListCtrl::OnItemActivated)
55 END_EVENT_TABLE()
58 std::list<CSearchListCtrl*> CSearchListCtrl::s_lists;
61 enum SearchListColumns {
62 ID_SEARCH_COL_NAME = 0,
63 ID_SEARCH_COL_SIZE,
64 ID_SEARCH_COL_SOURCES,
65 ID_SEARCH_COL_TYPE,
66 ID_SEARCH_COL_FILEID
70 CSearchListCtrl::CSearchListCtrl(
71 wxWindow *parent,
72 wxWindowID winid,
73 const wxPoint &pos,
74 const wxSize &size,
75 long style,
76 const wxValidator &validator,
77 const wxString &name)
79 CMuleListCtrl(parent, winid, pos, size, style | wxLC_OWNERDRAW, validator, name),
80 m_filterKnown(false),
81 m_invert(false),
82 m_filterEnabled(false)
84 // Setting the sorter function.
85 SetSortFunc( SortProc );
87 InsertColumn( ID_SEARCH_COL_NAME, _("File Name"), wxLIST_FORMAT_LEFT, 500);
88 InsertColumn( ID_SEARCH_COL_SIZE, _("Size"), wxLIST_FORMAT_LEFT, 100);
89 InsertColumn( ID_SEARCH_COL_SOURCES, _("Sources"), wxLIST_FORMAT_LEFT, 50);
90 InsertColumn( ID_SEARCH_COL_TYPE, _("Type"), wxLIST_FORMAT_LEFT, 65);
91 InsertColumn( ID_SEARCH_COL_FILEID, _("FileID"), wxLIST_FORMAT_LEFT, 280);
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 CSearchListCtrl::~CSearchListCtrl()
116 std::list<CSearchListCtrl*>::iterator it = std::find( s_lists.begin(), s_lists.end(), this );
118 if ( it != s_lists.end() )
119 s_lists.erase( it );
121 // We only save the settings if the last list was closed
122 if ( s_lists.empty() ) {
123 // In order to get the settings saved, we need to set the name
124 SetTableName( wxT("Search") );
129 void CSearchListCtrl::AddResult(CSearchFile* toshow)
131 wxCHECK_RET(toshow->GetSearchID() == m_nResultsID, wxT("Wrong search-id for result-list"));
133 const wxUIntPtr toshowdata = reinterpret_cast<wxUIntPtr>(toshow);
135 // Check if the result should be shown
136 if (FindItem(-1, toshowdata) != -1) {
137 return;
138 } else if (toshow->GetParent() && !toshow->GetParent()->ShowChildren()) {
139 return;
140 } else if (!IsFiltered(toshow)) {
141 if (toshow->HasChildren() && toshow->ShowChildren()) {
142 // Only filter the parent if none of the children are shown.
143 bool foundChild = false;
144 const CSearchResultList& children = toshow->GetChildren();
145 for (size_t i = 0; i < children.size(); ++i) {
146 if (IsFiltered(children.at(i))) {
147 foundChild = true;
148 break;
152 if (!foundChild) {
153 // No children left, and the parent is filtered.
154 m_filteredOut.push_back(toshow);
155 return;
157 } else {
158 m_filteredOut.push_back(toshow);
159 return;
163 // Insert the item before the item found by the search
164 uint32 newid = InsertItem(GetInsertPos(toshowdata), toshow->GetFileName().GetPrintable());
166 // Sanity checks to ensure that results/children are properly positioned.
167 #ifdef __WXDEBUG__
169 CSearchFile* parent = toshow->GetParent();
171 if (newid > 0) {
172 CSearchFile* before = (CSearchFile*)GetItemData(newid - 1);
173 wxASSERT(before);
174 if (parent) {
175 wxASSERT((before->GetParent() == parent) || (before == parent));
176 } else {
177 wxASSERT(before->GetParent() != toshow);
181 if ((int)newid < GetItemCount() - 1) {
182 CSearchFile* after = (CSearchFile*)GetItemData(newid + 1);
183 wxASSERT(after);
184 if (parent) {
185 wxASSERT((after->GetParent() == parent) || (!after->GetParent()));
186 } else {
187 wxASSERT((after->GetParent() == toshow) || (!after->GetParent()));
191 #endif
193 SetItemPtrData(newid, toshowdata);
195 // Filesize
196 SetItem(newid, ID_SEARCH_COL_SIZE, CastItoXBytes( toshow->GetFileSize() ) );
198 // Source count
199 wxString temp = wxString::Format(wxT("%d"), toshow->GetSourceCount());
200 if (toshow->GetCompleteSourceCount()) {
201 temp += wxString::Format(wxT(" (%d)"), toshow->GetCompleteSourceCount());
203 if (toshow->GetClientsCount()) {
204 temp += wxString::Format(wxT(" [%d]"), toshow->GetClientsCount());
206 #ifdef __DEBUG__
207 if (toshow->GetKadPublishInfo() == 0) {
208 temp += wxT(" | -");
209 } else {
210 temp += wxString::Format(wxT(" | N:%u, P:%u, T:%0.2f"), (toshow->GetKadPublishInfo() & 0xFF000000) >> 24, (toshow->GetKadPublishInfo() & 0x00FF0000) >> 16, (toshow->GetKadPublishInfo() & 0x0000FFFF) / 100.0);
212 #endif
213 SetItem( newid, ID_SEARCH_COL_SOURCES, temp );
215 // File-type
216 SetItem( newid, ID_SEARCH_COL_TYPE, GetFiletypeByName( toshow->GetFileName() ) );
218 // File-hash
219 SetItem(newid, ID_SEARCH_COL_FILEID, toshow->GetFileHash().Encode() );
221 // Set the color of the item
222 UpdateItemColor( newid );
226 void CSearchListCtrl::RemoveResult(CSearchFile* toremove)
228 ShowChildren(toremove, false);
230 long index = FindItem(-1, reinterpret_cast<wxUIntPtr>(toremove));
231 if (index != -1) {
232 DeleteItem(index);
233 } else {
234 ResultList::iterator it = std::find(m_filteredOut.begin(), m_filteredOut.end(), toremove);
235 if ( it != m_filteredOut.end()) {
236 m_filteredOut.erase(it);
242 void CSearchListCtrl::UpdateResult(CSearchFile* toupdate)
244 long index = FindItem(-1, reinterpret_cast<wxUIntPtr>(toupdate));
245 if (index != -1) {
246 // Update the filename, which may be changed in case of multiple variants.
247 SetItem(index, ID_SEARCH_COL_NAME, toupdate->GetFileName().GetPrintable());
249 wxString temp = wxString::Format(wxT("%d"), toupdate->GetSourceCount());
250 if (toupdate->GetCompleteSourceCount()) {
251 temp += wxString::Format(wxT(" (%d)"), toupdate->GetCompleteSourceCount());
253 if (toupdate->GetClientsCount()) {
254 temp += wxString::Format(wxT(" [%d]"), toupdate->GetClientsCount());
256 #ifdef __DEBUG__
257 if (toupdate->GetKadPublishInfo() == 0) {
258 temp += wxT(" | -");
259 } else {
260 temp += wxString::Format(wxT(" | N:%u, P:%u, T:%0.2f"), (toupdate->GetKadPublishInfo() & 0xFF000000) >> 24, (toupdate->GetKadPublishInfo() & 0x00FF0000) >> 16, (toupdate->GetKadPublishInfo() & 0x0000FFFF) / 100.0);
262 #endif
263 SetItem(index, ID_SEARCH_COL_SOURCES, temp);
265 UpdateItemColor(index);
267 // Deletions of items causes rather large amount of flicker, so to
268 // avoid this, we resort the list to ensure correct ordering.
269 if (!IsItemSorted(index)) {
270 SortList();
276 void CSearchListCtrl::UpdateItemColor( long index )
278 wxListItem item;
279 item.SetId( index );
280 item.SetColumn( ID_SEARCH_COL_SIZE );
281 item.SetMask( wxLIST_MASK_STATE|wxLIST_MASK_TEXT|wxLIST_MASK_IMAGE|wxLIST_MASK_DATA|wxLIST_MASK_WIDTH|wxLIST_MASK_FORMAT );
283 if ( GetItem(item) ) {
284 CMuleColour newcol(wxSYS_COLOUR_WINDOWTEXT);
286 CSearchFile* file = (CSearchFile*)GetItemData(index);
287 CKnownFile* sameFile = theApp->downloadqueue->GetFileByID(file->GetFileHash());
288 if ( !sameFile ) {
289 sameFile = theApp->knownfiles->FindKnownFileByID(file->GetFileHash());
292 int red = newcol.Red();
293 int green = newcol.Green();
294 int blue = newcol.Blue();
296 if ( sameFile ) {
297 if ( sameFile->IsPartFile() ) {
298 // File is already being downloaded. Mark as red.
299 red = 255;
300 } else if ( sameFile->GetStatus() == PS_COMPLETE ) {
301 // File has already been downloaded. Mark as green.
302 green = 200;
303 } else {
304 // File has been cancelled or removed. Mark as grey.
305 red = 128;
306 green = 128;
307 blue = 128;
309 } else {
310 // File is new, colour after number of files
311 blue += file->GetSourceCount() * 5;
312 if ( blue > 255 ) {
313 blue = 255;
317 // don't forget to set the item data back...
318 wxListItem newitem;
319 newitem.SetId( index );
320 newitem.SetTextColour( wxColour( red, green, blue ) );
321 SetItem( newitem );
326 // Update the colors of all assosiated items, which means parents and/or siblings.
327 void CSearchListCtrl::UpdateAllRelativesColor(
328 CSearchFile *file,
329 long index)
331 if ((file->ShowChildren() && file->HasChildren()) ||
332 file->GetParent()) {
333 CSearchFile *parent = file->GetParent() ?
334 file->GetParent() : file;
335 const CSearchResultList &list = parent->GetChildren();
336 for (size_t j = 0; j < list.size(); ++j) {
337 UpdateItemColor(FindItem(-1, reinterpret_cast<wxUIntPtr>(list.at(j))));
339 UpdateItemColor(FindItem(-1, reinterpret_cast<wxUIntPtr>(parent)));
340 } else {
341 UpdateItemColor(index);
346 void CSearchListCtrl::ShowResults( long ResultsID )
348 DeleteAllItems();
349 m_nResultsID = ResultsID;
350 if (ResultsID) {
351 const CSearchResultList& list = theApp->searchlist->GetSearchResults(ResultsID);
352 for (unsigned int i = 0; i < list.size(); ++i) {
353 AddResult( list[i] );
359 wxUIntPtr CSearchListCtrl::GetSearchId()
361 return m_nResultsID;
365 void CSearchListCtrl::SetFilter(const wxString& regExp, bool invert, bool filterKnown)
367 if (regExp.IsEmpty()) {
368 // Show everything
369 m_filterText = wxT(".*");
370 } else {
371 m_filterText = regExp;
374 m_filter.Compile(m_filterText, wxRE_DEFAULT | wxRE_ICASE);
375 m_filterKnown = filterKnown;
376 m_invert = invert;
378 if (m_filterEnabled) {
379 // Swap the list of filtered results so we can freely add new items to the list
380 ResultList curFiltered;
381 std::swap(curFiltered, m_filteredOut);
383 // Filter items already on the list
384 for (int i = 0; i < GetItemCount();) {
385 CSearchFile* file = (CSearchFile*)GetItemData(i);
387 if (IsFiltered(file)) {
388 ++i;
389 } else {
390 m_filteredOut.push_back(file);
391 DeleteItem(i);
395 // Check the previously filtered items.
396 ResultList::iterator it = curFiltered.begin();
397 for (; it != curFiltered.end(); ++it) {
398 if (IsFiltered(*it)) {
399 AddResult(*it);
400 } else {
401 m_filteredOut.push_back(*it);
408 void CSearchListCtrl::EnableFiltering(bool enabled)
410 if (enabled != m_filterEnabled) {
411 m_filterEnabled = enabled;
413 if (enabled) {
414 SetFilter(m_filterText, m_invert, m_filterKnown);
415 } else {
416 ResultList::iterator it = m_filteredOut.begin();
417 for (; it != m_filteredOut.end(); ++it) {
418 AddResult(*it);
421 m_filteredOut.clear();
427 size_t CSearchListCtrl::GetHiddenItemCount() const
429 return m_filteredOut.size();
433 bool CSearchListCtrl::IsFiltered(const CSearchFile* file)
435 // By default, everything is displayed
436 bool result = true;
438 if (m_filterEnabled && m_filter.IsValid()) {
439 result = m_filter.Matches(file->GetFileName().GetPrintable());
440 result = ((result && !m_invert) || (!result && m_invert));
442 if (result && m_filterKnown) {
443 result = !theApp->downloadqueue->GetFileByID(file->GetFileHash());
445 if (result) {
446 result = !theApp->knownfiles->FindKnownFileByID(file->GetFileHash());
451 return result;
455 int CSearchListCtrl::SortProc(wxUIntPtr item1, wxUIntPtr item2, long sortData)
457 CSearchFile* file1 = (CSearchFile*)item1;
458 CSearchFile* file2 = (CSearchFile*)item2;
460 // Modifies the result, 1 for ascending, -1 for decending
461 int modifier = (sortData & CMuleListCtrl::SORT_DES) ? -1 : 1;
462 bool alternate = (sortData & CMuleListCtrl::SORT_ALT) != 0;
464 // Decide if which should files we should sort by.
465 wxUIntPtr parent1 = reinterpret_cast<wxUIntPtr>(file1->GetParent());
466 wxUIntPtr parent2 = reinterpret_cast<wxUIntPtr>(file2->GetParent());
467 wxUIntPtr filePtr1 = reinterpret_cast<wxUIntPtr>(file1);
468 wxUIntPtr filePtr2 = reinterpret_cast<wxUIntPtr>(file2);
469 if (parent1 && parent2) {
470 if (parent1 != parent2) {
471 return SortProc(parent1, parent2, sortData);
473 } else if (parent1) {
474 if (parent1 == filePtr2) {
475 return 1;
476 } else {
477 return SortProc(parent1, filePtr2, sortData);
479 } else if (parent2) {
480 if (parent2 == filePtr1) {
481 return -1;
482 } else {
483 return SortProc(filePtr1, parent2, sortData);
487 int result = 0;
488 switch (sortData & CMuleListCtrl::COLUMN_MASK) {
489 // Sort by filename
490 case ID_SEARCH_COL_NAME:
491 result = CmpAny(file1->GetFileName(), file2->GetFileName());
492 break;
494 // Sort file-size
495 case ID_SEARCH_COL_SIZE:
496 result = CmpAny( file1->GetFileSize(), file2->GetFileSize() );
497 break;
499 // Sort by sources
500 case ID_SEARCH_COL_SOURCES: {
501 int cmp = CmpAny( file1->GetSourceCount(), file2->GetSourceCount() );
502 int cmp2 = CmpAny( file1->GetCompleteSourceCount(), file2->GetCompleteSourceCount() );
504 if ( alternate ) {
505 // Swap criterias
506 int temp = cmp2;
507 cmp2 = cmp;
508 cmp = temp;
511 if ( cmp == 0 ) {
512 cmp = cmp2;
515 result = cmp;
516 break;
519 // Sort by file-types
520 case ID_SEARCH_COL_TYPE: {
521 result = GetFiletypeByName(file1->GetFileName()).Cmp(GetFiletypeByName(file2->GetFileName()));
522 if (result == 0) {
523 // Same file-type, sort by extension
524 result = CmpAny(file1->GetFileName().GetExt(), file2->GetFileName().GetExt());
527 break;
530 // Sort by file-hash
531 case ID_SEARCH_COL_FILEID:
532 result = CmpAny(file2->GetFileHash(), file1->GetFileHash());
535 return modifier * result;
539 void CSearchListCtrl::SyncLists( CSearchListCtrl* src, CSearchListCtrl* dst )
541 wxCHECK_RET(src && dst, wxT("NULL argument in SyncLists"));
543 // Column widths
544 for ( int i = 0; i < src->GetColumnCount(); i++ ) {
545 // We do this check since just setting the width causes a redraw
546 if ( dst->GetColumnWidth( i ) != src->GetColumnWidth( i ) ) {
547 dst->SetColumnWidth( i, src->GetColumnWidth( i ) );
551 // Sync sorting
552 unsigned column = src->GetSortColumn();
553 unsigned order = src->GetSortOrder();
554 if (column != dst->GetSortColumn() || order != dst->GetSortOrder()) {
555 dst->SetSorting(column, order);
560 void CSearchListCtrl::SyncOtherLists( CSearchListCtrl* src )
562 std::list<CSearchListCtrl*>::iterator it;
564 for ( it = s_lists.begin(); it != s_lists.end(); ++it ) {
565 if ( (*it) != src ) {
566 SyncLists( src, *it );
572 void CSearchListCtrl::OnRightClick(wxListEvent& event)
574 CheckSelection(event);
576 if (GetSelectedItemCount()) {
577 // Create the popup-menu
578 wxMenu menu(_("File"));
579 menu.Append(MP_RESUME, _("Download"));
581 wxMenu* cats = new wxMenu(_("Category"));
582 cats->Append(MP_ASSIGNCAT, _("Main"));
583 for (unsigned i = 1; i < theApp->glob_prefs->GetCatCount(); i++) {
584 cats->Append(MP_ASSIGNCAT + i,
585 theApp->glob_prefs->GetCategory(i)->title);
588 menu.Append(MP_MENU_CATS, _("Download in category"), cats);
589 menu.AppendSeparator();
590 /* Commented out while it's gone
591 menu.Append(MP_RAZORSTATS, _("Get Razorback 2's stats for this file"));
592 menu.AppendSeparator();
594 menu.Append(MP_SEARCHRELATED, _("Search related files (eD2k, local server)"));
595 menu.AppendSeparator();
597 //#warning Uncomment this here to test the MP_MARK_AS_KNOWN feature. Beware! You are on your own here, this might break "known.met"
598 #if 0
599 menu.Append(MP_MARK_AS_KNOWN, _("Mark as known file"));
600 menu.AppendSeparator();
601 #endif
603 menu.Append(MP_GETED2KLINK, _("Copy eD2k link to clipboard"));
605 // These should only be enabled for single-selections
606 bool enable = (GetSelectedItemCount() == 1);
607 menu.Enable(MP_GETED2KLINK, enable);
608 menu.Enable(MP_MENU_CATS, (theApp->glob_prefs->GetCatCount() > 1));
610 PopupMenu(&menu, event.GetPoint());
611 } else {
612 event.Skip();
617 void CSearchListCtrl::OnColumnLClick( wxListEvent& event )
619 // Let the real event handler do its work first
620 CMuleListCtrl::OnColumnLClick( event );
622 SyncOtherLists( this );
626 void CSearchListCtrl::OnColumnResize( wxListEvent& WXUNUSED(event) )
628 SyncOtherLists( this );
632 void CSearchListCtrl::OnPopupGetUrl( wxCommandEvent& WXUNUSED(event) )
634 wxString URIs;
636 long index = GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
638 while( index != -1 ) {
639 CSearchFile* file = (CSearchFile*)GetItemData( index );
641 URIs += theApp->CreateED2kLink( file ) + wxT("\n");
643 index = GetNextItem( index, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
646 if ( !URIs.IsEmpty() ) {
647 theApp->CopyTextToClipboard( URIs.RemoveLast() );
652 void CSearchListCtrl::OnRazorStatsCheck( 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->amuledlg->LaunchUrl(wxT("http://stats.razorback2.com/ed2khistory?ed2k=") + file->GetFileHash().Encode());
664 void CSearchListCtrl::OnRelatedSearch( wxCommandEvent& WXUNUSED(event) )
666 int item = GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
667 if ( item == -1 ) {
668 return;
671 CSearchFile* file = (CSearchFile*)GetItemData( item );
672 theApp->searchlist->StopGlobalSearch();
673 theApp->amuledlg->m_searchwnd->ResetControls();
674 CastByID( IDC_SEARCHNAME, theApp->amuledlg->m_searchwnd, wxTextCtrl )->
675 SetValue(wxT("related::") + file->GetFileHash().Encode());
676 theApp->amuledlg->m_searchwnd->StartNewSearch();
680 void CSearchListCtrl::OnMarkAsKnown( wxCommandEvent& WXUNUSED(event) )
682 int index = GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
683 if (index == -1) {
684 return;
687 #ifndef CLIENT_GUI
688 CSearchFile *searchFile = (CSearchFile *)GetItemData(index);
689 CKnownFile *knownFile(new CKnownFile(*searchFile));
690 theApp->knownfiles->SafeAddKFile(knownFile);
691 UpdateAllRelativesColor(searchFile, index);
692 #endif
696 void CSearchListCtrl::OnPopupDownload(wxCommandEvent& event)
698 if (event.GetId() == MP_RESUME) {
699 // Via the "Download" menu-item, use category specified in drop-down menu
700 DownloadSelected();
701 } else {
702 // Via an "Download in category" item
703 DownloadSelected(event.GetId() - MP_ASSIGNCAT);
708 void CSearchListCtrl::OnItemActivated(wxListEvent& event)
710 CSearchFile* file = ((CSearchFile*)GetItemData(event.GetIndex()));
711 if (file->HasChildren()) {
712 ShowChildren(file, !file->ShowChildren());
713 } else {
714 DownloadSelected();
719 bool CSearchListCtrl::AltSortAllowed(unsigned column) const
721 switch ( column ) {
722 case ID_SEARCH_COL_SOURCES:
723 return true;
725 default:
726 return false;
731 void CSearchListCtrl::DownloadSelected(int category)
733 FindWindowById(IDC_SDOWNLOAD)->Enable(FALSE);
735 // Either the "Download" menu-item, the download-button, double-click or enter
736 if (category == -1) {
737 // Defaults to main category
738 category = 0;
740 if (CastByID(IDC_EXTENDEDSEARCHCHECK, NULL, wxCheckBox)->GetValue()) {
741 category = CastByID(ID_AUTOCATASSIGN, NULL, wxChoice)->GetSelection();
745 long index = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
746 while (index > -1) {
747 CSearchFile* file = (CSearchFile*)GetItemData(index);
748 CoreNotify_Search_Add_Download(file, category);
749 UpdateAllRelativesColor(file, index);
750 index = GetNextItem(index, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
755 const wxBrush& GetBrush(wxSystemColour index)
757 return *wxTheBrushList->FindOrCreateBrush(CMuleColour(index));
761 void CSearchListCtrl::OnDrawItem(
762 int item, wxDC* dc, const wxRect& rect, const wxRect& rectHL, bool highlighted)
764 CSearchFile* file = (CSearchFile*)GetItemData(item);
766 // Define text-color and background
767 if (highlighted) {
768 if (GetFocus()) {
769 dc->SetBackground(GetBrush(wxSYS_COLOUR_HIGHLIGHT));
770 dc->SetTextForeground(CMuleColour(wxSYS_COLOUR_HIGHLIGHTTEXT));
771 } else {
772 dc->SetBackground(GetBrush(wxSYS_COLOUR_BTNSHADOW));
773 dc->SetTextForeground(CMuleColour(wxSYS_COLOUR_HIGHLIGHTTEXT));
775 } else {
776 dc->SetBackground(GetBrush(wxSYS_COLOUR_LISTBOX));
777 dc->SetTextForeground(CMuleColour(wxSYS_COLOUR_WINDOWTEXT));
780 // Define the border of the drawn area
781 if ( highlighted ) {
782 dc->SetPen(*(wxThePenList->FindOrCreatePen(CMuleColour(dc->GetBackground().GetColour()).Blend(65), 1, wxSOLID)));
783 } else {
784 dc->SetPen(*wxTRANSPARENT_PEN);
785 dc->SetTextForeground(GetItemTextColour(item));
788 // Clear the background, not done automatically since the drawing is buffered.
789 dc->SetBrush( dc->GetBackground() );
790 dc->DrawRectangle( rectHL.x, rectHL.y, rectHL.width, rectHL.height );
792 // Various constant values we use
793 const int iTextOffset = ( rect.GetHeight() - dc->GetCharHeight() ) / 2;
794 const int iOffset = 4;
795 const int treeOffset = 11;
796 const int treeCenter = 6;
797 bool tree_show = false;
799 wxRect cur_rec(iOffset, rect.y, 0, rect.height );
800 for (int i = 0; i < GetColumnCount(); i++) {
801 wxListItem listitem;
802 GetColumn(i, listitem);
804 if ( listitem.GetWidth() > 0 ) {
805 cur_rec.width = listitem.GetWidth() - 2*iOffset;
807 // Make a copy of the current rectangle so we can apply specific tweaks
808 wxRect target_rec = cur_rec;
810 // will ensure that text is about in the middle ;)
811 target_rec.y += iTextOffset;
813 if (i == 0) {
814 if (file->HasChildren() || file->GetParent()) {
815 tree_show = (listitem.GetWidth() > 0);
816 target_rec.x += treeOffset;
817 target_rec.width -= treeOffset;
819 // Children are indented a bit
820 if (file->GetParent()) {
821 target_rec.x += 4;
822 target_rec.width -= 4;
826 // Check if the rating icon should be drawn
827 if (file->HasRating()) {
828 int image = Client_InvalidRating_Smiley + file->UserRating() - 1;
830 int imgWidth = 16;
832 theApp->amuledlg->m_imagelist.Draw(image, *dc, target_rec.GetX(),
833 target_rec.GetY() - 1, wxIMAGELIST_DRAW_TRANSPARENT);
835 // Move the text past the icon.
836 target_rec.x += imgWidth + 4;
837 target_rec.width -= imgWidth + 4;
841 wxListItem cellitem;
842 cellitem.SetColumn(i);
843 cellitem.SetId(item);
845 // Force clipper (clip 2 px more than the rectangle from the right side)
846 wxDCClipper clipper(*dc, target_rec.x, target_rec.y, target_rec.width - 2, target_rec.height);
848 if (GetItem(cellitem)) {
849 dc->DrawText(cellitem.GetText(), target_rec.GetX(), target_rec.GetY());
850 } else {
851 dc->DrawText(wxT("GetItem failed!"), target_rec.GetX(), target_rec.GetY());
854 // Increment to the next column
855 cur_rec.x += listitem.GetWidth();
859 // Draw tree last so it draws over selected and focus (looks better)
860 if ( tree_show ) {
861 // Gather some information
862 const bool notLast = (item + 1 < GetItemCount());
863 const bool notFirst = (item != 0);
864 const bool hasNext = notLast && ((CSearchFile*)GetItemData(item + 1))->GetParent();
865 const int middle = cur_rec.y + ( cur_rec.height + 1 ) / 2;
867 // Set up a new pen for drawing the tree
868 dc->SetPen( *(wxThePenList->FindOrCreatePen(dc->GetTextForeground(), 1, wxSOLID)) );
870 if (file->GetParent()) {
871 // Draw the line to the filename
872 dc->DrawLine(treeCenter, middle, treeOffset + 4, middle);
874 // Draw the line to the child node
875 if (hasNext) {
876 dc->DrawLine(treeCenter, middle, treeCenter, cur_rec.y + cur_rec.height + 1);
879 // Draw the line back up to parent node
880 if (notFirst) {
881 dc->DrawLine(treeCenter, middle, treeCenter, cur_rec.y - 1);
883 } else if (file->HasChildren()) {
884 if (file->ShowChildren()) {
885 // Draw empty circle
886 dc->SetBrush(*wxTRANSPARENT_BRUSH);
887 } else {
888 dc->SetBrush(*(wxTheBrushList->FindOrCreateBrush(GetItemTextColour(item))));
891 dc->DrawCircle( treeCenter, middle, 3 );
893 // Draw the line to the child node if there are any children
894 if (hasNext && file->ShowChildren()) {
895 dc->DrawLine(treeCenter, middle + 3, treeCenter, cur_rec.y + cur_rec.height + 1);
900 // Sanity checks to ensure that results/children are properly positioned.
901 #ifdef __WXDEBUG__
903 CSearchFile* parent = file->GetParent();
905 if (item > 0) {
906 CSearchFile* before = (CSearchFile*)GetItemData(item - 1);
907 wxASSERT(before);
908 if (parent) {
909 wxASSERT((before->GetParent() == parent) || (before == parent));
910 } else {
911 wxASSERT(before->GetParent() != file);
915 if (item < GetItemCount() - 1) {
916 CSearchFile* after = (CSearchFile*)GetItemData(item + 1);
917 wxASSERT(after);
918 if (parent) {
919 wxASSERT((after->GetParent() == parent) || (!after->GetParent()));
920 } else {
921 wxASSERT((after->GetParent() == file) || (!after->GetParent()));
925 #endif
929 void CSearchListCtrl::ShowChildren(CSearchFile* file, bool show)
931 Freeze();
933 file->SetShowChildren(show);
935 const CSearchResultList& results = file->GetChildren();
936 for (size_t i = 0; i < results.size(); ++i) {
937 if (show) {
938 AddResult(results[i]);
939 } else {
940 RemoveResult(results[i]);
944 Thaw();
948 wxString CSearchListCtrl::GetTTSText(unsigned item) const
950 return GetItemText(item);
952 // File_checked_for_headers