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.Windows; |
---|
21 | using System.Windows.Controls; |
---|
22 | using System.Windows.Media; |
---|
23 | using System.Windows.Media.TextFormatting; |
---|
24 | |
---|
25 | namespace ICSharpCode.AvalonEdit.Rendering |
---|
26 | { |
---|
27 | /// <summary> |
---|
28 | /// A inline UIElement in the document. |
---|
29 | /// </summary> |
---|
30 | public class InlineObjectElement : VisualLineElement |
---|
31 | { |
---|
32 | /// <summary> |
---|
33 | /// Gets the inline element that is displayed. |
---|
34 | /// </summary> |
---|
35 | public UIElement Element { get; private set; } |
---|
36 | |
---|
37 | /// <summary> |
---|
38 | /// Creates a new InlineObjectElement. |
---|
39 | /// </summary> |
---|
40 | /// <param name="documentLength">The length of the element in the document. Must be non-negative.</param> |
---|
41 | /// <param name="element">The element to display.</param> |
---|
42 | public InlineObjectElement(int documentLength, UIElement element) |
---|
43 | : base(1, documentLength) |
---|
44 | { |
---|
45 | if (element == null) |
---|
46 | throw new ArgumentNullException("element"); |
---|
47 | this.Element = element; |
---|
48 | } |
---|
49 | |
---|
50 | /// <inheritdoc/> |
---|
51 | public override TextRun CreateTextRun(int startVisualColumn, ITextRunConstructionContext context) |
---|
52 | { |
---|
53 | if (context == null) |
---|
54 | throw new ArgumentNullException("context"); |
---|
55 | |
---|
56 | return new InlineObjectRun(1, this.TextRunProperties, this.Element); |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | /// <summary> |
---|
61 | /// A text run with an embedded UIElement. |
---|
62 | /// </summary> |
---|
63 | public class InlineObjectRun : TextEmbeddedObject |
---|
64 | { |
---|
65 | UIElement element; |
---|
66 | int length; |
---|
67 | TextRunProperties properties; |
---|
68 | internal Size desiredSize; |
---|
69 | |
---|
70 | /// <summary> |
---|
71 | /// Creates a new InlineObjectRun instance. |
---|
72 | /// </summary> |
---|
73 | /// <param name="length">The length of the TextRun.</param> |
---|
74 | /// <param name="properties">The <see cref="TextRunProperties"/> to use.</param> |
---|
75 | /// <param name="element">The <see cref="UIElement"/> to display.</param> |
---|
76 | public InlineObjectRun(int length, TextRunProperties properties, UIElement element) |
---|
77 | { |
---|
78 | if (length <= 0) |
---|
79 | throw new ArgumentOutOfRangeException("length", length, "Value must be positive"); |
---|
80 | if (properties == null) |
---|
81 | throw new ArgumentNullException("properties"); |
---|
82 | if (element == null) |
---|
83 | throw new ArgumentNullException("element"); |
---|
84 | |
---|
85 | this.length = length; |
---|
86 | this.properties = properties; |
---|
87 | this.element = element; |
---|
88 | } |
---|
89 | |
---|
90 | /// <summary> |
---|
91 | /// Gets the element displayed by the InlineObjectRun. |
---|
92 | /// </summary> |
---|
93 | public UIElement Element { |
---|
94 | get { return element; } |
---|
95 | } |
---|
96 | |
---|
97 | /// <summary> |
---|
98 | /// Gets the VisualLine that contains this object. This property is only available after the object |
---|
99 | /// was added to the text view. |
---|
100 | /// </summary> |
---|
101 | public VisualLine VisualLine { get; internal set; } |
---|
102 | |
---|
103 | /// <inheritdoc/> |
---|
104 | public override LineBreakCondition BreakBefore { |
---|
105 | get { return LineBreakCondition.BreakDesired; } |
---|
106 | } |
---|
107 | |
---|
108 | /// <inheritdoc/> |
---|
109 | public override LineBreakCondition BreakAfter { |
---|
110 | get { return LineBreakCondition.BreakDesired; } |
---|
111 | } |
---|
112 | |
---|
113 | /// <inheritdoc/> |
---|
114 | public override bool HasFixedSize { |
---|
115 | get { return true; } |
---|
116 | } |
---|
117 | |
---|
118 | /// <inheritdoc/> |
---|
119 | public override CharacterBufferReference CharacterBufferReference { |
---|
120 | get { return new CharacterBufferReference(); } |
---|
121 | } |
---|
122 | |
---|
123 | /// <inheritdoc/> |
---|
124 | public override int Length { |
---|
125 | get { return length; } |
---|
126 | } |
---|
127 | |
---|
128 | /// <inheritdoc/> |
---|
129 | public override TextRunProperties Properties { |
---|
130 | get { return properties; } |
---|
131 | } |
---|
132 | |
---|
133 | /// <inheritdoc/> |
---|
134 | public override TextEmbeddedObjectMetrics Format(double remainingParagraphWidth) |
---|
135 | { |
---|
136 | double baseline = TextBlock.GetBaselineOffset(element); |
---|
137 | if (double.IsNaN(baseline)) |
---|
138 | baseline = desiredSize.Height; |
---|
139 | return new TextEmbeddedObjectMetrics(desiredSize.Width, desiredSize.Height, baseline); |
---|
140 | } |
---|
141 | |
---|
142 | /// <inheritdoc/> |
---|
143 | public override Rect ComputeBoundingBox(bool rightToLeft, bool sideways) |
---|
144 | { |
---|
145 | if (this.element.IsArrangeValid) { |
---|
146 | double baseline = TextBlock.GetBaselineOffset(element); |
---|
147 | if (double.IsNaN(baseline)) |
---|
148 | baseline = desiredSize.Height; |
---|
149 | return new Rect(new Point(0, -baseline), desiredSize); |
---|
150 | } else { |
---|
151 | return Rect.Empty; |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | /// <inheritdoc/> |
---|
156 | public override void Draw(DrawingContext drawingContext, Point origin, bool rightToLeft, bool sideways) |
---|
157 | { |
---|
158 | } |
---|
159 | } |
---|
160 | } |
---|