/// <summary> /// Builds a list of HilightMatchEntry objects. A HilightMatchEntry spans over a region that is painted with the same foreground and /// background colors. /// All regions which don't match a word-mode entry will be painted with the colors of a default entry (groundEntry). This is either the /// first matching non-word-mode highlight entry or a black-on-white default (if no matching entry was found). /// </summary> /// <param name="matchList">List of all highlight matches for the current cell</param> /// <param name="groundEntry">The entry that is used as the default.</param> /// <returns>List of HilightMatchEntry objects. The list spans over the whole cell and contains color infos for every substring.</returns> private static IList <HilightMatchEntry> MergeHighlightMatchEntries(IList <HilightMatchEntry> matchList, HilightMatchEntry groundEntry) { // Fill an area with lenth of whole text with a default hilight entry HilightEntry[] entryArray = new HilightEntry[groundEntry.Length]; for (int i = 0; i < entryArray.Length; ++i) { entryArray[i] = groundEntry.HilightEntry; } // "overpaint" with all matching word match enries // Non-word-mode matches will not overpaint because they use the groundEntry foreach (HilightMatchEntry me in matchList) { int endPos = me.StartPos + me.Length; for (int i = me.StartPos; i < endPos; ++i) { if (me.HilightEntry.IsWordMatch) { entryArray[i] = me.HilightEntry; } else { //entryArray[i].ForegroundColor = me.HilightEntry.ForegroundColor; } } } // collect areas with same hilight entry and build new highlight match entries for it IList <HilightMatchEntry> mergedList = new List <HilightMatchEntry>(); if (entryArray.Length > 0) { HilightEntry currentEntry = entryArray[0]; int lastStartPos = 0; int pos = 0; for (; pos < entryArray.Length; ++pos) { if (entryArray[pos] != currentEntry) { HilightMatchEntry me = new HilightMatchEntry(); me.StartPos = lastStartPos; me.Length = pos - lastStartPos; me.HilightEntry = currentEntry; mergedList.Add(me); currentEntry = entryArray[pos]; lastStartPos = pos; } } HilightMatchEntry me2 = new HilightMatchEntry(); me2.StartPos = lastStartPos; me2.Length = pos - lastStartPos; me2.HilightEntry = currentEntry; mergedList.Add(me2); } return(mergedList); }
void GetHighlightEntryMatches(string line, IList<HilightEntry> hilightEntryList, IList<HilightMatchEntry> resultList) { foreach (HilightEntry entry in hilightEntryList) { if (entry.IsWordMatch) { MatchCollection matches = entry.Regex.Matches(line); foreach (Match match in matches) { HilightMatchEntry me = new HilightMatchEntry(); me.HilightEntry = entry; me.StartPos = match.Index; me.Length = match.Length; resultList.Add(me); } } else { if (CheckHighlightEntryMatch(entry, line)) { HilightMatchEntry me = new HilightMatchEntry(); me.HilightEntry = entry; me.StartPos = 0; me.Length = line.Length; resultList.Add(me); } } } }
/// <summary> /// Builds a list of HilightMatchEntry objects. A HilightMatchEntry spans over a region that is painted with the same foreground and /// background colors. /// All regions which don't match a word-mode entry will be painted with the colors of a default entry (groundEntry). This is either the /// first matching non-word-mode highlight entry or a black-on-white default (if no matching entry was found). /// </summary> /// <param name="matchList">List of all highlight matches for the current cell</param> /// <param name="groundEntry">The entry that is used as the default.</param> /// <returns>List of HilightMatchEntry objects. The list spans over the whole cell and contains color infos for every substring.</returns> IList<HilightMatchEntry> MergeHighlightMatchEntries(IList<HilightMatchEntry> matchList, HilightMatchEntry groundEntry) { // Fill an area with lenth of whole text with a default hilight entry HilightEntry[] entryArray = new HilightEntry[groundEntry.Length]; for (int i = 0; i < entryArray.Length; ++i) { entryArray[i] = groundEntry.HilightEntry; } // "overpaint" with all matching word match enries // Non-word-mode matches will not overpaint because they use the groundEntry foreach (HilightMatchEntry me in matchList) { int endPos = me.StartPos + me.Length; for (int i = me.StartPos; i < endPos; ++i) { if (me.HilightEntry.IsWordMatch) { entryArray[i] = me.HilightEntry; } } } // collect areas with same hilight entry and build new highlight match entries for it IList<HilightMatchEntry> mergedList = new List<HilightMatchEntry>(); if (entryArray.Length > 0) { HilightEntry currentEntry = entryArray[0]; int lastStartPos = 0; int pos = 0; for (; pos < entryArray.Length; ++pos) { if (entryArray[pos] != currentEntry) { HilightMatchEntry me = new HilightMatchEntry(); me.StartPos = lastStartPos; me.Length = pos - lastStartPos; me.HilightEntry = currentEntry; mergedList.Add(me); currentEntry = entryArray[pos]; lastStartPos = pos; } } HilightMatchEntry me2 = new HilightMatchEntry(); me2.StartPos = lastStartPos; me2.Length = pos - lastStartPos; me2.HilightEntry = currentEntry; mergedList.Add(me2); } return mergedList; }
private static void PaintHighlightedCell(ILogPaintContext logPaintCtx, DataGridViewCellPaintingEventArgs e, DataGridView gridView, bool noBackgroundFill, HilightEntry groundEntry) { object value = e.Value != null ? e.Value : ""; IList <HilightMatchEntry> matchList = logPaintCtx.FindHilightMatches(value as string); // too many entries per line seem to cause problems with the GDI while (matchList.Count > 50) { matchList.RemoveAt(50); } var hme = new HilightMatchEntry(); hme.StartPos = 0; hme.Length = (value as string).Length; hme.HilightEntry = new HilightEntry((value as string), groundEntry != null ? groundEntry.ForegroundColor : Color.FromKnownColor(KnownColor.Black), groundEntry != null ? groundEntry.BackgroundColor : Color.Empty, false); matchList = MergeHighlightMatchEntries(matchList, hme); int leftPad = e.CellStyle.Padding.Left; RectangleF rect = new RectangleF(e.CellBounds.Left + leftPad, e.CellBounds.Top, e.CellBounds.Width, e.CellBounds.Height); Rectangle borderWidths = BorderWidths(e.AdvancedBorderStyle); Rectangle valBounds = e.CellBounds; valBounds.Offset(borderWidths.X, borderWidths.Y); valBounds.Width -= borderWidths.Right; valBounds.Height -= borderWidths.Bottom; if (e.CellStyle.Padding != Padding.Empty) { valBounds.Offset(e.CellStyle.Padding.Left, e.CellStyle.Padding.Top); valBounds.Width -= e.CellStyle.Padding.Horizontal; valBounds.Height -= e.CellStyle.Padding.Vertical; } TextFormatFlags flags = TextFormatFlags.Left | TextFormatFlags.SingleLine | TextFormatFlags.NoPrefix | TextFormatFlags.PreserveGraphicsClipping | TextFormatFlags.NoPadding | TextFormatFlags.VerticalCenter | TextFormatFlags.TextBoxControl ; // | TextFormatFlags.VerticalCenter // | TextFormatFlags.TextBoxControl // TextFormatFlags.SingleLine //TextRenderer.DrawText(e.Graphics, e.Value as String, e.CellStyle.Font, valBounds, Color.FromKnownColor(KnownColor.Black), flags); Point wordPos = valBounds.Location; Size proposedSize = new Size(valBounds.Width, valBounds.Height); Rectangle r = gridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true); e.Graphics.SetClip(e.CellBounds); foreach (HilightMatchEntry matchEntry in matchList) { Font font = matchEntry != null && matchEntry.HilightEntry.IsBold ? logPaintCtx.BoldFont : logPaintCtx.NormalFont; Brush bgBrush = matchEntry.HilightEntry.BackgroundColor != Color.Empty ? new SolidBrush(matchEntry.HilightEntry.BackgroundColor) : null; string matchWord = (value as string).Substring(matchEntry.StartPos, matchEntry.Length); Size wordSize = TextRenderer.MeasureText(e.Graphics, matchWord, font, proposedSize, flags); wordSize.Height = e.CellBounds.Height; Rectangle wordRect = new Rectangle(wordPos, wordSize); Color foreColor = matchEntry.HilightEntry.ForegroundColor; if ((e.State & DataGridViewElementStates.Selected) != DataGridViewElementStates.Selected) { if (!noBackgroundFill && bgBrush != null && !matchEntry.HilightEntry.NoBackground) { e.Graphics.FillRectangle(bgBrush, wordRect); } } else { if (foreColor.Equals(Color.Black)) { foreColor = Color.White; } } TextRenderer.DrawText(e.Graphics, matchWord, font, wordRect, foreColor, flags); wordPos.Offset(wordSize.Width, 0); if (bgBrush != null) { bgBrush.Dispose(); } } }
public static void PaintHighlightedCell(ILogPaintContext logPaintCtx, DataGridViewCellPaintingEventArgs e, DataGridView gridView, bool noBackgroundFill, HilightEntry groundEntry) { object value = e.Value != null ? e.Value : ""; IList<HilightMatchEntry> matchList = logPaintCtx.FindHilightMatches(value as string); // too many entries per line seem to cause problems with the GDI while (matchList.Count > 50) { matchList.RemoveAt(50); } var hme = new HilightMatchEntry(); hme.StartPos = 0; hme.Length = (value as string).Length; hme.HilightEntry = new HilightEntry((value as string), groundEntry != null ? groundEntry.ForegroundColor : Color.FromKnownColor(KnownColor.Black), groundEntry != null ? groundEntry.BackgroundColor : Color.Empty, false); matchList = MergeHighlightMatchEntries(matchList, hme); Rectangle borderWidths = BorderWidths(e.AdvancedBorderStyle); Rectangle valBounds = e.CellBounds; valBounds.Offset(borderWidths.X, borderWidths.Y); valBounds.Width -= borderWidths.Right; valBounds.Height -= borderWidths.Bottom; if (e.CellStyle.Padding != Padding.Empty) { valBounds.Offset(e.CellStyle.Padding.Left, e.CellStyle.Padding.Top); valBounds.Width -= e.CellStyle.Padding.Horizontal; valBounds.Height -= e.CellStyle.Padding.Vertical; } TextFormatFlags flags = TextFormatFlags.Left | TextFormatFlags.SingleLine | TextFormatFlags.NoPrefix | TextFormatFlags.PreserveGraphicsClipping | TextFormatFlags.NoPadding | TextFormatFlags.VerticalCenter | TextFormatFlags.TextBoxControl ; // | TextFormatFlags.VerticalCenter // | TextFormatFlags.TextBoxControl // TextFormatFlags.SingleLine //TextRenderer.DrawText(e.Graphics, e.Value as String, e.CellStyle.Font, valBounds, Color.FromKnownColor(KnownColor.Black), flags); Point wordPos = valBounds.Location; Size proposedSize = new Size(valBounds.Width, valBounds.Height); e.Graphics.SetClip(e.CellBounds); foreach (HilightMatchEntry matchEntry in matchList) { Font font = matchEntry != null && matchEntry.HilightEntry.IsBold ? logPaintCtx.BoldFont : logPaintCtx.NormalFont; Brush bgBrush = matchEntry.HilightEntry.BackgroundColor != Color.Empty ? new SolidBrush(matchEntry.HilightEntry.BackgroundColor) : null; string matchWord = (value as string).Substring(matchEntry.StartPos, matchEntry.Length); Size wordSize = TextRenderer.MeasureText(e.Graphics, matchWord, font, proposedSize, flags); wordSize.Height = e.CellBounds.Height; Rectangle wordRect = new Rectangle(wordPos, wordSize); Color foreColor = matchEntry.HilightEntry.ForegroundColor; if ((e.State & DataGridViewElementStates.Selected) != DataGridViewElementStates.Selected) { if (!noBackgroundFill && bgBrush != null && !matchEntry.HilightEntry.NoBackground) { e.Graphics.FillRectangle(bgBrush, wordRect); } } else { if (foreColor.Equals(Color.Black)) { foreColor = Color.White; } } TextRenderer.DrawText(e.Graphics, matchWord, font, wordRect, foreColor, flags); wordPos.Offset(wordSize.Width, 0); if (bgBrush != null) { bgBrush.Dispose(); } } }