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 ICSharpCode.AvalonEdit.Editing; |
---|
23 | using ICSharpCode.AvalonEdit.Utils; |
---|
24 | |
---|
25 | namespace ICSharpCode.AvalonEdit.CodeCompletion |
---|
26 | { |
---|
27 | /// <summary> |
---|
28 | /// A popup-like window that is attached to a text segment. |
---|
29 | /// </summary> |
---|
30 | public class InsightWindow : CompletionWindowBase |
---|
31 | { |
---|
32 | static InsightWindow() |
---|
33 | { |
---|
34 | DefaultStyleKeyProperty.OverrideMetadata(typeof(InsightWindow), |
---|
35 | new FrameworkPropertyMetadata(typeof(InsightWindow))); |
---|
36 | AllowsTransparencyProperty.OverrideMetadata(typeof(InsightWindow), |
---|
37 | new FrameworkPropertyMetadata(Boxes.True)); |
---|
38 | } |
---|
39 | |
---|
40 | /// <summary> |
---|
41 | /// Creates a new InsightWindow. |
---|
42 | /// </summary> |
---|
43 | public InsightWindow(TextArea textArea) : base(textArea) |
---|
44 | { |
---|
45 | this.CloseAutomatically = true; |
---|
46 | AttachEvents(); |
---|
47 | } |
---|
48 | |
---|
49 | /// <inheritdoc/> |
---|
50 | protected override void OnSourceInitialized(EventArgs e) |
---|
51 | { |
---|
52 | Rect caret = this.TextArea.Caret.CalculateCaretRectangle(); |
---|
53 | Point pointOnScreen = this.TextArea.TextView.PointToScreen(caret.Location - this.TextArea.TextView.ScrollOffset); |
---|
54 | Rect workingArea = System.Windows.Forms.Screen.FromPoint(pointOnScreen.ToSystemDrawing()).WorkingArea.ToWpf().TransformFromDevice(this); |
---|
55 | |
---|
56 | MaxHeight = workingArea.Height; |
---|
57 | MaxWidth = Math.Min(workingArea.Width, Math.Max(1000, workingArea.Width * 0.6)); |
---|
58 | |
---|
59 | base.OnSourceInitialized(e); |
---|
60 | } |
---|
61 | |
---|
62 | /// <summary> |
---|
63 | /// Gets/Sets whether the insight window should close automatically. |
---|
64 | /// The default value is true. |
---|
65 | /// </summary> |
---|
66 | public bool CloseAutomatically { get; set; } |
---|
67 | |
---|
68 | /// <inheritdoc/> |
---|
69 | protected override bool CloseOnFocusLost { |
---|
70 | get { return this.CloseAutomatically; } |
---|
71 | } |
---|
72 | |
---|
73 | void AttachEvents() |
---|
74 | { |
---|
75 | this.TextArea.Caret.PositionChanged += CaretPositionChanged; |
---|
76 | } |
---|
77 | |
---|
78 | /// <inheritdoc/> |
---|
79 | protected override void DetachEvents() |
---|
80 | { |
---|
81 | this.TextArea.Caret.PositionChanged -= CaretPositionChanged; |
---|
82 | base.DetachEvents(); |
---|
83 | } |
---|
84 | |
---|
85 | void CaretPositionChanged(object sender, EventArgs e) |
---|
86 | { |
---|
87 | if (this.CloseAutomatically) { |
---|
88 | int offset = this.TextArea.Caret.Offset; |
---|
89 | if (offset < this.StartOffset || offset > this.EndOffset) { |
---|
90 | Close(); |
---|
91 | } |
---|
92 | } |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | /// <summary> |
---|
97 | /// TemplateSelector for InsightWindow to replace plain string content by a TextBlock with TextWrapping. |
---|
98 | /// </summary> |
---|
99 | internal sealed class InsightWindowTemplateSelector : DataTemplateSelector |
---|
100 | { |
---|
101 | public override DataTemplate SelectTemplate(object item, DependencyObject container) |
---|
102 | { |
---|
103 | if (item is string) |
---|
104 | return (DataTemplate)((FrameworkElement)container).FindResource("TextBlockTemplate"); |
---|
105 | |
---|
106 | return null; |
---|
107 | } |
---|
108 | } |
---|
109 | } |
---|