Exemplo n.º 1
0
        /// <summary>
        /// Handler for inserting of the hyperlink
        /// </summary>
        public static void OnInsertHyperlink(object sender, ExecutedRoutedEventArgs e)
        {
            RichTextEditor control = (RichTextEditor)sender;
            RichTextBox richTextBox = control.RichTextBox;


            if (richTextBox.Selection.IsEmpty)
            {
                return;
            }

            TextPointer insertionPosition = richTextBox.Selection.Start;
            Paragraph paragraph = insertionPosition.Paragraph;
            Hyperlink hyperlink = null;

            Image image = Helper.GetImageAncestor(insertionPosition);
            InlineUIContainer inlineUIContainer = Helper.GetInlineUIContainer(insertionPosition);
            if (image != null)
            {
                OnEditPictureProperties(sender, e);
                return;
            }

            //textRange.Text
            //### Detect existing hyperlink
            //hyperlink = GetHyperlinkAncestor(insertionPosition);
            TextRange textRange = richTextBox.Selection;
            foreach (Inline inline in paragraph.Inlines)
            {
                //if (inline is Hyperlink && ((Hyperlink)inline).Tag is TextPointerPaar)
                if (inline is Hyperlink)
                {
                    hyperlink = (Hyperlink)inline;
                    TextRange textRangeHyper = new TextRange(hyperlink.ElementStart,hyperlink.ElementEnd);

                    //if (0 == richTextBox.Selection.Start.CompareTo(((TextPointerPaar)hyperlink.Tag).Start) && 0 == richTextBox.Selection.End.CompareTo(((TextPointerPaar)hyperlink.Tag).End))
                    if (textRange.Text == textRangeHyper.Text)
                    {
                        break;
                    }
                    else
                    {
                        hyperlink = null;
                    }
                }
            }

            HyperlinkPropertiesDialog hyperlinkPropertiesDlg = new HyperlinkPropertiesDialog(hyperlink);
            hyperlinkPropertiesDlg.ShowDialog();

            if (true == hyperlinkPropertiesDlg.DialogResult)
            {
                try
                {
                    Uri uri = new Uri(hyperlinkPropertiesDlg.NavigateUri); 
                    if (null == hyperlink)
                    {
                        hyperlink = new Hyperlink(richTextBox.Selection.Start, richTextBox.Selection.End);
                        //hyperlink.Tag = new TextPointerPaar(richTextBox.Selection.Start,richTextBox.Selection.End);
                    }
                    hyperlink.NavigateUri = uri;
                    /*
                    if (String.IsNullOrEmpty(hyperlinkPropertiesDlg.HyperlinkDesc))
                    {
                        hyperlink.ToolTip = null;
                    }
                    else
                    {
                        ToolTip toolTip = new ToolTip();
                        toolTip.Content = hyperlinkPropertiesDlg.HyperlinkDesc;
                        hyperlink.ToolTip = toolTip;
                    }*/
                }
                catch (Exception ex)
                {
                    MessageBox.Show(String.Format("{0} ({1}) :\n\r{2}",Res.txtLoadHyperlinkFailed,hyperlinkPropertiesDlg.NavigateUri,ex), Res.txtEditorName);
                }
            }

            //paragraph.Inlines.Add(hyperlink);
       }
Exemplo n.º 2
0
        /// <summary>
        /// Handler for inserting of the hyperlink
        /// </summary>
        public static void OnInsertHyperlink(object sender, ExecutedRoutedEventArgs e)
        {
            RichTextEditor control     = (RichTextEditor)sender;
            RichTextBox    richTextBox = control.RichTextBox;


            if (richTextBox.Selection.IsEmpty)
            {
                return;
            }

            TextPointer insertionPosition = richTextBox.Selection.Start;
            Paragraph   paragraph         = insertionPosition.Paragraph;
            Hyperlink   hyperlink         = null;

            Image             image             = Helper.GetImageAncestor(insertionPosition);
            InlineUIContainer inlineUIContainer = Helper.GetInlineUIContainer(insertionPosition);

            if (image != null)
            {
                OnEditPictureProperties(sender, e);
                return;
            }

            //textRange.Text
            //### Detect existing hyperlink
            //hyperlink = GetHyperlinkAncestor(insertionPosition);
            TextRange textRange = richTextBox.Selection;

            foreach (Inline inline in paragraph.Inlines)
            {
                //if (inline is Hyperlink && ((Hyperlink)inline).Tag is TextPointerPaar)
                if (inline is Hyperlink)
                {
                    hyperlink = (Hyperlink)inline;
                    TextRange textRangeHyper = new TextRange(hyperlink.ElementStart, hyperlink.ElementEnd);

                    //if (0 == richTextBox.Selection.Start.CompareTo(((TextPointerPaar)hyperlink.Tag).Start) && 0 == richTextBox.Selection.End.CompareTo(((TextPointerPaar)hyperlink.Tag).End))
                    if (textRange.Text == textRangeHyper.Text)
                    {
                        break;
                    }
                    else
                    {
                        hyperlink = null;
                    }
                }
            }

            HyperlinkPropertiesDialog hyperlinkPropertiesDlg = new HyperlinkPropertiesDialog(hyperlink);

            hyperlinkPropertiesDlg.ShowDialog();

            if (true == hyperlinkPropertiesDlg.DialogResult)
            {
                try
                {
                    Uri uri = new Uri(hyperlinkPropertiesDlg.NavigateUri);
                    if (null == hyperlink)
                    {
                        hyperlink = new Hyperlink(richTextBox.Selection.Start, richTextBox.Selection.End);
                        //hyperlink.Tag = new TextPointerPaar(richTextBox.Selection.Start,richTextBox.Selection.End);
                    }
                    hyperlink.NavigateUri = uri;

                    /*
                     * if (String.IsNullOrEmpty(hyperlinkPropertiesDlg.HyperlinkDesc))
                     * {
                     *  hyperlink.ToolTip = null;
                     * }
                     * else
                     * {
                     *  ToolTip toolTip = new ToolTip();
                     *  toolTip.Content = hyperlinkPropertiesDlg.HyperlinkDesc;
                     *  hyperlink.ToolTip = toolTip;
                     * }*/
                }
                catch (Exception ex)
                {
                    MessageBox.Show(String.Format("{0} ({1}) :\n\r{2}", Res.txtLoadHyperlinkFailed, hyperlinkPropertiesDlg.NavigateUri, ex), Res.txtEditorName);
                }
            }

            //paragraph.Inlines.Add(hyperlink);
        }