atlmisc.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  1. // Windows Template Library - WTL version 10.0
  2. // Copyright (C) Microsoft Corporation, WTL Team. All rights reserved.
  3. //
  4. // This file is a part of the Windows Template Library.
  5. // The use and distribution terms for this software are covered by the
  6. // Microsoft Public License (http://opensource.org/licenses/MS-PL)
  7. // which can be found in the file MS-PL.txt at the root folder.
  8. #ifndef __ATLMISC_H__
  9. #define __ATLMISC_H__
  10. #pragma once
  11. #ifndef __ATLAPP_H__
  12. #error atlmisc.h requires atlapp.h to be included first
  13. #endif
  14. #ifndef _WTL_NO_COMPATIBILITY_INCLUDES
  15. #include <atlstr.h>
  16. #include <atltypes.h>
  17. #endif // _WTL_NO_COMPATIBILITY_INCLUDES
  18. ///////////////////////////////////////////////////////////////////////////////
  19. // Classes in this file:
  20. //
  21. // CRecentDocumentListBase<T, t_cchItemLen, t_nFirstID, t_nLastID>
  22. // CRecentDocumentList
  23. // CFindFile
  24. //
  25. // Global functions:
  26. // AtlGetStockPen()
  27. // AtlGetStockBrush()
  28. // AtlGetStockFont()
  29. // AtlGetStockPalette()
  30. //
  31. // AtlCompactPath()
  32. namespace WTL
  33. {
  34. ///////////////////////////////////////////////////////////////////////////////
  35. // CSize scalar operators
  36. #if !defined(_WTL_NO_SIZE_SCALAR) && defined(__ATLTYPES_H__)
  37. template <class Num>
  38. inline CSize operator *(SIZE s, Num n)
  39. {
  40. return CSize((int)(s.cx * n), (int)(s.cy * n));
  41. };
  42. template <class Num>
  43. inline void operator *=(SIZE & s, Num n)
  44. {
  45. s = s * n;
  46. };
  47. template <class Num>
  48. inline CSize operator /(SIZE s, Num n)
  49. {
  50. return CSize((int)(s.cx / n), (int)(s.cy / n));
  51. };
  52. template <class Num>
  53. inline void operator /=(SIZE & s, Num n)
  54. {
  55. s = s / n;
  56. };
  57. #endif // !defined(_WTL_NO_SIZE_SCALAR) && defined(__ATLTYPES_H__)
  58. ///////////////////////////////////////////////////////////////////////////////
  59. // CRecentDocumentList - MRU List Support
  60. #ifndef _WTL_MRUEMPTY_TEXT
  61. #define _WTL_MRUEMPTY_TEXT _T("(empty)")
  62. #endif
  63. // forward declaration
  64. inline bool AtlCompactPath(LPTSTR lpstrOut, LPCTSTR lpstrIn, int cchLen);
  65. template <class T, int t_cchItemLen = MAX_PATH, int t_nFirstID = ID_FILE_MRU_FIRST, int t_nLastID = ID_FILE_MRU_LAST>
  66. class CRecentDocumentListBase
  67. {
  68. public:
  69. // Declarations
  70. struct _DocEntry
  71. {
  72. TCHAR szDocName[t_cchItemLen];
  73. bool operator ==(const _DocEntry& de) const
  74. { return (lstrcmpi(szDocName, de.szDocName) == 0); }
  75. };
  76. enum
  77. {
  78. m_nMaxEntries_Min = 2,
  79. m_nMaxEntries_Max = t_nLastID - t_nFirstID + 1,
  80. m_cchMaxItemLen_Min = 6,
  81. m_cchMaxItemLen_Max = t_cchItemLen,
  82. m_cchItemNameLen = 11
  83. };
  84. // Data members
  85. ATL::CSimpleArray<_DocEntry> m_arrDocs;
  86. int m_nMaxEntries; // default is 4
  87. HMENU m_hMenu;
  88. TCHAR m_szNoEntries[t_cchItemLen];
  89. int m_cchMaxItemLen;
  90. // Constructor
  91. CRecentDocumentListBase() : m_nMaxEntries(4), m_hMenu(NULL), m_cchMaxItemLen(-1)
  92. {
  93. // These ASSERTs verify values of the template arguments
  94. ATLASSERT(t_cchItemLen > m_cchMaxItemLen_Min);
  95. ATLASSERT(m_nMaxEntries_Max > m_nMaxEntries_Min);
  96. }
  97. // Attributes
  98. HMENU GetMenuHandle() const
  99. {
  100. return m_hMenu;
  101. }
  102. void SetMenuHandle(HMENU hMenu)
  103. {
  104. ATLASSERT((hMenu == NULL) || ::IsMenu(hMenu));
  105. m_hMenu = hMenu;
  106. if((m_hMenu == NULL) || (::GetMenuString(m_hMenu, t_nFirstID, m_szNoEntries, t_cchItemLen, MF_BYCOMMAND) == 0))
  107. {
  108. T* pT = static_cast<T*>(this);
  109. (void)pT; // avoid level 4 warning
  110. ATL::Checked::tcsncpy_s(m_szNoEntries, _countof(m_szNoEntries), pT->GetMRUEmptyText(), _TRUNCATE);
  111. }
  112. }
  113. int GetMaxEntries() const
  114. {
  115. return m_nMaxEntries;
  116. }
  117. void SetMaxEntries(int nMaxEntries)
  118. {
  119. ATLASSERT((nMaxEntries >= m_nMaxEntries_Min) && (nMaxEntries <= m_nMaxEntries_Max));
  120. if(nMaxEntries < m_nMaxEntries_Min)
  121. nMaxEntries = m_nMaxEntries_Min;
  122. else if(nMaxEntries > m_nMaxEntries_Max)
  123. nMaxEntries = m_nMaxEntries_Max;
  124. m_nMaxEntries = nMaxEntries;
  125. }
  126. int GetMaxItemLength() const
  127. {
  128. return m_cchMaxItemLen;
  129. }
  130. void SetMaxItemLength(int cchMaxLen)
  131. {
  132. ATLASSERT(((cchMaxLen >= m_cchMaxItemLen_Min) && (cchMaxLen <= m_cchMaxItemLen_Max)) || (cchMaxLen == -1));
  133. if(cchMaxLen != -1)
  134. {
  135. if(cchMaxLen < m_cchMaxItemLen_Min)
  136. cchMaxLen = m_cchMaxItemLen_Min;
  137. else if(cchMaxLen > m_cchMaxItemLen_Max)
  138. cchMaxLen = m_cchMaxItemLen_Max;
  139. }
  140. m_cchMaxItemLen = cchMaxLen;
  141. T* pT = static_cast<T*>(this);
  142. pT->UpdateMenu();
  143. }
  144. // Operations
  145. BOOL AddToList(LPCTSTR lpstrDocName)
  146. {
  147. _DocEntry de;
  148. errno_t nRet = ATL::Checked::tcsncpy_s(de.szDocName, _countof(de.szDocName), lpstrDocName, _TRUNCATE);
  149. if((nRet != 0) && (nRet != STRUNCATE))
  150. return FALSE;
  151. for(int i = 0; i < m_arrDocs.GetSize(); i++)
  152. {
  153. if(lstrcmpi(m_arrDocs[i].szDocName, lpstrDocName) == 0)
  154. {
  155. m_arrDocs.RemoveAt(i);
  156. break;
  157. }
  158. }
  159. if(m_arrDocs.GetSize() == m_nMaxEntries)
  160. m_arrDocs.RemoveAt(0);
  161. BOOL bRet = m_arrDocs.Add(de);
  162. if(bRet)
  163. {
  164. T* pT = static_cast<T*>(this);
  165. bRet = pT->UpdateMenu();
  166. }
  167. return bRet;
  168. }
  169. // This function is deprecated because it is not safe.
  170. // Use the version below that accepts the buffer length.
  171. __declspec(deprecated)
  172. BOOL GetFromList(int /*nItemID*/, LPTSTR /*lpstrDocName*/)
  173. {
  174. ATLASSERT(FALSE);
  175. return FALSE;
  176. }
  177. BOOL GetFromList(int nItemID, LPTSTR lpstrDocName, int cchLength)
  178. {
  179. int nIndex = m_arrDocs.GetSize() - (nItemID - t_nFirstID) - 1;
  180. if((nIndex < 0) || (nIndex >= m_arrDocs.GetSize()))
  181. return FALSE;
  182. if(lstrlen(m_arrDocs[nIndex].szDocName) >= cchLength)
  183. return FALSE;
  184. ATL::Checked::tcscpy_s(lpstrDocName, cchLength, m_arrDocs[nIndex].szDocName);
  185. return TRUE;
  186. }
  187. #ifdef __ATLSTR_H__
  188. BOOL GetFromList(int nItemID, ATL::CString& strDocName)
  189. {
  190. int nIndex = m_arrDocs.GetSize() - (nItemID - t_nFirstID) - 1;
  191. if((nIndex < 0) || (nIndex >= m_arrDocs.GetSize()))
  192. return FALSE;
  193. strDocName = m_arrDocs[nIndex].szDocName;
  194. return TRUE;
  195. }
  196. #endif // __ATLSTR_H__
  197. BOOL RemoveFromList(int nItemID)
  198. {
  199. int nIndex = m_arrDocs.GetSize() - (nItemID - t_nFirstID) - 1;
  200. BOOL bRet = m_arrDocs.RemoveAt(nIndex);
  201. if(bRet)
  202. {
  203. T* pT = static_cast<T*>(this);
  204. bRet = pT->UpdateMenu();
  205. }
  206. return bRet;
  207. }
  208. BOOL MoveToTop(int nItemID)
  209. {
  210. int nIndex = m_arrDocs.GetSize() - (nItemID - t_nFirstID) - 1;
  211. if((nIndex < 0) || (nIndex >= m_arrDocs.GetSize()))
  212. return FALSE;
  213. _DocEntry de;
  214. de = m_arrDocs[nIndex];
  215. m_arrDocs.RemoveAt(nIndex);
  216. BOOL bRet = m_arrDocs.Add(de);
  217. if(bRet)
  218. {
  219. T* pT = static_cast<T*>(this);
  220. bRet = pT->UpdateMenu();
  221. }
  222. return bRet;
  223. }
  224. BOOL ReadFromRegistry(LPCTSTR lpstrRegKey)
  225. {
  226. T* pT = static_cast<T*>(this);
  227. ATL::CRegKey rkParent;
  228. ATL::CRegKey rk;
  229. LONG lRet = rkParent.Open(HKEY_CURRENT_USER, lpstrRegKey);
  230. if(lRet != ERROR_SUCCESS)
  231. return FALSE;
  232. lRet = rk.Open(rkParent, pT->GetRegKeyName());
  233. if(lRet != ERROR_SUCCESS)
  234. return FALSE;
  235. DWORD dwRet = 0;
  236. lRet = rk.QueryDWORDValue(pT->GetRegCountName(), dwRet);
  237. if(lRet != ERROR_SUCCESS)
  238. return FALSE;
  239. SetMaxEntries(dwRet);
  240. m_arrDocs.RemoveAll();
  241. TCHAR szRetString[t_cchItemLen] = {};
  242. _DocEntry de;
  243. for(int nItem = m_nMaxEntries; nItem > 0; nItem--)
  244. {
  245. TCHAR szBuff[m_cchItemNameLen] = {};
  246. _stprintf_s(szBuff, m_cchItemNameLen, pT->GetRegItemName(), nItem);
  247. ULONG ulCount = t_cchItemLen;
  248. lRet = rk.QueryStringValue(szBuff, szRetString, &ulCount);
  249. if(lRet == ERROR_SUCCESS)
  250. {
  251. ATL::Checked::tcscpy_s(de.szDocName, _countof(de.szDocName), szRetString);
  252. m_arrDocs.Add(de);
  253. }
  254. }
  255. rk.Close();
  256. rkParent.Close();
  257. return pT->UpdateMenu();
  258. }
  259. BOOL WriteToRegistry(LPCTSTR lpstrRegKey)
  260. {
  261. T* pT = static_cast<T*>(this);
  262. (void)pT; // avoid level 4 warning
  263. ATL::CRegKey rkParent;
  264. ATL::CRegKey rk;
  265. LONG lRet = rkParent.Create(HKEY_CURRENT_USER, lpstrRegKey);
  266. if(lRet != ERROR_SUCCESS)
  267. return FALSE;
  268. lRet = rk.Create(rkParent, pT->GetRegKeyName());
  269. if(lRet != ERROR_SUCCESS)
  270. return FALSE;
  271. lRet = rk.SetDWORDValue(pT->GetRegCountName(), m_nMaxEntries);
  272. ATLASSERT(lRet == ERROR_SUCCESS);
  273. // set new values
  274. int nItem;
  275. for(nItem = m_arrDocs.GetSize(); nItem > 0; nItem--)
  276. {
  277. TCHAR szBuff[m_cchItemNameLen] = {};
  278. _stprintf_s(szBuff, m_cchItemNameLen, pT->GetRegItemName(), nItem);
  279. TCHAR szDocName[t_cchItemLen] = {};
  280. GetFromList(t_nFirstID + nItem - 1, szDocName, t_cchItemLen);
  281. lRet = rk.SetStringValue(szBuff, szDocName);
  282. ATLASSERT(lRet == ERROR_SUCCESS);
  283. }
  284. // delete unused keys
  285. for(nItem = m_arrDocs.GetSize() + 1; nItem <= m_nMaxEntries_Max; nItem++)
  286. {
  287. TCHAR szBuff[m_cchItemNameLen] = {};
  288. _stprintf_s(szBuff, m_cchItemNameLen, pT->GetRegItemName(), nItem);
  289. rk.DeleteValue(szBuff);
  290. }
  291. rk.Close();
  292. rkParent.Close();
  293. return TRUE;
  294. }
  295. // Implementation
  296. BOOL UpdateMenu()
  297. {
  298. if(m_hMenu == NULL)
  299. return FALSE;
  300. ATLASSERT(::IsMenu(m_hMenu));
  301. int nItems = ::GetMenuItemCount(m_hMenu);
  302. int nInsertPoint = 0;
  303. for(int i = 0; i < nItems; i++)
  304. {
  305. CMenuItemInfo mi;
  306. mi.fMask = MIIM_ID;
  307. ::GetMenuItemInfo(m_hMenu, i, TRUE, &mi);
  308. if (mi.wID == t_nFirstID)
  309. {
  310. nInsertPoint = i;
  311. break;
  312. }
  313. }
  314. ATLASSERT((nInsertPoint < nItems) && "You need a menu item with an ID = t_nFirstID");
  315. for(int j = t_nFirstID; j < (t_nFirstID + m_nMaxEntries); j++)
  316. {
  317. // keep the first one as an insertion point
  318. if (j != t_nFirstID)
  319. ::DeleteMenu(m_hMenu, j, MF_BYCOMMAND);
  320. }
  321. TCHAR szItemText[t_cchItemLen + 6] = {}; // add space for &, 2 digits, and a space
  322. int nSize = m_arrDocs.GetSize();
  323. int nItem = 0;
  324. if(nSize > 0)
  325. {
  326. for(nItem = 0; nItem < nSize; nItem++)
  327. {
  328. if(m_cchMaxItemLen == -1)
  329. {
  330. _stprintf_s(szItemText, t_cchItemLen + 6, _T("&%i %s"), nItem + 1, m_arrDocs[nSize - 1 - nItem].szDocName);
  331. }
  332. else
  333. {
  334. TCHAR szBuff[t_cchItemLen] = {};
  335. T* pT = static_cast<T*>(this);
  336. (void)pT; // avoid level 4 warning
  337. bool bRet = pT->CompactDocumentName(szBuff, m_arrDocs[nSize - 1 - nItem].szDocName, m_cchMaxItemLen);
  338. (void)bRet; // avoid level 4 warning
  339. ATLASSERT(bRet);
  340. _stprintf_s(szItemText, t_cchItemLen + 6, _T("&%i %s"), nItem + 1, szBuff);
  341. }
  342. ::InsertMenu(m_hMenu, nInsertPoint + nItem, MF_BYPOSITION | MF_STRING, t_nFirstID + nItem, szItemText);
  343. }
  344. }
  345. else // empty
  346. {
  347. ::InsertMenu(m_hMenu, nInsertPoint, MF_BYPOSITION | MF_STRING, t_nFirstID, m_szNoEntries);
  348. ::EnableMenuItem(m_hMenu, t_nFirstID, MF_GRAYED);
  349. nItem++;
  350. }
  351. ::DeleteMenu(m_hMenu, nInsertPoint + nItem, MF_BYPOSITION);
  352. return TRUE;
  353. }
  354. // Overrideables
  355. // override to provide a different method of compacting document names
  356. static bool CompactDocumentName(LPTSTR lpstrOut, LPCTSTR lpstrIn, int cchLen)
  357. {
  358. return AtlCompactPath(lpstrOut, lpstrIn, cchLen);
  359. }
  360. static LPCTSTR GetRegKeyName()
  361. {
  362. return _T("Recent Document List");
  363. }
  364. static LPCTSTR GetRegCountName()
  365. {
  366. return _T("DocumentCount");
  367. }
  368. static LPCTSTR GetRegItemName()
  369. {
  370. // Note: This string is a format string used with wsprintf().
  371. // Resulting formatted string must be m_cchItemNameLen or less
  372. // characters long, including the terminating null character.
  373. return _T("Document%i");
  374. }
  375. static LPCTSTR GetMRUEmptyText()
  376. {
  377. return _WTL_MRUEMPTY_TEXT;
  378. }
  379. };
  380. class CRecentDocumentList : public CRecentDocumentListBase<CRecentDocumentList>
  381. {
  382. public:
  383. // nothing here
  384. };
  385. ///////////////////////////////////////////////////////////////////////////////
  386. // CFindFile - file search helper class
  387. class CFindFile
  388. {
  389. public:
  390. // Data members
  391. HANDLE m_hFind;
  392. WIN32_FIND_DATA m_fd;
  393. LPTSTR m_lpszRoot;
  394. const TCHAR m_chDirSeparator;
  395. BOOL m_bFound;
  396. // Constructor/destructor
  397. CFindFile() : m_hFind(NULL), m_lpszRoot(NULL), m_chDirSeparator(_T('\\')), m_bFound(FALSE)
  398. { }
  399. ~CFindFile()
  400. {
  401. Close();
  402. }
  403. // Attributes
  404. ULONGLONG GetFileSize() const
  405. {
  406. ATLASSERT(m_hFind != NULL);
  407. ULARGE_INTEGER nFileSize = {};
  408. if(m_bFound)
  409. {
  410. nFileSize.LowPart = m_fd.nFileSizeLow;
  411. nFileSize.HighPart = m_fd.nFileSizeHigh;
  412. }
  413. else
  414. {
  415. nFileSize.QuadPart = 0;
  416. }
  417. return nFileSize.QuadPart;
  418. }
  419. BOOL GetFileName(LPTSTR lpstrFileName, int cchLength) const
  420. {
  421. ATLASSERT(m_hFind != NULL);
  422. if(lstrlen(m_fd.cFileName) >= cchLength)
  423. return FALSE;
  424. if(m_bFound)
  425. ATL::Checked::tcscpy_s(lpstrFileName, cchLength, m_fd.cFileName);
  426. return m_bFound;
  427. }
  428. BOOL GetFilePath(LPTSTR lpstrFilePath, int cchLength) const
  429. {
  430. ATLASSERT(m_hFind != NULL);
  431. int nLen = lstrlen(m_lpszRoot);
  432. ATLASSERT(nLen > 0);
  433. if(nLen == 0)
  434. return FALSE;
  435. bool bAddSep = (m_lpszRoot[nLen - 1] != m_chDirSeparator);
  436. if((lstrlen(m_lpszRoot) + (bAddSep ? 1 : 0)) >= cchLength)
  437. return FALSE;
  438. ATL::Checked::tcscpy_s(lpstrFilePath, cchLength, m_lpszRoot);
  439. if(bAddSep)
  440. {
  441. TCHAR szSeparator[2] = { m_chDirSeparator, 0 };
  442. ATL::Checked::tcscat_s(lpstrFilePath, cchLength, szSeparator);
  443. }
  444. ATL::Checked::tcscat_s(lpstrFilePath, cchLength, m_fd.cFileName);
  445. return TRUE;
  446. }
  447. BOOL GetFileTitle(LPTSTR lpstrFileTitle, int cchLength) const
  448. {
  449. ATLASSERT(m_hFind != NULL);
  450. TCHAR szBuff[MAX_PATH] = {};
  451. if(!GetFileName(szBuff, MAX_PATH))
  452. return FALSE;
  453. if((lstrlen(szBuff) >= cchLength) || (cchLength < 1))
  454. return FALSE;
  455. // find the last dot
  456. LPTSTR pstrDot = _tcsrchr(szBuff, _T('.'));
  457. if(pstrDot != NULL)
  458. *pstrDot = 0;
  459. ATL::Checked::tcscpy_s(lpstrFileTitle, cchLength, szBuff);
  460. return TRUE;
  461. }
  462. BOOL GetFileURL(LPTSTR lpstrFileURL, int cchLength) const
  463. {
  464. ATLASSERT(m_hFind != NULL);
  465. LPCTSTR lpstrFileURLPrefix = _T("file://");
  466. const int cchPrefix = lstrlen(lpstrFileURLPrefix);
  467. if(cchPrefix >= cchLength)
  468. return FALSE;
  469. ATL::Checked::tcscpy_s(lpstrFileURL, cchLength, lpstrFileURLPrefix);
  470. return GetFilePath(&lpstrFileURL[cchPrefix], cchLength - cchPrefix);
  471. }
  472. BOOL GetRoot(LPTSTR lpstrRoot, int cchLength) const
  473. {
  474. ATLASSERT(m_hFind != NULL);
  475. if(lstrlen(m_lpszRoot) >= cchLength)
  476. return FALSE;
  477. ATL::Checked::tcscpy_s(lpstrRoot, cchLength, m_lpszRoot);
  478. return TRUE;
  479. }
  480. #ifdef __ATLSTR_H__
  481. ATL::CString GetFileName() const
  482. {
  483. ATLASSERT(m_hFind != NULL);
  484. ATL::CString ret;
  485. if(m_bFound)
  486. ret = m_fd.cFileName;
  487. return ret;
  488. }
  489. ATL::CString GetFilePath() const
  490. {
  491. ATLASSERT(m_hFind != NULL);
  492. ATL::CString strResult = m_lpszRoot;
  493. int nLen = strResult.GetLength();
  494. ATLASSERT(nLen > 0);
  495. if(nLen == 0)
  496. return strResult;
  497. if(strResult[nLen - 1] != m_chDirSeparator)
  498. strResult += m_chDirSeparator;
  499. strResult += GetFileName();
  500. return strResult;
  501. }
  502. ATL::CString GetFileTitle() const
  503. {
  504. ATLASSERT(m_hFind != NULL);
  505. ATL::CString strResult;
  506. GetFileTitle(strResult.GetBuffer(MAX_PATH), MAX_PATH);
  507. strResult.ReleaseBuffer();
  508. return strResult;
  509. }
  510. ATL::CString GetFileURL() const
  511. {
  512. ATLASSERT(m_hFind != NULL);
  513. ATL::CString strResult("file://");
  514. strResult += GetFilePath();
  515. return strResult;
  516. }
  517. ATL::CString GetRoot() const
  518. {
  519. ATLASSERT(m_hFind != NULL);
  520. ATL::CString str = m_lpszRoot;
  521. return str;
  522. }
  523. #endif // __ATLSTR_H__
  524. BOOL GetLastWriteTime(FILETIME* pTimeStamp) const
  525. {
  526. ATLASSERT(m_hFind != NULL);
  527. ATLASSERT(pTimeStamp != NULL);
  528. if(m_bFound && (pTimeStamp != NULL))
  529. {
  530. *pTimeStamp = m_fd.ftLastWriteTime;
  531. return TRUE;
  532. }
  533. return FALSE;
  534. }
  535. BOOL GetLastAccessTime(FILETIME* pTimeStamp) const
  536. {
  537. ATLASSERT(m_hFind != NULL);
  538. ATLASSERT(pTimeStamp != NULL);
  539. if(m_bFound && (pTimeStamp != NULL))
  540. {
  541. *pTimeStamp = m_fd.ftLastAccessTime;
  542. return TRUE;
  543. }
  544. return FALSE;
  545. }
  546. BOOL GetCreationTime(FILETIME* pTimeStamp) const
  547. {
  548. ATLASSERT(m_hFind != NULL);
  549. if(m_bFound && (pTimeStamp != NULL))
  550. {
  551. *pTimeStamp = m_fd.ftCreationTime;
  552. return TRUE;
  553. }
  554. return FALSE;
  555. }
  556. BOOL MatchesMask(DWORD dwMask) const
  557. {
  558. ATLASSERT(m_hFind != NULL);
  559. if(m_bFound)
  560. return ((m_fd.dwFileAttributes & dwMask) != 0);
  561. return FALSE;
  562. }
  563. BOOL IsDots() const
  564. {
  565. ATLASSERT(m_hFind != NULL);
  566. // return TRUE if the file name is "." or ".." and
  567. // the file is a directory
  568. BOOL bResult = FALSE;
  569. if(m_bFound && IsDirectory())
  570. {
  571. if((m_fd.cFileName[0] == _T('.')) && ((m_fd.cFileName[1] == _T('\0')) || ((m_fd.cFileName[1] == _T('.')) && (m_fd.cFileName[2] == _T('\0')))))
  572. bResult = TRUE;
  573. }
  574. return bResult;
  575. }
  576. BOOL IsReadOnly() const
  577. {
  578. return MatchesMask(FILE_ATTRIBUTE_READONLY);
  579. }
  580. BOOL IsDirectory() const
  581. {
  582. return MatchesMask(FILE_ATTRIBUTE_DIRECTORY);
  583. }
  584. BOOL IsCompressed() const
  585. {
  586. return MatchesMask(FILE_ATTRIBUTE_COMPRESSED);
  587. }
  588. BOOL IsSystem() const
  589. {
  590. return MatchesMask(FILE_ATTRIBUTE_SYSTEM);
  591. }
  592. BOOL IsHidden() const
  593. {
  594. return MatchesMask(FILE_ATTRIBUTE_HIDDEN);
  595. }
  596. BOOL IsTemporary() const
  597. {
  598. return MatchesMask(FILE_ATTRIBUTE_TEMPORARY);
  599. }
  600. BOOL IsNormal() const
  601. {
  602. return MatchesMask(FILE_ATTRIBUTE_NORMAL);
  603. }
  604. BOOL IsArchived() const
  605. {
  606. return MatchesMask(FILE_ATTRIBUTE_ARCHIVE);
  607. }
  608. // Operations
  609. BOOL FindFile(LPCTSTR pstrName = NULL, bool bAutoLongPath = false)
  610. {
  611. Close();
  612. if(pstrName == NULL)
  613. pstrName = _T("*.*");
  614. if(bAutoLongPath && (lstrlen(pstrName) >= MAX_PATH))
  615. {
  616. LPCTSTR lpstrPrefix = _T("\\\\?\\");
  617. int cchLongPath = lstrlen(lpstrPrefix) + lstrlen(pstrName) + 1;
  618. ATL::CTempBuffer<TCHAR, _WTL_STACK_ALLOC_THRESHOLD> buff;
  619. LPTSTR lpstrLongPath = buff.Allocate(cchLongPath);
  620. if(lpstrLongPath != NULL)
  621. {
  622. ATL::Checked::tcscpy_s(lpstrLongPath, cchLongPath, lpstrPrefix);
  623. ATL::Checked::tcscat_s(lpstrLongPath, cchLongPath, pstrName);
  624. m_hFind = ::FindFirstFile(lpstrLongPath, &m_fd);
  625. }
  626. }
  627. else
  628. {
  629. m_hFind = ::FindFirstFile(pstrName, &m_fd);
  630. }
  631. if(m_hFind == INVALID_HANDLE_VALUE)
  632. return FALSE;
  633. int cchRoot = ::GetFullPathName(pstrName, 0, NULL, NULL);
  634. if(cchRoot > 0)
  635. ATLTRY(m_lpszRoot = new TCHAR[cchRoot]);
  636. if(m_lpszRoot == NULL)
  637. return FALSE;
  638. bool bFullPath = (::GetFullPathName(pstrName, cchRoot, m_lpszRoot, NULL) != 0);
  639. // passed name isn't a valid path but was found by the API
  640. ATLASSERT(bFullPath);
  641. if(!bFullPath)
  642. {
  643. Close();
  644. ::SetLastError(ERROR_INVALID_NAME);
  645. return FALSE;
  646. }
  647. else
  648. {
  649. // find the last separator
  650. LPTSTR pstrSep = _tcsrchr(m_lpszRoot, m_chDirSeparator);
  651. if(pstrSep != NULL)
  652. *pstrSep = _T('\0');
  653. }
  654. m_bFound = TRUE;
  655. return TRUE;
  656. }
  657. BOOL FindNextFile()
  658. {
  659. ATLASSERT(m_hFind != NULL);
  660. if(m_hFind == NULL)
  661. return FALSE;
  662. if(!m_bFound)
  663. return FALSE;
  664. m_bFound = ::FindNextFile(m_hFind, &m_fd);
  665. return m_bFound;
  666. }
  667. void Close()
  668. {
  669. m_bFound = FALSE;
  670. delete [] m_lpszRoot;
  671. m_lpszRoot = NULL;
  672. if((m_hFind != NULL) && (m_hFind != INVALID_HANDLE_VALUE))
  673. {
  674. ::FindClose(m_hFind);
  675. m_hFind = NULL;
  676. }
  677. }
  678. };
  679. ///////////////////////////////////////////////////////////////////////////////
  680. // Global functions for stock GDI objects
  681. inline HPEN AtlGetStockPen(int nPen)
  682. {
  683. ATLASSERT((nPen == WHITE_PEN) || (nPen == BLACK_PEN) || (nPen == NULL_PEN) || (nPen == DC_PEN));
  684. return (HPEN)::GetStockObject(nPen);
  685. }
  686. inline HBRUSH AtlGetStockBrush(int nBrush)
  687. {
  688. ATLASSERT(((nBrush >= WHITE_BRUSH) && (nBrush <= HOLLOW_BRUSH)) || (nBrush == DC_BRUSH));
  689. return (HBRUSH)::GetStockObject(nBrush);
  690. }
  691. inline HFONT AtlGetStockFont(int nFont)
  692. {
  693. ATLASSERT(((nFont >= OEM_FIXED_FONT) && (nFont <= SYSTEM_FIXED_FONT)) || (nFont == DEFAULT_GUI_FONT));
  694. return (HFONT)::GetStockObject(nFont);
  695. }
  696. inline HPALETTE AtlGetStockPalette(int nPalette)
  697. {
  698. ATLASSERT(nPalette == DEFAULT_PALETTE); // the only one supported
  699. return (HPALETTE)::GetStockObject(nPalette);
  700. }
  701. ///////////////////////////////////////////////////////////////////////////////
  702. // Global function for compacting a path by replacing parts with ellipsis
  703. // helper for multi-byte character sets
  704. inline bool _IsDBCSTrailByte(LPCTSTR lpstr, int nChar)
  705. {
  706. #ifndef _UNICODE
  707. int i = nChar;
  708. for( ; i > 0; i--)
  709. {
  710. if(!::IsDBCSLeadByte(lpstr[i - 1]))
  711. break;
  712. }
  713. return ((nChar > 0) && (((nChar - i) & 1) != 0));
  714. #else // _UNICODE
  715. (void)lpstr; // avoid level 4 warning
  716. (void)nChar; // avoid level 4 warning
  717. return false;
  718. #endif // _UNICODE
  719. }
  720. inline bool AtlCompactPath(LPTSTR lpstrOut, LPCTSTR lpstrIn, int cchLen)
  721. {
  722. ATLASSERT(lpstrOut != NULL);
  723. ATLASSERT(lpstrIn != NULL);
  724. ATLASSERT(cchLen > 0);
  725. LPCTSTR szEllipsis = _T("...");
  726. const int cchEndEllipsis = 3;
  727. const int cchMidEllipsis = 4;
  728. if(lstrlen(lpstrIn) < cchLen)
  729. {
  730. ATL::Checked::tcscpy_s(lpstrOut, cchLen, lpstrIn);
  731. return true;
  732. }
  733. lpstrOut[0] = 0;
  734. // check if the separator is a slash or a backslash
  735. TCHAR chSlash = _T('\\');
  736. for(LPTSTR lpstr = (LPTSTR)lpstrIn; *lpstr != 0; lpstr = ::CharNext(lpstr))
  737. {
  738. if((*lpstr == _T('/')) || (*lpstr == _T('\\')))
  739. chSlash = *lpstr;
  740. }
  741. // find the filename portion of the path
  742. LPCTSTR lpstrFileName = lpstrIn;
  743. for(LPCTSTR pPath = lpstrIn; *pPath; pPath = ::CharNext(pPath))
  744. {
  745. if(((pPath[0] == _T('\\')) || (pPath[0] == _T(':')) || (pPath[0] == _T('/')))
  746. && pPath[1] && (pPath[1] != _T('\\')) && (pPath[1] != _T('/')))
  747. lpstrFileName = pPath + 1;
  748. }
  749. int cchFileName = lstrlen(lpstrFileName);
  750. // handle just the filename without a path
  751. if((lpstrFileName == lpstrIn) && (cchLen > cchEndEllipsis))
  752. {
  753. bool bRet = (ATL::Checked::tcsncpy_s(lpstrOut, cchLen, lpstrIn, cchLen - cchEndEllipsis - 1) == 0);
  754. if(bRet)
  755. {
  756. #ifndef _UNICODE
  757. if(_IsDBCSTrailByte(lpstrIn, cchLen - cchEndEllipsis))
  758. lpstrOut[cchLen - cchEndEllipsis - 1] = 0;
  759. #endif // _UNICODE
  760. ATL::Checked::tcscat_s(lpstrOut, cchLen, szEllipsis);
  761. }
  762. return bRet;
  763. }
  764. // handle just ellipsis
  765. if((cchLen < (cchMidEllipsis + cchEndEllipsis)))
  766. {
  767. for(int i = 0; i < cchLen - 1; i++)
  768. lpstrOut[i] = ((i + 1) == cchMidEllipsis) ? chSlash : _T('.');
  769. lpstrOut[cchLen - 1] = 0;
  770. return true;
  771. }
  772. // calc how much we have to copy
  773. int cchToCopy = cchLen - (cchMidEllipsis + cchFileName) - 1;
  774. if(cchToCopy < 0)
  775. cchToCopy = 0;
  776. #ifndef _UNICODE
  777. if((cchToCopy > 0) && _IsDBCSTrailByte(lpstrIn, cchToCopy))
  778. cchToCopy--;
  779. #endif // _UNICODE
  780. bool bRet = (ATL::Checked::tcsncpy_s(lpstrOut, cchLen, lpstrIn, cchToCopy) == 0);
  781. if(!bRet)
  782. return false;
  783. // add ellipsis
  784. ATL::Checked::tcscat_s(lpstrOut, cchLen, szEllipsis);
  785. TCHAR szSlash[2] = { chSlash, 0 };
  786. ATL::Checked::tcscat_s(lpstrOut, cchLen, szSlash);
  787. // add filename (and ellipsis, if needed)
  788. if(cchLen > (cchMidEllipsis + cchFileName))
  789. {
  790. ATL::Checked::tcscat_s(lpstrOut, cchLen, lpstrFileName);
  791. }
  792. else
  793. {
  794. cchToCopy = cchLen - cchMidEllipsis - cchEndEllipsis - 1;
  795. #ifndef _UNICODE
  796. if((cchToCopy > 0) && _IsDBCSTrailByte(lpstrFileName, cchToCopy))
  797. cchToCopy--;
  798. #endif // _UNICODE
  799. bRet = (ATL::Checked::tcsncpy_s(&lpstrOut[cchMidEllipsis], cchLen - cchMidEllipsis, lpstrFileName, cchToCopy) == 0);
  800. if(bRet)
  801. ATL::Checked::tcscat_s(lpstrOut, cchLen, szEllipsis);
  802. }
  803. return bRet;
  804. }
  805. } // namespace WTL
  806. #endif // __ATLMISC_H__