* Sync up to trunk head (r65426).
[reactos.git] / dll / win32 / msctf / range.c
1 /*
2 * ITfRange implementation
3 *
4 * Copyright 2009 Aric Stewart, CodeWeavers
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include "msctf_internal.h"
22
23 typedef struct tagRange {
24 ITfRange ITfRange_iface;
25 /* const ITfRangeACPVtb *RangeACPVtbl; */
26 LONG refCount;
27
28 ITextStoreACP *pITextStoreACP;
29 ITfContext *pITfContext;
30
31 DWORD lockType;
32 TfGravity gravityStart, gravityEnd;
33 DWORD anchorStart, anchorEnd;
34
35 } Range;
36
37 static inline Range *impl_from_ITfRange(ITfRange *iface)
38 {
39 return CONTAINING_RECORD(iface, Range, ITfRange_iface);
40 }
41
42 static void Range_Destructor(Range *This)
43 {
44 TRACE("destroying %p\n", This);
45 HeapFree(GetProcessHeap(),0,This);
46 }
47
48 static HRESULT WINAPI Range_QueryInterface(ITfRange *iface, REFIID iid, LPVOID *ppvOut)
49 {
50 Range *This = impl_from_ITfRange(iface);
51 *ppvOut = NULL;
52
53 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfRange))
54 {
55 *ppvOut = &This->ITfRange_iface;
56 }
57
58 if (*ppvOut)
59 {
60 ITfRange_AddRef(iface);
61 return S_OK;
62 }
63
64 WARN("unsupported interface: %s\n", debugstr_guid(iid));
65 return E_NOINTERFACE;
66 }
67
68 static ULONG WINAPI Range_AddRef(ITfRange *iface)
69 {
70 Range *This = impl_from_ITfRange(iface);
71 return InterlockedIncrement(&This->refCount);
72 }
73
74 static ULONG WINAPI Range_Release(ITfRange *iface)
75 {
76 Range *This = impl_from_ITfRange(iface);
77 ULONG ret;
78
79 ret = InterlockedDecrement(&This->refCount);
80 if (ret == 0)
81 Range_Destructor(This);
82 return ret;
83 }
84
85 /*****************************************************
86 * ITfRange functions
87 *****************************************************/
88
89 static HRESULT WINAPI Range_GetText(ITfRange *iface, TfEditCookie ec,
90 DWORD dwFlags, WCHAR *pchText, ULONG cchMax, ULONG *pcch)
91 {
92 Range *This = impl_from_ITfRange(iface);
93 FIXME("STUB:(%p)\n",This);
94 return E_NOTIMPL;
95 }
96
97 static HRESULT WINAPI Range_SetText(ITfRange *iface, TfEditCookie ec,
98 DWORD dwFlags, const WCHAR *pchText, LONG cch)
99 {
100 Range *This = impl_from_ITfRange(iface);
101 FIXME("STUB:(%p)\n",This);
102 return E_NOTIMPL;
103 }
104
105 static HRESULT WINAPI Range_GetFormattedText(ITfRange *iface, TfEditCookie ec,
106 IDataObject **ppDataObject)
107 {
108 Range *This = impl_from_ITfRange(iface);
109 FIXME("STUB:(%p)\n",This);
110 return E_NOTIMPL;
111 }
112
113 static HRESULT WINAPI Range_GetEmbedded(ITfRange *iface, TfEditCookie ec,
114 REFGUID rguidService, REFIID riid, IUnknown **ppunk)
115 {
116 Range *This = impl_from_ITfRange(iface);
117 FIXME("STUB:(%p)\n",This);
118 return E_NOTIMPL;
119 }
120
121 static HRESULT WINAPI Range_InsertEmbedded(ITfRange *iface, TfEditCookie ec,
122 DWORD dwFlags, IDataObject *pDataObject)
123 {
124 Range *This = impl_from_ITfRange(iface);
125 FIXME("STUB:(%p)\n",This);
126 return E_NOTIMPL;
127 }
128
129 static HRESULT WINAPI Range_ShiftStart(ITfRange *iface, TfEditCookie ec,
130 LONG cchReq, LONG *pcch, const TF_HALTCOND *pHalt)
131 {
132 Range *This = impl_from_ITfRange(iface);
133 FIXME("STUB:(%p)\n",This);
134 return E_NOTIMPL;
135 }
136
137 static HRESULT WINAPI Range_ShiftEnd(ITfRange *iface, TfEditCookie ec,
138 LONG cchReq, LONG *pcch, const TF_HALTCOND *pHalt)
139 {
140 Range *This = impl_from_ITfRange(iface);
141 FIXME("STUB:(%p)\n",This);
142 return E_NOTIMPL;
143 }
144
145 static HRESULT WINAPI Range_ShiftStartToRange(ITfRange *iface, TfEditCookie ec,
146 ITfRange *pRange, TfAnchor aPos)
147 {
148 Range *This = impl_from_ITfRange(iface);
149 FIXME("STUB:(%p)\n",This);
150 return E_NOTIMPL;
151 }
152
153 static HRESULT WINAPI Range_ShiftEndToRange(ITfRange *iface, TfEditCookie ec,
154 ITfRange *pRange, TfAnchor aPos)
155 {
156 Range *This = impl_from_ITfRange(iface);
157 FIXME("STUB:(%p)\n",This);
158 return E_NOTIMPL;
159 }
160
161 static HRESULT WINAPI Range_ShiftStartRegion(ITfRange *iface, TfEditCookie ec,
162 TfShiftDir dir, BOOL *pfNoRegion)
163 {
164 Range *This = impl_from_ITfRange(iface);
165 FIXME("STUB:(%p)\n",This);
166 return E_NOTIMPL;
167 }
168
169 static HRESULT WINAPI Range_ShiftEndRegion(ITfRange *iface, TfEditCookie ec,
170 TfShiftDir dir, BOOL *pfNoRegion)
171 {
172 Range *This = impl_from_ITfRange(iface);
173 FIXME("STUB:(%p)\n",This);
174 return E_NOTIMPL;
175 }
176
177 static HRESULT WINAPI Range_IsEmpty(ITfRange *iface, TfEditCookie ec,
178 BOOL *pfEmpty)
179 {
180 Range *This = impl_from_ITfRange(iface);
181 FIXME("STUB:(%p)\n",This);
182 return E_NOTIMPL;
183 }
184
185 static HRESULT WINAPI Range_Collapse(ITfRange *iface, TfEditCookie ec,
186 TfAnchor aPos)
187 {
188 Range *This = impl_from_ITfRange(iface);
189 TRACE("(%p) %i %i\n",This,ec,aPos);
190
191 switch (aPos)
192 {
193 case TF_ANCHOR_START:
194 This->anchorEnd = This->anchorStart;
195 break;
196 case TF_ANCHOR_END:
197 This->anchorStart = This->anchorEnd;
198 break;
199 default:
200 return E_INVALIDARG;
201 }
202
203 return S_OK;
204 }
205
206 static HRESULT WINAPI Range_IsEqualStart(ITfRange *iface, TfEditCookie ec,
207 ITfRange *pWith, TfAnchor aPos, BOOL *pfEqual)
208 {
209 Range *This = impl_from_ITfRange(iface);
210 FIXME("STUB:(%p)\n",This);
211 return E_NOTIMPL;
212 }
213
214 static HRESULT WINAPI Range_IsEqualEnd(ITfRange *iface, TfEditCookie ec,
215 ITfRange *pWith, TfAnchor aPos, BOOL *pfEqual)
216 {
217 Range *This = impl_from_ITfRange(iface);
218 FIXME("STUB:(%p)\n",This);
219 return E_NOTIMPL;
220 }
221
222 static HRESULT WINAPI Range_CompareStart(ITfRange *iface, TfEditCookie ec,
223 ITfRange *pWith, TfAnchor aPos, LONG *plResult)
224 {
225 Range *This = impl_from_ITfRange(iface);
226 FIXME("STUB:(%p)\n",This);
227 return E_NOTIMPL;
228 }
229
230 static HRESULT WINAPI Range_CompareEnd(ITfRange *iface, TfEditCookie ec,
231 ITfRange *pWith, TfAnchor aPos, LONG *plResult)
232 {
233 Range *This = impl_from_ITfRange(iface);
234 FIXME("STUB:(%p)\n",This);
235 return E_NOTIMPL;
236 }
237
238 static HRESULT WINAPI Range_AdjustForInsert(ITfRange *iface, TfEditCookie ec,
239 ULONG cchInsert, BOOL *pfInsertOk)
240 {
241 Range *This = impl_from_ITfRange(iface);
242 FIXME("STUB:(%p)\n",This);
243 return E_NOTIMPL;
244 }
245
246 static HRESULT WINAPI Range_GetGravity(ITfRange *iface,
247 TfGravity *pgStart, TfGravity *pgEnd)
248 {
249 Range *This = impl_from_ITfRange(iface);
250 FIXME("STUB:(%p)\n",This);
251 return E_NOTIMPL;
252 }
253
254 static HRESULT WINAPI Range_SetGravity(ITfRange *iface, TfEditCookie ec,
255 TfGravity gStart, TfGravity gEnd)
256 {
257 Range *This = impl_from_ITfRange(iface);
258 FIXME("STUB:(%p)\n",This);
259 return E_NOTIMPL;
260 }
261
262 static HRESULT WINAPI Range_Clone(ITfRange *iface, ITfRange **ppClone)
263 {
264 Range *This = impl_from_ITfRange(iface);
265 FIXME("STUB:(%p)\n",This);
266 return E_NOTIMPL;
267 }
268
269 static HRESULT WINAPI Range_GetContext(ITfRange *iface, ITfContext **ppContext)
270 {
271 Range *This = impl_from_ITfRange(iface);
272 TRACE("(%p)\n",This);
273 if (!ppContext)
274 return E_INVALIDARG;
275 *ppContext = This->pITfContext;
276 return S_OK;
277 }
278
279 static const ITfRangeVtbl Range_RangeVtbl =
280 {
281 Range_QueryInterface,
282 Range_AddRef,
283 Range_Release,
284
285 Range_GetText,
286 Range_SetText,
287 Range_GetFormattedText,
288 Range_GetEmbedded,
289 Range_InsertEmbedded,
290 Range_ShiftStart,
291 Range_ShiftEnd,
292 Range_ShiftStartToRange,
293 Range_ShiftEndToRange,
294 Range_ShiftStartRegion,
295 Range_ShiftEndRegion,
296 Range_IsEmpty,
297 Range_Collapse,
298 Range_IsEqualStart,
299 Range_IsEqualEnd,
300 Range_CompareStart,
301 Range_CompareEnd,
302 Range_AdjustForInsert,
303 Range_GetGravity,
304 Range_SetGravity,
305 Range_Clone,
306 Range_GetContext
307 };
308
309 HRESULT Range_Constructor(ITfContext *context, ITextStoreACP *textstore, DWORD lockType, DWORD anchorStart, DWORD anchorEnd, ITfRange **ppOut)
310 {
311 Range *This;
312
313 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(Range));
314 if (This == NULL)
315 return E_OUTOFMEMORY;
316
317 TRACE("(%p) %p %p\n",This, context, textstore);
318
319 This->ITfRange_iface.lpVtbl = &Range_RangeVtbl;
320 This->refCount = 1;
321 This->pITfContext = context;
322 This->pITextStoreACP = textstore;
323 This->lockType = lockType;
324 This->anchorStart = anchorStart;
325 This->anchorEnd = anchorEnd;
326
327 *ppOut = &This->ITfRange_iface;
328 TRACE("returning %p\n", *ppOut);
329
330 return S_OK;
331 }
332
333 /* Internal conversion functions */
334
335 HRESULT TF_SELECTION_to_TS_SELECTION_ACP(const TF_SELECTION *tf, TS_SELECTION_ACP *tsAcp)
336 {
337 Range *This;
338
339 if (!tf || !tsAcp || !tf->range)
340 return E_INVALIDARG;
341
342 This = impl_from_ITfRange(tf->range);
343
344 tsAcp->acpStart = This->anchorStart;
345 tsAcp->acpEnd = This->anchorEnd;
346 tsAcp->style.ase = tf->style.ase;
347 tsAcp->style.fInterimChar = tf->style.fInterimChar;
348 return S_OK;
349 }