comparison EXCLUDE/GLTSF/src/TSF.cpp @ 4734:0c7c67d4e6ee

Added On_Char method to Window_Listener for WM_CHAR messages. Removed a lot of TSF code because part of it was wrong and part was too complicated. Added Clear method to clear the window. IME input should work in both windowed mode and fullscreen mode with these changes. I have tested on Windows XP SP3 and Windows 7 Ultimate in VirtualBox. When you type a character (with an IME or not), the console will show the code point as U+XXXX. You use Left Alt+Shift (or whatever you have it set to) to switch input languages as usual. Hit ESC to exit (or close the window in windowed mode). The program will pause before exiting so you can review the console output (press a key to exit).
author dewyatt
date Wed, 09 Jun 2010 00:03:54 -0400
parents e1307be6cb9e
children f51a9f01b508
comparison
equal deleted inserted replaced
4733:983eb9d5ed31 4734:0c7c67d4e6ee
1 #include "TSF.hpp" 1 #include "TSF.hpp"
2 #include <stdexcept> 2 #include <stdexcept>
3 3
4 TSF::TSF() : my_COM_Initialized(false), 4 bool TSF::COM_Initialized = false;
5 my_UI_Sink(0), 5 CComPtr<ITfThreadMgr> TSF::Thread_Manager;
6 my_UI_Element_Sink_Cookie(TF_INVALID_COOKIE),
7 my_IPPA_Sink_Cookie(TF_INVALID_COOKIE)
8 {
9
10 }
11
12 TSF::~TSF()
13 {
14 Finalize();
15 }
16 6
17 void TSF::Initialize() 7 void TSF::Initialize()
18 { 8 {
19 Finalize(); 9 if (!COM_Initialized)
10 {
11 HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
12 if (S_OK != hr && S_FALSE != hr)
13 throw std::runtime_error("Failed to initialize COM");
20 14
21 if (S_OK != CoInitializeEx(NULL, COINIT_APARTMENTTHREADED)) 15 COM_Initialized = true;
22 throw std::runtime_error("Failed to initialize COM"); 16 }
17 if (!Thread_Manager)
18 {
19 if (FAILED(CoCreateInstance(CLSID_TF_ThreadMgr, NULL, CLSCTX_INPROC_SERVER, IID_ITfThreadMgr, reinterpret_cast<void **>(&Thread_Manager))))
20 throw std::runtime_error("Failed to create ITfThreadMgr instance");
23 21
24 my_COM_Initialized = true; 22 TfClientId ClientId;
25 if (S_OK != CoCreateInstance(CLSID_TF_ThreadMgr, NULL, CLSCTX_INPROC_SERVER, __uuidof(ITfThreadMgrEx), (void **)&my_Thread_Manager)) 23 if (FAILED(Thread_Manager->Activate(&ClientId)))
26 throw std::runtime_error("Failed to create ITfThreadMgrEx instance"); 24 throw std::runtime_error("ITfThreadMgr::Activate failed");
27 25 }
28 TfClientId Client_Id = 0;
29 if (FAILED(my_Thread_Manager->ActivateEx(&Client_Id, TF_TMAE_UIELEMENTENABLEDONLY)))
30 throw std::runtime_error("ITfThreadMgrEx::ActivateEx failed");
31
32 my_UI_Sink = new UI_Sink();
33 ITfSource *Source = NULL;
34 if (FAILED(my_Thread_Manager->QueryInterface(__uuidof(ITfSource), (void **)&Source)))
35 throw std::runtime_error("QueryInterface failed");
36
37 if (FAILED(Source->AdviseSink(__uuidof(ITfUIElementSink), (ITfUIElementSink *)my_UI_Sink, &my_UI_Element_Sink_Cookie)))
38 throw std::runtime_error("AdviseSink failed");
39
40 if (FAILED(Source->AdviseSink(__uuidof(ITfInputProcessorProfileActivationSink), (ITfInputProcessorProfileActivationSink *)my_UI_Sink, &my_IPPA_Sink_Cookie)))
41 throw std::runtime_error("AdviseSink failed");
42 } 26 }
43 27
44 void TSF::Finalize() 28 void TSF::Finalize()
45 { 29 {
46 if (my_UI_Sink) 30 if (Thread_Manager)
47 { 31 {
48 ITfSource *Source = NULL; 32 Thread_Manager->Deactivate();
49 if (SUCCEEDED(my_Thread_Manager->QueryInterface(__uuidof(ITfSource), (void **)&Source))) 33 Thread_Manager = NULL;
50 {
51 Source->UnadviseSink(my_IPPA_Sink_Cookie);
52 Source->UnadviseSink(my_UI_Element_Sink_Cookie);
53 Source->Release();
54 }
55 if (my_Thread_Manager)
56 my_Thread_Manager->Deactivate();
57
58 my_UI_Sink->Release();
59 delete my_UI_Sink;
60 my_UI_Sink = NULL;
61 } 34 }
62 my_Thread_Manager = NULL; 35 if (COM_Initialized)
63 if (my_COM_Initialized)
64 { 36 {
65 CoUninitialize(); 37 CoUninitialize();
66 my_COM_Initialized = false; 38 COM_Initialized = false;
67 } 39 }
68 } 40 }
69
70 TSF::UI_Sink::UI_Sink()
71 {
72 my_Reference_Count = 1;
73 }
74
75 TSF::UI_Sink::~UI_Sink()
76 {
77
78 }
79
80 STDMETHODIMP TSF::UI_Sink::QueryInterface(REFIID riid, void **ppvObj)
81 {
82 if (NULL == ppvObj)
83 return E_INVALIDARG;
84
85 *ppvObj = NULL;
86 if (IsEqualIID(riid, IID_IUnknown))
87 *ppvObj = reinterpret_cast<IUnknown *>(this);
88 else if (IsEqualIID(riid, __uuidof(ITfUIElementSink)))
89 *ppvObj = reinterpret_cast<ITfUIElementSink *>(this);
90 else if (IsEqualIID(riid, __uuidof(ITfInputProcessorProfileActivationSink)))
91 *ppvObj = reinterpret_cast<ITfInputProcessorProfileActivationSink *>(this);
92 else if (IsEqualIID(riid, __uuidof(ITfCompartmentEventSink)))
93 *ppvObj = reinterpret_cast<ITfCompartmentEventSink *>(this);
94
95 if (*ppvObj)
96 {
97 AddRef();
98 return S_OK;
99 }
100 return E_NOINTERFACE;
101 }
102
103 ULONG TSF::UI_Sink::AddRef(void)
104 {
105 return ++my_Reference_Count;
106 }
107
108 ULONG TSF::UI_Sink::Release(void)
109 {
110 LONG Count = --my_Reference_Count;
111 if (0 == Count)
112 delete this;
113
114 return Count;
115 }
116
117 STDMETHODIMP TSF::UI_Sink::BeginUIElement(DWORD dwUIElementId, BOOL *pbShow)
118 {
119 return S_OK;
120 }
121
122 STDMETHODIMP TSF::UI_Sink::UpdateUIElement(DWORD dwUIElementId)
123 {
124 return S_OK;
125 }
126
127 STDMETHODIMP TSF::UI_Sink::EndUIElement(DWORD dwUIElementId)
128 {
129 return S_OK;
130 }
131
132 STDMETHODIMP TSF::UI_Sink::OnActivated(DWORD dwProfileType, LANGID langid, REFCLSID clsid, REFGUID catid, REFGUID guidProfile, HKL hkl, DWORD dwFlags)
133 {
134 return S_OK;
135 }
136
137 STDMETHODIMP TSF::UI_Sink::OnChange(REFGUID rguid)
138 {
139 return S_OK;
140 }