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.

210 lines
4.6 KiB

  1. //
  2. // tes.cpp
  3. //
  4. #include "private.h"
  5. #include "tes.h"
  6. #include "helpers.h"
  7. //////////////////////////////////////////////////////////////////////////////
  8. //
  9. // CTextEventSink
  10. //
  11. //////////////////////////////////////////////////////////////////////////////
  12. //+---------------------------------------------------------------------------
  13. //
  14. // IUnknown
  15. //
  16. //----------------------------------------------------------------------------
  17. STDAPI CTextEventSink::QueryInterface(REFIID riid, void **ppvObj)
  18. {
  19. *ppvObj = NULL;
  20. if (IsEqualIID(riid, IID_IUnknown) ||
  21. IsEqualIID(riid, IID_ITfTextEditSink))
  22. {
  23. *ppvObj = SAFECAST(this, ITfTextEditSink *);
  24. }
  25. else if (IsEqualIID(riid, IID_ITfTextLayoutSink))
  26. {
  27. *ppvObj = SAFECAST(this, ITfTextLayoutSink *);
  28. }
  29. if (*ppvObj)
  30. {
  31. AddRef();
  32. return S_OK;
  33. }
  34. return E_NOINTERFACE;
  35. }
  36. STDAPI_(ULONG) CTextEventSink::AddRef()
  37. {
  38. return ++_cRef;
  39. }
  40. STDAPI_(ULONG) CTextEventSink::Release()
  41. {
  42. long cr;
  43. cr = --_cRef;
  44. Assert(cr >= 0);
  45. if (cr == 0)
  46. {
  47. delete this;
  48. }
  49. return cr;
  50. }
  51. //+---------------------------------------------------------------------------
  52. //
  53. // ctor
  54. //
  55. //----------------------------------------------------------------------------
  56. CTextEventSink::CTextEventSink(TESCALLBACK pfnCallback, void *pv)
  57. {
  58. Dbg_MemSetThisName(TEXT("CTextEventSink"));
  59. _cRef = 1;
  60. _dwEditCookie = TES_INVALID_COOKIE;
  61. _dwLayoutCookie = TES_INVALID_COOKIE;
  62. _pfnCallback = pfnCallback;
  63. _pv = pv;
  64. }
  65. //+---------------------------------------------------------------------------
  66. //
  67. // EndEdit
  68. //
  69. //----------------------------------------------------------------------------
  70. STDAPI CTextEventSink::OnEndEdit(ITfContext *pic, TfEditCookie ecReadOnly, ITfEditRecord *pEditRecord)
  71. {
  72. TESENDEDIT ee;
  73. ee.ecReadOnly = ecReadOnly;
  74. ee.pEditRecord = pEditRecord;
  75. ee.pic = pic;
  76. return _pfnCallback(ICF_TEXTDELTA, _pv, &ee);
  77. }
  78. //+---------------------------------------------------------------------------
  79. //
  80. // OnLayoutChange
  81. //
  82. //----------------------------------------------------------------------------
  83. STDAPI CTextEventSink::OnLayoutChange(ITfContext *pic, TfLayoutCode lcode, ITfContextView *pView)
  84. {
  85. UINT uCode;
  86. switch (lcode)
  87. {
  88. case TF_LC_CREATE:
  89. uCode = ICF_LAYOUTDELTA_CREATE;
  90. break;
  91. case TF_LC_CHANGE:
  92. uCode = ICF_LAYOUTDELTA;
  93. break;
  94. case TF_LC_DESTROY:
  95. uCode = ICF_LAYOUTDELTA_DESTROY;
  96. break;
  97. default:
  98. Assert(0); // no other codes defined
  99. return E_INVALIDARG;
  100. }
  101. return _pfnCallback(uCode, _pv, pView);
  102. }
  103. //+---------------------------------------------------------------------------
  104. //
  105. // CTextEventSink::Advise
  106. //
  107. //----------------------------------------------------------------------------
  108. HRESULT CTextEventSink::_Advise(ITfContext *pic, DWORD dwFlags)
  109. {
  110. HRESULT hr;
  111. ITfSource *source = NULL;
  112. _pic = NULL;
  113. hr = E_FAIL;
  114. _dwFlags = dwFlags;
  115. if (FAILED(pic->QueryInterface(IID_ITfSource, (void **)&source)))
  116. goto Exit;
  117. if (dwFlags & ICF_TEXTDELTA)
  118. {
  119. if (FAILED(source->AdviseSink(IID_ITfTextEditSink, (ITfTextEditSink *)this, &_dwEditCookie)))
  120. goto Exit;
  121. }
  122. if (dwFlags & ICF_LAYOUTDELTA)
  123. {
  124. if (FAILED(source->AdviseSink(IID_ITfTextLayoutSink, (ITfTextLayoutSink *)this, &_dwLayoutCookie)))
  125. {
  126. source->UnadviseSink(_dwEditCookie);
  127. goto Exit;
  128. }
  129. }
  130. _pic = pic;
  131. _pic->AddRef();
  132. hr = S_OK;
  133. Exit:
  134. SafeRelease(source);
  135. return hr;
  136. }
  137. //+---------------------------------------------------------------------------
  138. //
  139. // CTextEventSink::Unadvise
  140. //
  141. //----------------------------------------------------------------------------
  142. HRESULT CTextEventSink::_Unadvise()
  143. {
  144. HRESULT hr;
  145. BOOL f;
  146. ITfSource *source = NULL;
  147. hr = E_FAIL;
  148. if (_pic == NULL)
  149. goto Exit;
  150. if (FAILED(_pic->QueryInterface(IID_ITfSource, (void **)&source)))
  151. goto Exit;
  152. f = TRUE;
  153. if (_dwFlags & ICF_TEXTDELTA)
  154. {
  155. f = SUCCEEDED(source->UnadviseSink(_dwEditCookie));
  156. }
  157. if (_dwFlags & ICF_LAYOUTDELTA)
  158. {
  159. f &= SUCCEEDED(source->UnadviseSink(_dwLayoutCookie));
  160. }
  161. if (!f)
  162. goto Exit;
  163. hr = S_OK;
  164. Exit:
  165. SafeRelease(source);
  166. SafeReleaseClear(_pic);
  167. return hr;
  168. }