-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextView.cs
147 lines (122 loc) · 3.37 KB
/
TextView.cs
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
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;
namespace NotepadCS
{
public partial class TextView : UserControl, ITextDataListener
{
private TextCaret Caret = new TextCaret();
private List<string> viewedRows = null;
private int row;
private int column;
private Font font = new Font( FontFamily.GenericSansSerif, 8.0f );
private Brush brush = Brushes.Black;
private Pen pen = Pens.Black;
private StringFormat stringFormat = new StringFormat()
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
};
private Brush selectedBrush = new SolidBrush( Color.FromArgb( 128, 0, 0, 255 ) );
private TextData textData = null;
public TextData TextData
{
get { return textData; }
set
{
if( textData != value )
{
if( textData != null)
textData.RemoveListener(this);
textData = value;
textData.AppendListener(this);
//redraw view from textData
}
}
}
public float CellWidth = 10;
public float CellHeight = 12;
public TextView()
{
InitializeComponent();
this.SetStyle( ControlStyles.OptimizedDoubleBuffer, true );
}
protected override void OnPaint( PaintEventArgs e )
{
if( textData == null )
return;
e.Graphics.Clear( Color.White );
var listText = textData.ReadRange( 0, textData.Rows );
var oxs = 5.0f;
var oys = 5.0f;
var ox = oxs;
var oy = oys;
foreach( var rowText in listText )
{
foreach( var character in rowText )
{
//e.Graphics.DrawRectangle( pen, ox, oy, CellWidth, CellHeight );
e.Graphics.DrawString( character.ToString(), font, brush, ox + CellWidth/2 , oy + CellHeight/2, stringFormat );
//e.Graphics.FillRectangle( selectedBrush, ox, oy, CellWidth, CellHeight );
ox += CellWidth;
}
ox = oxs;
oy += CellHeight;
}
Caret.Draw(e.Graphics);
e.Graphics.DrawRectangle( Pens.Black, 0, 0, this.Width-1, this.Height-1 );
}
protected override void OnPaintBackground( PaintEventArgs e )
{
//not needed if on paint fills entire background
//if only a small region is invalidated this will paint its background?
}
public void TextDataInsert( int rowNumber, IEnumerable<string> rows )
{
this.Invalidate();
}
public void TextDataRemove( int rowNumber, int rowCount )
{
//redraw only the updated rows from textdata and only if they are currently being looked at
//? or lets keep it simple for now and just fetch and draw all viewed rows
this.Invalidate();
}
private void TextView_MouseClick( object sender, MouseEventArgs e )
{
}
private void TextView_KeyPress( object sender, KeyPressEventArgs e )
{
//get selected rowText
//modify selected rowText
textData.InsertRange( 0, new string[]{e.KeyChar + ""} );
e.Handled = true;
//todo invalidate only a small region
this.Invalidate();
}
private void TextView_KeyDown( object sender, KeyEventArgs e )
{
}
private void TextView_KeyUp( object sender, KeyEventArgs e )
{
}
private void TextView_SizeChanged( object sender, EventArgs e )
{
this.Invalidate();
}
}
class TextCaret
{
public int x, y, w = 1, h = 10;
private Pen pen = Pens.Black;
//draw caret
public void Draw(Graphics g)
{
g.DrawRectangle( pen, x, y, x + w, y + h );
}
}
}