comparison win/edge.cpp @ 2000:77e43d71eaa7

Changes to allow mixing C and C++ when including dw.h. Win: Fixes to the last commit for embedding chromium based Edge.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Thu, 31 Oct 2019 08:26:58 +0000
parents 4e808c4cadfb
children f7acca46f795
comparison
equal deleted inserted replaced
1999:4e808c4cadfb 2000:77e43d71eaa7
9 * Currently only buildable with Visual Studio since it requires the EDGE 9 * Currently only buildable with Visual Studio since it requires the EDGE
10 * SDK which is currently distributed as a nuget package. 10 * SDK which is currently distributed as a nuget package.
11 */ 11 */
12 #include "dw.h" 12 #include "dw.h"
13 #include "webview2.h" 13 #include "webview2.h"
14 #include <wrl.h>
15
16 using namespace Microsoft::WRL;
14 17
15 #define _DW_HTML_DATA_NAME (char *)"_dw_edge" 18 #define _DW_HTML_DATA_NAME (char *)"_dw_edge"
16 19
17 BOOL _dw_edge_detect(VOID) 20 extern "C" {
18 { 21
19 return TRUE; 22 BOOL _dw_edge_detect(VOID)
20 } 23 {
21 24 return TRUE;
22 /******************************* dw_html_action() ************************** 25 }
23 * Implements the functionality of a "Back". "Forward", "Home", "Search", 26
24 * "Refresh", or "Stop" button. 27 /******************************* dw_html_action() **************************
25 * 28 * Implements the functionality of a "Back". "Forward", "Home", "Search",
26 * hwnd = Handle to the window hosting the browser object. 29 * "Refresh", or "Stop" button.
27 * action = One of the following: 30 *
28 * 0 = Move back to the previously viewed web page. 31 * hwnd = Handle to the window hosting the browser object.
29 * 1 = Move forward to the previously viewed web page. 32 * action = One of the following:
30 * 2 = Move to the home page. 33 * 0 = Move back to the previously viewed web page.
31 * 3 = Search. 34 * 1 = Move forward to the previously viewed web page.
32 * 4 = Refresh the page. 35 * 2 = Move to the home page.
33 * 5 = Stop the currently loading page. 36 * 3 = Search.
34 * 37 * 4 = Refresh the page.
35 * NOTE: EmbedBrowserObject() must have been successfully called once with the 38 * 5 = Stop the currently loading page.
36 * specified window, prior to calling this function. You need call 39 */
37 * EmbedBrowserObject() once only, and then you can make multiple calls to 40
38 * this function to display numerous pages in the specified window. 41 void _dw_edge_action(HWND hwnd, int action)
39 */ 42 {
40 43 IWebView2WebView* webview;
41 void _dw_edge_action(HWND hwnd, int action) 44
42 { 45 // Retrieve the browser object's pointer we stored in our window's GWL_USERDATA when
43 IWebView2WebView* webview; 46 // we initially attached the browser object to this window.
44 47 webview = (IWebView2WebView*)dw_window_get_data(hwnd, _DW_HTML_DATA_NAME);
45 // Retrieve the browser object's pointer we stored in our window's GWL_USERDATA when 48
46 // we initially attached the browser object to this window. 49 // We want to get the base address (ie, a pointer) to the IWebView2WebView object embedded within the browser
47 50 // object, so we can call some of the functions in the former's table.
48 webview = *((IWebView2WebView**)dw_window_get_data(hwnd, _DW_HTML_DATA_NAME)); 51 if (webview)
49 // We want to get the base address (ie, a pointer) to the IWebBrowser2 object embedded within the browser 52 {
50 // object, so we can call some of the functions in the former's table. 53 // Call the desired function
51 if (webview) 54 switch (action)
52 { 55 {
53 // Ok, now the pointer to our IWebBrowser2 object is in 'webBrowser2', and so its VTable is
54 // webBrowser2->lpVtbl.
55
56 // Call the desired function
57 switch (action)
58 {
59 case DW_HTML_GOBACK: 56 case DW_HTML_GOBACK:
60 { 57 {
61 // Call the IWebBrowser2 object's GoBack function. 58 // Call the IWebView2WebView object's GoBack function.
62 webview->GoBack(); 59 webview->GoBack();
63 break; 60 break;
64 } 61 }
65 62
66 case DW_HTML_GOFORWARD: 63 case DW_HTML_GOFORWARD:
67 { 64 {
68 // Call the IWebBrowser2 object's GoForward function. 65 // Call the IWebView2WebView object's GoForward function.
69 webview->GoForward(); 66 webview->GoForward();
70 break; 67 break;
71 } 68 }
72 69
73 case DW_HTML_GOHOME: 70 case DW_HTML_GOHOME:
74 { 71 {
75 // Call the IWebBrowser2 object's GoHome function. 72 // Call the IWebView2WebView object's GoHome function.
76 //webview->GoHome(); 73 //webview->GoHome();
77 break; 74 break;
78 } 75 }
79 76
80 case DW_HTML_SEARCH: 77 case DW_HTML_SEARCH:
81 { 78 {
82 // Call the IWebBrowser2 object's GoSearch function. 79 // Call the IWebView2WebView object's GoSearch function.
83 //webview->GoSearch(); 80 //webview->GoSearch();
84 break; 81 break;
85 } 82 }
86 83
87 case DW_HTML_RELOAD: 84 case DW_HTML_RELOAD:
88 { 85 {
89 // Call the IWebBrowser2 object's Refresh function. 86 // Call the IWebView2WebView object's Refresh function.
90 webview->Reload(); 87 webview->Reload();
91 } 88 }
92 89
93 case DW_HTML_STOP: 90 case DW_HTML_STOP:
94 { 91 {
95 // Call the IWebBrowser2 object's Stop function. 92 // Call the IWebView2WebView object's Stop function.
96 //webview->Stop(); 93 //webview->Stop();
97 } 94 }
98 } 95 }
99 } 96 }
100 } 97 }
101 98
102 /******************************* dw_html_raw() **************************** 99 /******************************* dw_html_raw() ****************************
103 * Takes a string containing some HTML BODY, and displays it in the specified 100 * Takes a string containing some HTML BODY, and displays it in the specified
104 * window. For example, perhaps you want to display the HTML text of... 101 * window. For example, perhaps you want to display the HTML text of...
105 * 102 *
106 * <P>This is a picture.<P><IMG src="mypic.jpg"> 103 * <P>This is a picture.<P><IMG src="mypic.jpg">
107 * 104 *
108 * hwnd = Handle to the window hosting the browser object. 105 * hwnd = Handle to the window hosting the browser object.
109 * string = Pointer to nul-terminated string containing the HTML BODY. 106 * string = Pointer to nul-terminated string containing the HTML BODY.
110 * (NOTE: No <BODY></BODY> tags are required in the string). 107 * (NOTE: No <BODY></BODY> tags are required in the string).
111 * 108 *
112 * RETURNS: 0 if success, or non-zero if an error. 109 * RETURNS: 0 if success, or non-zero if an error.
113 * 110 */
114 * NOTE: EmbedBrowserObject() must have been successfully called once with the 111
115 * specified window, prior to calling this function. You need call 112 int _dw_edge_raw(HWND hwnd, LPCWSTR string)
116 * EmbedBrowserObject() once only, and then you can make multiple calls to 113 {
117 * this function to display numerous pages in the specified window. 114 IWebView2WebView* webview;
118 */ 115
119 116 // Retrieve the browser object's pointer we stored in our window's GWL_USERDATA when
120 int _dw_edge_raw(HWND hwnd, LPCWSTR string) 117 // we initially attached the browser object to this window.
121 { 118 webview = (IWebView2WebView*)dw_window_get_data(hwnd, _DW_HTML_DATA_NAME);
122 IWebView2WebView* webview; 119
123 120 if (webview)
124 // Retrieve the browser object's pointer we stored in our window's GWL_USERDATA when 121 {
125 // we initially attached the browser object to this window. 122 return DW_ERROR_NONE;
126 webview = *((IWebView2WebView**)dw_window_get_data(hwnd, _DW_HTML_DATA_NAME)); 123 }
127 124 return DW_ERROR_UNKNOWN;
128 if (webview) 125 }
129 { 126
130 return DW_ERROR_NONE; 127 /******************************* dw_html_url() ****************************
131 } 128 * Displays a URL, or HTML file on disk.
132 return DW_ERROR_UNKNOWN; 129 *
133 } 130 * hwnd = Handle to the window hosting the browser object.
134 131 * webPageName = Pointer to nul-terminated name of the URL/file.
135 /******************************* dw_html_url() **************************** 132 *
136 * Displays a URL, or HTML file on disk. 133 * RETURNS: 0 if success, or non-zero if an error.
137 * 134 */
138 * hwnd = Handle to the window hosting the browser object. 135
139 * webPageName = Pointer to nul-terminated name of the URL/file. 136 int _dw_edge_url(HWND hwnd, LPCWSTR url)
140 * 137 {
141 * RETURNS: 0 if success, or non-zero if an error. 138 IWebView2WebView* webview;
142 * 139
143 * NOTE: EmbedBrowserObject() must have been successfully called once with the 140 // Retrieve the browser object's pointer we stored in our window's GWL_USERDATA when
144 * specified window, prior to calling this function. You need call 141 // we initially attached the browser object to this window.
145 * EmbedBrowserObject() once only, and then you can make multiple calls to 142 webview = (IWebView2WebView*)dw_window_get_data(hwnd, _DW_HTML_DATA_NAME);
146 * this function to display numerous pages in the specified window. 143
147 */ 144 if (webview)
148 145 {
149 int _dw_edge_url(HWND hwnd, LPCWSTR url) 146 webview->Navigate(url);
150 { 147 return DW_ERROR_NONE;
151 IWebView2WebView * webview; 148 }
152 149 return DW_ERROR_UNKNOWN;
153 // Retrieve the browser object's pointer we stored in our window's GWL_USERDATA when 150 }
154 // we initially attached the browser object to this window. 151
155 webview = *((IWebView2WebView**)dw_window_get_data(hwnd, _DW_HTML_DATA_NAME)); 152 /************************** browserWindowProc() *************************
156 153 * Our message handler for our window to host the browser.
157 if (webview) 154 */
158 { 155
159 webview->Navigate(url); 156 LRESULT CALLBACK _edgeWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
160 return DW_ERROR_NONE; 157 {
161 } 158 switch (uMsg)
162 return DW_ERROR_UNKNOWN; 159 {
163 }
164
165 /************************** browserWindowProc() *************************
166 * Our message handler for our window to host the browser.
167 */
168
169 LRESULT CALLBACK _edgeWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
170 {
171 switch (uMsg)
172 {
173 case WM_SIZE: 160 case WM_SIZE:
174 { 161 {
175 // Resize the browser object to fit the window 162 // Resize the browser object to fit the window
176 RECT bounds; 163 RECT bounds;
177 IWebView2WebView* webview; 164 IWebView2WebView* webview;
178 165
179 // Retrieve the browser object's pointer we stored in our window's GWL_USERDATA when 166 // Retrieve the browser object's pointer we stored in our window's GWL_USERDATA when
180 // we initially attached the browser object to this window. 167 // we initially attached the browser object to this window.
181 webview = *((IWebView2WebView**)dw_window_get_data(hWnd, _DW_HTML_DATA_NAME)); 168 webview = (IWebView2WebView*)dw_window_get_data(hWnd, _DW_HTML_DATA_NAME);
182 GetClientRect(hWnd, &bounds); 169 GetClientRect(hWnd, &bounds);
183 // Resize WebView to fit the bounds of the parent window 170 // Resize WebView to fit the bounds of the parent window
184 if(webview) 171 if (webview)
185 webview->put_Bounds(bounds); 172 webview->put_Bounds(bounds);
186 return(0); 173 return(0);
187 } 174 }
188 175
189 case WM_CREATE: 176 case WM_CREATE:
227 // Detach the browser object from this window, and free resources. 214 // Detach the browser object from this window, and free resources.
228 IWebView2WebView* webview; 215 IWebView2WebView* webview;
229 216
230 // Retrieve the browser object's pointer we stored in our window's GWL_USERDATA when 217 // Retrieve the browser object's pointer we stored in our window's GWL_USERDATA when
231 // we initially attached the browser object to this window. 218 // we initially attached the browser object to this window.
232 webview = *((IWebView2WebView**)dw_window_get_data(hWnd, _DW_HTML_DATA_NAME)); 219 webview = (IWebView2WebView*)dw_window_get_data(hWnd, _DW_HTML_DATA_NAME);
233 if (webview) 220 if (webview)
234 { 221 {
235 dw_window_set_data(hWnd, _DW_HTML_DATA_NAME, NULL); 222 dw_window_set_data(hWnd, _DW_HTML_DATA_NAME, NULL);
236 webview->Close(); 223 webview->Close();
237 224
238 } 225 }
239 return(TRUE); 226 return(TRUE);
240 } 227 }
241 } 228 }
242 229
243 return(DefWindowProc(hWnd, uMsg, wParam, lParam)); 230 return(DefWindowProc(hWnd, uMsg, wParam, lParam));
231 }
244 } 232 }
245