~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/CodeCompletion/InsightWindow.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
using System;
 
5
using System.Windows;
 
6
using System.Windows.Controls;
 
7
using ICSharpCode.AvalonEdit.Editing;
 
8
using ICSharpCode.AvalonEdit.Utils;
 
9
 
 
10
namespace ICSharpCode.AvalonEdit.CodeCompletion
 
11
{
 
12
        /// <summary>
 
13
        /// A popup-like window that is attached to a text segment.
 
14
        /// </summary>
 
15
        public class InsightWindow : CompletionWindowBase
 
16
        {
 
17
                static InsightWindow()
 
18
                {
 
19
                        DefaultStyleKeyProperty.OverrideMetadata(typeof(InsightWindow),
 
20
                                                                 new FrameworkPropertyMetadata(typeof(InsightWindow)));
 
21
                        AllowsTransparencyProperty.OverrideMetadata(typeof(InsightWindow),
 
22
                                                                    new FrameworkPropertyMetadata(Boxes.True));
 
23
                }
 
24
                
 
25
                /// <summary>
 
26
                /// Creates a new InsightWindow.
 
27
                /// </summary>
 
28
                public InsightWindow(TextArea textArea) : base(textArea)
 
29
                {
 
30
                        this.CloseAutomatically = true;
 
31
                        AttachEvents();
 
32
                        
 
33
                        Rect caret = this.TextArea.Caret.CalculateCaretRectangle();
 
34
                        Rect workingArea = System.Windows.Forms.Screen.FromPoint(caret.Location.ToSystemDrawing()).WorkingArea.ToWpf();
 
35
                        
 
36
                        MaxHeight = workingArea.Height;
 
37
                        MaxWidth = Math.Min(workingArea.Width, Math.Max(1000, workingArea.Width * 0.6));
 
38
                }
 
39
                
 
40
                /// <summary>
 
41
                /// Gets/Sets whether the insight window should close automatically.
 
42
                /// The default value is true.
 
43
                /// </summary>
 
44
                public bool CloseAutomatically { get; set; }
 
45
                
 
46
                /// <inheritdoc/>
 
47
                protected override bool CloseOnFocusLost {
 
48
                        get { return this.CloseAutomatically; }
 
49
                }
 
50
                
 
51
                void AttachEvents()
 
52
                {
 
53
                        this.TextArea.Caret.PositionChanged += CaretPositionChanged;
 
54
                }
 
55
                
 
56
                /// <inheritdoc/>
 
57
                protected override void DetachEvents()
 
58
                {
 
59
                        this.TextArea.Caret.PositionChanged -= CaretPositionChanged;
 
60
                        base.DetachEvents();
 
61
                }
 
62
                
 
63
                void CaretPositionChanged(object sender, EventArgs e)
 
64
                {
 
65
                        if (this.CloseAutomatically) {
 
66
                                int offset = this.TextArea.Caret.Offset;
 
67
                                if (offset < this.StartOffset || offset > this.EndOffset) {
 
68
                                        Close();
 
69
                                }
 
70
                        }
 
71
                }
 
72
        }
 
73
        
 
74
        /// <summary>
 
75
        /// TemplateSelector for InsightWindow to replace plain string content by a TextBlock with TextWrapping.
 
76
        /// </summary>
 
77
        internal sealed class InsightWindowTemplateSelector : DataTemplateSelector
 
78
        {
 
79
                /// <inheritdoc/>
 
80
                public override DataTemplate SelectTemplate(object item, DependencyObject container)
 
81
                {
 
82
                        if (item is string)
 
83
                                return (DataTemplate)((FrameworkElement)container).FindResource("TextBlockTemplate");
 
84
                        
 
85
                        return null;
 
86
                }
 
87
        }
 
88
}