2 // This file is part of the aMule Project.
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 )
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
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "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
)
56 std::list
<CSearchListCtrl
*> CSearchListCtrl::s_lists
;
59 enum SearchListColumns
{
60 ID_SEARCH_COL_NAME
= 0,
62 ID_SEARCH_COL_SOURCES
,
69 CSearchListCtrl::CSearchListCtrl(
75 const wxValidator
&validator
,
78 CMuleListCtrl(parent
, winid
, pos
, size
, style
| wxLC_OWNERDRAW
, validator
, name
),
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") );
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") );
102 // Unset the name to avoid the settings getting saved every time a list is closed
103 SetTableName( wxEmptyString
);
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() )
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) {
144 } else if (toshow
->GetParent() && !toshow
->GetParent()->ShowChildren()) {
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
))) {
159 // No children left, and the parent is filtered.
160 m_filteredOut
.push_back(toshow
);
164 m_filteredOut
.push_back(toshow
);
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.
175 CSearchFile
* parent
= toshow
->GetParent();
178 CSearchFile
* before
= (CSearchFile
*)GetItemData(newid
- 1);
181 wxASSERT((before
->GetParent() == parent
) || (before
== parent
));
183 wxASSERT(before
->GetParent() != toshow
);
187 if ((int)newid
< GetItemCount() - 1) {
188 CSearchFile
* after
= (CSearchFile
*)GetItemData(newid
+ 1);
191 wxASSERT((after
->GetParent() == parent
) || (!after
->GetParent()));
193 wxASSERT((after
->GetParent() == toshow
) || (!after
->GetParent()));
199 SetItemPtrData(newid
, toshowdata
);
202 SetItem(newid
, ID_SEARCH_COL_SIZE
, CastItoXBytes( toshow
->GetFileSize() ) );
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) {
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);
219 SetItem( newid
, ID_SEARCH_COL_SOURCES
, temp
);
222 SetItem( newid
, ID_SEARCH_COL_TYPE
, GetFiletypeByName( toshow
->GetFileName() ) );
225 SetItem(newid
, ID_SEARCH_COL_FILEID
, toshow
->GetFileHash().Encode() );
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
));
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
));
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) {
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);
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
)) {
287 void CSearchListCtrl::UpdateItemColor( long 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.
307 case CSearchFile::QUEUED
: // File is downloading.
308 case CSearchFile::QUEUEDCANCELED
: // File is downloading and has been canceled before.
312 case CSearchFile::CANCELED
: // File has been canceled. Mark as magenta.
316 default: // File is new, colour after number of files
317 blue
+= file
->GetSourceCount() * 5;
323 // don't forget to set the item data back...
325 newitem
.SetId( index
);
326 newitem
.SetTextColour( wxColour( red
, green
, blue
) );
332 void CSearchListCtrl::ShowResults( long ResultsID
)
335 m_nResultsID
= 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()
351 void CSearchListCtrl::SetFilter(const wxString
& regExp
, bool invert
, bool filterKnown
)
353 if (regExp
.IsEmpty()) {
355 m_filterText
= wxT(".*");
357 m_filterText
= regExp
;
360 m_filter
.Compile(m_filterText
, wxRE_DEFAULT
| wxRE_ICASE
);
361 m_filterKnown
= filterKnown
;
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
)) {
376 m_filteredOut
.push_back(file
);
381 // Check the previously filtered items.
382 ResultList::iterator it
= curFiltered
.begin();
383 for (; it
!= curFiltered
.end(); ++it
) {
384 if (IsFiltered(*it
)) {
387 m_filteredOut
.push_back(*it
);
394 void CSearchListCtrl::EnableFiltering(bool enabled
)
396 if (enabled
!= m_filterEnabled
) {
397 m_filterEnabled
= enabled
;
400 SetFilter(m_filterText
, m_invert
, m_filterKnown
);
402 ResultList::iterator it
= m_filteredOut
.begin();
403 for (; it
!= m_filteredOut
.end(); ++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
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
;
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
) {
459 return SortProc(parent1
, filePtr2
, sortData
);
461 } else if (parent2
) {
462 if (parent2
== filePtr1
) {
465 return SortProc(filePtr1
, parent2
, sortData
);
470 switch (sortData
& CMuleListCtrl::COLUMN_MASK
) {
472 case ID_SEARCH_COL_NAME
:
473 result
= CmpAny(file1
->GetFileName(), file2
->GetFileName());
477 case ID_SEARCH_COL_SIZE
:
478 result
= CmpAny( file1
->GetFileSize(), file2
->GetFileSize() );
482 case ID_SEARCH_COL_SOURCES
: {
483 int cmp
= CmpAny( file1
->GetSourceCount(), file2
->GetSourceCount() );
484 int cmp2
= CmpAny( file1
->GetCompleteSourceCount(), file2
->GetCompleteSourceCount() );
501 // Sort by file-types
502 case ID_SEARCH_COL_TYPE
: {
503 result
= GetFiletypeByName(file1
->GetFileName()).Cmp(GetFiletypeByName(file2
->GetFileName()));
505 // Same file-type, sort by extension
506 result
= CmpAny(file1
->GetFileName().GetExt(), file2
->GetFileName().GetExt());
513 case ID_SEARCH_COL_FILEID
:
514 result
= CmpAny(file2
->GetFileHash(), file1
->GetFileHash());
517 // Sort by file status
518 case ID_SEARCH_COL_STATUS
:
519 result
= CmpAny(DetermineStatusPrintable(file2
), DetermineStatusPrintable(file1
));
523 return modifier
* result
;
527 void CSearchListCtrl::SyncLists( CSearchListCtrl
* src
, CSearchListCtrl
* dst
)
529 wxCHECK_RET(src
&& dst
, wxT("NULL argument in SyncLists"));
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
) );
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"
587 menu
.Append(MP_MARK_AS_KNOWN
, _("Mark as known file"));
588 menu
.AppendSeparator();
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());
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
) )
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
);
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
);
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
) )
671 long index
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
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
);
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
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());
705 bool CSearchListCtrl::AltSortAllowed(unsigned column
) const
708 case ID_SEARCH_COL_SOURCES
:
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
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
);
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
756 dc
->SetBackground(GetBrush(wxSYS_COLOUR_HIGHLIGHT
));
757 dc
->SetTextForeground(CMuleColour(wxSYS_COLOUR_HIGHLIGHTTEXT
));
759 dc
->SetBackground(GetBrush(wxSYS_COLOUR_BTNSHADOW
));
760 dc
->SetTextForeground(CMuleColour(wxSYS_COLOUR_HIGHLIGHTTEXT
));
763 dc
->SetBackground(GetBrush(wxSYS_COLOUR_LISTBOX
));
764 dc
->SetTextForeground(CMuleColour(wxSYS_COLOUR_WINDOWTEXT
));
767 // Define the border of the drawn area
769 dc
->SetPen(*(wxThePenList
->FindOrCreatePen(CMuleColour(dc
->GetBackground().GetColour()).Blend(65), 1, wxSOLID
)));
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
++) {
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
;
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()) {
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;
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;
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());
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)
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
863 dc
->DrawLine(treeCenter
, middle
, treeCenter
, cur_rec
.y
+ cur_rec
.height
+ 1);
866 // Draw the line back up to parent node
868 dc
->DrawLine(treeCenter
, middle
, treeCenter
, cur_rec
.y
- 1);
870 } else if (file
->HasChildren()) {
871 if (file
->ShowChildren()) {
873 dc
->SetBrush(*wxTRANSPARENT_BRUSH
);
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.
890 CSearchFile
* parent
= file
->GetParent();
893 CSearchFile
* before
= (CSearchFile
*)GetItemData(item
- 1);
896 wxASSERT((before
->GetParent() == parent
) || (before
== parent
));
898 wxASSERT(before
->GetParent() != file
);
902 if (item
< GetItemCount() - 1) {
903 CSearchFile
* after
= (CSearchFile
*)GetItemData(item
+ 1);
906 wxASSERT((after
->GetParent() == parent
) || (!after
->GetParent()));
908 wxASSERT((after
->GetParent() == file
) || (!after
->GetParent()));
916 void CSearchListCtrl::ShowChildren(CSearchFile
* file
, bool show
)
920 file
->SetShowChildren(show
);
922 const CSearchResultList
& results
= file
->GetChildren();
923 for (size_t i
= 0; i
< results
.size(); ++i
) {
925 AddResult(results
[i
]);
927 RemoveResult(results
[i
]);
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