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 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 "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
)
58 std::list
<CSearchListCtrl
*> CSearchListCtrl::s_lists
;
61 enum SearchListColumns
{
62 ID_SEARCH_COL_NAME
= 0,
64 ID_SEARCH_COL_SOURCES
,
70 CSearchListCtrl::CSearchListCtrl(
76 const wxValidator
&validator
,
79 CMuleListCtrl(parent
, winid
, pos
, size
, style
| wxLC_OWNERDRAW
, validator
, name
),
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);
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 CSearchListCtrl::~CSearchListCtrl()
116 std::list
<CSearchListCtrl
*>::iterator it
= std::find( s_lists
.begin(), s_lists
.end(), this );
118 if ( it
!= s_lists
.end() )
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) {
138 } else if (toshow
->GetParent() && !toshow
->GetParent()->ShowChildren()) {
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
))) {
153 // No children left, and the parent is filtered.
154 m_filteredOut
.push_back(toshow
);
158 m_filteredOut
.push_back(toshow
);
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.
169 CSearchFile
* parent
= toshow
->GetParent();
172 CSearchFile
* before
= (CSearchFile
*)GetItemData(newid
- 1);
175 wxASSERT((before
->GetParent() == parent
) || (before
== parent
));
177 wxASSERT(before
->GetParent() != toshow
);
181 if ((int)newid
< GetItemCount() - 1) {
182 CSearchFile
* after
= (CSearchFile
*)GetItemData(newid
+ 1);
185 wxASSERT((after
->GetParent() == parent
) || (!after
->GetParent()));
187 wxASSERT((after
->GetParent() == toshow
) || (!after
->GetParent()));
193 SetItemPtrData(newid
, toshowdata
);
196 SetItem(newid
, ID_SEARCH_COL_SIZE
, CastItoXBytes( toshow
->GetFileSize() ) );
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());
207 if (toshow
->GetKadPublishInfo() == 0) {
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);
213 SetItem( newid
, ID_SEARCH_COL_SOURCES
, temp
);
216 SetItem( newid
, ID_SEARCH_COL_TYPE
, GetFiletypeByName( toshow
->GetFileName() ) );
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
));
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
));
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());
257 if (toupdate
->GetKadPublishInfo() == 0) {
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);
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
)) {
276 void CSearchListCtrl::UpdateItemColor( long 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());
289 sameFile
= theApp
->knownfiles
->FindKnownFileByID(file
->GetFileHash());
292 int red
= newcol
.Red();
293 int green
= newcol
.Green();
294 int blue
= newcol
.Blue();
297 if ( sameFile
->IsPartFile() ) {
298 // File is already being downloaded. Mark as red.
300 } else if ( sameFile
->GetStatus() == PS_COMPLETE
) {
301 // File has already been downloaded. Mark as green.
304 // File has been cancelled or removed. Mark as grey.
310 // File is new, colour after number of files
311 blue
+= file
->GetSourceCount() * 5;
317 // don't forget to set the item data back...
319 newitem
.SetId( index
);
320 newitem
.SetTextColour( wxColour( red
, green
, blue
) );
326 // Update the colors of all assosiated items, which means parents and/or siblings.
327 void CSearchListCtrl::UpdateAllRelativesColor(
331 if ((file
->ShowChildren() && file
->HasChildren()) ||
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
)));
341 UpdateItemColor(index
);
346 void CSearchListCtrl::ShowResults( long ResultsID
)
349 m_nResultsID
= 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()
365 void CSearchListCtrl::SetFilter(const wxString
& regExp
, bool invert
, bool filterKnown
)
367 if (regExp
.IsEmpty()) {
369 m_filterText
= wxT(".*");
371 m_filterText
= regExp
;
374 m_filter
.Compile(m_filterText
, wxRE_DEFAULT
| wxRE_ICASE
);
375 m_filterKnown
= filterKnown
;
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
)) {
390 m_filteredOut
.push_back(file
);
395 // Check the previously filtered items.
396 ResultList::iterator it
= curFiltered
.begin();
397 for (; it
!= curFiltered
.end(); ++it
) {
398 if (IsFiltered(*it
)) {
401 m_filteredOut
.push_back(*it
);
408 void CSearchListCtrl::EnableFiltering(bool enabled
)
410 if (enabled
!= m_filterEnabled
) {
411 m_filterEnabled
= enabled
;
414 SetFilter(m_filterText
, m_invert
, m_filterKnown
);
416 ResultList::iterator it
= m_filteredOut
.begin();
417 for (; it
!= m_filteredOut
.end(); ++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
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());
446 result
= !theApp
->knownfiles
->FindKnownFileByID(file
->GetFileHash());
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
) {
477 return SortProc(parent1
, filePtr2
, sortData
);
479 } else if (parent2
) {
480 if (parent2
== filePtr1
) {
483 return SortProc(filePtr1
, parent2
, sortData
);
488 switch (sortData
& CMuleListCtrl::COLUMN_MASK
) {
490 case ID_SEARCH_COL_NAME
:
491 result
= CmpAny(file1
->GetFileName(), file2
->GetFileName());
495 case ID_SEARCH_COL_SIZE
:
496 result
= CmpAny( file1
->GetFileSize(), file2
->GetFileSize() );
500 case ID_SEARCH_COL_SOURCES
: {
501 int cmp
= CmpAny( file1
->GetSourceCount(), file2
->GetSourceCount() );
502 int cmp2
= CmpAny( file1
->GetCompleteSourceCount(), file2
->GetCompleteSourceCount() );
519 // Sort by file-types
520 case ID_SEARCH_COL_TYPE
: {
521 result
= GetFiletypeByName(file1
->GetFileName()).Cmp(GetFiletypeByName(file2
->GetFileName()));
523 // Same file-type, sort by extension
524 result
= CmpAny(file1
->GetFileName().GetExt(), file2
->GetFileName().GetExt());
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"));
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
) );
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"
599 menu
.Append(MP_MARK_AS_KNOWN
, _("Mark as known file"));
600 menu
.AppendSeparator();
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());
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
) )
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
);
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
);
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
);
688 CSearchFile
*searchFile
= (CSearchFile
*)GetItemData(index
);
689 CKnownFile
*knownFile(new CKnownFile(*searchFile
));
690 theApp
->knownfiles
->SafeAddKFile(knownFile
);
691 UpdateAllRelativesColor(searchFile
, index
);
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
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());
719 bool CSearchListCtrl::AltSortAllowed(unsigned column
) const
722 case ID_SEARCH_COL_SOURCES
:
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
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
);
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
769 dc
->SetBackground(GetBrush(wxSYS_COLOUR_HIGHLIGHT
));
770 dc
->SetTextForeground(CMuleColour(wxSYS_COLOUR_HIGHLIGHTTEXT
));
772 dc
->SetBackground(GetBrush(wxSYS_COLOUR_BTNSHADOW
));
773 dc
->SetTextForeground(CMuleColour(wxSYS_COLOUR_HIGHLIGHTTEXT
));
776 dc
->SetBackground(GetBrush(wxSYS_COLOUR_LISTBOX
));
777 dc
->SetTextForeground(CMuleColour(wxSYS_COLOUR_WINDOWTEXT
));
780 // Define the border of the drawn area
782 dc
->SetPen(*(wxThePenList
->FindOrCreatePen(CMuleColour(dc
->GetBackground().GetColour()).Blend(65), 1, wxSOLID
)));
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
++) {
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
;
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()) {
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;
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;
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());
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)
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
876 dc
->DrawLine(treeCenter
, middle
, treeCenter
, cur_rec
.y
+ cur_rec
.height
+ 1);
879 // Draw the line back up to parent node
881 dc
->DrawLine(treeCenter
, middle
, treeCenter
, cur_rec
.y
- 1);
883 } else if (file
->HasChildren()) {
884 if (file
->ShowChildren()) {
886 dc
->SetBrush(*wxTRANSPARENT_BRUSH
);
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.
903 CSearchFile
* parent
= file
->GetParent();
906 CSearchFile
* before
= (CSearchFile
*)GetItemData(item
- 1);
909 wxASSERT((before
->GetParent() == parent
) || (before
== parent
));
911 wxASSERT(before
->GetParent() != file
);
915 if (item
< GetItemCount() - 1) {
916 CSearchFile
* after
= (CSearchFile
*)GetItemData(item
+ 1);
919 wxASSERT((after
->GetParent() == parent
) || (!after
->GetParent()));
921 wxASSERT((after
->GetParent() == file
) || (!after
->GetParent()));
929 void CSearchListCtrl::ShowChildren(CSearchFile
* file
, bool show
)
933 file
->SetShowChildren(show
);
935 const CSearchResultList
& results
= file
->GetChildren();
936 for (size_t i
= 0; i
< results
.size(); ++i
) {
938 AddResult(results
[i
]);
940 RemoveResult(results
[i
]);
948 wxString
CSearchListCtrl::GetTTSText(unsigned item
) const
950 return GetItemText(item
);
952 // File_checked_for_headers