[runtime] Fix corlib out of date error with disabled COM
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / Toolbox / TreeViewContainerStyleSelector.cs
1 //------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //------------------------------------------------------------
4
5 namespace System.Activities.Presentation.Toolbox
6 {
7     using System.Windows;
8     using System.Windows.Automation;
9     using System.Windows.Controls;
10     using System.Windows.Data;
11     using System.Windows.Input;
12     using System.Runtime;
13     using System.Globalization;
14
15     // This class is used to provide tree view item container template for Category
16     // and Tool objects
17
18     sealed class TreeViewContainerStyleSelector : StyleSelector
19     {
20         ToolboxControl owner;
21         Style styleForToolboxItem;
22         Style styleForCategoryItem;
23
24         public TreeViewContainerStyleSelector(ToolboxControl owner)
25         {
26             this.owner = owner;
27         }
28
29         //default style for ToolboxItemWrapper - this one is required to enable context search in tree view control
30         Style GetStyleForToolboxItem(Style baseStyle)
31         {
32             if (null == this.styleForToolboxItem)
33             {
34                 this.styleForToolboxItem = 
35                     (null == baseStyle ? new Style(typeof(TreeViewItem)) : new Style(typeof(TreeViewItem), baseStyle));
36
37                 //visibility information - i need to bind TreeViewItem to it
38                 if (null != this.owner.searchBox)
39                 {
40                     MultiBinding visibilityBinding = new MultiBinding() { Converter = new ToolItemVisibilityConverter() };
41                     Binding searchTextBinding = new Binding();
42                     searchTextBinding.Source = this.owner.searchBox;
43                     searchTextBinding.Path = new PropertyPath(TextBox.TextProperty);
44                     searchTextBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
45                     searchTextBinding.Mode = BindingMode.OneWay;
46
47                     Binding toolItemBinding = new Binding();
48                     toolItemBinding.RelativeSource = RelativeSource.Self;
49                     toolItemBinding.Path = new PropertyPath(TreeViewItem.HeaderProperty);
50                     toolItemBinding.Mode = BindingMode.OneWay;
51
52                     visibilityBinding.Bindings.Add(searchTextBinding);
53                     visibilityBinding.Bindings.Add(toolItemBinding);
54
55                     Setter visibilitySetter = new Setter();
56                     visibilitySetter.Property = TreeViewItem.VisibilityProperty;
57                     visibilitySetter.Value = visibilityBinding;
58
59                     //adding visibility setter to the style
60                     this.styleForToolboxItem.Setters.Add(visibilitySetter);
61                 }
62
63                 //take care of the AutomationId
64                 Binding automationIdBinding = new Binding();
65                 automationIdBinding.RelativeSource = RelativeSource.Self;
66                 automationIdBinding.Path = new PropertyPath("Header.ToolName");
67                 automationIdBinding.Mode = BindingMode.OneWay;
68
69                 Setter automationIdSetter = new Setter();
70                 automationIdSetter.Property = AutomationProperties.AutomationIdProperty;
71                 automationIdSetter.Value = automationIdBinding;
72                 this.styleForToolboxItem.Setters.Add(automationIdSetter);
73
74                 //to enable smooth keyboard operation, hidden items have to be disabled in order not to receive
75                 //keyboard events - Trigger does the job
76                 Setter enableSetter = new Setter(TreeViewItem.IsEnabledProperty, false);
77
78                 Trigger enableTrigger = new Trigger();
79                 enableTrigger.Property = TreeViewItem.VisibilityProperty;
80                 enableTrigger.Value = Visibility.Collapsed;
81                 enableTrigger.Setters.Add(enableSetter);
82
83                 //to enable drag&drop support add event setter for 
84                 EventSetter mouseMoveSetter = new EventSetter();
85                 mouseMoveSetter.Event = TreeViewItem.MouseMoveEvent;
86                 mouseMoveSetter.Handler = new MouseEventHandler(this.owner.OnToolMouseMove);
87                 this.styleForToolboxItem.Setters.Add(mouseMoveSetter);
88
89                 EventSetter mouseDoubleClickSetter = new EventSetter();
90                 mouseDoubleClickSetter.Event = TreeViewItem.MouseDoubleClickEvent;
91                 mouseDoubleClickSetter.Handler = new MouseButtonEventHandler(this.owner.OnTreeViewDoubleClick);
92                 this.styleForToolboxItem.Setters.Add(mouseDoubleClickSetter);
93
94                 //adding trigger to the style
95                 this.styleForToolboxItem.Triggers.Add(enableTrigger);
96             }
97             return this.styleForToolboxItem;
98         }
99
100         //default style for CategoryItem 
101         Style GetStyleForCategoryItem(Style baseStyle)
102         {
103             if (null == this.styleForCategoryItem)
104             {
105                 this.styleForCategoryItem =
106                     (null == baseStyle ? new Style(typeof(TreeViewItem)) : new Style(typeof(TreeViewItem), baseStyle));
107
108                 //take care of the AutomationId
109                 Binding automationIdBinding = new Binding();
110                 automationIdBinding.RelativeSource = RelativeSource.Self;
111                 automationIdBinding.Path = new PropertyPath("Header.CategoryName");
112                 automationIdBinding.Mode = BindingMode.OneWay;
113
114                 Setter automationIdSetter = new Setter();
115                 automationIdSetter.Property = AutomationProperties.AutomationIdProperty;
116                 automationIdSetter.Value = automationIdBinding;
117                 this.styleForCategoryItem.Setters.Add(automationIdSetter);
118             }
119             return this.styleForCategoryItem;
120         }
121
122         public override Style SelectStyle(object item, DependencyObject container)
123         {
124             //try get default style
125             Style result = base.SelectStyle(item, container);
126             if (item is ToolboxItemWrapper)
127             {
128                 result = GetStyleForToolboxItem(this.owner.ToolItemStyle);
129             }
130             if (item is ToolboxCategory && null != this.owner.CategoryItemStyle)
131             {
132                 result = GetStyleForCategoryItem( this.owner.CategoryItemStyle );
133             }
134             return result;
135         }
136
137         //helper converter - used to show/hide tool items based on the search criteria
138         sealed class ToolItemVisibilityConverter : IMultiValueConverter
139         {
140             public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
141             {
142                 //get the search text string
143                 var text = values[0] as string;
144                 //get current tool
145                 var tool = values[1] as ToolboxItemWrapper;
146                 var result = Visibility.Collapsed;
147                 //if tool is set and is valid
148                 if (null != tool && tool.IsValid)
149                 {
150                     //if search text is empty - show item
151                     if (string.IsNullOrEmpty(text))
152                     {
153                         result = Visibility.Visible;
154                     }
155                     else
156                     {
157                         if (string.IsNullOrEmpty(tool.DisplayName) || tool.DisplayName.IndexOf(text, StringComparison.OrdinalIgnoreCase) < 0)
158                         {
159                             result = Visibility.Collapsed;
160                         }
161                         else
162                         {
163                             result = Visibility.Visible;
164                         }
165                         
166                     }
167                 }
168                 return result;
169             }
170
171             public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
172             {
173                 throw FxTrace.Exception.AsError(new NotSupportedException());
174             }
175         }
176     }
177 }