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.

396 lines
8.4 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1999 - 1999
  6. //
  7. // File: tfc.h
  8. //
  9. //--------------------------------------------------------------------------
  10. #ifndef _TFC_H_
  11. #define _TFC_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. class CBitmap
  39. {
  40. private:
  41. HBITMAP m_hBmp;
  42. public:
  43. CBitmap();
  44. ~CBitmap();
  45. HBITMAP LoadBitmap(UINT iRsc);
  46. // operators
  47. operator HBITMAP ( ) const { return m_hBmp; }
  48. };
  49. typedef struct _ELT_PTR
  50. {
  51. _ELT_PTR* pNext;
  52. // other data
  53. void* pData;
  54. } ELT_PTR, *PELT_PTR;
  55. struct __POSITION { };
  56. typedef __POSITION* POSITION;
  57. template<class TYPE, class ARG_TYPE>
  58. class CList
  59. {
  60. private:
  61. PELT_PTR m_pHead;
  62. public:
  63. CList() {m_pHead = NULL;}
  64. ~CList() { Init();}
  65. void Init()
  66. {
  67. RemoveAll();
  68. m_pHead = NULL;
  69. }
  70. TYPE GetHead() { return (TYPE) m_pHead->pData; }
  71. TYPE GetNext(POSITION& pos)
  72. {
  73. POSITION poslast = pos;
  74. pos = (POSITION)((PELT_PTR)pos)->pNext;
  75. return (TYPE) ( ((PELT_PTR)poslast)->pData);
  76. }
  77. TYPE GetAt(POSITION pos)
  78. {
  79. return (TYPE)((PELT_PTR)pos)->pData;
  80. }
  81. POSITION GetHeadPosition() { return (POSITION)m_pHead; }
  82. POSITION GetTailPosition()
  83. {
  84. PELT_PTR p = m_pHead;
  85. PELT_PTR pPrev = NULL;
  86. while(p)
  87. {
  88. pPrev = p;
  89. p = p->pNext;
  90. }
  91. return (POSITION)pPrev;
  92. }
  93. POSITION AddHead(ARG_TYPE typeNewElt)
  94. {
  95. PELT_PTR p = (PELT_PTR)LocalAlloc(LMEM_FIXED, sizeof(ELT_PTR));
  96. if (p)
  97. {
  98. p->pData = (void*)typeNewElt;
  99. p->pNext = m_pHead;
  100. m_pHead = p;
  101. }
  102. return (POSITION)p;
  103. }
  104. POSITION AddTail(ARG_TYPE typeNewElt)
  105. {
  106. PELT_PTR ptail = (PELT_PTR)GetTailPosition();
  107. PELT_PTR p = (PELT_PTR)LocalAlloc(LMEM_FIXED, sizeof(ELT_PTR));
  108. if (p)
  109. {
  110. p->pData = (void*)typeNewElt;
  111. p->pNext = NULL;
  112. }
  113. if (ptail)
  114. {
  115. ptail->pNext = p;
  116. }
  117. else
  118. {
  119. m_pHead = p;
  120. }
  121. return (POSITION)p;
  122. }
  123. void RemoveAt(POSITION pos)
  124. {
  125. PELT_PTR p = m_pHead;
  126. PELT_PTR pPrev = NULL;
  127. while (p && (p != (PELT_PTR)pos))
  128. {
  129. pPrev = p; // keep tabs on prev elt
  130. p = p->pNext; // inc cur elt
  131. }
  132. if (p) // found
  133. {
  134. if (pPrev)
  135. {
  136. pPrev->pNext = p->pNext; // pull out of list
  137. }
  138. else
  139. {
  140. m_pHead = p->pNext; // pull out of head of list
  141. }
  142. LocalFree(p); // free it
  143. }
  144. }
  145. void RemoveAll()
  146. {
  147. PELT_PTR p;
  148. while (m_pHead)
  149. {
  150. p = m_pHead;
  151. m_pHead = m_pHead->pNext;
  152. LocalFree(p);
  153. }
  154. ASSERT(m_pHead == NULL);
  155. }
  156. };
  157. template<class TYPE, class ARG_TYPE>
  158. class CArray
  159. {
  160. private:
  161. TYPE* rgtypeArray;
  162. int iArraySize;
  163. public:
  164. CArray() {iArraySize = 0; rgtypeArray=NULL;}
  165. ~CArray() { Init(); }
  166. void Init()
  167. {
  168. if (rgtypeArray)
  169. {
  170. LocalFree(rgtypeArray);
  171. rgtypeArray = NULL;
  172. iArraySize = 0;
  173. }
  174. }
  175. // operators
  176. TYPE operator [](int i) { return GetAt(i); }
  177. int GetSize() { return iArraySize; }
  178. int GetUpperBound() { return iArraySize -1; }
  179. TYPE GetAt(int i)
  180. {
  181. ASSERT (i < iArraySize);
  182. return rgtypeArray[i];
  183. }
  184. int Add(ARG_TYPE arg)
  185. {
  186. TYPE* p;
  187. if (rgtypeArray)
  188. p = (TYPE*)LocalReAlloc(rgtypeArray, (iArraySize+1) * sizeof(TYPE), LMEM_MOVEABLE);
  189. else
  190. p = (TYPE*)LocalAlloc(LMEM_FIXED, sizeof(TYPE));
  191. if (p == NULL)
  192. return -1;
  193. rgtypeArray = p;
  194. rgtypeArray[iArraySize] = arg;
  195. iArraySize++;
  196. return iArraySize-1;
  197. }
  198. void RemoveAt(int idx, int nCount = 1)
  199. {
  200. // make sure idx is in our range AND
  201. // we're not asked to remove elts past end of our array
  202. ASSERT(GetUpperBound() >= idx);
  203. // IF idx is within bounds
  204. if (GetUpperBound() >= idx)
  205. {
  206. // truncate if we hit the end
  207. if (GetSize() < (idx + nCount))
  208. nCount = GetSize() - idx;
  209. MoveMemory(&rgtypeArray[idx], &rgtypeArray[idx+nCount], ((GetSize() - idx) - nCount)*sizeof(TYPE));
  210. iArraySize -= nCount;
  211. }
  212. }
  213. const TYPE* GetData() { return rgtypeArray; }
  214. };
  215. class CComboBox
  216. {
  217. private:
  218. HWND m_hWnd;
  219. public:
  220. CComboBox() { m_hWnd = NULL; }
  221. ~CComboBox() {}
  222. void Init(HWND hWnd);
  223. void ResetContent() ;
  224. int SetItemData(int idx, DWORD dwData) ;
  225. DWORD GetItemData(int idx) ;
  226. int AddString(LPWSTR sz) ;
  227. int AddString(LPCWSTR sz) ;
  228. int GetCurSel() ;
  229. int SetCurSel(int iSel) ;
  230. int SelectString(int nAfter, LPCWSTR szItem) ;
  231. };
  232. class CFont
  233. {
  234. private:
  235. HFONT m_hFont;
  236. public:
  237. CFont() {m_hFont = NULL;}
  238. ~CFont() {if (m_hFont) DeleteObject(m_hFont);}
  239. operator HFONT () const { ASSERT(m_hFont); return m_hFont; }
  240. BOOL CreateFontIndirect(const LOGFONT* pFont)
  241. {
  242. if (m_hFont)
  243. DeleteObject(m_hFont);
  244. m_hFont = ::CreateFontIndirect(pFont);
  245. return (m_hFont!=NULL);
  246. }
  247. };
  248. class CWaitCursor
  249. {
  250. private:
  251. HCURSOR hPrevCur;
  252. public:
  253. CWaitCursor() { hPrevCur = SetCursor(LoadCursor(NULL, IDC_WAIT));};
  254. ~CWaitCursor() { SetCursor(hPrevCur); };
  255. };
  256. // ListView helpers
  257. int ListView_NewItem(HWND hList, int iIndex, LPCWSTR szText, LPARAM lParam = NULL, int iImage=-1);
  258. int ListView_NewColumn(HWND hwndListView, int iCol, int cx, LPCWSTR szHeading=NULL, int fmt=0 /*LVCFMT_LEFT*/);
  259. LPARAM ListView_GetItemData(HWND hListView, int iItem);
  260. int ListView_GetCurSel(HWND hwndList);
  261. void
  262. ListView_SetItemFiletime(
  263. IN HWND hwndList,
  264. IN int iItem,
  265. IN int iColumn,
  266. IN FILETIME const *pft);
  267. #define AfxMessageBox(__XX__) MessageBox(NULL, __XX__, _TEXT("Debug Message"), MB_OK)
  268. //
  269. // Iterates through a 0 based safe array
  270. //
  271. template <class CElemType>
  272. class SafeArrayEnum
  273. {
  274. public:
  275. SafeArrayEnum(SAFEARRAY* psa) :
  276. m_psa(psa),
  277. m_nIndex(0) {}
  278. void Reset() {m_nIndex = 0;}
  279. HRESULT Next(CElemType& elem)
  280. {
  281. HRESULT hr = S_OK;
  282. hr = SafeArrayGetElement(
  283. m_psa,
  284. &m_nIndex,
  285. &elem);
  286. if(S_OK==hr)
  287. m_nIndex++;
  288. return hr;
  289. }
  290. HRESULT GetAt(LONG nIndex, CElemType& elem)
  291. {
  292. return SafeArrayGetElement(
  293. m_psa,
  294. &nIndex,
  295. &elem);
  296. }
  297. LONG GetCount()
  298. {
  299. LONG lCount = -1;
  300. SafeArrayGetUBound(
  301. m_psa,
  302. 1,
  303. &lCount);
  304. return lCount+1;
  305. }
  306. // test if one dimension and zero based
  307. bool IsValid()
  308. {
  309. if(1!=SafeArrayGetDim(m_psa))
  310. return false;
  311. LONG lCount = -1;
  312. SafeArrayGetLBound(
  313. m_psa,
  314. 1,
  315. &lCount);
  316. return 0==lCount;
  317. }
  318. protected:
  319. SAFEARRAY* m_psa;
  320. LONG m_nIndex;
  321. };
  322. #include "cstring.h"
  323. #endif // #ifndef _TFC_H_