Esempio n. 1
0
        private void ClearStyle(ITextRange range)
        {
            var start = Math.Min(range.StartPosition, range.EndPosition);
            var end   = Math.Max(range.StartPosition, range.EndPosition);

            range.SetRange(start, end);
            range.CharacterFormat = Document.GetDefaultCharacterFormat();

            range.GetText(TextGetOptions.NoHidden, out string text);
            range.SetText(TextSetOptions.Unlink, text);
            range.SetRange(start, start + text.Length);
        }
        /// <summary>
        /// Pad range with Zero-Width-Spaces.
        /// </summary>
        /// <param name="range">Range to pad.</param>
        /// <param name="format">Character format to apply to the padding.</param>
        private static void PadRange(ITextRange range, ITextCharacterFormat format)
        {
            var startPosition = range.StartPosition;
            var endPosition   = range.EndPosition + 1;
            var clone         = range.GetClone();

            clone.Collapse(true);
            clone.SetText(TextSetOptions.Unhide, "\u200B");
            clone.CharacterFormat.SetClone(format);
            clone.SetRange(endPosition, endPosition);
            clone.SetText(TextSetOptions.Unhide, "\u200B");
            clone.CharacterFormat.SetClone(format);
            range.SetRange(startPosition, endPosition + 1);
        }
        private string GetValue()
        {
            if (_type == WindowsEditBox.EditboxType.Password)
            {
                // Trying to retrieve the text (through WM_GETTEXT) from an edit box that has
                // the ES_PASSWORD style throws an UnauthorizedAccessException.
                throw new UnauthorizedAccessException();
            }

            if (IsDocument())
            {
                ITextRange range = _document.Range(0, 0);
                int        start = 0;
                int        end   = range.StoryLength;

                range.SetRange(start, end);

                string text = range.Text;
                // Empty edits contain a degenerate/empty range, and will return null
                // for their text - treat this as "", not null, since we do want to expose
                // a non-null value.
                if (string.IsNullOrEmpty(text))
                {
                    return("");
                }

                int embeddedObjectOffset = text.IndexOf((char)0xFFFC);
                if (embeddedObjectOffset != -1)
                {
                    StringBuilder sbText = new StringBuilder();
                    object        embeddedObject;
                    while (start < end && embeddedObjectOffset != -1)
                    {
                        sbText.Append(text.Substring(start, embeddedObjectOffset - start));
                        range.SetRange(embeddedObjectOffset, end);
                        if (range.GetEmbeddedObject(out embeddedObject) == NativeMethods.S_OK && embeddedObject != null)
                        {
                            GetEmbeddedObjectText(embeddedObject, sbText);
                        }
                        else
                        {
                            // If there is some kind of error, just append a space to the text.  In this way
                            // we will be no worse of then before implementing the embedded object get text.
                            sbText.Append(" ");
                        }
                        start = embeddedObjectOffset + 1;
                        embeddedObjectOffset = text.IndexOf((char)0xFFFC, start);
                    }

                    if (start < end)
                    {
                        sbText.Append(text.Substring(start, end - start));
                    }

                    text = sbText.ToString();
                }

                return(text);
            }
            else
            {
                return(Misc.ProxyGetText(_hwnd));
            }
        }
        //------------------------------------------------------
        //
        //  Internal Methods
        //
        //------------------------------------------------------

        #region Internal Methods

        // returns the range that is visible in the RichEdit window
        internal ITextRange GetVisibleRange()
        {
            // get a range from the center point of the client rectangle
            Rect       rect  = BoundingRectangle;
            int        x     = ((int)rect.Left + (int)rect.Right) / 2;
            int        y     = ((int)rect.Top + (int)rect.Bottom) / 2;
            ITextRange range = _document.RangeFromPoint(x, y);

            // expand it to fill the window.
            range.Expand(TomUnit.tomWindow);

            // There is a bug with RichEdit 3.0.  The expand to tomWindow may gets 0 as the range's cpBegin (Start).
            // So need to trim off what is outside of the window.
            int start = range.Start;

            // The ITextRange::SetRange method sets this range's Start = min(cp1, cp2) and End = max(cp1, cp2).
            // If the range is a nondegenerate selection, cp2 is the active end; if it's a degenerate selection,
            // the ambiguous cp is displayed at the start of the line (rather than at the end of the previous line).
            // Set the end to the start and the start to the end to create an ambiguous cp.
            range.SetRange(range.End, range.Start);
            bool gotPoint = WindowsRichEditRange.RangeGetPoint(range, TomGetPoint.tomStart, out x, out y);

            while (!gotPoint || !Misc.PtInRect(ref rect, x, y))
            {
                range.MoveStart(TomUnit.tomWord, 1);
                gotPoint = WindowsRichEditRange.RangeGetPoint(range, TomGetPoint.tomStart, out x, out y);
            }

            if (start != range.Start)
            {
                // The trimming was done based on the left edge of the range.  The last visiable partial
                // character/word has been also added back into the range, need to remove it.  Do the comparing
                // against the characters right edge and the window rectangle.
                ITextRange rangeAdjust = _document.Range(0, range.Start - 1);
                gotPoint = WindowsRichEditRange.RangeGetPoint(rangeAdjust, TomGetPoint.TA_BOTTOM | TomGetPoint.TA_RIGHT, out x, out y);

                while (gotPoint && Misc.PtInRect(ref rect, x, y) && rangeAdjust.Start != rangeAdjust.End)
                {
                    rangeAdjust.MoveEnd(TomUnit.tomCharacter, -1);
                    range.MoveStart(TomUnit.tomCharacter, -1);
                    gotPoint = WindowsRichEditRange.RangeGetPoint(rangeAdjust, TomGetPoint.TA_BOTTOM | TomGetPoint.TA_RIGHT, out x, out y);
                }
            }

            // There is a bug with RichEdit 3.0.  The expand to tomWindow gets the last cp of the bottom
            // line in the window as the range's cpLim (End).  The cpLim may be passed the right side of
            // the window.
            // So need to trim off what is on the right side of the window.
            int end = range.End;

            gotPoint = WindowsRichEditRange.RangeGetPoint(range, TomGetPoint.TA_RIGHT, out x, out y);
            while (!gotPoint || !Misc.PtInRect(ref rect, x, y))
            {
                range.MoveEnd(TomUnit.tomWord, -1);
                gotPoint = WindowsRichEditRange.RangeGetPoint(range, TomGetPoint.TA_RIGHT, out x, out y);
            }

            if (end != range.End)
            {
                // The trimming was done based on the right edge of the range.  The last visiable partial
                // character/word has been also trimmed so add it back to the range.  Do the comparing
                // against the characters left edge and the window rectangle.
                ITextRange rangeAdjust = _document.Range(range.End, end);
                do
                {
                    if (range.MoveEnd(TomUnit.tomCharacter, 1) == 0)
                    {
                        break;
                    }
                    rangeAdjust.MoveStart(TomUnit.tomCharacter, 1);
                    gotPoint = WindowsRichEditRange.RangeGetPoint(rangeAdjust, TomGetPoint.tomStart, out x, out y);
                } while (gotPoint && Misc.PtInRect(ref rect, x, y));
            }

            return(range);
        }
        ITextRangeProvider [] ITextProvider.GetSelection()
        {
            // we must have called EnsureTextDocument() before arriving here.
            Debug.Assert(_document != null);

            // clone a range from the documents selection
            ITextRange     range     = null;
            ITextSelection selection = _document.Selection;

            if (selection != null)
            {
                // duplicate the selection range since we don't want their modifications to affect the selection
                range = selection.GetDuplicate();

                // for future reference: active endpoint is
                // ((selection.Flags & TomSelectionFlags.tomSelStartActive) == TomSelectionFlags.tomSelStartActive) ? TextPatternRangeEndpoint.Start : TextPatternRangeEndpoint.End;
            }

            if (range == null)
            {
                return new ITextRangeProvider[] { }
            }
            ;
            else
            {
                return new ITextRangeProvider[] { new WindowsRichEditRange(range, this) }
            };
        }

        ITextRangeProvider [] ITextProvider.GetVisibleRanges()
        {
            ITextRange range = GetVisibleRange();

            if (range == null)
            {
                return new ITextRangeProvider[] { }
            }
            ;
            else
            {
                return new ITextRangeProvider[] { new WindowsRichEditRange(range, this) }
            };
        }

        ITextRangeProvider ITextProvider.RangeFromChild(IRawElementProviderSimple childElement)
        {
            // we don't have any children so this call must be in error.
            // if we implement children for hyperlinks and embedded objects then we'll need to change this.
            throw new InvalidOperationException(SR.Get(SRID.RichEditTextPatternHasNoChildren, GetType().FullName));
        }

        ITextRangeProvider ITextProvider.RangeFromPoint(Point screenLocation)
        {
            // we must have called EnsureTextDocument() before arriving here.
            Debug.Assert(_document != null);

            // TextPattern has verified that the point is inside our client area so we don't need to check for that.

            // get the degenerate range at the point
            // we're assuming ITextDocument::RangeFromPoint always returns a degenerate range
            ITextRange range = _document.RangeFromPoint((int)screenLocation.X, (int)screenLocation.Y);

            Debug.Assert(range.Start == range.End);

            // if you wanted to get the character under the point instead of the degenerate range nearest
            // the point, then you would add:

            //// if the point is within the character to the right then expand the degenerate range to
            //// include the character.
            //Rect rect;
            //range.MoveEnd(TomUnit.tomCharacter, 1);
            //rect = WindowsRichEditRange.GetSingleLineRangeRectangle(range, BoundingRectangle);
            //if (!rect.Contains(screenLocation))
            //{
            //    // if the point is within the character to the left then expand the degenerate range
            //    // to include the character.
            //    range.Collapse(TomStartEnd.tomStart);
            //    range.MoveStart(TomUnit.tomCharacter, -1);
            //    rect = WindowsRichEditRange.GetSingleLineRangeRectangle(range, BoundingRectangle);
            //    if (!rect.Contains(screenLocation))
            //    {
            //        // the point is not in an adjacent character cell so leave it degenerate.
            //        range.Collapse(TomStartEnd.tomEnd);
            //    }
            //}

            return(range != null ? new WindowsRichEditRange(range, this) : null);
        }

        #endregion TextPattern Methods

        #region TextPattern Properties

        ITextRangeProvider ITextProvider.DocumentRange
        {
            get
            {
                // we must have called EnsureTextDocument() before arriving here.
                Debug.Assert(_document != null);

                // create a text range that covers the entire main story.
                ITextRange range = _document.Range(0, 0);
                range.SetRange(0, range.StoryLength);
                return(new WindowsRichEditRange(range, this));
            }
        }


        SupportedTextSelection ITextProvider.SupportedTextSelection
        {
            get
            {
                return(SupportedTextSelection.Single);
            }
        }
Esempio n. 6
0
        public static string ConvertToHtml(RichEditBox richEditBox, List <string> list)
        {
            string strColour, strFntName, strHTML;

            richEditBox.Document.GetText(TextGetOptions.None, out string text);
            ITextRange txtRange = richEditBox.Document.GetRange(0, text.Length);

            strHTML = "<!DOCTYPE html><html>";
            if (Settings.Default.HTMLEncoding != 0)
            {
                strHTML += "<meta charset=\"" + list[Settings.Default.HTMLEncoding] + "\">";
            }
            strHTML += "<head><title>" + Settings.Default.DefaultExportName + "</title></head>";
            int   lngOriginalStart  = txtRange.StartPosition;
            int   lngOriginalLength = txtRange.EndPosition;
            float shtSize           = 11;
            // txtRange.SetRange(txtRange.StartPosition, txtRange.EndPosition);
            bool bOpened = false, liOpened = false, numbLiOpened = false, iOpened = false, uOpened = false, bulletOpened = false, numberingOpened = false;

            for (int i = 0; i < text.Length; i++)
            {
                txtRange.SetRange(i, i + 1);
                if (i == 0)
                {
                    strColour  = Windows.UI.Colors.Black.ToHex().ToString();
                    shtSize    = txtRange.CharacterFormat.Size;
                    strFntName = txtRange.CharacterFormat.Name;
                    strHTML   += "<span style=\"font-family:" + strFntName + "; font-size: " + shtSize + "pt; color: #" + strColour.Substring(3) + "\">";
                }
                if (txtRange.CharacterFormat.Size != shtSize)
                {
                    shtSize  = txtRange.CharacterFormat.Size;
                    strHTML += "</span><span style=\"font-family: " + txtRange.CharacterFormat.Name + "; font-size: " + txtRange.CharacterFormat.Size + "pt; color: #" + txtRange.CharacterFormat.ForegroundColor.ToString().Substring(3) + "\">";
                }
                if (txtRange.Character == Convert.ToChar(13))
                {
                    strHTML += "<br/>";
                }
                #region bullet
                if (txtRange.ParagraphFormat.ListType == MarkerType.Bullet)
                {
                    if (!bulletOpened)
                    {
                        strHTML     += "<ul>";
                        bulletOpened = true;
                    }

                    if (!liOpened)
                    {
                        strHTML += "<li>";
                        liOpened = true;
                    }

                    if (txtRange.Character == Convert.ToChar(13))
                    {
                        strHTML += "</li>";
                        liOpened = false;
                    }
                }
                else
                {
                    if (bulletOpened)
                    {
                        strHTML     += "</ul>";
                        bulletOpened = false;
                    }
                }
                #endregion
                #region numbering
                if (txtRange.ParagraphFormat.ListType == MarkerType.LowercaseRoman)
                {
                    if (!numberingOpened)
                    {
                        strHTML        += "<ol type=\"i\">";
                        numberingOpened = true;
                    }

                    if (!numbLiOpened)
                    {
                        strHTML     += "<li>";
                        numbLiOpened = true;
                    }

                    if (txtRange.Character == Convert.ToChar(13))
                    {
                        strHTML     += "</li>";
                        numbLiOpened = false;
                    }
                }
                else
                {
                    if (numberingOpened)
                    {
                        strHTML        += "</ol>";
                        numberingOpened = false;
                    }
                }
                #endregion
                #region bold
                if (txtRange.CharacterFormat.Bold == FormatEffect.On)
                {
                    if (!bOpened)
                    {
                        strHTML += "<b>";
                        bOpened  = true;
                    }
                }
                else
                {
                    if (bOpened)
                    {
                        strHTML += "</b>";
                        bOpened  = false;
                    }
                }
                #endregion
                #region italic
                if (txtRange.CharacterFormat.Italic == FormatEffect.On)
                {
                    if (!iOpened)
                    {
                        strHTML += "<i>";
                        iOpened  = true;
                    }
                }
                else
                {
                    if (iOpened)
                    {
                        strHTML += "</i>";
                        iOpened  = false;
                    }
                }
                #endregion
                #region underline
                if (txtRange.CharacterFormat.Underline == UnderlineType.Single)
                {
                    if (!uOpened)
                    {
                        strHTML += "<u>";
                        uOpened  = true;
                    }
                }
                else
                {
                    if (uOpened)
                    {
                        strHTML += "</u>";
                        uOpened  = false;
                    }
                }
                #endregion
                strHTML += txtRange.Character;
            }
            strHTML += "</span></html>";
            return(strHTML);
        }
        public static void ToHtml(ITextDocument doc)
        {
            // Takes a RichEditBox control and returns a
            // simple HTML-formatted version of its contents
            string     strHTML, strFont, strColour, strBold, strFntName;
            float      shtSize;
            int        lngOriginalStart, lngOriginalLength;
            int        intCount = 0;
            ITextRange tr       = doc.GetRange(0, 9999999);

            // If nothing in the box, exit
            if (tr.Length == 0)
            {
                //App.Current.Exit();
            }

            // Store original selections, then select first character
            lngOriginalStart  = 0;
            lngOriginalLength = tr.Length;
            tr.SetRange(0, 1);

            // Add HTML header
            strHTML = "<html>";

            // Set up initial parameters
            strColour  = tr.CharacterFormat.ForegroundColor.ToString();
            strFont    = tr.CharacterFormat.FontStyle.ToString();
            shtSize    = tr.CharacterFormat.Size;
            strBold    = tr.CharacterFormat.Bold.ToString();
            strFntName = tr.CharacterFormat.Name;
            Debug.WriteLine("Colour: " + strColour);

            // Include first 'style' parameters in the HTML
            strHTML += "<span style=\"font-family:" + strFntName + "; font-size: " + shtSize + "pt; color: #" + strColour.Substring(3) + "\">";

            // Include bold tag, if required
            if (strBold.ToLower() == "on")
            {
                strHTML += "<b>";
            }
            // Include italic tag, if required
            if (strFont.ToLower() == "italic")
            {
                strHTML += "<i>";
            }

            // Finally, add our first character
            strHTML += tr.Character;

            // Loop around all remaining characters
            for (intCount = 2; intCount < lngOriginalLength; intCount++)
            {
                // Select current character
                tr.SetRange(intCount - 1, intCount + 1);

                //     If this is a line break, add HTML tag
                if (tr.Character == Convert.ToChar(13))
                {
                    strHTML += "<br />";
                }

                //    ' Check/implement any changes in style
                if (tr.CharacterFormat.ForegroundColor.ToString() != strColour || tr.CharacterFormat.Name != strFntName || tr.CharacterFormat.Size != shtSize)
                {
                    strHTML += "</span><span style=\"font-family: " + tr.CharacterFormat.Name + "; font size: " + tr.CharacterFormat.Size + "pt; color: #" + tr.CharacterFormat.ForegroundColor.ToString().Substring(3) + "\">";
                }

                //     Check for bold changes
                if (tr.CharacterFormat.Bold.ToString().ToLower() != strBold.ToLower())
                {
                    if (tr.CharacterFormat.Bold.ToString().ToLower() != "on")
                    {
                        strHTML += "</b>";
                    }
                    else
                    {
                        strHTML += "<b>";
                    }
                }

                //    Check for italic changes
                if (tr.CharacterFormat.FontStyle.ToString().ToLower() != strFont.ToLower())
                {
                    if (tr.CharacterFormat.FontStyle.ToString().ToLower() != "italic")
                    {
                        strHTML += "</i>";
                    }
                    else
                    {
                        strHTML += "<i>";
                    }
                }

                //    ' Add the actual character
                strHTML += tr.Character;

                //    ' Update variables with current style
                strColour  = tr.CharacterFormat.ForegroundColor.ToString();
                strFont    = tr.CharacterFormat.FontStyle.ToString();
                shtSize    = tr.CharacterFormat.Size;
                strFntName = tr.CharacterFormat.Name;
                strBold    = tr.CharacterFormat.Bold.ToString();
            }

            // Close off any open bold/italic tags
            if (strBold == "on")
            {
                strHTML += "";
            }
            if (strFont.ToLower() == "italic")
            {
                strHTML += "";
            }

            // Terminate outstanding HTML tags
            strHTML += "</span></html>";

            //' Restore original RichTextBox selection
            tr.SetRange(lngOriginalStart, lngOriginalLength);

            doc.SetText(TextSetOptions.FormatRtf, strHTML);
        }
Esempio n. 8
0
 public void SetRange(int active, int other)
 {
     _range.SetRange(active, other);
 }