Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef NSTEXTSTORE_H_ |
michael@0 | 7 | #define NSTEXTSTORE_H_ |
michael@0 | 8 | |
michael@0 | 9 | #include "nsAutoPtr.h" |
michael@0 | 10 | #include "nsString.h" |
michael@0 | 11 | #include "nsCOMPtr.h" |
michael@0 | 12 | #include "nsIWidget.h" |
michael@0 | 13 | #include "nsWindowBase.h" |
michael@0 | 14 | #include "mozilla/Attributes.h" |
michael@0 | 15 | #include "mozilla/TextRange.h" |
michael@0 | 16 | #include "mozilla/WindowsVersion.h" |
michael@0 | 17 | |
michael@0 | 18 | #include <msctf.h> |
michael@0 | 19 | #include <textstor.h> |
michael@0 | 20 | |
michael@0 | 21 | // GUID_PROP_INPUTSCOPE is declared in inputscope.h using INIT_GUID. |
michael@0 | 22 | // With initguid.h, we get its instance instead of extern declaration. |
michael@0 | 23 | #ifdef INPUTSCOPE_INIT_GUID |
michael@0 | 24 | #include <initguid.h> |
michael@0 | 25 | #endif |
michael@0 | 26 | #include <inputscope.h> |
michael@0 | 27 | |
michael@0 | 28 | // TSF InputScope, for earlier SDK 8 |
michael@0 | 29 | #define IS_SEARCH static_cast<InputScope>(50) |
michael@0 | 30 | |
michael@0 | 31 | struct ITfThreadMgr; |
michael@0 | 32 | struct ITfDocumentMgr; |
michael@0 | 33 | struct ITfDisplayAttributeMgr; |
michael@0 | 34 | struct ITfCategoryMgr; |
michael@0 | 35 | class nsWindow; |
michael@0 | 36 | #ifdef MOZ_METRO |
michael@0 | 37 | class MetroWidget; |
michael@0 | 38 | #endif |
michael@0 | 39 | |
michael@0 | 40 | namespace mozilla { |
michael@0 | 41 | namespace widget { |
michael@0 | 42 | struct MSGResult; |
michael@0 | 43 | } // namespace widget |
michael@0 | 44 | } // namespace mozilla |
michael@0 | 45 | |
michael@0 | 46 | /* |
michael@0 | 47 | * Text Services Framework text store |
michael@0 | 48 | */ |
michael@0 | 49 | |
michael@0 | 50 | class nsTextStore MOZ_FINAL : public ITextStoreACP, |
michael@0 | 51 | public ITfContextOwnerCompositionSink, |
michael@0 | 52 | public ITfActiveLanguageProfileNotifySink, |
michael@0 | 53 | public ITfInputProcessorProfileActivationSink |
michael@0 | 54 | { |
michael@0 | 55 | public: /*IUnknown*/ |
michael@0 | 56 | STDMETHODIMP_(ULONG) AddRef(void); |
michael@0 | 57 | STDMETHODIMP QueryInterface(REFIID, void**); |
michael@0 | 58 | STDMETHODIMP_(ULONG) Release(void); |
michael@0 | 59 | |
michael@0 | 60 | public: /*ITextStoreACP*/ |
michael@0 | 61 | STDMETHODIMP AdviseSink(REFIID, IUnknown*, DWORD); |
michael@0 | 62 | STDMETHODIMP UnadviseSink(IUnknown*); |
michael@0 | 63 | STDMETHODIMP RequestLock(DWORD, HRESULT*); |
michael@0 | 64 | STDMETHODIMP GetStatus(TS_STATUS*); |
michael@0 | 65 | STDMETHODIMP QueryInsert(LONG, LONG, ULONG, LONG*, LONG*); |
michael@0 | 66 | STDMETHODIMP GetSelection(ULONG, ULONG, TS_SELECTION_ACP*, ULONG*); |
michael@0 | 67 | STDMETHODIMP SetSelection(ULONG, const TS_SELECTION_ACP*); |
michael@0 | 68 | STDMETHODIMP GetText(LONG, LONG, WCHAR*, ULONG, ULONG*, TS_RUNINFO*, ULONG, |
michael@0 | 69 | ULONG*, LONG*); |
michael@0 | 70 | STDMETHODIMP SetText(DWORD, LONG, LONG, const WCHAR*, ULONG, TS_TEXTCHANGE*); |
michael@0 | 71 | STDMETHODIMP GetFormattedText(LONG, LONG, IDataObject**); |
michael@0 | 72 | STDMETHODIMP GetEmbedded(LONG, REFGUID, REFIID, IUnknown**); |
michael@0 | 73 | STDMETHODIMP QueryInsertEmbedded(const GUID*, const FORMATETC*, BOOL*); |
michael@0 | 74 | STDMETHODIMP InsertEmbedded(DWORD, LONG, LONG, IDataObject*, TS_TEXTCHANGE*); |
michael@0 | 75 | STDMETHODIMP RequestSupportedAttrs(DWORD, ULONG, const TS_ATTRID*); |
michael@0 | 76 | STDMETHODIMP RequestAttrsAtPosition(LONG, ULONG, const TS_ATTRID*, DWORD); |
michael@0 | 77 | STDMETHODIMP RequestAttrsTransitioningAtPosition(LONG, ULONG, |
michael@0 | 78 | const TS_ATTRID*, DWORD); |
michael@0 | 79 | STDMETHODIMP FindNextAttrTransition(LONG, LONG, ULONG, const TS_ATTRID*, |
michael@0 | 80 | DWORD, LONG*, BOOL*, LONG*); |
michael@0 | 81 | STDMETHODIMP RetrieveRequestedAttrs(ULONG, TS_ATTRVAL*, ULONG*); |
michael@0 | 82 | STDMETHODIMP GetEndACP(LONG*); |
michael@0 | 83 | STDMETHODIMP GetActiveView(TsViewCookie*); |
michael@0 | 84 | STDMETHODIMP GetACPFromPoint(TsViewCookie, const POINT*, DWORD, LONG*); |
michael@0 | 85 | STDMETHODIMP GetTextExt(TsViewCookie, LONG, LONG, RECT*, BOOL*); |
michael@0 | 86 | STDMETHODIMP GetScreenExt(TsViewCookie, RECT*); |
michael@0 | 87 | STDMETHODIMP GetWnd(TsViewCookie, HWND*); |
michael@0 | 88 | STDMETHODIMP InsertTextAtSelection(DWORD, const WCHAR*, ULONG, LONG*, LONG*, |
michael@0 | 89 | TS_TEXTCHANGE*); |
michael@0 | 90 | STDMETHODIMP InsertEmbeddedAtSelection(DWORD, IDataObject*, LONG*, LONG*, |
michael@0 | 91 | TS_TEXTCHANGE*); |
michael@0 | 92 | |
michael@0 | 93 | public: /*ITfContextOwnerCompositionSink*/ |
michael@0 | 94 | STDMETHODIMP OnStartComposition(ITfCompositionView*, BOOL*); |
michael@0 | 95 | STDMETHODIMP OnUpdateComposition(ITfCompositionView*, ITfRange*); |
michael@0 | 96 | STDMETHODIMP OnEndComposition(ITfCompositionView*); |
michael@0 | 97 | |
michael@0 | 98 | public: /*ITfActiveLanguageProfileNotifySink*/ |
michael@0 | 99 | STDMETHODIMP OnActivated(REFCLSID clsid, REFGUID guidProfile, |
michael@0 | 100 | BOOL fActivated); |
michael@0 | 101 | |
michael@0 | 102 | public: /*ITfInputProcessorProfileActivationSink*/ |
michael@0 | 103 | STDMETHODIMP OnActivated(DWORD, LANGID, REFCLSID, REFGUID, REFGUID, |
michael@0 | 104 | HKL, DWORD); |
michael@0 | 105 | |
michael@0 | 106 | protected: |
michael@0 | 107 | typedef mozilla::widget::IMENotification IMENotification; |
michael@0 | 108 | typedef mozilla::widget::IMEState IMEState; |
michael@0 | 109 | typedef mozilla::widget::InputContext InputContext; |
michael@0 | 110 | typedef mozilla::widget::InputContextAction InputContextAction; |
michael@0 | 111 | |
michael@0 | 112 | public: |
michael@0 | 113 | static void Initialize(void); |
michael@0 | 114 | static void Terminate(void); |
michael@0 | 115 | |
michael@0 | 116 | static bool ProcessRawKeyMessage(const MSG& aMsg); |
michael@0 | 117 | static void ProcessMessage(nsWindowBase* aWindow, UINT aMessage, |
michael@0 | 118 | WPARAM& aWParam, LPARAM& aLParam, |
michael@0 | 119 | mozilla::widget::MSGResult& aResult); |
michael@0 | 120 | |
michael@0 | 121 | |
michael@0 | 122 | static void SetIMEOpenState(bool); |
michael@0 | 123 | static bool GetIMEOpenState(void); |
michael@0 | 124 | |
michael@0 | 125 | static void CommitComposition(bool aDiscard) |
michael@0 | 126 | { |
michael@0 | 127 | NS_ENSURE_TRUE_VOID(sTsfTextStore); |
michael@0 | 128 | sTsfTextStore->CommitCompositionInternal(aDiscard); |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | static void SetInputContext(nsWindowBase* aWidget, |
michael@0 | 132 | const InputContext& aContext, |
michael@0 | 133 | const InputContextAction& aAction); |
michael@0 | 134 | |
michael@0 | 135 | static nsresult OnFocusChange(bool aGotFocus, |
michael@0 | 136 | nsWindowBase* aFocusedWidget, |
michael@0 | 137 | IMEState::Enabled aIMEEnabled); |
michael@0 | 138 | static nsresult OnTextChange(const IMENotification& aIMENotification) |
michael@0 | 139 | { |
michael@0 | 140 | NS_ENSURE_TRUE(sTsfTextStore, NS_ERROR_NOT_AVAILABLE); |
michael@0 | 141 | return sTsfTextStore->OnTextChangeInternal(aIMENotification); |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | static nsresult OnSelectionChange(void) |
michael@0 | 145 | { |
michael@0 | 146 | NS_ENSURE_TRUE(sTsfTextStore, NS_ERROR_NOT_AVAILABLE); |
michael@0 | 147 | return sTsfTextStore->OnSelectionChangeInternal(); |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | static nsresult OnLayoutChange() |
michael@0 | 151 | { |
michael@0 | 152 | NS_ENSURE_TRUE(sTsfTextStore, NS_ERROR_NOT_AVAILABLE); |
michael@0 | 153 | return sTsfTextStore->OnLayoutChangeInternal(); |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | static nsIMEUpdatePreference GetIMEUpdatePreference(); |
michael@0 | 157 | |
michael@0 | 158 | // Returns the address of the pointer so that the TSF automatic test can |
michael@0 | 159 | // replace the system object with a custom implementation for testing. |
michael@0 | 160 | static void* GetNativeData(uint32_t aDataType) |
michael@0 | 161 | { |
michael@0 | 162 | switch (aDataType) { |
michael@0 | 163 | case NS_NATIVE_TSF_THREAD_MGR: |
michael@0 | 164 | Initialize(); // Apply any previous changes |
michael@0 | 165 | return static_cast<void*>(&sTsfThreadMgr); |
michael@0 | 166 | case NS_NATIVE_TSF_CATEGORY_MGR: |
michael@0 | 167 | return static_cast<void*>(&sCategoryMgr); |
michael@0 | 168 | case NS_NATIVE_TSF_DISPLAY_ATTR_MGR: |
michael@0 | 169 | return static_cast<void*>(&sDisplayAttrMgr); |
michael@0 | 170 | default: |
michael@0 | 171 | return nullptr; |
michael@0 | 172 | } |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | static ITfMessagePump* GetMessagePump() |
michael@0 | 176 | { |
michael@0 | 177 | return sMessagePump; |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | static void* GetTextStore() |
michael@0 | 181 | { |
michael@0 | 182 | return static_cast<void*>(sTsfTextStore); |
michael@0 | 183 | } |
michael@0 | 184 | |
michael@0 | 185 | static bool ThinksHavingFocus() |
michael@0 | 186 | { |
michael@0 | 187 | return (sTsfTextStore && sTsfTextStore->mContext); |
michael@0 | 188 | } |
michael@0 | 189 | |
michael@0 | 190 | static bool IsInTSFMode() |
michael@0 | 191 | { |
michael@0 | 192 | return sTsfThreadMgr != nullptr; |
michael@0 | 193 | } |
michael@0 | 194 | |
michael@0 | 195 | static bool IsComposing() |
michael@0 | 196 | { |
michael@0 | 197 | return (sTsfTextStore && sTsfTextStore->mComposition.IsComposing()); |
michael@0 | 198 | } |
michael@0 | 199 | |
michael@0 | 200 | static bool IsComposingOn(nsWindowBase* aWidget) |
michael@0 | 201 | { |
michael@0 | 202 | return (IsComposing() && sTsfTextStore->mWidget == aWidget); |
michael@0 | 203 | } |
michael@0 | 204 | |
michael@0 | 205 | static bool IsIMM_IME() |
michael@0 | 206 | { |
michael@0 | 207 | if (!sTsfTextStore || !sTsfTextStore->EnsureInitActiveTIPKeyboard()) { |
michael@0 | 208 | return IsIMM_IME(::GetKeyboardLayout(0)); |
michael@0 | 209 | } |
michael@0 | 210 | return sTsfTextStore->mIsIMM_IME; |
michael@0 | 211 | } |
michael@0 | 212 | |
michael@0 | 213 | static bool IsIMM_IME(HKL aHKL) |
michael@0 | 214 | { |
michael@0 | 215 | return (::ImmGetIMEFileNameW(aHKL, nullptr, 0) > 0); |
michael@0 | 216 | } |
michael@0 | 217 | |
michael@0 | 218 | #ifdef DEBUG |
michael@0 | 219 | // Returns true when keyboard layout has IME (TIP). |
michael@0 | 220 | static bool CurrentKeyboardLayoutHasIME(); |
michael@0 | 221 | #endif // #ifdef DEBUG |
michael@0 | 222 | |
michael@0 | 223 | protected: |
michael@0 | 224 | nsTextStore(); |
michael@0 | 225 | ~nsTextStore(); |
michael@0 | 226 | |
michael@0 | 227 | bool Init(ITfThreadMgr* aThreadMgr); |
michael@0 | 228 | |
michael@0 | 229 | static void MarkContextAsKeyboardDisabled(ITfContext* aContext); |
michael@0 | 230 | static void MarkContextAsEmpty(ITfContext* aContext); |
michael@0 | 231 | |
michael@0 | 232 | static bool IsTIPCategoryKeyboard(REFCLSID aTextService, LANGID aLangID, |
michael@0 | 233 | REFGUID aProfile); |
michael@0 | 234 | static void GetTIPDescription(REFCLSID aTextService, LANGID aLangID, |
michael@0 | 235 | REFGUID aProfile, nsAString& aDescription); |
michael@0 | 236 | |
michael@0 | 237 | bool Create(nsWindowBase* aWidget); |
michael@0 | 238 | bool Destroy(void); |
michael@0 | 239 | |
michael@0 | 240 | bool IsReadLock(DWORD aLock) const |
michael@0 | 241 | { |
michael@0 | 242 | return (TS_LF_READ == (aLock & TS_LF_READ)); |
michael@0 | 243 | } |
michael@0 | 244 | bool IsReadWriteLock(DWORD aLock) const |
michael@0 | 245 | { |
michael@0 | 246 | return (TS_LF_READWRITE == (aLock & TS_LF_READWRITE)); |
michael@0 | 247 | } |
michael@0 | 248 | bool IsReadLocked() const { return IsReadLock(mLock); } |
michael@0 | 249 | bool IsReadWriteLocked() const { return IsReadWriteLock(mLock); } |
michael@0 | 250 | |
michael@0 | 251 | // This is called immediately after a call of OnLockGranted() of mSink. |
michael@0 | 252 | // Note that mLock isn't cleared yet when this is called. |
michael@0 | 253 | void DidLockGranted(); |
michael@0 | 254 | |
michael@0 | 255 | bool GetScreenExtInternal(RECT &aScreenExt); |
michael@0 | 256 | // If aDispatchTextEvent is true, this method will dispatch text event if |
michael@0 | 257 | // this is called during IME composing. aDispatchTextEvent should be true |
michael@0 | 258 | // only when this is called from SetSelection. Because otherwise, the text |
michael@0 | 259 | // event should not be sent from here. |
michael@0 | 260 | HRESULT SetSelectionInternal(const TS_SELECTION_ACP*, |
michael@0 | 261 | bool aDispatchTextEvent = false); |
michael@0 | 262 | bool InsertTextAtSelectionInternal(const nsAString &aInsertStr, |
michael@0 | 263 | TS_TEXTCHANGE* aTextChange); |
michael@0 | 264 | void CommitCompositionInternal(bool); |
michael@0 | 265 | nsresult OnTextChangeInternal(const IMENotification& aIMENotification); |
michael@0 | 266 | nsresult OnSelectionChangeInternal(void); |
michael@0 | 267 | HRESULT GetDisplayAttribute(ITfProperty* aProperty, |
michael@0 | 268 | ITfRange* aRange, |
michael@0 | 269 | TF_DISPLAYATTRIBUTE* aResult); |
michael@0 | 270 | HRESULT RestartCompositionIfNecessary(ITfRange* pRangeNew = nullptr); |
michael@0 | 271 | |
michael@0 | 272 | // Following methods record composing action(s) to mPendingActions. |
michael@0 | 273 | // They will be flushed FlushPendingActions(). |
michael@0 | 274 | HRESULT RecordCompositionStartAction(ITfCompositionView* aCompositionView, |
michael@0 | 275 | ITfRange* aRange, |
michael@0 | 276 | bool aPreserveSelection); |
michael@0 | 277 | HRESULT RecordCompositionUpdateAction(); |
michael@0 | 278 | HRESULT RecordCompositionEndAction(); |
michael@0 | 279 | |
michael@0 | 280 | // FlushPendingActions() performs pending actions recorded in mPendingActions |
michael@0 | 281 | // and clear it. |
michael@0 | 282 | void FlushPendingActions(); |
michael@0 | 283 | |
michael@0 | 284 | nsresult OnLayoutChangeInternal(); |
michael@0 | 285 | HRESULT ProcessScopeRequest(DWORD dwFlags, |
michael@0 | 286 | ULONG cFilterAttrs, |
michael@0 | 287 | const TS_ATTRID *paFilterAttrs); |
michael@0 | 288 | void SetInputScope(const nsString& aHTMLInputType); |
michael@0 | 289 | |
michael@0 | 290 | // Creates native caret over our caret. This method only works on desktop |
michael@0 | 291 | // application. Otherwise, this does nothing. |
michael@0 | 292 | void CreateNativeCaret(); |
michael@0 | 293 | |
michael@0 | 294 | bool EnsureInitActiveTIPKeyboard(); |
michael@0 | 295 | |
michael@0 | 296 | // Holds the pointer to our current win32 or metro widget |
michael@0 | 297 | nsRefPtr<nsWindowBase> mWidget; |
michael@0 | 298 | // Document manager for the currently focused editor |
michael@0 | 299 | nsRefPtr<ITfDocumentMgr> mDocumentMgr; |
michael@0 | 300 | // Edit cookie associated with the current editing context |
michael@0 | 301 | DWORD mEditCookie; |
michael@0 | 302 | // Cookie of installing ITfInputProcessorProfileActivationSink |
michael@0 | 303 | DWORD mIPProfileCookie; |
michael@0 | 304 | // Cookie of installing ITfActiveLanguageProfileNotifySink |
michael@0 | 305 | DWORD mLangProfileCookie; |
michael@0 | 306 | // Editing context at the bottom of mDocumentMgr's context stack |
michael@0 | 307 | nsRefPtr<ITfContext> mContext; |
michael@0 | 308 | // Currently installed notification sink |
michael@0 | 309 | nsRefPtr<ITextStoreACPSink> mSink; |
michael@0 | 310 | // TS_AS_* mask of what events to notify |
michael@0 | 311 | DWORD mSinkMask; |
michael@0 | 312 | // 0 if not locked, otherwise TS_LF_* indicating the current lock |
michael@0 | 313 | DWORD mLock; |
michael@0 | 314 | // 0 if no lock is queued, otherwise TS_LF_* indicating the queue lock |
michael@0 | 315 | DWORD mLockQueued; |
michael@0 | 316 | // Active TIP keyboard's description. If active language profile isn't TIP, |
michael@0 | 317 | // i.e., IMM-IME or just a keyboard layout, this is empty. |
michael@0 | 318 | nsString mActiveTIPKeyboardDescription; |
michael@0 | 319 | |
michael@0 | 320 | class Composition MOZ_FINAL |
michael@0 | 321 | { |
michael@0 | 322 | public: |
michael@0 | 323 | // nullptr if no composition is active, otherwise the current composition |
michael@0 | 324 | nsRefPtr<ITfCompositionView> mView; |
michael@0 | 325 | |
michael@0 | 326 | // Current copy of the active composition string. Only mString is |
michael@0 | 327 | // changed during a InsertTextAtSelection call if we have a composition. |
michael@0 | 328 | // mString acts as a buffer until OnUpdateComposition is called |
michael@0 | 329 | // and mString is flushed to editor through NS_TEXT_TEXT. This |
michael@0 | 330 | // way all changes are updated in batches to avoid |
michael@0 | 331 | // inconsistencies/artifacts. |
michael@0 | 332 | nsString mString; |
michael@0 | 333 | |
michael@0 | 334 | // The latest composition string which was dispatched by composition update |
michael@0 | 335 | // event. |
michael@0 | 336 | nsString mLastData; |
michael@0 | 337 | |
michael@0 | 338 | // The start of the current active composition, in ACP offsets |
michael@0 | 339 | LONG mStart; |
michael@0 | 340 | |
michael@0 | 341 | bool IsComposing() const |
michael@0 | 342 | { |
michael@0 | 343 | return (mView != nullptr); |
michael@0 | 344 | } |
michael@0 | 345 | |
michael@0 | 346 | LONG EndOffset() const |
michael@0 | 347 | { |
michael@0 | 348 | return mStart + static_cast<LONG>(mString.Length()); |
michael@0 | 349 | } |
michael@0 | 350 | |
michael@0 | 351 | // Start() and End() updates the members for emulating the latest state. |
michael@0 | 352 | // Unless flush the pending actions, this data never matches the actual |
michael@0 | 353 | // content. |
michael@0 | 354 | void Start(ITfCompositionView* aCompositionView, |
michael@0 | 355 | LONG aCompositionStartOffset, |
michael@0 | 356 | const nsAString& aCompositionString); |
michael@0 | 357 | void End(); |
michael@0 | 358 | }; |
michael@0 | 359 | // While the document is locked, we cannot dispatch any events which cause |
michael@0 | 360 | // DOM events since the DOM events' handlers may modify the locked document. |
michael@0 | 361 | // However, even while the document is locked, TSF may queries us. |
michael@0 | 362 | // For that, nsTextStore modifies mComposition even while the document is |
michael@0 | 363 | // locked. With mComposition, query methods can returns the text content |
michael@0 | 364 | // information. |
michael@0 | 365 | Composition mComposition; |
michael@0 | 366 | |
michael@0 | 367 | class Selection |
michael@0 | 368 | { |
michael@0 | 369 | public: |
michael@0 | 370 | Selection() : mDirty(true) {} |
michael@0 | 371 | |
michael@0 | 372 | bool IsDirty() const { return mDirty; }; |
michael@0 | 373 | void MarkDirty() { mDirty = true; } |
michael@0 | 374 | |
michael@0 | 375 | TS_SELECTION_ACP& ACP() |
michael@0 | 376 | { |
michael@0 | 377 | MOZ_ASSERT(!mDirty); |
michael@0 | 378 | return mACP; |
michael@0 | 379 | } |
michael@0 | 380 | |
michael@0 | 381 | void SetSelection(const TS_SELECTION_ACP& aSelection) |
michael@0 | 382 | { |
michael@0 | 383 | mDirty = false; |
michael@0 | 384 | mACP = aSelection; |
michael@0 | 385 | // Selection end must be active in our editor. |
michael@0 | 386 | if (mACP.style.ase != TS_AE_START) { |
michael@0 | 387 | mACP.style.ase = TS_AE_END; |
michael@0 | 388 | } |
michael@0 | 389 | // We're not support interim char selection for now. |
michael@0 | 390 | // XXX Probably, this is necessary for supporting South Asian languages. |
michael@0 | 391 | mACP.style.fInterimChar = FALSE; |
michael@0 | 392 | } |
michael@0 | 393 | |
michael@0 | 394 | void SetSelection(uint32_t aStart, uint32_t aLength, bool aReversed) |
michael@0 | 395 | { |
michael@0 | 396 | mDirty = false; |
michael@0 | 397 | mACP.acpStart = static_cast<LONG>(aStart); |
michael@0 | 398 | mACP.acpEnd = static_cast<LONG>(aStart + aLength); |
michael@0 | 399 | mACP.style.ase = aReversed ? TS_AE_START : TS_AE_END; |
michael@0 | 400 | mACP.style.fInterimChar = FALSE; |
michael@0 | 401 | } |
michael@0 | 402 | |
michael@0 | 403 | bool IsCollapsed() const |
michael@0 | 404 | { |
michael@0 | 405 | MOZ_ASSERT(!mDirty); |
michael@0 | 406 | return (mACP.acpStart == mACP.acpEnd); |
michael@0 | 407 | } |
michael@0 | 408 | |
michael@0 | 409 | void CollapseAt(uint32_t aOffset) |
michael@0 | 410 | { |
michael@0 | 411 | mDirty = false; |
michael@0 | 412 | mACP.acpStart = mACP.acpEnd = static_cast<LONG>(aOffset); |
michael@0 | 413 | mACP.style.ase = TS_AE_END; |
michael@0 | 414 | mACP.style.fInterimChar = FALSE; |
michael@0 | 415 | } |
michael@0 | 416 | |
michael@0 | 417 | LONG MinOffset() const |
michael@0 | 418 | { |
michael@0 | 419 | MOZ_ASSERT(!mDirty); |
michael@0 | 420 | LONG min = std::min(mACP.acpStart, mACP.acpEnd); |
michael@0 | 421 | MOZ_ASSERT(min >= 0); |
michael@0 | 422 | return min; |
michael@0 | 423 | } |
michael@0 | 424 | |
michael@0 | 425 | LONG MaxOffset() const |
michael@0 | 426 | { |
michael@0 | 427 | MOZ_ASSERT(!mDirty); |
michael@0 | 428 | LONG max = std::max(mACP.acpStart, mACP.acpEnd); |
michael@0 | 429 | MOZ_ASSERT(max >= 0); |
michael@0 | 430 | return max; |
michael@0 | 431 | } |
michael@0 | 432 | |
michael@0 | 433 | LONG StartOffset() const |
michael@0 | 434 | { |
michael@0 | 435 | MOZ_ASSERT(!mDirty); |
michael@0 | 436 | MOZ_ASSERT(mACP.acpStart >= 0); |
michael@0 | 437 | return mACP.acpStart; |
michael@0 | 438 | } |
michael@0 | 439 | |
michael@0 | 440 | LONG EndOffset() const |
michael@0 | 441 | { |
michael@0 | 442 | MOZ_ASSERT(!mDirty); |
michael@0 | 443 | MOZ_ASSERT(mACP.acpEnd >= 0); |
michael@0 | 444 | return mACP.acpEnd; |
michael@0 | 445 | } |
michael@0 | 446 | |
michael@0 | 447 | LONG Length() const |
michael@0 | 448 | { |
michael@0 | 449 | MOZ_ASSERT(!mDirty); |
michael@0 | 450 | MOZ_ASSERT(mACP.acpEnd >= mACP.acpStart); |
michael@0 | 451 | return std::abs(mACP.acpEnd - mACP.acpStart); |
michael@0 | 452 | } |
michael@0 | 453 | |
michael@0 | 454 | bool IsReversed() const |
michael@0 | 455 | { |
michael@0 | 456 | MOZ_ASSERT(!mDirty); |
michael@0 | 457 | return (mACP.style.ase == TS_AE_START); |
michael@0 | 458 | } |
michael@0 | 459 | |
michael@0 | 460 | TsActiveSelEnd ActiveSelEnd() const |
michael@0 | 461 | { |
michael@0 | 462 | MOZ_ASSERT(!mDirty); |
michael@0 | 463 | return mACP.style.ase; |
michael@0 | 464 | } |
michael@0 | 465 | |
michael@0 | 466 | bool IsInterimChar() const |
michael@0 | 467 | { |
michael@0 | 468 | MOZ_ASSERT(!mDirty); |
michael@0 | 469 | return (mACP.style.fInterimChar != FALSE); |
michael@0 | 470 | } |
michael@0 | 471 | |
michael@0 | 472 | private: |
michael@0 | 473 | TS_SELECTION_ACP mACP; |
michael@0 | 474 | bool mDirty; |
michael@0 | 475 | }; |
michael@0 | 476 | // Don't access mSelection directly except at calling MarkDirty(). |
michael@0 | 477 | // Use CurrentSelection() instead. This is marked as dirty when the |
michael@0 | 478 | // selection or content is changed without document lock. |
michael@0 | 479 | Selection mSelection; |
michael@0 | 480 | |
michael@0 | 481 | // Get "current selection" while the document is locked. The selection is |
michael@0 | 482 | // NOT modified immediately during document lock. The pending changes will |
michael@0 | 483 | // be flushed at unlocking the document. The "current selection" is the |
michael@0 | 484 | // modified selection during document lock. This is also called |
michael@0 | 485 | // CurrentContent() too. |
michael@0 | 486 | Selection& CurrentSelection(); |
michael@0 | 487 | |
michael@0 | 488 | struct PendingAction MOZ_FINAL |
michael@0 | 489 | { |
michael@0 | 490 | enum ActionType MOZ_ENUM_TYPE(uint8_t) |
michael@0 | 491 | { |
michael@0 | 492 | COMPOSITION_START, |
michael@0 | 493 | COMPOSITION_UPDATE, |
michael@0 | 494 | COMPOSITION_END, |
michael@0 | 495 | SELECTION_SET |
michael@0 | 496 | }; |
michael@0 | 497 | ActionType mType; |
michael@0 | 498 | // For compositionstart and selectionset |
michael@0 | 499 | LONG mSelectionStart; |
michael@0 | 500 | LONG mSelectionLength; |
michael@0 | 501 | // For compositionupdate and compositionend |
michael@0 | 502 | nsString mData; |
michael@0 | 503 | // For compositionupdate |
michael@0 | 504 | nsRefPtr<mozilla::TextRangeArray> mRanges; |
michael@0 | 505 | // For selectionset |
michael@0 | 506 | bool mSelectionReversed; |
michael@0 | 507 | }; |
michael@0 | 508 | // Items of mPendingActions are appended when TSF tells us to need to dispatch |
michael@0 | 509 | // DOM composition events. However, we cannot dispatch while the document is |
michael@0 | 510 | // locked because it can cause modifying the locked document. So, the pending |
michael@0 | 511 | // actions should be performed when document lock is unlocked. |
michael@0 | 512 | nsTArray<PendingAction> mPendingActions; |
michael@0 | 513 | |
michael@0 | 514 | PendingAction* GetPendingCompositionUpdate() |
michael@0 | 515 | { |
michael@0 | 516 | if (!mPendingActions.IsEmpty()) { |
michael@0 | 517 | PendingAction& lastAction = mPendingActions.LastElement(); |
michael@0 | 518 | if (lastAction.mType == PendingAction::COMPOSITION_UPDATE) { |
michael@0 | 519 | return &lastAction; |
michael@0 | 520 | } |
michael@0 | 521 | } |
michael@0 | 522 | PendingAction* newAction = mPendingActions.AppendElement(); |
michael@0 | 523 | newAction->mType = PendingAction::COMPOSITION_UPDATE; |
michael@0 | 524 | newAction->mRanges = new mozilla::TextRangeArray(); |
michael@0 | 525 | return newAction; |
michael@0 | 526 | } |
michael@0 | 527 | |
michael@0 | 528 | // When On*Composition() is called without document lock, we need to flush |
michael@0 | 529 | // the recorded actions at quitting the method. |
michael@0 | 530 | // AutoPendingActionAndContentFlusher class is usedful for it. |
michael@0 | 531 | class MOZ_STACK_CLASS AutoPendingActionAndContentFlusher MOZ_FINAL |
michael@0 | 532 | { |
michael@0 | 533 | public: |
michael@0 | 534 | AutoPendingActionAndContentFlusher(nsTextStore* aTextStore) |
michael@0 | 535 | : mTextStore(aTextStore) |
michael@0 | 536 | { |
michael@0 | 537 | MOZ_ASSERT(!mTextStore->mIsRecordingActionsWithoutLock); |
michael@0 | 538 | if (!mTextStore->IsReadWriteLocked()) { |
michael@0 | 539 | mTextStore->mIsRecordingActionsWithoutLock = true; |
michael@0 | 540 | } |
michael@0 | 541 | } |
michael@0 | 542 | |
michael@0 | 543 | ~AutoPendingActionAndContentFlusher() |
michael@0 | 544 | { |
michael@0 | 545 | if (!mTextStore->mIsRecordingActionsWithoutLock) { |
michael@0 | 546 | return; |
michael@0 | 547 | } |
michael@0 | 548 | mTextStore->FlushPendingActions(); |
michael@0 | 549 | mTextStore->mIsRecordingActionsWithoutLock = false; |
michael@0 | 550 | } |
michael@0 | 551 | |
michael@0 | 552 | private: |
michael@0 | 553 | AutoPendingActionAndContentFlusher() {} |
michael@0 | 554 | |
michael@0 | 555 | nsRefPtr<nsTextStore> mTextStore; |
michael@0 | 556 | }; |
michael@0 | 557 | |
michael@0 | 558 | class Content MOZ_FINAL |
michael@0 | 559 | { |
michael@0 | 560 | public: |
michael@0 | 561 | Content(nsTextStore::Composition& aComposition, |
michael@0 | 562 | nsTextStore::Selection& aSelection) : |
michael@0 | 563 | mComposition(aComposition), mSelection(aSelection) |
michael@0 | 564 | { |
michael@0 | 565 | Clear(); |
michael@0 | 566 | } |
michael@0 | 567 | |
michael@0 | 568 | void Clear() |
michael@0 | 569 | { |
michael@0 | 570 | mText.Truncate(); |
michael@0 | 571 | mInitialized = false; |
michael@0 | 572 | } |
michael@0 | 573 | |
michael@0 | 574 | bool IsInitialized() const { return mInitialized; } |
michael@0 | 575 | |
michael@0 | 576 | void Init(const nsAString& aText) |
michael@0 | 577 | { |
michael@0 | 578 | mText = aText; |
michael@0 | 579 | mMinTextModifiedOffset = NOT_MODIFIED; |
michael@0 | 580 | mInitialized = true; |
michael@0 | 581 | } |
michael@0 | 582 | |
michael@0 | 583 | const nsDependentSubstring GetSelectedText() const; |
michael@0 | 584 | const nsDependentSubstring GetSubstring(uint32_t aStart, |
michael@0 | 585 | uint32_t aLength) const; |
michael@0 | 586 | void ReplaceSelectedTextWith(const nsAString& aString); |
michael@0 | 587 | void ReplaceTextWith(LONG aStart, LONG aLength, const nsAString& aString); |
michael@0 | 588 | |
michael@0 | 589 | void StartComposition(ITfCompositionView* aCompositionView, |
michael@0 | 590 | const PendingAction& aCompStart, |
michael@0 | 591 | bool aPreserveSelection); |
michael@0 | 592 | void EndComposition(const PendingAction& aCompEnd); |
michael@0 | 593 | |
michael@0 | 594 | const nsString& Text() const |
michael@0 | 595 | { |
michael@0 | 596 | MOZ_ASSERT(mInitialized); |
michael@0 | 597 | return mText; |
michael@0 | 598 | } |
michael@0 | 599 | |
michael@0 | 600 | // Returns true if layout of the character at the aOffset has not been |
michael@0 | 601 | // calculated. |
michael@0 | 602 | bool IsLayoutChangedAfter(uint32_t aOffset) const |
michael@0 | 603 | { |
michael@0 | 604 | return mInitialized && (mMinTextModifiedOffset < aOffset); |
michael@0 | 605 | } |
michael@0 | 606 | // Returns true if layout of the content has been changed, i.e., the new |
michael@0 | 607 | // layout has not been calculated. |
michael@0 | 608 | bool IsLayoutChanged() const |
michael@0 | 609 | { |
michael@0 | 610 | return mInitialized && (mMinTextModifiedOffset != NOT_MODIFIED); |
michael@0 | 611 | } |
michael@0 | 612 | |
michael@0 | 613 | nsTextStore::Composition& Composition() { return mComposition; } |
michael@0 | 614 | nsTextStore::Selection& Selection() { return mSelection; } |
michael@0 | 615 | |
michael@0 | 616 | private: |
michael@0 | 617 | nsString mText; |
michael@0 | 618 | nsTextStore::Composition& mComposition; |
michael@0 | 619 | nsTextStore::Selection& mSelection; |
michael@0 | 620 | |
michael@0 | 621 | // The minimum offset of modified part of the text. |
michael@0 | 622 | enum MOZ_ENUM_TYPE(uint32_t) |
michael@0 | 623 | { |
michael@0 | 624 | NOT_MODIFIED = UINT32_MAX |
michael@0 | 625 | }; |
michael@0 | 626 | uint32_t mMinTextModifiedOffset; |
michael@0 | 627 | |
michael@0 | 628 | bool mInitialized; |
michael@0 | 629 | }; |
michael@0 | 630 | // mContent caches "current content" of the document ONLY while the document |
michael@0 | 631 | // is locked. I.e., the content is cleared at unlocking the document since |
michael@0 | 632 | // we need to reduce the memory usage. This is initialized by |
michael@0 | 633 | // CurrentContent() automatically, so, don't access this member directly |
michael@0 | 634 | // except at calling Clear(), IsInitialized(), IsLayoutChangedAfter() or |
michael@0 | 635 | // IsLayoutChanged(). |
michael@0 | 636 | Content mContent; |
michael@0 | 637 | |
michael@0 | 638 | Content& CurrentContent(); |
michael@0 | 639 | |
michael@0 | 640 | // The input scopes for this context, defaults to IS_DEFAULT. |
michael@0 | 641 | nsTArray<InputScope> mInputScopes; |
michael@0 | 642 | bool mInputScopeDetected; |
michael@0 | 643 | bool mInputScopeRequested; |
michael@0 | 644 | // If edit actions are being recorded without document lock, this is true. |
michael@0 | 645 | // Otherwise, false. |
michael@0 | 646 | bool mIsRecordingActionsWithoutLock; |
michael@0 | 647 | // During recording actions, we shouldn't call mSink->OnSelectionChange() |
michael@0 | 648 | // because it may cause TSF request new lock. This is a problem if the |
michael@0 | 649 | // selection change is caused by a call of On*Composition() without document |
michael@0 | 650 | // lock since RequestLock() tries to flush the pending actions again (which |
michael@0 | 651 | // are flushing). Therefore, OnSelectionChangeInternal() sets this true |
michael@0 | 652 | // during recoding actions and then, RequestLock() will call |
michael@0 | 653 | // mSink->OnSelectionChange() after mLock becomes 0. |
michael@0 | 654 | bool mPendingOnSelectionChange; |
michael@0 | 655 | // If GetTextExt() or GetACPFromPoint() is called and the layout hasn't been |
michael@0 | 656 | // calculated yet, these methods return TS_E_NOLAYOUT. Then, RequestLock() |
michael@0 | 657 | // will call mSink->OnLayoutChange() and |
michael@0 | 658 | // ITfContextOwnerServices::OnLayoutChange() after the layout is fixed and |
michael@0 | 659 | // the document is unlocked. |
michael@0 | 660 | bool mPendingOnLayoutChange; |
michael@0 | 661 | // While there is native caret, this is true. Otherwise, false. |
michael@0 | 662 | bool mNativeCaretIsCreated; |
michael@0 | 663 | |
michael@0 | 664 | // True if current IME is implemented with IMM. |
michael@0 | 665 | bool mIsIMM_IME; |
michael@0 | 666 | // True if OnActivated() is already called |
michael@0 | 667 | bool mOnActivatedCalled; |
michael@0 | 668 | |
michael@0 | 669 | // TSF thread manager object for the current application |
michael@0 | 670 | static ITfThreadMgr* sTsfThreadMgr; |
michael@0 | 671 | // sMessagePump is QI'ed from sTsfThreadMgr |
michael@0 | 672 | static ITfMessagePump* sMessagePump; |
michael@0 | 673 | // sKeystrokeMgr is QI'ed from sTsfThreadMgr |
michael@0 | 674 | static ITfKeystrokeMgr* sKeystrokeMgr; |
michael@0 | 675 | // TSF display attribute manager |
michael@0 | 676 | static ITfDisplayAttributeMgr* sDisplayAttrMgr; |
michael@0 | 677 | // TSF category manager |
michael@0 | 678 | static ITfCategoryMgr* sCategoryMgr; |
michael@0 | 679 | |
michael@0 | 680 | // TSF client ID for the current application |
michael@0 | 681 | static DWORD sTsfClientId; |
michael@0 | 682 | // Current text store. Currently only ONE nsTextStore instance is ever used, |
michael@0 | 683 | // although Create is called when an editor is focused and Destroy called |
michael@0 | 684 | // when the focused editor is blurred. |
michael@0 | 685 | static nsTextStore* sTsfTextStore; |
michael@0 | 686 | |
michael@0 | 687 | // For IME (keyboard) disabled state: |
michael@0 | 688 | static ITfDocumentMgr* sTsfDisabledDocumentMgr; |
michael@0 | 689 | static ITfContext* sTsfDisabledContext; |
michael@0 | 690 | |
michael@0 | 691 | static ITfInputProcessorProfiles* sInputProcessorProfiles; |
michael@0 | 692 | |
michael@0 | 693 | // Enables/Disables hack for specific TIP. |
michael@0 | 694 | static bool sCreateNativeCaretForATOK; |
michael@0 | 695 | |
michael@0 | 696 | // Message the Tablet Input Panel uses to flush text during blurring. |
michael@0 | 697 | // See comments in Destroy |
michael@0 | 698 | static UINT sFlushTIPInputMessage; |
michael@0 | 699 | |
michael@0 | 700 | private: |
michael@0 | 701 | ULONG mRefCnt; |
michael@0 | 702 | }; |
michael@0 | 703 | |
michael@0 | 704 | #endif /*NSTEXTSTORE_H_*/ |