-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathWebKitXCtrl.h
287 lines (246 loc) · 11 KB
/
WebKitXCtrl.h
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#pragma once
#ifndef __WEBKITCTL_H__
#define __WEBKITCTL_H__
//$(SolutionDir)..\Studio\
//$(SolutionDir)\CEF\CEF1\DLLs\
#include "comutil.h"
#include "include/cef_app.h"
#include "include/cef_browser.h"
#include "include/cef_frame.h"
#include "include/cef_runnable.h"
#include "include/cef_client.h"
#include "include/cef_dom.h"
#include "WebKitHandler.h"
#include "WebKitV8Extension.h"
#define COMMAND_TIMEOUT_LOW 2000
#define COMMAND_TIMEOUT_HIGH 30000
typedef LONG (__stdcall *VISUAL_BASIC_6_FN_PTR)(LONG);
std::string Replace(std::string str, const std::string& find, const std::string& replace); \
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class CWebKitXCtrl :
public COleControl,
public CefApp
#ifdef CEF_VERSION_3
,public CefBrowserProcessHandler
#endif
{
DECLARE_DYNCREATE(CWebKitXCtrl)
public:
static bool CEF_INITIALIZED;
static CWebKitXCtrl* g_instnace;
CefRefPtr<CefBrowser> m_Browser; // The child browser window
CefRefPtr<CefV8Context> v8context; // V8 Context
WebKitV8Extension* v8handler;
HANDLE SIG_HTML_READY; // Wait Event for HTML source to be collected
HANDLE SIG_HTML_SELECTION_READY; // Wait Event for selected html
HANDLE SIG_JAVASCRIPT_READY; // Wait Event for selected html
HANDLE SIG_TIDY_HTML_READY;
CefWindowHandle m_MainHwnd; // The main frame window handle
CefWindowHandle m_BrowserHwnd; // The child browser window handle
HHOOK hook;
bool CEF_BROWSER_CREATED;
CComBSTR document_html;
VARIANT_BOOL m_Editable;
VARIANT_BOOL m_ActiveXCreated;
UINT UID_COUNTER;
bool LoadingHTML;
bool SkipSelectionEvent;
CComBSTR jsresult;
std::vector<std::string> EditorCommands;
CComBSTR SelectedNodeHTML;
CComBSTR FocusedNodePath;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
CWebKitXCtrl();
~CWebKitXCtrl();
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifdef CEF_VERSION_3
virtual void OnContextInitialized() OVERRIDE;
#endif
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Windows WM_MESSAGEs
virtual void OnDraw(CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid) OVERRIDE;
virtual BOOL IsSubclassedControl() OVERRIDE;
virtual BOOL PreCreateWindow(CREATESTRUCT& cs) OVERRIDE;
virtual void DoPropExchange(CPropExchange* pPX) OVERRIDE;
virtual void OnResetState() OVERRIDE;
LRESULT OnOcmCommand(WPARAM wParam, LPARAM lParam);
LRESULT OnResize(WPARAM wParam, LPARAM lParam);
LRESULT CancelMessage(WPARAM wParam, LPARAM lParam);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CEF API
static bool InitCEF();
static void QuitCEF();
void CreateCEFBrowser();
void DestroyCEFBrowser();
static void ExecuteGetSource(CefRefPtr<CefFrame> frame);
static void ExecuteEditable(CefRefPtr<CefFrame> frame);
static void ExecuteAddEventEx(std::string elementID, std::string eventType, IDispatch* vbObject, std::string vbObjectFunctionName, VARIANT_BOOL UseCapture);
static void ExecuteSelectNode(std::string selector, std::string html, bool MoveCaret, bool SelectContents);
static void ExecuteCode(LPCTSTR JavaScript);
static void ExecuteTidy(LPCTSTR HTML, bool CleanHTML);
template<typename F, typename P> static void ExecuteAddEvent(std::string elementID, std::string eventType, F handler, VARIANT_BOOL UseCapture);
template<typename F, typename P> void __addEventHandler(std::string elementID, std::string eventType, F handler, VARIANT_BOOL UseCapture);
typedef void (__stdcall *EVENT_HANDLER_FN)(CefRefPtr<CefDOMEvent>*);
static void __stdcall __HandleDOMEvent(CefRefPtr<CefDOMEvent>* DOMEvnet);
void HandleDOMEvent(CefRefPtr<CefDOMEvent>* e);
void AttachEditDOMEvents();
void __set_attribute(std::string selector, std::string attrName, std::string attrValue);
CComBSTR getSelectedNodeHTML(bool CleanHTML);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ActiveX API
void OpenURL(LPCTSTR URL);
BSTR GetHTML();
void SetHTML(LPCTSTR newVal);
void Edit(void);
void Preview(void);
void Reload(void);
void Repaint(void);
void addEventListener(LPCTSTR Selector, LPCTSTR Event, LONG AddressOfEventHandler, VARIANT_BOOL UseCapture);
void addEventListenerEx(LPCTSTR Selector, LPCTSTR Event, IDispatch* vbObject, LPCTSTR vbObjectFunctionName, VARIANT_BOOL UseCapture);
void SetStyle(LPCTSTR Selector, LPCTSTR StyleName, LPCTSTR StyleValue);
VARIANT_BOOL Modified(void);
void SelectElement(LPCTSTR Selector, VARIANT_BOOL MoveCaret, VARIANT_BOOL SelectContents);
OLE_HANDLE hBrowserHWND(void);
void LoadHTML(LPCTSTR HTML, LPCTSTR URL);
VARIANT_BOOL Created(void);
BSTR SelectedHTML(VARIANT_BOOL FullHTML);
BSTR ExecJavaScript(LPCTSTR Code);
BSTR ExecCommand(LONG id, VARIANT& Params);
BSTR TidyHTML(LPCTSTR HTML);
BSTR CleanHTML(LPCTSTR HTML);
VARIANT_BOOL Editable(void);
void Find(LPCTSTR RegularExpression);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ActiveX Events
//---------------------------------------------------------------------------------------------------------------------------------------------
void FireOnReady()
{
AttachEditDOMEvents();
SetTimer(eventidOnReady, 10, OnReadyTimerProc);
}
static void CALLBACK OnReadyTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime) { g_instnace->OnReadyTimer(); }
void OnReadyTimer() { KillTimer(eventidOnReady); OnReady(); }
void OnReady(void)
{
debugPrint("OnReady.\n");
if(m_Editable==VARIANT_TRUE)
{
Edit();
}
else
{
Preview();
}
FireEvent(eventidOnReady, EVENT_PARAM(VTS_NONE));
}
//---------------------------------------------------------------------------------------------------------------------------------------------
void FireOnCreate() { SetTimer(eventidOnCreate, 10, OnCreateTimerProc); }
static void CALLBACK OnCreateTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime) { g_instnace->OnCreateTimer(); }
void OnCreateTimer() { KillTimer(eventidOnCreate); OnCreate(); }
void OnCreate(void)
{
debugPrint("OnCreate.\n");
m_ActiveXCreated = VARIANT_TRUE;
FireEvent(eventidOnCreate, EVENT_PARAM(VTS_NONE));
}
//---------------------------------------------------------------------------------------------------------------------------------------------
static const UINT_PTR ON_MOUSE_DOWN_TIMER = 1000;
void FireOnMouseDown() { KillTimer(ON_MOUSE_DOWN_TIMER); SetTimer(ON_MOUSE_DOWN_TIMER, 10, OnMouseDownTimerProc); }
static void CALLBACK OnMouseDownTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime) { g_instnace->OnMouseDownTimer(); }
void OnMouseDownTimer() { KillTimer(ON_MOUSE_DOWN_TIMER); OnMouseDown(); }
void OnMouseDown(void)
{
debugPrint("OnMouseDown\n");
FireMouseDown(1, 0, 0,0);
}
//---------------------------------------------------------------------------------------------------------------------------------------------
static const UINT_PTR ON_FOCUS_TIMER = 1001;
void FireOnFocus() { KillTimer(ON_FOCUS_TIMER); SetTimer(ON_FOCUS_TIMER, 100, OnFocusTimerProc); }
static void CALLBACK OnFocusTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime) { g_instnace->OnFocusTimer(); }
void OnFocusTimer() { KillTimer(ON_FOCUS_TIMER); OnFocus(); }
void OnFocus(void)
{
if(FocusedNodePath.Length()==0) return;
debugPrint("OnFocus\n");
FireEvent(eventidOnFocus, EVENT_PARAM(VTS_BSTR VTS_BSTR), FocusedNodePath, SelectedNodeHTML);
}
//---------------------------------------------------------------------------------------------------------------------------------------------
static const UINT_PTR ON_MODIFIED_TIMER = 1002;
void FireOnModified() { KillTimer(ON_MODIFIED_TIMER); SetTimer(ON_MODIFIED_TIMER, 1000, OnModifiedTimerProc); }
static void CALLBACK OnModifiedTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime) { g_instnace->OnModifiedTimer(); }
void OnModifiedTimer() { KillTimer(ON_MODIFIED_TIMER); OnModified(); }
void OnModified(void)
{
debugPrint("OnModified\n");
SetModifiedFlag(TRUE);
FireEvent(eventidOnModified, EVENT_PARAM(VTS_NONE));
}
//---------------------------------------------------------------------------------------------------------------------------------------------
static const UINT_PTR ON_SELECTION_CHANGED_TIMER = 1003;
void FireOnSelectionChanged() { KillTimer(ON_SELECTION_CHANGED_TIMER); SetTimer(ON_SELECTION_CHANGED_TIMER, 1000, OnSelectionChangedTimerProc); }
static void CALLBACK OnSelectionChangedTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime) { g_instnace->OnSelectionChangedTimer(); }
void OnSelectionChangedTimer() { KillTimer(ON_SELECTION_CHANGED_TIMER); OnSelectionChanged(); }
void OnSelectionChanged(void)
{
FireEvent(eventidOnSelectionChanged, EVENT_PARAM(VTS_BSTR VTS_BSTR), document_html, SelectedNodeHTML);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TODO: Handle mouse move to prevent CSS3 hover styles during editing
static LRESULT CALLBACK MouseHook(int nCode, WPARAM wp, LPARAM lp)
{
/*
MOUSEHOOKSTRUCT *pmh = (MOUSEHOOKSTRUCT *) lp;
if(nCode >= 0)
{
if(wp == WM_RBUTTONDOWN || wp == WM_RBUTTONUP || wp==WM_MOUSEMOVE)
{
return 1;
}
}
*/
return CallNextHookEx(NULL, nCode, wp, lp);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
IMPLEMENT_REFCOUNTING(CWebKitXCtrl);
IMPLEMENT_LOCKING(CWebKitXCtrl);
DECLARE_OLECREATE_EX(CWebKitXCtrl) // Class factory and guid
DECLARE_OLETYPELIB(CWebKitXCtrl) // GetTypeInfo
DECLARE_PROPPAGEIDS(CWebKitXCtrl) // Property page IDs
DECLARE_OLECTLTYPE(CWebKitXCtrl) // Type name and misc status
// Message maps
DECLARE_MESSAGE_MAP()
// Dispatch maps
DECLARE_DISPATCH_MAP()
// Event maps
DECLARE_EVENT_MAP()
enum
{
dispidFind = 23L,
dispidCleanHTML = 22L,
dispidEditable = 21L,
dispidTidyHTML = 20L,
dispidExecCommand = 19L,
dispidExecJavaScript = 18L,
dispidaddEventListener = 7L,
dispidaddEventListenerEx = 8L,
dispidCreated = 16L,
dispidEdit = 3L,
dispidhBrowserHWND = 14L,
dispidHTML = 2L,
dispidLoadHTML = 15L,
dispidModified = 5L,
dispidOpenURL = 1L,
dispidPreview = 4L,
dispidReload = 6L,
dispidRepaint = 10L,
dispidSelectedHTML = 17L,
dispidSelectElement = 13L,
dispidSetStyle = 11L,
eventidOnSelectionChanged = 5L,
eventidOnCreate = 2L,
eventidOnFocus = 3L,
eventidOnModified = 4L,
eventidOnReady = 1L
};
};
#endif