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.

364 lines
9.7 KiB

  1. #include "private.h"
  2. #include "globals.h"
  3. #include "dispattr.h"
  4. #include "proputil.h"
  5. #include "catutil.h"
  6. #include "ctffunc.h"
  7. #include "helpers.h"
  8. #include "ciccs.h"
  9. extern CCicCriticalSectionStatic g_cs;
  10. CDispAttrPropCache *g_pPropCache = NULL;
  11. //+---------------------------------------------------------------------------
  12. //
  13. // GetDAMLib
  14. //
  15. //----------------------------------------------------------------------------
  16. ITfDisplayAttributeMgr *GetDAMLib(LIBTHREAD *plt)
  17. {
  18. return plt->_pDAM;
  19. }
  20. //+---------------------------------------------------------------------------
  21. //
  22. // InitDisplayAttributeLib
  23. //
  24. //----------------------------------------------------------------------------
  25. HRESULT InitDisplayAttrbuteLib(LIBTHREAD *plt)
  26. {
  27. IEnumGUID *pEnumProp = NULL;
  28. if ( plt == NULL )
  29. return E_FAIL;
  30. if (plt->_pDAM)
  31. plt->_pDAM->Release();
  32. plt->_pDAM = NULL;
  33. if (FAILED(g_pfnCoCreate(CLSID_TF_DisplayAttributeMgr,
  34. NULL,
  35. CLSCTX_INPROC_SERVER,
  36. IID_ITfDisplayAttributeMgr,
  37. (void**)&plt->_pDAM)))
  38. {
  39. return E_FAIL;
  40. }
  41. LibEnumItemsInCategory(plt, GUID_TFCAT_DISPLAYATTRIBUTEPROPERTY, &pEnumProp);
  42. HRESULT hr;
  43. EnterCriticalSection(g_cs);
  44. //
  45. // make a database for Display Attribute Properties.
  46. //
  47. if (pEnumProp && !g_pPropCache)
  48. {
  49. GUID guidProp;
  50. g_pPropCache = new CDispAttrPropCache;
  51. if (!g_pPropCache)
  52. {
  53. hr = E_OUTOFMEMORY;
  54. goto Exit;
  55. }
  56. //
  57. // add System Display Attribute first.
  58. // so no other Display Attribute property overwrite it.
  59. //
  60. g_pPropCache->Add(GUID_PROP_ATTRIBUTE);
  61. while(pEnumProp->Next(1, &guidProp, NULL) == S_OK)
  62. {
  63. if (!IsEqualGUID(guidProp, GUID_PROP_ATTRIBUTE))
  64. g_pPropCache->Add(guidProp);
  65. }
  66. }
  67. hr = S_OK;
  68. Exit:
  69. LeaveCriticalSection(g_cs);
  70. SafeRelease(pEnumProp);
  71. return hr;
  72. }
  73. //+---------------------------------------------------------------------------
  74. //
  75. // UninitDisplayAttributeLib
  76. //
  77. //----------------------------------------------------------------------------
  78. HRESULT UninitDisplayAttrbuteLib(LIBTHREAD *plt)
  79. {
  80. Assert(plt);
  81. if ( plt == NULL )
  82. return E_FAIL;
  83. if (plt->_pDAM)
  84. plt->_pDAM->Release();
  85. plt->_pDAM = NULL;
  86. // if (plt->_fDAMCoInit)
  87. // CoUninitialize();
  88. //
  89. // plt->_fDAMCoInit = FALSE;
  90. return S_OK;
  91. }
  92. //+---------------------------------------------------------------------------
  93. //
  94. // GetDisplayAttributeTrackPropertyRange
  95. //
  96. //----------------------------------------------------------------------------
  97. HRESULT GetDisplayAttributeTrackPropertyRange(TfEditCookie ec, ITfContext *pic, ITfRange *pRange, ITfReadOnlyProperty **ppProp, IEnumTfRanges **ppEnum, ULONG *pulNumProp)
  98. {
  99. ITfReadOnlyProperty *pProp = NULL;
  100. HRESULT hr = E_FAIL;
  101. GUID *pguidProp = NULL;
  102. const GUID **ppguidProp;
  103. ULONG ulNumProp = 0;
  104. ULONG i;
  105. EnterCriticalSection(g_cs);
  106. if (!g_pPropCache)
  107. goto Exit;
  108. pguidProp = g_pPropCache->GetPropTable();
  109. if (!pguidProp)
  110. goto Exit;
  111. ulNumProp = g_pPropCache->Count();
  112. if (!ulNumProp)
  113. goto Exit;
  114. // TrackProperties wants an array of GUID *'s
  115. if ((ppguidProp = (const GUID **)cicMemAlloc(sizeof(GUID *)*ulNumProp)) == NULL)
  116. {
  117. hr = E_OUTOFMEMORY;
  118. goto Exit;
  119. }
  120. for (i=0; i<ulNumProp; i++)
  121. {
  122. ppguidProp[i] = pguidProp++;
  123. }
  124. if (SUCCEEDED(hr = pic->TrackProperties(ppguidProp,
  125. ulNumProp,
  126. 0,
  127. NULL,
  128. &pProp)))
  129. {
  130. hr = pProp->EnumRanges(ec, ppEnum, pRange);
  131. if (SUCCEEDED(hr))
  132. {
  133. *ppProp = pProp;
  134. pProp->AddRef();
  135. }
  136. pProp->Release();
  137. }
  138. cicMemFree(ppguidProp);
  139. if (SUCCEEDED(hr))
  140. *pulNumProp = ulNumProp;
  141. Exit:
  142. LeaveCriticalSection(g_cs);
  143. return hr;
  144. }
  145. //+---------------------------------------------------------------------------
  146. //
  147. // GetDisplayAttributeData
  148. //
  149. //----------------------------------------------------------------------------
  150. HRESULT GetDisplayAttributeData(LIBTHREAD *plt, TfEditCookie ec, ITfReadOnlyProperty *pProp, ITfRange *pRange, TF_DISPLAYATTRIBUTE *pda, TfGuidAtom *pguid, ULONG ulNumProp)
  151. {
  152. VARIANT var;
  153. IEnumTfPropertyValue *pEnumPropertyVal;
  154. TF_PROPERTYVAL tfPropVal;
  155. GUID guid;
  156. TfGuidAtom gaVal;
  157. ITfDisplayAttributeInfo *pDAI;
  158. HRESULT hr = E_FAIL;
  159. if (SUCCEEDED(pProp->GetValue(ec, pRange, &var)))
  160. {
  161. Assert(var.vt == VT_UNKNOWN);
  162. if (SUCCEEDED(var.punkVal->QueryInterface(IID_IEnumTfPropertyValue,
  163. (void **)&pEnumPropertyVal)))
  164. {
  165. while (pEnumPropertyVal->Next(1, &tfPropVal, NULL) == S_OK)
  166. {
  167. if (tfPropVal.varValue.vt == VT_EMPTY)
  168. continue; // prop has no value over this span
  169. Assert(tfPropVal.varValue.vt == VT_I4); // expecting GUIDATOMs
  170. gaVal = (TfGuidAtom)tfPropVal.varValue.lVal;
  171. GetGUIDFromGUIDATOM(plt, gaVal, &guid);
  172. if ((plt != NULL) && SUCCEEDED(plt->_pDAM->GetDisplayAttributeInfo(guid, &pDAI, NULL)))
  173. {
  174. //
  175. // Issue: for simple apps.
  176. //
  177. // Small apps can not show multi underline. So
  178. // this helper function returns only one
  179. // DISPLAYATTRIBUTE structure.
  180. //
  181. if (pda)
  182. {
  183. pDAI->GetAttributeInfo(pda);
  184. }
  185. if (pguid)
  186. {
  187. *pguid = gaVal;
  188. }
  189. pDAI->Release();
  190. hr = S_OK;
  191. break;
  192. }
  193. }
  194. pEnumPropertyVal->Release();
  195. }
  196. VariantClear(&var);
  197. }
  198. return hr;
  199. }
  200. //+---------------------------------------------------------------------------
  201. //
  202. // GetAttributeColor
  203. //
  204. //----------------------------------------------------------------------------
  205. HRESULT GetAttributeColor(TF_DA_COLOR *pdac, COLORREF *pcr)
  206. {
  207. switch (pdac->type)
  208. {
  209. case TF_CT_NONE:
  210. return S_FALSE;
  211. case TF_CT_SYSCOLOR:
  212. *pcr = GetSysColor(pdac->nIndex);
  213. break;
  214. case TF_CT_COLORREF:
  215. *pcr = pdac->cr;
  216. break;
  217. }
  218. return S_OK;
  219. }
  220. //+---------------------------------------------------------------------------
  221. //
  222. // SetAttributeColor
  223. //
  224. //----------------------------------------------------------------------------
  225. HRESULT SetAttributeColor(TF_DA_COLOR *pdac, COLORREF cr)
  226. {
  227. pdac->type = TF_CT_COLORREF;
  228. pdac->cr = cr;
  229. return S_OK;
  230. }
  231. //+---------------------------------------------------------------------------
  232. //
  233. // SetAttributeSysColor
  234. //
  235. //----------------------------------------------------------------------------
  236. HRESULT SetAttributeSysColor(TF_DA_COLOR *pdac, int nIndex)
  237. {
  238. pdac->type = TF_CT_SYSCOLOR;
  239. pdac->nIndex = nIndex;
  240. return S_OK;
  241. }
  242. //+---------------------------------------------------------------------------
  243. //
  244. // ClearAttributeColor
  245. //
  246. //----------------------------------------------------------------------------
  247. HRESULT ClearAttributeColor(TF_DA_COLOR *pdac)
  248. {
  249. pdac->type = TF_CT_NONE;
  250. pdac->nIndex = 0;
  251. return S_OK;
  252. }
  253. //+---------------------------------------------------------------------------
  254. //
  255. // GetReconversionFromDisplayAttribute
  256. //
  257. //----------------------------------------------------------------------------
  258. HRESULT GetReconversionFromDisplayAttribute(LIBTHREAD *plt, TfEditCookie ec, ITfThreadMgr *ptim, ITfContext *pic, ITfRange *pRange, ITfFnReconversion **ppReconv, ITfDisplayAttributeMgr *pDAM)
  259. {
  260. IEnumTfRanges *epr = NULL;
  261. ITfReadOnlyProperty *pProp;
  262. ITfRange *proprange;
  263. ULONG ulNumProp;
  264. HRESULT hr = E_FAIL;
  265. //
  266. // get an enumorator
  267. //
  268. if (FAILED(GetDisplayAttributeTrackPropertyRange(ec, pic, pRange, &pProp, &epr, &ulNumProp)))
  269. goto Exit;
  270. //
  271. // Get display attribute of the first proprange.
  272. //
  273. if (epr->Next(1, &proprange, NULL) == S_OK)
  274. {
  275. ITfRange *rangeTmp = NULL;
  276. TfGuidAtom guidatom;
  277. if (SUCCEEDED(GetDisplayAttributeData(plt, ec, pProp, proprange, NULL, &guidatom, ulNumProp)))
  278. {
  279. CLSID clsid;
  280. GUID guid;
  281. if (GetGUIDFromGUIDATOM(plt, guidatom, &guid) &&
  282. SUCCEEDED(pDAM->GetDisplayAttributeInfo(guid, NULL, &clsid)))
  283. {
  284. ITfFunctionProvider *pFuncPrv;
  285. if (SUCCEEDED(ptim->GetFunctionProvider(clsid, &pFuncPrv)))
  286. {
  287. hr = pFuncPrv->GetFunction(GUID_NULL, IID_ITfFnReconversion, (IUnknown **)ppReconv);
  288. pFuncPrv->Release();
  289. }
  290. }
  291. }
  292. proprange->Release();
  293. }
  294. epr->Release();
  295. pProp->Release();
  296. Exit:
  297. return hr;
  298. }