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.

278 lines
5.5 KiB

  1. //-----------------------------------------------------------------------------
  2. // File: useful.h
  3. //
  4. // Desc: Contains various utility classes and functions to help the
  5. // UI carry its operations more easily.
  6. //
  7. // Copyright (C) 1999-2000 Microsoft Corporation. All Rights Reserved.
  8. //-----------------------------------------------------------------------------
  9. #ifndef __USEFUL_H__
  10. #define __USEFUL_H__
  11. class utilstr
  12. {
  13. public:
  14. utilstr() : m_str(NULL), m_len(0) {}
  15. ~utilstr() {Empty();}
  16. operator LPCTSTR () const {return m_str;}
  17. LPCTSTR Get() const {return m_str;}
  18. const utilstr & operator = (const utilstr &s) {equal(s); return *this;}
  19. const utilstr & operator = (LPCTSTR s) {equal(s); return *this;}
  20. const utilstr & operator += (const utilstr &s) {add(s); return *this;}
  21. const utilstr & operator += (LPCTSTR s) {add(s); return *this;}
  22. LPTSTR Eject();
  23. void Empty();
  24. bool IsEmpty() const;
  25. int GetLength() const;
  26. void Format(LPCTSTR format, ...);
  27. private:
  28. LPTSTR m_str;
  29. int m_len;
  30. void equal(LPCTSTR str);
  31. void add(LPCTSTR str);
  32. };
  33. template <class T>
  34. struct rgref {
  35. rgref(T *p) : pt(p) {}
  36. T &operator [] (int i) {return pt[i];}
  37. const T &operator [] (int i) const {return pt[i];}
  38. private:
  39. T *pt;
  40. };
  41. struct SPOINT {
  42. SPOINT() :
  43. #define SPOINT_INITIALIZERS \
  44. p(u.p), \
  45. s(u.s), \
  46. a(((int *)(void *)u.a)), \
  47. x(u.p.x), \
  48. y(u.p.y), \
  49. cx(u.s.cx), \
  50. cy(u.s.cy)
  51. SPOINT_INITIALIZERS
  52. {x = y = 0;}
  53. SPOINT(int, POINT *r) :
  54. p(*r),
  55. s(*((SIZE *)(void *)r)),
  56. a(((int *)(void *)r)),
  57. x(r->x),
  58. y(r->y),
  59. cx(r->x),
  60. cy(r->y)
  61. {}
  62. SPOINT(const SPOINT &sp) :
  63. SPOINT_INITIALIZERS
  64. {p = sp.p;}
  65. SPOINT(int b, int c) :
  66. SPOINT_INITIALIZERS
  67. {x = b; y = c;}
  68. SPOINT(const POINT &point) :
  69. SPOINT_INITIALIZERS
  70. {p = point;}
  71. SPOINT(const SIZE &size) :
  72. SPOINT_INITIALIZERS
  73. {s = size;}
  74. #undef SPOINT_INITIALIZERS
  75. SPOINT operator = (const SPOINT &sp) {p = sp.p; return *this;}
  76. SPOINT operator = (const POINT &_p) {p = _p; return *this;}
  77. SPOINT operator = (const SIZE &_s) {s = _s; return *this;}
  78. operator POINT () const {return p;}
  79. operator SIZE () const {return s;}
  80. long &x, &y, &cx, &cy;
  81. POINT &p;
  82. SIZE &s;
  83. rgref<int> a;
  84. private:
  85. union {
  86. POINT p;
  87. SIZE s;
  88. int a[2];
  89. } u;
  90. };
  91. struct SRECT {
  92. SRECT() :
  93. #define SRECT_INITIALIZERS \
  94. a(((int *)(void *)u.a)), \
  95. r(u.r), \
  96. left(u.r.left), \
  97. top(u.r.top), \
  98. right(u.r.right), \
  99. bottom(u.r.bottom), \
  100. ul(0, &u.p.ul), \
  101. lr(0, &u.p.lr)
  102. SRECT_INITIALIZERS
  103. {RECT z = {0,0,0,0}; u.r = z;}
  104. SRECT(const SRECT &sr) :
  105. SRECT_INITIALIZERS
  106. {u.r = sr.r;}
  107. SRECT(int c, int d, int e, int f) :
  108. SRECT_INITIALIZERS
  109. {RECT z = {c,d,e,f}; u.r = z;}
  110. SRECT(const RECT &_r) :
  111. SRECT_INITIALIZERS
  112. {u.r = _r;}
  113. #undef SRECT_INITIALIZERS
  114. SRECT operator = (const SRECT &sr) {u.r = sr.r; return *this;}
  115. SRECT operator = (const RECT &_r) {u.r = _r; return *this;}
  116. operator RECT () const {return r;}
  117. long &left, &top, &right, &bottom;
  118. rgref<int> a;
  119. RECT &r;
  120. SPOINT ul, lr;
  121. private:
  122. union {
  123. int a[4];
  124. RECT r;
  125. struct {
  126. POINT ul, lr;
  127. } p;
  128. } u;
  129. };
  130. int ConvertVal(int x, int a1, int a2, int b1, int b2);
  131. double dConvertVal(double x, double a1, double a2, double b1, double b2);
  132. SIZE GetTextSize(LPCTSTR tszText, HFONT hFont = NULL);
  133. int GetTextHeight(HFONT hFont);
  134. SIZE GetRectSize(const RECT &rect);
  135. int FormattedMsgBox(HINSTANCE, HWND, UINT, UINT, UINT, ...);
  136. int FormattedErrorBox(HINSTANCE, HWND, UINT, UINT, ...);
  137. int FormattedLastErrorBox(HINSTANCE, HWND, UINT, UINT, DWORD);
  138. BOOL UserConfirm(HINSTANCE, HWND, UINT, UINT, ...);
  139. struct AFS_FLAG {DWORD value; LPCTSTR name;};
  140. LPTSTR AllocFlagStr(DWORD value, const AFS_FLAG flag[], DWORD flags);
  141. LPTSTR AllocFileNameNoPath(LPTSTR path);
  142. LPTSTR AllocLPTSTR(LPCWSTR wstr);
  143. LPTSTR AllocLPTSTR(LPCSTR cstr);
  144. LPWSTR AllocLPWSTR(LPCWSTR wstr);
  145. LPWSTR AllocLPWSTR(LPCSTR str);
  146. LPSTR AllocLPSTR(LPCWSTR wstr);
  147. LPSTR AllocLPSTR(LPCSTR str);
  148. void CopyStr(LPWSTR dest, LPCWSTR src, size_t max);
  149. void CopyStr(LPSTR dest, LPCSTR src, size_t max);
  150. void CopyStr(LPWSTR dest, LPCSTR src, size_t max);
  151. void CopyStr(LPSTR dest, LPCWSTR src, size_t max);
  152. void PolyLineArrowShadow(HDC hDC, POINT *p, int i);
  153. void PolyLineArrow(HDC hDC, POINT *, int, BOOL bDoShadow = FALSE);
  154. BOOL bEq(BOOL a, BOOL b);
  155. void DrawArrow(HDC hDC, const RECT &rect, BOOL bVert, BOOL bUpLeft);
  156. BOOL ScreenToClient(HWND hWnd, LPRECT rect);
  157. BOOL ClientToScreen(HWND hWnd, LPRECT rect);
  158. int StrLen(LPCWSTR s);
  159. int StrLen(LPCSTR s);
  160. //@@BEGIN_MSINTERNAL
  161. #ifdef DDKBUILD
  162. LPCTSTR GetOpenFileName(HINSTANCE hInst, HWND hWnd, LPCTSTR title, LPCTSTR filter, LPCTSTR defext, LPCTSTR inidir = NULL);
  163. #endif
  164. //@@END_MSINTERNAL
  165. template<class T>
  166. int GetSuperStringByteSize(const T *str)
  167. {
  168. for (int i = 0;; i++)
  169. if (!str[i] && !str[i + 1])
  170. return (i + 2) * sizeof(T);
  171. }
  172. template<class T>
  173. T *DupSuperString(const T *str)
  174. {
  175. int s = GetSuperStringByteSize(str);
  176. T *ret = (T *)malloc(s);
  177. if (ret != NULL)
  178. {
  179. CopyMemory((void *)ret, (const void *)str, (DWORD)s);
  180. }
  181. return ret;
  182. }
  183. template<class T>
  184. int CountSubStrings(const T *str)
  185. {
  186. int n = 0;
  187. while (1)
  188. {
  189. str += StrLen(str) + 1;
  190. n++;
  191. if (!*str)
  192. return n;
  193. }
  194. }
  195. template<class T>
  196. const T *GetSubString(const T *str, int i)
  197. {
  198. int n = 0;
  199. while (1)
  200. {
  201. if (n == i)
  202. return str;
  203. str += StrLen(str) + 1;
  204. n++;
  205. if (!*str)
  206. return NULL;
  207. }
  208. }
  209. template<class T>
  210. class SetOnFunctionExit
  211. {
  212. public:
  213. SetOnFunctionExit(T &var, T value) : m_var(var), m_value(value) {}
  214. ~SetOnFunctionExit() {m_var = m_value;}
  215. private:
  216. T &m_var;
  217. T m_value;
  218. };
  219. #endif //__USEFUL_H__