Commit | Line | Data |
---|---|---|
2c990ba6 KO |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: webkit.mm | |
3 | // Purpose: wxWebKitCtrl - embeddable web kit control | |
4 | // Author: Jethro Grassie / Kevin Ollivier | |
5 | // Modified by: | |
6 | // Created: 2004-4-16 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Jethro Grassie / Kevin Ollivier | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
3ca7ba74 | 12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
7eb8c6ee | 13 | #pragma implementation "webkit.h" |
2c990ba6 KO |
14 | #endif |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
216e968a | 18 | #include "wx/splitter.h" |
2c990ba6 | 19 | |
2c990ba6 KO |
20 | #ifndef WX_PRECOMP |
21 | #include "wx/wx.h" | |
22 | #endif | |
23 | ||
948f6c6e JS |
24 | #if wxUSE_WEBKIT |
25 | ||
2c990ba6 KO |
26 | #ifdef __WXCOCOA__ |
27 | #include "wx/cocoa/autorelease.h" | |
28 | #else | |
29 | #include "wx/mac/uma.h" | |
30 | #include <Carbon/Carbon.h> | |
b384c0fb | 31 | #include <WebKit/WebKit.h> |
2c990ba6 KO |
32 | #include <WebKit/HIWebView.h> |
33 | #include <WebKit/CarbonUtils.h> | |
34 | #endif | |
35 | ||
36 | #include "wx/html/webkit.h" | |
14f21d6d | 37 | #include "wx/notebook.h" |
2c990ba6 KO |
38 | |
39 | ||
40 | // ---------------------------------------------------------------------------- | |
41 | // macros | |
42 | // ---------------------------------------------------------------------------- | |
43 | ||
2c990ba6 | 44 | IMPLEMENT_DYNAMIC_CLASS(wxWebKitCtrl, wxControl) |
2c990ba6 KO |
45 | |
46 | BEGIN_EVENT_TABLE(wxWebKitCtrl, wxControl) | |
005198fa | 47 | EVT_SIZE(wxWebKitCtrl::OnSize) |
2c990ba6 KO |
48 | END_EVENT_TABLE() |
49 | ||
50 | // ---------------------------------------------------------------------------- | |
51 | // wxWebKit Events | |
52 | // ---------------------------------------------------------------------------- | |
53 | ||
54 | IMPLEMENT_DYNAMIC_CLASS( wxWebKitStateChangedEvent, wxCommandEvent ) | |
55 | ||
56 | DEFINE_EVENT_TYPE( wxEVT_WEBKIT_STATE_CHANGED ) | |
57 | ||
58 | wxWebKitStateChangedEvent::wxWebKitStateChangedEvent( wxWindow* win ) | |
59 | { | |
60 | SetEventType( wxEVT_WEBKIT_STATE_CHANGED); | |
61 | SetEventObject( win ); | |
3ca7ba74 | 62 | SetId(win->GetId()); |
2c990ba6 KO |
63 | } |
64 | ||
65 | //--------------------------------------------------------- | |
66 | // helper functions for NSString<->wxString conversion | |
67 | //--------------------------------------------------------- | |
68 | ||
69 | inline wxString wxStringWithNSString(NSString *nsstring) | |
70 | { | |
71 | #if wxUSE_UNICODE | |
72 | return wxString([nsstring UTF8String], wxConvUTF8); | |
73 | #else | |
74 | return wxString([nsstring lossyCString]); | |
75 | #endif // wxUSE_UNICODE | |
76 | } | |
77 | ||
78 | inline NSString* wxNSStringWithWxString(const wxString &wxstring) | |
79 | { | |
80 | #if wxUSE_UNICODE | |
81 | return [NSString stringWithUTF8String: wxstring.mb_str(wxConvUTF8)]; | |
82 | #else | |
83 | return [NSString stringWithCString: wxstring.c_str() length:wxstring.Len()]; | |
84 | #endif // wxUSE_UNICODE | |
85 | } | |
86 | ||
87 | @interface MyFrameLoadMonitor : NSObject | |
88 | { | |
89 | wxWindow* webKitWindow; | |
90 | } | |
91 | ||
92 | - initWithWxWindow: (wxWindow*)inWindow; | |
93 | ||
94 | @end | |
95 | ||
96 | // ---------------------------------------------------------------------------- | |
97 | // creation/destruction | |
98 | // ---------------------------------------------------------------------------- | |
99 | ||
100 | bool wxWebKitCtrl::Create(wxWindow *parent, | |
3ca7ba74 | 101 | wxWindowID winID, |
2c990ba6 KO |
102 | const wxString& strURL, |
103 | const wxPoint& pos, | |
3ca7ba74 RD |
104 | const wxSize& size, long style, |
105 | const wxValidator& validator, | |
106 | const wxString& name) | |
2c990ba6 KO |
107 | { |
108 | ||
109 | m_currentURL = strURL; | |
14f21d6d | 110 | //m_pageTitle = _("Untitled Page"); |
9548f380 | 111 | |
2c990ba6 KO |
112 | //still needed for wxCocoa?? |
113 | /* | |
114 | int width, height; | |
3ca7ba74 | 115 | wxSize sizeInstance; |
9548f380 | 116 | if (size.x == wxDefaultCoord || size.y == wxDefaultCoord) |
3ca7ba74 RD |
117 | { |
118 | m_parent->GetClientSize(&width, &height); | |
119 | sizeInstance.x = width; | |
120 | sizeInstance.y = height; | |
121 | } | |
122 | else | |
123 | { | |
124 | sizeInstance.x = size.x; | |
125 | sizeInstance.y = size.y; | |
126 | } | |
9548f380 | 127 | */ |
3ca7ba74 | 128 | // now create and attach WebKit view... |
2c990ba6 KO |
129 | #ifdef __WXCOCOA__ |
130 | wxControl::Create(parent, m_windowID, pos, sizeInstance, style , validator , name); | |
3ca7ba74 | 131 | SetSize(pos.x, pos.y, sizeInstance.x, sizeInstance.y); |
9548f380 | 132 | |
2c990ba6 KO |
133 | wxTopLevelWindowCocoa *topWin = wxDynamicCast(this, wxTopLevelWindowCocoa); |
134 | NSWindow* nsWin = topWin->GetNSWindow(); | |
3ca7ba74 | 135 | NSRect rect; |
2c990ba6 KO |
136 | rect.origin.x = pos.x; |
137 | rect.origin.y = pos.y; | |
138 | rect.size.width = sizeInstance.x; | |
139 | rect.size.height = sizeInstance.y; | |
140 | m_webView = (WebView*)[[WebView alloc] initWithFrame:rect frameName:@"webkitFrame" groupName:@"webkitGroup"]; | |
141 | SetNSView(m_webView); | |
142 | [m_cocoaNSView release]; | |
143 | ||
144 | if(m_parent) m_parent->CocoaAddChild(this); | |
145 | SetInitialFrameRect(pos,sizeInstance); | |
146 | #else | |
147 | m_macIsUserPane = false; | |
daef4689 | 148 | wxControl::Create(parent, winID, pos, size, style , validator , name); |
637013eb | 149 | m_peer = new wxMacControl(this); |
2c990ba6 | 150 | WebInitForCarbon(); |
f8405d6e | 151 | HIWebViewCreate( m_peer->GetControlRefAddr() ); |
9548f380 | 152 | |
f8405d6e | 153 | m_webView = (WebView*) HIWebViewGetWebView( m_peer->GetControlRef() ); |
14f21d6d | 154 | MacPostControlCreate(pos, size); |
9548f380 | 155 | HIViewSetVisible( m_peer->GetControlRef(), true ); |
5b8f917c | 156 | [m_webView setHidden:false]; |
b384c0fb | 157 | #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3 |
daef4689 | 158 | if ( UMAGetSystemVersion() >= 0x1030 ) |
b384c0fb SC |
159 | HIViewChangeFeatures( m_peer->GetControlRef() , kHIViewIsOpaque , 0 ) ; |
160 | #endif | |
2c990ba6 KO |
161 | #endif |
162 | ||
163 | // Register event listener interfaces | |
164 | MyFrameLoadMonitor* myFrameLoadMonitor = [[MyFrameLoadMonitor alloc] initWithWxWindow: (wxWindow*)this]; | |
3ca7ba74 | 165 | [m_webView setFrameLoadDelegate:myFrameLoadMonitor]; |
9548f380 | 166 | |
2c990ba6 KO |
167 | LoadURL(m_currentURL); |
168 | return true; | |
169 | } | |
170 | ||
171 | wxWebKitCtrl::~wxWebKitCtrl() | |
172 | { | |
173 | ||
174 | } | |
175 | ||
176 | // ---------------------------------------------------------------------------- | |
177 | // public methods | |
178 | // ---------------------------------------------------------------------------- | |
179 | ||
180 | void wxWebKitCtrl::LoadURL(const wxString &url) | |
181 | { | |
182 | if( !m_webView ) | |
183 | return; | |
9548f380 | 184 | |
7eb8c6ee | 185 | [[m_webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:wxNSStringWithWxString(url)]]]; |
2c990ba6 KO |
186 | |
187 | m_currentURL = url; | |
188 | } | |
189 | ||
190 | bool wxWebKitCtrl::CanGoBack(){ | |
191 | if ( !m_webView ) | |
192 | return false; | |
9548f380 | 193 | |
2c990ba6 KO |
194 | return [m_webView canGoBack]; |
195 | } | |
196 | ||
197 | bool wxWebKitCtrl::CanGoForward(){ | |
198 | if ( !m_webView ) | |
199 | return false; | |
9548f380 | 200 | |
2c990ba6 KO |
201 | return [m_webView canGoForward]; |
202 | } | |
203 | ||
204 | bool wxWebKitCtrl::GoBack(){ | |
205 | if ( !m_webView ) | |
206 | return false; | |
9548f380 | 207 | |
8f3b30d5 KO |
208 | bool result = [(WebView*)m_webView goBack]; |
209 | return result; | |
2c990ba6 KO |
210 | } |
211 | ||
9548f380 | 212 | bool wxWebKitCtrl::GoForward(){ |
2c990ba6 KO |
213 | if ( !m_webView ) |
214 | return false; | |
9548f380 | 215 | |
8f3b30d5 KO |
216 | bool result = [(WebView*)m_webView goForward]; |
217 | return result; | |
2c990ba6 KO |
218 | } |
219 | ||
9548f380 | 220 | void wxWebKitCtrl::Reload(){ |
2c990ba6 KO |
221 | if ( !m_webView ) |
222 | return; | |
9548f380 | 223 | |
2c990ba6 KO |
224 | [[m_webView mainFrame] reload]; |
225 | } | |
226 | ||
227 | void wxWebKitCtrl::Stop(){ | |
228 | if ( !m_webView ) | |
229 | return; | |
9548f380 | 230 | |
2c990ba6 KO |
231 | [[m_webView mainFrame] stopLoading]; |
232 | } | |
233 | ||
234 | bool wxWebKitCtrl::CanGetPageSource(){ | |
235 | if ( !m_webView ) | |
14f21d6d | 236 | return false; |
9548f380 | 237 | |
2c990ba6 KO |
238 | WebDataSource* dataSource = [[m_webView mainFrame] dataSource]; |
239 | return ( [[dataSource representation] canProvideDocumentSource] ); | |
240 | } | |
241 | ||
242 | wxString wxWebKitCtrl::GetPageSource(){ | |
9548f380 | 243 | |
2c990ba6 KO |
244 | if (CanGetPageSource()){ |
245 | WebDataSource* dataSource = [[m_webView mainFrame] dataSource]; | |
246 | return wxStringWithNSString( [[dataSource representation] documentSource] ); | |
247 | } | |
9548f380 WS |
248 | |
249 | return wxEmptyString; | |
2c990ba6 KO |
250 | } |
251 | ||
252 | void wxWebKitCtrl::SetPageSource(wxString& source, const wxString& baseUrl){ | |
253 | if ( !m_webView ) | |
254 | return; | |
9548f380 | 255 | |
2c990ba6 KO |
256 | if (CanGetPageSource()){ |
257 | [[m_webView mainFrame] loadHTMLString:(NSString*)wxNSStringWithWxString( source ) baseURL:[NSURL URLWithString:wxNSStringWithWxString( baseUrl )]]; | |
258 | } | |
259 | ||
260 | } | |
261 | ||
005198fa | 262 | void wxWebKitCtrl::OnSize(wxSizeEvent &event){ |
39104bc1 KO |
263 | // This is a nasty hack because WebKit seems to lose its position when it is embedded |
264 | // in a control that is not itself the content view for a TLW. | |
daef4689 VZ |
265 | // I put it in OnSize because these calcs are not perfect, and in fact are basically |
266 | // guesses based on reverse engineering, so it's best to give people the option of | |
267 | // overriding OnSize with their own calcs if need be. | |
268 | // I also left some test debugging print statements as a convenience if a(nother) | |
269 | // problem crops up. | |
270 | ||
271 | // Let's hope that Tiger fixes this mess... | |
272 | ||
273 | int x, y; | |
274 | x = 0; | |
275 | y = 0; | |
276 | ||
277 | wxWindow* parent = GetParent(); | |
278 | ||
279 | wxWindow* tlw = MacGetTopLevelWindow(); | |
280 | ||
281 | // This must be the case that Apple tested with, because well, in this one case | |
282 | // we don't need to do anything! It just works. ;) | |
283 | if (parent == tlw){ | |
284 | return; | |
285 | } | |
286 | ||
287 | while(parent != NULL) | |
288 | { | |
289 | if ( parent->IsKindOf( CLASSINFO( wxSplitterWindow ) ) && GetParent()->IsKindOf( CLASSINFO( wxSplitterWindow ) ) ){ | |
290 | // When parent is not a wxSplitterWindow, we can rely on it's GetPosition() to give us the correct | |
291 | // coordinates, but when the parent is a wxSplitterWindow, we need to manually calculate | |
292 | // the sash position of it and any parent wxSplitterWindows into the webkit's position. | |
293 | wxSplitterWindow* splitter; | |
294 | splitter = dynamic_cast<wxSplitterWindow*>(parent); | |
295 | if (splitter->GetSplitMode() == wxSPLIT_HORIZONTAL){ | |
296 | if (splitter->GetPosition().y > 0) | |
297 | y += splitter->GetPosition().y; | |
298 | ||
299 | if (splitter->GetSashSize() > 0) | |
300 | y += splitter->GetSashSize(); | |
301 | ||
302 | if (splitter->GetSashPosition() > 0) | |
303 | y += splitter->GetSashPosition(); | |
304 | } | |
305 | else{ | |
306 | if (splitter->GetPosition().x > 0) | |
307 | x += splitter->GetPosition().x; | |
308 | ||
309 | if (splitter->GetSashSize() > 0) | |
310 | x += splitter->GetSashSize(); | |
311 | ||
312 | if (splitter->GetSashPosition() > 0) | |
313 | x += splitter->GetSashPosition(); | |
314 | } | |
315 | } | |
316 | else{ | |
317 | if (!parent->IsTopLevel()) { | |
318 | //printf("Parent: %s\n", parent->GetClassInfo()->GetClassName()); | |
319 | int plusx = 0; | |
320 | plusx = parent->GetClientAreaOrigin().x + parent->GetPosition().x; | |
321 | if (plusx > 0){ | |
322 | x += plusx; | |
323 | //printf("Parent: %s Added x: %d\n", parent->GetClassInfo()->GetClassName(), parent->GetClientAreaOrigin().x + parent->GetPosition().x); | |
324 | } | |
325 | ||
326 | int plusy = 0; | |
327 | plusy = parent->GetClientAreaOrigin().y + parent->GetPosition().y; | |
328 | if (plusy > 0){ | |
329 | y += plusy; | |
330 | //printf("Parent: %s Added y: %d\n", parent->GetClassInfo()->GetClassName(), parent->GetClientAreaOrigin().y + parent->GetPosition().y); | |
331 | } | |
332 | else{ | |
333 | //printf("Parent: %s Origin: %d Position:%d\n", parent->GetClassInfo()->GetClassName(), parent->GetClientAreaOrigin().y, parent->GetPosition().y); | |
334 | } | |
335 | ||
336 | } | |
337 | else{ | |
338 | // | |
339 | x += parent->GetClientAreaOrigin().x; | |
340 | // calculate the title bar height (26 pixels) into the top offset. | |
341 | // This becomes important later when we must flip the y coordinate | |
342 | // to convert to Cocoa's coordinate system. | |
343 | y += parent->GetClientAreaOrigin().y += 26; | |
344 | //printf("x: %d, y:%d\n", x, y); | |
345 | } | |
346 | //we still need to add the y, because we have to convert/flip coordinates for Cocoa | |
347 | ||
348 | if ( parent->IsKindOf( CLASSINFO( wxNotebook ) ) ){ | |
349 | //Not sure why calcs are off in this one scenario... | |
350 | y -= 4; | |
351 | //printf("x: %d, y:%d\n", x, y); | |
352 | } | |
353 | ||
354 | if (parent->IsKindOf( CLASSINFO( wxPanel ) ) ){ | |
355 | // Another strange case. Adding a wxPanel to the parent heirarchy | |
356 | // causes wxWebKitCtrl's Cocoa y origin to be 4 pixels off | |
357 | // for some reason, even if the panel has a position and origin of 0. | |
358 | // This corrects that. Man, I wish I could debug Carbon/HIWebView!! ;) | |
359 | y -= 4; | |
360 | } | |
361 | } | |
362 | ||
363 | parent = parent->GetParent(); | |
364 | } | |
365 | ||
366 | // Tried using MacWindowToRootWindow both for wxWebKitCtrl and its parent, | |
367 | // but coordinates were off by a significant amount. | |
368 | // Am leaving the code here if anyone wants to play with it. | |
369 | ||
370 | //int x2, y2 = 0; | |
371 | //if (GetParent()) | |
372 | // GetParent()->MacWindowToRootWindow(&x2, &y2); | |
373 | //printf("x = %d, y = %d\n", x, y); | |
374 | //printf("x2 = %d, y2 = %d\n", x2, y2); | |
375 | //x = x2; | |
376 | //y = y2; | |
377 | ||
378 | if (tlw){ | |
379 | //flip the y coordinate to convert to Cocoa coordinates | |
380 | //printf("tlw y: %d, y: %d\n", tlw->GetSize().y, (GetSize().y + y)); | |
381 | y = tlw->GetSize().y - ((GetSize().y) + y); | |
382 | } | |
383 | ||
384 | //printf("Added to bounds x=%d, y=%d\n", x, y); | |
385 | NSRect bounds = [m_webView frame]; | |
386 | bounds.origin.x = x; | |
387 | bounds.origin.y = y; | |
388 | [m_webView setFrame:bounds]; | |
9548f380 | 389 | |
39104bc1 | 390 | //printf("Carbon position x=%d, y=%d\n", GetPosition().x, GetPosition().y); |
fe3fc02e | 391 | if (IsShown()) |
14753ffe | 392 | [(WebView*)m_webView display]; |
005198fa KO |
393 | event.Skip(); |
394 | } | |
2c990ba6 | 395 | |
14f21d6d | 396 | void wxWebKitCtrl::MacVisibilityChanged(){ |
f8405d6e | 397 | bool isHidden = !IsControlVisible( m_peer->GetControlRef()); |
fe3fc02e | 398 | if (!isHidden) |
14753ffe | 399 | [(WebView*)m_webView display]; |
9548f380 | 400 | |
14f21d6d KO |
401 | [m_webView setHidden:isHidden]; |
402 | } | |
403 | ||
2c990ba6 KO |
404 | //------------------------------------------------------------ |
405 | // Listener interfaces | |
406 | //------------------------------------------------------------ | |
407 | ||
408 | @implementation MyFrameLoadMonitor | |
409 | ||
410 | - initWithWxWindow: (wxWindow*)inWindow | |
411 | { | |
412 | [super init]; | |
3ca7ba74 | 413 | webKitWindow = inWindow; // non retained |
2c990ba6 KO |
414 | return self; |
415 | } | |
416 | ||
417 | - (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame | |
418 | { | |
419 | if (frame == [sender mainFrame]){ | |
420 | NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString]; | |
421 | wxWebKitStateChangedEvent thisEvent(webKitWindow); | |
422 | thisEvent.SetState(wxWEBKIT_STATE_NEGOTIATING); | |
423 | thisEvent.SetURL( wxStringWithNSString( url ) ); | |
424 | webKitWindow->GetEventHandler()->ProcessEvent( thisEvent ); | |
425 | } | |
426 | } | |
427 | ||
428 | - (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame | |
429 | { | |
430 | if (frame == [sender mainFrame]){ | |
fba8e95e | 431 | NSString *url = [[[[frame dataSource] request] URL] absoluteString]; |
2c990ba6 KO |
432 | wxWebKitStateChangedEvent thisEvent(webKitWindow); |
433 | thisEvent.SetState(wxWEBKIT_STATE_TRANSFERRING); | |
434 | thisEvent.SetURL( wxStringWithNSString( url ) ); | |
435 | webKitWindow->GetEventHandler()->ProcessEvent( thisEvent ); | |
436 | } | |
437 | } | |
438 | ||
439 | - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame | |
440 | { | |
441 | if (frame == [sender mainFrame]){ | |
fba8e95e | 442 | NSString *url = [[[[frame dataSource] request] URL] absoluteString]; |
2c990ba6 KO |
443 | wxWebKitStateChangedEvent thisEvent(webKitWindow); |
444 | thisEvent.SetState(wxWEBKIT_STATE_STOP); | |
445 | thisEvent.SetURL( wxStringWithNSString( url ) ); | |
446 | webKitWindow->GetEventHandler()->ProcessEvent( thisEvent ); | |
447 | } | |
448 | } | |
449 | ||
450 | - (void)webView:(WebView *)sender didFailLoadWithError:(NSError*) error forFrame:(WebFrame *)frame | |
451 | { | |
452 | if (frame == [sender mainFrame]){ | |
fba8e95e | 453 | NSString *url = [[[[frame dataSource] request] URL] absoluteString]; |
2c990ba6 KO |
454 | wxWebKitStateChangedEvent thisEvent(webKitWindow); |
455 | thisEvent.SetState(wxWEBKIT_STATE_FAILED); | |
456 | thisEvent.SetURL( wxStringWithNSString( url ) ); | |
457 | webKitWindow->GetEventHandler()->ProcessEvent( thisEvent ); | |
458 | } | |
459 | } | |
460 | ||
461 | - (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError*) error forFrame:(WebFrame *)frame | |
462 | { | |
463 | if (frame == [sender mainFrame]){ | |
464 | NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString]; | |
465 | wxWebKitStateChangedEvent thisEvent(webKitWindow); | |
466 | thisEvent.SetState(wxWEBKIT_STATE_FAILED); | |
467 | thisEvent.SetURL( wxStringWithNSString( url ) ); | |
468 | webKitWindow->GetEventHandler()->ProcessEvent( thisEvent ); | |
469 | } | |
470 | } | |
471 | ||
472 | - (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame | |
473 | { | |
474 | if (frame == [sender mainFrame]){ | |
475 | webKitWindow->SetTitle(wxStringWithNSString( title )); | |
476 | } | |
477 | } | |
3ca7ba74 | 478 | @end |
5b8f917c | 479 | |
948f6c6e | 480 | #endif //wxUSE_WEBKIT |