Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2012 lines
52 KiB

  1. // This is a part of the Active Template Library.
  2. // Copyright (C) 1996-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Active Template Library Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Active Template Library product.
  10. #ifndef __ATLDLGS_H__
  11. #define __ATLDLGS_H__
  12. #ifndef __cplusplus
  13. #error ATL requires C++ compilation (use a .cpp suffix)
  14. #endif
  15. #ifndef __ATLWIN_H__
  16. #error atldlgs.h requires atlwin.h to be included first
  17. #endif
  18. #if (_ATL_VER < 0x0300)
  19. #ifndef __ATLWIN21_H__
  20. #error atldlgs.h requires atlwin21.h to be included first when used with ATL 2.0/2.1
  21. #endif
  22. #endif //(_ATL_VER < 0x0300)
  23. #include <commdlg.h>
  24. #include <commctrl.h>
  25. #include <shlobj.h>
  26. namespace ATL
  27. {
  28. /////////////////////////////////////////////////////////////////////////////
  29. // Forward declarations
  30. template <class T> class CFileDialogImpl;
  31. class CFileDialog;
  32. #ifndef UNDER_CE
  33. template <class T> class CFolderDialogImpl;
  34. class CFolderDialog;
  35. template <class T> class CFontDialogImpl;
  36. class CFontDialog;
  37. #ifdef _RICHEDIT_
  38. template <class T> class CRichEditFontDialogImpl;
  39. class CRichEditFontDialog;
  40. #endif //_RICHEDIT_
  41. template <class T> class CColorDialogImpl;
  42. class CColorDialog;
  43. template <class T> class CPrintDialogImpl;
  44. class CPrintDialog;
  45. template <class T> class CPageSetupDialogImpl;
  46. class CPageSetupDialog;
  47. template <class T> class CFindReplaceDialogImpl;
  48. class CFindReplaceDialog;
  49. #endif //!UNDER_CE
  50. class CPropertySheetWindow;
  51. template <class T, class TBase = CPropertySheetWindow> class CPropertySheetImpl;
  52. class CPropertySheet;
  53. class CPropertyPageWindow;
  54. template <class T, class TBase = CPropertyPageWindow> class CPropertyPageImpl;
  55. template <WORD t_wDlgTemplateID> class CPropertyPage;
  56. /////////////////////////////////////////////////////////////////////////////
  57. // CFileDialogImpl - used for File Open or File Save As
  58. template <class T>
  59. class ATL_NO_VTABLE CFileDialogImpl : public CDialogImplBase
  60. {
  61. public:
  62. OPENFILENAME m_ofn;
  63. BOOL m_bOpenFileDialog; // TRUE for file open, FALSE for file save
  64. TCHAR m_szFileTitle[_MAX_FNAME]; // contains file title after return
  65. TCHAR m_szFileName[_MAX_PATH]; // contains full path name after return
  66. CFileDialogImpl(BOOL bOpenFileDialog, // TRUE for FileOpen, FALSE for FileSaveAs
  67. LPCTSTR lpszDefExt = NULL,
  68. LPCTSTR lpszFileName = NULL,
  69. DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
  70. LPCTSTR lpszFilter = NULL,
  71. HWND hWndParent = NULL)
  72. {
  73. memset(&m_ofn, 0, sizeof(m_ofn)); // initialize structure to 0/NULL
  74. m_szFileName[0] = '\0';
  75. m_szFileTitle[0] = '\0';
  76. m_bOpenFileDialog = bOpenFileDialog;
  77. m_ofn.lStructSize = sizeof(m_ofn);
  78. m_ofn.lpstrFile = m_szFileName;
  79. m_ofn.nMaxFile = sizeof(TCHAR) * _MAX_PATH;
  80. m_ofn.lpstrDefExt = lpszDefExt;
  81. m_ofn.lpstrFileTitle = (LPTSTR)m_szFileTitle;
  82. m_ofn.nMaxFileTitle = sizeof(TCHAR) * _MAX_FNAME;
  83. m_ofn.Flags |= dwFlags | OFN_ENABLEHOOK | OFN_EXPLORER;
  84. m_ofn.lpstrFilter = lpszFilter;
  85. m_ofn.hInstance = _Module.GetResourceInstance();
  86. m_ofn.lpfnHook = (LPOFNHOOKPROC)T::StartDialogProc;
  87. m_ofn.hwndOwner = hWndParent;
  88. // setup initial file name
  89. if(lpszFileName != NULL)
  90. lstrcpyn(m_szFileName, lpszFileName, sizeof(TCHAR) * _MAX_PATH);
  91. }
  92. int DoModal(HWND hWndParent = ::GetActiveWindow())
  93. {
  94. ATLASSERT(m_ofn.Flags & OFN_ENABLEHOOK);
  95. ATLASSERT(m_ofn.lpfnHook != NULL); // can still be a user hook
  96. ATLASSERT(m_ofn.Flags & OFN_EXPLORER);
  97. if(m_ofn.hwndOwner == NULL) // set only if not specified before
  98. m_ofn.hwndOwner = hWndParent;
  99. ATLASSERT(m_hWnd == NULL);
  100. _Module.AddCreateWndData(&m_thunk.cd, (CDialogImplBase*)this);
  101. int nResult;
  102. if(m_bOpenFileDialog)
  103. nResult = ::GetOpenFileName(&m_ofn);
  104. else
  105. nResult = ::GetSaveFileName(&m_ofn);
  106. m_hWnd = NULL;
  107. return nResult ? nResult : IDCANCEL;
  108. }
  109. // Operations
  110. int GetFilePath(LPTSTR lpstrFilePath, int nLength)
  111. {
  112. ATLASSERT(::IsWindow(m_hWnd));
  113. ATLASSERT(m_ofn.Flags & OFN_EXPLORER);
  114. return (int)SendMessage(CDM_GETFILEPATH, nLength, (LPARAM)lpstrFilePath);
  115. }
  116. int GetFolderIDList(LPVOID lpBuff, int nLength)
  117. {
  118. ATLASSERT(::IsWindow(m_hWnd));
  119. ATLASSERT(m_ofn.Flags & OFN_EXPLORER);
  120. return (int)SendMessage(CDM_GETFOLDERIDLIST, nLength, (LPARAM)lpBuff);
  121. }
  122. int GetFolderPath(LPTSTR lpstrFolderPath, int nLength)
  123. {
  124. ATLASSERT(::IsWindow(m_hWnd));
  125. ATLASSERT(m_ofn.Flags & OFN_EXPLORER);
  126. return (int)SendMessage(CDM_GETFOLDERPATH, nLength, (LPARAM)lpstrFolderPath);
  127. }
  128. int GetSpec(LPTSTR lpstrSpec, int nLength)
  129. {
  130. ATLASSERT(::IsWindow(m_hWnd));
  131. ATLASSERT(m_ofn.Flags & OFN_EXPLORER);
  132. return (int)SendMessage(CDM_GETSPEC, nLength, (LPARAM)lpstrSpec);
  133. }
  134. void HideControl(int nCtrlID)
  135. {
  136. ATLASSERT(::IsWindow(m_hWnd));
  137. ATLASSERT(m_ofn.Flags & OFN_EXPLORER);
  138. SendMessage(CDM_HIDECONTROL, nCtrlID);
  139. }
  140. void SetControlText(int nCtrlID, LPCTSTR lpstrText)
  141. {
  142. ATLASSERT(::IsWindow(m_hWnd));
  143. ATLASSERT(m_ofn.Flags & OFN_EXPLORER);
  144. SendMessage(CDM_SETCONTROLTEXT, nCtrlID, (LPARAM)lpstrText);
  145. }
  146. void SetDefExt(LPCTSTR lpstrExt)
  147. {
  148. ATLASSERT(::IsWindow(m_hWnd));
  149. ATLASSERT(m_ofn.Flags & OFN_EXPLORER);
  150. SendMessage(CDM_SETDEFEXT, 0, (LPARAM)lpstrExt);
  151. }
  152. BOOL GetReadOnlyPref() const // return TRUE if readonly checked
  153. {
  154. return m_ofn.Flags & OFN_READONLY ? TRUE : FALSE;
  155. }
  156. BEGIN_MSG_MAP(CFileDialogImpl< T >)
  157. NOTIFY_CODE_HANDLER(CDN_FILEOK, _OnFileOK)
  158. NOTIFY_CODE_HANDLER(CDN_FOLDERCHANGE, _OnFolderChange)
  159. NOTIFY_CODE_HANDLER(CDN_HELP, _OnHelp)
  160. NOTIFY_CODE_HANDLER(CDN_INITDONE, _OnInitDone)
  161. NOTIFY_CODE_HANDLER(CDN_SELCHANGE, _OnSelChange)
  162. NOTIFY_CODE_HANDLER(CDN_SHAREVIOLATION, _OnShareViolation)
  163. NOTIFY_CODE_HANDLER(CDN_TYPECHANGE, _OnTypeChange)
  164. END_MSG_MAP()
  165. LRESULT _OnFileOK(int /*idCtrl*/, LPNMHDR pnmh, BOOL& /*bHandled*/)
  166. {
  167. ATLASSERT(::IsWindow(m_hWnd));
  168. T* pT = static_cast<T*>(this);
  169. return !pT->OnFileOK((LPOFNOTIFY)pnmh);
  170. }
  171. LRESULT _OnFolderChange(int /*idCtrl*/, LPNMHDR pnmh, BOOL& /*bHandled*/)
  172. {
  173. ATLASSERT(::IsWindow(m_hWnd));
  174. T* pT = static_cast<T*>(this);
  175. pT->OnFolderChange((LPOFNOTIFY)pnmh);
  176. return 0;
  177. }
  178. LRESULT _OnHelp(int /*idCtrl*/, LPNMHDR pnmh, BOOL& /*bHandled*/)
  179. {
  180. ATLASSERT(::IsWindow(m_hWnd));
  181. T* pT = static_cast<T*>(this);
  182. pT->OnHelp((LPOFNOTIFY)pnmh);
  183. return 0;
  184. }
  185. LRESULT _OnInitDone(int /*idCtrl*/, LPNMHDR pnmh, BOOL& /*bHandled*/)
  186. {
  187. ATLASSERT(::IsWindow(m_hWnd));
  188. T* pT = static_cast<T*>(this);
  189. pT->OnInitDone((LPOFNOTIFY)pnmh);
  190. return 0;
  191. }
  192. LRESULT _OnSelChange(int /*idCtrl*/, LPNMHDR pnmh, BOOL& /*bHandled*/)
  193. {
  194. ATLASSERT(::IsWindow(m_hWnd));
  195. T* pT = static_cast<T*>(this);
  196. pT->OnSelChange((LPOFNOTIFY)pnmh);
  197. return 0;
  198. }
  199. LRESULT _OnShareViolation(int /*idCtrl*/, LPNMHDR pnmh, BOOL& /*bHandled*/)
  200. {
  201. ATLASSERT(::IsWindow(m_hWnd));
  202. T* pT = static_cast<T*>(this);
  203. return pT->OnShareViolation((LPOFNOTIFY)pnmh);
  204. }
  205. LRESULT _OnTypeChange(int /*idCtrl*/, LPNMHDR pnmh, BOOL& /*bHandled*/)
  206. {
  207. ATLASSERT(::IsWindow(m_hWnd));
  208. T* pT = static_cast<T*>(this);
  209. pT->OnSelChange((LPOFNOTIFY)pnmh);
  210. return 0;
  211. }
  212. BOOL OnFileOK(LPOFNOTIFY /*lpon*/)
  213. {
  214. return TRUE;
  215. }
  216. void OnFolderChange(LPOFNOTIFY /*lpon*/)
  217. {
  218. }
  219. void OnHelp(LPOFNOTIFY /*lpon*/)
  220. {
  221. }
  222. void OnInitDone(LPOFNOTIFY /*lpon*/)
  223. {
  224. }
  225. void OnSelChange(LPOFNOTIFY /*lpon*/)
  226. {
  227. }
  228. int OnShareViolation(LPOFNOTIFY /*lpon*/)
  229. {
  230. return 0;
  231. }
  232. void OnTypeChange(LPOFNOTIFY /*lpon*/)
  233. {
  234. }
  235. };
  236. class CFileDialog : public CFileDialogImpl<CFileDialog>
  237. {
  238. public:
  239. CFileDialog(BOOL bOpenFileDialog, // TRUE for FileOpen, FALSE for FileSaveAs
  240. LPCTSTR lpszDefExt = NULL,
  241. LPCTSTR lpszFileName = NULL,
  242. DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
  243. LPCTSTR lpszFilter = NULL,
  244. HWND hWndParent = NULL)
  245. : CFileDialogImpl<CFileDialog>(bOpenFileDialog, lpszDefExt, lpszFileName, dwFlags, lpszFilter, hWndParent)
  246. { }
  247. // override base class map and references to handlers
  248. DECLARE_EMPTY_MSG_MAP()
  249. };
  250. /////////////////////////////////////////////////////////////////////////////
  251. // CFolderDialogImpl - used for browsing for a folder
  252. #ifndef UNDER_CE
  253. #ifndef BFFM_VALIDATEFAILED
  254. #define BFFM_VALIDATEFAILEDA 3
  255. #define BFFM_VALIDATEFAILEDW 4
  256. #ifdef UNICODE
  257. #define BFFM_VALIDATEFAILED BFFM_VALIDATEFAILEDW
  258. #else
  259. #define BFFM_VALIDATEFAILED BFFM_VALIDATEFAILEDA
  260. #endif
  261. #endif //!BFFM_VALIDATEFAILED
  262. template <class T>
  263. class CFolderDialogImpl
  264. {
  265. public:
  266. BROWSEINFO m_bi;
  267. TCHAR m_szFolderDisplayName[MAX_PATH];
  268. TCHAR m_szFolderPath[MAX_PATH];
  269. HWND m_hWnd; // used only in the callback function
  270. // Constructor
  271. CFolderDialogImpl(HWND hWndParent = NULL, LPCTSTR lpstrTitle = NULL, UINT uFlags = BIF_RETURNONLYFSDIRS)
  272. {
  273. memset(&m_bi, 0, sizeof(m_bi)); // initialize structure to 0/NULL
  274. m_bi.hwndOwner = hWndParent;
  275. m_bi.pidlRoot = NULL;
  276. m_bi.pszDisplayName = m_szFolderDisplayName;
  277. m_bi.lpszTitle = lpstrTitle;
  278. m_bi.ulFlags = uFlags;
  279. m_bi.lpfn = BrowseCallbackProc;
  280. m_bi.lParam = (LPARAM)this;
  281. m_szFolderPath[0] = 0;
  282. m_szFolderDisplayName[0] = 0;
  283. m_hWnd = NULL;
  284. }
  285. // Operations
  286. int DoModal(HWND hWndParent = ::GetActiveWindow())
  287. {
  288. if(m_bi.hwndOwner == NULL) // set only if not specified before
  289. m_bi.hwndOwner = hWndParent;
  290. int nRet = -1;
  291. LPITEMIDLIST pItemIDList = ::SHBrowseForFolder(&m_bi);
  292. if(pItemIDList != NULL)
  293. {
  294. if(::SHGetPathFromIDList(pItemIDList, m_szFolderPath))
  295. {
  296. IMalloc* pMalloc = NULL;
  297. if(SUCCEEDED(::SHGetMalloc(&pMalloc)))
  298. {
  299. pMalloc->Free(pItemIDList);
  300. pMalloc->Release();
  301. }
  302. nRet = IDOK;
  303. }
  304. else
  305. nRet = IDCANCEL;
  306. }
  307. return nRet;
  308. }
  309. // filled after a call to DoModal
  310. LPTSTR GetFolderPath()
  311. {
  312. return m_szFolderPath;
  313. }
  314. LPTSTR GetFolderDisplayName()
  315. {
  316. return m_szFolderDisplayName;
  317. }
  318. int GetFolderImageIndex()
  319. {
  320. return m_bi.iImage;
  321. }
  322. // Callback function and overrideables
  323. static int CALLBACK BrowseCallbackProc(HWND hWnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
  324. {
  325. int nRet = 0;
  326. T* pT = (T*)lpData;
  327. pT->m_hWnd = hWnd;
  328. switch(uMsg)
  329. {
  330. case BFFM_INITIALIZED:
  331. pT->OnInitialized();
  332. break;
  333. case BFFM_SELCHANGED:
  334. pT->OnSelChanged((LPITEMIDLIST)lParam);
  335. break;
  336. case BFFM_VALIDATEFAILED:
  337. nRet = pT->OnValidateFailed((LPCTSTR)lParam);
  338. break;
  339. default:
  340. ATLTRACE2(atlTraceWindowing, 0, _T("Unknown message received in CFolderDialogImpl::BrowseCallbackProc\n"));
  341. break;
  342. }
  343. pT->m_hWnd = NULL;
  344. return nRet;
  345. }
  346. void OnInitialized()
  347. {
  348. }
  349. void OnSelChanged(LPITEMIDLIST /*pItemIDList*/)
  350. {
  351. }
  352. int OnValidateFailed(LPCTSTR /*lpstrFolderPath*/)
  353. {
  354. return 1; // 1=continue, 0=EndDialog
  355. }
  356. // Commands - valid to call only from handlers
  357. void EnableOK(BOOL bEnable)
  358. {
  359. ATLASSERT(m_hWnd != NULL);
  360. ::SendMessage(m_hWnd, BFFM_ENABLEOK, bEnable, 0L);
  361. }
  362. void SetSelection(LPITEMIDLIST pItemIDList)
  363. {
  364. ATLASSERT(m_hWnd != NULL);
  365. ::SendMessage(m_hWnd, BFFM_SETSELECTION, FALSE, (LPARAM)pItemIDList);
  366. }
  367. void SetSelection(LPCTSTR lpstrFolderPath)
  368. {
  369. ATLASSERT(m_hWnd != NULL);
  370. ::SendMessage(m_hWnd, BFFM_SETSELECTION, TRUE, (LPARAM)lpstrFolderPath);
  371. }
  372. void SetStatusText(LPCTSTR lpstrText)
  373. {
  374. ATLASSERT(m_hWnd != NULL);
  375. ::SendMessage(m_hWnd, BFFM_SETSTATUSTEXT, 0, (LPARAM)lpstrText);
  376. }
  377. };
  378. class CFolderDialog : public CFolderDialogImpl<CFolderDialog>
  379. {
  380. public:
  381. CFolderDialog(HWND hWndParent = NULL, LPCTSTR lpstrTitle = NULL, UINT uFlags = BIF_RETURNONLYFSDIRS)
  382. : CFolderDialogImpl<CFolderDialog>(hWndParent, lpstrTitle, uFlags)
  383. {
  384. m_bi.lpfn = NULL;
  385. }
  386. };
  387. #endif //!UNDER_CE
  388. /////////////////////////////////////////////////////////////////////////////
  389. // CCommonDialogBaseImpl - base class for common dialog classes
  390. class CCommonDialogImplBase : public CWindowImplBase
  391. {
  392. public:
  393. static UINT_PTR APIENTRY HookProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  394. {
  395. if(uMsg != WM_INITDIALOG)
  396. return 0;
  397. CCommonDialogImplBase* pT = (CCommonDialogImplBase*)_Module.ExtractCreateWndData();
  398. ATLASSERT(pT != NULL);
  399. ATLASSERT(pT->m_hWnd == NULL);
  400. ATLASSERT(::IsWindow(hWnd));
  401. // subclass dialog's window
  402. if(!pT->SubclassWindow(hWnd))
  403. {
  404. ATLTRACE2(atlTraceWindowing, 0, _T("Subclassing a common dialog failed\n"));
  405. return 0;
  406. }
  407. // check message map for WM_INITDIALOG handler
  408. LRESULT lRes;
  409. if(pT->ProcessWindowMessage(pT->m_hWnd, uMsg, wParam, lParam, lRes, 0) == FALSE)
  410. return 0;
  411. return lRes;
  412. }
  413. BOOL EndDialog(int nRetCode)
  414. {
  415. ATLASSERT(::IsWindow(m_hWnd));
  416. return ::EndDialog(m_hWnd, nRetCode);
  417. }
  418. // Attempt to override these, to prevent errors
  419. HWND Create(HWND /*hWndParent*/, RECT& /*rcPos*/, LPCTSTR /*szWindowName*/,
  420. DWORD /*dwStyle*/, DWORD /*dwExStyle*/, UINT /*nID*/, ATOM /*atom*/, LPVOID /*lpCreateParam = NULL*/)
  421. {
  422. ATLASSERT(FALSE); // should not be called
  423. return NULL;
  424. }
  425. static LRESULT CALLBACK StartWindowProc(HWND /*hWnd*/, UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/)
  426. {
  427. ATLASSERT(FALSE); // should not be called
  428. return 0;
  429. }
  430. };
  431. /////////////////////////////////////////////////////////////////////////////
  432. // CFontDialogImpl - font selection dialog
  433. #ifndef UNDER_CE
  434. template <class T>
  435. class ATL_NO_VTABLE CFontDialogImpl : public CCommonDialogImplBase
  436. {
  437. public:
  438. CHOOSEFONT m_cf;
  439. TCHAR m_szStyleName[64]; // contains style name after return
  440. LOGFONT m_lf; // default LOGFONT to store the info
  441. // Constructors
  442. CFontDialogImpl(LPLOGFONT lplfInitial = NULL,
  443. DWORD dwFlags = CF_EFFECTS | CF_SCREENFONTS,
  444. HDC hDCPrinter = NULL,
  445. HWND hWndParent = NULL)
  446. {
  447. memset(&m_cf, 0, sizeof(m_cf));
  448. memset(&m_lf, 0, sizeof(m_lf));
  449. memset(&m_szStyleName, 0, sizeof(m_szStyleName));
  450. m_cf.lStructSize = sizeof(m_cf);
  451. m_cf.hwndOwner = hWndParent;
  452. m_cf.rgbColors = RGB(0, 0, 0);
  453. m_cf.lpszStyle = (LPTSTR)&m_szStyleName;
  454. m_cf.Flags = dwFlags | CF_ENABLEHOOK;
  455. m_cf.lpfnHook = (LPCFHOOKPROC)T::HookProc;
  456. if(lplfInitial != NULL)
  457. {
  458. /*?*/ m_cf.lpLogFont = lplfInitial;
  459. m_cf.Flags |= CF_INITTOLOGFONTSTRUCT;
  460. memcpy(&m_lf, m_cf.lpLogFont, sizeof(m_lf));
  461. }
  462. else
  463. {
  464. m_cf.lpLogFont = &m_lf;
  465. }
  466. if(hDCPrinter != NULL)
  467. {
  468. m_cf.hDC = hDCPrinter;
  469. m_cf.Flags |= CF_PRINTERFONTS;
  470. }
  471. }
  472. // Operations
  473. int DoModal(HWND hWndParent = ::GetActiveWindow())
  474. {
  475. ATLASSERT(m_cf.Flags & CF_ENABLEHOOK);
  476. ATLASSERT(m_cf.lpfnHook != NULL); // can still be a user hook
  477. if(m_cf.hwndOwner == NULL) // set only if not specified before
  478. m_cf.hwndOwner = hWndParent;
  479. ATLASSERT(m_hWnd == NULL);
  480. _Module.AddCreateWndData(&m_thunk.cd, (CCommonDialogImplBase*)this);
  481. int nResult = ::ChooseFont(&m_cf);
  482. m_hWnd = NULL;
  483. if(nResult == IDOK)
  484. {
  485. // copy logical font from user's initialization buffer (if needed)
  486. memcpy(&m_lf, m_cf.lpLogFont, sizeof(m_lf));
  487. return IDOK;
  488. }
  489. return nResult ? nResult : IDCANCEL;
  490. }
  491. // Get the selected font (works during DoModal displayed or after)
  492. void GetCurrentFont(LPLOGFONT lplf)
  493. {
  494. ATLASSERT(lplf != NULL);
  495. if(m_hWnd != NULL)
  496. SendMessage(WM_CHOOSEFONT_GETLOGFONT, 0, (LPARAM)lplf);
  497. else
  498. *lplf = m_lf;
  499. }
  500. // Helpers for parsing information after successful return
  501. LPCTSTR GetFaceName() const // return the face name of the font
  502. {
  503. return (LPCTSTR)m_cf.lpLogFont->lfFaceName;
  504. }
  505. LPCTSTR GetStyleName() const // return the style name of the font
  506. {
  507. return m_cf.lpszStyle;
  508. }
  509. int GetSize() const // return the pt size of the font
  510. {
  511. return m_cf.iPointSize;
  512. }
  513. COLORREF GetColor() const // return the color of the font
  514. {
  515. return m_cf.rgbColors;
  516. }
  517. int GetWeight() const // return the chosen font weight
  518. {
  519. return (int)m_cf.lpLogFont->lfWeight;
  520. }
  521. BOOL IsStrikeOut() const // return TRUE if strikeout
  522. {
  523. return m_cf.lpLogFont->lfStrikeOut ? TRUE : FALSE;
  524. }
  525. BOOL IsUnderline() const // return TRUE if underline
  526. {
  527. return m_cf.lpLogFont->lfUnderline ? TRUE : FALSE;
  528. }
  529. BOOL IsBold() const // return TRUE if bold font
  530. {
  531. return m_cf.lpLogFont->lfWeight == FW_BOLD ? TRUE : FALSE;
  532. }
  533. BOOL IsItalic() const // return TRUE if italic font
  534. {
  535. return m_cf.lpLogFont->lfItalic ? TRUE : FALSE;
  536. }
  537. };
  538. class CFontDialog : public CFontDialogImpl<CFontDialog>
  539. {
  540. public:
  541. CFontDialog(LPLOGFONT lplfInitial = NULL,
  542. DWORD dwFlags = CF_EFFECTS | CF_SCREENFONTS,
  543. HDC hDCPrinter = NULL,
  544. HWND hWndParent = NULL)
  545. : CFontDialogImpl<CFontDialog>(lplfInitial, dwFlags, hDCPrinter, hWndParent)
  546. { }
  547. DECLARE_EMPTY_MSG_MAP()
  548. };
  549. #endif //!UNDER_CE
  550. /////////////////////////////////////////////////////////////////////////////
  551. // CRichEditFontDialogImpl - font selection for the Rich Edit ctrl
  552. #ifndef UNDER_CE
  553. #ifdef _RICHEDIT_
  554. template <class T>
  555. class ATL_NO_VTABLE CRichEditFontDialogImpl : public CFontDialogImpl< T >
  556. {
  557. public:
  558. CRichEditFontDialogImpl(const CHARFORMAT& charformat,
  559. DWORD dwFlags = CF_SCREENFONTS,
  560. HDC hDCPrinter = NULL,
  561. HWND hWndParent = NULL)
  562. : CFontDialogImpl< T >(NULL, dwFlags, hDCPrinter, hWndParent)
  563. {
  564. m_cf.Flags |= CF_INITTOLOGFONTSTRUCT;
  565. m_cf.Flags |= FillInLogFont(charformat);
  566. m_cf.lpLogFont = &m_lf;
  567. if(charformat.dwMask & CFM_COLOR)
  568. m_cf.rgbColors = charformat.crTextColor;
  569. }
  570. void GetCharFormat(CHARFORMAT& cf) const
  571. {
  572. USES_CONVERSION;
  573. cf.dwEffects = 0;
  574. cf.dwMask = 0;
  575. if((m_cf.Flags & CF_NOSTYLESEL) == 0)
  576. {
  577. cf.dwMask |= CFM_BOLD | CFM_ITALIC;
  578. cf.dwEffects |= (IsBold()) ? CFE_BOLD : 0;
  579. cf.dwEffects |= (IsItalic()) ? CFE_ITALIC : 0;
  580. }
  581. if((m_cf.Flags & CF_NOSIZESEL) == 0)
  582. {
  583. cf.dwMask |= CFM_SIZE;
  584. //GetSize() returns in tenths of points so mulitply by 2 to get twips
  585. cf.yHeight = GetSize() * 2;
  586. }
  587. if((m_cf.Flags & CF_NOFACESEL) == 0)
  588. {
  589. cf.dwMask |= CFM_FACE;
  590. cf.bPitchAndFamily = m_cf.lpLogFont->lfPitchAndFamily;
  591. #if (_RICHEDIT_VER >= 0x0200)
  592. lstrcpy(cf.szFaceName, GetFaceName());
  593. #else
  594. lstrcpyA(cf.szFaceName, T2A((LPTSTR)(LPCTSTR)GetFaceName()));
  595. #endif //(_RICHEDIT_VER >= 0x0200)
  596. }
  597. if(m_cf.Flags & CF_EFFECTS)
  598. {
  599. cf.dwMask |= CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR;
  600. cf.dwEffects |= (IsUnderline()) ? CFE_UNDERLINE : 0;
  601. cf.dwEffects |= (IsStrikeOut()) ? CFE_STRIKEOUT : 0;
  602. cf.crTextColor = GetColor();
  603. }
  604. if((m_cf.Flags & CF_NOSCRIPTSEL) == 0)
  605. {
  606. cf.bCharSet = m_cf.lpLogFont->lfCharSet;
  607. cf.dwMask |= CFM_CHARSET;
  608. }
  609. cf.yOffset = 0;
  610. }
  611. DWORD FillInLogFont(const CHARFORMAT& cf)
  612. {
  613. USES_CONVERSION;
  614. DWORD dwFlags = 0;
  615. if(cf.dwMask & CFM_SIZE)
  616. {
  617. HDC hDC = ::CreateDC(_T("DISPLAY"), NULL, NULL, NULL);
  618. LONG yPerInch = ::GetDeviceCaps(hDC, LOGPIXELSY);
  619. m_lf.lfHeight = -(int)((cf.yHeight * yPerInch) / 1440);
  620. }
  621. else
  622. m_lf.lfHeight = 0;
  623. m_lf.lfWidth = 0;
  624. m_lf.lfEscapement = 0;
  625. m_lf.lfOrientation = 0;
  626. if((cf.dwMask & (CFM_ITALIC|CFM_BOLD)) == (CFM_ITALIC|CFM_BOLD))
  627. {
  628. m_lf.lfWeight = (cf.dwEffects & CFE_BOLD) ? FW_BOLD : FW_NORMAL;
  629. m_lf.lfItalic = (BYTE)((cf.dwEffects & CFE_ITALIC) ? TRUE : FALSE);
  630. }
  631. else
  632. {
  633. dwFlags |= CF_NOSTYLESEL;
  634. m_lf.lfWeight = FW_DONTCARE;
  635. m_lf.lfItalic = FALSE;
  636. }
  637. if((cf.dwMask & (CFM_UNDERLINE|CFM_STRIKEOUT|CFM_COLOR)) ==
  638. (CFM_UNDERLINE|CFM_STRIKEOUT|CFM_COLOR))
  639. {
  640. dwFlags |= CF_EFFECTS;
  641. m_lf.lfUnderline = (BYTE)((cf.dwEffects & CFE_UNDERLINE) ? TRUE : FALSE);
  642. m_lf.lfStrikeOut = (BYTE)((cf.dwEffects & CFE_STRIKEOUT) ? TRUE : FALSE);
  643. }
  644. else
  645. {
  646. m_lf.lfUnderline = (BYTE)FALSE;
  647. m_lf.lfStrikeOut = (BYTE)FALSE;
  648. }
  649. if(cf.dwMask & CFM_CHARSET)
  650. m_lf.lfCharSet = cf.bCharSet;
  651. else
  652. dwFlags |= CF_NOSCRIPTSEL;
  653. m_lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
  654. m_lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
  655. m_lf.lfQuality = DEFAULT_QUALITY;
  656. if(cf.dwMask & CFM_FACE)
  657. {
  658. m_lf.lfPitchAndFamily = cf.bPitchAndFamily;
  659. #if (_RICHEDIT_VER >= 0x0200)
  660. lstrcpy(m_lf.lfFaceName, cf.szFaceName);
  661. #else
  662. lstrcpy(m_lf.lfFaceName, A2T((LPSTR)cf.szFaceName));
  663. #endif //(_RICHEDIT_VER >= 0x0200)
  664. }
  665. else
  666. {
  667. m_lf.lfPitchAndFamily = DEFAULT_PITCH|FF_DONTCARE;
  668. m_lf.lfFaceName[0] = (TCHAR)0;
  669. }
  670. return dwFlags;
  671. }
  672. };
  673. class CRichEditFontDialog : public CRichEditFontDialogImpl<CRichEditFontDialog>
  674. {
  675. public:
  676. CRichEditFontDialog(const CHARFORMAT& charformat,
  677. DWORD dwFlags = CF_SCREENFONTS,
  678. HDC hDCPrinter = NULL,
  679. HWND hWndParent = NULL)
  680. : CRichEditFontDialogImpl<CRichEditFontDialog>(charformat, dwFlags, hDCPrinter, hWndParent)
  681. { }
  682. };
  683. #endif // _RICHEDIT_
  684. #endif //!UNDER_CE
  685. /////////////////////////////////////////////////////////////////////////////
  686. // CColorDialogImpl - color selection
  687. //REVIEW - temp
  688. #ifndef UNDER_CE
  689. static const UINT _nMsgCOLOROK = ::RegisterWindowMessage(COLOROKSTRING);
  690. const UINT _nMsgSETRGB = ::RegisterWindowMessage(SETRGBSTRING);
  691. template <class T>
  692. class ATL_NO_VTABLE CColorDialogImpl : public CCommonDialogImplBase
  693. {
  694. public:
  695. CHOOSECOLOR m_cc;
  696. // Constructors
  697. CColorDialogImpl(COLORREF clrInit = 0, DWORD dwFlags = 0, HWND hWndParent = NULL)
  698. {
  699. memset(&m_cc, 0, sizeof(m_cc));
  700. m_cc.lStructSize = sizeof(m_cc);
  701. m_cc.lpCustColors = GetCustomColors();
  702. m_cc.hwndOwner = hWndParent;
  703. m_cc.Flags = dwFlags | CC_ENABLEHOOK;
  704. m_cc.lpfnHook = (LPCCHOOKPROC)T::HookProc;
  705. if(clrInit != 0)
  706. {
  707. m_cc.rgbResult = clrInit;
  708. m_cc.Flags |= CC_RGBINIT;
  709. }
  710. }
  711. // Operations
  712. int DoModal(HWND hWndParent = ::GetActiveWindow())
  713. {
  714. ATLASSERT(m_cc.Flags & CC_ENABLEHOOK);
  715. ATLASSERT(m_cc.lpfnHook != NULL); // can still be a user hook
  716. if(m_cc.hwndOwner == NULL) // set only if not specified before
  717. m_cc.hwndOwner = hWndParent;
  718. ATLASSERT(m_hWnd == NULL);
  719. _Module.AddCreateWndData(&m_thunk.cd, (CCommonDialogImplBase*)this);
  720. int nResult = ::ChooseColor(&m_cc);
  721. m_hWnd = NULL;
  722. return nResult ? nResult : IDCANCEL;
  723. }
  724. // Set the current color while dialog is displayed
  725. void SetCurrentColor(COLORREF clr)
  726. {
  727. ATLASSERT(::IsWindow(m_hWnd));
  728. SendMessage(_nMsgSETRGB, 0, (DWORD)clr);
  729. }
  730. // Helpers for parsing information after successful return
  731. COLORREF GetColor() const
  732. {
  733. return m_cc.rgbResult;
  734. }
  735. static COLORREF* GetCustomColors()
  736. {
  737. static COLORREF rgbCustomColors[16] =
  738. {
  739. RGB(255, 255, 255), RGB(255, 255, 255),
  740. RGB(255, 255, 255), RGB(255, 255, 255),
  741. RGB(255, 255, 255), RGB(255, 255, 255),
  742. RGB(255, 255, 255), RGB(255, 255, 255),
  743. RGB(255, 255, 255), RGB(255, 255, 255),
  744. RGB(255, 255, 255), RGB(255, 255, 255),
  745. RGB(255, 255, 255), RGB(255, 255, 255),
  746. RGB(255, 255, 255), RGB(255, 255, 255),
  747. };
  748. return rgbCustomColors;
  749. }
  750. // Overridable callbacks
  751. BEGIN_MSG_MAP(CColorDialogImpl< T >)
  752. MESSAGE_HANDLER(_nMsgCOLOROK, _OnColorOK)
  753. END_MSG_MAP()
  754. LRESULT _OnColorOK(UINT, WPARAM, LPARAM, BOOL&)
  755. {
  756. T* pT = static_cast<T*>(this);
  757. return pT->OnColorOK();
  758. }
  759. BOOL OnColorOK() // validate color
  760. {
  761. return FALSE;
  762. }
  763. };
  764. class CColorDialog : public CColorDialogImpl<CColorDialog>
  765. {
  766. public:
  767. CColorDialog(COLORREF clrInit = 0, DWORD dwFlags = 0, HWND hWndParent = NULL)
  768. : CColorDialogImpl<CColorDialog>(clrInit, dwFlags, hWndParent)
  769. { }
  770. // override base class map and references to handlers
  771. DECLARE_EMPTY_MSG_MAP()
  772. };
  773. #endif //!UNDER_CE
  774. /////////////////////////////////////////////////////////////////////////////
  775. // CPrintDialogImpl - used for Print... and PrintSetup...
  776. //REVIEW - temp
  777. #ifndef UNDER_CE
  778. // global helper
  779. static HDC _AtlCreateDC(HGLOBAL hDevNames, HGLOBAL hDevMode)
  780. {
  781. if(hDevNames == NULL)
  782. return NULL;
  783. LPDEVNAMES lpDevNames = (LPDEVNAMES)::GlobalLock(hDevNames);
  784. LPDEVMODE lpDevMode = (hDevMode != NULL) ? (LPDEVMODE)::GlobalLock(hDevMode) : NULL;
  785. if(lpDevNames == NULL)
  786. return NULL;
  787. HDC hDC = ::CreateDC((LPCTSTR)lpDevNames + lpDevNames->wDriverOffset,
  788. (LPCTSTR)lpDevNames + lpDevNames->wDeviceOffset,
  789. (LPCTSTR)lpDevNames + lpDevNames->wOutputOffset,
  790. lpDevMode);
  791. ::GlobalUnlock(hDevNames);
  792. if(hDevMode != NULL)
  793. ::GlobalUnlock(hDevMode);
  794. return hDC;
  795. }
  796. template <class T>
  797. class ATL_NO_VTABLE CPrintDialogImpl : public CCommonDialogImplBase
  798. {
  799. public:
  800. // print dialog parameter block (note this is a reference)
  801. PRINTDLG& m_pd;
  802. // Constructors
  803. CPrintDialogImpl(BOOL bPrintSetupOnly, // TRUE for Print Setup, FALSE for Print Dialog
  804. DWORD dwFlags = PD_ALLPAGES | PD_USEDEVMODECOPIES | PD_NOPAGENUMS | PD_HIDEPRINTTOFILE | PD_NOSELECTION,
  805. HWND hWndParent = NULL)
  806. : m_pd(m_pdActual)
  807. {
  808. memset(&m_pdActual, 0, sizeof(m_pdActual));
  809. m_pd.lStructSize = sizeof(m_pdActual);
  810. m_pd.hwndOwner = hWndParent;
  811. m_pd.Flags = (dwFlags | PD_ENABLEPRINTHOOK | PD_ENABLESETUPHOOK);
  812. m_pd.lpfnPrintHook = (LPPRINTHOOKPROC)T::HookProc;
  813. m_pd.lpfnSetupHook = (LPSETUPHOOKPROC)T::HookProc;
  814. if(bPrintSetupOnly)
  815. m_pd.Flags |= PD_PRINTSETUP;
  816. else
  817. m_pd.Flags |= PD_RETURNDC;
  818. m_pd.Flags &= ~PD_RETURNIC; // do not support information context
  819. }
  820. // Operations
  821. int DoModal(HWND hWndParent = ::GetActiveWindow())
  822. {
  823. ATLASSERT(m_pd.Flags & PD_ENABLEPRINTHOOK);
  824. ATLASSERT(m_pd.Flags & PD_ENABLESETUPHOOK);
  825. ATLASSERT(m_pd.lpfnPrintHook != NULL); // can still be a user hook
  826. ATLASSERT(m_pd.lpfnSetupHook != NULL); // can still be a user hook
  827. if(m_pd.hwndOwner == NULL) // set only if not specified before
  828. m_pd.hwndOwner = hWndParent;
  829. ATLASSERT(m_hWnd == NULL);
  830. _Module.AddCreateWndData(&m_thunk.cd, (CCommonDialogImplBase*)this);
  831. int nResult = ::PrintDlg(&m_pd);
  832. m_hWnd = NULL;
  833. return nResult ? nResult : IDCANCEL;
  834. }
  835. // GetDefaults will not display a dialog but will get
  836. // device defaults
  837. BOOL GetDefaults()
  838. {
  839. m_pd.Flags |= PD_RETURNDEFAULT;
  840. return ::PrintDlg(&m_pd);
  841. }
  842. // Helpers for parsing information after successful return
  843. int GetCopies() const // num. copies requested
  844. {
  845. if(m_pd.Flags & PD_USEDEVMODECOPIES)
  846. return GetDevMode()->dmCopies;
  847. else
  848. return m_pd.nCopies;
  849. }
  850. BOOL PrintCollate() const // TRUE if collate checked
  851. {
  852. return m_pd.Flags & PD_COLLATE ? TRUE : FALSE;
  853. }
  854. BOOL PrintSelection() const // TRUE if printing selection
  855. {
  856. return m_pd.Flags & PD_SELECTION ? TRUE : FALSE;
  857. }
  858. BOOL PrintAll() const // TRUE if printing all pages
  859. {
  860. return !PrintRange() && !PrintSelection() ? TRUE : FALSE;
  861. }
  862. BOOL PrintRange() const // TRUE if printing page range
  863. {
  864. return m_pd.Flags & PD_PAGENUMS ? TRUE : FALSE;
  865. }
  866. int GetFromPage() const // starting page if valid
  867. {
  868. return (PrintRange() ? m_pd.nFromPage :-1);
  869. }
  870. int GetToPage() const // starting page if valid
  871. {
  872. return (PrintRange() ? m_pd.nToPage :-1);
  873. }
  874. LPDEVMODE GetDevMode() const // return DEVMODE
  875. {
  876. if(m_pd.hDevMode == NULL)
  877. return NULL;
  878. return (LPDEVMODE)::GlobalLock(m_pd.hDevMode);
  879. }
  880. LPCTSTR GetDriverName() const // return driver name
  881. {
  882. if(m_pd.hDevNames == NULL)
  883. return NULL;
  884. LPDEVNAMES lpDev = (LPDEVNAMES)GlobalLock(m_pd.hDevNames);
  885. return (LPCTSTR)lpDev + lpDev->wDriverOffset;
  886. }
  887. LPCTSTR GetDeviceName() const // return device name
  888. {
  889. if(m_pd.hDevNames == NULL)
  890. return NULL;
  891. LPDEVNAMES lpDev = (LPDEVNAMES)GlobalLock(m_pd.hDevNames);
  892. return (LPCTSTR)lpDev + lpDev->wDeviceOffset;
  893. }
  894. LPCTSTR GetPortName() const // return output port name
  895. {
  896. if(m_pd.hDevNames == NULL)
  897. return NULL;
  898. LPDEVNAMES lpDev = (LPDEVNAMES)GlobalLock(m_pd.hDevNames);
  899. return (LPCTSTR)lpDev + lpDev->wOutputOffset;
  900. }
  901. HDC GetPrinterDC() const // return HDC (caller must delete)
  902. {
  903. ATLASSERT(m_pd.Flags & PD_RETURNDC);
  904. return m_pd.hDC;
  905. }
  906. // This helper creates a DC based on the DEVNAMES and DEVMODE structures.
  907. // This DC is returned, but also stored in m_pd.hDC as though it had been
  908. // returned by CommDlg. It is assumed that any previously obtained DC
  909. // has been/will be deleted by the user. This may be
  910. // used without ever invoking the print/print setup dialogs.
  911. HDC CreatePrinterDC()
  912. {
  913. m_pd.hDC = _AtlCreateDC(m_pd.hDevNames, m_pd.hDevMode);
  914. return m_pd.hDC;
  915. }
  916. // Implementation
  917. PRINTDLG m_pdActual; // the Print/Print Setup need to share this
  918. // The following handle the case of print setup... from the print dialog
  919. CPrintDialogImpl(PRINTDLG& pdInit) : m_pd(pdInit)
  920. { }
  921. BEGIN_MSG_MAP(CPrintDialogImpl< T >)
  922. COMMAND_ID_HANDLER(psh1, OnPrintSetup) // print setup button when print is displayed
  923. END_MSG_MAP()
  924. LRESULT OnPrintSetup(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& /*bHandled*/)
  925. {
  926. CPrintDialogImpl< T >* pDlgSetup = NULL;
  927. ATLTRY(pDlgSetup = new CPrintDialogImpl< T >(m_pd));
  928. ATLASSERT(pDlgSetup != NULL);
  929. _Module.AddCreateWndData(&m_thunk.cd, (CCommonDialogImplBase*)pDlgSetup);
  930. LRESULT lRet = DefWindowProc(WM_COMMAND, MAKEWPARAM(wID, wNotifyCode), (LPARAM)hWndCtl);
  931. delete pDlgSetup;
  932. return lRet;
  933. }
  934. };
  935. class CPrintDialog : public CPrintDialogImpl<CPrintDialog>
  936. {
  937. public:
  938. CPrintDialog(BOOL bPrintSetupOnly,
  939. DWORD dwFlags = PD_ALLPAGES | PD_USEDEVMODECOPIES | PD_NOPAGENUMS | PD_HIDEPRINTTOFILE | PD_NOSELECTION,
  940. HWND hWndParent = NULL)
  941. : CPrintDialogImpl<CPrintDialog>(bPrintSetupOnly, dwFlags, hWndParent)
  942. { }
  943. CPrintDialog(PRINTDLG& pdInit) : CPrintDialogImpl<CPrintDialog>(pdInit)
  944. { }
  945. };
  946. #endif //!UNDER_CE
  947. /////////////////////////////////////////////////////////////////////////////
  948. // CPageSetupDialogImpl - Page Setup dialog
  949. #ifndef UNDER_CE
  950. template <class T>
  951. class ATL_NO_VTABLE CPageSetupDialogImpl : public CCommonDialogImplBase
  952. {
  953. public:
  954. PAGESETUPDLG m_psd;
  955. CWndProcThunk m_thunkPaint;
  956. // Constructors
  957. CPageSetupDialogImpl(DWORD dwFlags = PSD_MARGINS | PSD_INWININIINTLMEASURE, HWND hWndParent = NULL)
  958. {
  959. memset(&m_psd, 0, sizeof(m_psd));
  960. m_psd.lStructSize = sizeof(m_psd);
  961. m_psd.hwndOwner = hWndParent;
  962. m_psd.Flags = (dwFlags | PSD_ENABLEPAGESETUPHOOK | PSD_ENABLEPAGEPAINTHOOK);
  963. m_psd.lpfnPageSetupHook = (LPPAGESETUPHOOK)T::HookProc;
  964. m_thunkPaint.Init((WNDPROC)T::PaintHookProc, this);
  965. m_psd.lpfnPagePaintHook = (LPPAGEPAINTHOOK)(m_thunkPaint.thunk.pThunk);
  966. }
  967. DECLARE_EMPTY_MSG_MAP()
  968. // Attributes
  969. LPDEVMODE GetDevMode() const // return DEVMODE
  970. {
  971. if(m_psd.hDevMode == NULL)
  972. return NULL;
  973. return (LPDEVMODE)::GlobalLock(m_psd.hDevMode);
  974. }
  975. LPCTSTR GetDriverName() const // return driver name
  976. {
  977. if(m_psd.hDevNames == NULL)
  978. return NULL;
  979. LPDEVNAMES lpDev = (LPDEVNAMES)GlobalLock(m_psd.hDevNames);
  980. return (LPCTSTR)lpDev + lpDev->wDriverOffset;
  981. }
  982. LPCTSTR GetDeviceName() const // return device name
  983. {
  984. if(m_psd.hDevNames == NULL)
  985. return NULL;
  986. LPDEVNAMES lpDev = (LPDEVNAMES)GlobalLock(m_psd.hDevNames);
  987. return (LPCTSTR)lpDev + lpDev->wDeviceOffset;
  988. }
  989. LPCTSTR GetPortName() const // return output port name
  990. {
  991. if(m_psd.hDevNames == NULL)
  992. return NULL;
  993. LPDEVNAMES lpDev = (LPDEVNAMES)GlobalLock(m_psd.hDevNames);
  994. return (LPCTSTR)lpDev + lpDev->wOutputOffset;
  995. }
  996. HDC CreatePrinterDC()
  997. {
  998. return _AtlCreateDC(m_psd.hDevNames, m_psd.hDevMode);
  999. }
  1000. SIZE GetPaperSize() const
  1001. {
  1002. SIZE size;
  1003. size.cx = m_psd.ptPaperSize.x;
  1004. size.cy = m_psd.ptPaperSize.y;
  1005. return size;
  1006. }
  1007. void GetMargins(LPRECT lpRectMargins, LPRECT lpRectMinMargins) const
  1008. {
  1009. if(lpRectMargins != NULL)
  1010. memcpy(lpRectMargins, &m_psd.rtMargin, sizeof(RECT));
  1011. if(lpRectMinMargins != NULL)
  1012. memcpy(lpRectMinMargins, &m_psd.rtMinMargin, sizeof(RECT));
  1013. }
  1014. // Operations
  1015. int DoModal(HWND hWndParent = ::GetActiveWindow())
  1016. {
  1017. ATLASSERT(m_psd.Flags & PSD_ENABLEPAGESETUPHOOK);
  1018. ATLASSERT(m_psd.Flags & PSD_ENABLEPAGEPAINTHOOK);
  1019. ATLASSERT(m_psd.lpfnPageSetupHook != NULL); // can still be a user hook
  1020. ATLASSERT(m_psd.lpfnPagePaintHook != NULL); // can still be a user hook
  1021. if(m_psd.hwndOwner == NULL) // set only if not specified before
  1022. m_psd.hwndOwner = hWndParent;
  1023. ATLASSERT(m_hWnd == NULL);
  1024. _Module.AddCreateWndData(&m_thunk.cd, (CCommonDialogImplBase*)this);
  1025. int nResult = ::PageSetupDlg(&m_psd);
  1026. m_hWnd = NULL;
  1027. return nResult ? nResult : IDCANCEL;
  1028. }
  1029. // Implementation
  1030. static UINT CALLBACK PaintHookProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  1031. {
  1032. CPageSetupDialogImpl< T >* pDlg = (CPageSetupDialogImpl< T >*)hWnd;
  1033. ATLASSERT(pDlg->m_hWnd == ::GetParent(hWnd));
  1034. UINT uRet = 0;
  1035. switch(uMsg)
  1036. {
  1037. case WM_PSD_PAGESETUPDLG:
  1038. uRet = pDlg->PreDrawPage(LOWORD(wParam), HIWORD(wParam), (LPPAGESETUPDLG)lParam);
  1039. break;
  1040. case WM_PSD_FULLPAGERECT:
  1041. case WM_PSD_MINMARGINRECT:
  1042. case WM_PSD_MARGINRECT:
  1043. case WM_PSD_GREEKTEXTRECT:
  1044. case WM_PSD_ENVSTAMPRECT:
  1045. case WM_PSD_YAFULLPAGERECT:
  1046. uRet = pDlg->OnDrawPage(uMsg, (HDC)wParam, (LPRECT)lParam);
  1047. break;
  1048. default:
  1049. ATLTRACE2(atlTraceWindowing, 0, _T("CPageSetupDialogImpl::PaintHookProc - unknown message received\n"));
  1050. break;
  1051. }
  1052. return uRet;
  1053. }
  1054. // Overridables
  1055. UINT PreDrawPage(WORD /*wPaper*/, WORD /*wFlags*/, LPPAGESETUPDLG /*pPSD*/)
  1056. {
  1057. // return 1 to prevent any more drawing
  1058. return 0;
  1059. }
  1060. UINT OnDrawPage(UINT /*uMsg*/, HDC /*hDC*/, LPRECT /*lpRect*/)
  1061. {
  1062. return 0; // do the default
  1063. }
  1064. };
  1065. class CPageSetupDialog : public CPageSetupDialogImpl<CPageSetupDialog>
  1066. {
  1067. public:
  1068. CPageSetupDialog(DWORD dwFlags = PSD_MARGINS | PSD_INWININIINTLMEASURE, HWND hWndParent = NULL)
  1069. : CPageSetupDialogImpl<CPageSetupDialog>(dwFlags, hWndParent)
  1070. { }
  1071. // override PaintHookProc and references to handlers
  1072. static UINT CALLBACK PaintHookProc(HWND, UINT, WPARAM, LPARAM)
  1073. {
  1074. return 0;
  1075. }
  1076. };
  1077. #endif //!UNDER_CE
  1078. /////////////////////////////////////////////////////////////////////////////
  1079. // CFindReplaceDialogImpl - Find/FindReplace modeless dialogs
  1080. #ifndef UNDER_CE
  1081. template <class T>
  1082. class ATL_NO_VTABLE CFindReplaceDialogImpl : public CCommonDialogImplBase
  1083. {
  1084. public:
  1085. FINDREPLACE m_fr;
  1086. TCHAR m_szFindWhat[128];
  1087. TCHAR m_szReplaceWith[128];
  1088. // Constructors
  1089. CFindReplaceDialogImpl()
  1090. {
  1091. memset(&m_fr, 0, sizeof(m_fr));
  1092. m_szFindWhat[0] = '\0';
  1093. m_szReplaceWith[0] = '\0';
  1094. m_fr.lStructSize = sizeof(m_fr);
  1095. m_fr.Flags = FR_ENABLEHOOK;
  1096. m_fr.lpfnHook = (LPFRHOOKPROC)T::HookProc;
  1097. m_fr.lpstrFindWhat = (LPTSTR)m_szFindWhat;
  1098. }
  1099. // Note: You must allocate the object on the heap.
  1100. // If you do not, you must override OnFinalMessage()
  1101. virtual void OnFinalMessage(HWND /*hWnd*/)
  1102. {
  1103. delete this;
  1104. }
  1105. HWND Create(BOOL bFindDialogOnly, // TRUE for Find, FALSE for FindReplace
  1106. LPCTSTR lpszFindWhat,
  1107. LPCTSTR lpszReplaceWith = NULL,
  1108. DWORD dwFlags = FR_DOWN,
  1109. HWND hWndParent = NULL)
  1110. {
  1111. ATLASSERT(m_fr.Flags & FR_ENABLEHOOK);
  1112. ATLASSERT(m_fr.lpfnHook != NULL);
  1113. m_fr.wFindWhatLen = sizeof(m_szFindWhat)/sizeof(TCHAR);
  1114. m_fr.lpstrReplaceWith = (LPTSTR)m_szReplaceWith;
  1115. m_fr.wReplaceWithLen = sizeof(m_szReplaceWith)/sizeof(TCHAR);
  1116. m_fr.Flags |= dwFlags;
  1117. if(hWndParent == NULL)
  1118. m_fr.hwndOwner = ::GetActiveWindow();
  1119. else
  1120. m_fr.hwndOwner = hWndParent;
  1121. ATLASSERT(m_fr.hwndOwner != NULL); // must have an owner for modeless dialog
  1122. if(lpszFindWhat != NULL)
  1123. lstrcpyn(m_szFindWhat, lpszFindWhat, sizeof(m_szFindWhat)/sizeof(TCHAR));
  1124. if(lpszReplaceWith != NULL)
  1125. lstrcpyn(m_szReplaceWith, lpszReplaceWith, sizeof(m_szReplaceWith)/sizeof(TCHAR));
  1126. ATLASSERT(m_hWnd == NULL);
  1127. _Module.AddCreateWndData(&m_thunk.cd, (CCommonDialogImplBase*)this);
  1128. HWND hWnd;
  1129. if(bFindDialogOnly)
  1130. hWnd = ::FindText(&m_fr);
  1131. else
  1132. hWnd = ::ReplaceText(&m_fr);
  1133. ATLASSERT(m_hWnd == hWnd);
  1134. return hWnd;
  1135. }
  1136. BOOL EndDialog(int /*nRetCode*/)
  1137. {
  1138. ATLASSERT(FALSE);
  1139. // should not be called
  1140. return FALSE;
  1141. }
  1142. static const UINT GetFindReplaceMsg()
  1143. {
  1144. static const UINT nMsgFindReplace = ::RegisterWindowMessage(FINDMSGSTRING);
  1145. return nMsgFindReplace;
  1146. }
  1147. // call while handling FINDMSGSTRING registered message
  1148. // to retreive the object
  1149. static T* PASCAL GetNotifier(LPARAM lParam)
  1150. {
  1151. ATLASSERT(lParam != NULL);
  1152. T* pDlg = (T*)(lParam - offsetof(T, m_fr));
  1153. return pDlg;
  1154. }
  1155. // Operations
  1156. // Helpers for parsing information after successful return
  1157. LPCTSTR GetFindString() const // get find string
  1158. {
  1159. return (LPCTSTR)m_fr.lpstrFindWhat;
  1160. }
  1161. LPCTSTR GetReplaceString() const // get replacement string
  1162. {
  1163. return (LPCTSTR)m_fr.lpstrReplaceWith;
  1164. }
  1165. BOOL SearchDown() const // TRUE if search down, FALSE is up
  1166. {
  1167. return m_fr.Flags & FR_DOWN ? TRUE : FALSE;
  1168. }
  1169. BOOL FindNext() const // TRUE if command is find next
  1170. {
  1171. return m_fr.Flags & FR_FINDNEXT ? TRUE : FALSE;
  1172. }
  1173. BOOL MatchCase() const // TRUE if matching case
  1174. {
  1175. return m_fr.Flags & FR_MATCHCASE ? TRUE : FALSE;
  1176. }
  1177. BOOL MatchWholeWord() const // TRUE if matching whole words only
  1178. {
  1179. return m_fr.Flags & FR_WHOLEWORD ? TRUE : FALSE;
  1180. }
  1181. BOOL ReplaceCurrent() const // TRUE if replacing current string
  1182. {
  1183. return m_fr. Flags & FR_REPLACE ? TRUE : FALSE;
  1184. }
  1185. BOOL ReplaceAll() const // TRUE if replacing all occurrences
  1186. {
  1187. return m_fr.Flags & FR_REPLACEALL ? TRUE : FALSE;
  1188. }
  1189. BOOL IsTerminating() const // TRUE if terminating dialog
  1190. {
  1191. return m_fr.Flags & FR_DIALOGTERM ? TRUE : FALSE ;
  1192. }
  1193. };
  1194. class CFindReplaceDialog : public CFindReplaceDialogImpl<CFindReplaceDialog>
  1195. {
  1196. public:
  1197. DECLARE_EMPTY_MSG_MAP()
  1198. };
  1199. #endif //!UNDER_CE
  1200. /////////////////////////////////////////////////////////////////////////////
  1201. // CPropertySheetWindow - client side for a property sheet
  1202. class CPropertySheetWindow : public CWindow
  1203. {
  1204. public:
  1205. // Constructors
  1206. CPropertySheetWindow(HWND hWnd = NULL) : CWindow(hWnd) { }
  1207. CPropertySheetWindow& operator=(HWND hWnd)
  1208. {
  1209. m_hWnd = hWnd;
  1210. return *this;
  1211. }
  1212. // Attributes
  1213. UINT GetPageCount() const
  1214. {
  1215. ATLASSERT(::IsWindow(m_hWnd));
  1216. HWND hWndTabCtrl = GetTabControl();
  1217. ATLASSERT(hWndTabCtrl != NULL);
  1218. return (UINT)::SendMessage(hWndTabCtrl, TCM_GETITEMCOUNT, 0, 0L);
  1219. }
  1220. HWND GetActivePage() const
  1221. {
  1222. ATLASSERT(::IsWindow(m_hWnd));
  1223. return (HWND)::SendMessage(m_hWnd, PSM_GETCURRENTPAGEHWND, 0, 0L);
  1224. }
  1225. UINT GetActiveIndex() const
  1226. {
  1227. ATLASSERT(::IsWindow(m_hWnd));
  1228. HWND hWndTabCtrl = GetTabControl();
  1229. ATLASSERT(hWndTabCtrl != NULL);
  1230. return (UINT)::SendMessage(hWndTabCtrl, TCM_GETCURSEL, 0, 0L);
  1231. }
  1232. BOOL SetActivePage(UINT uPageIndex)
  1233. {
  1234. ATLASSERT(::IsWindow(m_hWnd));
  1235. return (BOOL)::SendMessage(m_hWnd, PSM_SETCURSEL, uPageIndex, 0L);
  1236. }
  1237. BOOL SetActivePage(HPROPSHEETPAGE hPage)
  1238. {
  1239. ATLASSERT(::IsWindow(m_hWnd));
  1240. ATLASSERT(hPage != NULL);
  1241. return (BOOL)::SendMessage(m_hWnd, PSM_SETCURSEL, 0, (LPARAM)hPage);
  1242. }
  1243. BOOL SetActivePageByID(int nPageID)
  1244. {
  1245. ATLASSERT(::IsWindow(m_hWnd));
  1246. return (BOOL)::SendMessage(m_hWnd, PSM_SETCURSELID, 0, nPageID);
  1247. }
  1248. void SetTitle(LPCTSTR lpszText, UINT nStyle = 0)
  1249. {
  1250. ATLASSERT(::IsWindow(m_hWnd));
  1251. ATLASSERT((nStyle & ~PSH_PROPTITLE) == 0); // only PSH_PROPTITLE is valid
  1252. ATLASSERT(lpszText == NULL);
  1253. ::SendMessage(m_hWnd, PSM_SETTITLE, nStyle, (LPARAM)lpszText);
  1254. }
  1255. HWND GetTabControl() const
  1256. {
  1257. ATLASSERT(::IsWindow(m_hWnd));
  1258. return (HWND)::SendMessage(m_hWnd, PSM_GETTABCONTROL, 0, 0L);
  1259. }
  1260. void SetFinishText(LPCTSTR lpszText)
  1261. {
  1262. ATLASSERT(::IsWindow(m_hWnd));
  1263. ::SendMessage(m_hWnd, PSM_SETFINISHTEXT, 0, (LPARAM)lpszText);
  1264. }
  1265. void SetWizardButtons(DWORD dwFlags)
  1266. {
  1267. ATLASSERT(::IsWindow(m_hWnd));
  1268. ::SendMessage(m_hWnd, PSM_SETWIZBUTTONS, 0, dwFlags);
  1269. }
  1270. // Operations
  1271. void AddPage(HPROPSHEETPAGE hPage)
  1272. {
  1273. ATLASSERT(::IsWindow(m_hWnd));
  1274. ATLASSERT(hPage != NULL);
  1275. ::SendMessage(m_hWnd, PSM_ADDPAGE, 0, (LPARAM)hPage);
  1276. }
  1277. BOOL AddPage(LPCPROPSHEETPAGE pPage)
  1278. {
  1279. ATLASSERT(::IsWindow(m_hWnd));
  1280. ATLASSERT(pPage != NULL);
  1281. HPROPSHEETPAGE hPage = ::CreatePropertySheetPage(pPage);
  1282. if(hPage == NULL)
  1283. return FALSE;
  1284. ::SendMessage(m_hWnd, PSM_ADDPAGE, 0, (LPARAM)hPage);
  1285. return TRUE;
  1286. }
  1287. void RemovePage(UINT uPageIndex)
  1288. {
  1289. ATLASSERT(::IsWindow(m_hWnd));
  1290. ::SendMessage(m_hWnd, PSM_REMOVEPAGE, uPageIndex, 0L);
  1291. }
  1292. void RemovePage(HPROPSHEETPAGE hPage)
  1293. {
  1294. ATLASSERT(::IsWindow(m_hWnd));
  1295. ATLASSERT(hPage != NULL);
  1296. ::SendMessage(m_hWnd, PSM_REMOVEPAGE, 0, (LPARAM)hPage);
  1297. }
  1298. BOOL PressButton(int nButton)
  1299. {
  1300. ATLASSERT(::IsWindow(m_hWnd));
  1301. return (BOOL)::SendMessage(m_hWnd, PSM_PRESSBUTTON, nButton, 0L);
  1302. }
  1303. BOOL Apply()
  1304. {
  1305. ATLASSERT(::IsWindow(m_hWnd));
  1306. return (BOOL)::SendMessage(m_hWnd, PSM_APPLY, 0, 0L);
  1307. }
  1308. void CancelToClose()
  1309. {
  1310. ATLASSERT(::IsWindow(m_hWnd));
  1311. ::SendMessage(m_hWnd, PSM_CANCELTOCLOSE, 0, 0L);
  1312. }
  1313. void SetModified(HWND hWndPage, BOOL bChanged = TRUE)
  1314. {
  1315. ATLASSERT(::IsWindow(m_hWnd));
  1316. ATLASSERT(::IsWindow(hWndPage));
  1317. UINT uMsg = bChanged ? PSM_CHANGED : PSM_UNCHANGED;
  1318. ::SendMessage(m_hWnd, uMsg, (WPARAM)hWndPage, 0L);
  1319. }
  1320. LRESULT QuerySiblings(WPARAM wParam, LPARAM lParam)
  1321. {
  1322. ATLASSERT(::IsWindow(m_hWnd));
  1323. return ::SendMessage(m_hWnd, PSM_QUERYSIBLINGS, wParam, lParam);
  1324. }
  1325. void RebootSystem()
  1326. {
  1327. ATLASSERT(::IsWindow(m_hWnd));
  1328. ::SendMessage(m_hWnd, PSM_REBOOTSYSTEM, 0, 0L);
  1329. }
  1330. void RestartWindow()
  1331. {
  1332. ATLASSERT(::IsWindow(m_hWnd));
  1333. ::SendMessage(m_hWnd, PSM_RESTARTWINDOWS, 0, 0L);
  1334. }
  1335. BOOL IsDialogMessage(LPMSG lpMsg)
  1336. {
  1337. ATLASSERT(::IsWindow(m_hWnd));
  1338. return (BOOL)::SendMessage(m_hWnd, PSM_ISDIALOGMESSAGE, 0, (LPARAM)lpMsg);
  1339. }
  1340. };
  1341. /////////////////////////////////////////////////////////////////////////////
  1342. // CPropertySheetImpl - implements a property sheet
  1343. template <class T, class TBase /*= CPropertySheetWindow */>
  1344. class ATL_NO_VTABLE CPropertySheetImpl : public CWindowImplBaseT< TBase >
  1345. {
  1346. public:
  1347. PROPSHEETHEADER m_psh;
  1348. // Construction/Destruction
  1349. CPropertySheetImpl(LPCTSTR lpszTitle = NULL, UINT uStartPage = 0, HWND hWndParent = NULL)
  1350. {
  1351. memset(&m_psh, 0, sizeof(PROPSHEETHEADER));
  1352. m_psh.dwSize = sizeof(PROPSHEETHEADER);
  1353. m_psh.dwFlags = PSH_USECALLBACK;
  1354. m_psh.hInstance = _Module.GetResourceInstance();
  1355. m_psh.phpage = NULL;
  1356. m_psh.nPages = 0;
  1357. m_psh.pszCaption = lpszTitle;
  1358. m_psh.nStartPage = uStartPage;
  1359. m_psh.hwndParent = hWndParent; // if NULL, will be set in DoModal/Create
  1360. m_psh.pfnCallback = T::PropSheetCallback;
  1361. }
  1362. ~CPropertySheetImpl()
  1363. {
  1364. if(m_psh.phpage != NULL)
  1365. delete[] m_psh.phpage;
  1366. }
  1367. static int CALLBACK PropSheetCallback(HWND hWnd, UINT uMsg, LPARAM)
  1368. {
  1369. if(uMsg == PSCB_INITIALIZED)
  1370. {
  1371. ATLASSERT(hWnd != NULL);
  1372. CWindowImplBaseT< TBase >* pT = (CWindowImplBaseT< TBase >*)_Module.ExtractCreateWndData();
  1373. pT->SubclassWindow(hWnd);
  1374. }
  1375. return 0;
  1376. }
  1377. HWND Create(HWND hWndParent = NULL)
  1378. {
  1379. ATLASSERT(m_hWnd == NULL);
  1380. m_psh.dwFlags |= PSH_MODELESS;
  1381. if(m_psh.hwndParent == NULL)
  1382. m_psh.hwndParent = hWndParent;
  1383. _Module.AddCreateWndData(&m_thunk.cd, (CWindowImplBaseT< TBase >*)this);
  1384. HWND hWnd = (HWND)::PropertySheet(&m_psh);
  1385. ATLASSERT(m_hWnd == hWnd);
  1386. return hWnd;
  1387. }
  1388. INT_PTR DoModal(HWND hWndParent = ::GetActiveWindow())
  1389. {
  1390. ATLASSERT(m_hWnd == NULL);
  1391. m_psh.dwFlags &= ~PSH_MODELESS;
  1392. if(m_psh.hwndParent == NULL)
  1393. m_psh.hwndParent = hWndParent;
  1394. _Module.AddCreateWndData(&m_thunk.cd, (CWindowImplBaseT< TBase >*)this);
  1395. return ::PropertySheet(&m_psh);
  1396. }
  1397. // Attributes (extended overrides of client class methods)
  1398. UINT GetPageCount() const
  1399. {
  1400. if(m_hWnd == NULL)
  1401. return m_psh.nPages;
  1402. return TBase::GetPageCount();
  1403. }
  1404. UINT GetActiveIndex() const
  1405. {
  1406. if(m_hWnd == NULL)
  1407. return m_psh.nStartPage;
  1408. return TBase::GetActiveIndex();
  1409. }
  1410. HPROPSHEETPAGE GetPage(UINT uPageIndex)
  1411. {
  1412. ATLASSERT(uPageIndex < m_psh.nPages);
  1413. return m_psh.phpage[uPageIndex];
  1414. }
  1415. UINT GetPageIndex(HPROPSHEETPAGE hPage)
  1416. {
  1417. for(UINT i = 0; i < m_psh.nPages; i++)
  1418. {
  1419. if(m_psh.phpage[i] == hPage)
  1420. return i;
  1421. }
  1422. return (UINT)-1; // hPage not found
  1423. }
  1424. BOOL SetActivePage(UINT uPageIndex)
  1425. {
  1426. if(m_hWnd == NULL)
  1427. {
  1428. m_psh.nStartPage = uPageIndex;
  1429. return TRUE;
  1430. }
  1431. return TBase::SetActivePage(uPageIndex);
  1432. }
  1433. BOOL SetActivePage(HPROPSHEETPAGE hPage)
  1434. {
  1435. ATLASSERT(hPage != NULL);
  1436. UINT uPageIndex = GetPageIndex(hPage);
  1437. if(uPageIndex == (UINT)-1)
  1438. return FALSE;
  1439. return SetActivePage(uPageIndex);
  1440. }
  1441. void SetTitle(LPCTSTR lpszText, UINT nStyle = 0)
  1442. {
  1443. ATLASSERT((nStyle & ~PSH_PROPTITLE) == 0); // only PSH_PROPTITLE is valid
  1444. ATLASSERT(lpszText == NULL);
  1445. if(m_hWnd == NULL)
  1446. {
  1447. // set internal state
  1448. m_psh.pszCaption = lpszText;
  1449. m_psh.dwFlags &= ~PSH_PROPTITLE;
  1450. m_psh.dwFlags |= nStyle;
  1451. }
  1452. else
  1453. {
  1454. // set external state
  1455. TBase::SetTitle(lpszText, nStyle);
  1456. }
  1457. }
  1458. #ifndef UNDER_CE
  1459. void SetWizardMode()
  1460. {
  1461. m_psh.dwFlags |= PSH_WIZARD;
  1462. }
  1463. #endif //!UNDER_CE
  1464. // Operations
  1465. BOOL AddPage(HPROPSHEETPAGE hPage)
  1466. {
  1467. ATLASSERT(hPage != NULL);
  1468. // add page to internal list
  1469. HPROPSHEETPAGE* php = (HPROPSHEETPAGE*)realloc(m_psh.phpage, (m_psh.nPages + 1) * sizeof(HPROPSHEETPAGE));
  1470. if(php == NULL)
  1471. return FALSE;
  1472. m_psh.phpage = php;
  1473. m_psh.phpage[m_psh.nPages] = hPage;
  1474. m_psh.nPages++;
  1475. if(m_hWnd != NULL)
  1476. TBase::AddPage(hPage);
  1477. return TRUE;
  1478. }
  1479. BOOL AddPage(LPCPROPSHEETPAGE pPage)
  1480. {
  1481. ATLASSERT(pPage != NULL);
  1482. HPROPSHEETPAGE hPage = ::CreatePropertySheetPage(pPage);
  1483. if(hPage == NULL)
  1484. return FALSE;
  1485. AddPage(hPage);
  1486. return TRUE;
  1487. }
  1488. BOOL RemovePage(HPROPSHEETPAGE hPage, BOOL bDestroyPage = TRUE)
  1489. {
  1490. ATLASSERT(hPage != NULL);
  1491. int nPage = GetPageIndex(hPage);
  1492. if(nPage == -1)
  1493. return FALSE;
  1494. return RemovePage(nPage, bDestroyPage);
  1495. }
  1496. BOOL RemovePage(UINT uPageIndex, BOOL bDestroyPage = TRUE)
  1497. {
  1498. // remove the page externally
  1499. if(m_hWnd != NULL)
  1500. TBase::RemovePage(uPageIndex);
  1501. // remove the page from internal list
  1502. if(uPageIndex >= m_psh.nPages)
  1503. return FALSE;
  1504. if(bDestroyPage && !DestroyPropertySheetPage(m_psh.phpage[uPageIndex]))
  1505. return FALSE;
  1506. for(UINT i = uPageIndex; i < m_psh.nPages - 1; i++)
  1507. m_psh.phpage[i] = m_psh.phpage[i+1];
  1508. if (m_psh.nPages == 1) {
  1509. free(m_psh.phpage);
  1510. m_psh.phpage = NULL;
  1511. m_psh.nPages = 0;
  1512. } else {
  1513. HPROPSHEETPAGE* php = (HPROPSHEETPAGE*)realloc(m_psh.phpage, (m_psh.nPages - 1) * sizeof(HPROPSHEETPAGE));
  1514. if (php == NULL)
  1515. return FALSE;
  1516. m_psh.phpage = php;
  1517. m_psh.nPages--;
  1518. }
  1519. return TRUE;
  1520. }
  1521. };
  1522. // for non-customized sheets
  1523. class CPropertySheet : public CPropertySheetImpl<CPropertySheet>
  1524. {
  1525. public:
  1526. CPropertySheet(LPCTSTR lpszTitle = NULL, UINT uStartPage = 0, HWND hWndParent = NULL)
  1527. : CPropertySheetImpl<CPropertySheet>(lpszTitle, uStartPage, hWndParent)
  1528. { }
  1529. DECLARE_EMPTY_MSG_MAP()
  1530. };
  1531. /////////////////////////////////////////////////////////////////////////////
  1532. // CPropertyPageWindow - client side for a property page
  1533. class CPropertyPageWindow : public CWindow
  1534. {
  1535. public:
  1536. // Constructors
  1537. CPropertyPageWindow(HWND hWnd = NULL) : CWindow(hWnd) { }
  1538. CPropertyPageWindow& operator=(HWND hWnd)
  1539. {
  1540. m_hWnd = hWnd;
  1541. return *this;
  1542. }
  1543. // Operations
  1544. BOOL Apply()
  1545. {
  1546. ATLASSERT(::IsWindow(m_hWnd));
  1547. ATLASSERT(GetParent() != NULL);
  1548. return (BOOL)::SendMessage(GetParent(), PSM_APPLY, 0, 0L);
  1549. }
  1550. void CancelToClose()
  1551. {
  1552. ATLASSERT(::IsWindow(m_hWnd));
  1553. ATLASSERT(GetParent() != NULL);
  1554. ::SendMessage(GetParent(), PSM_CANCELTOCLOSE, 0, 0L);
  1555. }
  1556. void SetModified(BOOL bChanged = TRUE)
  1557. {
  1558. ATLASSERT(::IsWindow(m_hWnd));
  1559. ATLASSERT(GetParent() != NULL);
  1560. UINT uMsg = bChanged ? PSM_CHANGED : PSM_UNCHANGED;
  1561. ::SendMessage(GetParent(), uMsg, (WPARAM)m_hWnd, 0L);
  1562. }
  1563. LRESULT QuerySiblings(WPARAM wParam, LPARAM lParam)
  1564. {
  1565. ATLASSERT(::IsWindow(m_hWnd));
  1566. ATLASSERT(GetParent() != NULL);
  1567. return ::SendMessage(GetParent(), PSM_QUERYSIBLINGS, wParam, lParam);
  1568. }
  1569. void RebootSystem()
  1570. {
  1571. ATLASSERT(::IsWindow(m_hWnd));
  1572. ATLASSERT(GetParent() != NULL);
  1573. ::SendMessage(GetParent(), PSM_REBOOTSYSTEM, 0, 0L);
  1574. }
  1575. void RestartWindow()
  1576. {
  1577. ATLASSERT(::IsWindow(m_hWnd));
  1578. ATLASSERT(GetParent() != NULL);
  1579. ::SendMessage(GetParent(), PSM_RESTARTWINDOWS, 0, 0L);
  1580. }
  1581. };
  1582. /////////////////////////////////////////////////////////////////////////////
  1583. // CPropertyPageImpl - implements a property page
  1584. template <class T, class TBase /*= CPropertyPageWindow */>
  1585. class ATL_NO_VTABLE CPropertyPageImpl : public CDialogImplBaseT< TBase >
  1586. {
  1587. public:
  1588. PROPSHEETPAGE m_psp;
  1589. operator PROPSHEETPAGE*() { return &m_psp; }
  1590. // Construction
  1591. CPropertyPageImpl(LPCTSTR lpszTitle = NULL)
  1592. {
  1593. // initialize PROPSHEETPAGE struct
  1594. memset(&m_psp, 0, sizeof(PROPSHEETPAGE));
  1595. m_psp.dwSize = sizeof(PROPSHEETPAGE);
  1596. m_psp.dwFlags = PSP_USECALLBACK;
  1597. m_psp.hInstance = _Module.GetResourceInstance();
  1598. m_psp.pszTemplate = MAKEINTRESOURCE(T::IDD);
  1599. m_psp.pfnDlgProc = T::StartDialogProc;
  1600. m_psp.pfnCallback = T::PropPageCallback;
  1601. m_psp.lParam = (LPARAM)this;
  1602. if(lpszTitle != NULL)
  1603. SetTitle(lpszTitle);
  1604. }
  1605. static UINT CALLBACK PropPageCallback(HWND hWnd, UINT uMsg, LPPROPSHEETPAGE ppsp)
  1606. {
  1607. if(uMsg == PSPCB_CREATE)
  1608. {
  1609. ATLASSERT(hWnd == NULL);
  1610. CDialogImplBaseT< TBase >* pPage = (CDialogImplBaseT< TBase >*)ppsp->lParam;
  1611. _Module.AddCreateWndData(&pPage->m_thunk.cd, pPage);
  1612. }
  1613. return 1;
  1614. }
  1615. HPROPSHEETPAGE Create()
  1616. {
  1617. return ::CreatePropertySheetPage(&m_psp);
  1618. }
  1619. // Attributes
  1620. void SetTitle(LPCTSTR lpszTitle)
  1621. {
  1622. m_psp.pszTitle = lpszTitle;
  1623. m_psp.dwFlags |= PSP_USETITLE;
  1624. }
  1625. void SetTitle(UINT nTitleID)
  1626. {
  1627. SetTitle(MAKEINTRESOURCE(nTitleID));
  1628. }
  1629. // Message map and handlers
  1630. typedef CPropertyPageImpl< T, TBase > thisClass;
  1631. BEGIN_MSG_MAP(thisClass)
  1632. MESSAGE_HANDLER(WM_NOTIFY, OnNotify)
  1633. END_MSG_MAP()
  1634. LRESULT OnNotify(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  1635. {
  1636. ATLASSERT(::IsWindow(m_hWnd));
  1637. NMHDR* pNMHDR = (NMHDR*)lParam;
  1638. // don't handle messages not from the page/sheet itself
  1639. if(pNMHDR->hwndFrom != m_hWnd && pNMHDR->hwndFrom != ::GetParent(m_hWnd))
  1640. {
  1641. bHandled = FALSE;
  1642. return 1;
  1643. }
  1644. T* pT = static_cast<T*>(this);
  1645. LRESULT lResult = 0;
  1646. switch(pNMHDR->code)
  1647. {
  1648. case PSN_SETACTIVE:
  1649. lResult = pT->OnSetActive() ? 0 : -1;
  1650. break;
  1651. case PSN_KILLACTIVE:
  1652. lResult = !pT->OnKillActive();
  1653. break;
  1654. case PSN_APPLY:
  1655. lResult = pT->OnApply() ? PSNRET_NOERROR : PSNRET_INVALID_NOCHANGEPAGE;
  1656. break;
  1657. case PSN_RESET:
  1658. pT->OnReset();
  1659. break;
  1660. case PSN_QUERYCANCEL:
  1661. lResult = !pT->OnQueryCancel();
  1662. break;
  1663. case PSN_WIZNEXT:
  1664. lResult = pT->OnWizardNext();
  1665. break;
  1666. case PSN_WIZBACK:
  1667. lResult = pT->OnWizardBack();
  1668. break;
  1669. case PSN_WIZFINISH:
  1670. lResult = !pT->OnWizardFinish();
  1671. break;
  1672. case PSN_HELP:
  1673. pT->OnHelp();
  1674. break;
  1675. #if (_WIN32_IE >= 0x0400)
  1676. case PSN_GETOBJECT:
  1677. if(!pT->OnGetObject((LPNMOBJECTNOTIFY)lParam))
  1678. bHandled = FALSE;
  1679. break;
  1680. #endif //(_WIN32_IE >= 0x0400)
  1681. default:
  1682. bHandled = FALSE; // not handled
  1683. }
  1684. return lResult;
  1685. }
  1686. // Overridables
  1687. BOOL OnSetActive()
  1688. {
  1689. return TRUE;
  1690. }
  1691. BOOL OnKillActive()
  1692. {
  1693. return TRUE;
  1694. }
  1695. BOOL OnApply()
  1696. {
  1697. return TRUE;
  1698. }
  1699. void OnReset()
  1700. {
  1701. }
  1702. BOOL OnQueryCancel()
  1703. {
  1704. return TRUE; // ok to cancel
  1705. }
  1706. int OnWizardBack()
  1707. {
  1708. // 0 = goto next page
  1709. // -1 = prevent page change
  1710. // >0 = jump to page by dlg ID
  1711. return 0;
  1712. }
  1713. int OnWizardNext()
  1714. {
  1715. // 0 = goto next page
  1716. // -1 = prevent page change
  1717. // >0 = jump to page by dlg ID
  1718. return 0;
  1719. }
  1720. BOOL OnWizardFinish()
  1721. {
  1722. return TRUE;
  1723. }
  1724. void OnHelp()
  1725. {
  1726. }
  1727. #if (_WIN32_IE >= 0x0400)
  1728. BOOL OnGetObject(LPNMOBJECTNOTIFY /*lpObjectNotify*/)
  1729. {
  1730. return FALSE; // not processed
  1731. }
  1732. #endif //(_WIN32_IE >= 0x0400)
  1733. };
  1734. // for non-customized pages
  1735. template <WORD t_wDlgTemplateID>
  1736. class CPropertyPage : public CPropertyPageImpl<CPropertyPage<t_wDlgTemplateID> >
  1737. {
  1738. public:
  1739. enum { IDD = t_wDlgTemplateID };
  1740. CPropertyPage(LPCTSTR lpszTitle = NULL) : CPropertyPageImpl<CPropertyPage>(lpszTitle)
  1741. { }
  1742. DECLARE_EMPTY_MSG_MAP()
  1743. };
  1744. }; //namespace ATL
  1745. #endif // __ATLDLGS_H__