2007-12-26 14:29:36 +00:00
|
|
|
|
|
|
|
#region ================== Copyright (c) 2007 Pascal vd Heiden
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2007 Pascal vd Heiden, www.codeimp.com
|
|
|
|
* This program is released under GNU General Public License
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Namespaces
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.ComponentModel;
|
|
|
|
using System.Drawing;
|
|
|
|
using System.Text;
|
|
|
|
using System.Globalization;
|
|
|
|
using System.Windows.Forms;
|
2008-05-29 11:34:01 +00:00
|
|
|
using CodeImp.DoomBuilder.Actions;
|
2007-12-26 14:29:36 +00:00
|
|
|
using CodeImp.DoomBuilder.Geometry;
|
|
|
|
using CodeImp.DoomBuilder.Rendering;
|
|
|
|
using CodeImp.DoomBuilder.Editing;
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2008-05-29 11:54:45 +00:00
|
|
|
namespace CodeImp.DoomBuilder.Controls
|
2007-12-26 14:29:36 +00:00
|
|
|
{
|
2008-01-04 00:16:58 +00:00
|
|
|
public class NumericTextbox : AutoSelectTextbox
|
2007-12-26 14:29:36 +00:00
|
|
|
{
|
|
|
|
#region ================== Constants
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Variables
|
|
|
|
|
2007-12-27 01:24:11 +00:00
|
|
|
private bool allownegative = false; // Allow negative numbers
|
|
|
|
private bool allowrelative = false; // Allow ++ and -- prefix for relative changes
|
|
|
|
|
2007-12-26 14:29:36 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Properties
|
|
|
|
|
2007-12-27 01:24:11 +00:00
|
|
|
public bool AllowNegative { get { return allownegative; } set { allownegative = value; } }
|
|
|
|
public bool AllowRelative { get { return allowrelative; } set { allowrelative = value; } }
|
2008-02-17 10:41:38 +00:00
|
|
|
|
2007-12-26 14:29:36 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Constructor / Disposer
|
|
|
|
|
2008-02-17 10:41:38 +00:00
|
|
|
// Constructor
|
|
|
|
public NumericTextbox()
|
|
|
|
{
|
|
|
|
this.ImeMode = ImeMode.Off;
|
|
|
|
}
|
|
|
|
|
2007-12-26 14:29:36 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Methods
|
|
|
|
|
|
|
|
// When a key is pressed
|
|
|
|
protected override void OnKeyPress(KeyPressEventArgs e)
|
|
|
|
{
|
2007-12-27 01:24:11 +00:00
|
|
|
string allowedchars = "0123456789\b";
|
|
|
|
string nonselectedtext;
|
|
|
|
string textpart;
|
|
|
|
int selectionpos;
|
|
|
|
int numprefixes;
|
|
|
|
char otherprefix;
|
|
|
|
|
|
|
|
// Determine allowed chars
|
|
|
|
if(allownegative) allowedchars += "-";
|
|
|
|
if(allowrelative) allowedchars += "+-";
|
|
|
|
|
|
|
|
// Check if key is not allowed
|
|
|
|
if(allowedchars.IndexOf(e.KeyChar) == -1)
|
2007-12-26 14:29:36 +00:00
|
|
|
{
|
|
|
|
// Cancel this
|
|
|
|
e.Handled = true;
|
|
|
|
}
|
2007-12-27 01:24:11 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Check if + or - is pressed
|
|
|
|
if((e.KeyChar == '+') || (e.KeyChar == '-'))
|
|
|
|
{
|
|
|
|
// Determine non-selected text
|
|
|
|
if(this.SelectionLength > 0)
|
|
|
|
{
|
|
|
|
nonselectedtext = this.Text.Substring(0, this.SelectionStart) +
|
|
|
|
this.Text.Substring(this.SelectionStart + this.SelectionLength);
|
|
|
|
}
|
|
|
|
else if(this.SelectionLength < 0)
|
|
|
|
{
|
|
|
|
nonselectedtext = this.Text.Substring(0, this.SelectionStart + this.SelectionLength) +
|
|
|
|
this.Text.Substring(this.SelectionStart);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nonselectedtext = this.Text;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not at the start?
|
|
|
|
selectionpos = this.SelectionStart - 1;
|
|
|
|
if(this.SelectionLength < 0) selectionpos = (this.SelectionStart + this.SelectionLength) - 1;
|
|
|
|
if(selectionpos > -1)
|
|
|
|
{
|
|
|
|
// Find any other characters before the insert position
|
|
|
|
textpart = this.Text.Substring(0, selectionpos + 1);
|
|
|
|
textpart = textpart.Replace("+", "");
|
|
|
|
textpart = textpart.Replace("-", "");
|
|
|
|
if(textpart.Length > 0)
|
|
|
|
{
|
|
|
|
// Cancel this
|
|
|
|
e.Handled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine other prefix
|
|
|
|
if(e.KeyChar == '+') otherprefix = '-'; else otherprefix = '+';
|
|
|
|
|
|
|
|
// Limit the number of + and - allowed
|
|
|
|
numprefixes = nonselectedtext.Split(e.KeyChar, otherprefix).Length;
|
|
|
|
if(numprefixes > 2)
|
|
|
|
{
|
|
|
|
// Can't have more than 2 prefixes
|
|
|
|
e.Handled = true;
|
|
|
|
}
|
|
|
|
else if(numprefixes > 1)
|
|
|
|
{
|
|
|
|
// Must have 2 the same prefixes
|
|
|
|
if(this.Text.IndexOf(e.KeyChar) == -1) e.Handled = true;
|
|
|
|
|
|
|
|
// Double prefix must be allowed
|
|
|
|
if(!allowrelative) e.Handled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-26 14:29:36 +00:00
|
|
|
// Call base
|
|
|
|
base.OnKeyPress(e);
|
|
|
|
}
|
|
|
|
|
2007-12-27 01:24:11 +00:00
|
|
|
// Validate contents
|
|
|
|
protected override void OnValidating(CancelEventArgs e)
|
|
|
|
{
|
|
|
|
string textpart = this.Text;
|
|
|
|
|
|
|
|
// Strip prefixes
|
|
|
|
textpart = textpart.Replace("+", "");
|
|
|
|
textpart = textpart.Replace("-", "");
|
|
|
|
|
|
|
|
// No numbers left?
|
|
|
|
if(textpart.Length == 0)
|
|
|
|
{
|
|
|
|
// Make the textbox empty
|
|
|
|
this.Text = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call base
|
|
|
|
base.OnValidating(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This determines the result value
|
|
|
|
public int GetResult(int original)
|
|
|
|
{
|
|
|
|
string textpart = this.Text;
|
2008-05-17 17:43:57 +00:00
|
|
|
int result = 0;
|
2007-12-27 01:24:11 +00:00
|
|
|
|
|
|
|
// Strip prefixes
|
|
|
|
textpart = textpart.Replace("+", "");
|
|
|
|
textpart = textpart.Replace("-", "");
|
|
|
|
|
|
|
|
// Any numbers left?
|
|
|
|
if(textpart.Length > 0)
|
|
|
|
{
|
|
|
|
// Prefixed with ++?
|
|
|
|
if(this.Text.StartsWith("++"))
|
|
|
|
{
|
|
|
|
// Add number to original
|
2008-05-17 17:43:57 +00:00
|
|
|
if(!int.TryParse(textpart, out result)) result = 0;
|
|
|
|
return original + result;
|
2007-12-27 01:24:11 +00:00
|
|
|
}
|
|
|
|
// Prefixed with --?
|
|
|
|
else if(this.Text.StartsWith("--"))
|
|
|
|
{
|
|
|
|
// Subtract number from original
|
2008-05-17 17:43:57 +00:00
|
|
|
if(!int.TryParse(textpart, out result)) result = 0;
|
|
|
|
return original - result;
|
2007-12-27 01:24:11 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Return the new value
|
2008-05-17 17:43:57 +00:00
|
|
|
if(int.TryParse(this.Text, out result)) return result; else return 0;
|
2007-12-27 01:24:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Nothing given, keep original value
|
|
|
|
return original;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-26 14:29:36 +00:00
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|