Example #1
0
        /// <summary>
        /// Sets the focus to the visible cell view corresponding to a location.
        /// </summary>
        /// <param name="x">X-coordinate of the location where to set the focus.</param>
        /// <param name="y">Y-coordinate of the location where to set the focus.</param>
        /// <param name="resetAnchor">If true, resets the selected text anchor.</param>
        /// <param name="isMoved">True if the focus was moved.</param>
        public virtual void SetFocusToPoint(double x, double y, bool resetAnchor, out bool isMoved)
        {
            isMoved = false;
            ulong OldFocusHash = FocusHash;
            int   OldIndex     = FocusChain.IndexOf(Focus);
            int   NewIndex     = -1;

            // Takes page margins and padding into account.
            DrawContext.ToRelativeLocation(ref x, ref y);

            for (int i = 0; i < FocusChain.Count; i++)
            {
                ILayoutFocus TestFocus = (ILayoutFocus)FocusChain[i];
                if (TestFocus.CellView.CellRect.IsPointInRect(x, y))
                {
                    NewIndex = i;
                    break;
                }
            }

            if (NewIndex >= 0)
            {
                if (NewIndex != OldIndex)
                {
                    ChangeFocus(NewIndex - OldIndex, OldIndex, NewIndex, resetAnchor, out bool IsRefreshed);
                }

                if (Focus is ILayoutTextFocus AsTextFocus)
                {
                    Point CellOrigin = Focus.CellView.CellOrigin;
                    Size  CellSize   = Focus.CellView.CellSize;

                    double XRelativeToCell = x - CellOrigin.X.Draw;
                    double YRelativeToCell = y - CellOrigin.Y.Draw;
                    Debug.Assert(XRelativeToCell >= 0 && XRelativeToCell < CellSize.Width.Draw);
                    Debug.Assert(YRelativeToCell >= 0 && YRelativeToCell < CellSize.Height.Draw);

                    string Text             = GetFocusedText(AsTextFocus);
                    int    NewCaretPosition = DrawContext.GetCaretPositionInText(XRelativeToCell, Text, FocusedTextStyle, CaretMode, Measure.Floating);
                    Debug.Assert(NewCaretPosition >= 0 && NewCaretPosition <= MaxCaretPosition);

                    SetTextCaretPosition(Text, NewCaretPosition, resetAnchor, out isMoved);
                }

                isMoved = true;
            }

            Debug.Assert(isMoved || OldFocusHash == FocusHash);
        }