This repository was archived by the owner on Oct 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 808
Expand file tree
/
Copy pathImageElement.cs
More file actions
101 lines (85 loc) · 2.81 KB
/
Copy pathImageElement.cs
File metadata and controls
101 lines (85 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.TextFormatting;
using ICSharpCode.AvalonEdit.Rendering;
namespace EmbeddedImageAddIn
{
/// <summary>
/// VisualLineElement implementation for embedded images.
/// </summary>
public class ImageElement : VisualLineElement
{
readonly string imageFileName;
readonly ImageSource image;
public ImageElement(string imageFileName, ImageSource image, int documentLength) : base(1, documentLength)
{
if (imageFileName == null)
throw new ArgumentNullException("imageFileName");
if (image == null)
throw new ArgumentNullException("image");
this.imageFileName = imageFileName;
this.image = image;
}
public override TextRun CreateTextRun(int startVisualColumn, ITextRunConstructionContext context)
{
return new ImageTextRun(image, this.TextRunProperties);
}
protected override void OnMouseDown(MouseButtonEventArgs e)
{
base.OnMouseDown(e);
if (!e.Handled && e.ClickCount == 2) {
// double click on image: open the image in editor
try {
Process.Start(imageFileName);
} catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
}
sealed class ImageTextRun : TextEmbeddedObject
{
readonly ImageSource image;
readonly TextRunProperties properties;
public ImageTextRun(ImageSource image, TextRunProperties properties)
{
this.image = image;
this.properties = properties;
}
public override LineBreakCondition BreakBefore {
get { return LineBreakCondition.BreakPossible; }
}
public override LineBreakCondition BreakAfter {
get { return LineBreakCondition.BreakPossible; }
}
public override bool HasFixedSize {
get { return true; }
}
public override CharacterBufferReference CharacterBufferReference {
get { return new CharacterBufferReference(); }
}
public override int Length {
get { return 1; }
}
public override TextRunProperties Properties {
get { return properties; }
}
public override TextEmbeddedObjectMetrics Format(double remainingParagraphWidth)
{
return new TextEmbeddedObjectMetrics(image.Width, image.Height, image.Height);
}
public override Rect ComputeBoundingBox(bool rightToLeft, bool sideways)
{
return new Rect(0, 0, image.Width, image.Height);
}
public override void Draw(DrawingContext drawingContext, Point origin, bool rightToLeft, bool sideways)
{
drawingContext.DrawImage(image, new Rect(origin.X, origin.Y - image.Height, image.Width, image.Height));
}
}
}
}