forked from sbozzie/test-intel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageView.cs
More file actions
88 lines (77 loc) · 2.82 KB
/
Copy pathMessageView.cs
File metadata and controls
88 lines (77 loc) · 2.82 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PleaseIgnore.IntelMap;
namespace TestIntelReporter {
public partial class MessageView : Control {
private const int maxMessages = 100;
private readonly Queue<MessageData> messages
= new Queue<MessageData>(maxMessages);
public MessageView() {
this.DoubleBuffered = true;
}
public void PushMessage(IntelEventArgs message) {
if (messages.Count > maxMessages) {
messages.Dequeue();
}
messages.Enqueue(new MessageData {
Message = message
});
this.Invalidate();
}
protected override void OnResize(EventArgs e) {
// Will need to lay it out again
foreach (var message in messages) {
message.Lines = null;
}
base.OnResize(e);
}
protected override void OnFontChanged(EventArgs e) {
// Will need to lay it out again
foreach (var message in messages) {
message.Lines = null;
}
base.OnFontChanged(e);
}
protected override void OnPaint(PaintEventArgs e) {
var rect = this.ClientRectangle;
float height = rect.Bottom;
using (var brush = new SolidBrush(this.ForeColor)) {
foreach (var message in this.messages.Reverse()) {
// Check if we need to layout the line
if (message.Lines == null) {
// Compute the initial string
var str = String.Format(
Application.CurrentCulture,
"[{0:HH:mm:ss}] {1}",
message.Message.Timestamp,
message.Message.Message);
var size = e.Graphics.MeasureString(str, this.Font);
// TODO: Word wrap, etc.
// Store the finished string
message.Lines = str;
message.Height = size.Height;
}
// Render the text
height -= message.Height;
e.Graphics.DrawString(message.Lines, this.Font, brush, 0, height);
// Cease rendering once we get atop the screen
if (height <= 0) {
break;
}
}
}
base.OnPaint(e);
}
private class MessageData {
public IntelEventArgs Message;
public string Lines;
public float Height;
}
}
}