-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathEdgeWebBrowser.h
More file actions
378 lines (322 loc) · 10.8 KB
/
EdgeWebBrowser.h
File metadata and controls
378 lines (322 loc) · 10.8 KB
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/* Copyright (C) 2014-2026 Stefan-Mihai MOGA
This file is part of IntelliLink application developed by Stefan-Mihai MOGA.
IntelliLink is an alternative Windows version to Online Link Managers!
IntelliLink is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Open
Source Initiative, either version 3 of the License, or any later version.
IntelliLink is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
IntelliLink. If not, see <http://www.opensource.org/licenses/gpl-3.0.html>*/
#pragma once
#include <EventToken.h>
#include <functional>
#include <map>
struct ICoreWebView2Environment;
struct ICoreWebView2Controller;
struct CWebBrowserImpl;
class CView;
/**
* @class CWebBrowser
* @brief MFC window class that hosts a Microsoft Edge WebView2 browser control.
*
* Provides navigation, script execution, printing, and event/callback support for embedding
* a modern web browser in an MFC application.
*/
class CWebBrowser : public CWnd
{
public:
/**
* @enum CallbackType
* @brief Types of asynchronous events for which callbacks can be registered.
*/
enum class CallbackType
{
CreationCompleted, ///< WebView2 creation finished
NavigationCompleted, ///< Navigation to a URL finished
TitleChanged, ///< Document title changed
};
/// Callback function type for asynchronous events.
using CallbackFunc = std::function<void()>;
/// Callback function type for text selection results.
using TextSelectionFunc = std::function<void(CString const&)>;
public:
/**
* @brief Constructor. Initializes the browser control.
*/
CWebBrowser();
/**
* @brief Destructor. Cleans up resources.
*/
virtual ~CWebBrowser();
/**
* @brief Creates the browser window and initializes WebView2 synchronously.
* @param lpszClassName Window class name.
* @param lpszWindowName Window name.
* @param dwStyle Window style.
* @param rect Initial window rectangle.
* @param pParentWnd Parent window pointer.
* @param nID Control ID.
* @param pContext Optional creation context.
* @return TRUE if successful, FALSE otherwise.
*/
virtual BOOL Create(
LPCTSTR lpszClassName,
LPCTSTR lpszWindowName,
DWORD dwStyle,
const RECT& rect,
CWnd* pParentWnd,
UINT nID,
CCreateContext* = NULL) override;
/**
* @brief Asynchronously creates the browser window and initializes WebView2.
* @param dwStyle Window style.
* @param rect Initial window rectangle.
* @param pParentWnd Parent window pointer.
* @param nID Control ID.
* @param onCreated Callback invoked when creation is complete.
* @return TRUE if successful, FALSE otherwise.
*/
BOOL CreateAsync(
DWORD dwStyle,
const RECT& rect,
CWnd* pParentWnd,
UINT nID,
CallbackFunc onCreated);
/**
* @brief Registers a callback for a specific browser event.
* @param type Callback type.
* @param callback Function to call on event.
*/
void RegisterCallback(CallbackType const type, CallbackFunc callback);
/**
* @brief Gets the bounds of the browser control.
* @return RECT structure with bounds.
*/
RECT GetBounds();
/**
* @brief Sets the bounds (width and height) of the browser control.
* @param width New width.
* @param height New height.
*/
void SetBounds(LONG const width, LONG const height) { Resize(width, height); }
/**
* @brief Resizes the browser control.
* @param width New width.
* @param height New height.
*/
void Resize(LONG const width, LONG const height);
/**
* @brief Gets the current URL loaded in the browser.
* @return Current URL as CString.
*/
CString GetLocationURL();
/**
* @brief Navigates to a URL and invokes a callback upon completion.
* @param url URL to navigate to.
* @param onComplete Callback to invoke when navigation completes.
*/
void Navigate(CString const& url, CallbackFunc onComplete);
/**
* @brief Navigates to a URL using HTTP POST with custom content and headers.
* @param url Target URL.
* @param content POST data.
* @param headers HTTP headers.
* @param onComplete Callback to invoke when navigation completes.
*/
void NavigatePost(CString const& url, CString const& content, CString const& headers, CallbackFunc onComplete = nullptr);
/**
* @brief Navigates back in the browser history if possible.
*/
void GoBack();
/**
* @brief Navigates forward in the browser history if possible.
*/
void GoForward();
/**
* @brief Reloads the current page.
*/
void Reload();
/**
* @brief Stops the current navigation or page load.
*/
void Stop();
/**
* @brief Checks if a navigation is currently in progress.
* @return true if navigating, false otherwise.
*/
bool IsNavigating() const { return m_isNavigating; }
/**
* @brief Disables JavaScript popups and default script dialogs.
*/
void DisablePopups();
/**
* @brief Prints the current document using the browser's print dialog.
*/
void PrintDocument();
/**
* @brief Gets the current document title.
* @return Document title as CString.
*/
CString GetTitle() const { return m_strTitle; }
/**
* @brief Sets the parent MFC view for the browser control.
* @param pViewParent Pointer to parent view.
*/
void SetParentView(CView* pViewParent) { m_pViewParent = pViewParent; }
/**
* @brief Checks if the WebView2 control has been created.
* @return true if created, false otherwise.
*/
bool IsWebViewCreated() const;
/**
* @brief Gets the currently selected text in the browser and invokes a callback with the result.
* @param callback Function to receive the selected text.
*/
void GetSelectedText(TextSelectionFunc callback);
/**
* @brief Executes JavaScript code in the context of the current page.
* @param code JavaScript code to execute.
*/
void ExecuteScript(CString const& code);
/**
* @brief Shows the print UI for the current document.
* @param systemDialog If true, shows the system print dialog; otherwise, shows the browser print dialog.
*/
void ShowPrintUI(bool const systemDialog);
/**
* @brief Prints the current page to a PDF file.
* @param landscape If true, prints in landscape orientation.
* @param callback Callback to invoke with the result and file path.
*/
void PrintToPDF(bool const landscape, std::function<void(bool, CString)> callback);
/**
* @brief Checks if a PrintToPDF operation is in progress.
* @return true if printing to PDF, false otherwise.
*/
bool IsPrintToPdfInProgress() const { return m_printToPdfInProgress; }
protected:
DECLARE_DYNCREATE(CWebBrowser)
DECLARE_MESSAGE_MAP()
private:
CWebBrowserImpl* m_pImpl; ///< Internal implementation details (WebView2 COM pointers)
std::map<CallbackType, CallbackFunc> m_callbacks; ///< Registered event callbacks
EventRegistrationToken m_navigationCompletedToken = {};
EventRegistrationToken m_navigationStartingToken = {};
EventRegistrationToken m_documentTitleChangedToken = {};
bool m_isNavigating = false; ///< True if navigation is in progress
CView* m_pViewParent = nullptr; ///< Parent MFC view
CString m_strTitle; ///< Current document title
bool m_printToPdfInProgress = false; ///< True if PrintToPDF is running
private:
/**
* @brief Runs a callback asynchronously on the UI thread.
* @param callback Callback function to run.
*/
void RunAsync(CallbackFunc callback);
/**
* @brief Closes and releases all WebView and related resources.
*/
void CloseWebView();
/**
* @brief Registers event handlers for navigation and title changes.
*/
void RegisterEventHandlers();
/**
* @brief Resizes the WebView to fit the client area of the window.
*/
void ResizeToClientArea();
/**
* @brief Navigates the browser to the specified URL.
* @param url URL to navigate to.
*/
void NavigateTo(CString url);
/**
* @brief Normalizes a URL, adding protocol if missing.
* @param url Input URL.
* @return Normalized URL.
*/
CString NormalizeUrl(CString url);
/**
* @brief Gets the install path for WebView2 or Edge.
* @return Install path as CString.
*/
static CString GetInstallPath();
/**
* @brief Gets the install path from the registry.
* @param searchWebView If true, searches for WebView2; otherwise, searches for Edge.
* @return Install path as CString.
*/
static CString GetInstallPathFromRegistry(bool const searchWebView = true);
/**
* @brief Gets the install path from disk.
* @param searchWebView If true, searches for WebView2; otherwise, searches for Edge.
* @return Install path as CString.
*/
static CString GetInstallPathFromDisk(bool const searchWebView = true);
/**
* @brief Gets the user data folder for WebView2.
* @return User data folder as CString.
*/
static CString GetUserDataFolder();
/**
* @brief Initializes the WebView2 environment and starts browser creation.
*/
void InitializeWebView();
/**
* @brief Callback for when the WebView2 environment is created.
* @param result HRESULT result.
* @param environment Pointer to the created environment.
* @return HRESULT
*/
HRESULT OnCreateEnvironmentCompleted(HRESULT result, ICoreWebView2Environment* environment);
/**
* @brief Callback for when the WebView2 controller is created.
* @param result HRESULT result.
* @param controller Pointer to the created controller.
* @return HRESULT
*/
HRESULT OnCreateWebViewControllerCompleted(HRESULT result, ICoreWebView2Controller* controller);
/**
* @brief Registers and returns the window class name for the browser host.
* @return Window class name.
*/
static PCTSTR GetWindowClass();
/**
* @brief Static window procedure for the browser host window.
* @param hWnd Window handle.
* @param message Message ID.
* @param wParam WPARAM.
* @param lParam LPARAM.
* @return LRESULT
*/
static LRESULT CALLBACK WndProcStatic(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
/**
* @brief Handles window messages for the browser host window.
* @param hWnd Window handle.
* @param message Message ID.
* @param wParam WPARAM.
* @param lParam LPARAM.
* @param result Pointer to LRESULT for output.
* @return true if message was handled, false otherwise.
*/
bool HandleWindowMessage(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, LRESULT* result);
/**
* @brief Creates the host window for the browser control.
* @param lpszClassName Window class name.
* @param lpszWindowName Window name.
* @param dwStyle Window style.
* @param rect Initial window rectangle.
* @param pParentWnd Parent window pointer.
* @param nID Control ID.
* @return TRUE if successful, FALSE otherwise.
*/
BOOL CreateHostWindow(
LPCTSTR lpszClassName,
LPCTSTR lpszWindowName,
DWORD dwStyle,
const RECT& rect,
CWnd* pParentWnd,
UINT nID);
};