Utils.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. #ifndef __UTILS_H__
  2. #define __UTILS_H__
  3. #pragma once
  4. #include "OAIdl.h"
  5. #include <vector>
  6. namespace DuiLib
  7. {
  8. /////////////////////////////////////////////////////////////////////////////////////
  9. //
  10. class UILIB_API STRINGorID
  11. {
  12. public:
  13. STRINGorID(LPCTSTR lpString) : m_lpstr(lpString)
  14. { }
  15. STRINGorID(UINT nID) : m_lpstr(MAKEINTRESOURCE(nID))
  16. { }
  17. LPCTSTR m_lpstr;
  18. };
  19. /////////////////////////////////////////////////////////////////////////////////////
  20. //
  21. class UILIB_API CDuiPoint : public tagPOINT
  22. {
  23. public:
  24. CDuiPoint();
  25. CDuiPoint(const POINT& src);
  26. CDuiPoint(int x, int y);
  27. CDuiPoint(LPARAM lParam);
  28. };
  29. /////////////////////////////////////////////////////////////////////////////////////
  30. //
  31. class UILIB_API CDuiSize : public tagSIZE
  32. {
  33. public:
  34. CDuiSize();
  35. CDuiSize(const SIZE& src);
  36. CDuiSize(const RECT rc);
  37. CDuiSize(int cx, int cy);
  38. };
  39. /////////////////////////////////////////////////////////////////////////////////////
  40. //
  41. class UILIB_API CDuiRect : public tagRECT
  42. {
  43. public:
  44. CDuiRect();
  45. CDuiRect(const RECT& src);
  46. CDuiRect(int iLeft, int iTop, int iRight, int iBottom);
  47. int GetWidth() const;
  48. int GetHeight() const;
  49. void Empty();
  50. bool IsNull() const;
  51. void Join(const RECT& rc);
  52. void ResetOffset();
  53. void Normalize();
  54. void Offset(int cx, int cy);
  55. void Inflate(int cx, int cy);
  56. void Deflate(int cx, int cy);
  57. void Union(CDuiRect& rc);
  58. };
  59. /////////////////////////////////////////////////////////////////////////////////////
  60. //
  61. class UILIB_API CStdPtrArray
  62. {
  63. public:
  64. CStdPtrArray(int iPreallocSize = 0);
  65. CStdPtrArray(const CStdPtrArray& src);
  66. ~CStdPtrArray();
  67. void Empty();
  68. void Resize(int iSize);
  69. bool IsEmpty() const;
  70. int Find(LPVOID iIndex) const;
  71. bool Add(LPVOID pData);
  72. bool SetAt(int iIndex, LPVOID pData);
  73. bool InsertAt(int iIndex, LPVOID pData);
  74. bool Remove(int iIndex);
  75. int GetSize() const;
  76. LPVOID* GetData();
  77. LPVOID GetAt(int iIndex) const;
  78. LPVOID operator[] (int nIndex) const;
  79. protected:
  80. LPVOID* m_ppVoid;
  81. int m_nCount;
  82. int m_nAllocated;
  83. };
  84. /////////////////////////////////////////////////////////////////////////////////////
  85. //
  86. class UILIB_API CStdValArray
  87. {
  88. public:
  89. CStdValArray(int iElementSize, int iPreallocSize = 0);
  90. ~CStdValArray();
  91. void Empty();
  92. bool IsEmpty() const;
  93. bool Add(LPCVOID pData);
  94. bool Remove(int iIndex);
  95. int GetSize() const;
  96. LPVOID GetData();
  97. LPVOID GetAt(int iIndex) const;
  98. LPVOID operator[] (int nIndex) const;
  99. protected:
  100. LPBYTE m_pVoid;
  101. int m_iElementSize;
  102. int m_nCount;
  103. int m_nAllocated;
  104. };
  105. /////////////////////////////////////////////////////////////////////////////////////
  106. //
  107. class UILIB_API CDuiString
  108. {
  109. public:
  110. enum { MAX_LOCAL_STRING_LEN = 63 };
  111. CDuiString();
  112. CDuiString(const TCHAR ch);
  113. CDuiString(const CDuiString& src);
  114. CDuiString(LPCTSTR lpsz, int nLen = -1);
  115. ~CDuiString();
  116. void Empty();
  117. int GetLength() const;
  118. bool IsEmpty() const;
  119. TCHAR GetAt(int nIndex) const;
  120. void Append(LPCTSTR pstr);
  121. void Assign(LPCTSTR pstr, int nLength = -1);
  122. LPCTSTR GetData() const;
  123. void SetAt(int nIndex, TCHAR ch);
  124. operator LPCTSTR() const;
  125. TCHAR operator[] (int nIndex) const;
  126. const CDuiString& operator=(const CDuiString& src);
  127. const CDuiString& operator=(const TCHAR ch);
  128. const CDuiString& operator=(LPCTSTR pstr);
  129. #ifdef _UNICODE
  130. const CDuiString& operator=(LPCSTR lpStr);
  131. const CDuiString& operator+=(LPCSTR lpStr);
  132. #else
  133. const CDuiString& operator=(LPCWSTR lpwStr);
  134. const CDuiString& operator+=(LPCWSTR lpwStr);
  135. #endif
  136. CDuiString operator+(const CDuiString& src) const;
  137. CDuiString operator+(LPCTSTR pstr) const;
  138. const CDuiString& operator+=(const CDuiString& src);
  139. const CDuiString& operator+=(LPCTSTR pstr);
  140. const CDuiString& operator+=(const TCHAR ch);
  141. bool operator == (LPCTSTR str) const;
  142. bool operator != (LPCTSTR str) const;
  143. bool operator <= (LPCTSTR str) const;
  144. bool operator < (LPCTSTR str) const;
  145. bool operator >= (LPCTSTR str) const;
  146. bool operator > (LPCTSTR str) const;
  147. int Compare(LPCTSTR pstr) const;
  148. int CompareNoCase(LPCTSTR pstr) const;
  149. void MakeUpper();
  150. void MakeLower();
  151. CDuiString Left(int nLength) const;
  152. CDuiString Mid(int iPos, int nLength = -1) const;
  153. CDuiString Right(int nLength) const;
  154. CDuiString& TrimLeft();
  155. CDuiString& TrimRight();
  156. CDuiString& Trim();
  157. int Find(TCHAR ch, int iPos = 0) const;
  158. int Find(LPCTSTR pstr, int iPos = 0) const;
  159. int ReverseFind(TCHAR ch) const;
  160. int Replace(LPCTSTR pstrFrom, LPCTSTR pstrTo);
  161. int __cdecl Format(LPCTSTR pstrFormat, ...);
  162. int __cdecl SmallFormat(LPCTSTR pstrFormat, ...);
  163. protected:
  164. int __cdecl InnerFormat(LPCTSTR pstrFormat, va_list Args);
  165. protected:
  166. LPTSTR m_pstr;
  167. TCHAR m_szBuffer[MAX_LOCAL_STRING_LEN + 1];
  168. };
  169. static std::vector<CDuiString> StrSplit(CDuiString text, CDuiString sp)
  170. {
  171. std::vector<CDuiString> vResults;
  172. int pos = text.Find(sp, 0);
  173. while (pos >= 0)
  174. {
  175. CDuiString t = text.Left(pos);
  176. vResults.push_back(t);
  177. text = text.Right(text.GetLength() - pos - sp.GetLength());
  178. pos = text.Find(sp);
  179. }
  180. vResults.push_back(text);
  181. return vResults;
  182. }
  183. /////////////////////////////////////////////////////////////////////////////////////
  184. //
  185. struct TITEM
  186. {
  187. CDuiString Key;
  188. LPVOID Data;
  189. struct TITEM* pPrev;
  190. struct TITEM* pNext;
  191. };
  192. class UILIB_API CStdStringPtrMap
  193. {
  194. public:
  195. CStdStringPtrMap(int nSize = 83);
  196. ~CStdStringPtrMap();
  197. void Resize(int nSize = 83);
  198. LPVOID Find(LPCTSTR key, bool optimize = true) const;
  199. bool Insert(LPCTSTR key, LPVOID pData);
  200. LPVOID Set(LPCTSTR key, LPVOID pData);
  201. bool Remove(LPCTSTR key);
  202. void RemoveAll();
  203. int GetSize() const;
  204. LPCTSTR GetAt(int iIndex) const;
  205. LPCTSTR operator[] (int nIndex) const;
  206. protected:
  207. TITEM** m_aT;
  208. int m_nBuckets;
  209. int m_nCount;
  210. };
  211. /////////////////////////////////////////////////////////////////////////////////////
  212. //
  213. class UILIB_API CWaitCursor
  214. {
  215. public:
  216. CWaitCursor();
  217. ~CWaitCursor();
  218. protected:
  219. HCURSOR m_hOrigCursor;
  220. };
  221. /////////////////////////////////////////////////////////////////////////////////////
  222. //
  223. class CDuiVariant : public VARIANT
  224. {
  225. public:
  226. CDuiVariant()
  227. {
  228. VariantInit(this);
  229. }
  230. CDuiVariant(int i)
  231. {
  232. VariantInit(this);
  233. this->vt = VT_I4;
  234. this->intVal = i;
  235. }
  236. CDuiVariant(float f)
  237. {
  238. VariantInit(this);
  239. this->vt = VT_R4;
  240. this->fltVal = f;
  241. }
  242. CDuiVariant(LPOLESTR s)
  243. {
  244. VariantInit(this);
  245. this->vt = VT_BSTR;
  246. this->bstrVal = s;
  247. }
  248. CDuiVariant(IDispatch *disp)
  249. {
  250. VariantInit(this);
  251. this->vt = VT_DISPATCH;
  252. this->pdispVal = disp;
  253. }
  254. ~CDuiVariant()
  255. {
  256. VariantClear(this);
  257. }
  258. };
  259. //////////////////////////////////////////////////////////////////////////////////////
  260. //
  261. static char* w2a(wchar_t* lpszSrc, UINT CodePage = CP_ACP)
  262. {
  263. if (lpszSrc != NULL)
  264. {
  265. int nANSILen = WideCharToMultiByte(CodePage, 0, lpszSrc, -1, NULL, 0, NULL, NULL);
  266. char* pANSI = new char[nANSILen + 1];
  267. if (pANSI != NULL)
  268. {
  269. ZeroMemory(pANSI, nANSILen + 1);
  270. WideCharToMultiByte(CodePage, 0, lpszSrc, -1, pANSI, nANSILen, NULL, NULL);
  271. return pANSI;
  272. }
  273. }
  274. return NULL;
  275. }
  276. static wchar_t* a2w(char* lpszSrc, UINT CodePage = CP_ACP)
  277. {
  278. if (lpszSrc != NULL)
  279. {
  280. int nUnicodeLen = MultiByteToWideChar(CodePage, 0, lpszSrc, -1, NULL, 0);
  281. LPWSTR pUnicode = new WCHAR[nUnicodeLen + 1];
  282. if (pUnicode != NULL)
  283. {
  284. ZeroMemory((void*)pUnicode, (nUnicodeLen + 1) * sizeof(WCHAR));
  285. MultiByteToWideChar(CodePage, 0, lpszSrc,-1, pUnicode, nUnicodeLen);
  286. return pUnicode;
  287. }
  288. }
  289. return NULL;
  290. }
  291. ///////////////////////////////////////////////////////////////////////////////////////
  292. ////
  293. //struct TImageInfo;
  294. //class CPaintManagerUI;
  295. //class UILIB_API CImageString
  296. //{
  297. //public:
  298. // CImageString();
  299. // CImageString(const CImageString&);
  300. // const CImageString& operator=(const CImageString&);
  301. // virtual ~CImageString();
  302. // const CDuiString& GetAttributeString() const;
  303. // void SetAttributeString(LPCTSTR pStrImageAttri);
  304. // void ModifyAttribute(LPCTSTR pStrModify);
  305. // bool LoadImage(CPaintManagerUI* pManager);
  306. // bool IsLoadSuccess();
  307. // RECT GetDest() const;
  308. // void SetDest(const RECT &rcDest);
  309. // const TImageInfo* GetImageInfo() const;
  310. //private:
  311. // void Clone(const CImageString&);
  312. // void Clear();
  313. // void ParseAttribute(LPCTSTR pStrImageAttri);
  314. //protected:
  315. // friend class CRenderEngine;
  316. // CDuiString m_sImageAttribute;
  317. // CDuiString m_sImage;
  318. // CDuiString m_sResType;
  319. // TImageInfo *m_imageInfo;
  320. // bool m_bLoadSuccess;
  321. // RECT m_rcDest;
  322. // RECT m_rcSource;
  323. // RECT m_rcCorner;
  324. // BYTE m_bFade;
  325. // DWORD m_dwMask;
  326. // bool m_bHole;
  327. // bool m_bTiledX;
  328. // bool m_bTiledY;
  329. //};
  330. }// namespace DuiLib
  331. #endif // __UTILS_H__