Source code of Windows XP (NT5)
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.

470 lines
9.5 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1999 - 1999
  6. //
  7. // File: tfc.h
  8. //
  9. //--------------------------------------------------------------------------
  10. #ifndef _CSTRING_H_
  11. #define _CSTRING_H_
  12. /////////////////////////////////////////////////////////////////////////////
  13. // Diagnostic support
  14. #define AFX_MANAGE_STATE(__XX__) ((void)0)
  15. #define AfxGetInstanceHandle() g_hInstance
  16. #define AfxGetResourceHandle() g_hInstance
  17. #define afx_msg
  18. #define DECLARE_MESSAGE_MAP()
  19. #define DECLARE_DYNCREATE(__XX__)
  20. #ifndef ASSERT
  21. #ifdef _DEBUG
  22. BOOL AssertFailedLine(LPCSTR lpszFileName, int nLine);
  23. #define ASSERT(f) \
  24. do \
  25. { \
  26. if (!(f) && AssertFailedLine(__FILE__, __LINE__)) \
  27. DebugBreak(); \
  28. } while (0)
  29. #else // _DEBUG
  30. #define ASSERT(f) ((void)0)
  31. #endif // _DEBUG
  32. #endif // ASSERT
  33. #ifdef _DEBUG
  34. #define VERIFY(f) ASSERT(f)
  35. #else // _DEBUG
  36. #define VERIFY(f) ((void)(f))
  37. #endif // !_DEBUG
  38. extern const WCHAR* wszNull;
  39. class CString
  40. {
  41. public:
  42. // empty constructor
  43. CString();
  44. // copy constructor
  45. CString(const CString& stringSrc);
  46. // from an ANSI string (converts to WCHAR)
  47. CString(LPCSTR lpsz);
  48. // from a UNICODE string (converts to WCHAR)
  49. CString(LPCWSTR lpsz);
  50. ~CString();
  51. private:
  52. // data members
  53. LPWSTR szData;
  54. DWORD dwDataLen;
  55. public:
  56. void Init();
  57. void Empty();
  58. BOOL IsEmpty() const;
  59. LPWSTR GetBuffer(DWORD x=0);
  60. DWORD GetLength() const;
  61. void ReleaseBuffer() {}
  62. // warning: insertion strings cannot exceed MAX_PATH chars
  63. void Format(LPCWSTR lpszFormat, ...);
  64. BSTR AllocSysString() const;
  65. // resource helpers
  66. BOOL LoadString(UINT iRsc);
  67. BOOL FromWindow(HWND hWnd);
  68. BOOL ToWindow(HWND hWnd);
  69. void SetAt(int nIndex, WCHAR ch);
  70. // operators
  71. operator LPCWSTR ( ) const
  72. {
  73. if (szData)
  74. return (LPCWSTR)szData;
  75. else
  76. return (LPCWSTR)wszNull;
  77. }
  78. // test
  79. BOOL IsEqual(LPCWSTR sz);
  80. // assignmt
  81. const CString& operator=(const CString& stringSrc) ;
  82. // W
  83. const CString& operator=(LPCWSTR lpsz);
  84. const CString& operator=(LPWSTR lpsz);
  85. // A
  86. const CString& operator=(LPCSTR lpsz);
  87. const CString& operator=(LPSTR lpsz);
  88. // concat
  89. const CString& operator+=(LPCWSTR lpsz);
  90. const CString& operator+=(const CString& string);
  91. };
  92. class CBitmap
  93. {
  94. private:
  95. HBITMAP m_hBmp;
  96. public:
  97. CBitmap();
  98. ~CBitmap();
  99. HBITMAP LoadBitmap(UINT iRsc);
  100. // operators
  101. operator HBITMAP ( ) const { return m_hBmp; }
  102. };
  103. typedef struct _ELT_PTR
  104. {
  105. _ELT_PTR* pNext;
  106. // other data
  107. void* pData;
  108. } ELT_PTR, *PELT_PTR;
  109. struct __POSITION { };
  110. typedef __POSITION* POSITION;
  111. template<class TYPE, class ARG_TYPE>
  112. class CList
  113. {
  114. private:
  115. PELT_PTR m_pHead;
  116. public:
  117. CList() {m_pHead = NULL;}
  118. ~CList() { Init();}
  119. void Init()
  120. {
  121. RemoveAll();
  122. m_pHead = NULL;
  123. }
  124. TYPE GetHead() { return (TYPE) m_pHead->pData; }
  125. TYPE GetNext(POSITION& pos)
  126. {
  127. POSITION poslast = pos;
  128. pos = (POSITION)((PELT_PTR)pos)->pNext;
  129. return (TYPE) ( ((PELT_PTR)poslast)->pData);
  130. }
  131. TYPE GetAt(POSITION pos)
  132. {
  133. return (TYPE)((PELT_PTR)pos)->pData;
  134. }
  135. POSITION GetHeadPosition() { return (POSITION)m_pHead; }
  136. POSITION GetTailPosition()
  137. {
  138. PELT_PTR p = m_pHead;
  139. PELT_PTR pPrev = NULL;
  140. while(p)
  141. {
  142. pPrev = p;
  143. p = p->pNext;
  144. }
  145. return (POSITION)pPrev;
  146. }
  147. POSITION AddHead(ARG_TYPE typeNewElt)
  148. {
  149. PELT_PTR p = (PELT_PTR)LocalAlloc(LMEM_FIXED, sizeof(ELT_PTR));
  150. if (p)
  151. {
  152. p->pData = (void*)typeNewElt;
  153. p->pNext = m_pHead;
  154. m_pHead = p;
  155. }
  156. return (POSITION)p;
  157. }
  158. POSITION AddTail(ARG_TYPE typeNewElt)
  159. {
  160. PELT_PTR ptail = (PELT_PTR)GetTailPosition();
  161. PELT_PTR p = (PELT_PTR)LocalAlloc(LMEM_FIXED, sizeof(ELT_PTR));
  162. if (p)
  163. {
  164. p->pData = (void*)typeNewElt;
  165. p->pNext = NULL;
  166. }
  167. if (ptail)
  168. {
  169. ptail->pNext = p;
  170. }
  171. else
  172. {
  173. m_pHead = p;
  174. }
  175. return (POSITION)p;
  176. }
  177. void RemoveAt(POSITION pos)
  178. {
  179. PELT_PTR p = m_pHead;
  180. PELT_PTR pPrev = NULL;
  181. while (p && (p != (PELT_PTR)pos))
  182. {
  183. pPrev = p; // keep tabs on prev elt
  184. p = p->pNext; // inc cur elt
  185. }
  186. if (p) // found
  187. {
  188. if (pPrev)
  189. {
  190. pPrev->pNext = p->pNext; // pull out of list
  191. }
  192. else
  193. {
  194. m_pHead = p->pNext; // pull out of head of list
  195. }
  196. LocalFree(p); // free it
  197. }
  198. }
  199. void RemoveAll()
  200. {
  201. PELT_PTR p;
  202. while (m_pHead)
  203. {
  204. p = m_pHead;
  205. m_pHead = m_pHead->pNext;
  206. LocalFree(p);
  207. }
  208. ASSERT(m_pHead == NULL);
  209. }
  210. };
  211. template<class TYPE, class ARG_TYPE>
  212. class CArray
  213. {
  214. private:
  215. TYPE* rgtypeArray;
  216. int iArraySize;
  217. public:
  218. CArray() {iArraySize = 0; rgtypeArray=NULL;}
  219. ~CArray() { Init(); }
  220. void Init()
  221. {
  222. if (rgtypeArray)
  223. {
  224. LocalFree(rgtypeArray);
  225. rgtypeArray = NULL;
  226. iArraySize = 0;
  227. }
  228. }
  229. // operators
  230. TYPE operator [](int i) { return GetAt(i); }
  231. int GetSize() { return iArraySize; }
  232. int GetUpperBound() { return iArraySize -1; }
  233. TYPE GetAt(int i)
  234. {
  235. ASSERT (i < iArraySize);
  236. return rgtypeArray[i];
  237. }
  238. int Add(ARG_TYPE arg)
  239. {
  240. TYPE* p;
  241. if (rgtypeArray)
  242. p = (TYPE*)LocalReAlloc(rgtypeArray, (iArraySize+1) * sizeof(TYPE), LMEM_MOVEABLE);
  243. else
  244. p = (TYPE*)LocalAlloc(LMEM_FIXED, sizeof(TYPE));
  245. if (p == NULL)
  246. return -1;
  247. rgtypeArray = p;
  248. rgtypeArray[iArraySize] = arg;
  249. iArraySize++;
  250. return iArraySize-1;
  251. }
  252. void RemoveAt(int idx, int nCount = 1)
  253. {
  254. // make sure idx is in our range AND
  255. // we're not asked to remove elts past end of our array
  256. ASSERT(GetUpperBound() >= idx);
  257. // IF idx is within bounds
  258. if (GetUpperBound() >= idx)
  259. {
  260. // truncate if we hit the end
  261. if (GetSize() < (idx + nCount))
  262. nCount = GetSize() - idx;
  263. MoveMemory(&rgtypeArray[idx], &rgtypeArray[idx+nCount], ((GetSize() - idx) - nCount)*sizeof(TYPE));
  264. iArraySize -= nCount;
  265. }
  266. }
  267. const TYPE* GetData() { return rgtypeArray; }
  268. };
  269. class CComboBox
  270. {
  271. private:
  272. HWND m_hWnd;
  273. public:
  274. CComboBox() { m_hWnd = NULL; }
  275. ~CComboBox() {}
  276. void Init(HWND hWnd);
  277. void ResetContent() ;
  278. int SetItemData(int idx, DWORD dwData) ;
  279. DWORD GetItemData(int idx) ;
  280. int AddString(LPWSTR sz) ;
  281. int AddString(LPCWSTR sz) ;
  282. int GetCurSel() ;
  283. int SetCurSel(int iSel) ;
  284. int SelectString(int nAfter, LPCWSTR szItem) ;
  285. };
  286. class CFont
  287. {
  288. private:
  289. HFONT m_hFont;
  290. public:
  291. CFont() {m_hFont = NULL;}
  292. ~CFont() {if (m_hFont) DeleteObject(m_hFont);}
  293. operator HFONT () const { ASSERT(m_hFont); return m_hFont; }
  294. BOOL CreateFontIndirect(const LOGFONT* pFont)
  295. {
  296. if (m_hFont)
  297. DeleteObject(m_hFont);
  298. m_hFont = ::CreateFontIndirect(pFont);
  299. return (m_hFont!=NULL);
  300. }
  301. };
  302. class CWaitCursor
  303. {
  304. private:
  305. HCURSOR hPrevCur;
  306. public:
  307. CWaitCursor() { hPrevCur = SetCursor(LoadCursor(NULL, IDC_WAIT));};
  308. ~CWaitCursor() { SetCursor(hPrevCur); };
  309. };
  310. // ListView helpers
  311. int ListView_NewItem(HWND hList, int iIndex, LPCWSTR szText, LPARAM lParam = NULL, int iImage=-1);
  312. int ListView_NewColumn(HWND hwndListView, int iCol, int cx, LPCWSTR szHeading=NULL, int fmt=0 /*LVCFMT_LEFT*/);
  313. LPARAM ListView_GetItemData(HWND hListView, int iItem);
  314. int ListView_GetCurSel(HWND hwndList);
  315. void
  316. ListView_SetItemFiletime(
  317. IN HWND hwndList,
  318. IN int iItem,
  319. IN int iColumn,
  320. IN FILETIME const *pft);
  321. #define AfxMessageBox(__XX__) MessageBox(NULL, __XX__, _TEXT("Debug Message"), MB_OK)
  322. //
  323. // Iterates through a 0 based safe array
  324. //
  325. template <class CElemType>
  326. class SafeArrayEnum
  327. {
  328. public:
  329. SafeArrayEnum(SAFEARRAY* psa) :
  330. m_psa(psa),
  331. m_nIndex(0) {}
  332. void Reset() {m_nIndex = 0;}
  333. HRESULT Next(CElemType& elem)
  334. {
  335. HRESULT hr = S_OK;
  336. hr = SafeArrayGetElement(
  337. m_psa,
  338. &m_nIndex,
  339. &elem);
  340. if(S_OK==hr)
  341. m_nIndex++;
  342. return hr;
  343. }
  344. HRESULT GetAt(LONG nIndex, CElemType& elem)
  345. {
  346. return SafeArrayGetElement(
  347. m_psa,
  348. &nIndex,
  349. &elem);
  350. }
  351. LONG GetCount()
  352. {
  353. LONG lCount = -1;
  354. SafeArrayGetUBound(
  355. m_psa,
  356. 1,
  357. &lCount);
  358. return lCount;
  359. }
  360. // test if one dimension and zero based
  361. bool IsValid()
  362. {
  363. if(1!=SafeArrayGetDim(m_psa))
  364. return false;
  365. LONG lCount = -1;
  366. SafeArrayGetLBound(
  367. m_psa,
  368. 1,
  369. &lCount);
  370. return 0==lCount;
  371. }
  372. protected:
  373. SAFEARRAY* m_psa;
  374. LONG m_nIndex;
  375. };
  376. #endif // #ifndef _CSTRING_H_