-
Notifications
You must be signed in to change notification settings - Fork 186
Expand file tree
/
Copy pathBufferedDataGridView.cs
More file actions
431 lines (357 loc) · 12 KB
/
BufferedDataGridView.cs
File metadata and controls
431 lines (357 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
using System.ComponentModel;
using System.Drawing.Drawing2D;
using System.Runtime.Versioning;
using LogExpert.Core.Entities;
using LogExpert.Core.EventArguments;
using NLog;
namespace LogExpert.UI.Controls;
[SupportedOSPlatform("windows")]
internal partial class BufferedDataGridView : DataGridView
{
#region Fields
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
private static Color BubbleColor =>
Application.IsDarkModeEnabled
? Color.FromArgb(160, 80, 80, 0) // muted yellow on dark
: Color.FromArgb(160, 250, 250, 0); // bright yellow on light
private static Color TextColor =>
Application.IsDarkModeEnabled
? Color.FromArgb(200, 180, 200, 255) // light blue on dark
: Color.FromArgb(200, 0, 0, 90); // dark blue on light
private readonly Font _font = new("Segoe UI", 9.75f);
private Pen? _pen;
private Brush? _brush;
private Brush? _textBrush;
private Color _currentBubbleColor;
private Color _currentTextColor;
private readonly StringFormat _format = new()
{
LineAlignment = StringAlignment.Center,
Alignment = StringAlignment.Near
};
private readonly Lock _overlayLock = new();
private readonly List<BookmarkOverlay> _overlayStaging = [];
private BookmarkOverlay[] _overlaySnapshot = [];
private BookmarkOverlay? _draggedOverlay;
private Point _dragStartPoint;
private bool _isDrag;
private Size _oldOverlayOffset;
#endregion
#region cTor
public BufferedDataGridView ()
{
InitializeComponent();
DoubleBuffered = true;
VirtualMode = true;
}
#endregion
#region Events
public event EventHandler<OverlayEventArgs> OverlayDoubleClicked;
#endregion
#region Properties
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public ContextMenuStrip EditModeMenuStrip { get; set; }
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public bool PaintWithOverlays { get; set; }
#endregion
#region Public methods
public void AddOverlay (BookmarkOverlay overlay)
{
lock (_overlayLock)
{
_overlayStaging.Add(overlay);
}
}
/// <summary>
/// Atomically captures all staged overlays and clears the staging list. Call this once per paint cycle before
/// drawing.
/// </summary>
private BookmarkOverlay[] SwapOverlaySnapshot ()
{
lock (_overlayLock)
{
_overlaySnapshot = [.. _overlayStaging];
_overlayStaging.Clear();
return _overlaySnapshot;
}
}
/// <summary>
/// Ensures GDI+ drawing resources match the current color mode.
/// Called at the start of each paint cycle.
/// </summary>
private void EnsureDrawingResources ()
{
var bubbleColor = BubbleColor;
var textColor = TextColor;
if (bubbleColor == _currentBubbleColor
&& textColor == _currentTextColor
&& _pen is not null)
{
return;
}
_pen?.Dispose();
_brush?.Dispose();
_textBrush?.Dispose();
_currentBubbleColor = bubbleColor;
_currentTextColor = textColor;
_pen = new Pen(_currentBubbleColor, 3.0f);
_brush = new SolidBrush(_currentBubbleColor);
_textBrush = new SolidBrush(_currentTextColor);
}
#endregion
#region Overrides
protected override void Dispose (bool disposing)
{
if (disposing)
{
components?.Dispose();
_brush?.Dispose();
_pen?.Dispose();
_textBrush?.Dispose();
_font?.Dispose();
_format?.Dispose();
}
base.Dispose(disposing);
}
protected override void OnPaint (PaintEventArgs e)
{
try
{
if (PaintWithOverlays)
{
PaintOverlays(e);
}
else
{
base.OnPaint(e);
}
}
catch (Exception ex)
{
_logger.Error($"Overlay painting failed, falling back to base paint. {ex}");
try
{
base.OnPaint(e);
}
catch (Exception innerEx)
{
_logger.Error($"Base paint also failed. {innerEx}");
}
}
}
private void PaintOverlays (PaintEventArgs e)
{
EnsureDrawingResources();
// Let the base DataGridView paint into its own double buffer first.
base.OnPaint(e);
// Atomically capture and clear staged overlays. No lock held after this.
var overlays = SwapOverlaySnapshot();
if (overlays.Length == 0)
{
return;
}
// Save the original clip and set up overlay clipping area.
var originalClip = e.Graphics.Clip;
e.Graphics.SetClip(DisplayRectangle, CombineMode.Replace);
// Exclude column headers from overlay drawing area.
Rectangle rectTableHeader = new(
DisplayRectangle.X,
DisplayRectangle.Y,
DisplayRectangle.Width,
ColumnHeadersHeight);
e.Graphics.SetClip(rectTableHeader, CombineMode.Exclude);
foreach (var overlay in overlays)
{
var textSize = e.Graphics.MeasureString(overlay.Bookmark.Text, _font, 300);
Rectangle rectBubble = new(
overlay.Position,
new Size((int)textSize.Width,
(int)textSize.Height));
rectBubble.Offset(60, -(rectBubble.Height + 40));
rectBubble.Inflate(3, 3);
rectBubble.Location += overlay.Bookmark.OverlayOffset;
overlay.BubbleRect = rectBubble;
// Temporarily extend clip to include the bubble area.
e.Graphics.SetClip(rectBubble, CombineMode.Union);
e.Graphics.SetClip(rectTableHeader, CombineMode.Exclude);
RectangleF textRect = new(
rectBubble.X,
rectBubble.Y,
rectBubble.Width,
rectBubble.Height);
e.Graphics.FillRectangle(_brush, rectBubble);
e.Graphics.DrawLine(
_pen,
overlay.Position,
new Point(rectBubble.X, rectBubble.Y + rectBubble.Height));
e.Graphics.DrawString(overlay.Bookmark.Text, _font, _textBrush, textRect, _format);
if (_logger.IsDebugEnabled)
{
_logger.Debug($"### PaintOverlays: {e.Graphics.ClipBounds.Left}, {e.Graphics.ClipBounds.Top}, {e.Graphics.ClipBounds.Width}, {e.Graphics.ClipBounds.Height}");
}
}
// Restore original clip region.
e.Graphics.Clip = originalClip;
}
protected override void OnEditingControlShowing (DataGridViewEditingControlShowingEventArgs e)
{
base.OnEditingControlShowing(e);
e.Control.KeyDown -= OnControlKeyDown;
e.Control.KeyDown += OnControlKeyDown;
var editControl = (DataGridViewTextBoxEditingControl)e.Control;
e.Control.PreviewKeyDown -= OnControlPreviewKeyDown;
e.Control.PreviewKeyDown += OnControlPreviewKeyDown;
editControl.ContextMenuStrip = EditModeMenuStrip;
}
protected override void OnMouseDown (MouseEventArgs e)
{
var overlay = GetOverlayForPosition(e.Location);
if (overlay != null)
{
if (e.Button == MouseButtons.Right)
{
if (_isDrag)
{
_isDrag = false;
overlay.Bookmark.OverlayOffset = _oldOverlayOffset;
Refresh();
}
}
else
{
_dragStartPoint = e.Location;
_isDrag = true;
_draggedOverlay = overlay;
_oldOverlayOffset = overlay.Bookmark.OverlayOffset;
}
}
else
{
_isDrag = false;
base.OnMouseDown(e);
}
}
protected override void OnMouseUp (MouseEventArgs e)
{
if (_isDrag)
{
_isDrag = false;
Refresh();
}
else
{
base.OnMouseUp(e);
}
}
protected override void OnMouseMove (MouseEventArgs e)
{
if (_isDrag && _draggedOverlay is not null)
{
Cursor = Cursors.Hand;
Size offset = new(e.X - _dragStartPoint.X, e.Y - _dragStartPoint.Y);
_draggedOverlay.Bookmark.OverlayOffset = _oldOverlayOffset + offset;
Refresh();
}
else
{
var overlay = GetOverlayForPosition(e.Location);
Cursor = overlay != null ? Cursors.Hand : Cursors.Default;
base.OnMouseMove(e);
}
}
protected override void OnMouseDoubleClick (MouseEventArgs e)
{
var overlay = GetOverlayForPosition(e.Location);
if (overlay != null)
{
if (e.Button == MouseButtons.Left)
{
OnOverlayDoubleClicked(new OverlayEventArgs(overlay));
}
}
else
{
base.OnMouseDoubleClick(e);
}
}
protected override void OnMouseLeave (EventArgs e)
{
if (!_isDrag)
{
Cursor = Cursors.Default;
}
base.OnMouseLeave(e);
}
#endregion
#region Private Methods
private BookmarkOverlay GetOverlayForPosition (Point pos)
{
var overlays = _overlaySnapshot;
foreach (var overlay in overlays)
{
if (overlay.BubbleRect.Contains(pos))
{
return overlay;
}
}
return null;
}
#endregion
#region Events handler
private void OnControlPreviewKeyDown (object sender, PreviewKeyDownEventArgs e)
{
if ((e.KeyCode == Keys.C || e.KeyCode == Keys.Insert) && e.Control)
{
if (EditingControl != null)
{
e.IsInputKey = true;
}
}
}
private void OnControlKeyDown (object sender, KeyEventArgs e)
{
if (e.KeyCode is Keys.Up or Keys.Down)
{
if (EditingControl != null)
{
if (EditingControl is LogCellEditingControl editControl)
{
_ = editControl.EditingControlDataGridView.EndEdit();
var line = editControl.EditingControlDataGridView.CurrentCellAddress.Y;
if (e.KeyCode == Keys.Up)
{
if (line > 0)
{
line--;
}
}
if (e.KeyCode == Keys.Down)
{
if (line < editControl.EditingControlDataGridView.RowCount - 1)
{
line++;
}
}
var col = editControl.EditingControlDataGridView.CurrentCellAddress.X;
var scrollIndex = editControl.EditingControlDataGridView.HorizontalScrollingOffset;
var selStart = editControl.SelectionStart;
editControl.EditingControlDataGridView.CurrentCell = editControl.EditingControlDataGridView.Rows[line].Cells[col];
_ = editControl.EditingControlDataGridView.BeginEdit(false);
editControl.SelectionStart = selStart;
editControl.ScrollToCaret();
editControl.EditingControlDataGridView.HorizontalScrollingOffset = scrollIndex;
e.Handled = true;
}
//else
//{
// _logger.Warn($"Edit control was null, to be checked");
//}
}
}
}
protected virtual void OnOverlayDoubleClicked (OverlayEventArgs e)
{
OverlayDoubleClicked?.Invoke(this, e);
}
#endregion
}