Correctly deallocate buffer
[TortoiseGit.git] / src / Utils / MiscUI / HighColorTab.hpp
blob28fdf1bd3423edd19047ad2760d7a252d9a15c71
1 // HighColorTab.hpp
2 //
3 // Author: Yves Tkaczyk (yves@tkaczyk.net)
4 //
5 // This software is released into the public domain. You are free to use it
6 // in any way you like BUT LEAVE THIS HEADER INTACT.
7 //
8 // This software is provided "as is" with no expressed or implied warranty.
9 // I accept no liability for any damage or loss of business that this software
10 // may cause.
12 ///////////////////////////////////////////////////////////////////////////////
14 #ifndef _HIGHCOLORTAB_HPP_INCLUDED_
15 #define _HIGHCOLORTAB_HPP_INCLUDED_
17 #if _MSC_VER > 1000
18 #pragma once
19 #endif // _MSC_VER > 1000
21 #include <memory>
23 namespace HighColorTab
25 /*! \brief Policy class for creating image list.
27 Policy for creating a high color (32 bits) image list. The policy
28 ensure that there is a Win32 image list associated with the CImageList.
29 If this is not the case, a NULL pointer shall be returned.
31 Returned image list is wrapped in an std::auto_ptr.
33 \sa UpdateImageListFull */
34 struct CHighColorListCreator
36 /*! Create the image list.
37 \retval std::auto_ptr<CImageList> Not null if success. */
38 static std::auto_ptr<CImageList> CreateImageList()
40 std::auto_ptr<CImageList> apILNew( new CImageList() );
41 if( NULL == apILNew.get() )
43 // ASSERT: The CImageList object creation failed.
44 ASSERT( FALSE );
45 return std::auto_ptr<CImageList>();
48 if( 0 == apILNew->Create( 16, 16, ILC_COLOR32|ILC_MASK, 0, 1 ) )
50 // ASSERT: The image list (Win32) creation failed.
51 ASSERT( FALSE );
52 return std::auto_ptr<CImageList>();
55 return apILNew;
61 /*! \brief Change the image list of the provided control (property sheet interface)
63 This method provides full customization via policy over image list creation. The policy
64 must have a method with the signature:
65 <code>static std::auto_ptr<CImageList> CreateImageList()</code>
67 \author Yves Tkaczyk (yves@tkaczyk.net)
68 \date 02/2004 */
69 template<typename TSheet,
70 typename TListCreator>
71 bool UpdateImageListFull(TSheet& rSheet)
73 // Get the tab control...
74 CTabCtrl* pTab = rSheet.GetTabControl();
75 if (!IsWindow(pTab->GetSafeHwnd()))
77 // ASSERT: Tab control could not be retrieved or it is not a valid window.
78 ASSERT( FALSE );
79 return false;
82 // Create the replacement image list via policy.
83 std::auto_ptr<CImageList> apILNew( TListCreator::CreateImageList() );
85 bool bSuccess = (NULL != apILNew.get() );
87 // Reload the icons from the property pages.
88 int nTotalPageCount = rSheet.GetPageCount();
89 for(int nCurrentPage = 0; nCurrentPage < nTotalPageCount && bSuccess; ++nCurrentPage )
91 // Get the page.
92 CPropertyPage* pPage = rSheet.GetPage( nCurrentPage );
93 ASSERT( pPage );
94 // Set the icon in the image list from the page properties.
95 if( pPage && ( pPage->m_psp.dwFlags & PSP_USEHICON ) )
97 bSuccess &= ( -1 != apILNew->Add( pPage->m_psp.hIcon ) );
100 if( pPage && ( pPage->m_psp.dwFlags & PSP_USEICONID ) )
102 bSuccess &= ( -1 != apILNew->Add( AfxGetApp()->LoadIcon( pPage->m_psp.pszIcon ) ) );
106 if( !bSuccess )
108 // This ASSERT because either the image list could not be created or icon insertion failed.
109 ASSERT( FALSE );
110 // Cleanup what we have in the new image list.
111 if( apILNew.get() )
113 apILNew->DeleteImageList();
116 return false;
119 // Replace the image list from the tab control.
120 CImageList* pilOld = pTab->SetImageList( CImageList::FromHandle( apILNew->Detach() ) );
121 // Clean the old image list if there was one.
122 if( pilOld )
124 pilOld->DeleteImageList();
127 return true;
130 /*! \brief Change the image list of the provided control (property sheet)
132 This method uses 32 bits image list creation default policy. */
133 template<typename TSheet>
134 bool UpdateImageList(TSheet& rSheet)
136 return UpdateImageListFull<TSheet, HighColorTab::CHighColorListCreator>( rSheet );
140 #endif // _HIGHCOLORTAB_HPP_INCLUDED_