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
        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;
            }
        }