df2fe178c2214c1729f6be666ccfd581aec1164b
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / DialogWindow.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4
5 namespace System.Activities.Presentation
6 {
7     using System.Windows;
8     using System.Activities.Presentation.View;
9     using System.Windows.Interop;
10     using System.Windows.Input;
11     using System.ComponentModel;
12     using Microsoft.Tools.Common;
13
14     internal class DialogWindow : Window
15     {        
16         public static readonly DependencyProperty ContextProperty =
17             DependencyProperty.Register("Context",
18             typeof(EditingContext),
19             typeof(DialogWindow));
20
21         string helpKeyword = HelpKeywords.HomePage;
22
23         public EditingContext Context
24         {
25             get { return (EditingContext)GetValue(ContextProperty); }
26             set { SetValue(ContextProperty, value); }
27         }
28
29         protected string HelpKeyword
30         {
31             get
32             {
33                 return this.helpKeyword;
34             }
35             set
36             {
37                 this.helpKeyword = value;
38             }
39         }
40
41         protected override void OnSourceInitialized(EventArgs e)
42         {
43             base.OnSourceInitialized(e);
44
45             this.HideMinMaxButton();
46             this.ShowContextHelpButton();
47             this.HideIcon();
48             this.AddWindowsHook(OnHookedWindowMessage);
49             this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Help, new ExecutedRoutedEventHandler(OnHelpExecuted)));
50             this.Closing += new CancelEventHandler(OnWindowClosing);
51         }
52
53         static IntPtr OnHookedWindowMessage(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
54         {
55             if (msg == Win32Interop.WM_SYSCOMMAND && wParam.ToInt64() == (long)Win32Interop.SC_CONTEXTHELP)
56             {
57                 var rootVisual = HwndSource.FromHwnd(hwnd).RootVisual;
58                 var focusedElement = FocusManager.GetFocusedElement(rootVisual);
59                 if (focusedElement == null)
60                 {
61                     focusedElement = rootVisual as IInputElement;
62                 }
63                 ApplicationCommands.Help.Execute(null, focusedElement);
64                 handled = true;
65             }
66
67             // According to MSDN, zero should be returned after handling WM_SYSCOMMAND.
68             // If this message is unhandled, it's still safe to return zero
69             // because WPF framework (HwndSource) will return zero anyway if the
70             // message is unhandled.
71             return IntPtr.Zero;
72         }       
73         
74         void OnWindowClosing(object sender, CancelEventArgs e)
75         {
76             // Note: Do NOT remove the hook if the close operation needs to be canceled.
77             this.RemoveWindowsHook(OnHookedWindowMessage);
78         }
79
80         void OnHelpExecuted(Object sender, ExecutedRoutedEventArgs e)
81         {
82             if (this.Context != null)            
83             {
84                 IIntegratedHelpService help = this.Context.Services.GetService<IIntegratedHelpService>();
85                 if (help != null)
86                 {
87                     help.ShowHelpFromKeyword(this.helpKeyword);
88                     return;
89                 }
90             }
91             System.Diagnostics.Process.Start(SR.DefaultHelpUrl);
92         }
93     }
94 }