using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; namespace Accentra.Controls { /// /// A FrontPage style table dimensions picker. /// public class TablePicker : System.Windows.Forms.Form { /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public TablePicker() { // Activates double buffering SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.DoubleBuffer, true); // // Required for Windows Form Designer support // InitializeComponent(); } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { // // TablePicker // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.BackColor = System.Drawing.Color.WhiteSmoke; 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.Click += new System.EventHandler(this.TablePicker_Click); this.Paint += new System.Windows.Forms.PaintEventHandler(this.TablePicker_Paint); this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.TablePicker_MouseMove); this.MouseEnter += new System.EventHandler(this.TablePicker_MouseEnter); this.MouseLeave += new System.EventHandler(this.TablePicker_MouseLeave); this.Deactivate += new System.EventHandler(this.TablePicker_Deactivate); } #endregion private Pen BeigePen = new Pen(Color.Beige, 1); private Brush BeigeBrush = System.Drawing.Brushes.Beige; private Brush GrayBrush = System.Drawing.Brushes.Gray; private Brush BlackBrush = System.Drawing.Brushes.Black; private Brush WhiteBrush = System.Drawing.Brushes.White; private Pen BorderPen = new Pen(SystemColors.ControlDark); private Pen BluePen = new Pen(Color.SlateGray, 1); private string DispText = "Cancel"; // Display text private int DispHeight = 20; // Display ("Table 1x1", "Cancel") private Font DispFont = new Font("Tahoma", 8.25F); private int SquareX = 20; // Width of squares private 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) private bool bHiding = false; private bool bCancel = true; // Determines whether to Cancel /// /// Similar to /// == , /// but is used as a state value before the form /// is hidden and cancellation is finalized. /// public bool Cancel { get { return bCancel; } } /// /// Returns the number of columns, or the horizontal / X count, /// of the selection. /// public int SelectedColumns { get { return SelQX; } } /// /// Returns the number of rows, or the vertical / Y count, /// of the selection. /// public int SelectedRows { get { return 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; // 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; if (this.Cancel) { DispText = "Cancel"; } else { DispText = SelQX.ToString() + " by " + SelQY.ToString() + " Table"; } g.DrawString(DispText, DispFont, BlackBrush, 3, dispY + 2); // Draw each of the squares and fill with the default color. for (int x=0; x /// Detect termination. Hides form. /// private void TablePicker_Deactivate(object sender, System.EventArgs e) { // bCancel = true // and DialogResult = DialogResult.Cancel // were previously already set in MouseLeave. this.Hide(); } /// /// Detects mouse movement. Tracks table dimensions selection. /// 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(); } /// /// Detects mouse sudden exit from the form to indicate /// escaped (canceling) state. /// private void TablePicker_MouseLeave(object sender, System.EventArgs e) { if (!bHiding) bCancel = true; this.DialogResult = DialogResult.Cancel; this.Invalidate(); } /// /// Cancels the prior cancellation caused by MouseLeave. /// private void TablePicker_MouseEnter(object sender, System.EventArgs e) { bHiding = false; bCancel = false; this.DialogResult = DialogResult.OK; this.Invalidate(); } /// /// Detects that the user made a selection by clicking. /// private void TablePicker_Click(object sender, System.EventArgs e) { bHiding = true; // Not the same as Visible == false // because bHiding suggests that the control // is still "active" (not canceled). this.Hide(); } } }