1 | // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team |
---|
2 | // |
---|
3 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this |
---|
4 | // software and associated documentation files (the "Software"), to deal in the Software |
---|
5 | // without restriction, including without limitation the rights to use, copy, modify, merge, |
---|
6 | // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons |
---|
7 | // to whom the Software is furnished to do so, subject to the following conditions: |
---|
8 | // |
---|
9 | // The above copyright notice and this permission notice shall be included in all copies or |
---|
10 | // substantial portions of the Software. |
---|
11 | // |
---|
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
---|
13 | // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
---|
14 | // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
---|
15 | // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
---|
16 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
17 | // DEALINGS IN THE SOFTWARE. |
---|
18 | |
---|
19 | using System; |
---|
20 | using System.Collections.Generic; |
---|
21 | using System.Diagnostics; |
---|
22 | using System.Windows; |
---|
23 | using System.Windows.Documents; |
---|
24 | using System.Windows.Input; |
---|
25 | using System.Windows.Media; |
---|
26 | using System.Windows.Media.TextFormatting; |
---|
27 | using System.Windows.Navigation; |
---|
28 | |
---|
29 | namespace ICSharpCode.AvalonEdit.Rendering |
---|
30 | { |
---|
31 | /// <summary> |
---|
32 | /// VisualLineElement that represents a piece of text and is a clickable link. |
---|
33 | /// </summary> |
---|
34 | public class VisualLineLinkText : VisualLineText |
---|
35 | { |
---|
36 | /// <summary> |
---|
37 | /// Gets/Sets the URL that is navigated to when the link is clicked. |
---|
38 | /// </summary> |
---|
39 | public Uri NavigateUri { get; set; } |
---|
40 | |
---|
41 | /// <summary> |
---|
42 | /// Gets/Sets the window name where the URL will be opened. |
---|
43 | /// </summary> |
---|
44 | public string TargetName { get; set; } |
---|
45 | |
---|
46 | /// <summary> |
---|
47 | /// Gets/Sets whether the user needs to press Control to click the link. |
---|
48 | /// The default value is true. |
---|
49 | /// </summary> |
---|
50 | public bool RequireControlModifierForClick { get; set; } |
---|
51 | |
---|
52 | /// <summary> |
---|
53 | /// Creates a visual line text element with the specified length. |
---|
54 | /// It uses the <see cref="ITextRunConstructionContext.VisualLine"/> and its |
---|
55 | /// <see cref="VisualLineElement.RelativeTextOffset"/> to find the actual text string. |
---|
56 | /// </summary> |
---|
57 | public VisualLineLinkText(VisualLine parentVisualLine, int length) : base(parentVisualLine, length) |
---|
58 | { |
---|
59 | this.RequireControlModifierForClick = true; |
---|
60 | } |
---|
61 | |
---|
62 | /// <inheritdoc/> |
---|
63 | public override TextRun CreateTextRun(int startVisualColumn, ITextRunConstructionContext context) |
---|
64 | { |
---|
65 | this.TextRunProperties.SetForegroundBrush(context.TextView.LinkTextForegroundBrush); |
---|
66 | this.TextRunProperties.SetBackgroundBrush(context.TextView.LinkTextBackgroundBrush); |
---|
67 | this.TextRunProperties.SetTextDecorations(TextDecorations.Underline); |
---|
68 | return base.CreateTextRun(startVisualColumn, context); |
---|
69 | } |
---|
70 | |
---|
71 | /// <summary> |
---|
72 | /// Gets whether the link is currently clickable. |
---|
73 | /// </summary> |
---|
74 | /// <remarks>Returns true when control is pressed; or when |
---|
75 | /// <see cref="RequireControlModifierForClick"/> is disabled.</remarks> |
---|
76 | protected bool LinkIsClickable() |
---|
77 | { |
---|
78 | if (NavigateUri == null) |
---|
79 | return false; |
---|
80 | if (RequireControlModifierForClick) |
---|
81 | return (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control; |
---|
82 | else |
---|
83 | return true; |
---|
84 | } |
---|
85 | |
---|
86 | /// <inheritdoc/> |
---|
87 | protected internal override void OnQueryCursor(QueryCursorEventArgs e) |
---|
88 | { |
---|
89 | if (LinkIsClickable()) { |
---|
90 | e.Handled = true; |
---|
91 | e.Cursor = Cursors.Hand; |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | /// <inheritdoc/> |
---|
96 | [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", |
---|
97 | Justification = "I've seen Process.Start throw undocumented exceptions when the mail client / web browser is installed incorrectly")] |
---|
98 | protected internal override void OnMouseDown(MouseButtonEventArgs e) |
---|
99 | { |
---|
100 | if (e.ChangedButton == MouseButton.Left && !e.Handled && LinkIsClickable()) { |
---|
101 | RequestNavigateEventArgs args = new RequestNavigateEventArgs(this.NavigateUri, this.TargetName); |
---|
102 | args.RoutedEvent = Hyperlink.RequestNavigateEvent; |
---|
103 | FrameworkElement element = e.Source as FrameworkElement; |
---|
104 | if (element != null) { |
---|
105 | // allow user code to handle the navigation request |
---|
106 | element.RaiseEvent(args); |
---|
107 | } |
---|
108 | if (!args.Handled) { |
---|
109 | try { |
---|
110 | Process.Start(this.NavigateUri.ToString()); |
---|
111 | } catch { |
---|
112 | // ignore all kinds of errors during web browser start |
---|
113 | } |
---|
114 | } |
---|
115 | e.Handled = true; |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | /// <inheritdoc/> |
---|
120 | protected override VisualLineText CreateInstance(int length) |
---|
121 | { |
---|
122 | return new VisualLineLinkText(ParentVisualLine, length) { |
---|
123 | NavigateUri = this.NavigateUri, |
---|
124 | TargetName = this.TargetName, |
---|
125 | RequireControlModifierForClick = this.RequireControlModifierForClick |
---|
126 | }; |
---|
127 | } |
---|
128 | } |
---|
129 | } |
---|