-
Notifications
You must be signed in to change notification settings - Fork 0
/
HighColorTab.hpp
155 lines (128 loc) · 4.44 KB
/
HighColorTab.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// HighColorTab.hpp
//
// Author: Yves Tkaczyk (yves@tkaczyk.net)
//
// This software is released into the public domain. You are free to use it
// in any way you like BUT LEAVE THIS HEADER INTACT.
//
// This software is provided "as is" with no expressed or implied warranty.
// I accept no liability for any damage or loss of business that this software
// may cause.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef _HIGHCOLORTAB_HPP_INCLUDED_
#define _HIGHCOLORTAB_HPP_INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <memory>
namespace HighColorTab
{
/*! \brief Policy class for creating image list.
Policy for creating a high color (32 bits) image list. The policy
ensure that there is a Win32 image list associated with the CImageList.
If this is not the case, a NULL pointer shall be returned.
Returned image list is wrapped in an std::auto_ptr.
\sa UpdateImageListFull */
struct CHighColorListCreator
{
/*! Create the image list.
\retval std::auto_ptr<CImageList> Not null if success. */
static std::auto_ptr<CImageList> CreateImageList()
{
std::auto_ptr<CImageList> apILNew( new CImageList() );
if( NULL == apILNew.get() )
{
// ASSERT: The CImageList object creation failed.
ASSERT( FALSE );
return std::auto_ptr<CImageList>();
}
if( 0 == apILNew->Create( 16, 16, ILC_COLOR32|ILC_MASK, 0, 1 ) )
{
// ASSERT: The image list (Win32) creation failed.
ASSERT( FALSE );
return std::auto_ptr<CImageList>();
}
return apILNew;
}
};
/*! \brief Change the image list of the provided control (property sheet interface)
This method provides full customization via policy over image list creation. The policy
must have a method with the signature:
<code>static std::auto_ptr<CImageList> CreateImageList()</code>
\author Yves Tkaczyk (yves@tkaczyk.net)
\date 02/2004 */
template<typename TSheet,
typename TListCreator>
bool UpdateImageListFull(TSheet& rSheet)
{
// Get the tab control...
CTabCtrl* pTab = rSheet.GetTabControl();
if (!IsWindow(pTab->GetSafeHwnd()))
{
// ASSERT: Tab control could not be retrieved or it is not a valid window.
ASSERT( FALSE );
return false;
}
/*
CRect rc;
CSize sz;
sz.cx = 32;
sz.cy = 32;
pTab->GetItemRect(0, &rc);
pTab->GetWindowRect(&rc);
rc.bottom += 16;
pTab->SetWindowPos(NULL, 0, 0,
rc.Width(), rc.Height(),
SWP_NOMOVE | SWP_NOOWNERZORDER |
SWP_NOZORDER | SWP_NOACTIVATE);
*/
// Create the replacement image list via policy.
std::auto_ptr<CImageList> apILNew( TListCreator::CreateImageList() );
bool bSuccess = (NULL != apILNew.get() );
// Reload the icons from the property pages.
int nTotalPageCount = rSheet.GetPageCount();
for(int nCurrentPage = 0; nCurrentPage < nTotalPageCount && bSuccess; ++nCurrentPage )
{
// Get the page.
CPropertyPage* pPage = rSheet.GetPage( nCurrentPage );
ASSERT( pPage );
// Set the icon in the image list from the page properties.
if( pPage && ( pPage->m_psp.dwFlags & PSP_USEHICON ) )
{
bSuccess &= ( -1 != apILNew->Add( pPage->m_psp.hIcon ) );
}
if( pPage && ( pPage->m_psp.dwFlags & PSP_USEICONID ) )
{
bSuccess &= ( -1 != apILNew->Add( AfxGetApp()->LoadIcon( pPage->m_psp.pszIcon ) ) );
}
}
if( !bSuccess )
{
// This ASSERT because either the image list could not be created or icon insertion failed.
ASSERT( FALSE );
// Cleanup what we have in the new image list.
if( apILNew.get() )
{
apILNew->DeleteImageList();
}
return false;
}
// Replace the image list from the tab control.
CImageList* pilOld = pTab->SetImageList( CImageList::FromHandle( apILNew->Detach() ) );
// Clean the old image list if there was one.
if( pilOld )
{
pilOld->DeleteImageList();
}
return true;
};
/*! \brief Change the image list of the provided control (property sheet)
This method uses 32 bits image list creation default policy. */
template<typename TSheet>
bool UpdateImageList(TSheet& rSheet)
{
return UpdateImageListFull<TSheet, HighColorTab::CHighColorListCreator>( rSheet );
};
};
#endif // _HIGHCOLORTAB_HPP_INCLUDED_