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.

396 lines
10 KiB

  1. //
  2. // langbar.cpp
  3. //
  4. // Language bar ui code.
  5. //
  6. #include "globals.h"
  7. #include "case.h"
  8. #include "resource.h"
  9. #define CASE_LANGBARITEMSINK_COOKIE 0x0fab0fab
  10. const struct
  11. {
  12. const WCHAR *pchDesc;
  13. void (*pfnHandler)(CCaseTextService *_this);
  14. }
  15. c_rgMenuItems[] =
  16. {
  17. { L"Show Snoop Wnd", CCaseTextService::_Menu_ShowSnoopWnd }, // must match MENU_SHOWSNOOP_INDEX
  18. { L"Hello World", CCaseTextService::_Menu_HelloWord },
  19. { L"Flip Selection", CCaseTextService::_Menu_FlipSel },
  20. { L"Flip Doc", CCaseTextService::_Menu_FlipDoc },
  21. { L"Flip Keystrokes", CCaseTextService::_Menu_FlipKeys }, // must match MENU_FLIPKEYS_INDEX
  22. };
  23. #define MENU_SHOWSNOOP_INDEX 0
  24. #define MENU_FLIPKEYS_INDEX 4
  25. class CLangBarItemButton : public ITfLangBarItemButton,
  26. public ITfSource
  27. {
  28. public:
  29. CLangBarItemButton(CCaseTextService *pCase);
  30. ~CLangBarItemButton();
  31. // IUnknown
  32. STDMETHODIMP QueryInterface(REFIID riid, void **ppvObj);
  33. STDMETHODIMP_(ULONG) AddRef(void);
  34. STDMETHODIMP_(ULONG) Release(void);
  35. // ITfLangBarItem
  36. STDMETHODIMP GetInfo(TF_LANGBARITEMINFO *pInfo);
  37. STDMETHODIMP GetStatus(DWORD *pdwStatus);
  38. STDMETHODIMP Show(BOOL fShow);
  39. STDMETHODIMP GetTooltipString(BSTR *pbstrToolTip);
  40. // ITfLangBarItemButton
  41. STDMETHODIMP OnClick(TfLBIClick click, POINT pt, const RECT *prcArea);
  42. STDMETHODIMP InitMenu(ITfMenu *pMenu);
  43. STDMETHODIMP OnMenuSelect(UINT wID);
  44. STDMETHODIMP GetIcon(HICON *phIcon);
  45. STDMETHODIMP GetText(BSTR *pbstrText);
  46. // ITfSource
  47. STDMETHODIMP AdviseSink(REFIID riid, IUnknown *punk, DWORD *pdwCookie);
  48. STDMETHODIMP UnadviseSink(DWORD dwCookie);
  49. private:
  50. CCaseTextService *_pCase;
  51. ITfLangBarItemSink *_pLangBarItemSink;
  52. TF_LANGBARITEMINFO _tfLangBarItemInfo;
  53. LONG _cRef;
  54. };
  55. //+---------------------------------------------------------------------------
  56. //
  57. // ctor
  58. //
  59. //----------------------------------------------------------------------------
  60. CLangBarItemButton::CLangBarItemButton(CCaseTextService *pCase)
  61. {
  62. DllAddRef();
  63. _tfLangBarItemInfo.clsidService = c_clsidCaseTextService;
  64. _tfLangBarItemInfo.guidItem = c_guidLangBarItemButton;
  65. _tfLangBarItemInfo.dwStyle = TF_LBI_STYLE_BTN_MENU;
  66. _tfLangBarItemInfo.ulSort = 0;
  67. SafeStringCopy(_tfLangBarItemInfo.szDescription, ARRAYSIZE(_tfLangBarItemInfo.szDescription), LANGBAR_ITEM_DESC);
  68. _pCase = pCase;
  69. _pCase->AddRef();
  70. _pLangBarItemSink = NULL;
  71. _cRef = 1;
  72. }
  73. //+---------------------------------------------------------------------------
  74. //
  75. // dtor
  76. //
  77. //----------------------------------------------------------------------------
  78. CLangBarItemButton::~CLangBarItemButton()
  79. {
  80. DllRelease();
  81. _pCase->Release();
  82. }
  83. //+---------------------------------------------------------------------------
  84. //
  85. // QueryInterface
  86. //
  87. //----------------------------------------------------------------------------
  88. STDAPI CLangBarItemButton::QueryInterface(REFIID riid, void **ppvObj)
  89. {
  90. if (ppvObj == NULL)
  91. return E_INVALIDARG;
  92. *ppvObj = NULL;
  93. if (IsEqualIID(riid, IID_IUnknown) ||
  94. IsEqualIID(riid, IID_ITfLangBarItem) ||
  95. IsEqualIID(riid, IID_ITfLangBarItemButton))
  96. {
  97. *ppvObj = (ITfLangBarItemButton *)this;
  98. }
  99. else if (IsEqualIID(riid, IID_ITfSource))
  100. {
  101. *ppvObj = (ITfSource *)this;
  102. }
  103. if (*ppvObj)
  104. {
  105. AddRef();
  106. return S_OK;
  107. }
  108. return E_NOINTERFACE;
  109. }
  110. //+---------------------------------------------------------------------------
  111. //
  112. // AddRef
  113. //
  114. //----------------------------------------------------------------------------
  115. STDAPI_(ULONG) CLangBarItemButton::AddRef()
  116. {
  117. return ++_cRef;
  118. }
  119. //+---------------------------------------------------------------------------
  120. //
  121. // Release
  122. //
  123. //----------------------------------------------------------------------------
  124. STDAPI_(ULONG) CLangBarItemButton::Release()
  125. {
  126. LONG cr = --_cRef;
  127. assert(_cRef >= 0);
  128. if (_cRef == 0)
  129. {
  130. delete this;
  131. }
  132. return cr;
  133. }
  134. //+---------------------------------------------------------------------------
  135. //
  136. // GetInfo
  137. //
  138. //----------------------------------------------------------------------------
  139. STDAPI CLangBarItemButton::GetInfo(TF_LANGBARITEMINFO *pInfo)
  140. {
  141. *pInfo = _tfLangBarItemInfo;
  142. return S_OK;
  143. }
  144. //+---------------------------------------------------------------------------
  145. //
  146. // GetStatus
  147. //
  148. //----------------------------------------------------------------------------
  149. STDAPI CLangBarItemButton::GetStatus(DWORD *pdwStatus)
  150. {
  151. *pdwStatus = 0;
  152. return S_OK;
  153. }
  154. //+---------------------------------------------------------------------------
  155. //
  156. // Show
  157. //
  158. //----------------------------------------------------------------------------
  159. STDAPI CLangBarItemButton::Show(BOOL fShow)
  160. {
  161. return E_NOTIMPL;
  162. }
  163. //+---------------------------------------------------------------------------
  164. //
  165. // GetTooltipString
  166. //
  167. //----------------------------------------------------------------------------
  168. STDAPI CLangBarItemButton::GetTooltipString(BSTR *pbstrToolTip)
  169. {
  170. *pbstrToolTip = SysAllocString(LANGBAR_ITEM_DESC);
  171. return (*pbstrToolTip == NULL) ? E_OUTOFMEMORY : S_OK;
  172. }
  173. //+---------------------------------------------------------------------------
  174. //
  175. // OnClick
  176. //
  177. //----------------------------------------------------------------------------
  178. STDAPI CLangBarItemButton::OnClick(TfLBIClick click, POINT pt, const RECT *prcArea)
  179. {
  180. return S_OK;
  181. }
  182. //+---------------------------------------------------------------------------
  183. //
  184. // InitMenu
  185. //
  186. //----------------------------------------------------------------------------
  187. STDAPI CLangBarItemButton::InitMenu(ITfMenu *pMenu)
  188. {
  189. int i;
  190. DWORD dwFlags;
  191. for (i=0; i<ARRAYSIZE(c_rgMenuItems); i++)
  192. {
  193. switch (i)
  194. {
  195. case MENU_SHOWSNOOP_INDEX:
  196. dwFlags = _pCase->_IsSnoopWndVisible() ? TF_LBMENUF_CHECKED : 0;
  197. break;
  198. case MENU_FLIPKEYS_INDEX:
  199. dwFlags = _pCase->_IsKeyFlipping() ? TF_LBMENUF_CHECKED : 0;
  200. break;
  201. default:
  202. // all other items are plain
  203. dwFlags = 0;
  204. break;
  205. }
  206. pMenu->AddMenuItem(i, dwFlags, NULL, NULL, c_rgMenuItems[i].pchDesc, wcslen(c_rgMenuItems[i].pchDesc), NULL);
  207. }
  208. return S_OK;
  209. }
  210. //+---------------------------------------------------------------------------
  211. //
  212. // OnMenuSelect
  213. //
  214. //----------------------------------------------------------------------------
  215. STDAPI CLangBarItemButton::OnMenuSelect(UINT wID)
  216. {
  217. c_rgMenuItems[wID].pfnHandler(_pCase);
  218. return S_OK;
  219. }
  220. //+---------------------------------------------------------------------------
  221. //
  222. // GetIcon
  223. //
  224. //----------------------------------------------------------------------------
  225. STDAPI CLangBarItemButton::GetIcon(HICON *phIcon)
  226. {
  227. *phIcon = (HICON)LoadImage(g_hInst, TEXT("IDI_CASE"), IMAGE_ICON, 16, 16, 0);
  228. return (*phIcon != NULL) ? S_OK : E_FAIL;
  229. }
  230. //+---------------------------------------------------------------------------
  231. //
  232. // GetText
  233. //
  234. //----------------------------------------------------------------------------
  235. STDAPI CLangBarItemButton::GetText(BSTR *pbstrText)
  236. {
  237. *pbstrText = SysAllocString(LANGBAR_ITEM_DESC);
  238. return (*pbstrText == NULL) ? E_OUTOFMEMORY : S_OK;
  239. }
  240. //+---------------------------------------------------------------------------
  241. //
  242. // AdviseSink
  243. //
  244. //----------------------------------------------------------------------------
  245. STDAPI CLangBarItemButton::AdviseSink(REFIID riid, IUnknown *punk, DWORD *pdwCookie)
  246. {
  247. if (!IsEqualIID(IID_ITfLangBarItemSink, riid))
  248. return CONNECT_E_CANNOTCONNECT;
  249. if (_pLangBarItemSink != NULL)
  250. return CONNECT_E_ADVISELIMIT;
  251. if (punk->QueryInterface(IID_ITfLangBarItemSink, (void **)&_pLangBarItemSink) != S_OK)
  252. {
  253. _pLangBarItemSink = NULL;
  254. return E_NOINTERFACE;
  255. }
  256. *pdwCookie = CASE_LANGBARITEMSINK_COOKIE;
  257. return S_OK;
  258. }
  259. //+---------------------------------------------------------------------------
  260. //
  261. // UnadviseSink
  262. //
  263. //----------------------------------------------------------------------------
  264. STDAPI CLangBarItemButton::UnadviseSink(DWORD dwCookie)
  265. {
  266. if (dwCookie != CASE_LANGBARITEMSINK_COOKIE)
  267. return CONNECT_E_NOCONNECTION;
  268. if (_pLangBarItemSink == NULL)
  269. return CONNECT_E_NOCONNECTION;
  270. _pLangBarItemSink->Release();
  271. _pLangBarItemSink = NULL;
  272. return S_OK;
  273. }
  274. //+---------------------------------------------------------------------------
  275. //
  276. // _InitLanguageBar
  277. //
  278. //----------------------------------------------------------------------------
  279. BOOL CCaseTextService::_InitLanguageBar()
  280. {
  281. ITfLangBarItemMgr *pLangBarItemMgr;
  282. BOOL fRet;
  283. if (_pThreadMgr->QueryInterface(IID_ITfLangBarItemMgr, (void **)&pLangBarItemMgr) != S_OK)
  284. return FALSE;
  285. fRet = FALSE;
  286. if ((_pLangBarItem = new CLangBarItemButton(this)) == NULL)
  287. goto Exit;
  288. if (pLangBarItemMgr->AddItem(_pLangBarItem) != S_OK)
  289. {
  290. _pLangBarItem->Release();
  291. _pLangBarItem = NULL;
  292. goto Exit;
  293. }
  294. fRet = TRUE;
  295. Exit:
  296. pLangBarItemMgr->Release();
  297. return fRet;
  298. }
  299. //+---------------------------------------------------------------------------
  300. //
  301. // _UninitLanguageBar
  302. //
  303. //----------------------------------------------------------------------------
  304. void CCaseTextService::_UninitLanguageBar()
  305. {
  306. ITfLangBarItemMgr *pLangBarItemMgr;
  307. if (_pLangBarItem == NULL)
  308. return;
  309. if (_pThreadMgr->QueryInterface(IID_ITfLangBarItemMgr, (void **)&pLangBarItemMgr) == S_OK)
  310. {
  311. pLangBarItemMgr->RemoveItem(_pLangBarItem);
  312. pLangBarItemMgr->Release();
  313. }
  314. _pLangBarItem->Release();
  315. _pLangBarItem = NULL;
  316. }