txtsrv.c 13.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * RichEdit - functions and interfaces around CreateTextServices
 *
 * Copyright 2005, 2006, Maarten Lankhorst
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 22 23 24 25 26 27 28 29
 */

#include "config.h"
#include "wine/port.h"

#define NONAMELESSSTRUCT
#define NONAMELESSUNION
#define COBJMACROS

#include "editor.h"
#include "ole2.h"
30
#include "oleauto.h"
31 32 33 34 35 36 37 38 39
#include "richole.h"
#include "imm.h"
#include "textserv.h"
#include "wine/debug.h"
#include "editstr.h"

#ifdef __i386__  /* thiscall functions are i386-specific */

#define THISCALL(func) __thiscall_ ## func
40
#define DEFINE_THISCALL_WRAPPER(func,args) \
41
   extern typeof(func) THISCALL(func); \
42
   __ASM_STDCALL_FUNC(__thiscall_ ## func, args, \
43 44 45
                   "popl %eax\n\t" \
                   "pushl %ecx\n\t" \
                   "pushl %eax\n\t" \
46
                   "jmp " __ASM_NAME(#func) __ASM_STDCALL(args) )
47 48 49
#else /* __i386__ */

#define THISCALL(func) func
50
#define DEFINE_THISCALL_WRAPPER(func,args) /* nothing */
51 52 53 54 55 56

#endif /* __i386__ */

WINE_DEFAULT_DEBUG_CHANNEL(richedit);

typedef struct ITextServicesImpl {
57
   IUnknown IUnknown_inner;
58
   ITextServices ITextServices_iface;
59
   IUnknown *outer_unk;
60
   LONG ref;
61
   ITextHost *pMyHost;
62
   CRITICAL_SECTION csTxtSrv;
63
   ME_TextEditor *editor;
64
   char spare[256];
65 66
} ITextServicesImpl;

67
static inline ITextServicesImpl *impl_from_IUnknown(IUnknown *iface)
68
{
69
   return CONTAINING_RECORD(iface, ITextServicesImpl, IUnknown_inner);
70
}
71

72
static HRESULT WINAPI ITextServicesImpl_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
73
{
74 75 76 77 78 79 80 81 82 83 84 85
   ITextServicesImpl *This = impl_from_IUnknown(iface);

   TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);

   if (IsEqualIID(riid, &IID_IUnknown))
      *ppv = &This->IUnknown_inner;
   else if IsEqualIID(riid, &IID_ITextServices)
      *ppv = &This->ITextServices_iface;
   else {
      *ppv = NULL;
      FIXME("Unknown interface: %s\n", debugstr_guid(riid));
      return E_NOINTERFACE;
86
   }
87 88 89

   IUnknown_AddRef((IUnknown*)*ppv);
   return S_OK;
90 91
}

92
static ULONG WINAPI ITextServicesImpl_AddRef(IUnknown *iface)
93
{
94 95 96 97
   ITextServicesImpl *This = impl_from_IUnknown(iface);
   LONG ref = InterlockedIncrement(&This->ref);

   TRACE("(%p) ref=%d\n", This, ref);
98 99 100 101

   return ref;
}

102
static ULONG WINAPI ITextServicesImpl_Release(IUnknown *iface)
103
{
104 105
   ITextServicesImpl *This = impl_from_IUnknown(iface);
   LONG ref = InterlockedDecrement(&This->ref);
106

107
   TRACE("(%p) ref=%d\n", This, ref);
108 109 110 111

   if (!ref)
   {
      ITextHost_Release(This->pMyHost);
112
      This->csTxtSrv.DebugInfo->Spare[0] = 0;
113 114 115 116 117 118
      DeleteCriticalSection(&This->csTxtSrv);
      CoTaskMemFree(This);
   }
   return ref;
}

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
static const IUnknownVtbl textservices_inner_vtbl =
{
   ITextServicesImpl_QueryInterface,
   ITextServicesImpl_AddRef,
   ITextServicesImpl_Release
};

static inline ITextServicesImpl *impl_from_ITextServices(ITextServices *iface)
{
   return CONTAINING_RECORD(iface, ITextServicesImpl, ITextServices_iface);
}

static HRESULT WINAPI fnTextSrv_QueryInterface(ITextServices *iface, REFIID riid, void **ppv)
{
   ITextServicesImpl *This = impl_from_ITextServices(iface);
   return IUnknown_QueryInterface(This->outer_unk, riid, ppv);
}

static ULONG WINAPI fnTextSrv_AddRef(ITextServices *iface)
{
   ITextServicesImpl *This = impl_from_ITextServices(iface);
   return IUnknown_AddRef(This->outer_unk);
}

static ULONG WINAPI fnTextSrv_Release(ITextServices *iface)
{
   ITextServicesImpl *This = impl_from_ITextServices(iface);
   return IUnknown_Release(This->outer_unk);
}

149
DECLSPEC_HIDDEN HRESULT WINAPI fnTextSrv_TxSendMessage(ITextServices *iface, UINT msg, WPARAM wparam,
150
                                       LPARAM lparam, LRESULT *plresult)
151
{
152
   ITextServicesImpl *This = impl_from_ITextServices(iface);
153
   HRESULT hresult;
154
   LRESULT lresult;
155

156 157
   lresult = ME_HandleMessage(This->editor, msg, wparam, lparam, TRUE, &hresult);
   if (plresult) *plresult = lresult;
158
   return hresult;
159 160
}

161
DECLSPEC_HIDDEN HRESULT WINAPI fnTextSrv_TxDraw(ITextServices *iface, DWORD dwDrawAspect, LONG lindex,
162 163 164
                                void *pvAspect, DVTARGETDEVICE *ptd, HDC hdcDraw, HDC hdcTargetDev,
                                LPCRECTL lprcBounds, LPCRECTL lprcWBounds, LPRECT lprcUpdate,
                                BOOL (CALLBACK * pfnContinue)(DWORD), DWORD dwContinue,
165 166
                                LONG lViewId)
{
167
   ITextServicesImpl *This = impl_from_ITextServices(iface);
168 169 170 171 172

   FIXME("%p: STUB\n", This);
   return E_NOTIMPL;
}

173
DECLSPEC_HIDDEN HRESULT WINAPI fnTextSrv_TxGetHScroll(ITextServices *iface, LONG *plMin, LONG *plMax, LONG *plPos,
174
                                      LONG *plPage, BOOL *pfEnabled)
175
{
176
   ITextServicesImpl *This = impl_from_ITextServices(iface);
177

178 179 180 181 182 183
   *plMin = This->editor->horz_si.nMin;
   *plMax = This->editor->horz_si.nMax;
   *plPos = This->editor->horz_si.nPos;
   *plPage = This->editor->horz_si.nPage;
   *pfEnabled = (This->editor->styleFlags & WS_HSCROLL) != 0;
   return S_OK;
184 185
}

186
DECLSPEC_HIDDEN HRESULT WINAPI fnTextSrv_TxGetVScroll(ITextServices *iface, LONG *plMin, LONG *plMax, LONG *plPos,
187
                                      LONG *plPage, BOOL *pfEnabled)
188
{
189
   ITextServicesImpl *This = impl_from_ITextServices(iface);
190

191 192 193 194 195 196
   *plMin = This->editor->vert_si.nMin;
   *plMax = This->editor->vert_si.nMax;
   *plPos = This->editor->vert_si.nPos;
   *plPage = This->editor->vert_si.nPage;
   *pfEnabled = (This->editor->styleFlags & WS_VSCROLL) != 0;
   return S_OK;
197 198
}

199
DECLSPEC_HIDDEN HRESULT WINAPI fnTextSrv_OnTxSetCursor(ITextServices *iface, DWORD dwDrawAspect, LONG lindex,
200 201
                                       void *pvAspect, DVTARGETDEVICE *ptd, HDC hdcDraw,
                                       HDC hicTargetDev, LPCRECT lprcClient, INT x, INT y)
202
{
203
   ITextServicesImpl *This = impl_from_ITextServices(iface);
204 205 206 207 208

   FIXME("%p: STUB\n", This);
   return E_NOTIMPL;
}

209
DECLSPEC_HIDDEN HRESULT WINAPI fnTextSrv_TxQueryHitPoint(ITextServices *iface, DWORD dwDrawAspect, LONG lindex,
210 211 212
                                         void *pvAspect, DVTARGETDEVICE *ptd, HDC hdcDraw,
                                         HDC hicTargetDev, LPCRECT lprcClient, INT x, INT y,
                                         DWORD *pHitResult)
213
{
214
   ITextServicesImpl *This = impl_from_ITextServices(iface);
215 216 217 218 219

   FIXME("%p: STUB\n", This);
   return E_NOTIMPL;
}

220
DECLSPEC_HIDDEN HRESULT WINAPI fnTextSrv_OnTxInplaceActivate(ITextServices *iface, LPCRECT prcClient)
221
{
222
   ITextServicesImpl *This = impl_from_ITextServices(iface);
223 224 225 226 227

   FIXME("%p: STUB\n", This);
   return E_NOTIMPL;
}

228
DECLSPEC_HIDDEN HRESULT WINAPI fnTextSrv_OnTxInplaceDeactivate(ITextServices *iface)
229
{
230
   ITextServicesImpl *This = impl_from_ITextServices(iface);
231 232 233 234 235

   FIXME("%p: STUB\n", This);
   return E_NOTIMPL;
}

236
DECLSPEC_HIDDEN HRESULT WINAPI fnTextSrv_OnTxUIActivate(ITextServices *iface)
237
{
238
   ITextServicesImpl *This = impl_from_ITextServices(iface);
239 240 241 242 243

   FIXME("%p: STUB\n", This);
   return E_NOTIMPL;
}

244
DECLSPEC_HIDDEN HRESULT WINAPI fnTextSrv_OnTxUIDeactivate(ITextServices *iface)
245
{
246
   ITextServicesImpl *This = impl_from_ITextServices(iface);
247 248 249 250 251

   FIXME("%p: STUB\n", This);
   return E_NOTIMPL;
}

252
DECLSPEC_HIDDEN HRESULT WINAPI fnTextSrv_TxGetText(ITextServices *iface, BSTR *pbstrText)
253
{
254
   ITextServicesImpl *This = impl_from_ITextServices(iface);
255
   int length;
256

257 258 259
   length = ME_GetTextLength(This->editor);
   if (length)
   {
260
      ME_Cursor start;
261 262 263 264 265
      BSTR bstr;
      bstr = SysAllocStringByteLen(NULL, length * sizeof(WCHAR));
      if (bstr == NULL)
         return E_OUTOFMEMORY;

266 267
      ME_CursorFromCharOfs(This->editor, 0, &start);
      ME_GetTextW(This->editor, bstr, length, &start, INT_MAX, FALSE);
268 269 270 271 272 273
      *pbstrText = bstr;
   } else {
      *pbstrText = NULL;
   }

   return S_OK;
274 275
}

276
DECLSPEC_HIDDEN HRESULT WINAPI fnTextSrv_TxSetText(ITextServices *iface, LPCWSTR pszText)
277
{
278
   ITextServicesImpl *This = impl_from_ITextServices(iface);
279
   ME_Cursor cursor;
280

281 282 283
   ME_SetCursorToStart(This->editor, &cursor);
   ME_InternalDeleteText(This->editor, &cursor,
                         ME_GetTextLength(This->editor), FALSE);
284 285 286 287 288 289
   ME_InsertTextFromCursor(This->editor, 0, pszText, -1,
                           This->editor->pBuffer->pDefaultStyle);
   ME_SetSelection(This->editor, 0, 0);
   This->editor->nModifyStep = 0;
   OleFlushClipboard();
   ME_EmptyUndoStack(This->editor);
290
   ME_UpdateRepaint(This->editor, FALSE);
291 292

   return S_OK;
293 294
}

295
DECLSPEC_HIDDEN HRESULT WINAPI fnTextSrv_TxGetCurrentTargetX(ITextServices *iface, LONG *x)
296
{
297
   ITextServicesImpl *This = impl_from_ITextServices(iface);
298 299 300 301 302

   FIXME("%p: STUB\n", This);
   return E_NOTIMPL;
}

303
DECLSPEC_HIDDEN HRESULT WINAPI fnTextSrv_TxGetBaseLinePos(ITextServices *iface, LONG *x)
304
{
305
   ITextServicesImpl *This = impl_from_ITextServices(iface);
306 307 308 309 310

   FIXME("%p: STUB\n", This);
   return E_NOTIMPL;
}

311
DECLSPEC_HIDDEN HRESULT WINAPI fnTextSrv_TxGetNaturalSize(ITextServices *iface, DWORD dwAspect, HDC hdcDraw,
312 313
                                          HDC hicTargetDev, DVTARGETDEVICE *ptd, DWORD dwMode,
                                          const SIZEL *psizelExtent, LONG *pwidth, LONG *pheight)
314
{
315
   ITextServicesImpl *This = impl_from_ITextServices(iface);
316 317 318 319 320

   FIXME("%p: STUB\n", This);
   return E_NOTIMPL;
}

321
DECLSPEC_HIDDEN HRESULT WINAPI fnTextSrv_TxGetDropTarget(ITextServices *iface, IDropTarget **ppDropTarget)
322
{
323
   ITextServicesImpl *This = impl_from_ITextServices(iface);
324 325 326 327 328

   FIXME("%p: STUB\n", This);
   return E_NOTIMPL;
}

329
DECLSPEC_HIDDEN HRESULT WINAPI fnTextSrv_OnTxPropertyBitsChange(ITextServices *iface, DWORD dwMask, DWORD dwBits)
330
{
331
   ITextServicesImpl *This = impl_from_ITextServices(iface);
332 333 334 335 336

   FIXME("%p: STUB\n", This);
   return E_NOTIMPL;
}

337
DECLSPEC_HIDDEN HRESULT WINAPI fnTextSrv_TxGetCachedSize(ITextServices *iface, DWORD *pdwWidth, DWORD *pdwHeight)
338
{
339
   ITextServicesImpl *This = impl_from_ITextServices(iface);
340 341 342 343 344

   FIXME("%p: STUB\n", This);
   return E_NOTIMPL;
}

345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
DEFINE_THISCALL_WRAPPER(fnTextSrv_TxSendMessage,20)
DEFINE_THISCALL_WRAPPER(fnTextSrv_TxDraw,52)
DEFINE_THISCALL_WRAPPER(fnTextSrv_TxGetHScroll,24)
DEFINE_THISCALL_WRAPPER(fnTextSrv_TxGetVScroll,24)
DEFINE_THISCALL_WRAPPER(fnTextSrv_OnTxSetCursor,40)
DEFINE_THISCALL_WRAPPER(fnTextSrv_TxQueryHitPoint,44)
DEFINE_THISCALL_WRAPPER(fnTextSrv_OnTxInplaceActivate,8)
DEFINE_THISCALL_WRAPPER(fnTextSrv_OnTxInplaceDeactivate,4)
DEFINE_THISCALL_WRAPPER(fnTextSrv_OnTxUIActivate,4)
DEFINE_THISCALL_WRAPPER(fnTextSrv_OnTxUIDeactivate,4)
DEFINE_THISCALL_WRAPPER(fnTextSrv_TxGetText,8)
DEFINE_THISCALL_WRAPPER(fnTextSrv_TxSetText,8)
DEFINE_THISCALL_WRAPPER(fnTextSrv_TxGetCurrentTargetX,8)
DEFINE_THISCALL_WRAPPER(fnTextSrv_TxGetBaseLinePos,8)
DEFINE_THISCALL_WRAPPER(fnTextSrv_TxGetNaturalSize,36)
DEFINE_THISCALL_WRAPPER(fnTextSrv_TxGetDropTarget,8)
DEFINE_THISCALL_WRAPPER(fnTextSrv_OnTxPropertyBitsChange,12)
DEFINE_THISCALL_WRAPPER(fnTextSrv_TxGetCachedSize,12)
363

364
static const ITextServicesVtbl textservices_vtbl =
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
{
   fnTextSrv_QueryInterface,
   fnTextSrv_AddRef,
   fnTextSrv_Release,
   THISCALL(fnTextSrv_TxSendMessage),
   THISCALL(fnTextSrv_TxDraw),
   THISCALL(fnTextSrv_TxGetHScroll),
   THISCALL(fnTextSrv_TxGetVScroll),
   THISCALL(fnTextSrv_OnTxSetCursor),
   THISCALL(fnTextSrv_TxQueryHitPoint),
   THISCALL(fnTextSrv_OnTxInplaceActivate),
   THISCALL(fnTextSrv_OnTxInplaceDeactivate),
   THISCALL(fnTextSrv_OnTxUIActivate),
   THISCALL(fnTextSrv_OnTxUIDeactivate),
   THISCALL(fnTextSrv_TxGetText),
   THISCALL(fnTextSrv_TxSetText),
   THISCALL(fnTextSrv_TxGetCurrentTargetX),
   THISCALL(fnTextSrv_TxGetBaseLinePos),
   THISCALL(fnTextSrv_TxGetNaturalSize),
   THISCALL(fnTextSrv_TxGetDropTarget),
   THISCALL(fnTextSrv_OnTxPropertyBitsChange),
   THISCALL(fnTextSrv_TxGetCachedSize)
};
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407

/******************************************************************
 *        CreateTextServices (RICHED20.4)
 */
HRESULT WINAPI CreateTextServices(IUnknown  *pUnkOuter, ITextHost *pITextHost, IUnknown  **ppUnk)
{
   ITextServicesImpl *ITextImpl;
   HRESULT hres;
   TRACE("%p %p --> %p\n", pUnkOuter, pITextHost, ppUnk);
   if (pITextHost == NULL)
      return E_POINTER;

   ITextImpl = CoTaskMemAlloc(sizeof(*ITextImpl));
   if (ITextImpl == NULL)
      return E_OUTOFMEMORY;
   InitializeCriticalSection(&ITextImpl->csTxtSrv);
   ITextImpl->csTxtSrv.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ITextServicesImpl.csTxtSrv");
   ITextImpl->ref = 1;
   ITextHost_AddRef(pITextHost);
   ITextImpl->pMyHost = pITextHost;
408
   ITextImpl->IUnknown_inner.lpVtbl = &textservices_inner_vtbl;
409 410 411 412 413 414 415 416 417 418 419
   ITextImpl->ITextServices_iface.lpVtbl = &textservices_vtbl;
   ITextImpl->editor = ME_MakeEditor(pITextHost, FALSE);
   ITextImpl->editor->exStyleFlags = 0;
   ITextImpl->editor->rcFormat.left = 0;
   ITextImpl->editor->rcFormat.top = 0;
   ITextImpl->editor->rcFormat.right = 0;
   ITextImpl->editor->rcFormat.bottom = 0;

   ME_HandleMessage(ITextImpl->editor, WM_CREATE, 0, 0, TRUE, &hres);

   if (pUnkOuter)
420 421 422
      ITextImpl->outer_unk = pUnkOuter;
   else
      ITextImpl->outer_unk = &ITextImpl->IUnknown_inner;
423

424
   *ppUnk = &ITextImpl->IUnknown_inner;
425 426
   return S_OK;
}