Debian Codegraph @

gdk/win32/gdkwin32langnotification.c

raw
1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 2019 Руслан Ижбулатов <[email protected]>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "config.h"
19
20 #define COBJMACROS
21 #include <msctf.h>
22
23 #include <gdk/gdk.h>
24
25 #include "gdkprivate-win32.h"
26
27 struct _GdkWin32ALPNSink
28 {
29 ITfActiveLanguageProfileNotifySink itf_alpn_sink;
30
31 gint ref_count;
32 };
33
34 typedef struct _GdkWin32ALPNSink GdkWin32ALPNSink;
35
36 static GdkWin32ALPNSink *actlangchangenotify = NULL;
37 static ITfSource *itf_source = NULL;
38 static DWORD actlangchangenotify_id = 0;
39
40 static ULONG STDMETHODCALLTYPE
41 alpn_sink_addref (ITfActiveLanguageProfileNotifySink *This)
42 {
43 GdkWin32ALPNSink *alpn_sink = (GdkWin32ALPNSink *) This;
44 int ref_count = ++alpn_sink->ref_count;
45
46 return ref_count;
47 }
48
49 static HRESULT STDMETHODCALLTYPE
50 alpn_sink_queryinterface (ITfActiveLanguageProfileNotifySink *This,
51 REFIID riid,
52 LPVOID *ppvObject)
53 {
54 *ppvObject = NULL;
55
56 if (IsEqualGUID (riid, &IID_IUnknown))
57 {
58 ITfActiveLanguageProfileNotifySink_AddRef (This);
59 *ppvObject = This;
60 return S_OK;
61 }
62
63 if (IsEqualGUID (riid, &IID_ITfActiveLanguageProfileNotifySink))
64 {
65 ITfActiveLanguageProfileNotifySink_AddRef (This);
66 *ppvObject = This;
67 return S_OK;
68 }
69
70 return E_NOINTERFACE;
71 }
72
73 static ULONG STDMETHODCALLTYPE
74 alpn_sink_release (ITfActiveLanguageProfileNotifySink *This)
75 {
76 GdkWin32ALPNSink *alpn_sink = (GdkWin32ALPNSink *) This;
77 int ref_count = --alpn_sink->ref_count;
78
79 if (ref_count == 0)
80 {
81 g_free (This);
82 }
83
84 return ref_count;
85 }
86
87 static HRESULT STDMETHODCALLTYPE
88 alpn_sink_on_activated (ITfActiveLanguageProfileNotifySink *This,
89 REFCLSID clsid,
90 REFGUID guidProfile,
91 BOOL fActivated)
92 {
93 _gdk_input_locale_is_ime = fActivated;
94 return S_OK;
95 }
96
97 static ITfActiveLanguageProfileNotifySinkVtbl alpn_sink_vtbl = {
98 alpn_sink_queryinterface,
99 alpn_sink_addref,
100 alpn_sink_release,
101 alpn_sink_on_activated,
102 };
103
104 static GdkWin32ALPNSink *
105 alpn_sink_new ()
106 {
107 GdkWin32ALPNSink *result;
108
109 result = g_new0 (GdkWin32ALPNSink, 1);
110 result->itf_alpn_sink.lpVtbl = &alpn_sink_vtbl;
111 result->ref_count = 0;
112
113 ITfActiveLanguageProfileNotifySink_AddRef (&result->itf_alpn_sink);
114
115 return result;
116 }
117
118
119 void
120 _gdk_win32_lang_notification_init ()
121 {
122 HRESULT hr;
123 ITfThreadMgr *itf_threadmgr;
124
125 CoInitializeEx (NULL, COINIT_APARTMENTTHREADED);
126
127 if (actlangchangenotify != NULL)
128 return;
129
130 hr = CoCreateInstance (&CLSID_TF_ThreadMgr,
131 NULL,
132 CLSCTX_INPROC_SERVER,
133 &IID_ITfThreadMgr,
134 (LPVOID *) &itf_threadmgr);
135
136 if (!SUCCEEDED (hr))
137 return;
138
139 hr = ITfThreadMgr_QueryInterface (itf_threadmgr, &IID_ITfSource, (VOID **) &itf_source);
140 ITfThreadMgr_Release (itf_threadmgr);
141
142 if (!SUCCEEDED (hr))
143 return;
144
145 actlangchangenotify = alpn_sink_new ();
146
147 hr = ITfSource_AdviseSink (itf_source,
148 &IID_ITfActiveLanguageProfileNotifySink,
149 (IUnknown *) actlangchangenotify,
150 &actlangchangenotify_id);
151
152 if (!SUCCEEDED (hr))
153 {
154 ITfActiveLanguageProfileNotifySink_Release (&actlangchangenotify->itf_alpn_sink);
155 actlangchangenotify = NULL;
156 ITfSource_Release (itf_source);
157 itf_source = NULL;
158 }
159 }
160
161 void
162 _gdk_win32_lang_notification_exit ()
163 {
164 if (actlangchangenotify != NULL && itf_source != NULL)
165 {
166 ITfSource_UnadviseSink (itf_source, actlangchangenotify_id);
167 ITfSource_Release (itf_source);
168 ITfActiveLanguageProfileNotifySink_Release (&actlangchangenotify->itf_alpn_sink);
169 }
170
171 CoUninitialize ();
172 }