[amd64/tramp] hide interpreter specific trampoline behind ifdef
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / Base / Core / Internal / PropertyEditing / PropertyValueDialogHost.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4 namespace System.Activities.Presentation.Internal.PropertyEditing
5 {
6     using System;
7     using System.ComponentModel;
8     using System.Diagnostics;
9     using System.Diagnostics.CodeAnalysis;
10     using System.Globalization;
11     using System.Windows;
12     using System.Windows.Input;
13     using System.Windows.Forms;
14     using System.Windows.Forms.Integration;
15
16     using System.Runtime;
17     using System.Activities.Presentation.Model;
18     using System.Activities.Presentation.PropertyEditing;
19
20     using System.Activities.Presentation.Internal.PropertyEditing.Model;
21     using System.Activities.Presentation.Internal.PropertyEditing.Editors;
22     using Microsoft.Win32;
23
24     // <summary>
25     // Helper class that is responsible for opening a [....] Form that hosts the WPF
26     // PropertyValueDialogControl that ultimately hosts a DialogPropertyValueEditor.
27     // Both PropertyInspector control as well as PropertyValueDialogControl use this class.
28     // </summary>
29     internal class PropertyValueDialogHost
30     {
31
32         private System.Windows.Forms.Form _dialogWindow;
33
34         // Ctor is private because it is created automatically when the OpenDialogWindow fires. 
35         private PropertyValueDialogHost()
36         {
37         }
38
39         // <summary>
40         // Helper that attaches the OpenDialogWindow command to a handler within this host
41         // class.  The handler will automatically open and populate the dialog window when
42         // the OpenDialogWindow command is fired.
43         // </summary>
44         // <param name="dialogRoot">UIElement that should handle the OpenDialogWindow command</param>
45         public static void AttachOpenDialogHandlers(UIElement dialogRoot)
46         {
47             Fx.Assert(dialogRoot != null, "dialogRoot parameter is null");
48
49             PropertyValueDialogHost dialogHost = new PropertyValueDialogHost();
50             dialogRoot.CommandBindings.Add(new CommandBinding(PropertyContainer.OpenDialogWindow, dialogHost.OnOpenDialogWindow, dialogHost.OnCanOpenDialogWindow));
51         }
52
53         // <summary>
54         // Called in response to OpenDialogWindow command firing. A dialog can be opened if
55         // a dialog editor DataTemplate exists on the corresponding property being edited.
56         // </summary>
57         // <param name="sender"></param>
58         // <param name="e"></param>
59         public void OnCanOpenDialogWindow(object sender, CanExecuteRoutedEventArgs e)
60         {
61             e.CanExecute = false;
62             DataTemplate dialogEditorTemplate = GetDialogEditorTemplate(GetParentProperty(e.OriginalSource));
63             if (dialogEditorTemplate == null)
64             {
65                 return;
66             }
67
68             // We can show a dialog
69             e.CanExecute = true;
70         }
71
72         // <summary>
73         // Called in response to OpenDialogWindow command firing. The method opens a
74         // [....] Form that contains an ElementHost that, in turn, contains
75         // PropertyValueDialogControl.
76         // </summary>
77         // <param name="sender"></param>
78         // <param name="e"></param>
79         [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
80         public void OnOpenDialogWindow(object sender, ExecutedRoutedEventArgs e)
81         {
82             // Hook on to the system user-preference changed event.
83             SystemEvents.UserPreferenceChanged += new UserPreferenceChangedEventHandler(OnUserPreferenceChanged);
84
85             // Is there a DataTemplate and PropertyValue to display?
86             PropertyEntry parentProperty = GetParentProperty(e.OriginalSource);
87             DataTemplate dialogEditorTemplate = GetDialogEditorTemplate(parentProperty);
88             if (dialogEditorTemplate == null)
89             {
90                 return;
91             }
92
93             // Create and populate a new Form
94             _dialogWindow = new Form();
95             _dialogWindow.ShowInTaskbar = false;
96             _dialogWindow.ShowIcon = false;
97             _dialogWindow.MaximizeBox = false;
98             _dialogWindow.MinimizeBox = false;
99             _dialogWindow.HelpButton = false;
100             _dialogWindow.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Auto;
101             _dialogWindow.Width = 600;
102             _dialogWindow.Height = 400;
103
104             // We need to change the title based on the type of the property being edited.
105             // e.g: For CollectionEditors we should say "CollectionEditor : <DisplayName>"
106             // For everything else we should say "Property Editor : <DisplayName>"
107             string title = System.Activities.Presentation.Internal.Properties.Resources.PropertyEditing_DialogValueEditorTitle;
108             _dialogWindow.MinimumSize = new System.Drawing.Size(575, 400); // Magic min-size numbers from Dan
109             _dialogWindow.Text = string.Format(
110                 CultureInfo.CurrentCulture,
111                 title,
112                 parentProperty.DisplayName);
113
114             PropertyValueDialogControl dialogControl = new PropertyValueDialogControl(parentProperty, dialogEditorTemplate);
115             dialogControl.CloseParentDialog += new EventHandler(OnCloseParentDialog);
116
117             using (ElementHost elementHost = new ElementHost())
118             {
119                 elementHost.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right;
120                 elementHost.Size = _dialogWindow.ClientSize;
121                 elementHost.Child = dialogControl;
122
123                 _dialogWindow.Controls.Add(elementHost);
124                 _dialogWindow.ShowDialog();
125                 dialogControl.OnParentDialogClosing();
126             }
127         }
128
129         // <summary>
130         // Event handler for system color changes. This will change the BackColor of the dialog
131         // matching the current tool window background color for Visual Studio.
132         // </summary>
133         // <param name="sender"></param>
134         // <param name="e"></param>
135
136         private void OnUserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e)
137         {
138             // 
139             //             if (PropertyInspectorFontAndColorDictionary.ColorService != null) {
140             //                 System.Drawing.Color backColor = PropertyInspectorFontAndColorDictionary.ColorService.GetColor(Microsoft.VisualStudio.Shell.Interop.__VSSYSCOLOREX.VSCOLOR_TOOLWINDOW_BACKGROUND, 255);
141             //                 _dialogWindow.BackColor = backColor;
142             //                 _dialogWindow.Refresh();
143             //                 _dialogWindow.Invalidate(true);
144             //             }//
145         }
146
147         // Event handler invoked when the PropertyValueDialogControl indicates that the dialog should close
148         private void OnCloseParentDialog(object sender, EventArgs e)
149         {
150             // Unhook on to the system user-preference changed event.
151             SystemEvents.UserPreferenceChanged -= new UserPreferenceChangedEventHandler(OnUserPreferenceChanged);
152
153             if (_dialogWindow != null)
154             {
155                 _dialogWindow.Close();
156             }
157         }
158
159         private static DataTemplate GetDialogEditorTemplate(PropertyEntry property)
160         {
161             if (property == null)
162             {
163                 return null;
164             }
165
166             // Does the current PropertyEntry have a dialog editor?
167             DialogPropertyValueEditor dialogEditor = property.PropertyValueEditor as DialogPropertyValueEditor;
168             if (dialogEditor == null)
169             {
170                 return null;
171             }
172
173             return dialogEditor.DialogEditorTemplate;
174         }
175
176         private static PropertyEntry GetParentProperty(object showDialogCommandSource)
177         {
178             // Was the command invoked from a control that we expect?
179             DependencyObject source = showDialogCommandSource as DependencyObject;
180             if (source == null)
181             {
182                 return null;
183             }
184
185             PropertyContainer container = PropertyContainer.GetOwningPropertyContainer(source);
186             if (container == null)
187             {
188                 return null;
189             }
190
191             // Does the current PropertyEntry have a dialog editor?
192             return container.PropertyEntry;
193         }
194     }
195 }