math-suite/Source/MatrixCalculator/Forms/Editor.cs

182 lines
5.8 KiB
C#
Raw Permalink Normal View History

2018-02-05 23:24:46 +00:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MathNet.Numerics.LinearAlgebra;
namespace MatrixCalculator
{
public partial class Editor : Form
{
bool allowExit = true;
#region Setters and getters
public string MatrixName {
get { return inputName.Text; }
set { inputName.Text = value; }
}
public string MatrixDescription
{
get { return inputDescription.Text; }
set { inputDescription.Text = value; }
}
public Matrix<double> Matrix
{
get { return GridViewHelper.GetMatrix(dataEditor); }
set {
GridViewHelper.PutMatrix(value, dataEditor);
inputColumns.Value = value.ColumnCount;
inputRows.Value = value.RowCount;
}
}
#endregion
#region Constructor
public Editor()
{
InitializeComponent();
inputName.Text = "New matrix ";
}
#endregion
#region Resize buttons
private void buttonResizeReset_Click(object sender, EventArgs e)
{
inputRows.Value = dataEditor.Rows.Count;
inputColumns.Value = dataEditor.Columns.Count;
}
private void buttonResizeApply_Click(object sender, EventArgs e)
{
int rows = Convert.ToInt32(inputRows.Value);
int cols = Convert.ToInt32(inputColumns.Value);
GridViewHelper.Resize(dataEditor, cols, rows);
}
#endregion
#region Form buttons
private void buttonAccept_Click(object sender, EventArgs e)
{
this.DialogResult = System.Windows.Forms.DialogResult.OK;
// Make sure name is not empty
if (MatrixName == "")
{
allowExit = false;
MessageBox.Show("You must enter a name!", "Error!");
}
// Make sure size is not empty! Empty matrices behave very weird
else if (dataEditor.Columns.Count == 0 || dataEditor.Rows.Count == 0)
{
allowExit = false;
MessageBox.Show("The matrix must have at least one row and one column!", "Error!");
}
}
private void buttonCancel_Click(object sender, EventArgs e)
{
this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.Close();
}
#endregion
#region Right click menu
private void contextMenuCut_Click(object sender, EventArgs e)
{
GridViewHelper.CopySelected(dataEditor);
foreach (DataGridViewCell i in dataEditor.SelectedCells) i.Value = "0";
}
private void contextMenuCopy_Click(object sender, EventArgs e)
{
GridViewHelper.CopySelected(dataEditor);
}
private void contextMenuPaste_Click(object sender, EventArgs e)
{
GridViewHelper.Paste(dataEditor);
}
private void contextMenuClear_Click(object sender, EventArgs e)
{
foreach (DataGridViewCell i in dataEditor.SelectedCells) i.Value = "0";
}
private void contextMenuClearAll_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow i in dataEditor.Rows)
foreach (DataGridViewCell j in i.Cells)
j.Value = "0";
}
private void contextMenuInsertRow_Click(object sender, EventArgs e)
{
if (dataEditor.CurrentCell != null) dataEditor.Rows.Insert(dataEditor.CurrentCell.RowIndex, new DataGridViewRow());
else dataEditor.Rows.Add();
}
private void contextMenuInsertCol_Click(object sender, EventArgs e)
{
var col = new DataGridViewTextBoxColumn()
{
SortMode = DataGridViewColumnSortMode.NotSortable,
AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCellsExceptHeader
};
if (dataEditor.CurrentCell != null) dataEditor.Columns.Insert(dataEditor.CurrentCell.ColumnIndex, col);
else dataEditor.Columns.Add(col);
}
private void contextMenuDelRow_Click(object sender, EventArgs e)
{
foreach (DataGridViewCell i in dataEditor.SelectedCells)
dataEditor.Rows.RemoveAt(i.RowIndex);
}
private void contextMenuDelColumn_Click(object sender, EventArgs e)
{
foreach (DataGridViewCell i in dataEditor.SelectedCells)
dataEditor.Columns.RemoveAt(i.ColumnIndex);
}
#endregion
#region Drawing
protected override void OnPaintBackground(PaintEventArgs e)
{
DynamicLink.Controls.BackgroundGradient.Paint(e.Graphics, new Rectangle(-1, -1, this.Width, this.Height));
}
#endregion
private void dataEditor_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.ColumnIndex < 0 || e.ColumnIndex >= dataEditor.Columns.Count) return;
if (e.RowIndex < 0 || e.RowIndex >= dataEditor.Rows.Count) return;
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
dataEditor.CurrentCell = dataEditor.Rows[e.RowIndex].Cells[e.ColumnIndex];
if (dataEditor.ContextMenu != null) dataEditor.ContextMenu.Show(dataEditor, e.Location);
}
}
private void Editor_FormClosing(object sender, FormClosingEventArgs e)
{
if (!allowExit)
{
allowExit = true;
e.Cancel = true;
}
}
}
}