blob: b87fb436899d86b414973965d057d32e56c74d5b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
using System.Windows.Markup;
using System.Xml.Linq;
using Tango.Core.DI;
using Tango.FSE.Common.Notifications;
using Tango.FSE.Common.Threading;
using Tango.FSE.Common.WindowsManager;
using Tango.FSE.Procedures.Windows;
namespace Tango.FSE.Procedures
{
public class WindowController : IDialogWindowController
{
private const int GWL_HWNDPARENT = -8;
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);
private String _xaml;
private FrameworkElement _rootElement;
private ProcedureDialogWindow _dialogWindow;
private Thread _windowThread;
private String _title;
private bool _isShown;
[TangoInject]
private IDispatcherProvider DispatcherProvider { get; set; }
public static void SetOwnerWindowMultithread(IntPtr windowHandleOwned, IntPtr intPtrOwner)
{
if (windowHandleOwned != IntPtr.Zero && intPtrOwner != IntPtr.Zero)
{
SetWindowLong(windowHandleOwned, GWL_HWNDPARENT, intPtrOwner.ToInt32());
}
}
internal WindowController(String xaml, String title)
{
_xaml = xaml;
_title = title;
TangoIOC.Default.Inject(this);
}
internal void Init()
{
String xaml = _xaml;
XDocument doc = XDocument.Parse(xaml);
var textBoxes = doc.Descendants().Where(x => x.Name.LocalName == "TextBox").ToList();
textBoxes.ForEach(x => x.SetAttributeValue("Style", "{StaticResource ProcedureWindowTextBoxStyle}"));
xaml = doc.ToString();
FrameworkElement rootElement;
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write(xaml);
writer.Flush();
stream.Position = 0;
rootElement = (FrameworkElement)XamlReader.Load(stream);
_rootElement = rootElement;
stream.Dispose();
_dialogWindow = new ProcedureDialogWindow();
_dialogWindow.Width = _rootElement.Width;
_dialogWindow.Height = _rootElement.Height;
_dialogWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
_dialogWindow.Title = _title;
_dialogWindow.SetContent(rootElement);
//var textBoxesToRestore = rootElement.FindVisualChildren<TextBox>().ToList();
//textBoxesToRestore.ForEach(x => x.Style = null);
var helper = new WindowInteropHelper(_dialogWindow);
helper.EnsureHandle();
_windowThread = _dialogWindow.Dispatcher.Thread;
//System.Windows.Threading.Dispatcher.Run();
//Make the main window as owner ? could be risky..
//IntPtr mainWindowHanle = IntPtr.Zero;
//DispatcherProvider.InvokeBlock(() =>
//{
// mainWindowHanle = new WindowInteropHelper(Application.Current.MainWindow).Handle;
//});
//SetOwnerWindowMultithread(helper.Handle, mainWindowHanle);
}
public void Show()
{
if (!_isShown)
{
_isShown = true;
_dialogWindow.ShowDialog();
}
else
{
//_dialogWindow.Visibility = Visibility.Visible;
_dialogWindow.WindowState = WindowState.Normal;
}
}
public T FindControl<T>(String name) where T : DependencyObject
{
if (Thread.CurrentThread == _windowThread)
{
return _rootElement.FindChild<T>(name) as T;
}
else
{
bool completed = false;
T child = null;
DispatcherProvider.Invoke(() =>
{
child = _rootElement.FindChild<T>(name) as T;
completed = true;
});
while (!completed)
{
Thread.Sleep(10);
}
return child;
}
}
public void Close()
{
_dialogWindow.Close();
}
public void Hide()
{
//_dialogWindow.Hide();
_dialogWindow.WindowState = WindowState.Minimized;
}
}
}
|