public void Do () { Region.SetNeedToEvaluate (true); var parent = Region.Selection.SelectedToken.Parent; Token _base; Token power; if (Region.Selection.SelectedToken is TextToken) { var textToken = Region.Selection.SelectedToken as TextToken; _base = new TextToken () { Text = GetLeftString (textToken.Text) }; power = new TextToken () { Text = GetRightString (textToken.Text) }; } else { if (Region.Selection.SelectedToken is FractionToken) { var fraction = Region.Selection.SelectedToken; var parentheses = new ParenthesesToken (); parentheses.ShowCloseParentheses = true; parent.Replace (fraction, parentheses); var container = new HBoxToken (); parentheses.Child = container; container.Add (fraction); _base = parentheses; Region.Selection.SelectedToken = parentheses; } else _base = Region.Selection.SelectedToken; power = new TextToken (); } var exponentiation = new ExponentiationToken (); parent.Replace (Region.Selection.SelectedToken, exponentiation); exponentiation.Base = _base; exponentiation.Power = power; Region.Selection.SelectedToken = power; }
public void Do () { Region.SetNeedToEvaluate (true); if (Region.Selection.SelectedToken is TextToken == false) { if (Region.Selection.Type == SelectionType.Right) { var operation = new MultiplicationToken (); new InsertHBinaryOperation (Region, operation).Do (); } } else { var textToken = Region.Selection.SelectedToken as TextToken; if (!string.IsNullOrEmpty (textToken.Text) && Region.Selection.Position != 0) { if (Regex.IsMatch (textToken.Text, @"^\D") && Region.Selection.Position == textToken.Text.Length) { var p = GetHBoxToken (textToken); var i = p.Tokens.IndexOf (textToken); p.Tokens.Insert (i + 1, new TextToken ()); new RightAction (Region).Do (); } else { var operation = new MultiplicationToken (); new InsertHBinaryOperation (Region, operation).Do (); } } } var parent = GetHBoxToken (Region.Selection.SelectedToken); var index = parent.Tokens.IndexOf (Region.Selection.SelectedToken); var newContainer = new HBoxToken (); foreach (var token in parent.Tokens.Skip (index).ToList()) { parent.Tokens.Remove (token); newContainer.Add (token); } var parenthess = new ParenthesesToken (); parenthess.Child = newContainer; parent.Tokens.Add (parenthess); }
private HBoxToken GetHBoxToken (Token token) { if (token.Parent is HBoxToken == false) { var parent = token.Parent; var container = new HBoxToken (); parent.Replace (token, container); container.Add (token); } return token.Parent as HBoxToken; }
public MathRegionViewModel (DocumentViewModel document) { Document = document; x = BuildProperty<double> ("X"); y = BuildProperty<double> ("Y"); root = BuildProperty<HBoxToken> ("Root"); selection = BuildProperty<Selection> ("Selection"); insertCharacterCommand = BuildProperty<ICommand> ("InsertCharacterCommand"); insertCharacterCommand.Value = new DelegateCommand<Key> (o => new InsertCharacterAction (o, this).Do ()); plusCommand = BuildProperty<ICommand> ("PlusCommand"); plusCommand.Value = new DelegateCommand<object> (o => new PlusAction (this).Do ()); minusCommand = BuildProperty<ICommand> ("MinusCommand"); minusCommand.Value = new DelegateCommand<object> (o => new MinusAction (this).Do ()); multiplicationCommand = BuildProperty<ICommand> ("MultiplicationCommand"); multiplicationCommand.Value = new DelegateCommand<object> (o => new MultiplicationAction (this).Do ()); divideCommand = BuildProperty<ICommand> ("DivideCommand"); divideCommand.Value = new DelegateCommand<object> (o => new DivideAction (this).Do ()); assignmentCommand = BuildProperty<ICommand> ("AssignmentCommand"); assignmentCommand.Value = new DelegateCommand<object> (o => new AssignmentAction (this).Do ()); resultCommand = BuildProperty<ICommand> ("ResultCommand"); resultCommand.Value = new DelegateCommand<object> (o => new ResultAction (this).Do ()); leftCommand = BuildProperty<ICommand> ("LeftCommand"); leftCommand.Value = new DelegateCommand<object> (o => new LeftAction (this).Do ()); rightCommand = BuildProperty<ICommand> ("RightCommand"); rightCommand.Value = new DelegateCommand<object> (o => new RightAction (this).Do ()); evaluateCommand = BuildProperty<ICommand> ("EvaluateCommand"); evaluateCommand.Value = new DelegateCommand<object> (o => new EvaluateAction (this).Do ()); openBracketCommand = BuildProperty<ICommand> ("OpenBracketCommand"); openBracketCommand.Value = new DelegateCommand<object> (o => new OpenBracketAction (this).Do ()); closeBracketCommand = BuildProperty<ICommand> ("CloseBracketCommand"); closeBracketCommand.Value = new DelegateCommand<object> (o => new CloseBracketAction (this).Do ()); commaCommand = BuildProperty<ICommand> ("CommaCommand"); commaCommand.Value = new DelegateCommand<object> (o => new CommaAction (this).Do ()); exponentiationCommand = BuildProperty<ICommand> ("ExponentiationCommand"); exponentiationCommand.Value = new DelegateCommand<object> (o => new ExponentiationAction (this).Do ()); squareRootCommand = BuildProperty<ICommand> ("SquareRootCommand"); squareRootCommand.Value = new DelegateCommand<object> (o => new SquareRootAction (this).Do ()); absoluteCommand = BuildProperty<ICommand> ("AbsoluteCommand"); absoluteCommand.Value = new DelegateCommand<object> (o => new AbsoluteAction (this).Do ()); needToEvaluate = BuildProperty<bool> ("NeedToEvaluate"); needToEvaluate.DependencyPropertyValueChanged += HandleNeedToEvaluateChanged; Root = new HBoxToken (); Selection = new Selection (); var token = new TextToken (); Root.Add (token); Selection.SelectedToken = token; hasError = BuildProperty<bool> ("HasError"); }