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.

437 lines
11 KiB

  1. //
  2. // dap.cpp
  3. //
  4. // ITfDisplayAttributeProvider implementation.
  5. //
  6. #include "globals.h"
  7. #include "mark.h"
  8. static const TCHAR c_szAttributeInfoKey[] = TEXT("Software\\Mark Text Service");
  9. static const TCHAR c_szAttributeInfoValueName[] = TEXT("DisplayAttr");
  10. // this text service has only a single display attribute so we'll use
  11. // a single static object.
  12. class CDisplayAttributeInfo : public ITfDisplayAttributeInfo
  13. {
  14. public:
  15. // IUnknown
  16. STDMETHODIMP QueryInterface(REFIID riid, void **ppvObj);
  17. STDMETHODIMP_(ULONG) AddRef(void)
  18. {
  19. return DllAddRef();
  20. }
  21. STDMETHODIMP_(ULONG) Release(void)
  22. {
  23. return DllRelease();
  24. }
  25. // ITfDisplayAttributeInfo
  26. STDMETHODIMP GetGUID(GUID *pguid);
  27. STDMETHODIMP GetDescription(BSTR *pbstrDesc);
  28. STDMETHODIMP GetAttributeInfo(TF_DISPLAYATTRIBUTE *ptfDisplayAttr);
  29. STDMETHODIMP SetAttributeInfo(const TF_DISPLAYATTRIBUTE *ptfDisplayAttr);
  30. STDMETHODIMP Reset();
  31. private:
  32. static const TF_DISPLAYATTRIBUTE _c_DefaultDisplayAttribute;
  33. }
  34. g_DisplayAttributeInfo;
  35. const TF_DISPLAYATTRIBUTE CDisplayAttributeInfo::_c_DefaultDisplayAttribute =
  36. {
  37. { TF_CT_COLORREF, RGB(255, 0, 0) }, // text color
  38. { TF_CT_NONE, 0 }, // background color (TF_CT_NONE => app default)
  39. TF_LS_SOLID, // underline style
  40. FALSE, // underline boldness
  41. { TF_CT_COLORREF, RGB(255, 0, 0) }, // underline color
  42. TF_ATTR_INPUT // attribute info
  43. };
  44. //+---------------------------------------------------------------------------
  45. //
  46. // QueryInterface
  47. //
  48. //----------------------------------------------------------------------------
  49. STDAPI CDisplayAttributeInfo::QueryInterface(REFIID riid, void **ppvObj)
  50. {
  51. if (ppvObj == NULL)
  52. return E_INVALIDARG;
  53. *ppvObj = NULL;
  54. if (IsEqualIID(riid, IID_IUnknown) ||
  55. IsEqualIID(riid, IID_ITfDisplayAttributeInfo))
  56. {
  57. *ppvObj = (ITfDisplayAttributeInfo *)this;
  58. }
  59. if (*ppvObj)
  60. {
  61. AddRef();
  62. return S_OK;
  63. }
  64. return E_NOINTERFACE;
  65. }
  66. //+---------------------------------------------------------------------------
  67. //
  68. // GetGUID
  69. //
  70. //----------------------------------------------------------------------------
  71. STDAPI CDisplayAttributeInfo::GetGUID(GUID *pguid)
  72. {
  73. if (pguid == NULL)
  74. return E_INVALIDARG;
  75. *pguid = c_guidMarkDisplayAttribute;
  76. return S_OK;
  77. }
  78. //+---------------------------------------------------------------------------
  79. //
  80. // GetDescription
  81. //
  82. //----------------------------------------------------------------------------
  83. STDAPI CDisplayAttributeInfo::GetDescription(BSTR *pbstrDesc)
  84. {
  85. BSTR bstrDesc;
  86. if (pbstrDesc == NULL)
  87. return E_INVALIDARG;
  88. *pbstrDesc = NULL;
  89. if ((bstrDesc = SysAllocString(L"Mark Display Attribute")) == NULL)
  90. return E_OUTOFMEMORY;
  91. *pbstrDesc = bstrDesc;
  92. return S_OK;
  93. }
  94. //+---------------------------------------------------------------------------
  95. //
  96. // GetAttributeInfo
  97. //
  98. //----------------------------------------------------------------------------
  99. STDAPI CDisplayAttributeInfo::GetAttributeInfo(TF_DISPLAYATTRIBUTE *ptfDisplayAttr)
  100. {
  101. HKEY hKeyAttributeInfo;
  102. LONG lResult;
  103. DWORD cbData;
  104. if (ptfDisplayAttr == NULL)
  105. return E_INVALIDARG;
  106. lResult = E_FAIL;
  107. if (RegOpenKeyEx(HKEY_CURRENT_USER, c_szAttributeInfoKey, 0, KEY_READ, &hKeyAttributeInfo) == ERROR_SUCCESS)
  108. {
  109. cbData = sizeof(*ptfDisplayAttr);
  110. lResult = RegQueryValueEx(hKeyAttributeInfo, c_szAttributeInfoValueName,
  111. NULL, NULL,
  112. (LPBYTE)ptfDisplayAttr, &cbData);
  113. RegCloseKey(hKeyAttributeInfo);
  114. }
  115. if (lResult != ERROR_SUCCESS || cbData != sizeof(*ptfDisplayAttr))
  116. {
  117. // go with the defaults
  118. *ptfDisplayAttr = _c_DefaultDisplayAttribute;
  119. }
  120. return S_OK;
  121. }
  122. //+---------------------------------------------------------------------------
  123. //
  124. // SetAttributeInfo
  125. //
  126. //----------------------------------------------------------------------------
  127. STDAPI CDisplayAttributeInfo::SetAttributeInfo(const TF_DISPLAYATTRIBUTE *ptfDisplayAttr)
  128. {
  129. HKEY hKeyAttributeInfo;
  130. LONG lResult;
  131. lResult = RegCreateKeyEx(HKEY_CURRENT_USER, c_szAttributeInfoKey, 0, TEXT(""),
  132. REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
  133. &hKeyAttributeInfo, NULL);
  134. if (lResult != ERROR_SUCCESS)
  135. return E_FAIL;
  136. lResult = RegSetValueEx(hKeyAttributeInfo, c_szAttributeInfoValueName,
  137. 0, REG_BINARY, (const BYTE *)ptfDisplayAttr,
  138. sizeof(*ptfDisplayAttr));
  139. RegCloseKey(hKeyAttributeInfo);
  140. return (lResult == ERROR_SUCCESS) ? S_OK : E_FAIL;
  141. }
  142. //+---------------------------------------------------------------------------
  143. //
  144. // Reset
  145. //
  146. //----------------------------------------------------------------------------
  147. STDAPI CDisplayAttributeInfo::Reset()
  148. {
  149. return SetAttributeInfo(&_c_DefaultDisplayAttribute);
  150. }
  151. class CEnumDisplayAttributeInfo : public IEnumTfDisplayAttributeInfo
  152. {
  153. public:
  154. CEnumDisplayAttributeInfo();
  155. ~CEnumDisplayAttributeInfo();
  156. // IUnknown
  157. STDMETHODIMP QueryInterface(REFIID riid, void **ppvObj);
  158. STDMETHODIMP_(ULONG) AddRef(void);
  159. STDMETHODIMP_(ULONG) Release(void);
  160. // IEnumTfDisplayAttributeInfo
  161. STDMETHODIMP Clone(IEnumTfDisplayAttributeInfo **ppEnum);
  162. STDMETHODIMP Next(ULONG ulCount, ITfDisplayAttributeInfo **rgInfo, ULONG *pcFetched);
  163. STDMETHODIMP Reset();
  164. STDMETHODIMP Skip(ULONG ulCount);
  165. private:
  166. LONG _iIndex; // next display attribute to enum
  167. LONG _cRef; // COM ref count
  168. };
  169. //+---------------------------------------------------------------------------
  170. //
  171. // ctor
  172. //
  173. //----------------------------------------------------------------------------
  174. CEnumDisplayAttributeInfo::CEnumDisplayAttributeInfo()
  175. {
  176. DllAddRef();
  177. _iIndex = 0;
  178. _cRef = 1;
  179. }
  180. //+---------------------------------------------------------------------------
  181. //
  182. // dtor
  183. //
  184. //----------------------------------------------------------------------------
  185. CEnumDisplayAttributeInfo::~CEnumDisplayAttributeInfo()
  186. {
  187. DllRelease();
  188. }
  189. //+---------------------------------------------------------------------------
  190. //
  191. // QueryInterface
  192. //
  193. //----------------------------------------------------------------------------
  194. STDAPI CEnumDisplayAttributeInfo::QueryInterface(REFIID riid, void **ppvObj)
  195. {
  196. if (ppvObj == NULL)
  197. return E_INVALIDARG;
  198. *ppvObj = NULL;
  199. if (IsEqualIID(riid, IID_IUnknown) ||
  200. IsEqualIID(riid, IID_IEnumTfDisplayAttributeInfo))
  201. {
  202. *ppvObj = (IEnumTfDisplayAttributeInfo *)this;
  203. }
  204. if (*ppvObj)
  205. {
  206. AddRef();
  207. return S_OK;
  208. }
  209. return E_NOINTERFACE;
  210. }
  211. //+---------------------------------------------------------------------------
  212. //
  213. // AddRef
  214. //
  215. //----------------------------------------------------------------------------
  216. STDAPI_(ULONG) CEnumDisplayAttributeInfo::AddRef()
  217. {
  218. return ++_cRef;
  219. }
  220. //+---------------------------------------------------------------------------
  221. //
  222. // Release
  223. //
  224. //----------------------------------------------------------------------------
  225. STDAPI_(ULONG) CEnumDisplayAttributeInfo::Release()
  226. {
  227. LONG cr = --_cRef;
  228. assert(_cRef >= 0);
  229. if (_cRef == 0)
  230. {
  231. delete this;
  232. }
  233. return cr;
  234. }
  235. //+---------------------------------------------------------------------------
  236. //
  237. // Clone
  238. //
  239. // Returns a copy of the object.
  240. //----------------------------------------------------------------------------
  241. STDAPI CEnumDisplayAttributeInfo::Clone(IEnumTfDisplayAttributeInfo **ppEnum)
  242. {
  243. CEnumDisplayAttributeInfo *pClone;
  244. if (ppEnum == NULL)
  245. return E_INVALIDARG;
  246. *ppEnum = NULL;
  247. if ((pClone = new CEnumDisplayAttributeInfo) == NULL)
  248. return E_OUTOFMEMORY;
  249. // the clone should match this object's state
  250. pClone->_iIndex = _iIndex;
  251. *ppEnum = pClone;
  252. return S_OK;
  253. }
  254. //+---------------------------------------------------------------------------
  255. //
  256. // Next
  257. //
  258. // Returns an array of display attribute info objects supported by this service.
  259. //----------------------------------------------------------------------------
  260. STDAPI CEnumDisplayAttributeInfo::Next(ULONG ulCount, ITfDisplayAttributeInfo **rgInfo, ULONG *pcFetched)
  261. {
  262. ULONG cFetched;
  263. if (pcFetched == NULL)
  264. {
  265. // technically this is only legal if ulCount == 1, but we won't check
  266. pcFetched = &cFetched;
  267. }
  268. *pcFetched = 0;
  269. if (ulCount == 0)
  270. return S_OK;
  271. // we only have a single display attribute to enum, so this is trivial
  272. if (_iIndex == 0)
  273. {
  274. *rgInfo = &g_DisplayAttributeInfo;
  275. (*rgInfo)->AddRef();
  276. *pcFetched = 1;
  277. _iIndex++;
  278. }
  279. return (*pcFetched == ulCount) ? S_OK : S_FALSE;
  280. }
  281. //+---------------------------------------------------------------------------
  282. //
  283. // Reset
  284. //
  285. // Resets the enumeration.
  286. //----------------------------------------------------------------------------
  287. STDAPI CEnumDisplayAttributeInfo::Reset()
  288. {
  289. _iIndex = 0;
  290. return S_OK;
  291. }
  292. //+---------------------------------------------------------------------------
  293. //
  294. // Skip
  295. //
  296. // Skips past objects in the enumeration.
  297. //----------------------------------------------------------------------------
  298. STDAPI CEnumDisplayAttributeInfo::Skip(ULONG ulCount)
  299. {
  300. // we have only a single item to enum
  301. // so we can just skip it and avoid any overflow errors
  302. if (ulCount > 0 && _iIndex == 0)
  303. {
  304. _iIndex++;
  305. }
  306. return S_OK;
  307. }
  308. //+---------------------------------------------------------------------------
  309. //
  310. // EnumDisplayAttributeInfo
  311. //
  312. //----------------------------------------------------------------------------
  313. STDAPI CMarkTextService::EnumDisplayAttributeInfo(IEnumTfDisplayAttributeInfo **ppEnum)
  314. {
  315. CEnumDisplayAttributeInfo *pAttributeEnum;
  316. if (ppEnum == NULL)
  317. return E_INVALIDARG;
  318. *ppEnum = NULL;
  319. if ((pAttributeEnum = new CEnumDisplayAttributeInfo) == NULL)
  320. return E_OUTOFMEMORY;
  321. *ppEnum = pAttributeEnum;
  322. return S_OK;
  323. }
  324. //+---------------------------------------------------------------------------
  325. //
  326. // GetDisplayAttributeInfo
  327. //
  328. //----------------------------------------------------------------------------
  329. STDAPI CMarkTextService::GetDisplayAttributeInfo(REFGUID guidInfo, ITfDisplayAttributeInfo **ppInfo)
  330. {
  331. if (ppInfo == NULL)
  332. return E_INVALIDARG;
  333. *ppInfo = NULL;
  334. // unsupported GUID?
  335. if (!IsEqualGUID(guidInfo, c_guidMarkDisplayAttribute))
  336. return E_INVALIDARG;
  337. *ppInfo = &g_DisplayAttributeInfo;
  338. (*ppInfo)->AddRef();
  339. return S_OK;
  340. }