Ejemplo n.º 1
0
        public void Draw(object item, Graphics g, Rectangle boundRect, bool drawSelected)
        {
            if (!IsHandled(item))
            {
                return;
            }

            RichText.RichText text = ((ListItem)myItems[item]).RichText;
            Image             icon = ((ListItem)myItems[item]).Icon;

            Rectangle rect        = CalculateItemRectangle(item, boundRect);
            Rectangle contentRect = CalculateContentRectangle(item, g, rect);

            // Center content vertically
            if (contentRect.Height < rect.Height)
            {
                int topOffset = (rect.Height - contentRect.Height) / 2;
                contentRect.Y += topOffset;
            }

            //object selectedItem = myListBox.SelectedIndex < 0 || myListBox.SelectedIndex >= myListBox.Items.Count
            //  ? null : myListBox.Items[myListBox.SelectedIndex];

            g.FillRectangle(new SolidBrush(myListBox.BackColor), boundRect);

            if (drawSelected)
            {
                Color backgroundColor = Colors.ListSelectionBackColor(myListBox.Focused);

                text = (RichText.RichText)text.Clone();

                g.FillRectangle(new SolidBrush(backgroundColor), rect);
                text.SetColors(SystemColors.HighlightText, backgroundColor);
            }

            if (icon != null)
            {
                DrawIcon(g, icon, boundRect, item);
            }

            IntPtr hdc = g.GetHdc();

            try
            {
                text.Draw(hdc, contentRect);
                //System.Console.WriteLine(text.ToString());
            }
            finally
            {
                g.ReleaseHdc(hdc);
            }

            if (drawSelected && myListBox.Focused)
            {
                DrawDottedRectangle(g, rect);
            }
        }
Ejemplo n.º 2
0
        private static void QuickFind(RichText.RichText richText, string text, bool next, bool fromStart = false)
        {
            if (string.IsNullOrEmpty(text))
            {
                return;
            }

            Range selection = richText.Selection;

            selection.Normalize();

            int startLine;
            int?startIndex;

            if (next)
            {
                startLine  = selection.Start.iLine;
                startIndex = selection.Start.iChar + selection.Text.Length;
            }
            else
            {
                startLine  = selection.End.iLine;
                startIndex = selection.End.iChar - selection.Text.Length;
            }

            if (fromStart)
            {
                startLine  = next ? 0 : richText.LinesCount - 1;
                startIndex = next ? 0 : richText.Lines.LastOrDefault()?.Length ?? 0;
            }

            int?resultLine              = null;
            int?resultIndex             = null;
            StringComparison comparison =
                Settings.Default.FindMatchCase ? StringComparison.InvariantCulture : StringComparison.InvariantCultureIgnoreCase;

            if (next)
            {
                for (int i = startLine; i < richText.LinesCount; i++)
                {
                    startIndex ??= 0;
                    string lineText   = richText[i].Text;
                    int    textLength = lineText.Length;
                    if (textLength >= startIndex.Value &&
                        lineText.Substring(startIndex.Value, lineText.Length - startIndex.Value)
                        .IndexOf(text, comparison) is var findIndex and >= 0)
                    {
                        resultLine  = i;
                        resultIndex = findIndex + startIndex;
                        break;
                    }
                    else
                    {
                        startIndex = null;
                    }
                }
Ejemplo n.º 3
0
        /// <summary>
        /// Calculated content rectangle of node
        /// </summary>
        private Rectangle CalculateContentRectangle(TreeNode node, IntPtr hdc, int offset)
        {
            Rectangle rect = new Rectangle(node.TreeView.ClientRectangle.Left, node.Bounds.Top, node.TreeView.ClientSize.Width, node.Bounds.Height);

            RichText.RichText text = (RichText.RichText)myNodes[node];

            rect.X     += offset;
            rect.Width -= offset;

            return(new Rectangle(rect.Location, text.GetSize(hdc).ToSize()));
        }
Ejemplo n.º 4
0
        public void Draw(System.Windows.Forms.ListViewItem item, System.Drawing.Graphics g, Rectangle boundRect)
        {
            if (!IsHandled(item))
            {
                return;
            }

            ListView listView = item.ListView;

            RichText.RichText text        = (RichText.RichText)myItems[item];
            Rectangle         rect        = CalculateItemRectangle(item, boundRect);
            Rectangle         contentRect = CalculateContentRectangle(item, g, rect);

            // Center content vertically
            if (contentRect.Height < rect.Height)
            {
                int topOffset = (rect.Height - contentRect.Height) / 2;
                contentRect.Y += topOffset;
            }

            g.FillRectangle(new SolidBrush(listView.BackColor), rect);

            if (item.Selected)
            {
                Color backgroundColor = Colors.ListSelectionBackColor(listView.Focused);

                text = (RichText.RichText)text.Clone();

                g.FillRectangle(new SolidBrush(backgroundColor), rect);
                text.SetColors(SystemColors.HighlightText, backgroundColor);
            }

            g.SetClip(rect);

            IntPtr hdc = g.GetHdc();

            try
            {
                text.Draw(hdc, contentRect);
            }
            finally
            {
                g.ReleaseHdc(hdc);
            }

            if (item.Selected && listView.Focused)
            {
                DrawDottedRectangle(g, rect);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Calculated content rectangle of Item
        /// </summary>
        private Rectangle CalculateContentRectangle(object item, Graphics g, Rectangle rect)
        {
            RichText.RichText text = ((ListItem)myItems[item]).RichText;
            IntPtr            hdc  = g.GetHdc();

            try
            {
                Rectangle result = new Rectangle(rect.Location, text.GetSize(hdc).ToSize());
                result.Width = rect.Width;
                return(result);
            }
            finally
            {
                g.ReleaseHdc(hdc);
            }
        }
Ejemplo n.º 6
0
        public System.Drawing.Size GetSize(object item, System.Drawing.Graphics g)
        {
            if (!IsHandled(item))
            {
                return(new Size());
            }

            RichText.RichText text = ((ListItem)myItems[item]).RichText;
            IntPtr            hdc  = g.GetHdc();

            try
            {
                Size size = text.GetSize(hdc).ToSize();
                size.Height = myListBox.ItemHeight;
                size.Width += 3 + CalculateOffset(item);

                return(size);
            }
            finally
            {
                g.ReleaseHdc(hdc);
            }
        }
Ejemplo n.º 7
0
        public Size GetSize(ListViewItem item, Graphics g)
        {
            if (!IsHandled(item))
            {
                return(new Size());
            }

            RichText.RichText text = (RichText.RichText)myItems[item];
            IntPtr            hdc  = g.GetHdc();

            try
            {
                Size size = text.GetSize(hdc).ToSize();
                size.Height = item.Bounds.Height;
                size.Width += 3 + CalculateOffset(item);

                return(size);
            }
            finally
            {
                g.ReleaseHdc(hdc);
            }
        }
Ejemplo n.º 8
0
        public static void UpdateLines(Dictionary <int, string> updateLines)
        {
            RichText.RichText tasText = Studio.Instance.richText;
            foreach (int lineNumber in updateLines.Keys)
            {
                string lineText = updateLines[lineNumber];
                if (tasText.Lines.Count > lineNumber)
                {
                    Line line = tasText.TextSource[lineNumber];
                    line.Clear();
                    if (lineText.Length > 0)
                    {
                        line.AddRange(lineText.ToCharArray().Select(c => new Char(c)));
                        Range range = new(tasText, 0, lineNumber, line.Count, lineNumber);
                        range.SetStyle(SyntaxHighlighter.ChocolateStyle);
                    }
                }
            }

            if (updateLines.Count > 0)
            {
                tasText.NeedRecalc();
            }
        }
Ejemplo n.º 9
0
        public static Span ToSpan(this RichText.RichText richtext)
        {
            var    span      = new Span();
            string plaintext = richtext.Text;

            foreach (RichString part in richtext.GetFormattedParts())
            {
                var run = new Run(plaintext.Substring(part.Offset, part.Length));
                span.Inlines.Add(run);

                if ((part.Style.FontStyle & FontStyle.Bold) != 0)
                {
                    run.FontWeight = FontWeights.Bold;
                }
                if ((part.Style.FontStyle & FontStyle.Italic) != 0)
                {
                    run.FontStyle = FontStyles.Italic;
                }
                if ((part.Style.FontStyle & FontStyle.Underline) != 0)
                {
                    run.TextDecorations.Add(TextDecorations.Underline);
                }
                if ((part.Style.FontStyle & FontStyle.Strikeout) != 0)
                {
                    run.TextDecorations.Add(TextDecorations.Strikethrough);
                }

                System.Drawing.Color color = part.Style.ForegroundColor;
                if (!color.IsEmpty)
                {
                    run.Foreground = new SolidColorBrush(Color.FromArgb(color.A, color.R, color.G, color.B));
                }
                color = part.Style.BackgroundColor;
                if (!color.IsEmpty)
                {
                    run.Background = new SolidColorBrush(Color.FromArgb(color.A, color.R, color.G, color.B));
                }

                color = part.Style.EffectColor;
                Pen pen = null;
                if (!color.IsEmpty)
                {
                    pen = new Pen {
                        Brush = new SolidColorBrush(Color.FromArgb(color.A, color.R, color.G, color.B))
                    }
                }
                ;

                switch (part.Style.Effect)
                {
                case TextStyle.EffectStyle.None:
                    break;

                case TextStyle.EffectStyle.StraightUnderline:
                    run.TextDecorations.Add(new TextDecoration {
                        Location = TextDecorationLocation.Underline, Pen = pen
                    });
                    break;

                case TextStyle.EffectStyle.WeavyUnderline:
                    run.TextDecorations.Add(new TextDecoration {
                        Location = TextDecorationLocation.Underline, Pen = pen
                    });
                    break;

                case TextStyle.EffectStyle.StrikeOut:
                    run.TextDecorations.Add(new TextDecoration {
                        Location = TextDecorationLocation.Strikethrough, Pen = pen
                    });
                    break;

                default:
                    throw new InvalidOperationException("Effect.");
                }
            }
            return(span);
        }

        #endregion
    }
Ejemplo n.º 10
0
 /// <summary>
 /// Adds text to a List Item
 /// </summary>
 public void Add(object item, RichText.RichText text, Image icon)
 {
     myItems[item] = new ListItem(text, icon);
 }
Ejemplo n.º 11
0
 public ListItem(RichText.RichText richText, Image icon)
 {
     myRichText = richText;
     myIcon     = icon;
 }
Ejemplo n.º 12
0
 /// <summary>
 /// Adds text to a List Item
 /// </summary>
 public void Add(ListViewItem item, RichText.RichText text)
 {
     myItems[item] = text;
     //InvalidateItem(Item);
 }
Ejemplo n.º 13
0
        public static void ShowFindDialog(RichText.RichText richText)
        {
            const int padding      = 10;
            const int buttonWidth  = 75;
            const int buttonHeight = 30;

            Size size       = new(300, buttonHeight * 2 + padding * 3);
            bool pressEnter = false;

            using Form inputBox      = new();
            inputBox.FormBorderStyle = FormBorderStyle.FixedDialog;
            inputBox.ClientSize      = size;
            inputBox.Text            = "Find";
            inputBox.StartPosition   = FormStartPosition.CenterParent;
            inputBox.MinimizeBox     = false;
            inputBox.MaximizeBox     = false;
            inputBox.KeyPreview      = true;
            inputBox.KeyDown        += (sender, args) => {
                if (args.KeyCode == Keys.Escape)
                {
                    inputBox.Close();
                }
                else
                {
                    pressEnter = args.KeyCode == Keys.Enter;
                }
            };

            TextBox textBox = new();

            textBox.Size      = new Size(size.Width - 3 * padding - buttonWidth, buttonHeight);
            textBox.Location  = new Point(padding, padding);
            textBox.Font      = new Font(FontFamily.GenericSansSerif, 12);
            textBox.ForeColor = Color.FromArgb(50, 50, 50);
            textBox.Text      = richText.SelectedText.Length == 0 ? lastFindText : richText.SelectedText;
            textBox.SelectAll();
            textBox.KeyDown  += (sender, args) => pressEnter = args.KeyCode == Keys.Enter;
            textBox.KeyPress += (sender, args) => {
                if (pressEnter)
                {
                    args.Handled = true;
                    QuickFind(richText, textBox.Text, true);
                }
            };
            textBox.KeyUp += (sender, args) => lastFindText = textBox.Text;

            inputBox.KeyPress += (sender, args) => {
                if (pressEnter)
                {
                    args.Handled = true;
                    QuickFind(richText, textBox.Text, true);
                }
            };
            inputBox.Controls.Add(textBox);

            Button previousButton = new();

            previousButton.Name     = "previouButton";
            previousButton.Size     = new Size(buttonWidth, buttonHeight);
            previousButton.Text     = "&Previous";
            previousButton.Location = new Point(textBox.Right + padding, textBox.Top);
            previousButton.Click   += (sender, args) => QuickFind(richText, textBox.Text, false);
            inputBox.Controls.Add(previousButton);

            Button nextButton = new();

            nextButton.Name     = "nextButton";
            nextButton.Size     = new Size(buttonWidth, buttonHeight);
            nextButton.Text     = "&Next";
            nextButton.Location = new Point(previousButton.Left, previousButton.Bottom + padding);
            nextButton.Click   += (sender, args) => QuickFind(richText, textBox.Text, true);
            inputBox.Controls.Add(nextButton);

            CheckBox caseCheckbox = new();

            caseCheckbox.Size            = new Size(textBox.Width, buttonHeight);
            caseCheckbox.Text            = "&Match case";
            caseCheckbox.Location        = new Point(textBox.Left, nextButton.Top);
            caseCheckbox.Checked         = Settings.Default.FindMatchCase;
            caseCheckbox.CheckedChanged += (sender, args) => Settings.Default.FindMatchCase = caseCheckbox.Checked;
            inputBox.Controls.Add(caseCheckbox);

            inputBox.ShowDialog();
        }
Ejemplo n.º 14
0
        public void Draw(TreeNode node, IntPtr hdc, Rectangle rect)
        {
            if (!IsHandled(node) || node.Bounds.IsEmpty || node.IsEditing)
            {
                return;
            }

            CustomTreeView.CustomTreeView treeView = (CustomTreeView.CustomTreeView)node.TreeView;

            int offset = CalculateOffset(node);

            RichText.RichText text = (RichText.RichText)myNodes[node];

            Rectangle contentRect = CalculateContentRectangle(node, hdc, offset);

            rect.X     += offset;
            rect.Width -= offset;

            Rectangle fullRect = new Rectangle(contentRect.X - 1, node.Bounds.Top, contentRect.Width + 5, node.Bounds.Height);

            //g.SetClip(fullRect);

            TreeNode dropHiliteNode = treeView.DraggingOver ? treeView.DropHighlightedNode : null;
            bool     hasFocus       = node.TreeView.Focused;
            bool     drawSelected   = treeView.DraggingOver
          ? (node == dropHiliteNode)
          : node.IsSelected && hasFocus;
            bool drawNonfocusedSelection = node.IsSelected && !node.TreeView.HideSelection &&
                                           (!hasFocus || treeView.DraggingOver);

            Color backColor;

            if (drawSelected)
            {
                backColor = SystemColors.Highlight;
            }
            else if (drawNonfocusedSelection)
            {
                backColor = SystemColors.Control;
            }
            else
            {
                backColor = treeView.BackColor;
            }

            IntPtr hBrush = Win32Declarations.CreateSolidBrush(Win32Declarations.ColorToRGB(backColor));

            RECT lineRect = new RECT(fullRect.Left - 1, node.Bounds.Top, fullRect.Right + 1, node.Bounds.Bottom);

            Win32Declarations.FillRect(hdc, ref lineRect, hBrush);
            Win32Declarations.DeleteObject(hBrush);

            if (drawSelected)
            {
                text = (RichText.RichText)text.Clone();
                text.SetColors(SystemColors.HighlightText, SystemColors.Highlight);
            }
            else if (drawNonfocusedSelection)
            {
                text = (RichText.RichText)text.Clone();
                text.SetColors(SystemColors.WindowText, SystemColors.Control);
            }

            text.Draw(hdc, contentRect);

            if (hasFocus && treeView.SelectedNode == node && treeView.NeedFocusRect() && dropHiliteNode == null)
            {
                RECT rc = new RECT(fullRect.Left - 1, fullRect.Top, fullRect.Right + 1, fullRect.Bottom);
                Win32Declarations.DrawFocusRect(hdc, ref rc);
            }
        }
Ejemplo n.º 15
0
 /// <summary>
 /// Adds text to a tree node
 /// </summary>
 public void Add(TreeNode node, RichText.RichText text)
 {
     myNodes [node] = text;
     InvalidateNode(node);
 }