DialogBase.cs

Craig Presti, 08/23/2010 02:43 pm

Download (2.8 kB)

 
1
using VastPark.Data;
2
using System.Threading;
3
using VastPark.Common;
4
using Plugin.Dialog.Controllers;
5
6
namespace Plugin.Dialog.Model
7
{
8
    public abstract class DialogBase : IDialog
9
    {
10
        public event Event<object> Closed;
11
12
        public string Title { get; set; }        
13
        public string Description { get; set; }
14
15
        public IDialogController DialogController { get; private set; }
16
        public IParkEngine ParkEngine { get; private set; }
17
18
        private Thread _StaThread;
19
20
        private object _ThreadLock;
21
        private double _DialogWidth;
22
        private double _DialogHeight;
23
        private System.Windows.Window _Dialog;
24
        
25
        public DialogBase(IDialogController dialogController, IParkEngine parkEngine)
26
        {
27
            this.DialogController = dialogController;
28
            this.ParkEngine = parkEngine;
29
30
            _ThreadLock = new object();
31
32
            _StaThread = new Thread(new ThreadStart(_WpfThread));
33
            _StaThread.IsBackground = true;
34
            _StaThread.SetApartmentState(ApartmentState.STA);
35
            _StaThread.Start();
36
        }
37
38
        private void _WpfThread()
39
        {
40
            _Dialog = this.DialogController.CreateWindow(this.ParkEngine.RenderEngine.Handle);
41
            _DialogWidth = _Dialog.Width;
42
            _DialogHeight = _Dialog.Height;
43
44
            lock (_ThreadLock)
45
            {
46
                Monitor.PulseAll(_ThreadLock);
47
            }
48
49
            System.Windows.Threading.Dispatcher.Run();
50
        }
51
52
        public IDialogResult Show()
53
        {
54
            lock (_ThreadLock)
55
            {
56
                while (_Dialog == null)
57
                {
58
                    Monitor.Wait(_ThreadLock);
59
                }
60
            }
61
62
            //work out the position of the host control
63
            var host = System.Windows.Forms.Control.FromHandle(this.ParkEngine.RenderEngine.Handle);
64
65
            var location = new System.Drawing.Point();
66
67
            host.Invoke((ThreadStart)delegate
68
            {
69
                location = host.PointToScreen(host.Location);
70
            }); 
71
72
            var left = location.X + this.ParkEngine.RenderEngine.ViewportWidth / 2 - (_DialogWidth / 2);
73
            var top = location.Y + this.ParkEngine.RenderEngine.ViewportHeight / 2 - (_DialogHeight / 2);
74
75
            IDialogResult result = null;
76
77
            System.Windows.Threading.Dispatcher.FromThread(_StaThread).Invoke((System.Threading.ThreadStart)delegate
78
            {
79
                result = this.DialogController.ShowDialog(this, left, top);
80
            });
81
82
            this.Closed.Raise(this, result);
83
84
            return result;
85
        }
86
87
        public void Dispose()
88
        {
89
            System.Windows.Threading.Dispatcher.FromThread(_StaThread).InvokeShutdown();
90
        }
91
    }
92
}