Ejemplo n.º 1
0
        void ConvertItemClicked(TextBox target, int radix)
        {
            this.calculator.Reset();

            var lines          = LineString.Split(target.Text).ToArray();
            var selectionStart = target.SelectionStart;
            var selectionEnd   = target.SelectionStart + target.SelectionLength;

            TextBoxHelper.GetStartEndLineIndex(lines, selectionStart, selectionEnd,
                                               out var lineIndexStart, out var lineIndexEnd);

            for (int i = lineIndexStart; i <= lineIndexEnd; i++)
            {
                if (this.calculator.Calculate(lines[i].Text))
                {
                    if (decimal.TryParse(this.calculator.Result, out var value))
                    {
                        lines[i].Text = ResultConvertHelper.ConvertToResultString(value, radix, ResultConvertHelper.Styles.Prefix);
                    }
                }
            }

            target.Text            = LineString.Join(lines);
            target.SelectionStart  = TextBoxHelper.GetCharacterIndexOfLineStartFromLineIndex(lines, lineIndexStart);
            target.SelectionLength = TextBoxHelper.GetCharacterIndexOfLineEndFromLineIndex(lines, lineIndexEnd) - target.SelectionStart;
        }
Ejemplo n.º 2
0
        private void InputTextBox_SelectionChanged(object sender, RoutedEventArgs e)
        {
            if (!this.IsInitialized)
            {
                return;
            }

            var caretIndex = this.InputTextBox.CaretIndex;
            var lineIndex  = this.InputTextBox.GetLineIndexFromCharacterIndex(caretIndex);
            var lines      = LineString.Split(this.InputTextBox.Text).ToArray();
            // GetLineText and GetCharacterIndexFromLineIndex does not work correctly in full-screen mode.
            var currentLine  = lines[lineIndex]; //this.InputTextBox.GetLineText(lineIndex);
            var selectedText = this.InputTextBox.SelectedText;

            if (lineIndex != this.lastCalculatedLineIndex || this.RxSelectionLength.Value != selectedText.Length)
            {
                // Recalculate current line
                this.calculator.Reset();
                for (int i = 0; i <= lineIndex; i++)
                {
                    this.calculator.Calculate(lines[i].Text);
                }
                if (0 < selectedText.Length)
                {
                    this.calculator.Calculate(selectedText);
                }
                if (this.calculator.Error == null)
                {
                    this.RxResult.Value = this.calculator.Result.ToString();
                }
                this.RxErrorText.Value       = this.calculator.Error;
                this.lastCalculatedLineIndex = lineIndex;
            }

            if (selectedText.Length == 0)
            {
                int lineStartCharIndex = TextBoxHelper.GetCharacterIndexOfLineStartFromLineIndex(lines, lineIndex);
                int charIndexOfLine    = caretIndex - lineStartCharIndex;
                var offset             = (charIndexOfLine < currentLine.Text.Length) ? 0 : -1;
                selectedText = CharInfoConvertHelper.GetUnicodeElement(currentLine.Text, charIndexOfLine + offset);
            }
            this.RxCurrentText.Value     = selectedText;
            this.RxSelectionLength.Value = this.InputTextBox.SelectionLength;
        }
Ejemplo n.º 3
0
        void FunctionItemClicked(TextBox target, string name)
        {
            var lines          = LineString.Split(target.Text).ToArray();
            var selectionStart = target.SelectionStart;
            var selectionEnd   = target.SelectionStart + target.SelectionLength;

            TextBoxHelper.GetStartEndLineIndex(lines, selectionStart, selectionEnd,
                                               out var lineIndexStart, out var lineIndexEnd);

            if (lineIndexStart < lineIndexEnd)
            {
                // Multi lines are selected
                for (int i = lineIndexStart; i <= lineIndexEnd; i++)
                {
                    lines[i].Text = $"{name}({lines[i].Text})";
                }

                target.Text            = LineString.Join(lines);
                target.SelectionStart  = TextBoxHelper.GetCharacterIndexOfLineStartFromLineIndex(lines, lineIndexStart);
                target.SelectionLength = TextBoxHelper.GetCharacterIndexOfLineEndFromLineIndex(lines, lineIndexEnd) - target.SelectionStart;
            }
            else
            {
                var prevSelectedLength = target.SelectedText.Length;
                if (0 < name.Length &&
                    selectionEnd < target.Text.Length &&
                    target.Text[selectionEnd] == '(')
                {
                    // xxxx(... -> FUNC(...
                    // ~~~~             |
                    target.SelectedText    = name;
                    target.SelectionLength = 0;
                }
                else
                {
                    // xxxx -> xFUNC(xx)x
                    //  ~~           ~~
                    target.SelectedText    = $"{name}({target.SelectedText})";
                    target.SelectionLength = prevSelectedLength;
                }
                target.SelectionStart += name.Length + 1;
            }
        }
Ejemplo n.º 4
0
 public static int GetCharacterIndexOfLineEndFromLineIndex(LineString[] lines, int lineIndex)
 {
     return(TextBoxHelper.GetCharacterIndexOfLineStartFromLineIndex(lines, lineIndex) + lines.ElementAt(lineIndex).FullLength);
 }