-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlashWrapper.cs
182 lines (152 loc) · 4.3 KB
/
FlashWrapper.cs
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using AxShockwaveFlashObjects;
using log4net;
using Qisope.Utils.Delegates;
using Qisope.Views.App;
namespace Qisope
{
public class FlashWrapper : IDisposable
{
private readonly ILog _log;
private AxFlashControl _axFlashControl;
private Form _hostForm;
public FlashWrapper(AxFlashControl axFlashControl)
{
_log = LogManager.GetLogger(GetType());
_axFlashControl = axFlashControl;
if (_axFlashControl.IsHandleCreated)
{
Initialize(_axFlashControl);
}
else
{
_log.Debug("Handle is not created for AxFlashControl, wiring HandleCreated event");
_axFlashControl.HandleCreated += AxFlashControlOnHandleCreated;
}
}
public Point LocationOnForm
{
get
{
if (_axFlashControl == null)
{
return Point.Empty;
}
if (_hostForm == null)
{
_hostForm = _axFlashControl.FindForm();
if (_hostForm == null)
{
return Point.Empty;
}
}
Point flashPointToScreen = _axFlashControl.PointToScreen(_axFlashControl.Location);
Point hostFormLocation = _hostForm.Location;
return new Point(flashPointToScreen.X - hostFormLocation.X, flashPointToScreen.Y - hostFormLocation.Y);
}
}
public Rectangle BoundsOnControl
{
get { return new Rectangle(LocationOnForm.X, LocationOnForm.Y, _axFlashControl.Width, _axFlashControl.Height); }
}
#region IDisposable Members
public void Dispose()
{
if (_axFlashControl != null)
{
_axFlashControl.HandleCreated -= AxFlashControlOnHandleCreated;
_axFlashControl.FlashCall -= AxShockwaveFlashOnFlashCall;
_axFlashControl.PreviewKeyDown -= AxShockwaveFlashPreviewKeyDown;
_axFlashControl = null;
}
_hostForm = null;
FlashCall = null;
UnhandledFlashCallback = null;
PreviewKeyDown = null;
}
#endregion
public event PreviewKeyDownEventHandler PreviewKeyDown;
public event EventHandler<FlashCallEventArgs> FlashCall;
public event Action<string> UnhandledFlashCallback;
public void Initialize(AxFlashControl axFlashControl)
{
_axFlashControl.FlashCall += AxShockwaveFlashOnFlashCall;
_axFlashControl.PreviewKeyDown += AxShockwaveFlashPreviewKeyDown;
_hostForm = _axFlashControl.FindForm();
}
private void AxFlashControlOnHandleCreated(object sender, EventArgs args)
{
if (_axFlashControl.IsHandleCreated)
{
Initialize(_axFlashControl);
}
}
private void AxShockwaveFlashOnFlashCall(object sender, _IShockwaveFlashEvents_FlashCallEvent e)
{
EventHandler<FlashCallEventArgs> eventHandler = FlashCall;
if (eventHandler == null)
{
return;
}
var args = new FlashCallEventArgs(e, false);
eventHandler(sender, args);
if (!args.Handled && UnhandledFlashCallback != null)
{
UnhandledFlashCallback(e.request);
}
}
private void AxShockwaveFlashPreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (PreviewKeyDown != null)
{
PreviewKeyDown(sender, e);
}
}
public string CallFunction(string methodInvokeMessage)
{
try
{
if (_axFlashControl == null || !_axFlashControl.IsHandleCreated || _axFlashControl.IsDisposed)
{
return "<undefined/>";
}
string result;
if (_axFlashControl.InvokeRequired)
{
Func<string, string> callFunction = _axFlashControl.CallFunction;
result = _axFlashControl.Invoke(callFunction, methodInvokeMessage) as string;
}
else
{
result = _axFlashControl.CallFunction(methodInvokeMessage);
}
return result;
}
catch (COMException)
{
_log.WarnFormat("Flash Method Not Implemented: '{0}'", methodInvokeMessage);
return "<undefined/>";
}
catch (Exception e)
{
if (_axFlashControl == null || !_axFlashControl.IsHandleCreated || _axFlashControl.IsDisposed)
{
_log.Warn("Control was disposed or otherwise killed before call");
return "<undefined/>";
}
_log.ErrorFormat("Unhandled exception: {0}", e);
throw;
}
}
public void SetReturnValue(string returnXml)
{
if (_axFlashControl != null)
{
_axFlashControl.SetReturnValue(returnXml);
}
}
}
}