void DrawFoldMarker(Graphics g, RectangleF rectangle, bool isOpened, bool isSelected) { HighlightColor foldMarkerColor = textArea.Document.HighlightingStrategy.GetEnvironmentColorForName("FoldMarker"); HighlightColor foldLineColor = textArea.Document.HighlightingStrategy.GetEnvironmentColorForName("FoldLine"); HighlightColor selectedFoldLine = textArea.Document.HighlightingStrategy.GetEnvironmentColorForName("SelectedFoldLine"); Rectangle intRect = new Rectangle((int)rectangle.X, (int)rectangle.Y, (int)rectangle.Width, (int)rectangle.Height); g.FillRectangle(BrushRegistry.GetBrush(foldMarkerColor.BackgroundColor), intRect); g.DrawRectangle(BrushRegistry.GetPen(isSelected ? selectedFoldLine.Color : foldMarkerColor.Color), intRect); int space = (int)Math.Round(((double)rectangle.Height) / 8d) + 1; int mid = intRect.Height / 2 + intRect.Height % 2; g.DrawLine(BrushRegistry.GetPen(foldLineColor.BackgroundColor), rectangle.X + space, rectangle.Y + mid, rectangle.X + rectangle.Width - space, rectangle.Y + mid); if (!isOpened) { g.DrawLine(BrushRegistry.GetPen(foldLineColor.BackgroundColor), rectangle.X + mid, rectangle.Y + space, rectangle.X + mid, rectangle.Y + rectangle.Height - space); } }
Brush GetBgColorBrush(int lineNumber) { if (DrawLineMarkerAtLine(lineNumber)) //如果当前行是标记行(即要画充满整个TextArea宽度的一行) { HighlightColor caretLine = textArea.Document.HighlightingStrategy.GetEnvironmentColorForName("CaretMarker"); return(BrushRegistry.GetBrush(caretLine.Color)); } HighlightColor background = (HighlightColor)textArea.Document.HighlightingStrategy.GetEnvironmentColorForName("Default"); return(BrushRegistry.GetBrush(background.BackgroundColor)); }
float DrawEOLMarker(Graphics g, Color color, Brush backBrush, float x, float y) { float width = GetWidth(g, '\u00B6'); g.FillRectangle(backBrush, new RectangleF(x, y, width, fontHeight)); HighlightColor eolMarkerColor = textArea.Document.HighlightingStrategy.GetEnvironmentColorForName("EOLMarkers"); g.DrawString("\u00B6", eolMarkerColor.Font, BrushRegistry.GetBrush(color), x, y, measureStringFormat); return(width); }
/// <summary> /// Get the marker brush (for solid block markers) at a given position. /// </summary> /// <param name="offset">The offset.</param> /// <param name="length">The length.</param> /// <returns>The Brush or null when no marker was found.</returns> Brush GetMarkerBrushAt(int offset, int length) { ArrayList markers = Document.TextMarkerStrategy.GetMarkers(offset, length); foreach (TextMarker marker in markers) { if (marker.TextMarkerType == TextMarkerType.SolidBlock) { return(BrushRegistry.GetBrush(marker.Color)); } } return(null); }
float DrawDocumentWord(Graphics g, string word, PointF position, Font font, Color foreColor, Brush backBrush) { if (word == null || word.Length == 0) { return(0f); } float wordWidth = g.MeasureString(word, font, 32768, measureStringFormat).Width; g.FillRectangle(backBrush, new RectangleF(position.X, position.Y, (float)Math.Ceiling(wordWidth), FontHeight)); g.DrawString(word, font, BrushRegistry.GetBrush(foreColor), position.X, position.Y, measureStringFormat); return(wordWidth); }
public override void OnPaint(Graphics g, Rectangle rect) { if (rect.Width <= 0 || rect.Height <= 0) { return; } HighlightColor lineNumberPainterColor = textArea.Document.HighlightingStrategy.GetEnvironmentColorForName("LineNumbers"); HighlightColor foldLineColor = textArea.Document.HighlightingStrategy.GetEnvironmentColorForName("FoldLine"); for (int y = 0; y < (DrawingRectangle.Height + textArea.TextViewMargin.VisibleLineDrawingRemainder) / textArea.TextViewMargin.FontHeight + 1; ++y) { Rectangle markerRectangle = new Rectangle(DrawingRectangle.X, DrawingRectangle.Top + y * textArea.TextViewMargin.FontHeight - textArea.TextViewMargin.VisibleLineDrawingRemainder, DrawingRectangle.Width, textArea.TextViewMargin.FontHeight); if (rect.IntersectsWith(markerRectangle)) { // draw dotted separator line if (textArea.Document.TextEditorProperties.ShowLineNumbers) { g.FillRectangle(BrushRegistry.GetBrush(textArea.Enabled ? lineNumberPainterColor.BackgroundColor : SystemColors.InactiveBorder), new Rectangle(markerRectangle.X + 1, markerRectangle.Y, markerRectangle.Width - 1, markerRectangle.Height)); g.DrawLine(BrushRegistry.GetDotPen(lineNumberPainterColor.Color, lineNumberPainterColor.BackgroundColor), base.DrawingRectangle.X, markerRectangle.Y, base.DrawingRectangle.X, markerRectangle.Bottom); } else { g.FillRectangle(BrushRegistry.GetBrush(textArea.Enabled ? lineNumberPainterColor.BackgroundColor : SystemColors.InactiveBorder), markerRectangle); } int currentLine = textArea.Document.GetFirstLogicalLine(textArea.Document.GetVisibleLine(textArea.TextViewMargin.FirstVisibleLine) + y); PaintFoldMarker(g, currentLine, markerRectangle); } } }
void DrawLine(Graphics g, LineSegment line, float yPos, RectangleF margin) { float xPos = 0; float fontHeight = Font.GetHeight(g); // bool gotNonWhitespace = false; curTabIndent = 0; foreach (TextWord word in line.Words) { switch (word.Type) { case TextWordType.Space: Advance(ref xPos, ref yPos, margin.Width, textAreaControl.TextArea.TextViewMargin.GetWidth(' '), fontHeight); // if (!gotNonWhitespace) { // curTabIndent = xPos; // } break; case TextWordType.Tab: Advance(ref xPos, ref yPos, margin.Width, TabIndent * textAreaControl.TextArea.TextViewMargin.GetWidth(' '), fontHeight); // if (!gotNonWhitespace) { // curTabIndent = xPos; // } break; case TextWordType.Word: // if (!gotNonWhitespace) { // gotNonWhitespace = true; // curTabIndent += TabIndent * textAreaControl.TextArea.TextView.GetWidth(' '); // } g.DrawString(word.Word, word.Font, BrushRegistry.GetBrush(word.Color), xPos + margin.X, yPos); SizeF drawingSize = g.MeasureString(word.Word, word.Font, new SizeF(margin.Width, fontHeight * 100), printingStringFormat); Advance(ref xPos, ref yPos, margin.Width, drawingSize.Width, fontHeight); break; } } }
void DrawMarker(Graphics g, TextMarker marker, RectangleF drawingRect) { float drawYPos = drawingRect.Bottom - 1; switch (marker.TextMarkerType) { case TextMarkerType.Underlined: g.DrawLine(BrushRegistry.GetPen(marker.Color), drawingRect.X, drawYPos, drawingRect.Right, drawYPos); break; case TextMarkerType.WaveLine: int reminder = ((int)drawingRect.X) % 6; for (float i = drawingRect.X - reminder; i < drawingRect.Right + reminder; i += 6) { g.DrawLine(BrushRegistry.GetPen(marker.Color), i, drawYPos + 3 - 4, i + 3, drawYPos + 1 - 4); g.DrawLine(BrushRegistry.GetPen(marker.Color), i + 3, drawYPos + 1 - 4, i + 6, drawYPos + 3 - 4); } break; case TextMarkerType.SolidBlock: g.FillRectangle(BrushRegistry.GetBrush(marker.Color), drawingRect); break; } }
void DrawInvalidLineMarker(Graphics g, float x, float y) { HighlightColor invalidLinesColor = textArea.Document.HighlightingStrategy.GetEnvironmentColorForName("InvalidLines"); g.DrawString("~", invalidLinesColor.Font, BrushRegistry.GetBrush(invalidLinesColor.Color), x, y, measureStringFormat); }
void DrawBracketHighlight(Graphics g, Rectangle rect) { g.FillRectangle(BrushRegistry.GetBrush(Color.FromArgb(50, 0, 0, 255)), rect); g.DrawRectangle(Pens.Blue, rect); }
float PaintLinePart(Graphics g, int lineNumber, int startColumn, int endColumn, Rectangle lineRectangle, float physicalXPos) { bool drawLineMarker = DrawLineMarkerAtLine(lineNumber); Brush bgColorBrush = GetBgColorBrush(lineNumber); Brush backgroundBrush = textArea.Enabled ? bgColorBrush : SystemBrushes.InactiveBorder; HighlightColor selectionColor = textArea.Document.HighlightingStrategy.GetEnvironmentColorForName("Selection"); ColumnRange selectionRange = textArea.SelectionManager.GetSelectionAtLine(lineNumber); HighlightColor tabMarkerColor = textArea.Document.HighlightingStrategy.GetEnvironmentColorForName("TabMarkers"); HighlightColor spaceMarkerColor = textArea.Document.HighlightingStrategy.GetEnvironmentColorForName("SpaceMarkers"); float spaceWidth = GetWidth(g, ' '); LineSegment currentLine = textArea.Document.GetLineSegment(lineNumber); int logicalColumn = startColumn; Brush selectionBackgroundBrush = BrushRegistry.GetBrush(selectionColor.BackgroundColor); Brush unselectedBackgroundBrush = backgroundBrush; if (currentLine.Words != null) { int startword = 0; int wordOffset = 0; //首先查找第一个不是空格或Tab的单词. for (; startword < currentLine.Words.Count; ++startword) { if (wordOffset >= startColumn) { break; } TextWord currentWord = ((TextWord)currentLine.Words[startword]); if (currentWord.Type == TextWordType.Tab) { ++wordOffset; } else if (currentWord.Type == TextWordType.Space) { ++wordOffset; } else { wordOffset += currentWord.Length; } } for (int i = startword; i < currentLine.Words.Count; ++i) { // 到行的末尾(即全部的单词都画完了),则退出for语句. if (logicalColumn >= endColumn) { break; } ArrayList markers = Document.TextMarkerStrategy.GetMarkers(currentLine.Offset + wordOffset); foreach (TextMarker marker in markers) { if (marker.TextMarkerType == TextMarkerType.SolidBlock) { unselectedBackgroundBrush = BrushRegistry.GetBrush(marker.Color); break; } } // Clear old marker arrary // TODO: cut the word if startColumn or endColimn is in the word; // needed for foldings wich can start or end in the middle of a word TextWord currentWord = ((TextWord)currentLine.Words[i]); switch (currentWord.Type) { case TextWordType.Space: RectangleF spaceRectangle = new RectangleF(physicalXPos, lineRectangle.Y, (float)Math.Ceiling(spaceWidth), lineRectangle.Height); Brush spaceBackgroundBrush; if (ColumnRange.WholeColumn.Equals(selectionRange) || logicalColumn >= selectionRange.StartColumn && logicalColumn < selectionRange.EndColumn) { spaceBackgroundBrush = selectionBackgroundBrush; } else { Brush markerBrush = GetMarkerBrushAt(currentLine.Offset + logicalColumn, 1); if (!drawLineMarker && markerBrush != null) { spaceBackgroundBrush = markerBrush; } else if (!drawLineMarker && currentWord.HighlightColor != null && currentWord.HighlightColor.HasBackground) { spaceBackgroundBrush = BrushRegistry.GetBrush(currentWord.HighlightColor.BackgroundColor); } else { spaceBackgroundBrush = unselectedBackgroundBrush; } } g.FillRectangle(spaceBackgroundBrush, spaceRectangle); if (TextEditorProperties.ShowSpaces) { DrawSpaceMarker(g, spaceMarkerColor.Color, physicalXPos, lineRectangle.Y); } foreach (TextMarker marker in markers) { DrawMarker(g, marker, spaceRectangle); } physicalXPos += spaceWidth; ++logicalColumn; ++physicalColumn; break; case TextWordType.Tab: int oldPhysicalColumn = physicalColumn; physicalColumn += TextEditorProperties.TabIndent; physicalColumn = (physicalColumn / TextEditorProperties.TabIndent) * TextEditorProperties.TabIndent; float tabWidth = (physicalColumn - oldPhysicalColumn) * spaceWidth; RectangleF tabRectangle = new RectangleF(physicalXPos, lineRectangle.Y, (float)Math.Ceiling(tabWidth), lineRectangle.Height); if (ColumnRange.WholeColumn.Equals(selectionRange) || logicalColumn >= selectionRange.StartColumn && logicalColumn <= selectionRange.EndColumn - 1) { spaceBackgroundBrush = selectionBackgroundBrush; } else { Brush markerBrush = GetMarkerBrushAt(currentLine.Offset + logicalColumn, 1); if (!drawLineMarker && markerBrush != null) { spaceBackgroundBrush = markerBrush; } else if (!drawLineMarker && currentWord.HighlightColor != null && currentWord.HighlightColor.HasBackground) { spaceBackgroundBrush = BrushRegistry.GetBrush(currentWord.HighlightColor.BackgroundColor); } else { spaceBackgroundBrush = unselectedBackgroundBrush; } } g.FillRectangle(spaceBackgroundBrush, tabRectangle); if (TextEditorProperties.ShowTabs) { DrawTabMarker(g, tabMarkerColor.Color, physicalXPos, lineRectangle.Y); } foreach (TextMarker marker in markers) { DrawMarker(g, marker, tabRectangle); } physicalXPos += tabWidth; ++logicalColumn; break; case TextWordType.Word: string word = currentWord.Word; float lastPos = physicalXPos; Brush bgMarkerBrush = GetMarkerBrushAt(currentLine.Offset + logicalColumn, word.Length); Brush wordBackgroundBrush; if (!drawLineMarker && bgMarkerBrush != null) { wordBackgroundBrush = bgMarkerBrush; } else if (!drawLineMarker && currentWord.HighlightColor.HasBackground) { wordBackgroundBrush = BrushRegistry.GetBrush(currentWord.HighlightColor.BackgroundColor); } else { wordBackgroundBrush = unselectedBackgroundBrush; } if (ColumnRange.WholeColumn.Equals(selectionRange) || selectionRange.EndColumn - 1 >= word.Length + logicalColumn && selectionRange.StartColumn <= logicalColumn) { physicalXPos += DrawDocumentWord(g, word, new PointF(physicalXPos, lineRectangle.Y), currentWord.Font, selectionColor.HasForgeground ? selectionColor.Color : currentWord.Color, selectionBackgroundBrush); } else { if (ColumnRange.NoColumn.Equals(selectionRange) /* || selectionRange.StartColumn > logicalColumn + word.Length || selectionRange.EndColumn - 1 <= logicalColumn */) { physicalXPos += DrawDocumentWord(g, word, new PointF(physicalXPos, lineRectangle.Y), currentWord.Font, currentWord.Color, wordBackgroundBrush); } else { int offset1 = Math.Min(word.Length, Math.Max(0, selectionRange.StartColumn - logicalColumn)); int offset2 = Math.Max(offset1, Math.Min(word.Length, selectionRange.EndColumn - logicalColumn)); physicalXPos += DrawDocumentWord(g, word.Substring(0, offset1), new PointF(physicalXPos, lineRectangle.Y), currentWord.Font, currentWord.Color, wordBackgroundBrush); physicalXPos += DrawDocumentWord(g, word.Substring(offset1, offset2 - offset1), new PointF(physicalXPos, lineRectangle.Y), currentWord.Font, selectionColor.HasForgeground ? selectionColor.Color : currentWord.Color, selectionBackgroundBrush); physicalXPos += DrawDocumentWord(g, word.Substring(offset2), new PointF(physicalXPos, lineRectangle.Y), currentWord.Font, currentWord.Color, wordBackgroundBrush); } } // if (markerBrush != null) { // markerBrush.Dispose(); // } foreach (TextMarker marker in markers) { if (marker.TextMarkerType != TextMarkerType.SolidBlock) { DrawMarker(g, marker, new RectangleF(lastPos, lineRectangle.Y, (physicalXPos - lastPos), lineRectangle.Height)); } } // draw bracket highlight if (highlight != null) { if (highlight.OpenBrace.Y == lineNumber && highlight.OpenBrace.X == logicalColumn || highlight.CloseBrace.Y == lineNumber && highlight.CloseBrace.X == logicalColumn) { DrawBracketHighlight(g, new Rectangle((int)lastPos, lineRectangle.Y, (int)(physicalXPos - lastPos) - 1, lineRectangle.Height - 1)); } } physicalColumn += word.Length; logicalColumn += word.Length; break; } markers.Clear(); } //for } //if return(physicalXPos); }
void PaintDocumentLine(Graphics g, int lineNumber, Rectangle lineRectangle) { Brush bgColorBrush = GetBgColorBrush(lineNumber); Brush backgroundBrush = textArea.Enabled ? bgColorBrush : SystemBrushes.InactiveBorder; if (lineNumber >= textArea.Document.TotalNumberOfLines) { g.FillRectangle(backgroundBrush, lineRectangle); if (TextEditorProperties.ShowInvalidLines) //判断是否要画无效的行。 { DrawInvalidLineMarker(g, lineRectangle.Left, lineRectangle.Top); } if (TextEditorProperties.ShowVerticalRuler) //判断是否要画垂直标尺。 { DrawVerticalRuler(g, lineRectangle); } return; } float physicalXPos = lineRectangle.X; int column = 0; physicalColumn = 0; if (TextEditorProperties.EnableFolding) { while (true) { ArrayList starts = textArea.Document.FoldingManager.GetFoldedFoldingsWithStartAfterColumn(lineNumber, column - 1); if (starts == null || starts.Count <= 0) { if (lineNumber < textArea.Document.TotalNumberOfLines) { physicalXPos = PaintLinePart(g, lineNumber, column, textArea.Document.GetLineSegment(lineNumber).Length, lineRectangle, physicalXPos); } break; } // search the first starting folding FoldMarker firstFolding = (FoldMarker)starts[0]; foreach (FoldMarker fm in starts) { if (fm.StartColumn < firstFolding.StartColumn) { firstFolding = fm; } } starts.Clear(); physicalXPos = PaintLinePart(g, lineNumber, column, firstFolding.StartColumn, lineRectangle, physicalXPos); column = firstFolding.EndColumn; lineNumber = firstFolding.EndLine; ColumnRange selectionRange2 = textArea.SelectionManager.GetSelectionAtLine(lineNumber); bool drawSelected = ColumnRange.WholeColumn.Equals(selectionRange2) || firstFolding.StartColumn >= selectionRange2.StartColumn && firstFolding.EndColumn <= selectionRange2.EndColumn; physicalXPos = PaintFoldingText(g, lineNumber, physicalXPos, lineRectangle, firstFolding.FoldText, drawSelected); } } else { physicalXPos = PaintLinePart(g, lineNumber, 0, textArea.Document.GetLineSegment(lineNumber).Length, lineRectangle, physicalXPos); } if (lineNumber < textArea.Document.TotalNumberOfLines) { // Paint things after end of line ColumnRange selectionRange = textArea.SelectionManager.GetSelectionAtLine(lineNumber); LineSegment currentLine = textArea.Document.GetLineSegment(lineNumber); HighlightColor selectionColor = textArea.Document.HighlightingStrategy.GetEnvironmentColorForName("Selection"); float spaceWidth = GetWidth(g, ' '); bool selectionBeyondEOL = selectionRange.EndColumn > currentLine.Length || ColumnRange.WholeColumn.Equals(selectionRange); if (TextEditorProperties.ShowEOLMarker) { HighlightColor eolMarkerColor = textArea.Document.HighlightingStrategy.GetEnvironmentColorForName("EOLMarkers"); physicalXPos += DrawEOLMarker(g, eolMarkerColor.Color, selectionBeyondEOL ? bgColorBrush : backgroundBrush, physicalXPos, lineRectangle.Y); } else { if (selectionBeyondEOL) { g.FillRectangle(BrushRegistry.GetBrush(selectionColor.BackgroundColor), new RectangleF(physicalXPos, lineRectangle.Y, spaceWidth, lineRectangle.Height)); physicalXPos += spaceWidth; } } Brush fillBrush = selectionBeyondEOL && TextEditorProperties.AllowCaretBeyondEOL ? bgColorBrush : backgroundBrush; g.FillRectangle(fillBrush, new RectangleF(physicalXPos, lineRectangle.Y, lineRectangle.Width - physicalXPos + lineRectangle.X, lineRectangle.Height)); } if (TextEditorProperties.ShowVerticalRuler) { DrawVerticalRuler(g, lineRectangle); } }
void DrawTabMarker(Graphics g, Color color, float x, float y) { HighlightColor tabMarkerColor = textArea.Document.HighlightingStrategy.GetEnvironmentColorForName("TabMarkers"); g.DrawString("\u00BB", tabMarkerColor.Font, BrushRegistry.GetBrush(color), x, y, measureStringFormat); }