Skip to content

Commit

Permalink
removed GetTheTreeListControl, and dependencies thereon
Browse files Browse the repository at this point in the history
  • Loading branch information
ariccio committed Jan 31, 2015
1 parent 9ffb12e commit 45b72d9
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 63 deletions.
2 changes: 1 addition & 1 deletion WinDirStat/windirstat/SelectDrivesDlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class CDriveItem final : public COwnerDrawnListItem {

private:
virtual INT Compare ( _In_ const COwnerDrawnListItem* const other, RANGE_ENUM_COL const column::ENUM_COL subitem ) const override final;
virtual bool DrawSubitem( RANGE_ENUM_COL const column::ENUM_COL subitem, _In_ CDC& pdc, _In_ RECT rc, _In_ const UINT state, _Out_opt_ _Deref_out_range_( 0, 100 ) INT* const width, _Inout_ INT* const focusLeft ) const override final {
virtual bool DrawSubitem( RANGE_ENUM_COL const column::ENUM_COL subitem, _In_ CDC& pdc, _In_ RECT rc, _In_ const UINT state, _Out_opt_ _Deref_out_range_( 0, 100 ) INT* const width, _Inout_ INT* const focusLeft, _In_ COwnerDrawnListCtrl* const list ) const override final {
UNREFERENCED_PARAMETER( pdc );
UNREFERENCED_PARAMETER( state );
UNREFERENCED_PARAMETER( rc );
Expand Down
90 changes: 54 additions & 36 deletions WinDirStat/windirstat/TreeListControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,35 @@ namespace
return path;
}

int __cdecl _compareProc_orig( const void* const p1, const void* const p2 ) {
const auto item1 = * ( reinterpret_cast< const CTreeListItem* const* >( p1 ) );
const auto item2 = * ( reinterpret_cast< const CTreeListItem* const* >( p2 ) );
return item1->CompareS( item2, CTreeListItem::GetTreeListControl( )->m_sorting );
}


//int __cdecl _compareProc_orig( const void* const p1, const void* const p2 ) {
// const auto item1 = * ( reinterpret_cast< const CTreeListItem* const* >( p1 ) );
// const auto item2 = * ( reinterpret_cast< const CTreeListItem* const* >( p2 ) );
// return item1->CompareS( item2, CTreeListItem::GetTreeListControl( )->m_sorting );
// }

}

struct compare_CTreeListItems {
compare_CTreeListItems( const CTreeListControl* const ctrl_in ) : ctrl( ctrl_in ) { }

bool operator()( const CTreeListItem* const lhs, const CTreeListItem* const rhs ) {
const auto result = lhs->CompareS( rhs, ctrl->m_sorting );
return result;
}
const CTreeListControl* const ctrl;
};


//CTreeListItem::~CTreeListItem( ) {
// //delete m_vi;
// //m_vi = { NULL };
// //m_parent = { NULL };
// }

//CRect rc is NOT const here so that other virtual functions may modify it?
bool CTreeListItem::DrawSubitem( RANGE_ENUM_COL const column::ENUM_COL subitem, _In_ CDC& pdc, _In_ RECT rc, _In_ const UINT state, _Out_opt_ INT* const width, _Inout_ INT* const focusLeft ) const {
bool CTreeListItem::DrawSubitem( RANGE_ENUM_COL const column::ENUM_COL subitem, _In_ CDC& pdc, _In_ RECT rc, _In_ const UINT state, _Out_opt_ INT* const width, _Inout_ INT* const focusLeft, _In_ COwnerDrawnListCtrl* const list ) const {
//ASSERT_VALID( pdc );
ASSERT( ( focusLeft != NULL ) && ( subitem >= 0 ) );

Expand All @@ -92,12 +105,13 @@ bool CTreeListItem::DrawSubitem( RANGE_ENUM_COL const column::ENUM_COL subitem,
if ( width != NULL ) {
*width = ( rc.right - rc.left );
}
const auto tree_list_control = GetTreeListControl( );
tree_list_control->DrawNode( this, pdc, rcNode, rcPlusMinus );//pass subitem to drawNode?
//const auto tree_list_control = GetTreeListControl( );
//tree_list_control->DrawNode( this, pdc, rcNode, rcPlusMinus );//pass subitem to drawNode?
static_cast<const CTreeListControl* const>( list )->DrawNode( this, pdc, rcNode, rcPlusMinus );//pass subitem to drawNode?

auto rcLabel = rc;
rcLabel.left = rcNode.right;
DrawLabel( tree_list_control, pdc, rcLabel, state, width, focusLeft, false );
DrawLabel( list, pdc, rcLabel, state, width, focusLeft, false );
if ( width != NULL ) {
*width = ( rcLabel.right - rcLabel.left );
}
Expand Down Expand Up @@ -139,7 +153,7 @@ void CTreeListItem::childNotNull( CItemBranch* const aTreeListChild, const size_
}

_Pre_satisfies_( this->m_vi._Myptr != nullptr )
void CTreeListItem::SortChildren( ) {
void CTreeListItem::SortChildren( const CTreeListControl* const ctrl ) {
ASSERT( IsVisible( ) );

const auto thisBranch = static_cast<const CItemBranch* >( this );
Expand All @@ -148,14 +162,19 @@ void CTreeListItem::SortChildren( ) {

if ( !m_vi->cache_sortedChildren.empty( ) ) {
//qsort( m_vi->cache_sortedChildren.data( ), m_vi->cache_sortedChildren.size( ) -1, sizeof( CTreeListItem * ), &_compareProc_orig );
std::sort( m_vi->cache_sortedChildren.begin( ), m_vi->cache_sortedChildren.end( ), &_compareProc2 );


//compare_CTreeListItems comp_functor( GetTreeListControl( ) );

//std::sort( m_vi->cache_sortedChildren.begin( ), m_vi->cache_sortedChildren.end( ), &_compareProc2 );
std::sort( m_vi->cache_sortedChildren.begin( ), m_vi->cache_sortedChildren.end( ), compare_CTreeListItems( ctrl ) );
}
}

bool CTreeListItem::_compareProc2( const CTreeListItem* const lhs, const CTreeListItem* const rhs ) {
auto result = lhs->CompareS( rhs, GetTreeListControl( )->m_sorting ) < 0;
return result;
}
//bool CTreeListItem::_compareProc2( const CTreeListItem* const lhs, const CTreeListItem* const rhs ) {
// auto result = lhs->CompareS( rhs, GetTreeListControl( )->m_sorting ) < 0;
// return result;
// }

std::uint64_t CTreeListItem::size_recurse_( ) const {
static_assert( std::is_same<decltype( std::declval<CTreeListItem>( ).size_recurse_( ) ), decltype( std::declval<CItemBranch>( ).size_recurse( ) )>::value , "The return type of CTreeListItem::size_recurse_ needs to be fixed!!" );
Expand Down Expand Up @@ -248,7 +267,7 @@ void CTreeListItem::SetVisible( _In_ const bool next_state_visible ) const {
m_vi.reset( );
}
ASSERT( m_vi == nullptr );
m_vi.reset( std::make_unique<VISIBLEINFO>( ) );
m_vi.reset( new VISIBLEINFO );
m_vi->isExpanded = false;
if ( m_parent == NULL ) {
m_vi->indent = 0;
Expand All @@ -274,20 +293,19 @@ void CTreeListItem::SetVisible( _In_ const bool next_state_visible ) const {
}
}

_Ret_notnull_ CTreeListControl* CTreeListItem::GetTreeListControl( ) {
// As we only have 1 TreeListControl and want to economize memory, we simple made the TreeListControl global.
const auto tlc = CTreeListControl::GetTheTreeListControl( );
ASSERT( tlc != NULL );
if ( tlc == NULL ) {
displayWindowsMsgBoxWithMessage( L"Serious error in CTreeListItem::GetTreeListControl: tlc == NULL, This should never happen!(aborting)" );
//throw std::logic_error( "This should never happen!" );
std::terminate( );

//need to 'call' abort because `/analyze` doesn't understand that std::terminate DOES NOT RETURN!
abort( );
}
return tlc;
}
//_Ret_notnull_ CTreeListControl* CTreeListItem::GetTreeListControl( ) {
// // As we only have 1 TreeListControl and want to economize memory, we simple made the TreeListControl global.
// const auto tlc = CTreeListControl::GetTheTreeListControl( );
// ASSERT( tlc != NULL );
// if ( tlc == NULL ) {
// displayWindowsMsgBoxWithMessage( L"Serious error in CTreeListItem::GetTreeListControl: tlc == NULL, This should never happen!(aborting)" );
// std::terminate( );
//
// //need to 'call' abort because `/analyze` doesn't understand that std::terminate DOES NOT RETURN!
// abort( );
// }
// return tlc;
// }

_Pre_satisfies_( this->m_vi._Myptr != nullptr )
CRect CTreeListItem::GetPlusMinusRect( ) const {
Expand All @@ -304,7 +322,7 @@ CRect CTreeListItem::GetTitleRect( ) const {
/////////////////////////////////////////////////////////////////////////////
// CTreeListControl

CTreeListControl* CTreeListControl::_theTreeListControl;
//CTreeListControl* CTreeListControl::_theTreeListControl;

IMPLEMENT_DYNAMIC( CTreeListControl, COwnerDrawnListCtrl )

Expand Down Expand Up @@ -635,7 +653,7 @@ BEGIN_MESSAGE_MAP(CTreeListControl, COwnerDrawnListCtrl)
ON_WM_DESTROY()
END_MESSAGE_MAP()

void CTreeListControl::DrawNodeNullWidth( _In_ const CTreeListItem* const item, _In_ CDC& pdc, _In_ const RECT& rcRest, _Inout_ bool& didBitBlt, _In_ CDC& dcmem, _In_ const UINT ysrc ) {
void CTreeListControl::DrawNodeNullWidth( _In_ const CTreeListItem* const item, _In_ CDC& pdc, _In_ const RECT& rcRest, _Inout_ bool& didBitBlt, _In_ CDC& dcmem, _In_ const UINT ysrc ) const {
auto ancestor = item;
//Not vectorized: 1304, loop includes assignments of different sizes
for ( auto indent = ( item->GetIndent( ) - 2 ); indent >= 0; indent-- ) {
Expand Down Expand Up @@ -832,7 +850,7 @@ int CTreeListControl::EnumNode( _In_ const CTreeListItem* const item ) const {
return static_cast<int>( ENUM_NODE::NODE_END );
}

void CTreeListControl::DrawNode( _In_ const CTreeListItem* const item, _In_ CDC& pdc, _Inout_ RECT& rc, _Out_ RECT& rcPlusMinus ) {
void CTreeListControl::DrawNode( _In_ const CTreeListItem* const item, _In_ CDC& pdc, _Inout_ RECT& rc, _Out_ RECT& rcPlusMinus ) const {
//ASSERT_VALID( pdc );
RECT rcRest = rc;
bool didBitBlt = false;
Expand Down Expand Up @@ -1124,7 +1142,7 @@ void CTreeListControl::ExpandItem( _In_ _In_range_( 0, INT_MAX ) const int i, _I
auto qpc_1 = help_QueryPerformanceCounter( );
#endif

item->SortChildren( );
item->SortChildren( this );

ExpandItemInsertChildren( item, i, scroll );

Expand All @@ -1137,7 +1155,7 @@ void CTreeListControl::ExpandItem( _In_ _In_range_( 0, INT_MAX ) const int i, _I
TRACE( _T( "Inserting items ( expansion ) took %f!\r\n" ), timing );
#endif

item->SortChildren( );
item->SortChildren( this );

//UnlockWindowUpdate( );
//item->SortChildren( );
Expand Down Expand Up @@ -1266,7 +1284,7 @@ void CTreeListControl::Sort( ) {
auto const Item = GetItem( i );
if ( Item != NULL ) {
if ( Item->IsExpanded( ) ) {
Item->SortChildren( );
Item->SortChildren( this );
}
}
ASSERT( Item != NULL );
Expand Down
34 changes: 17 additions & 17 deletions WinDirStat/windirstat/TreeListControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ struct VISIBLEINFO {
class CTreeListItem : public COwnerDrawnListItem {


virtual bool DrawSubitem ( RANGE_ENUM_COL const column::ENUM_COL subitem, _In_ CDC& pdc, _In_ RECT rc, _In_ const UINT state, _Out_opt_ INT* const width, _Inout_ INT* const focusLeft ) const override final;
virtual bool DrawSubitem ( RANGE_ENUM_COL const column::ENUM_COL subitem, _In_ CDC& pdc, _In_ RECT rc, _In_ const UINT state, _Out_opt_ INT* const width, _Inout_ INT* const focusLeft, _In_ COwnerDrawnListCtrl* const list ) const override final;
virtual INT Compare ( _In_ const COwnerDrawnListItem* const other, RANGE_ENUM_COL const column::ENUM_COL subitem ) const override final;

public:
Expand Down Expand Up @@ -126,7 +126,7 @@ class CTreeListItem : public COwnerDrawnListItem {
void SetVisible ( _In_ const bool next_state_visible = true ) const;

_Pre_satisfies_( this->m_vi._Myptr != nullptr )
void SortChildren ( );
void SortChildren ( const CTreeListControl* const ctrl );

_Pre_satisfies_( this->m_parent != NULL )
bool HasSiblings ( ) const;
Expand All @@ -153,10 +153,10 @@ class CTreeListItem : public COwnerDrawnListItem {
_Pre_satisfies_( this->m_vi._Myptr != nullptr )
CRect GetTitleRect( ) const;

static bool _compareProc2( const CTreeListItem* const lhs, const CTreeListItem* const rhs );
//static bool _compareProc2( const CTreeListItem* const lhs, const CTreeListItem* const rhs );

_Ret_notnull_
static CTreeListControl* GetTreeListControl( );
//static CTreeListControl* GetTreeListControl( );

public:
const CTreeListItem* m_parent;
Expand All @@ -181,22 +181,22 @@ class CTreeListControl final : public COwnerDrawnListCtrl {
CTreeListControl( const CTreeListControl& in ) = delete;


_Pre_satisfies_( _theTreeListControl != NULL )
static CTreeListControl *GetTheTreeListControl( ) {
ASSERT( _theTreeListControl != NULL );
return _theTreeListControl;
}
//_Pre_satisfies_( _theTreeListControl != NULL )
//static CTreeListControl *GetTheTreeListControl( ) {
// ASSERT( _theTreeListControl != NULL );
// return _theTreeListControl;
// }

_Pre_satisfies_( rowHeight % 2 == 0 )
CTreeListControl( _In_range_( 0, NODE_HEIGHT ) UINT rowHeight ) : COwnerDrawnListCtrl( global_strings::treelist_str, rowHeight ) {
ASSERT( _theTreeListControl == NULL );
_theTreeListControl = this;
//ASSERT( _theTreeListControl == NULL );
//_theTreeListControl = this;
ASSERT( rowHeight <= NODE_HEIGHT ); // größer können wir nicht//"larger, we can not"?
ASSERT( rowHeight % 2 == 0 ); // muss gerade sein//"must be straight"?
}

virtual ~CTreeListControl( ) {
_theTreeListControl = { NULL };
//_theTreeListControl = { NULL };
}

//#pragma warning( once : 4263 )
Expand Down Expand Up @@ -229,8 +229,8 @@ class CTreeListControl final : public COwnerDrawnListCtrl {
//void ExpandItem ( _In_ const CTreeListItem* const item );
void handle_VK_LEFT ( _In_ const CTreeListItem* const item, _In_ _In_range_( 0, INT32_MAX ) const int i );
void SetItemScrollPosition ( _In_ const CTreeListItem* const item, _In_ const INT top );
void DrawNodeNullWidth ( _In_ const CTreeListItem* const item, _In_ CDC& pdc, _In_ const RECT& rcRest, _Inout_ bool& didBitBlt, _In_ CDC& dcmem, _In_ const UINT ysrc );
void DrawNode ( _In_ const CTreeListItem* const item, _In_ CDC& pdc, _Inout_ RECT& rc, _Out_ RECT& rcPlusMinus );
void DrawNodeNullWidth ( _In_ const CTreeListItem* const item, _In_ CDC& pdc, _In_ const RECT& rcRest, _Inout_ bool& didBitBlt, _In_ CDC& dcmem, _In_ const UINT ysrc ) const;
void DrawNode ( _In_ const CTreeListItem* const item, _In_ CDC& pdc, _Inout_ RECT& rc, _Out_ RECT& rcPlusMinus ) const;

_Pre_satisfies_( ( parent + 1 ) < index )
void collapse_parent_plus_one_through_index ( _In_ const CTreeListItem* thisPath, _Inout_ _Out_range_( -1, INT_MAX ) int& index, _In_range_( 0, INT_MAX ) const int parent );
Expand Down Expand Up @@ -265,9 +265,9 @@ class CTreeListControl final : public COwnerDrawnListCtrl {
_Must_inspect_result_ _Success_( return != -1 )
INT GetSelectedItem( ) const;
void InitializeNodeBitmaps ( );
static CTreeListControl* _theTreeListControl;
CBitmap m_bmNodes0; // The bitmaps needed to draw the treecontrol-like branches
CBitmap m_bmNodes1; // The same bitmaps with stripe-background color
//static CTreeListControl* _theTreeListControl;
mutable CBitmap m_bmNodes0; // The bitmaps needed to draw the treecontrol-like branches
mutable CBitmap m_bmNodes1; // The same bitmaps with stripe-background color
INT m_lButtonDownItem; // Set in OnLButtonDown(). -1 if not item hit.
//C4820: 'CTreeListControl' : '3' bytes padding added after data member 'CTreeListControl::m_lButtonDownOnPlusMinusRect'
bool m_lButtonDownOnPlusMinusRect; // Set in OnLButtonDown(). True, if plus-minus-rect hit.
Expand Down
3 changes: 2 additions & 1 deletion WinDirStat/windirstat/dirstatdoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,10 +416,11 @@ void CDirstatDoc::ForgetItemTree( ) {

void CDirstatDoc::SortTreeList( ) {
ASSERT( m_rootItem != NULL );
m_rootItem->SortChildren( );

ASSERT( m_frameptr == GetMainFrame( ) );
const auto DirStatView = ( m_frameptr->GetDirstatView( ) );
if ( DirStatView != NULL ) {
m_rootItem->SortChildren( &( DirStatView->m_treeListControl ) );
DirStatView->m_treeListControl.Sort( );//awkward, roundabout way of sorting. TOTALLY breaks encapsulation. Deal with it.
}
}
Expand Down
2 changes: 1 addition & 1 deletion WinDirStat/windirstat/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class CItemBranch final : public CTreeListItem {


_Pre_satisfies_( this->m_parent == NULL )
void AddChildren( );
void AddChildren( _In_ CTreeListControl* const tree_list_control );

//data members - DON'T FUCK WITH LAYOUT! It's tweaked for good memory layout!

Expand Down
10 changes: 5 additions & 5 deletions WinDirStat/windirstat/ownerdrawnlistcontrol.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ class COwnerDrawnListItem {
//defined at bottom of THIS file.
void DrawSelection ( _In_ const COwnerDrawnListCtrl* const list, _In_ CDC& pdc, _In_ RECT rc, _In_ const UINT state ) const;

bool DrawSubitem_ ( RANGE_ENUM_COL const column::ENUM_COL subitem, _In_ CDC& pdc, _In_ RECT rc, _In_ const UINT state, _Out_opt_ INT* const width, _Inout_ INT* const focusLeft ) const {
return DrawSubitem( subitem, pdc, rc, state, width, focusLeft );
bool DrawSubitem_ ( RANGE_ENUM_COL const column::ENUM_COL subitem, _In_ CDC& pdc, _In_ RECT rc, _In_ const UINT state, _Out_opt_ INT* const width, _Inout_ INT* const focusLeft, _In_ COwnerDrawnListCtrl* const list ) const {
return DrawSubitem( subitem, pdc, rc, state, width, focusLeft, list );
}

COLORREF item_text_color( ) const {
Expand Down Expand Up @@ -143,7 +143,7 @@ class COwnerDrawnListItem {
virtual COLORREF ItemTextColor( ) const = 0;

// Return value is true, if the item draws itself. width != NULL -> only determine width, do not draw. If focus rectangle shall not begin leftmost, set *focusLeft to the left edge of the desired focus rectangle.
virtual bool DrawSubitem ( RANGE_ENUM_COL const column::ENUM_COL subitem, _In_ CDC& pdc, _In_ RECT rc, _In_ const UINT state, _Out_opt_ INT* const width, _Inout_ INT* const focusLeft ) const = 0;
virtual bool DrawSubitem ( RANGE_ENUM_COL const column::ENUM_COL subitem, _In_ CDC& pdc, _In_ RECT rc, _In_ const UINT state, _Out_opt_ INT* const width, _Inout_ INT* const focusLeft, _In_ COwnerDrawnListCtrl* const list ) const = 0;

public:
_Field_z_ _Field_size_( m_name_length ) std::unique_ptr<_Null_terminated_ const wchar_t[]> m_name;
Expand Down Expand Up @@ -247,7 +247,7 @@ namespace {
//draw the proper text in each column?

//focusLefts_temp[ i ] = rects_draw[ i ].left;
if ( !item->DrawSubitem_( subitems[ i ], dcmem, rects_draw[ i ], pdis->itemState, NULL, &focusLefts_temp[ i ] ) ) {//if DrawSubItem returns true, item draws self. Therefore `!item->DrawSubitem` is true when item DOES NOT draw self
if ( !item->DrawSubitem_( subitems[ i ], dcmem, rects_draw[ i ], pdis->itemState, NULL, &focusLefts_temp[ i ], owner_drawn_list_ctrl ) ) {//if DrawSubItem returns true, item draws self. Therefore `!item->DrawSubitem` is true when item DOES NOT draw self
owner_drawn_list_ctrl->DoDrawSubItemBecauseItCannotDrawItself( item, subitems[ i ], dcmem, rects_draw[ i ], pdis, showSelectionAlways, bIsFullRowSelection, is_right_aligned_cache );
}
}
Expand Down Expand Up @@ -1109,7 +1109,7 @@ class COwnerDrawnListCtrl : public CListCtrl {
//TODO: find a better way to do this!
//store item width in some sort of cache?
//BUGBUG: this is an extremely slow way of doing this!
if ( item->DrawSubitem_( subitem, dc, rc, 0, &width, &dummy ) ) {
if ( item->DrawSubitem_( subitem, dc, rc, 0, &width, &dummy, this ) ) {
if ( subitem == column::COL_NAME ) {
ASSERT( width == ( GetStringWidth( item->m_name.get( ) ) + static_cast<int>( GENERAL_INDENT ) + static_cast<int>( LABEL_INFLATE_CX ) + 2 ) );
}
Expand Down
2 changes: 1 addition & 1 deletion WinDirStat/windirstat/typeview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ CTypeView::CTypeView( ) : m_extensionListControl( this ), m_showTypes( true ) {

//CTypeView::~CTypeView( ) { }

bool CListItem::DrawSubitem( RANGE_ENUM_COL const column::ENUM_COL subitem, _In_ CDC& pdc, _In_ RECT rc, _In_ const UINT state, _Out_opt_ INT* const width, _Inout_ INT* const focusLeft ) const {
bool CListItem::DrawSubitem( RANGE_ENUM_COL const column::ENUM_COL subitem, _In_ CDC& pdc, _In_ RECT rc, _In_ const UINT state, _Out_opt_ INT* const width, _Inout_ INT* const focusLeft, _In_ COwnerDrawnListCtrl* const list ) const {
//ASSERT_VALID( pdc );
//Why are we bothering to draw this ourselves?
if ( subitem == column::COL_EXTENSION ) {
Expand Down

0 comments on commit 45b72d9

Please sign in to comment.