Пример #1
0
        private void OnCodeClicked(object sender, RoutedEventArgs e)
        {
            var textwrap = DescriptionTextBox.TextWrapping;

            if (textwrap == TextWrapping.Wrap)
            {
                DescriptionTextBox.TextWrapping = TextWrapping.NoWrap;
            }

            var startLineIndex = DescriptionTextBox.GetLineIndexFromCharacterIndex(DescriptionTextBox.SelectionStart);
            var endLineIndex   = DescriptionTextBox.GetLineIndexFromCharacterIndex(DescriptionTextBox.SelectionStart +
                                                                                   DescriptionTextBox.SelectionLength);

            if (startLineIndex != endLineIndex)
            {
                var collection = ColorCode.Languages.All.Select(l => l.Name).OrderBy(n => n).ToList();
                collection.Insert(0, "None");
                var vm = InputViewFactory.ShowComboBoxInput("Please choose a code language:", "Code Language", collection);

                var language = String.Empty;
                if (vm.UserCancelled)
                {
                    DescriptionTextBox.TextWrapping = textwrap;
                    return;
                }

                if (vm.SelectedValue != "None")
                {
                    language = vm.SelectedValue;
                }

                for (var index = startLineIndex; index <= endLineIndex; index++)
                {
                    var lineStartIndex = DescriptionTextBox.GetCharacterIndexFromLineIndex(index);
                    DescriptionTextBox.Text = DescriptionTextBox.Text.Insert(lineStartIndex, "    ");
                }

                if (language != String.Empty)
                {
                    DescriptionTextBox.Text =
                        DescriptionTextBox.Text.Insert(
                            DescriptionTextBox.GetCharacterIndexFromLineIndex(startLineIndex),
                            "    " + OmniTextRenderer.LangDefinitionText + language + Environment.NewLine);
                    endLineIndex++;
                }
                DescriptionTextBox.SelectionStart = DescriptionTextBox.GetCharacterIndexFromLineIndex(startLineIndex);
                var endOfLastLineIndex = DescriptionTextBox.GetCharacterIndexFromLineIndex(endLineIndex) +
                                         DescriptionTextBox.GetLineText(endLineIndex).Length;
                DescriptionTextBox.SelectionLength = endOfLastLineIndex - DescriptionTextBox.SelectionStart;

                DescriptionTextBox.Focus();
            }
            else
            {
                SurroundDescriptionTextWith("`", "code text");
            }

            DescriptionTextBox.TextWrapping = textwrap;
        }