Ejemplo n.º 1
0
        private void _RegisterTextStore(TextStore textstore)
        {
            int editCookie        = -1;
            int threadFocusCookie = -1;
            int editSinkCookie    = -1;

            if (this._threadManager == null)
            {
                this._threadManager = new SecurityCriticalDataClass <UnsafeNativeMethods.ITfThreadMgr>(TextServicesLoader.Load());
                if (this._threadManager.Value == null)
                {
                    this._threadManager = null;
                    return;
                }
                int value;
                this._threadManager.Value.Activate(out value);
                this._clientId = new SecurityCriticalData <int>(value);
                base.Dispatcher.ShutdownFinished += this.OnDispatcherShutdownFinished;
            }
            UnsafeNativeMethods.ITfDocumentMgr tfDocumentMgr;
            this._threadManager.Value.CreateDocumentMgr(out tfDocumentMgr);
            UnsafeNativeMethods.ITfContext tfContext;
            tfDocumentMgr.CreateContext(this._clientId.Value, (UnsafeNativeMethods.CreateContextFlags) 0, textstore, out tfContext, out editCookie);
            tfDocumentMgr.Push(tfContext);
            if (textstore != null)
            {
                Guid guid = UnsafeNativeMethods.IID_ITfThreadFocusSink;
                UnsafeNativeMethods.ITfSource tfSource = this._threadManager.Value as UnsafeNativeMethods.ITfSource;
                tfSource.AdviseSink(ref guid, textstore, out threadFocusCookie);
            }
            if (textstore != null)
            {
                Guid guid = UnsafeNativeMethods.IID_ITfTextEditSink;
                UnsafeNativeMethods.ITfSource tfSource = tfContext as UnsafeNativeMethods.ITfSource;
                tfSource.AdviseSink(ref guid, textstore, out editSinkCookie);
            }
            Marshal.ReleaseComObject(tfContext);
            textstore.DocumentManager   = tfDocumentMgr;
            textstore.ThreadFocusCookie = threadFocusCookie;
            textstore.EditSinkCookie    = editSinkCookie;
            textstore.EditCookie        = editCookie;
            if (textstore.UiScope.IsKeyboardFocused)
            {
                textstore.OnGotFocus();
            }
            this._registeredtextstorecount++;
        }
Ejemplo n.º 2
0
        private void _RegisterTextStore(TextStore textstore)
        {
            UnsafeNativeMethods.ITfDocumentMgr doc;
            UnsafeNativeMethods.ITfContext     context;
            UnsafeNativeMethods.ITfSource      source;
            int  editCookie        = UnsafeNativeMethods.TF_INVALID_COOKIE;
            int  threadFocusCookie = UnsafeNativeMethods.TF_INVALID_COOKIE;
            int  editSinkCookie    = UnsafeNativeMethods.TF_INVALID_COOKIE;
            Guid guid;

            Debug.Assert(CheckAccess(), "RegisterTextStore called on bad thread!");

            // Get ITfThreadMgr
            if (_threadManager == null)
            {
                Debug.Assert(_isDispatcherShutdownFinished == false, "Was this dispather finished?");
                Debug.Assert(_registeredtextstorecount == 0, "TextStore was registered without ThreadMgr?");

                // TextServicesLoader.Load() might return null if no text services are installed or enabled.
                _threadManager = new SecurityCriticalDataClass <UnsafeNativeMethods.ITfThreadMgr>(TextServicesLoader.Load());

                if (_threadManager.Value == null)
                {
                    _threadManager = null;
                    return;
                }

                // Activate TSF on this thread if this is the first TextStore.
                int clientIdTemp;
                _threadManager.Value.Activate(out clientIdTemp);
                _clientId = new SecurityCriticalData <int>(clientIdTemp);

                // We want to get the notification when Dispatcher is finished.
                Dispatcher.ShutdownFinished += new EventHandler(OnDispatcherShutdownFinished);
            }

            // Create a TSF document.
            _threadManager.Value.CreateDocumentMgr(out doc);
            doc.CreateContext(_clientId.Value, 0 /* flags */, textstore, out context, out editCookie);
            doc.Push(context);

            // Attach a thread focus sink.
            if (textstore is UnsafeNativeMethods.ITfThreadFocusSink)
            {
                guid   = UnsafeNativeMethods.IID_ITfThreadFocusSink;
                source = _threadManager.Value as UnsafeNativeMethods.ITfSource;
                source.AdviseSink(ref guid, textstore, out threadFocusCookie);
            }

            // Attach an edit sink.
            if (textstore is UnsafeNativeMethods.ITfTextEditSink)
            {
                guid   = UnsafeNativeMethods.IID_ITfTextEditSink;
                source = context as UnsafeNativeMethods.ITfSource;
                source.AdviseSink(ref guid, textstore, out editSinkCookie);
            }

            // Release any native resources we're done with.
            Marshal.ReleaseComObject(context);

            textstore.DocumentManager   = doc;
            textstore.ThreadFocusCookie = threadFocusCookie;
            textstore.EditSinkCookie    = editSinkCookie;
            textstore.EditCookie        = editCookie;

            // If Scope of this textstore already has a focus, we need to call SetFocus()
            // in order to put this DIM on Cicero's focus. TextStore.OnGotFocus() calls
            // ITfThreadMgr::SetFocus();
            if (textstore.UiScope.IsKeyboardFocused)
            {
                textstore.OnGotFocus();
            }

            _registeredtextstorecount++;
        }