foobar2000 SDK  2015-08-03
WTL-PP.h
Go to the documentation of this file.
1 // Various WTL extensions that are not fb2k specific and can be reused in other WTL based software
2 
3 #define ATLASSERT_SUCCESS(X) {auto RetVal = (X); ATLASSERT( RetVal ); }
4 
5 class NoRedrawScope {
6 public:
7  NoRedrawScope(HWND p_wnd) throw() : m_wnd(p_wnd) {
8  m_wnd.SetRedraw(FALSE);
9  }
10  ~NoRedrawScope() throw() {
11  m_wnd.SetRedraw(TRUE);
12  }
13 private:
14  CWindow m_wnd;
15 };
16 
18 public:
19  NoRedrawScopeEx(HWND p_wnd) throw() : m_wnd(p_wnd), m_active() {
20  if (m_wnd.IsWindowVisible()) {
21  m_active = true;
22  m_wnd.SetRedraw(FALSE);
23  }
24  }
25  ~NoRedrawScopeEx() throw() {
26  if (m_active) {
27  m_wnd.SetRedraw(TRUE);
28  m_wnd.RedrawWindow(NULL,NULL,RDW_INVALIDATE|RDW_ERASE|RDW_ALLCHILDREN);
29  }
30  }
31 private:
32  bool m_active;
33  CWindow m_wnd;
34 };
35 
36 
37 #define MSG_WM_TIMER_EX(timerId, func) \
38  if (uMsg == WM_TIMER && (UINT_PTR)wParam == timerId) \
39  { \
40  SetMsgHandled(TRUE); \
41  func(); \
42  lResult = 0; \
43  if(IsMsgHandled()) \
44  return TRUE; \
45  }
46 
47 #define MESSAGE_HANDLER_SIMPLE(msg, func) \
48  if(uMsg == msg) \
49  { \
50  SetMsgHandled(TRUE); \
51  func(); \
52  lResult = 0; \
53  if(IsMsgHandled()) \
54  return TRUE; \
55  }
56 
57 // void OnSysCommandHelp()
58 #define MSG_WM_SYSCOMMAND_HELP(func) \
59  if (uMsg == WM_SYSCOMMAND && wParam == SC_CONTEXTHELP) \
60  { \
61  SetMsgHandled(TRUE); \
62  func(); \
63  lResult = 0; \
64  if(IsMsgHandled()) \
65  return TRUE; \
66  }
67 
68 //BOOL ProcessWindowMessage(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT& lResult, DWORD dwMsgMapID)
69 #define END_MSG_MAP_HOOK() \
70  break; \
71  default: \
72  return __super::ProcessWindowMessage(hWnd, uMsg, wParam, lParam, lResult, dwMsgMapID); \
73  } \
74  return FALSE; \
75  }
76 
77 
79 public:
81  ~CImageListContainer() {Destroy();}
82 private:
83  const CImageListContainer & operator=(const CImageListContainer&);
85 };
86 
87 
88 template<bool managed> class CThemeT {
89 public:
90  CThemeT(HTHEME source = NULL) : m_theme(source) {}
91 
93  Release();
94  }
95 
96  HTHEME OpenThemeData(HWND wnd,LPCWSTR classList) {
97  Release();
98  return m_theme = ::OpenThemeData(wnd, classList);
99  }
100 
101  void Release() {
102  HTHEME releaseme = pfc::replace_null_t(m_theme);
103  if (managed && releaseme != NULL) CloseThemeData(releaseme);
104  }
105 
106  operator HTHEME() const {return m_theme;}
107  HTHEME m_theme;
108 };
111 
112 
113 class CCheckBox : public CButton {
114 public:
115  void ToggleCheck(bool state) {SetCheck(state ? BST_CHECKED : BST_UNCHECKED);}
116  bool IsChecked() const {return GetCheck() == BST_CHECKED;}
117 
118  CCheckBox(HWND hWnd = NULL) : CButton(hWnd) { }
119  CCheckBox & operator=(HWND wnd) {m_hWnd = wnd; return *this; }
120 };
121 
122 
123 class CEditNoEscSteal : public CContainedWindowT<CEdit>, private CMessageMap {
124 public:
125  CEditNoEscSteal() : CContainedWindowT<CEdit>(this, 0) {}
127  MSG_WM_GETDLGCODE(OnEditGetDlgCode)
128  END_MSG_MAP()
129 private:
130  UINT OnEditGetDlgCode(LPMSG lpMsg) {
131  if (lpMsg == NULL) {
132  SetMsgHandled(FALSE); return 0;
133  } else {
134  switch(lpMsg->message) {
135  case WM_KEYDOWN:
136  case WM_SYSKEYDOWN:
137  switch(lpMsg->wParam) {
138  case VK_ESCAPE:
139  return 0;
140  default:
141  SetMsgHandled(FALSE); return 0;
142  }
143  default:
144  SetMsgHandled(FALSE); return 0;
145 
146  }
147  }
148  }
149 };
150 
151 class CEditNoEnterEscSteal : public CContainedWindowT<CEdit>, private CMessageMap {
152 public:
153  CEditNoEnterEscSteal() : CContainedWindowT<CEdit>(this, 0) {}
155  MSG_WM_GETDLGCODE(OnEditGetDlgCode)
156  END_MSG_MAP()
157 private:
158  UINT OnEditGetDlgCode(LPMSG lpMsg) {
159  if (lpMsg == NULL) {
160  SetMsgHandled(FALSE); return 0;
161  } else {
162  switch(lpMsg->message) {
163  case WM_KEYDOWN:
164  case WM_SYSKEYDOWN:
165  switch(lpMsg->wParam) {
166  case VK_ESCAPE:
167  case VK_RETURN:
168  return 0;
169  default:
170  SetMsgHandled(FALSE); return 0;
171  }
172  default:
173  SetMsgHandled(FALSE); return 0;
174 
175  }
176  }
177  }
178 };
179 
180 
181 
183 public:
185  const TCHAR * name;
186  void Set(const TCHAR * n) {ATLASSERT( name == NULL ); name = n; }
187  bool IsActive() const {return name != NULL;}
188  void CleanUp() {
189  const TCHAR * n = name; name = NULL;
190  if (n != NULL) ATLASSERT_SUCCESS( UnregisterClass(n, (HINSTANCE)&__ImageBase) );
191  }
193 };
194 
195 
196 // CWindowRegisteredT
197 // Minimalistic wrapper for registering own window classes that can be created by class name, included in dialogs and such.
198 // Usage:
199 // class myClass : public CWindowRegisteredT<myClass> {...};
200 // Call myClass::Register() before first use
201 template<typename TClass, typename TBaseClass = CWindow>
203 public:
204  static UINT GetClassStyle() {
205  return CS_VREDRAW | CS_HREDRAW;
206  }
207  static HCURSOR GetCursor() {
208  return ::LoadCursor(NULL, IDC_ARROW);
209  }
210 
212  END_MSG_MAP()
213 
214 
215  static void Register() {
216  static CWindowClassUnregisterScope scope;
217  if (!scope.IsActive()) {
218  WNDCLASS wc = {};
219  wc.style = TClass::GetClassStyle();
220  wc.cbWndExtra = sizeof(void*);
221  wc.lpszClassName = TClass::GetClassName();
222  wc.lpfnWndProc = myWindowProc;
223  wc.hInstance = (HINSTANCE)&__ImageBase;
224  wc.hCursor = TClass::GetCursor();
225  ATLASSERT_SUCCESS( RegisterClass(&wc) != 0 );
226  scope.Set(wc.lpszClassName);
227  }
228  }
229 protected:
230  virtual ~CWindowRegisteredT() {}
231 private:
232  static LRESULT CALLBACK myWindowProc(HWND wnd, UINT msg, WPARAM wp, LPARAM lp) {
233  TClass * i = NULL;
234  if (msg == WM_NCCREATE) {
235  i = new TClass;
236  i->Attach(wnd);
237  ::SetWindowLongPtr(wnd, 0, reinterpret_cast<LONG_PTR>(i));
238  } else {
239  i = reinterpret_cast<TClass*>( ::GetWindowLongPtr(wnd, 0) );
240  }
241  LRESULT r;
242  if (i == NULL || !i->ProcessWindowMessage(wnd, msg, wp, lp, r)) r = ::DefWindowProc(wnd, msg, wp, lp);
243  if (msg == WM_NCDESTROY) {
244  ::SetWindowLongPtr(wnd, 0, 0);
245  delete i;
246  }
247  return r;
248  }
249 };
250 
251 
252 
253 
254 class CSRWlock {
255 public:
256  CSRWlock() : theLock() {
257 #if _WIN32_WINNT < 0x600
258  auto dll = GetModuleHandle(_T("kernel32"));
259  Bind(AcquireSRWLockExclusive, dll, "AcquireSRWLockExclusive");
260  Bind(AcquireSRWLockShared, dll, "AcquireSRWLockShared");
261  Bind(ReleaseSRWLockExclusive, dll, "ReleaseSRWLockExclusive");
262  Bind(ReleaseSRWLockShared, dll, "ReleaseSRWLockShared");
263 #endif
264  }
265 
266  bool HaveAPI() {
267 #if _WIN32_WINNT < 0x600
268  return AcquireSRWLockExclusive != NULL;
269 #else
270  return true;
271 #endif
272  }
273 
274  void EnterShared() {
275  AcquireSRWLockShared( & theLock );
276  }
277  void EnterExclusive() {
278  AcquireSRWLockExclusive( & theLock );
279  }
280  void LeaveShared() {
281  ReleaseSRWLockShared( & theLock );
282  }
283  void LeaveExclusive() {
284  ReleaseSRWLockExclusive( &theLock );
285  }
286 
287 private:
288  CSRWlock(const CSRWlock&);
289  void operator=(const CSRWlock&);
290 
291  SRWLOCK theLock;
292 #if _WIN32_WINNT < 0x600
293  template<typename func_t> static void Bind(func_t & func, HMODULE dll, const char * name) {
294  func = reinterpret_cast<func_t>(GetProcAddress( dll, name ) );
295  }
296 
297  VOID (WINAPI * AcquireSRWLockExclusive)(PSRWLOCK SRWLock);
298  VOID (WINAPI * AcquireSRWLockShared)(PSRWLOCK SRWLock);
299  VOID (WINAPI * ReleaseSRWLockExclusive)(PSRWLOCK SRWLock);
300  VOID (WINAPI * ReleaseSRWLockShared)(PSRWLOCK SRWLock);
301 #endif
302 };
303 
304 #if _WIN32_WINNT < 0x600
305 class CSRWorCS {
306 public:
307  CSRWorCS() : cs() {
308  if (!srw.HaveAPI()) InitializeCriticalSection(&cs);
309  }
311  if (!srw.HaveAPI()) DeleteCriticalSection(& cs );
312  }
313  void EnterShared() {
314  if (srw.HaveAPI()) srw.EnterShared();
315  else EnterCriticalSection(&cs);
316  }
317  void EnterExclusive() {
318  if (srw.HaveAPI()) srw.EnterExclusive();
319  else EnterCriticalSection(&cs);
320  }
321  void LeaveShared() {
322  if (srw.HaveAPI()) srw.LeaveShared();
323  else LeaveCriticalSection(&cs);
324  }
325  void LeaveExclusive() {
326  if (srw.HaveAPI()) srw.LeaveExclusive();
327  else LeaveCriticalSection(&cs);
328  }
329 private:
330  CSRWorCS(const CSRWorCS&);
331  void operator=(const CSRWorCS&);
332 
334  CRITICAL_SECTION cs;
335 };
336 #else
338 #endif
BEGIN_MSG_MAP_EX(CEditNoEscSteal) MSG_WM_GETDLGCODE(OnEditGetDlgCode) END_MSG_MAP() private
Definition: WTL-PP.h:154
CWindow m_wnd
Definition: WTL-PP.h:14
void EnterShared()
Definition: WTL-PP.h:274
CThemeT< true > CTheme
Definition: WTL-PP.h:110
void LeaveExclusive()
Definition: WTL-PP.h:325
CSRWlock srw
Definition: WTL-PP.h:333
CThemeT(HTHEME source=NULL)
Definition: WTL-PP.h:90
CThemeT< false > CThemeHandle
Definition: WTL-PP.h:109
t_type replace_null_t(t_type &p_var)
Definition: primitives.h:688
CRITICAL_SECTION cs
Definition: WTL-PP.h:334
SRWLOCK theLock
Definition: WTL-PP.h:291
NoRedrawScopeEx(HWND p_wnd)
Definition: WTL-PP.h:19
NoRedrawScope(HWND p_wnd)
Definition: WTL-PP.h:7
void EnterExclusive()
Definition: WTL-PP.h:317
~CSRWorCS()
Definition: WTL-PP.h:310
CCheckBox & operator=(HWND wnd)
Definition: WTL-PP.h:119
static UINT GetClassStyle()
Definition: WTL-PP.h:204
void LeaveShared()
Definition: WTL-PP.h:321
void ToggleCheck(bool state)
Definition: WTL-PP.h:115
static void Bind(func_t &func, HMODULE dll, const char *name)
Definition: WTL-PP.h:293
CSRWlock()
Definition: WTL-PP.h:256
void EnterExclusive()
Definition: WTL-PP.h:277
void EnterShared()
Definition: WTL-PP.h:313
CCheckBox(HWND hWnd=NULL)
Definition: WTL-PP.h:118
BEGIN_MSG_MAP_EX(CWindowRegisteredT) END_MSG_MAP() static void Register()
Definition: WTL-PP.h:211
void Release()
Definition: WTL-PP.h:101
void InitializeCriticalSection(LPCRITICAL_SECTION lpCriticalSection)
Definition: pp-winapi.h:24
BEGIN_MSG_MAP_EX(CEditNoEscSteal) MSG_WM_GETDLGCODE(OnEditGetDlgCode) END_MSG_MAP() private
Definition: WTL-PP.h:126
static LRESULT CALLBACK myWindowProc(HWND wnd, UINT msg, WPARAM wp, LPARAM lp)
Definition: WTL-PP.h:232
bool m_active
Definition: WTL-PP.h:32
CSRWlock CSRWorCS
Definition: WTL-PP.h:337
CSRWorCS()
Definition: WTL-PP.h:307
bool HaveAPI()
Definition: WTL-PP.h:266
~NoRedrawScopeEx()
Definition: WTL-PP.h:25
virtual ~CWindowRegisteredT()
Definition: WTL-PP.h:230
HTHEME m_theme
Definition: WTL-PP.h:107
~NoRedrawScope()
Definition: WTL-PP.h:10
void LeaveExclusive()
Definition: WTL-PP.h:283
Definition: WTL-PP.h:88
const TCHAR * name
Definition: WTL-PP.h:185
CWindow m_wnd
Definition: WTL-PP.h:33
void LeaveShared()
Definition: WTL-PP.h:280
HTHEME OpenThemeData(HWND wnd, LPCWSTR classList)
Definition: WTL-PP.h:96
void Set(const TCHAR *n)
Definition: WTL-PP.h:186
bool IsActive() const
Definition: WTL-PP.h:187
~CThemeT()
Definition: WTL-PP.h:92
bool IsChecked() const
Definition: WTL-PP.h:116
static HCURSOR GetCursor()
Definition: WTL-PP.h:207