Browse Source
Text is drawn by System.Drawing. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@6395 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61pull/1/head
4 changed files with 113 additions and 98 deletions
@ -1,110 +1,124 @@
@@ -1,110 +1,124 @@
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Drawing.Drawing2D; |
||||
using System.Globalization; |
||||
using System.Linq; |
||||
using System.Text; |
||||
using System.Windows.Forms; |
||||
using System.Drawing; |
||||
using ICSharpCode.SharpDevelop.Gui.ClassBrowser; |
||||
using System.Windows; |
||||
using System.Windows.Media; |
||||
using System.Windows.Media.Imaging; |
||||
using System.Windows.Threading; |
||||
|
||||
namespace ICSharpCode.CodeQualityAnalysis.Controls |
||||
{ |
||||
public class MatrixControl<TValue> : DataGridView |
||||
public class MatrixControl<TValue> : FrameworkElement // TODO: Virtualization
|
||||
{ |
||||
Matrix<TValue> matrix; |
||||
object [,] cache; |
||||
Dictionary<string, ImageSource> imgs = new Dictionary<string, ImageSource>(); |
||||
Point currentPoint = new Point(-1, -1); |
||||
string font; |
||||
|
||||
public Matrix<TValue> Matrix |
||||
{ |
||||
get |
||||
{ |
||||
return matrix; |
||||
} |
||||
|
||||
set |
||||
{ |
||||
Rows.Clear(); |
||||
Columns.Clear(); |
||||
cache = new object[value.HeaderRows.Count, value.HeaderColumns.Count]; |
||||
matrix = value; |
||||
} |
||||
} |
||||
// will be loaded from Matrix
|
||||
int cellsVertically = 25; |
||||
int cellsHorizontally = 25; |
||||
|
||||
public Matrix<TValue> Matrix { get; set; } |
||||
|
||||
public int CellHeight { get; set; } |
||||
|
||||
public int CellWidth { get; set; } |
||||
|
||||
public MatrixControl() |
||||
{ |
||||
BackgroundColor = Color.White; |
||||
BorderStyle = BorderStyle.None; |
||||
|
||||
AllowUserToAddRows = false; |
||||
AllowUserToDeleteRows = false; |
||||
AllowUserToResizeRows = false; |
||||
AllowUserToResizeColumns = false; |
||||
AllowUserToOrderColumns = false; |
||||
AllowDrop = false; |
||||
|
||||
EnableHeadersVisualStyles = false; |
||||
|
||||
EditMode = DataGridViewEditMode.EditProgrammatically; |
||||
|
||||
AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells; |
||||
AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells; |
||||
|
||||
CellBorderStyle = DataGridViewCellBorderStyle.None; |
||||
|
||||
ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; |
||||
RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders; |
||||
ColumnHeadersDefaultCellStyle.BackColor = Color.White; |
||||
RowHeadersDefaultCellStyle.BackColor = Color.White; |
||||
RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.None; |
||||
ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.None; |
||||
|
||||
// disable selection
|
||||
DefaultCellStyle.SelectionForeColor = Color.Black; |
||||
DefaultCellStyle.SelectionBackColor = Color.White; |
||||
|
||||
ColumnHeadersDefaultCellStyle.SelectionForeColor = Color.Black; |
||||
ColumnHeadersDefaultCellStyle.SelectionBackColor = Color.White; |
||||
CellHeight = CellWidth = 36; |
||||
font = "Verdana"; |
||||
} |
||||
|
||||
protected override void OnMouseMove(System.Windows.Input.MouseEventArgs e) |
||||
{ |
||||
base.OnMouseMove(e); |
||||
|
||||
RowHeadersDefaultCellStyle.SelectionForeColor = Color.Black; |
||||
RowHeadersDefaultCellStyle.SelectionBackColor = Color.White; |
||||
var point = e.GetPosition(this); |
||||
if (point.X < cellsHorizontally * CellWidth && point.Y < cellsVertically * CellHeight) |
||||
currentPoint = point; |
||||
else |
||||
currentPoint = new Point(-1, -1); |
||||
|
||||
SelectionMode = DataGridViewSelectionMode.CellSelect; |
||||
InvalidateVisual(); |
||||
} |
||||
|
||||
protected override void OnRender(DrawingContext drawingContext) |
||||
{ |
||||
base.OnRender(drawingContext); |
||||
|
||||
// background
|
||||
var background = new Rect(0, 0, cellsHorizontally * CellWidth, cellsVertically * CellHeight); |
||||
var backgroundColor = new SolidColorBrush(Colors.Yellow); |
||||
drawingContext.DrawRectangle(backgroundColor, null, background); |
||||
|
||||
// hover cell
|
||||
if (currentPoint.X > 0 || currentPoint.Y > 0) { |
||||
var rect = new Rect(currentPoint.X - currentPoint.X % CellWidth, currentPoint.Y - currentPoint.Y % CellHeight, CellWidth, CellHeight); |
||||
var brush = new SolidColorBrush(Colors.Red); |
||||
drawingContext.DrawRectangle(brush, null, rect); |
||||
} |
||||
|
||||
ReadOnly = true; |
||||
VirtualMode = true; |
||||
EditMode = DataGridViewEditMode.EditProgrammatically; |
||||
} |
||||
// grid
|
||||
var pen = new Pen(Brushes.Black, 1); |
||||
|
||||
public void DrawMatrix() |
||||
{ |
||||
DrawHeaders(); |
||||
} |
||||
for (int i = 0; i <= cellsHorizontally; i++) { |
||||
drawingContext.DrawLine(pen, |
||||
new Point(i * CellWidth, 0), |
||||
new Point(i * CellWidth, |
||||
cellsHorizontally * CellWidth)); |
||||
} |
||||
|
||||
protected void DrawHeaders() |
||||
{ |
||||
foreach (var headerColumn in Matrix.HeaderColumns) { |
||||
var column = new DataGridViewTextBoxColumn(); |
||||
column.HeaderText = headerColumn.Value.ToString(); |
||||
this.Columns.Add(column); |
||||
for (int i = 0; i <= cellsVertically; i++) { |
||||
drawingContext.DrawLine(pen, |
||||
new Point(0, i * CellHeight), |
||||
new Point(cellsVertically * CellHeight, |
||||
i * CellHeight)); |
||||
} |
||||
|
||||
foreach (var headerRow in Matrix.HeaderRows) { |
||||
var row = new DataGridViewRow(); |
||||
row.HeaderCell.Value = headerRow.Value.ToString(); |
||||
this.Rows.Add(row); |
||||
|
||||
//if (!row.Displayed) // crashes sharpdevelop debugger
|
||||
// break;
|
||||
// text
|
||||
for (int i = 0; i < cellsHorizontally; i++) { |
||||
for (int j = 0; j < cellsVertically; j++) { |
||||
drawingContext.DrawImage(CreateText(i * j), new Rect(i * CellWidth, j * CellHeight, CellWidth, CellHeight)); |
||||
} |
||||
} |
||||
} |
||||
|
||||
protected override void OnCellValueNeeded(DataGridViewCellValueEventArgs e) |
||||
public ImageSource CreateText(int number) |
||||
{ |
||||
if (cache[e.RowIndex, e.ColumnIndex] != null) |
||||
e.Value = cache[e.RowIndex, e.ColumnIndex]; |
||||
if (e.RowIndex < Matrix.HeaderRows.Count && e.ColumnIndex < Matrix.HeaderColumns.Count) { |
||||
e.Value = Matrix.EvaluateCell(Matrix.HeaderRows[e.RowIndex], Matrix.HeaderColumns[e.ColumnIndex]); |
||||
} |
||||
return CreateText(number.ToString()); |
||||
} |
||||
|
||||
public ImageSource CreateText(string text) |
||||
{ |
||||
if (imgs.ContainsKey(text)) |
||||
return imgs[text]; |
||||
|
||||
var bmp = new System.Drawing.Bitmap(CellWidth, CellHeight); |
||||
var g = System.Drawing.Graphics.FromImage(bmp); |
||||
g.SmoothingMode = SmoothingMode.AntiAlias; |
||||
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; |
||||
|
||||
var fontOjb = new System.Drawing.Font(font, 12); |
||||
|
||||
var size = g.MeasureString(text, fontOjb); |
||||
|
||||
float spanWidth = CellWidth / 2 - size.Width / 2; |
||||
float spanHeight = CellHeight / 2 - size.Height / 2; |
||||
|
||||
g.DrawString(text, fontOjb, System.Drawing.Brushes.Black, new System.Drawing.PointF(spanWidth, spanHeight)); |
||||
g.Dispose(); |
||||
|
||||
var img = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), |
||||
IntPtr.Zero, |
||||
System.Windows.Int32Rect.Empty, |
||||
BitmapSizeOptions.FromWidthAndHeight(bmp.Width, bmp.Height)); |
||||
imgs.Add(text, img); |
||||
|
||||
return img; |
||||
} |
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue