Fix XMM scanning on Mac x86.
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / Base / Core / Internal / PropertyEditing / Automation / PropertyContainerAutomationPeer.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4 namespace System.Activities.Presentation.Internal.PropertyEditing.Automation 
5 {
6     using System;
7     using System.Collections;
8     using System.Collections.Generic;
9     using System.ComponentModel;
10     using System.Text;
11     using System.Windows;
12     using System.Windows.Automation;
13     using System.Windows.Automation.Peers;
14     using System.Windows.Automation.Provider;
15     using System.Windows.Controls;
16     using System.Windows.Media;
17
18     using System.Activities.Presentation;
19     using System.Activities.Presentation.PropertyEditing;
20
21     using System.Activities.Presentation.Internal.PropertyEditing.FromExpression.Framework.PropertyInspector;
22     using System.Activities.Presentation.Internal.PropertyEditing.Selection;
23
24     // <summary>
25     // AutomationPeer for PropertyContainer
26     // </summary>
27     internal class PropertyContainerAutomationPeer : UIElementAutomationPeer, IValueProvider, IScrollItemProvider, IAutomationFocusChangedEventSource
28     {
29
30         private PropertyContainer _container;
31
32         // <summary>
33         // Basic ctor
34         // </summary>
35         // <param name="container"></param>
36         public PropertyContainerAutomationPeer(PropertyContainer container)
37             : base(container) 
38         {
39             if (container == null)
40             {
41                 throw FxTrace.Exception.ArgumentNull("container");
42             }
43
44             _container = container;
45         }
46
47         // <summary>
48         // Gets a value indicating whether the contained PropertyContainer is read-only
49         // </summary>
50         public bool IsReadOnly 
51         {
52             get { return _container.PropertyEntry.IsReadOnly || !_container.PropertyEntry.PropertyValue.CanConvertFromString; }
53         }
54
55         // <summary>
56         // Gets the value of the property within the PropertyContainer
57         // </summary>
58         public string Value 
59         {
60             get { return _container.PropertyEntry.PropertyValue.StringValue; }
61         }
62
63
64         // IScrollItemProvider Members
65
66         // <summary>
67         //  This public method is called when parent creates the CategoryContainerItemAutomationPeer
68         //  in the GetChildrenCore, thus listening to the changes in the IsSelectedProperty.
69         // </summary>
70         internal void AddFocusEvents() 
71         {
72             if (_container != null) 
73             {
74                 HookUpFocusEvents(_container, OnIsSelectedValueChanged);
75             }
76         }
77
78         // <summary>
79         // "PropertyContainer"
80         // </summary>
81         // <returns>"PropertyContainer"</returns>
82         protected override string GetClassNameCore() 
83         {
84             return typeof(PropertyContainer).Name;
85         }
86
87         // <summary>
88         // Returns the name of the contained property
89         // </summary>
90         // <returns></returns>
91         protected override string GetNameCore() 
92         {
93             return _container.PropertyEntry == null ? string.Empty : _container.PropertyEntry.DisplayName;
94         }
95
96         // <summary>
97         // Currently supported patterns: Value
98         // </summary>
99         // <param name="patternInterface"></param>
100         // <returns></returns>
101         public override object GetPattern(PatternInterface patternInterface) 
102         {
103             if (patternInterface == PatternInterface.Value)
104             {
105                 return this;
106             }
107             else if (patternInterface == PatternInterface.ScrollItem)
108             {
109                 return this;
110             }
111
112             return base.GetPattern(patternInterface);
113         }
114
115         // <summary>
116         // Private helper function to listen to the changes in the IsSelectedProperty,
117         // which then fires the OnValueChanged event.
118         // </summary>
119         // <param name="expander">Expander control</param>
120         // <param name="valueChangedEvent">ValueChanged event</param>
121         private static void HookUpFocusEvents(PropertyContainer container, EventHandler valueChangedEvent) 
122         {
123             if (container != null) 
124             {
125                 DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(PropertySelection.IsSelectedProperty, typeof(PropertyContainer));
126                 if (dpd != null) 
127                 {
128                     dpd.AddValueChanged(container, valueChangedEvent);
129                 }
130             }
131         }
132
133         // <summary>
134         // The actual event handler, that fires when the IsSelected DP changes.
135         // Here we raise the AutomationFocus event for the
136         // Advanced (More Properties) properties expander.
137         // </summary>
138         // <param name="sender">Expander</param>
139         // <param name="e">EventArgs</param>
140         private void OnIsSelectedValueChanged(object sender, EventArgs e) 
141         {
142             // Add logic to respond to "Selection"
143             bool curVal = PropertySelection.GetIsSelected(sender as DependencyObject);
144             if (curVal) 
145             {
146                 this.RaiseAutomationEvent(AutomationEvents.AutomationFocusChanged);
147             }
148         }
149
150         // <summary>
151         //  This public method is called when parent creates the CategoryContainerItemAutomationPeer
152         //  in the GetChildrenCore, thus clearing off all the event listeners before we add new ones
153         // </summary>
154         public void RemoveFocusEvents() 
155         {
156             if (_container != null) 
157             {
158                 UnHookFocusEvents(_container, OnIsSelectedValueChanged);
159             }
160         }
161
162         // <summary>
163         // Private method to unhook the ValueChanged event.
164         // </summary>
165         // <param name="expander">Expander</param>
166         // <param name="valueChangedEvent">ValueChanged event</param>
167         private static void UnHookFocusEvents(PropertyContainer container, EventHandler valueChangedEvent) 
168         {
169             if (container != null) 
170             {
171                 DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(PropertySelection.IsSelectedProperty, typeof(PropertyContainer));
172                 if (dpd != null) 
173                 {
174                     dpd.RemoveValueChanged(container, valueChangedEvent);
175                 }
176             }
177         }
178
179         // IValueProvider Members
180
181         // <summary>
182         // Sets the value of the property within the PropertyContainer
183         // </summary>
184         // <param name="value">Value to set</param>
185         public void SetValue(string value) 
186         {
187             _container.PropertyEntry.PropertyValue.StringValue = value;
188         }
189
190         // <summary>
191         // Scrolls the contained PropertyContainer into view, if it's within a scrolling control
192         // </summary>
193         public void ScrollIntoView() 
194         {
195             _container.BringIntoView();
196         }
197
198
199         // IAutomationFocusChangedEventSource Members
200         public void UnloadEventHook() 
201         {
202             RemoveFocusEvents();
203         }
204
205     }
206 }