Browse Source

MatrixControl is drawn by WPF.

Text is drawn by System.Drawing.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@6395 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
pull/1/head
Tomáš Linhart 15 years ago
parent
commit
d21fc2d0c9
  1. 11
      src/AddIns/Analysis/CodeQuality/CodeQualityAnalysis.csproj
  2. 180
      src/AddIns/Analysis/CodeQuality/Src/Controls/MatrixControl.cs
  3. 11
      src/AddIns/Analysis/CodeQuality/Src/Controls/TreeMatrixControl.xaml
  4. 9
      src/AddIns/Analysis/CodeQuality/Src/Controls/TreeMatrixControl.xaml.cs

11
src/AddIns/Analysis/CodeQuality/CodeQualityAnalysis.csproj

@ -114,12 +114,16 @@ @@ -114,12 +114,16 @@
<Compile Include="Src\Controls\DependencyGraph.cs" />
<Compile Include="Src\Controls\DependencyGraphLayout.cs" />
<Compile Include="Src\Controls\DependencyMatrixControl.cs">
<SubType>Component</SubType> </Compile> <Compile Include="Src\Controls\DependencyMatrix.cs" />
<SubType>Component</SubType>
</Compile>
<Compile Include="Src\Controls\DependencyMatrix.cs" />
<Compile Include="Src\Controls\DependencyVertex.cs" />
<Compile Include="Src\Controls\DependencyIconVertexConverter.cs" />
<Compile Include="Src\Controls\Matrix.cs" />
<Compile Include="Src\Controls\MatrixControl.cs">
<SubType>Component</SubType> </Compile> <Compile Include="Src\Controls\TreeMatrixControl.xaml.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Src\Controls\TreeMatrixControl.xaml.cs">
<DependentUpon>TreeMatrixControl.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
@ -127,7 +131,8 @@ @@ -127,7 +131,8 @@
<Compile Include="Src\Event.cs" />
<Compile Include="Src\Field.cs" />
<Compile Include="Src\INode.cs" />
<Compile Include="Src\Instruction.cs" /> <Compile Include="Src\NodeIconService.cs" />
<Compile Include="Src\Instruction.cs" />
<Compile Include="Src\NodeIconService.cs" />
<Compile Include="Src\IDependency.cs" />
<Compile Include="Src\MetricsReader.cs" />
<Compile Include="Src\Method.cs" />

180
src/AddIns/Analysis/CodeQuality/Src/Controls/MatrixControl.cs

@ -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;
}
}
}

11
src/AddIns/Analysis/CodeQuality/Src/Controls/TreeMatrixControl.xaml

@ -21,7 +21,7 @@ @@ -21,7 +21,7 @@
<TreeView Name="leftTree" Grid.Row="1" Grid.Column="0">
<TreeView.Resources>
</TreeView.Resources>
</TreeView>
@ -31,8 +31,11 @@ @@ -31,8 +31,11 @@
</TreeView.LayoutTransform>
</TreeView>
<WindowsFormsHost Grid.Column="1" Grid.Row="1">
<Controls:DependencyMatrixControl x:Name="matrixControl"></Controls:DependencyMatrixControl>
</WindowsFormsHost>
<Controls:DependencyMatrixControl Grid.Column="1" Grid.Row="1"
x:Name="matrixControl"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
</Controls:DependencyMatrixControl>
</Grid>
</UserControl>

9
src/AddIns/Analysis/CodeQuality/Src/Controls/TreeMatrixControl.xaml.cs

@ -33,18 +33,11 @@ namespace ICSharpCode.CodeQualityAnalysis.Controls @@ -33,18 +33,11 @@ namespace ICSharpCode.CodeQualityAnalysis.Controls
public TreeMatrixControl()
{
InitializeComponent();
matrixControl.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
matrixControl.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
matrixControl.BorderStyle = BorderStyle.FixedSingle;
matrixControl.RowHeadersVisible = false;
matrixControl.ColumnHeadersVisible = false;
}
public void DrawMatrix()
{
matrixControl.DrawMatrix();
// matrixControl.DrawMatrix();
}
public void DrawTree(Module module)

Loading…
Cancel
Save