atlmisc.h 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352
  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. // CRegProperty
  25. // CRegPropertyImpl<T>
  26. //
  27. // Global functions:
  28. // AtlGetStockPen()
  29. // AtlGetStockBrush()
  30. // AtlGetStockFont()
  31. // AtlGetStockPalette()
  32. //
  33. // AtlCompactPath()
  34. namespace WTL
  35. {
  36. ///////////////////////////////////////////////////////////////////////////////
  37. // CSize scalar operators
  38. #if !defined(_WTL_NO_SIZE_SCALAR) && defined(__ATLTYPES_H__)
  39. template <class Num>
  40. inline CSize operator *(SIZE s, Num n)
  41. {
  42. return CSize((int)(s.cx * n), (int)(s.cy * n));
  43. };
  44. template <class Num>
  45. inline void operator *=(SIZE & s, Num n)
  46. {
  47. s = s * n;
  48. };
  49. template <class Num>
  50. inline CSize operator /(SIZE s, Num n)
  51. {
  52. return CSize((int)(s.cx / n), (int)(s.cy / n));
  53. };
  54. template <class Num>
  55. inline void operator /=(SIZE & s, Num n)
  56. {
  57. s = s / n;
  58. };
  59. #endif // !defined(_WTL_NO_SIZE_SCALAR) && defined(__ATLTYPES_H__)
  60. ///////////////////////////////////////////////////////////////////////////////
  61. // CRecentDocumentList - MRU List Support
  62. #ifndef _WTL_MRUEMPTY_TEXT
  63. #define _WTL_MRUEMPTY_TEXT _T("(empty)")
  64. #endif
  65. // forward declaration
  66. inline bool AtlCompactPath(LPTSTR lpstrOut, LPCTSTR lpstrIn, int cchLen);
  67. template <class T, int t_cchItemLen = MAX_PATH, int t_nFirstID = ID_FILE_MRU_FIRST, int t_nLastID = ID_FILE_MRU_LAST>
  68. class CRecentDocumentListBase
  69. {
  70. public:
  71. // Declarations
  72. struct _DocEntry
  73. {
  74. TCHAR szDocName[t_cchItemLen];
  75. bool operator ==(const _DocEntry& de) const
  76. { return (lstrcmpi(szDocName, de.szDocName) == 0); }
  77. };
  78. enum
  79. {
  80. m_nMaxEntries_Min = 2,
  81. m_nMaxEntries_Max = t_nLastID - t_nFirstID + 1,
  82. m_cchMaxItemLen_Min = 6,
  83. m_cchMaxItemLen_Max = t_cchItemLen,
  84. m_cchItemNameLen = 11
  85. };
  86. // Data members
  87. ATL::CSimpleArray<_DocEntry> m_arrDocs;
  88. int m_nMaxEntries; // default is 4
  89. HMENU m_hMenu;
  90. TCHAR m_szNoEntries[t_cchItemLen];
  91. int m_cchMaxItemLen;
  92. // Constructor
  93. CRecentDocumentListBase() : m_nMaxEntries(4), m_hMenu(NULL), m_cchMaxItemLen(-1)
  94. {
  95. m_szNoEntries[0] = 0;
  96. // These ASSERTs verify values of the template arguments
  97. ATLASSERT(t_cchItemLen > m_cchMaxItemLen_Min);
  98. ATLASSERT(m_nMaxEntries_Max > m_nMaxEntries_Min);
  99. }
  100. // Attributes
  101. HMENU GetMenuHandle() const
  102. {
  103. return m_hMenu;
  104. }
  105. void SetMenuHandle(HMENU hMenu)
  106. {
  107. ATLASSERT((hMenu == NULL) || ::IsMenu(hMenu));
  108. m_hMenu = hMenu;
  109. if((m_hMenu == NULL) || (::GetMenuString(m_hMenu, t_nFirstID, m_szNoEntries, t_cchItemLen, MF_BYCOMMAND) == 0))
  110. {
  111. T* pT = static_cast<T*>(this);
  112. (void)pT; // avoid level 4 warning
  113. ATL::Checked::tcsncpy_s(m_szNoEntries, _countof(m_szNoEntries), pT->GetMRUEmptyText(), _TRUNCATE);
  114. }
  115. }
  116. int GetMaxEntries() const
  117. {
  118. return m_nMaxEntries;
  119. }
  120. void SetMaxEntries(int nMaxEntries)
  121. {
  122. ATLASSERT((nMaxEntries >= m_nMaxEntries_Min) && (nMaxEntries <= m_nMaxEntries_Max));
  123. if(nMaxEntries < m_nMaxEntries_Min)
  124. nMaxEntries = m_nMaxEntries_Min;
  125. else if(nMaxEntries > m_nMaxEntries_Max)
  126. nMaxEntries = m_nMaxEntries_Max;
  127. m_nMaxEntries = nMaxEntries;
  128. }
  129. int GetMaxItemLength() const
  130. {
  131. return m_cchMaxItemLen;
  132. }
  133. void SetMaxItemLength(int cchMaxLen)
  134. {
  135. ATLASSERT(((cchMaxLen >= m_cchMaxItemLen_Min) && (cchMaxLen <= m_cchMaxItemLen_Max)) || (cchMaxLen == -1));
  136. if(cchMaxLen != -1)
  137. {
  138. if(cchMaxLen < m_cchMaxItemLen_Min)
  139. cchMaxLen = m_cchMaxItemLen_Min;
  140. else if(cchMaxLen > m_cchMaxItemLen_Max)
  141. cchMaxLen = m_cchMaxItemLen_Max;
  142. }
  143. m_cchMaxItemLen = cchMaxLen;
  144. T* pT = static_cast<T*>(this);
  145. pT->UpdateMenu();
  146. }
  147. // Operations
  148. BOOL AddToList(LPCTSTR lpstrDocName)
  149. {
  150. _DocEntry de;
  151. errno_t nRet = ATL::Checked::tcsncpy_s(de.szDocName, _countof(de.szDocName), lpstrDocName, _TRUNCATE);
  152. if((nRet != 0) && (nRet != STRUNCATE))
  153. return FALSE;
  154. for(int i = 0; i < m_arrDocs.GetSize(); i++)
  155. {
  156. if(lstrcmpi(m_arrDocs[i].szDocName, lpstrDocName) == 0)
  157. {
  158. m_arrDocs.RemoveAt(i);
  159. break;
  160. }
  161. }
  162. if(m_arrDocs.GetSize() == m_nMaxEntries)
  163. m_arrDocs.RemoveAt(0);
  164. BOOL bRet = m_arrDocs.Add(de);
  165. if(bRet)
  166. {
  167. T* pT = static_cast<T*>(this);
  168. bRet = pT->UpdateMenu();
  169. }
  170. return bRet;
  171. }
  172. // This function is deprecated because it is not safe.
  173. // Use the version below that accepts the buffer length.
  174. __declspec(deprecated)
  175. BOOL GetFromList(int /*nItemID*/, LPTSTR /*lpstrDocName*/)
  176. {
  177. ATLASSERT(FALSE);
  178. return FALSE;
  179. }
  180. BOOL GetFromList(int nItemID, LPTSTR lpstrDocName, int cchLength)
  181. {
  182. int nIndex = m_arrDocs.GetSize() - (nItemID - t_nFirstID) - 1;
  183. if((nIndex < 0) || (nIndex >= m_arrDocs.GetSize()))
  184. return FALSE;
  185. if(lstrlen(m_arrDocs[nIndex].szDocName) >= cchLength)
  186. return FALSE;
  187. ATL::Checked::tcscpy_s(lpstrDocName, cchLength, m_arrDocs[nIndex].szDocName);
  188. return TRUE;
  189. }
  190. #ifdef __ATLSTR_H__
  191. BOOL GetFromList(int nItemID, ATL::CString& strDocName)
  192. {
  193. int nIndex = m_arrDocs.GetSize() - (nItemID - t_nFirstID) - 1;
  194. if((nIndex < 0) || (nIndex >= m_arrDocs.GetSize()))
  195. return FALSE;
  196. strDocName = m_arrDocs[nIndex].szDocName;
  197. return TRUE;
  198. }
  199. #endif // __ATLSTR_H__
  200. BOOL RemoveFromList(int nItemID)
  201. {
  202. int nIndex = m_arrDocs.GetSize() - (nItemID - t_nFirstID) - 1;
  203. BOOL bRet = m_arrDocs.RemoveAt(nIndex);
  204. if(bRet)
  205. {
  206. T* pT = static_cast<T*>(this);
  207. bRet = pT->UpdateMenu();
  208. }
  209. return bRet;
  210. }
  211. BOOL MoveToTop(int nItemID)
  212. {
  213. int nIndex = m_arrDocs.GetSize() - (nItemID - t_nFirstID) - 1;
  214. if((nIndex < 0) || (nIndex >= m_arrDocs.GetSize()))
  215. return FALSE;
  216. _DocEntry de;
  217. de = m_arrDocs[nIndex];
  218. m_arrDocs.RemoveAt(nIndex);
  219. BOOL bRet = m_arrDocs.Add(de);
  220. if(bRet)
  221. {
  222. T* pT = static_cast<T*>(this);
  223. bRet = pT->UpdateMenu();
  224. }
  225. return bRet;
  226. }
  227. BOOL ReadFromRegistry(LPCTSTR lpstrRegKey)
  228. {
  229. T* pT = static_cast<T*>(this);
  230. ATL::CRegKey rkParent;
  231. ATL::CRegKey rk;
  232. LONG lRet = rkParent.Open(HKEY_CURRENT_USER, lpstrRegKey);
  233. if(lRet != ERROR_SUCCESS)
  234. return FALSE;
  235. lRet = rk.Open(rkParent, pT->GetRegKeyName());
  236. if(lRet != ERROR_SUCCESS)
  237. return FALSE;
  238. DWORD dwRet = 0;
  239. lRet = rk.QueryDWORDValue(pT->GetRegCountName(), dwRet);
  240. if(lRet != ERROR_SUCCESS)
  241. return FALSE;
  242. SetMaxEntries(dwRet);
  243. m_arrDocs.RemoveAll();
  244. TCHAR szRetString[t_cchItemLen] = {};
  245. _DocEntry de;
  246. for(int nItem = m_nMaxEntries; nItem > 0; nItem--)
  247. {
  248. TCHAR szBuff[m_cchItemNameLen] = {};
  249. _stprintf_s(szBuff, m_cchItemNameLen, pT->GetRegItemName(), nItem);
  250. ULONG ulCount = t_cchItemLen;
  251. lRet = rk.QueryStringValue(szBuff, szRetString, &ulCount);
  252. if(lRet == ERROR_SUCCESS)
  253. {
  254. ATL::Checked::tcscpy_s(de.szDocName, _countof(de.szDocName), szRetString);
  255. m_arrDocs.Add(de);
  256. }
  257. }
  258. rk.Close();
  259. rkParent.Close();
  260. return pT->UpdateMenu();
  261. }
  262. BOOL WriteToRegistry(LPCTSTR lpstrRegKey)
  263. {
  264. T* pT = static_cast<T*>(this);
  265. (void)pT; // avoid level 4 warning
  266. ATL::CRegKey rkParent;
  267. ATL::CRegKey rk;
  268. LONG lRet = rkParent.Create(HKEY_CURRENT_USER, lpstrRegKey);
  269. if(lRet != ERROR_SUCCESS)
  270. return FALSE;
  271. lRet = rk.Create(rkParent, pT->GetRegKeyName());
  272. if(lRet != ERROR_SUCCESS)
  273. return FALSE;
  274. lRet = rk.SetDWORDValue(pT->GetRegCountName(), m_nMaxEntries);
  275. ATLASSERT(lRet == ERROR_SUCCESS);
  276. // set new values
  277. int nItem;
  278. for(nItem = m_arrDocs.GetSize(); nItem > 0; nItem--)
  279. {
  280. TCHAR szBuff[m_cchItemNameLen] = {};
  281. _stprintf_s(szBuff, m_cchItemNameLen, pT->GetRegItemName(), nItem);
  282. TCHAR szDocName[t_cchItemLen] = {};
  283. GetFromList(t_nFirstID + nItem - 1, szDocName, t_cchItemLen);
  284. lRet = rk.SetStringValue(szBuff, szDocName);
  285. ATLASSERT(lRet == ERROR_SUCCESS);
  286. }
  287. // delete unused keys
  288. for(nItem = m_arrDocs.GetSize() + 1; nItem <= m_nMaxEntries_Max; nItem++)
  289. {
  290. TCHAR szBuff[m_cchItemNameLen] = {};
  291. _stprintf_s(szBuff, m_cchItemNameLen, pT->GetRegItemName(), nItem);
  292. rk.DeleteValue(szBuff);
  293. }
  294. rk.Close();
  295. rkParent.Close();
  296. return TRUE;
  297. }
  298. // Implementation
  299. BOOL UpdateMenu()
  300. {
  301. if(m_hMenu == NULL)
  302. return FALSE;
  303. ATLASSERT(::IsMenu(m_hMenu));
  304. int nItems = ::GetMenuItemCount(m_hMenu);
  305. int nInsertPoint = 0;
  306. for(int i = 0; i < nItems; i++)
  307. {
  308. CMenuItemInfo mi;
  309. mi.fMask = MIIM_ID;
  310. ::GetMenuItemInfo(m_hMenu, i, TRUE, &mi);
  311. if (mi.wID == t_nFirstID)
  312. {
  313. nInsertPoint = i;
  314. break;
  315. }
  316. }
  317. ATLASSERT((nInsertPoint < nItems) && "You need a menu item with an ID = t_nFirstID");
  318. for(int j = t_nFirstID; j < (t_nFirstID + m_nMaxEntries); j++)
  319. {
  320. // keep the first one as an insertion point
  321. if (j != t_nFirstID)
  322. ::DeleteMenu(m_hMenu, j, MF_BYCOMMAND);
  323. }
  324. TCHAR szItemText[t_cchItemLen + 6] = {}; // add space for &, 2 digits, and a space
  325. int nSize = m_arrDocs.GetSize();
  326. int nItem = 0;
  327. if(nSize > 0)
  328. {
  329. for(nItem = 0; nItem < nSize; nItem++)
  330. {
  331. if(m_cchMaxItemLen == -1)
  332. {
  333. _stprintf_s(szItemText, t_cchItemLen + 6, _T("&%i %s"), nItem + 1, m_arrDocs[nSize - 1 - nItem].szDocName);
  334. }
  335. else
  336. {
  337. TCHAR szBuff[t_cchItemLen] = {};
  338. T* pT = static_cast<T*>(this);
  339. (void)pT; // avoid level 4 warning
  340. bool bRet = pT->CompactDocumentName(szBuff, m_arrDocs[nSize - 1 - nItem].szDocName, m_cchMaxItemLen);
  341. (void)bRet; // avoid level 4 warning
  342. ATLASSERT(bRet);
  343. _stprintf_s(szItemText, t_cchItemLen + 6, _T("&%i %s"), nItem + 1, szBuff);
  344. }
  345. ::InsertMenu(m_hMenu, nInsertPoint + nItem, MF_BYPOSITION | MF_STRING, t_nFirstID + nItem, szItemText);
  346. }
  347. }
  348. else // empty
  349. {
  350. ::InsertMenu(m_hMenu, nInsertPoint, MF_BYPOSITION | MF_STRING, t_nFirstID, m_szNoEntries);
  351. ::EnableMenuItem(m_hMenu, t_nFirstID, MF_GRAYED);
  352. nItem++;
  353. }
  354. ::DeleteMenu(m_hMenu, nInsertPoint + nItem, MF_BYPOSITION);
  355. return TRUE;
  356. }
  357. // Overrideables
  358. // override to provide a different method of compacting document names
  359. static bool CompactDocumentName(LPTSTR lpstrOut, LPCTSTR lpstrIn, int cchLen)
  360. {
  361. return AtlCompactPath(lpstrOut, lpstrIn, cchLen);
  362. }
  363. static LPCTSTR GetRegKeyName()
  364. {
  365. return _T("Recent Document List");
  366. }
  367. static LPCTSTR GetRegCountName()
  368. {
  369. return _T("DocumentCount");
  370. }
  371. static LPCTSTR GetRegItemName()
  372. {
  373. // Note: This string is a format string used with wsprintf().
  374. // Resulting formatted string must be m_cchItemNameLen or less
  375. // characters long, including the terminating null character.
  376. return _T("Document%i");
  377. }
  378. static LPCTSTR GetMRUEmptyText()
  379. {
  380. return _WTL_MRUEMPTY_TEXT;
  381. }
  382. };
  383. class CRecentDocumentList : public CRecentDocumentListBase<CRecentDocumentList>
  384. {
  385. public:
  386. // nothing here
  387. };
  388. ///////////////////////////////////////////////////////////////////////////////
  389. // CFindFile - file search helper class
  390. class CFindFile
  391. {
  392. public:
  393. // Data members
  394. HANDLE m_hFind;
  395. WIN32_FIND_DATA m_fd;
  396. LPTSTR m_lpszRoot;
  397. const TCHAR m_chDirSeparator;
  398. BOOL m_bFound;
  399. // Constructor/destructor
  400. CFindFile() : m_hFind(NULL), m_lpszRoot(NULL), m_chDirSeparator(_T('\\')), m_bFound(FALSE)
  401. {
  402. memset(&m_fd, 0, sizeof(m_fd));
  403. }
  404. ~CFindFile()
  405. {
  406. Close();
  407. }
  408. // Attributes
  409. ULONGLONG GetFileSize() const
  410. {
  411. ATLASSERT(m_hFind != NULL);
  412. ULARGE_INTEGER nFileSize = {};
  413. if(m_bFound)
  414. {
  415. nFileSize.LowPart = m_fd.nFileSizeLow;
  416. nFileSize.HighPart = m_fd.nFileSizeHigh;
  417. }
  418. else
  419. {
  420. nFileSize.QuadPart = 0;
  421. }
  422. return nFileSize.QuadPart;
  423. }
  424. BOOL GetFileName(LPTSTR lpstrFileName, int cchLength) const
  425. {
  426. ATLASSERT(m_hFind != NULL);
  427. if(lstrlen(m_fd.cFileName) >= cchLength)
  428. return FALSE;
  429. if(m_bFound)
  430. ATL::Checked::tcscpy_s(lpstrFileName, cchLength, m_fd.cFileName);
  431. return m_bFound;
  432. }
  433. BOOL GetFilePath(LPTSTR lpstrFilePath, int cchLength) const
  434. {
  435. ATLASSERT(m_hFind != NULL);
  436. int nLen = lstrlen(m_lpszRoot);
  437. ATLASSERT(nLen > 0);
  438. if(nLen == 0)
  439. return FALSE;
  440. bool bAddSep = (m_lpszRoot[nLen - 1] != m_chDirSeparator);
  441. if((lstrlen(m_lpszRoot) + (bAddSep ? 1 : 0)) >= cchLength)
  442. return FALSE;
  443. ATL::Checked::tcscpy_s(lpstrFilePath, cchLength, m_lpszRoot);
  444. if(bAddSep)
  445. {
  446. TCHAR szSeparator[2] = { m_chDirSeparator, 0 };
  447. ATL::Checked::tcscat_s(lpstrFilePath, cchLength, szSeparator);
  448. }
  449. ATL::Checked::tcscat_s(lpstrFilePath, cchLength, m_fd.cFileName);
  450. return TRUE;
  451. }
  452. BOOL GetFileTitle(LPTSTR lpstrFileTitle, int cchLength) const
  453. {
  454. ATLASSERT(m_hFind != NULL);
  455. TCHAR szBuff[MAX_PATH] = {};
  456. if(!GetFileName(szBuff, MAX_PATH))
  457. return FALSE;
  458. if(lstrlen(szBuff) >= cchLength)
  459. return FALSE;
  460. // find the last dot
  461. LPTSTR pstrDot = _tcsrchr(szBuff, _T('.'));
  462. if(pstrDot != NULL)
  463. *pstrDot = 0;
  464. ATL::Checked::tcscpy_s(lpstrFileTitle, cchLength, szBuff);
  465. return TRUE;
  466. }
  467. BOOL GetFileURL(LPTSTR lpstrFileURL, int cchLength) const
  468. {
  469. ATLASSERT(m_hFind != NULL);
  470. LPCTSTR lpstrFileURLPrefix = _T("file://");
  471. const int cchPrefix = lstrlen(lpstrFileURLPrefix);
  472. if(cchPrefix >= cchLength)
  473. return FALSE;
  474. ATL::Checked::tcscpy_s(lpstrFileURL, cchLength, lpstrFileURLPrefix);
  475. return GetFilePath(&lpstrFileURL[cchPrefix], cchLength - cchPrefix);
  476. }
  477. BOOL GetRoot(LPTSTR lpstrRoot, int cchLength) const
  478. {
  479. ATLASSERT(m_hFind != NULL);
  480. if(lstrlen(m_lpszRoot) >= cchLength)
  481. return FALSE;
  482. ATL::Checked::tcscpy_s(lpstrRoot, cchLength, m_lpszRoot);
  483. return TRUE;
  484. }
  485. #ifdef __ATLSTR_H__
  486. ATL::CString GetFileName() const
  487. {
  488. ATLASSERT(m_hFind != NULL);
  489. ATL::CString ret;
  490. if(m_bFound)
  491. ret = m_fd.cFileName;
  492. return ret;
  493. }
  494. ATL::CString GetFilePath() const
  495. {
  496. ATLASSERT(m_hFind != NULL);
  497. ATL::CString strResult = m_lpszRoot;
  498. int nLen = strResult.GetLength();
  499. ATLASSERT(nLen > 0);
  500. if(nLen == 0)
  501. return strResult;
  502. if(strResult[nLen - 1] != m_chDirSeparator)
  503. strResult += m_chDirSeparator;
  504. strResult += GetFileName();
  505. return strResult;
  506. }
  507. ATL::CString GetFileTitle() const
  508. {
  509. ATLASSERT(m_hFind != NULL);
  510. ATL::CString strResult;
  511. GetFileTitle(strResult.GetBuffer(MAX_PATH), MAX_PATH);
  512. strResult.ReleaseBuffer();
  513. return strResult;
  514. }
  515. ATL::CString GetFileURL() const
  516. {
  517. ATLASSERT(m_hFind != NULL);
  518. ATL::CString strResult("file://");
  519. strResult += GetFilePath();
  520. return strResult;
  521. }
  522. ATL::CString GetRoot() const
  523. {
  524. ATLASSERT(m_hFind != NULL);
  525. ATL::CString str = m_lpszRoot;
  526. return str;
  527. }
  528. #endif // __ATLSTR_H__
  529. BOOL GetLastWriteTime(FILETIME* pTimeStamp) const
  530. {
  531. ATLASSERT(m_hFind != NULL);
  532. ATLASSERT(pTimeStamp != NULL);
  533. if(m_bFound && (pTimeStamp != NULL))
  534. {
  535. *pTimeStamp = m_fd.ftLastWriteTime;
  536. return TRUE;
  537. }
  538. return FALSE;
  539. }
  540. BOOL GetLastAccessTime(FILETIME* pTimeStamp) const
  541. {
  542. ATLASSERT(m_hFind != NULL);
  543. ATLASSERT(pTimeStamp != NULL);
  544. if(m_bFound && (pTimeStamp != NULL))
  545. {
  546. *pTimeStamp = m_fd.ftLastAccessTime;
  547. return TRUE;
  548. }
  549. return FALSE;
  550. }
  551. BOOL GetCreationTime(FILETIME* pTimeStamp) const
  552. {
  553. ATLASSERT(m_hFind != NULL);
  554. if(m_bFound && (pTimeStamp != NULL))
  555. {
  556. *pTimeStamp = m_fd.ftCreationTime;
  557. return TRUE;
  558. }
  559. return FALSE;
  560. }
  561. BOOL MatchesMask(DWORD dwMask) const
  562. {
  563. ATLASSERT(m_hFind != NULL);
  564. if(m_bFound)
  565. return ((m_fd.dwFileAttributes & dwMask) != 0);
  566. return FALSE;
  567. }
  568. BOOL IsDots() const
  569. {
  570. ATLASSERT(m_hFind != NULL);
  571. // return TRUE if the file name is "." or ".." and
  572. // the file is a directory
  573. BOOL bResult = FALSE;
  574. if(m_bFound && IsDirectory())
  575. {
  576. if((m_fd.cFileName[0] == _T('.')) && ((m_fd.cFileName[1] == _T('\0')) || ((m_fd.cFileName[1] == _T('.')) && (m_fd.cFileName[2] == _T('\0')))))
  577. bResult = TRUE;
  578. }
  579. return bResult;
  580. }
  581. BOOL IsReadOnly() const
  582. {
  583. return MatchesMask(FILE_ATTRIBUTE_READONLY);
  584. }
  585. BOOL IsDirectory() const
  586. {
  587. return MatchesMask(FILE_ATTRIBUTE_DIRECTORY);
  588. }
  589. BOOL IsCompressed() const
  590. {
  591. return MatchesMask(FILE_ATTRIBUTE_COMPRESSED);
  592. }
  593. BOOL IsSystem() const
  594. {
  595. return MatchesMask(FILE_ATTRIBUTE_SYSTEM);
  596. }
  597. BOOL IsHidden() const
  598. {
  599. return MatchesMask(FILE_ATTRIBUTE_HIDDEN);
  600. }
  601. BOOL IsTemporary() const
  602. {
  603. return MatchesMask(FILE_ATTRIBUTE_TEMPORARY);
  604. }
  605. BOOL IsNormal() const
  606. {
  607. return MatchesMask(FILE_ATTRIBUTE_NORMAL);
  608. }
  609. BOOL IsArchived() const
  610. {
  611. return MatchesMask(FILE_ATTRIBUTE_ARCHIVE);
  612. }
  613. // Operations
  614. BOOL FindFile(LPCTSTR pstrName = NULL, bool bAutoLongPath = false)
  615. {
  616. Close();
  617. if(pstrName == NULL)
  618. pstrName = _T("*.*");
  619. if(bAutoLongPath && (lstrlen(pstrName) >= MAX_PATH))
  620. {
  621. LPCTSTR lpstrPrefix = _T("\\\\?\\");
  622. int cchLongPath = lstrlen(lpstrPrefix) + lstrlen(pstrName) + 1;
  623. ATL::CTempBuffer<TCHAR, _WTL_STACK_ALLOC_THRESHOLD> buff;
  624. LPTSTR lpstrLongPath = buff.Allocate(cchLongPath);
  625. if(lpstrLongPath != NULL)
  626. {
  627. ATL::Checked::tcscpy_s(lpstrLongPath, cchLongPath, lpstrPrefix);
  628. ATL::Checked::tcscat_s(lpstrLongPath, cchLongPath, pstrName);
  629. m_hFind = ::FindFirstFile(lpstrLongPath, &m_fd);
  630. }
  631. }
  632. else
  633. {
  634. m_hFind = ::FindFirstFile(pstrName, &m_fd);
  635. }
  636. if(m_hFind == INVALID_HANDLE_VALUE)
  637. return FALSE;
  638. int cchRoot = ::GetFullPathName(pstrName, 0, NULL, NULL);
  639. if(cchRoot > 0)
  640. ATLTRY(m_lpszRoot = new TCHAR[cchRoot]);
  641. if(m_lpszRoot == NULL)
  642. return FALSE;
  643. bool bFullPath = (::GetFullPathName(pstrName, cchRoot, m_lpszRoot, NULL) != 0);
  644. // passed name isn't a valid path but was found by the API
  645. ATLASSERT(bFullPath);
  646. if(!bFullPath)
  647. {
  648. Close();
  649. ::SetLastError(ERROR_INVALID_NAME);
  650. return FALSE;
  651. }
  652. else
  653. {
  654. // find the last separator
  655. LPTSTR pstrSep = _tcsrchr(m_lpszRoot, m_chDirSeparator);
  656. if(pstrSep != NULL)
  657. *pstrSep = _T('\0');
  658. }
  659. m_bFound = TRUE;
  660. return TRUE;
  661. }
  662. BOOL FindNextFile()
  663. {
  664. ATLASSERT(m_hFind != NULL);
  665. if(m_hFind == NULL)
  666. return FALSE;
  667. if(!m_bFound)
  668. return FALSE;
  669. m_bFound = ::FindNextFile(m_hFind, &m_fd);
  670. return m_bFound;
  671. }
  672. void Close()
  673. {
  674. m_bFound = FALSE;
  675. delete [] m_lpszRoot;
  676. m_lpszRoot = NULL;
  677. if((m_hFind != NULL) && (m_hFind != INVALID_HANDLE_VALUE))
  678. {
  679. ::FindClose(m_hFind);
  680. m_hFind = NULL;
  681. }
  682. }
  683. };
  684. ///////////////////////////////////////////////////////////////////////////////
  685. // CRegProperty and CRegPropertyImpl<> - properties stored in registry
  686. // How to use: Derive a class from CRegPropertyImpl, add data members
  687. // for properties, and add REGPROP map to map properties to registry value names.
  688. // You can then call Read() and Write() methods to read and write properties to/from registry.
  689. // You can also use CRegProperty class directly, for one time read/write, or for custom stuff.
  690. #define REGPROP_CURRENTUSER 0x0000
  691. #define REGPROP_LOCALMACHINE 0x0001
  692. #define REGPROP_READONLY 0x0002
  693. #define REGPROP_WRITEONLY 0x0004
  694. class CRegProperty
  695. {
  696. public:
  697. // Type declarations
  698. struct BinaryProp
  699. {
  700. void* pBinary;
  701. ULONG uSize; // buffer size in bytes, used size after read
  702. BinaryProp() : pBinary(NULL), uSize(0U)
  703. { }
  704. };
  705. struct CharArrayProp
  706. {
  707. LPTSTR lpstrText;
  708. ULONG uSize; // buffer size in chars
  709. CharArrayProp() : lpstrText(NULL), uSize(0U)
  710. { }
  711. };
  712. // Data members
  713. ATL::CRegKey m_regkey;
  714. WORD m_wFlags;
  715. // Constructor
  716. CRegProperty() : m_wFlags(REGPROP_CURRENTUSER)
  717. { }
  718. // Registry key methods
  719. LSTATUS OpenRegKey(LPCTSTR lpstrRegKey, bool bWrite)
  720. {
  721. ATLASSERT(m_regkey.m_hKey == NULL);
  722. HKEY hKey = ((m_wFlags & REGPROP_LOCALMACHINE) != 0) ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
  723. REGSAM sam = KEY_READ | KEY_WRITE;
  724. LSTATUS lRet = -1;
  725. if(bWrite)
  726. lRet = m_regkey.Create(hKey, lpstrRegKey, NULL, 0, ((m_wFlags & REGPROP_WRITEONLY) != 0) ? KEY_WRITE : sam);
  727. else
  728. lRet = m_regkey.Open(hKey, lpstrRegKey, ((m_wFlags & REGPROP_READONLY) != 0) ? KEY_READ : sam);
  729. return lRet;
  730. }
  731. void CloseRegKey()
  732. {
  733. LSTATUS lRet = m_regkey.Close();
  734. (void)lRet; // avoid level 4 warning
  735. ATLASSERT(lRet == ERROR_SUCCESS);
  736. }
  737. // Flag methods
  738. WORD GetFlags() const
  739. {
  740. return m_wFlags;
  741. }
  742. WORD SetFlags(WORD wFlags, WORD wMask = 0)
  743. {
  744. WORD wPrevFlags = m_wFlags;
  745. if(wMask == 0)
  746. m_wFlags = wFlags;
  747. else
  748. m_wFlags = (m_wFlags & ~wMask) | (wFlags & wMask);
  749. return wPrevFlags;
  750. }
  751. // Generic read/write methods
  752. template <class TProp>
  753. LSTATUS ReadProp(LPCTSTR lpstrRegValue, TProp& prop)
  754. {
  755. ATLASSERT(m_regkey.m_hKey != NULL);
  756. DWORD dwRet = 0;
  757. LSTATUS lRet = m_regkey.QueryDWORDValue(lpstrRegValue, dwRet);
  758. if(lRet == ERROR_SUCCESS)
  759. prop = static_cast<TProp>(dwRet);
  760. return lRet;
  761. }
  762. template <class TProp>
  763. LSTATUS WriteProp(LPCTSTR lpstrRegValue, TProp& prop)
  764. {
  765. ATLASSERT(m_regkey.m_hKey != NULL);
  766. return m_regkey.SetDWORDValue(lpstrRegValue, (DWORD)prop);
  767. }
  768. // Specialization for bool
  769. template <>
  770. LSTATUS ReadProp(LPCTSTR lpstrRegValue, bool& bProp)
  771. {
  772. ATLASSERT(m_regkey.m_hKey != NULL);
  773. DWORD dwRet = 0;
  774. LSTATUS lRet = m_regkey.QueryDWORDValue(lpstrRegValue, dwRet);
  775. if(lRet == ERROR_SUCCESS)
  776. bProp = (dwRet != 0);
  777. return lRet;
  778. }
  779. template <>
  780. LSTATUS WriteProp(LPCTSTR lpstrRegValue, bool& bProp)
  781. {
  782. ATLASSERT(m_regkey.m_hKey != NULL);
  783. return m_regkey.SetDWORDValue(lpstrRegValue, bProp ? 1 : 0);
  784. }
  785. // Specialization for HFONT
  786. template <>
  787. LSTATUS ReadProp(LPCTSTR lpstrRegValue, HFONT& hFont)
  788. {
  789. ATLASSERT(m_regkey.m_hKey != NULL);
  790. LOGFONT lf = {};
  791. ULONG uSize = sizeof(lf);
  792. LSTATUS lRet = m_regkey.QueryBinaryValue(lpstrRegValue, &lf, &uSize);
  793. if(lRet == ERROR_SUCCESS)
  794. {
  795. if(hFont != NULL)
  796. ::DeleteObject(hFont);
  797. hFont = ::CreateFontIndirect(&lf);
  798. if(hFont == NULL)
  799. lRet = ERROR_INVALID_DATA;
  800. }
  801. return lRet;
  802. }
  803. template <>
  804. LSTATUS WriteProp(LPCTSTR lpstrRegValue, HFONT& hFont)
  805. {
  806. ATLASSERT(m_regkey.m_hKey != NULL);
  807. CLogFont lf(hFont);
  808. return m_regkey.SetBinaryValue(lpstrRegValue, &lf, sizeof(lf));
  809. }
  810. // Specialization for BinaryProp
  811. template <>
  812. LSTATUS ReadProp(LPCTSTR lpstrRegValue, BinaryProp& binProp)
  813. {
  814. ATLASSERT(m_regkey.m_hKey != NULL);
  815. ULONG uSize = 0U;
  816. LSTATUS lRet = m_regkey.QueryBinaryValue(lpstrRegValue, NULL, &uSize);
  817. if(lRet == ERROR_SUCCESS)
  818. {
  819. if(uSize <= binProp.uSize)
  820. lRet = m_regkey.QueryBinaryValue(lpstrRegValue, binProp.pBinary, &binProp.uSize);
  821. else
  822. lRet = ERROR_OUTOFMEMORY;
  823. }
  824. return lRet;
  825. }
  826. template <>
  827. LSTATUS WriteProp(LPCTSTR lpstrRegValue, BinaryProp& binProp)
  828. {
  829. ATLASSERT(m_regkey.m_hKey != NULL);
  830. return m_regkey.SetBinaryValue(lpstrRegValue, binProp.pBinary, binProp.uSize);
  831. }
  832. // Specialization for CharArrayProp
  833. template <>
  834. LSTATUS ReadProp(LPCTSTR lpstrRegValue, CharArrayProp& caProp)
  835. {
  836. ATLASSERT(m_regkey.m_hKey != NULL);
  837. ULONG uSize = 0U;
  838. LSTATUS lRet = m_regkey.QueryStringValue(lpstrRegValue, NULL, &uSize);
  839. if(lRet == ERROR_SUCCESS)
  840. {
  841. if(uSize <= caProp.uSize)
  842. lRet = m_regkey.QueryStringValue(lpstrRegValue, caProp.lpstrText, &caProp.uSize);
  843. else
  844. lRet = ERROR_OUTOFMEMORY;
  845. }
  846. return lRet;
  847. }
  848. template <>
  849. LSTATUS WriteProp(LPCTSTR lpstrRegValue, CharArrayProp& caProp)
  850. {
  851. ATLASSERT(m_regkey.m_hKey != NULL);
  852. return m_regkey.SetStringValue(lpstrRegValue, caProp.lpstrText);
  853. }
  854. // Specialization for CString
  855. #ifdef __ATLSTR_H__
  856. template <>
  857. LSTATUS ReadProp(LPCTSTR lpstrRegValue, ATL::CString& strProp)
  858. {
  859. ATLASSERT(m_regkey.m_hKey != NULL);
  860. ULONG uSize = 0U;
  861. LSTATUS lRet = m_regkey.QueryStringValue(lpstrRegValue, NULL, &uSize);
  862. if(lRet == ERROR_SUCCESS)
  863. {
  864. lRet = m_regkey.QueryStringValue(lpstrRegValue, strProp.GetBufferSetLength(uSize), &uSize);
  865. strProp.ReleaseBuffer();
  866. }
  867. return lRet;
  868. }
  869. template <>
  870. LSTATUS WriteProp(LPCTSTR lpstrRegValue, ATL::CString& strProp)
  871. {
  872. ATLASSERT(m_regkey.m_hKey != NULL);
  873. return m_regkey.SetStringValue(lpstrRegValue, (LPCTSTR)strProp);
  874. }
  875. #endif // __ATLSTR_H__
  876. // Static methods for one time read/write
  877. template <class TProp>
  878. static bool ReadOne(LPCTSTR lpstrRegKey, LPCTSTR lpstrRegValue, TProp& prop, WORD wFlags = REGPROP_CURRENTUSER)
  879. {
  880. CRegProperty rp;
  881. rp.SetFlags(wFlags);
  882. LSTATUS lRet = rp.OpenRegKey(lpstrRegKey, false);
  883. if(lRet == ERROR_SUCCESS)
  884. {
  885. lRet = rp.ReadProp(lpstrRegValue, prop);
  886. rp.CloseRegKey();
  887. }
  888. return (lRet == ERROR_SUCCESS) || (lRet == ERROR_FILE_NOT_FOUND);
  889. }
  890. template <class TProp>
  891. static bool WriteOne(LPCTSTR lpstrRegKey, LPCTSTR lpstrRegValue, TProp& prop, WORD wFlags = REGPROP_CURRENTUSER)
  892. {
  893. CRegProperty rp;
  894. rp.SetFlags(wFlags);
  895. LSTATUS lRet = rp.OpenRegKey(lpstrRegKey, true);
  896. if(lRet == ERROR_SUCCESS)
  897. {
  898. lRet = rp.WriteProp(lpstrRegValue, prop);
  899. rp.CloseRegKey();
  900. }
  901. return (lRet == ERROR_SUCCESS);
  902. }
  903. };
  904. #define BEGIN_REGPROP_MAP(class) \
  905. void ReadWriteAll(bool bWrite) \
  906. {
  907. #define REG_PROPERTY(name, prop) \
  908. this->ReadWriteProp(name, prop, bWrite);
  909. #define END_REGPROP_MAP() \
  910. }
  911. template <class T>
  912. class CRegPropertyImpl : public CRegProperty
  913. {
  914. public:
  915. // Methods
  916. void Read(LPCTSTR lpstrRegKey)
  917. {
  918. T* pT = static_cast<T*>(this);
  919. LSTATUS lRet = pT->OpenRegKey(lpstrRegKey, false);
  920. if(lRet == ERROR_SUCCESS)
  921. {
  922. pT->ReadWriteAll(false);
  923. pT->OnRead(lpstrRegKey);
  924. pT->CloseRegKey();
  925. }
  926. else if(lRet != ERROR_FILE_NOT_FOUND)
  927. {
  928. pT->OnReadError(NULL, lRet);
  929. }
  930. }
  931. void Write(LPCTSTR lpstrRegKey)
  932. {
  933. T* pT = static_cast<T*>(this);
  934. LSTATUS lRet = pT->OpenRegKey(lpstrRegKey, true);
  935. if(lRet == ERROR_SUCCESS)
  936. {
  937. pT->ReadWriteAll(true);
  938. pT->OnWrite(lpstrRegKey);
  939. pT->CloseRegKey();
  940. }
  941. else
  942. {
  943. pT->OnWriteError(NULL, lRet);
  944. }
  945. }
  946. // Implementation
  947. template <class TProp>
  948. void ReadWriteProp(LPCTSTR lpstrRegValue, TProp& prop, bool bWrite)
  949. {
  950. T* pT = static_cast<T*>(this);
  951. if(bWrite)
  952. {
  953. LSTATUS lRet = pT->WriteProp(lpstrRegValue, prop);
  954. if(lRet != ERROR_SUCCESS)
  955. pT->OnWriteError(lpstrRegValue, lRet);
  956. }
  957. else
  958. {
  959. LSTATUS lRet = pT->ReadProp(lpstrRegValue, prop);
  960. if((lRet != ERROR_SUCCESS) && (lRet != ERROR_FILE_NOT_FOUND))
  961. pT->OnReadError(lpstrRegValue, lRet);
  962. }
  963. }
  964. // Overrideable handlers
  965. void OnRead(LPCTSTR /*lpstrRegKey*/)
  966. { }
  967. void OnWrite(LPCTSTR /*lpstrRegKey*/)
  968. { }
  969. void OnReadError(LPCTSTR /*lpstrRegValue*/, LSTATUS /*lError*/)
  970. {
  971. ATLASSERT(FALSE);
  972. }
  973. void OnWriteError(LPCTSTR /*lpstrRegValue*/, LSTATUS /*lError*/)
  974. {
  975. ATLASSERT(FALSE);
  976. }
  977. };
  978. ///////////////////////////////////////////////////////////////////////////////
  979. // Global functions for stock GDI objects
  980. inline HPEN AtlGetStockPen(int nPen)
  981. {
  982. ATLASSERT((nPen == WHITE_PEN) || (nPen == BLACK_PEN) || (nPen == NULL_PEN) || (nPen == DC_PEN));
  983. return (HPEN)::GetStockObject(nPen);
  984. }
  985. inline HBRUSH AtlGetStockBrush(int nBrush)
  986. {
  987. ATLASSERT(((nBrush >= WHITE_BRUSH) && (nBrush <= HOLLOW_BRUSH)) || (nBrush == DC_BRUSH));
  988. return (HBRUSH)::GetStockObject(nBrush);
  989. }
  990. inline HFONT AtlGetStockFont(int nFont)
  991. {
  992. ATLASSERT(((nFont >= OEM_FIXED_FONT) && (nFont <= SYSTEM_FIXED_FONT)) || (nFont == DEFAULT_GUI_FONT));
  993. return (HFONT)::GetStockObject(nFont);
  994. }
  995. inline HPALETTE AtlGetStockPalette(int nPalette)
  996. {
  997. ATLASSERT(nPalette == DEFAULT_PALETTE); // the only one supported
  998. return (HPALETTE)::GetStockObject(nPalette);
  999. }
  1000. ///////////////////////////////////////////////////////////////////////////////
  1001. // Global function for compacting a path by replacing parts with ellipsis
  1002. // helper for multi-byte character sets
  1003. inline bool _IsDBCSTrailByte(LPCTSTR lpstr, int nChar)
  1004. {
  1005. #ifndef _UNICODE
  1006. int i = nChar;
  1007. for( ; i > 0; i--)
  1008. {
  1009. if(!::IsDBCSLeadByte(lpstr[i - 1]))
  1010. break;
  1011. }
  1012. return ((nChar > 0) && (((nChar - i) & 1) != 0));
  1013. #else // _UNICODE
  1014. (void)lpstr; // avoid level 4 warning
  1015. (void)nChar; // avoid level 4 warning
  1016. return false;
  1017. #endif // _UNICODE
  1018. }
  1019. inline bool AtlCompactPath(LPTSTR lpstrOut, LPCTSTR lpstrIn, int cchLen)
  1020. {
  1021. ATLASSERT(lpstrOut != NULL);
  1022. ATLASSERT(lpstrIn != NULL);
  1023. ATLASSERT(cchLen > 0);
  1024. LPCTSTR szEllipsis = _T("...");
  1025. const int cchEndEllipsis = 3;
  1026. const int cchMidEllipsis = 4;
  1027. if(lstrlen(lpstrIn) < cchLen)
  1028. {
  1029. ATL::Checked::tcscpy_s(lpstrOut, cchLen, lpstrIn);
  1030. return true;
  1031. }
  1032. lpstrOut[0] = 0;
  1033. // check if the separator is a slash or a backslash
  1034. TCHAR chSlash = _T('\\');
  1035. for(LPTSTR lpstr = (LPTSTR)lpstrIn; *lpstr != 0; lpstr = ::CharNext(lpstr))
  1036. {
  1037. if((*lpstr == _T('/')) || (*lpstr == _T('\\')))
  1038. chSlash = *lpstr;
  1039. }
  1040. // find the filename portion of the path
  1041. LPCTSTR lpstrFileName = lpstrIn;
  1042. for(LPCTSTR pPath = lpstrIn; *pPath; pPath = ::CharNext(pPath))
  1043. {
  1044. if(((pPath[0] == _T('\\')) || (pPath[0] == _T(':')) || (pPath[0] == _T('/')))
  1045. && pPath[1] && (pPath[1] != _T('\\')) && (pPath[1] != _T('/')))
  1046. lpstrFileName = pPath + 1;
  1047. }
  1048. int cchFileName = lstrlen(lpstrFileName);
  1049. // handle just the filename without a path
  1050. if((lpstrFileName == lpstrIn) && (cchLen > cchEndEllipsis))
  1051. {
  1052. bool bRet = (ATL::Checked::tcsncpy_s(lpstrOut, cchLen, lpstrIn, cchLen - cchEndEllipsis - 1) == 0);
  1053. if(bRet)
  1054. {
  1055. #ifndef _UNICODE
  1056. if(_IsDBCSTrailByte(lpstrIn, cchLen - cchEndEllipsis))
  1057. lpstrOut[cchLen - cchEndEllipsis - 1] = 0;
  1058. #endif // _UNICODE
  1059. ATL::Checked::tcscat_s(lpstrOut, cchLen, szEllipsis);
  1060. }
  1061. return bRet;
  1062. }
  1063. // handle just ellipsis
  1064. if((cchLen < (cchMidEllipsis + cchEndEllipsis)))
  1065. {
  1066. for(int i = 0; i < cchLen - 1; i++)
  1067. lpstrOut[i] = ((i + 1) == cchMidEllipsis) ? chSlash : _T('.');
  1068. lpstrOut[cchLen - 1] = 0;
  1069. return true;
  1070. }
  1071. // calc how much we have to copy
  1072. int cchToCopy = cchLen - (cchMidEllipsis + cchFileName) - 1;
  1073. if(cchToCopy < 0)
  1074. cchToCopy = 0;
  1075. #ifndef _UNICODE
  1076. if((cchToCopy > 0) && _IsDBCSTrailByte(lpstrIn, cchToCopy))
  1077. cchToCopy--;
  1078. #endif // _UNICODE
  1079. bool bRet = (ATL::Checked::tcsncpy_s(lpstrOut, cchLen, lpstrIn, cchToCopy) == 0);
  1080. if(!bRet)
  1081. return false;
  1082. // add ellipsis
  1083. ATL::Checked::tcscat_s(lpstrOut, cchLen, szEllipsis);
  1084. TCHAR szSlash[2] = { chSlash, 0 };
  1085. ATL::Checked::tcscat_s(lpstrOut, cchLen, szSlash);
  1086. // add filename (and ellipsis, if needed)
  1087. if(cchLen > (cchMidEllipsis + cchFileName))
  1088. {
  1089. ATL::Checked::tcscat_s(lpstrOut, cchLen, lpstrFileName);
  1090. }
  1091. else
  1092. {
  1093. cchToCopy = cchLen - cchMidEllipsis - cchEndEllipsis - 1;
  1094. #ifndef _UNICODE
  1095. if((cchToCopy > 0) && _IsDBCSTrailByte(lpstrFileName, cchToCopy))
  1096. cchToCopy--;
  1097. #endif // _UNICODE
  1098. bRet = (ATL::Checked::tcsncpy_s(&lpstrOut[cchMidEllipsis], cchLen - cchMidEllipsis, lpstrFileName, cchToCopy) == 0);
  1099. if(bRet)
  1100. ATL::Checked::tcscat_s(lpstrOut, cchLen, szEllipsis);
  1101. }
  1102. return bRet;
  1103. }
  1104. } // namespace WTL
  1105. #endif // __ATLMISC_H__