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.

107 lines
3.0 KiB

  1. //
  2. // hello.cpp
  3. //
  4. // "Hello World" menu item handler.
  5. //
  6. #include "globals.h"
  7. #include "case.h"
  8. #include "editsess.h"
  9. class CHelloEditSession : public CEditSessionBase
  10. {
  11. public:
  12. CHelloEditSession(ITfContext *pContext) : CEditSessionBase(pContext) {}
  13. // ITfEditSession
  14. STDMETHODIMP DoEditSession(TfEditCookie ec);
  15. };
  16. //+---------------------------------------------------------------------------
  17. //
  18. // _Menu_HelloWord
  19. //
  20. // Insert the string "Hello world!" to the focus context.
  21. //----------------------------------------------------------------------------
  22. void CCaseTextService::_Menu_HelloWord(CCaseTextService *_this)
  23. {
  24. ITfDocumentMgr *pFocusDoc;
  25. ITfContext *pContext;
  26. CHelloEditSession *pHelloEditSession;
  27. HRESULT hr;
  28. // get the focus document
  29. if (_this->_pThreadMgr->GetFocus(&pFocusDoc) != S_OK)
  30. return;
  31. // we want the topmost context, since the main doc context could be
  32. // superceded by a modal tip context
  33. if (pFocusDoc->GetTop(&pContext) != S_OK)
  34. {
  35. pContext = NULL;
  36. goto Exit;
  37. }
  38. if (pHelloEditSession = new CHelloEditSession(pContext))
  39. {
  40. // we need a document write lock to insert text
  41. // the CHelloEditSession will do all the work when the
  42. // CHelloEditSession::DoEditSession method is called by the context
  43. pContext->RequestEditSession(_this->_tfClientId, pHelloEditSession, TF_ES_READWRITE | TF_ES_ASYNCDONTCARE, &hr);
  44. pHelloEditSession->Release();
  45. }
  46. Exit:
  47. SafeRelease(pContext);
  48. pFocusDoc->Release();
  49. }
  50. //+---------------------------------------------------------------------------
  51. //
  52. // DoEditSession
  53. //
  54. //----------------------------------------------------------------------------
  55. STDAPI CHelloEditSession::DoEditSession(TfEditCookie ec)
  56. {
  57. InsertTextAtSelection(ec, _pContext, L"Hello world!", wcslen(L"Hello world!"));
  58. return S_OK;
  59. }
  60. //+---------------------------------------------------------------------------
  61. //
  62. // InsertTextAtSelection
  63. //
  64. //----------------------------------------------------------------------------
  65. void InsertTextAtSelection(TfEditCookie ec, ITfContext *pContext, const WCHAR *pchText, ULONG cchText)
  66. {
  67. ITfInsertAtSelection *pInsertAtSelection;
  68. ITfRange *pRange;
  69. TF_SELECTION tfSelection;
  70. // we need a special interface to insert text at the selection
  71. if (pContext->QueryInterface(IID_ITfInsertAtSelection, (void **)&pInsertAtSelection) != S_OK)
  72. return;
  73. // insert the text
  74. if (pInsertAtSelection->InsertTextAtSelection(ec, 0, pchText, cchText, &pRange) != S_OK)
  75. goto Exit;
  76. // update the selection, we'll make it an insertion point just past
  77. // the inserted text.
  78. pRange->Collapse(ec, TF_ANCHOR_END);
  79. tfSelection.range = pRange;
  80. tfSelection.style.ase = TF_AE_NONE;
  81. tfSelection.style.fInterimChar = FALSE;
  82. pContext->SetSelection(ec, 1, &tfSelection);
  83. pRange->Release();
  84. Exit:
  85. pInsertAtSelection->Release();
  86. }