/AvalonEdit/ICSharpCode.AvalonEdit/Rendering/VisualLineLinkText.cs

http://github.com/icsharpcode/ILSpy · C# · 130 lines · 74 code · 10 blank · 46 comment · 13 complexity · a93f3d3534a3a24b8f982663558bae92 MD5 · raw file

  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. using System;
  19. using System.Collections.Generic;
  20. using System.Diagnostics;
  21. using System.Windows;
  22. using System.Windows.Documents;
  23. using System.Windows.Input;
  24. using System.Windows.Media;
  25. using System.Windows.Media.TextFormatting;
  26. using System.Windows.Navigation;
  27. namespace ICSharpCode.AvalonEdit.Rendering
  28. {
  29. /// <summary>
  30. /// VisualLineElement that represents a piece of text and is a clickable link.
  31. /// </summary>
  32. public class VisualLineLinkText : VisualLineText
  33. {
  34. /// <summary>
  35. /// Gets/Sets the URL that is navigated to when the link is clicked.
  36. /// </summary>
  37. public Uri NavigateUri { get; set; }
  38. /// <summary>
  39. /// Gets/Sets the window name where the URL will be opened.
  40. /// </summary>
  41. public string TargetName { get; set; }
  42. /// <summary>
  43. /// Gets/Sets whether the user needs to press Control to click the link.
  44. /// The default value is true.
  45. /// </summary>
  46. public bool RequireControlModifierForClick { get; set; }
  47. /// <summary>
  48. /// Creates a visual line text element with the specified length.
  49. /// It uses the <see cref="ITextRunConstructionContext.VisualLine"/> and its
  50. /// <see cref="VisualLineElement.RelativeTextOffset"/> to find the actual text string.
  51. /// </summary>
  52. public VisualLineLinkText(VisualLine parentVisualLine, int length) : base(parentVisualLine, length)
  53. {
  54. this.RequireControlModifierForClick = true;
  55. }
  56. /// <inheritdoc/>
  57. public override TextRun CreateTextRun(int startVisualColumn, ITextRunConstructionContext context)
  58. {
  59. this.TextRunProperties.SetForegroundBrush(context.TextView.LinkTextForegroundBrush);
  60. this.TextRunProperties.SetBackgroundBrush(context.TextView.LinkTextBackgroundBrush);
  61. if (context.TextView.LinkTextUnderline)
  62. this.TextRunProperties.SetTextDecorations(TextDecorations.Underline);
  63. return base.CreateTextRun(startVisualColumn, context);
  64. }
  65. /// <summary>
  66. /// Gets whether the link is currently clickable.
  67. /// </summary>
  68. /// <remarks>Returns true when control is pressed; or when
  69. /// <see cref="RequireControlModifierForClick"/> is disabled.</remarks>
  70. protected bool LinkIsClickable()
  71. {
  72. if (NavigateUri == null)
  73. return false;
  74. if (RequireControlModifierForClick)
  75. return (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control;
  76. else
  77. return true;
  78. }
  79. /// <inheritdoc/>
  80. protected internal override void OnQueryCursor(QueryCursorEventArgs e)
  81. {
  82. if (LinkIsClickable()) {
  83. e.Handled = true;
  84. e.Cursor = Cursors.Hand;
  85. }
  86. }
  87. /// <inheritdoc/>
  88. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes",
  89. Justification = "I've seen Process.Start throw undocumented exceptions when the mail client / web browser is installed incorrectly")]
  90. protected internal override void OnMouseDown(MouseButtonEventArgs e)
  91. {
  92. if (e.ChangedButton == MouseButton.Left && !e.Handled && LinkIsClickable()) {
  93. RequestNavigateEventArgs args = new RequestNavigateEventArgs(this.NavigateUri, this.TargetName);
  94. args.RoutedEvent = Hyperlink.RequestNavigateEvent;
  95. FrameworkElement element = e.Source as FrameworkElement;
  96. if (element != null) {
  97. // allow user code to handle the navigation request
  98. element.RaiseEvent(args);
  99. }
  100. if (!args.Handled) {
  101. try {
  102. Process.Start(this.NavigateUri.ToString());
  103. } catch {
  104. // ignore all kinds of errors during web browser start
  105. }
  106. }
  107. e.Handled = true;
  108. }
  109. }
  110. /// <inheritdoc/>
  111. protected override VisualLineText CreateInstance(int length)
  112. {
  113. return new VisualLineLinkText(ParentVisualLine, length) {
  114. NavigateUri = this.NavigateUri,
  115. TargetName = this.TargetName,
  116. RequireControlModifierForClick = this.RequireControlModifierForClick
  117. };
  118. }
  119. }
  120. }