Files
SourceCode/PROMS/TablePicker/TablePicker.cs
T

191 lines
6.6 KiB
C#

using System.Drawing;
using System.Windows.Forms;
namespace Accentra.Controls
{
/// <summary>
/// A FrontPage style table dimensions picker.
/// </summary>
public class TablePicker : System.Windows.Forms.Form
{
public TablePicker()
{
// Activates double buffering
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);
//
// Required for Windows Form Designer support
//
InitializeComponent();
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
//
// TablePicker
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
this.BackColor = System.Drawing.Color.LightYellow;
this.ClientSize = new System.Drawing.Size(304, 256);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "TablePicker";
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.Text = "TablePicker";
this.Deactivate += new System.EventHandler(this.TablePicker_Deactivate);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.TablePicker_Paint);
this.MouseEnter += new System.EventHandler(this.TablePicker_MouseEnter);
this.Click += new System.EventHandler(this.TablePicker_Click);
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.TablePicker_KeyPress);
this.MouseLeave += new System.EventHandler(this.TablePicker_MouseLeave);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.TablePicker_MouseMove);
this.ResumeLayout(false);
}
#endregion
private readonly Brush BlackBrush = System.Drawing.Brushes.Black;
private readonly Brush WhiteBrush = System.Drawing.Brushes.White;
private readonly Brush Jbrush = System.Drawing.Brushes.LightBlue;
private readonly Pen BorderPen = new Pen(SystemColors.ControlDark);
private readonly Pen BluePen = new Pen(Color.SlateGray, 1);
private string DispText = "Esc to Cancel"; // Display text
private readonly int DispHeight = 40; // Display ("Table 1x1", "Cancel")
private readonly Font DispFont = new Font("Tahoma", 8.25F);
private readonly int SquareX = 20; // Width of squares
private readonly int SquareY = 20; // Height of squares
private int SquareQX = 3; // Number of visible squares (X)
private int SquareQY = 3; // Number of visible squares (Y)
private int SelQX = 1; // Number of selected squares (x)
private int SelQY = 1; // Number of selected squares (y)
public int MaxRows { get; set; } = -1;
public int MaxCols { get; set; } = -1;
/// <summary>
/// Returns the number of columns, or the horizontal / X count,
/// of the selection.
/// </summary>
public int SelectedColumns => SelQX;
/// <summary>
/// Returns the number of rows, or the vertical / Y count,
/// of the selection.
/// </summary>
public int SelectedRows => SelQY;
private void TablePicker_Paint(object sender, System.Windows.Forms.PaintEventArgs e) {
Graphics g = e.Graphics;
// First, increment the number of visible squares if the
// number of selected squares is equal to or greater than the
// number of visible squares.
if (SelQX > SquareQX - 1) SquareQX = SelQX + 1;
if (SelQY > SquareQY - 1) SquareQY = SelQY + 1;
if (SquareQX < 7) SquareQX = 7;
if (SquareQY < 5) SquareQY = 5;
if (MaxRows > 0 && SquareQY > MaxRows) SquareQY = MaxRows;
if (MaxCols > 0 && SquareQX > MaxCols) SquareQX = MaxCols;
// Second, expand the dimensions of this form according to the
// number of visible squares.
this.Width = (SquareX * (SquareQX)) + 5;
this.Height = (SquareY * (SquareQY)) + 6 + DispHeight;
// Draw an outer rectangle for the border.
g.DrawRectangle(BorderPen, 0, 0, this.Width - 1, this.Height - 1);
// Draw the text to describe the selection. Note that since
// the text is left-justified, only the Y (vertical) position
// is calculated.
int dispY = ((SquareY - 1) * SquareQY) + SquareQY + 4;
DispText = $"{SelQY} Row{((SelQY > 1) ? "s" : "")} {SelQX} Column{((SelQX > 1) ? "s" : "")}\nEsc to Cancel";
g.DrawString(DispText, DispFont, BlackBrush, 3, dispY + 2);
// Draw each of the squares and fill with the default color.
for (int x=0; x<SquareQX; x++) {
for (int y=0; y<SquareQY; y++) {
g.FillRectangle(WhiteBrush, (x*SquareX) + 3, (y*SquareY) + 3, SquareX - 2, SquareY - 2);
g.DrawRectangle(BorderPen, (x*SquareX) + 3, (y*SquareY) + 3, SquareX - 2, SquareY - 2);
}
}
// Go back and paint the squares with selection colors.
for (int x=0; x<SelQX; x++) {
for (int y=0; y<SelQY; y++) {
g.FillRectangle(Jbrush, (x * SquareX) + 3, (y * SquareY) + 3, SquareX - 2, SquareY - 2);
g.DrawRectangle(BluePen, (x * SquareX) + 3, (y * SquareY) + 3, SquareX - 2, SquareY - 2);
}
}
}
/// <summary>
/// Detect termination. Hides form.
/// </summary>
private void TablePicker_Deactivate(object sender, System.EventArgs e) {
}
/// <summary>
/// Detects mouse movement. Tracks table dimensions selection.
/// </summary>
private void TablePicker_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) {
int sqx = (e.X / SquareX) + 1;
int sqy = (e.Y / SquareY) + 1;
bool changed = false;
if (sqx != SelQX) {
changed = true;
SelQX = sqx;
}
if (sqy != SelQY) {
changed = true;
SelQY = sqy;
}
// Ask Windows to call the Paint event again.
if (changed) Invalidate();
}
/// <summary>
/// Detects mouse sudden exit from the form to indicate
/// escaped (canceling) state.
/// </summary>
private void TablePicker_MouseLeave(object sender, System.EventArgs e) {
if (this.DialogResult == DialogResult.None)
this.DialogResult = DialogResult.Cancel;
}
/// <summary>
/// Cancels the prior cancellation caused by MouseLeave.
/// </summary>
private void TablePicker_MouseEnter(object sender, System.EventArgs e) => this.Invalidate();
/// <summary>
/// Detects that the user made a selection by clicking.
/// </summary>
private void TablePicker_Click(object sender, System.EventArgs e) => this.DialogResult = DialogResult.OK;
private void TablePicker_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Escape)
{
this.DialogResult = DialogResult.Cancel;
}
}
}
}