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 @@ |
|||||||
using System; |
using System; |
||||||
using System.Collections.Generic; |
using System.Collections.Generic; |
||||||
|
using System.Drawing.Drawing2D; |
||||||
|
using System.Globalization; |
||||||
using System.Linq; |
using System.Linq; |
||||||
using System.Text; |
using System.Text; |
||||||
using System.Windows.Forms; |
using System.Windows; |
||||||
using System.Drawing; |
using System.Windows.Media; |
||||||
using ICSharpCode.SharpDevelop.Gui.ClassBrowser; |
using System.Windows.Media.Imaging; |
||||||
|
using System.Windows.Threading; |
||||||
|
|
||||||
namespace ICSharpCode.CodeQualityAnalysis.Controls |
namespace ICSharpCode.CodeQualityAnalysis.Controls |
||||||
{ |
{ |
||||||
public class MatrixControl<TValue> : DataGridView |
public class MatrixControl<TValue> : FrameworkElement // TODO: Virtualization
|
||||||
{ |
{ |
||||||
Matrix<TValue> matrix; |
Dictionary<string, ImageSource> imgs = new Dictionary<string, ImageSource>(); |
||||||
object [,] cache; |
Point currentPoint = new Point(-1, -1); |
||||||
|
string font; |
||||||
|
|
||||||
public Matrix<TValue> Matrix |
// will be loaded from Matrix
|
||||||
{ |
int cellsVertically = 25; |
||||||
get |
int cellsHorizontally = 25; |
||||||
{ |
|
||||||
return matrix; |
public Matrix<TValue> Matrix { get; set; } |
||||||
} |
|
||||||
|
public int CellHeight { get; set; } |
||||||
set |
|
||||||
{ |
public int CellWidth { get; set; } |
||||||
Rows.Clear(); |
|
||||||
Columns.Clear(); |
|
||||||
cache = new object[value.HeaderRows.Count, value.HeaderColumns.Count]; |
|
||||||
matrix = value; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
public MatrixControl() |
public MatrixControl() |
||||||
{ |
{ |
||||||
BackgroundColor = Color.White; |
CellHeight = CellWidth = 36; |
||||||
BorderStyle = BorderStyle.None; |
font = "Verdana"; |
||||||
|
} |
||||||
AllowUserToAddRows = false; |
|
||||||
AllowUserToDeleteRows = false; |
protected override void OnMouseMove(System.Windows.Input.MouseEventArgs e) |
||||||
AllowUserToResizeRows = false; |
{ |
||||||
AllowUserToResizeColumns = false; |
base.OnMouseMove(e); |
||||||
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; |
|
||||||
|
|
||||||
RowHeadersDefaultCellStyle.SelectionForeColor = Color.Black; |
var point = e.GetPosition(this); |
||||||
RowHeadersDefaultCellStyle.SelectionBackColor = Color.White; |
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; |
// grid
|
||||||
VirtualMode = true; |
var pen = new Pen(Brushes.Black, 1); |
||||||
EditMode = DataGridViewEditMode.EditProgrammatically; |
|
||||||
} |
|
||||||
|
|
||||||
public void DrawMatrix() |
for (int i = 0; i <= cellsHorizontally; i++) { |
||||||
{ |
drawingContext.DrawLine(pen, |
||||||
DrawHeaders(); |
new Point(i * CellWidth, 0), |
||||||
} |
new Point(i * CellWidth, |
||||||
|
cellsHorizontally * CellWidth)); |
||||||
|
} |
||||||
|
|
||||||
protected void DrawHeaders() |
for (int i = 0; i <= cellsVertically; i++) { |
||||||
{ |
drawingContext.DrawLine(pen, |
||||||
foreach (var headerColumn in Matrix.HeaderColumns) { |
new Point(0, i * CellHeight), |
||||||
var column = new DataGridViewTextBoxColumn(); |
new Point(cellsVertically * CellHeight, |
||||||
column.HeaderText = headerColumn.Value.ToString(); |
i * CellHeight)); |
||||||
this.Columns.Add(column); |
|
||||||
} |
} |
||||||
|
|
||||||
foreach (var headerRow in Matrix.HeaderRows) { |
// text
|
||||||
var row = new DataGridViewRow(); |
for (int i = 0; i < cellsHorizontally; i++) { |
||||||
row.HeaderCell.Value = headerRow.Value.ToString(); |
for (int j = 0; j < cellsVertically; j++) { |
||||||
this.Rows.Add(row); |
drawingContext.DrawImage(CreateText(i * j), new Rect(i * CellWidth, j * CellHeight, CellWidth, CellHeight)); |
||||||
|
} |
||||||
//if (!row.Displayed) // crashes sharpdevelop debugger
|
|
||||||
// break;
|
|
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
protected override void OnCellValueNeeded(DataGridViewCellValueEventArgs e) |
public ImageSource CreateText(int number) |
||||||
{ |
{ |
||||||
if (cache[e.RowIndex, e.ColumnIndex] != null) |
return CreateText(number.ToString()); |
||||||
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]); |
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