Exemplo n.º 1
0
        static void ConvertTabsToSpaces(IUserInterface ui,
                                        int begin, int end, ref int delta)
        {
            int           tabCount = 0;
            Document      doc      = ui.Document;
            StringBuilder text     = new StringBuilder(1024);

            for (int i = begin; i < end; i++)
            {
                if (doc[i] == '\t')
                {
                    tabCount++;
                    string spaces = UiImpl.GetTabEquivalentSpaces(ui, i);
                    text.Append(spaces);
                }
                else
                {
                    text.Append(doc[i]);
                }
            }

            // Replace with current content
            if (0 < tabCount)
            {
                doc.Replace(text.ToString(), begin, end);
            }

            delta += text.Length - (end - begin);
        }
Exemplo n.º 2
0
        public void HandleContentChanged(object sender, ContentChangedEventArgs e)
        {
            UiImpl   ui  = (UiImpl)sender;
            Document doc = ui.Document;
            int      lineIndex;
            int      lineHead, lineEnd;
            bool     shouldBeRedrawn;

            if (doc.MarksUri == false)
            {
                return;
            }

            // update marking in this line
            lineIndex       = doc.GetLineIndexFromCharIndex(e.Index);
            shouldBeRedrawn = MarkOrUnmarkOneLine(doc, lineIndex, true);
            if (shouldBeRedrawn)
            {
                // update entire graphic of the logical line
                // if marking bits associated with any character was changed
                lineHead = doc.GetLineHeadIndex(lineIndex);
                lineEnd  = lineHead + doc.GetLineLength(lineIndex);
                ui.View.Invalidate(lineHead, lineEnd);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Pastes clipboard content at where the caret is at.
        /// </summary>
        public static void Paste(IUserInterface ui)
        {
            Document     doc = ui.Document;
            string       clipboardText;
            int          begin, end;
            int          insertIndex;
            TextDataType dataType;

            // do nothing if the document is read-only
            if (doc.IsReadOnly)
            {
                Plat.Inst.MessageBeep();
                return;
            }

            // get clipboard content
            clipboardText = Plat.Inst.GetClipboardText(out dataType);
            if (clipboardText == null)
            {
                return;
            }

            // limit the content in a single line if it's in single line mode
            if (ui.IsSingleLineMode)
            {
                int eolIndex = clipboardText.IndexOfAny(new char[] { '\r', '\n' });
                if (0 <= eolIndex)
                {
                    clipboardText = clipboardText.Remove(eolIndex, clipboardText.Length - eolIndex);
                }
            }

            // begin grouping edit action
            doc.BeginUndo();

            // delete currently selected text before insertion
            doc.GetSelection(out begin, out end);
            if (doc.RectSelectRanges != null)
            {
                //--- case of rectangle selection ---
                // delete selected text
                doc.DeleteRectSelectText();
                ui.View.Invalidate();
            }
            else if (begin != end)
            {
                //--- case of normal selection ---
                // delete selected text
                doc.Replace("");
            }

            // paste according type of the text data
            if (dataType == TextDataType.Rectangle)
            {
                //--- rectangle text data ---
                Point  insertPos;
                int    rowBegin;
                int    rowEnd;
                string rowText;
                string padding;

                // Insert every row at same column position
                insertPos = ui.View.GetVirPosFromIndex(doc.CaretIndex);
                rowBegin  = 0;
                rowEnd    = TextUtil.NextLineHead(clipboardText, rowBegin);
                while (0 <= rowEnd)
                {
                    // get this row content
                    if (clipboardText[rowEnd - 1] == '\n')
                    {
                        if (clipboardText[rowEnd - 2] == '\r')
                        {
                            rowText = clipboardText.Substring(rowBegin, rowEnd - rowBegin - 2);
                        }
                        else
                        {
                            rowText = clipboardText.Substring(rowBegin, rowEnd - rowBegin - 1);
                        }
                    }
                    else if (clipboardText[rowEnd - 1] == '\r')
                    {
                        rowText = clipboardText.Substring(rowBegin, rowEnd - rowBegin - 1);
                    }
                    else
                    {
                        rowText = clipboardText.Substring(rowBegin, rowEnd - rowBegin);
                    }

                    // pad tabs if needed
                    padding = UiImpl.GetNeededPaddingChars(ui, insertPos, false);

                    // insert this row
                    insertIndex = ui.View.GetIndexFromVirPos(insertPos);
                    doc.Replace(padding.ToString() + rowText, insertIndex, insertIndex);

                    // goto next line
                    insertPos.Y += ui.LineSpacing;
                    rowBegin     = rowEnd;
                    rowEnd       = TextUtil.NextLineHead(clipboardText, rowBegin);
                }
            }
            else
            {
                //--- normal or line text data ---
                // calculate insertion index
                insertIndex = begin;
                if (dataType == TextDataType.Line)
                {
                    // make the insertion point to caret line head if it is line data type
                    insertIndex = doc.GetLineHeadIndexFromCharIndex(begin);
                }

                // insert
                doc.Replace(clipboardText, insertIndex, insertIndex);
            }

            // end grouping UNDO action
            doc.EndUndo();

            // move caret
            if (ui.UsesStickyCaret == false)
            {
                ui.View.SetDesiredColumn();
            }
            ui.ScrollToCaret();
        }