Team Fortress 2 Source Code as on 22/4/2020
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.

438 lines
7.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef RICHEDITCTRLEX_H
  8. #define RICHEDITCTRLEX_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #pragma warning(push, 1)
  13. #include <list>
  14. #include <stack>
  15. #pragma warning(pop)
  16. using namespace std;
  17. #pragma warning(disable : 4786)
  18. #ifdef RICHED_IMPL
  19. #ifdef _AFXEXT
  20. #define RICHED_DECL __declspec(dllexport)
  21. #else
  22. #define RICHED_DECL
  23. #endif
  24. #else
  25. #ifdef _AFXEXT
  26. #define RICHED_DECL __declspec(dllimport)
  27. #else
  28. #define RICHED_DECL
  29. #endif//_AFXEXT
  30. #endif //RICHED_IMPL
  31. class CRTFBuilder;
  32. class CStringManip;
  33. class CIntManip;
  34. typedef CRTFBuilder &(*RTFSM_PFUNC) (CRTFBuilder &);
  35. typedef CRTFBuilder &(*RTFSM_STRINGPFUNC) (CRTFBuilder &, CString &);
  36. typedef CRTFBuilder &(*RTFSM_INTPFUNC) (CRTFBuilder &, int);
  37. typedef CRTFBuilder &(*RTFSM_BOOLPFUNC) (CRTFBuilder &, bool);
  38. typedef CRTFBuilder &(*RTFSM_CONTROLPFUNC) (CRTFBuilder &, CRichEditCtrl &);
  39. class CBoolString
  40. {
  41. private:
  42. bool m_b;
  43. CString m_strOn;
  44. CString m_strOff;
  45. public:
  46. CBoolString(CString strOn, CString strOff = "")
  47. {
  48. m_strOn = strOn;
  49. m_strOff = strOff;
  50. m_b = false;
  51. }
  52. void operator=(bool b)
  53. {
  54. m_b = b;
  55. }
  56. operator CString() const
  57. {
  58. return m_b ? m_strOn : m_strOff;
  59. }
  60. };
  61. class CTextAttributes
  62. {
  63. protected:
  64. int m_nFontSize;
  65. CBoolString m_bsBold;
  66. CBoolString m_bsUnderline;
  67. CBoolString m_bsItalic;
  68. CBoolString m_bsStrike;
  69. int m_nFontNumber;
  70. int m_nColorFground;
  71. int m_nColorBground;
  72. public:
  73. CTextAttributes()
  74. : m_bsBold("\\b") ,
  75. m_bsUnderline("\\ul"),
  76. m_bsItalic("\\i"),
  77. m_bsStrike("\\strike")
  78. {
  79. m_nColorBground = m_nColorFground = m_nFontNumber = m_nFontSize = 0;
  80. m_bsBold = false;
  81. }
  82. operator CString() const
  83. {
  84. CString s;
  85. s.Format("\\plain%s%s%s%s\\f%d\\fs%d\\cb%d\\cf%d ",
  86. ((CString)m_bsBold).GetBuffer(),
  87. ((CString)m_bsUnderline).GetBuffer(), ((CString)m_bsItalic).GetBuffer(), ((CString)m_bsStrike).GetBuffer(),
  88. m_nFontNumber ,
  89. m_nFontSize ,
  90. m_nColorBground,
  91. m_nColorFground);
  92. return s;
  93. }
  94. friend class CRTFBuilder;
  95. };
  96. class CFontList : public list<CString>
  97. {
  98. public:
  99. operator CString() const
  100. {
  101. CString s;
  102. s = "{\\fonttbl";
  103. int nCount = 0;
  104. for (const_iterator i = begin(); i!=end(); i++)
  105. {
  106. CString s2;
  107. s2.Format("{\\f%d %s;}", nCount++, (const char*)(*i));
  108. s+=s2;
  109. }
  110. s+='}';
  111. return s;
  112. }
  113. void add(const CString &s)
  114. {
  115. push_back(s);
  116. }
  117. };
  118. class CColorList : public list<COLORREF>
  119. {
  120. public:
  121. int add(COLORREF c)
  122. {
  123. push_back(c);
  124. return size() - 1;
  125. }
  126. int find(COLORREF c)
  127. {
  128. int n = 0;
  129. for (iterator i = begin(); i != end(); i++, n++)
  130. {
  131. COLORREF cComp(*i);
  132. if (cComp == c)
  133. {
  134. return n;
  135. }
  136. }
  137. return -1;
  138. }
  139. operator CString() const
  140. {
  141. CString s("{\\colortbl");
  142. for (const_iterator i = begin(); i != end(); i++)
  143. {
  144. COLORREF c(*i);
  145. int r((c & 0x000000ff));
  146. int g((c >> 8) & 0x000000ff);
  147. int b((c >> 16) & 0x000000ff);
  148. CString s2;
  149. s2.Format("\\red%d\\green%d\\blue%d;", r, g, b);
  150. s += s2;
  151. }
  152. s += '}';
  153. return s;
  154. }
  155. };
  156. class RICHED_DECL CManip
  157. {
  158. protected:
  159. CString m_strVal;
  160. int m_nVal;
  161. LPVOID m_pFunc;
  162. bool m_bVal;
  163. public:
  164. virtual CRTFBuilder &go(CRTFBuilder &) = 0;
  165. CManip()
  166. {
  167. m_pFunc = NULL;
  168. m_nVal = 0;
  169. m_strVal = "";
  170. }
  171. CManip(LPVOID p, CString s)
  172. {
  173. m_pFunc = p;
  174. m_strVal = s;
  175. }
  176. CManip(LPVOID p, int n)
  177. {
  178. m_pFunc = p;
  179. m_nVal = n;
  180. }
  181. CManip(LPVOID p, bool b)
  182. {
  183. m_pFunc = p;
  184. m_bVal = b;
  185. }
  186. };
  187. class RICHED_DECL CStringManip : public CManip
  188. {
  189. public:
  190. CStringManip(RTFSM_STRINGPFUNC p, CString s = "") : CManip ((LPVOID)p, s) {};
  191. CRTFBuilder &go(CRTFBuilder &b)
  192. {
  193. return((RTFSM_STRINGPFUNC)m_pFunc) (b, m_strVal);
  194. }
  195. };
  196. class RICHED_DECL CControlManip : public CManip
  197. {
  198. protected:
  199. CRichEditCtrl &m_control;
  200. public:
  201. CControlManip(RTFSM_CONTROLPFUNC p, CRichEditCtrl& c) : m_control(c), CManip((LPVOID)p, (CString)"") {};
  202. CRTFBuilder &go(CRTFBuilder &b)
  203. {
  204. return((RTFSM_CONTROLPFUNC)m_pFunc)(b, m_control);
  205. }
  206. };
  207. class RICHED_DECL CIntManip : public CManip
  208. {
  209. public:
  210. CIntManip(RTFSM_INTPFUNC p, int n = 0) : CManip ((LPVOID)p, n) {};
  211. CRTFBuilder &go(CRTFBuilder &b)
  212. {
  213. return((RTFSM_INTPFUNC)m_pFunc)(b, m_nVal);
  214. }
  215. };
  216. class RICHED_DECL CBoolManip : public CManip
  217. {
  218. public:
  219. CBoolManip(RTFSM_BOOLPFUNC p, bool b) : CManip((LPVOID)p, b) {};
  220. CRTFBuilder &go(CRTFBuilder &b)
  221. {
  222. return ((RTFSM_BOOLPFUNC ) m_pFunc)( b, m_bVal);
  223. }
  224. };
  225. class RICHED_DECL CRTFBuilder
  226. {
  227. protected:
  228. CString m_string;
  229. CTextAttributes m_attr;
  230. CFontList m_fontList;
  231. CColorList m_colorList;
  232. stack<CTextAttributes> m_attrStack;
  233. public:
  234. void bold (bool b = true);
  235. void strike(bool b = true);
  236. void italic(bool b = true);
  237. void underline(bool b = true);
  238. void normal();
  239. void size(int n);
  240. void font(const CString &i);
  241. void black();
  242. void blue();
  243. void green();
  244. void red();
  245. void color(COLORREF);
  246. void backColor(COLORREF);
  247. void push();
  248. void pop();
  249. CRTFBuilder &operator+=(CString &s);
  250. CRTFBuilder();
  251. virtual ~CRTFBuilder();
  252. void addFont(const CString &s)
  253. {
  254. m_fontList.add(s);
  255. }
  256. void addColor(COLORREF c)
  257. {
  258. m_colorList.add(c);
  259. }
  260. CRTFBuilder & operator+=(LPCTSTR p);
  261. operator CString() const
  262. {
  263. return m_string;
  264. }
  265. void write(CRichEditCtrl &);
  266. int colorCount() const
  267. {
  268. return m_colorList.size();
  269. }
  270. public:
  271. CRTFBuilder & operator<<(LPCTSTR);
  272. CRTFBuilder & operator<<(int);
  273. CRTFBuilder & operator>>(CRichEditCtrl &);
  274. friend RICHED_DECL CRTFBuilder &normal(CRTFBuilder &);
  275. friend RICHED_DECL CRTFBuilder &push(CRTFBuilder&);
  276. friend RICHED_DECL CRTFBuilder &pop(CRTFBuilder &);
  277. friend RICHED_DECL CRTFBuilder &black(CRTFBuilder &);
  278. friend RICHED_DECL CRTFBuilder &red(CRTFBuilder &);
  279. friend RICHED_DECL CRTFBuilder &green(CRTFBuilder &);
  280. friend RICHED_DECL CRTFBuilder &blue(CRTFBuilder &);
  281. friend RICHED_DECL CRTFBuilder &bold(CRTFBuilder &);
  282. friend RICHED_DECL CRTFBuilder &strike(CRTFBuilder &);
  283. friend RICHED_DECL CRTFBuilder &italic(CRTFBuilder &);
  284. friend RICHED_DECL CRTFBuilder &underline(CRTFBuilder &);
  285. };
  286. RICHED_DECL CControlManip write (CRichEditCtrl &);
  287. RICHED_DECL CIntManip normal (int = 0);
  288. RICHED_DECL CIntManip push (int = 0);
  289. RICHED_DECL CIntManip pop (int = 0);
  290. RICHED_DECL CIntManip size (int);
  291. RICHED_DECL CIntManip color (int);
  292. RICHED_DECL CIntManip backColor (int);
  293. RICHED_DECL CIntManip addColor (int);
  294. RICHED_DECL CIntManip font (int);
  295. RICHED_DECL CStringManip font (LPCTSTR);
  296. RICHED_DECL CStringManip addFont (LPCTSTR);
  297. RICHED_DECL CBoolManip bold (bool);
  298. RICHED_DECL CBoolManip strike (bool);
  299. RICHED_DECL CBoolManip italic (bool);
  300. RICHED_DECL CBoolManip underline (bool);
  301. RICHED_DECL CRTFBuilder & operator<<(CRTFBuilder &, RTFSM_PFUNC);
  302. RICHED_DECL CRTFBuilder & operator<<(CRTFBuilder &, CManip & m);
  303. class RICHED_DECL CRichEditCtrlEx : public CRichEditCtrl
  304. {
  305. public:
  306. // Construction
  307. CRichEditCtrlEx();
  308. public:
  309. void enable(bool b = true)
  310. {
  311. ModifyStyle(b ? WS_DISABLED : 0, b ? 0 : WS_DISABLED, 0);
  312. }
  313. void disable(bool b = true)
  314. {
  315. enable(!b);
  316. }
  317. void readOnly(bool b = true)
  318. {
  319. SetReadOnly(b);
  320. }
  321. void writable(bool b = true)
  322. {
  323. readOnly(!b);
  324. }
  325. // ClassWizard generated virtual function overrides
  326. //{{AFX_VIRTUAL(CRichEditCtrlEx)
  327. protected:
  328. virtual void PreSubclassWindow();
  329. //}}AFX_VIRTUAL
  330. public:
  331. virtual ~CRichEditCtrlEx();
  332. protected:
  333. //{{AFX_MSG(CRichEditCtrlEx)
  334. //}}AFX_MSG
  335. DECLARE_MESSAGE_MAP()
  336. };
  337. #endif // RICHEDITCTRLEX_H