Пример #1
0
            async void CopyData(TextEditorData data, MonoDevelop.Ide.Editor.Selection selection)
            {
                if (!selection.IsEmpty && data != null && data.Document != null)
                {
                    this.docStyle = data.ColorStyle;
                    this.options  = data.Options;
                    copyData      = null;


                    switch (selection.SelectionMode)
                    {
                    case MonoDevelop.Ide.Editor.SelectionMode.Normal:
                        isBlockMode = false;
                        var segment = selection.GetSelectionRange(data);
                        copiedColoredChunks = await ClipboardColoredText.GetChunks(data, segment);

                        var pasteHandler = data.TextPasteHandler;
                        if (pasteHandler != null)
                        {
                            try {
                                copyData = pasteHandler.GetCopyData(segment.Offset, segment.Length);
                            } catch (Exception e) {
                                Console.WriteLine("Exception while getting copy data:" + e);
                            }
                        }
                        break;

                    case MonoDevelop.Ide.Editor.SelectionMode.Block:
                        isBlockMode = true;
                        var visStart = data.LogicalToVisualLocation(selection.Anchor);
                        var visEnd   = data.LogicalToVisualLocation(selection.Lead);
                        int startCol = System.Math.Min(visStart.Column, visEnd.Column);
                        int endCol   = System.Math.Max(visStart.Column, visEnd.Column);
                        copiedColoredChunks = new List <List <ClipboardColoredText> > ();
                        for (int lineNr = selection.MinLine; lineNr <= selection.MaxLine; lineNr++)
                        {
                            DocumentLine curLine = data.Document.GetLine(lineNr);
                            int          col1    = curLine.GetLogicalColumn(data, startCol) - 1;
                            int          col2    = System.Math.Min(curLine.GetLogicalColumn(data, endCol) - 1, curLine.Length);
                            if (col1 < col2)
                            {
                                copiedColoredChunks.Add((await ClipboardColoredText.GetChunks(data, new TextSegment(curLine.Offset + col1, col2 - col1))).First());
                            }
                            else
                            {
                                copiedColoredChunks.Add(new List <ClipboardColoredText> ());
                            }
                        }
                        break;
                    }
                }
                else
                {
                    copiedColoredChunks = null;
                }
            }
Пример #2
0
        internal static string GenerateRtf(List <List <ClipboardColoredText> > chunks, MonoDevelop.Ide.Editor.Highlighting.EditorTheme style, ITextEditorOptions options)
        {
            var rtfText   = new StringBuilder();
            var colorList = new List <Cairo.Color> ();

            bool isItalic = false;
            bool isBold   = false;
            int  curColor = -1;

            foreach (var line in chunks)
            {
                bool appendSpace = false;
                foreach (var chunk in line)
                {
                    var chunkStyle = style.GetChunkStyle(chunk.ScopeStack);
                    if (isBold != (chunkStyle.FontWeight == Xwt.Drawing.FontWeight.Bold))
                    {
                        isBold = chunkStyle.FontWeight == Xwt.Drawing.FontWeight.Bold;
                        rtfText.Append(isBold ? @"\b" : @"\b0");
                        appendSpace = true;
                    }
                    if (isItalic != (chunkStyle.FontStyle == Xwt.Drawing.FontStyle.Italic))
                    {
                        isItalic = chunkStyle.FontStyle == Xwt.Drawing.FontStyle.Italic;
                        rtfText.Append(isItalic ? @"\i" : @"\i0");
                        appendSpace = true;
                    }
                    var foreground = chunkStyle.Foreground;
                    if (!colorList.Contains(foreground))
                    {
                        colorList.Add(foreground);
                    }
                    int color = colorList.IndexOf(foreground);
                    if (curColor != color)
                    {
                        curColor = color;
                        rtfText.Append(@"\cf").Append(curColor + 1);
                        appendSpace = true;
                    }
                    AppendRtfText(rtfText, chunk.Text, ref appendSpace);
                }
                rtfText.AppendLine(@"\line");
            }

            var rtf = new StringBuilder();

            rtf.AppendLine(@"{\rtf1\ansi\deff0\adeflang1025");
            rtf.AppendLine(@"{\fonttbl");
            rtf.Append(@"{\f0\fnil\fprq1\fcharset128 ").Append(options.Font.Family).AppendLine(";}");
            rtf.AppendLine("}");
            rtf.Append(CreateColorTable(colorList));
            rtf.AppendLine(@"\viewkind4\uc1\pard");
            rtf.AppendLine(@"\f0");
            try {
                string fontName = options.Font.ToString();
                double fontSize = Double.Parse(fontName.Substring(fontName.LastIndexOf(' ') + 1), System.Globalization.CultureInfo.InvariantCulture) * 2;
                rtf.Append(@"\fs");
                rtf.Append(fontSize);
            } catch (Exception) {};
            rtf.AppendLine(@"\cf1");
            rtf.Append(rtfText.ToString());
            rtf.Append("}");
            return(rtf.ToString());
        }