FrameworkTextComposition.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / Orcas / SP / wpf / src / Framework / System / Windows / Documents / FrameworkTextComposition.cs / 1 / FrameworkTextComposition.cs

                            //---------------------------------------------------------------------------- 
//
// 
//    Copyright (C) Microsoft Corporation.  All rights reserved.
//  
//
// Description: the TextComposition class 
// 
// History:
//  11/18/2003 : yutakas created 
//
//---------------------------------------------------------------------------

using MS.Internal; 
using MS.Win32;
using System; 
using System.Diagnostics; 
using System.Runtime.InteropServices;
using System.Windows; 
using System.Windows.Input;
using System.Security;
using System.Security.Permissions;
 
namespace System.Windows.Documents
{ 
 
    /// 
    ///     the internal Composition class provides input-text/composition event promotion 
    ///     This class is used for simple TextBox control that does not expose TextRange.
    /// 
    public class FrameworkTextComposition: TextComposition
    { 

        //----------------------------------------------------- 
        // 
        //  Constructors
        // 
        //-----------------------------------------------------

        #region Constructors
 
        //
        // Constructor for TextStore's TextComposition. 
        // 
        internal FrameworkTextComposition(InputManager inputManager, IInputElement source, object owner)  : base(inputManager, source, String.Empty, TextCompositionAutoComplete.Off)
        { 
            _owner = owner;
        }

        #endregion Constructors 

        //------------------------------------------------------ 
        // 
        //  Public Methods
        // 
        //-----------------------------------------------------

        //------------------------------------------------------
        // 
        //  Public Interface Methods
        // 
        //------------------------------------------------------ 

        ///  
        ///     Finalize the composition.
        ///     This does not call base.Complete() because TextComposition.Complete()
        ///     will call TextServicesManager.CompleteComposition() directly to generate TextCompositionEvent.
        ///     We finalize Cicero's composition and TextStore will automatically 
        ///     generate the proper TextComposition events.
        ///  
        ///  
        ///     Callers must have UIPermission(PermissionState.Unrestricted) to call this API.
        ///  
        /// 
        ///     Critical: This method is critical only because its base class method is critical.
        ///     PublicOK: This just changes the state of the composition string from undetermined to determined.
        ///               And of course, this does not expose any critical data. 
        ///               Protected by link demand to match base class.
        ///  
        [SecurityCritical] 
        [UIPermissionAttribute(SecurityAction.LinkDemand,Unrestricted=true)]
        public override void Complete() 
        {
            _pendingComplete = true;
        }
 
        //-----------------------------------------------------
        // 
        //  Public Properties 
        //
        //------------------------------------------------------ 

        /// 
        /// Offset of the finalized composition text.
        ///  
        /// 
        /// This property is only meaningful during the handling of a 
        /// TextInput event.  During a TextInputStart or TextInputUpdate event, 
        /// this property has a value of -1.
        /// 
        /// When read in the context of TextBox, the offset refers to unicode
        /// code points.  When read in the context of a RichTextBox, the offset
        /// refers to FlowDocument symbols (unicode code points, TextElement edges,
        /// or embedded UIElements). 
        /// 
        public int ResultOffset 
        { 
            get
            { 
                return (_ResultStart == null) ? -1 : _offset;
            }
        }
 
        /// 
        /// Length of the current composition in unicode code points. 
        ///  
        /// 
        /// This property is only meaningful during the handling of a 
        /// TextInput event.  During a TextInputStart or TextInputUpdate event,
        /// this property has a value of -1.
        /// 
        public int ResultLength 
        {
            get 
            { 
                return (_ResultStart == null) ? -1 : _length;
            } 
        }

        /// 
        /// Offset of the composition text. 
        /// 
        ///  
        /// This property is only meaningful during the handling of a 
        /// TextInputStart or TextInputUpdate event.  During a TextInput event,
        /// this property has a value of -1. 
        ///
        /// When read in the context of TextBox, the offset refers to unicode
        /// code points.  When read in the context of a RichTextBox, the offset
        /// refers to FlowDocument symbols (unicode code points, TextElement edges, 
        /// or embedded UIElements).
        ///  
        public int CompositionOffset 
        {
            get 
            {
                return (_CompositionStart == null) ? -1 : _offset;
            }
        } 

        ///  
        /// Length of the current composition in unicode code points. 
        /// 
        ///  
        /// This property is only meaningful during the handling of a
        /// TextInputStart or TextInputUpdate event.  During a TextInput event,
        /// this property has a value of -1.
        ///  
        public int CompositionLength
        { 
            get 
            {
                return (_CompositionStart == null) ? -1 : _length; 
            }
        }

        //----------------------------------------------------- 
        //
        //  Internal Methods 
        // 
        //-----------------------------------------------------
 
        /// 
        ///     Critical: This calls critical COM interop.
        /// 
        [SecurityCritical] 
        internal static void CompleteCurrentComposition(UnsafeNativeMethods.ITfDocumentMgr documentMgr)
        { 
            UnsafeNativeMethods.ITfContext context; 

            documentMgr.GetBase(out context); 

            UnsafeNativeMethods.ITfCompositionView composition = GetComposition(context);

            if (composition != null) 
            {
                UnsafeNativeMethods.ITfContextOwnerCompositionServices compositionService = context as UnsafeNativeMethods.ITfContextOwnerCompositionServices; 
 
                // Terminate composition if there is a composition view.
                compositionService.TerminateComposition(composition); 
                Marshal.ReleaseComObject(composition);
            }

            Marshal.ReleaseComObject(context); 
        }
 
        ///  
        ///     Critical: This calls critical COM interop.
        ///  
        [SecurityCritical]
        internal static UnsafeNativeMethods.ITfCompositionView GetCurrentCompositionView(UnsafeNativeMethods.ITfDocumentMgr documentMgr)
        {
            UnsafeNativeMethods.ITfContext context; 

            documentMgr.GetBase(out context); 
 
            UnsafeNativeMethods.ITfCompositionView view = GetComposition(context);
 
            Marshal.ReleaseComObject(context);

            return view;
        } 

        // Set result string to TextComposition. 
        internal void SetResultPositions(ITextPointer start, ITextPointer end, string text) 
        {
            Invariant.Assert(start != null); 
            Invariant.Assert(end != null);
            Invariant.Assert(text != null);

            _compositionStart = null; 
            _compositionEnd = null;
 
            // We need to have another instances of TextPointer since we don't want to 
            // freeze original TextPointers.
            _resultStart = start.GetFrozenPointer(LogicalDirection.Backward); 
            _resultEnd = end.GetFrozenPointer(LogicalDirection.Forward);

            this.Text = text;
            this.CompositionText = String.Empty; 

            // We must cache integer offsets -- public listeners won't expect 
            // them to float like TextPointers if the document changes. 
            _offset = (_resultStart == null) ? -1 : _resultStart.Offset;
            _length = (_resultStart == null) ? -1 : _resultStart.GetOffsetToPosition(_resultEnd); 
        }

        // Set composition string to TextComposition.
        internal void SetCompositionPositions(ITextPointer start, ITextPointer end, string text) 
        {
            Invariant.Assert(start != null); 
            Invariant.Assert(end != null); 
            Invariant.Assert(text != null);
 
            // We need to have another instances of TextPointer since we don't want to
            // freeze original TextPointers.
            _compositionStart = start.GetFrozenPointer(LogicalDirection.Backward);
            _compositionEnd = end.GetFrozenPointer(LogicalDirection.Forward); 

            _resultStart = null; 
            _resultEnd = null; 

            this.Text = String.Empty; 
            this.CompositionText = text;

            // We must cache integer offsets -- public listeners won't expect
            // them to float like TextPointers if the document changes. 
            _offset = (_compositionStart == null) ? -1 : _compositionStart.Offset;
            _length = (_compositionStart == null) ? -1 : _compositionStart.GetOffsetToPosition(_compositionEnd); 
        } 

        //----------------------------------------------------- 
        //
        //  Internal Properties
        //
        //------------------------------------------------------ 

        ///  
        ///     The result text of the text input. 
        /// 
        internal ITextPointer _ResultStart 
        {
            get
            {
                return _resultStart; 
            }
        } 
 
        /// 
        ///     The result text of the text input. 
        /// 
        internal ITextPointer _ResultEnd
        {
            get 
            {
                return _resultEnd; 
            } 
        }
 
        /// 
        ///     The current composition text.
        /// 
        internal ITextPointer _CompositionStart 
        {
            get 
            { 
                return _compositionStart;
            } 
        }

        /// 
        ///     The current composition text. 
        /// 
        internal ITextPointer _CompositionEnd 
        { 
            get
            { 
                return _compositionEnd;
            }
        }
 
        // True if Complete has been called.
        internal bool PendingComplete 
        { 
            get
            { 
                return _pendingComplete;
            }
        }
 
        // TextStore or ImmComposition that created this object.
        internal object Owner 
        { 
            get
            { 
                return _owner;
            }
        }
 
        //-----------------------------------------------------
        // 
        //  Internal Events 
        //
        //------------------------------------------------------ 

        //------------------------------------------------------
        //
        //  Private Methods 
        //
        //----------------------------------------------------- 
 
        /// 
        ///     Get ITfContextView of the context. 
        /// 
        ///
        ///     Critical: calls Marshal.ReleaseComObject which LinkDemands
        /// 
        [SecurityCritical]
        private static UnsafeNativeMethods.ITfCompositionView GetComposition(UnsafeNativeMethods.ITfContext context) 
        { 
            UnsafeNativeMethods.ITfContextComposition contextComposition;
            UnsafeNativeMethods.IEnumITfCompositionView enumCompositionView; 
            UnsafeNativeMethods.ITfCompositionView[] compositionViews = new UnsafeNativeMethods.ITfCompositionView[1];
            int fetched;

            contextComposition = (UnsafeNativeMethods.ITfContextComposition)context; 
            contextComposition.EnumCompositions(out enumCompositionView);
 
            enumCompositionView.Next(1, compositionViews, out fetched); 

            Marshal.ReleaseComObject(enumCompositionView); 
            return compositionViews[0];
        }

        //------------------------------------------------------ 
        //
        //  Private Fields 
        // 
        //-----------------------------------------------------
 
        // the start poisiotn of the result text
        private ITextPointer _resultStart;

        // the end poisiotn of the result text 
        private ITextPointer _resultEnd;
 
        // the start position of the composition text 
        private ITextPointer _compositionStart;
 
        // the end position of the composition text
        private ITextPointer _compositionEnd;

        // Composition or result offset. 
        private int _offset;
 
        // Composition or result length. 
        private int _length;
 
        // TextStore or ImmComposition that created this object.
        private readonly object _owner;

        private bool _pendingComplete; 
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
//---------------------------------------------------------------------------- 
//
// 
//    Copyright (C) Microsoft Corporation.  All rights reserved.
//  
//
// Description: the TextComposition class 
// 
// History:
//  11/18/2003 : yutakas created 
//
//---------------------------------------------------------------------------

using MS.Internal; 
using MS.Win32;
using System; 
using System.Diagnostics; 
using System.Runtime.InteropServices;
using System.Windows; 
using System.Windows.Input;
using System.Security;
using System.Security.Permissions;
 
namespace System.Windows.Documents
{ 
 
    /// 
    ///     the internal Composition class provides input-text/composition event promotion 
    ///     This class is used for simple TextBox control that does not expose TextRange.
    /// 
    public class FrameworkTextComposition: TextComposition
    { 

        //----------------------------------------------------- 
        // 
        //  Constructors
        // 
        //-----------------------------------------------------

        #region Constructors
 
        //
        // Constructor for TextStore's TextComposition. 
        // 
        internal FrameworkTextComposition(InputManager inputManager, IInputElement source, object owner)  : base(inputManager, source, String.Empty, TextCompositionAutoComplete.Off)
        { 
            _owner = owner;
        }

        #endregion Constructors 

        //------------------------------------------------------ 
        // 
        //  Public Methods
        // 
        //-----------------------------------------------------

        //------------------------------------------------------
        // 
        //  Public Interface Methods
        // 
        //------------------------------------------------------ 

        ///  
        ///     Finalize the composition.
        ///     This does not call base.Complete() because TextComposition.Complete()
        ///     will call TextServicesManager.CompleteComposition() directly to generate TextCompositionEvent.
        ///     We finalize Cicero's composition and TextStore will automatically 
        ///     generate the proper TextComposition events.
        ///  
        ///  
        ///     Callers must have UIPermission(PermissionState.Unrestricted) to call this API.
        ///  
        /// 
        ///     Critical: This method is critical only because its base class method is critical.
        ///     PublicOK: This just changes the state of the composition string from undetermined to determined.
        ///               And of course, this does not expose any critical data. 
        ///               Protected by link demand to match base class.
        ///  
        [SecurityCritical] 
        [UIPermissionAttribute(SecurityAction.LinkDemand,Unrestricted=true)]
        public override void Complete() 
        {
            _pendingComplete = true;
        }
 
        //-----------------------------------------------------
        // 
        //  Public Properties 
        //
        //------------------------------------------------------ 

        /// 
        /// Offset of the finalized composition text.
        ///  
        /// 
        /// This property is only meaningful during the handling of a 
        /// TextInput event.  During a TextInputStart or TextInputUpdate event, 
        /// this property has a value of -1.
        /// 
        /// When read in the context of TextBox, the offset refers to unicode
        /// code points.  When read in the context of a RichTextBox, the offset
        /// refers to FlowDocument symbols (unicode code points, TextElement edges,
        /// or embedded UIElements). 
        /// 
        public int ResultOffset 
        { 
            get
            { 
                return (_ResultStart == null) ? -1 : _offset;
            }
        }
 
        /// 
        /// Length of the current composition in unicode code points. 
        ///  
        /// 
        /// This property is only meaningful during the handling of a 
        /// TextInput event.  During a TextInputStart or TextInputUpdate event,
        /// this property has a value of -1.
        /// 
        public int ResultLength 
        {
            get 
            { 
                return (_ResultStart == null) ? -1 : _length;
            } 
        }

        /// 
        /// Offset of the composition text. 
        /// 
        ///  
        /// This property is only meaningful during the handling of a 
        /// TextInputStart or TextInputUpdate event.  During a TextInput event,
        /// this property has a value of -1. 
        ///
        /// When read in the context of TextBox, the offset refers to unicode
        /// code points.  When read in the context of a RichTextBox, the offset
        /// refers to FlowDocument symbols (unicode code points, TextElement edges, 
        /// or embedded UIElements).
        ///  
        public int CompositionOffset 
        {
            get 
            {
                return (_CompositionStart == null) ? -1 : _offset;
            }
        } 

        ///  
        /// Length of the current composition in unicode code points. 
        /// 
        ///  
        /// This property is only meaningful during the handling of a
        /// TextInputStart or TextInputUpdate event.  During a TextInput event,
        /// this property has a value of -1.
        ///  
        public int CompositionLength
        { 
            get 
            {
                return (_CompositionStart == null) ? -1 : _length; 
            }
        }

        //----------------------------------------------------- 
        //
        //  Internal Methods 
        // 
        //-----------------------------------------------------
 
        /// 
        ///     Critical: This calls critical COM interop.
        /// 
        [SecurityCritical] 
        internal static void CompleteCurrentComposition(UnsafeNativeMethods.ITfDocumentMgr documentMgr)
        { 
            UnsafeNativeMethods.ITfContext context; 

            documentMgr.GetBase(out context); 

            UnsafeNativeMethods.ITfCompositionView composition = GetComposition(context);

            if (composition != null) 
            {
                UnsafeNativeMethods.ITfContextOwnerCompositionServices compositionService = context as UnsafeNativeMethods.ITfContextOwnerCompositionServices; 
 
                // Terminate composition if there is a composition view.
                compositionService.TerminateComposition(composition); 
                Marshal.ReleaseComObject(composition);
            }

            Marshal.ReleaseComObject(context); 
        }
 
        ///  
        ///     Critical: This calls critical COM interop.
        ///  
        [SecurityCritical]
        internal static UnsafeNativeMethods.ITfCompositionView GetCurrentCompositionView(UnsafeNativeMethods.ITfDocumentMgr documentMgr)
        {
            UnsafeNativeMethods.ITfContext context; 

            documentMgr.GetBase(out context); 
 
            UnsafeNativeMethods.ITfCompositionView view = GetComposition(context);
 
            Marshal.ReleaseComObject(context);

            return view;
        } 

        // Set result string to TextComposition. 
        internal void SetResultPositions(ITextPointer start, ITextPointer end, string text) 
        {
            Invariant.Assert(start != null); 
            Invariant.Assert(end != null);
            Invariant.Assert(text != null);

            _compositionStart = null; 
            _compositionEnd = null;
 
            // We need to have another instances of TextPointer since we don't want to 
            // freeze original TextPointers.
            _resultStart = start.GetFrozenPointer(LogicalDirection.Backward); 
            _resultEnd = end.GetFrozenPointer(LogicalDirection.Forward);

            this.Text = text;
            this.CompositionText = String.Empty; 

            // We must cache integer offsets -- public listeners won't expect 
            // them to float like TextPointers if the document changes. 
            _offset = (_resultStart == null) ? -1 : _resultStart.Offset;
            _length = (_resultStart == null) ? -1 : _resultStart.GetOffsetToPosition(_resultEnd); 
        }

        // Set composition string to TextComposition.
        internal void SetCompositionPositions(ITextPointer start, ITextPointer end, string text) 
        {
            Invariant.Assert(start != null); 
            Invariant.Assert(end != null); 
            Invariant.Assert(text != null);
 
            // We need to have another instances of TextPointer since we don't want to
            // freeze original TextPointers.
            _compositionStart = start.GetFrozenPointer(LogicalDirection.Backward);
            _compositionEnd = end.GetFrozenPointer(LogicalDirection.Forward); 

            _resultStart = null; 
            _resultEnd = null; 

            this.Text = String.Empty; 
            this.CompositionText = text;

            // We must cache integer offsets -- public listeners won't expect
            // them to float like TextPointers if the document changes. 
            _offset = (_compositionStart == null) ? -1 : _compositionStart.Offset;
            _length = (_compositionStart == null) ? -1 : _compositionStart.GetOffsetToPosition(_compositionEnd); 
        } 

        //----------------------------------------------------- 
        //
        //  Internal Properties
        //
        //------------------------------------------------------ 

        ///  
        ///     The result text of the text input. 
        /// 
        internal ITextPointer _ResultStart 
        {
            get
            {
                return _resultStart; 
            }
        } 
 
        /// 
        ///     The result text of the text input. 
        /// 
        internal ITextPointer _ResultEnd
        {
            get 
            {
                return _resultEnd; 
            } 
        }
 
        /// 
        ///     The current composition text.
        /// 
        internal ITextPointer _CompositionStart 
        {
            get 
            { 
                return _compositionStart;
            } 
        }

        /// 
        ///     The current composition text. 
        /// 
        internal ITextPointer _CompositionEnd 
        { 
            get
            { 
                return _compositionEnd;
            }
        }
 
        // True if Complete has been called.
        internal bool PendingComplete 
        { 
            get
            { 
                return _pendingComplete;
            }
        }
 
        // TextStore or ImmComposition that created this object.
        internal object Owner 
        { 
            get
            { 
                return _owner;
            }
        }
 
        //-----------------------------------------------------
        // 
        //  Internal Events 
        //
        //------------------------------------------------------ 

        //------------------------------------------------------
        //
        //  Private Methods 
        //
        //----------------------------------------------------- 
 
        /// 
        ///     Get ITfContextView of the context. 
        /// 
        ///
        ///     Critical: calls Marshal.ReleaseComObject which LinkDemands
        /// 
        [SecurityCritical]
        private static UnsafeNativeMethods.ITfCompositionView GetComposition(UnsafeNativeMethods.ITfContext context) 
        { 
            UnsafeNativeMethods.ITfContextComposition contextComposition;
            UnsafeNativeMethods.IEnumITfCompositionView enumCompositionView; 
            UnsafeNativeMethods.ITfCompositionView[] compositionViews = new UnsafeNativeMethods.ITfCompositionView[1];
            int fetched;

            contextComposition = (UnsafeNativeMethods.ITfContextComposition)context; 
            contextComposition.EnumCompositions(out enumCompositionView);
 
            enumCompositionView.Next(1, compositionViews, out fetched); 

            Marshal.ReleaseComObject(enumCompositionView); 
            return compositionViews[0];
        }

        //------------------------------------------------------ 
        //
        //  Private Fields 
        // 
        //-----------------------------------------------------
 
        // the start poisiotn of the result text
        private ITextPointer _resultStart;

        // the end poisiotn of the result text 
        private ITextPointer _resultEnd;
 
        // the start position of the composition text 
        private ITextPointer _compositionStart;
 
        // the end position of the composition text
        private ITextPointer _compositionEnd;

        // Composition or result offset. 
        private int _offset;
 
        // Composition or result length. 
        private int _length;
 
        // TextStore or ImmComposition that created this object.
        private readonly object _owner;

        private bool _pendingComplete; 
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK