1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxWebKitCtrl - embeddable web kit control
4 // Author: Jethro Grassie / Kevin Ollivier
8 // Copyright: (c) Jethro Grassie / Kevin Ollivier
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "webkit.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18 #include "wx/splitter.h"
27 #include "wx/cocoa/autorelease.h"
29 #include "wx/mac/uma.h"
30 #include <Carbon/Carbon.h>
31 #include <WebKit/HIWebView.h>
32 #include <WebKit/CarbonUtils.h>
33 #include <WebKit/WebKit.h>
36 #include "wx/html/webkit.h"
37 #include "wx/notebook.h"
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 #if !USE_SHARED_LIBRARY
45 IMPLEMENT_DYNAMIC_CLASS(wxWebKitCtrl, wxControl)
48 BEGIN_EVENT_TABLE(wxWebKitCtrl, wxControl)
49 EVT_SIZE(wxWebKitCtrl::OnSize)
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 IMPLEMENT_DYNAMIC_CLASS( wxWebKitStateChangedEvent, wxCommandEvent )
58 DEFINE_EVENT_TYPE( wxEVT_WEBKIT_STATE_CHANGED )
60 wxWebKitStateChangedEvent::wxWebKitStateChangedEvent( wxWindow* win )
62 SetEventType( wxEVT_WEBKIT_STATE_CHANGED);
63 SetEventObject( win );
67 //---------------------------------------------------------
68 // helper functions for NSString<->wxString conversion
69 //---------------------------------------------------------
71 inline wxString wxStringWithNSString(NSString *nsstring)
74 return wxString([nsstring UTF8String], wxConvUTF8);
76 return wxString([nsstring lossyCString]);
77 #endif // wxUSE_UNICODE
80 inline NSString* wxNSStringWithWxString(const wxString &wxstring)
83 return [NSString stringWithUTF8String: wxstring.mb_str(wxConvUTF8)];
85 return [NSString stringWithCString: wxstring.c_str() length:wxstring.Len()];
86 #endif // wxUSE_UNICODE
89 @interface MyFrameLoadMonitor : NSObject
91 wxWindow* webKitWindow;
94 - initWithWxWindow: (wxWindow*)inWindow;
98 // ----------------------------------------------------------------------------
99 // creation/destruction
100 // ----------------------------------------------------------------------------
102 bool wxWebKitCtrl::Create(wxWindow *parent,
104 const wxString& strURL,
106 const wxSize& size, long style,
107 const wxValidator& validator,
108 const wxString& name)
111 m_currentURL = strURL;
112 //m_pageTitle = _("Untitled Page");
114 //still needed for wxCocoa??
118 if (size.x == wxDefaultCoord || size.y == wxDefaultCoord)
120 m_parent->GetClientSize(&width, &height);
121 sizeInstance.x = width;
122 sizeInstance.y = height;
126 sizeInstance.x = size.x;
127 sizeInstance.y = size.y;
130 // now create and attach WebKit view...
132 wxControl::Create(parent, m_windowID, pos, sizeInstance, style , validator , name);
133 SetSize(pos.x, pos.y, sizeInstance.x, sizeInstance.y);
135 wxTopLevelWindowCocoa *topWin = wxDynamicCast(this, wxTopLevelWindowCocoa);
136 NSWindow* nsWin = topWin->GetNSWindow();
138 rect.origin.x = pos.x;
139 rect.origin.y = pos.y;
140 rect.size.width = sizeInstance.x;
141 rect.size.height = sizeInstance.y;
142 m_webView = (WebView*)[[WebView alloc] initWithFrame:rect frameName:@"webkitFrame" groupName:@"webkitGroup"];
143 SetNSView(m_webView);
144 [m_cocoaNSView release];
146 if(m_parent) m_parent->CocoaAddChild(this);
147 SetInitialFrameRect(pos,sizeInstance);
149 m_macIsUserPane = false;
150 wxControl::Create(parent, winID, pos, size, style , validator , name);
151 m_peer = new wxMacControl(this);
153 HIWebViewCreate( m_peer->GetControlRefAddr() );
155 m_webView = (WebView*) HIWebViewGetWebView( m_peer->GetControlRef() );
156 MacPostControlCreate(pos, size);
157 HIViewSetVisible( m_peer->GetControlRef(), true );
158 [m_webView setHidden:false];
161 // Register event listener interfaces
162 MyFrameLoadMonitor* myFrameLoadMonitor = [[MyFrameLoadMonitor alloc] initWithWxWindow: (wxWindow*)this];
163 [m_webView setFrameLoadDelegate:myFrameLoadMonitor];
165 LoadURL(m_currentURL);
169 wxWebKitCtrl::~wxWebKitCtrl()
174 // ----------------------------------------------------------------------------
176 // ----------------------------------------------------------------------------
178 void wxWebKitCtrl::LoadURL(const wxString &url)
183 [[m_webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:wxNSStringWithWxString(url)]]];
188 bool wxWebKitCtrl::CanGoBack(){
192 return [m_webView canGoBack];
195 bool wxWebKitCtrl::CanGoForward(){
199 return [m_webView canGoForward];
202 bool wxWebKitCtrl::GoBack(){
206 bool result = [(WebView*)m_webView goBack];
210 bool wxWebKitCtrl::GoForward(){
214 bool result = [(WebView*)m_webView goForward];
218 void wxWebKitCtrl::Reload(){
222 [[m_webView mainFrame] reload];
225 void wxWebKitCtrl::Stop(){
229 [[m_webView mainFrame] stopLoading];
232 bool wxWebKitCtrl::CanGetPageSource(){
236 WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
237 return ( [[dataSource representation] canProvideDocumentSource] );
240 wxString wxWebKitCtrl::GetPageSource(){
242 if (CanGetPageSource()){
243 WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
244 return wxStringWithNSString( [[dataSource representation] documentSource] );
247 return wxEmptyString;
250 void wxWebKitCtrl::SetPageSource(wxString& source, const wxString& baseUrl){
254 if (CanGetPageSource()){
255 [[m_webView mainFrame] loadHTMLString:(NSString*)wxNSStringWithWxString( source ) baseURL:[NSURL URLWithString:wxNSStringWithWxString( baseUrl )]];
260 void wxWebKitCtrl::OnSize(wxSizeEvent &event){
261 // This is a nasty hack because WebKit seems to lose its position when it is embedded
262 // in a control that is not itself the content view for a TLW.
263 // I put it in OnSize because these calcs are not perfect, and in fact are basically
264 // guesses based on reverse engineering, so it's best to give people the option of
265 // overriding OnSize with their own calcs if need be.
266 // I also left some test debugging print statements as a convenience if a(nother)
269 // Let's hope that Tiger fixes this mess...
275 bool isParentTopLevel = true;
277 wxWindow* parent = GetParent();
279 wxWindow* tlw = MacGetTopLevelWindow();
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. ;)
287 while(parent != NULL)
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;
299 if (splitter->GetSashSize() > 0)
300 y += splitter->GetSashSize();
302 if (splitter->GetSashPosition() > 0)
303 y += splitter->GetSashPosition();
306 if (splitter->GetPosition().x > 0)
307 x += splitter->GetPosition().x;
309 if (splitter->GetSashSize() > 0)
310 x += splitter->GetSashSize();
312 if (splitter->GetSashPosition() > 0)
313 x += splitter->GetSashPosition();
317 if (!parent->IsTopLevel()) {
318 //printf("Parent: %s\n", parent->GetClassInfo()->GetClassName());
320 plusx = parent->GetClientAreaOrigin().x + parent->GetPosition().x;
323 //printf("Parent: %s Added x: %d\n", parent->GetClassInfo()->GetClassName(), parent->GetClientAreaOrigin().x + parent->GetPosition().x);
327 plusy = parent->GetClientAreaOrigin().y + parent->GetPosition().y;
330 //printf("Parent: %s Added y: %d\n", parent->GetClassInfo()->GetClassName(), parent->GetClientAreaOrigin().y + parent->GetPosition().y);
333 //printf("Parent: %s Origin: %d Position:%d\n", parent->GetClassInfo()->GetClassName(), parent->GetClientAreaOrigin().y, parent->GetPosition().y);
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);
346 //we still need to add the y, because we have to convert/flip coordinates for Cocoa
348 if ( parent->IsKindOf( CLASSINFO( wxNotebook ) ) ){
349 //Not sure why calcs are off in this one scenario...
351 //printf("x: %d, y:%d\n", x, y);
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!! ;)
363 parent = parent->GetParent();
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.
372 // GetParent()->MacWindowToRootWindow(&x2, &y2);
373 //printf("x = %d, y = %d\n", x, y);
374 //printf("x2 = %d, y2 = %d\n", x2, y2);
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);
384 //printf("Added to bounds x=%d, y=%d\n", x, y);
385 NSRect bounds = [m_webView frame];
388 [m_webView setFrame:bounds];
390 //printf("Carbon position x=%d, y=%d\n", GetPosition().x, GetPosition().y);
392 [(WebView*)m_webView display];
396 void wxWebKitCtrl::MacVisibilityChanged(){
397 bool isHidden = !IsControlVisible( m_peer->GetControlRef());
399 [(WebView*)m_webView display];
401 [m_webView setHidden:isHidden];
404 //------------------------------------------------------------
405 // Listener interfaces
406 //------------------------------------------------------------
408 @implementation MyFrameLoadMonitor
410 - initWithWxWindow: (wxWindow*)inWindow
413 webKitWindow = inWindow; // non retained
417 - (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame
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 );
428 - (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame
430 if (frame == [sender mainFrame]){
431 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
432 wxWebKitStateChangedEvent thisEvent(webKitWindow);
433 thisEvent.SetState(wxWEBKIT_STATE_TRANSFERRING);
434 thisEvent.SetURL( wxStringWithNSString( url ) );
435 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
439 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
441 if (frame == [sender mainFrame]){
442 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
443 wxWebKitStateChangedEvent thisEvent(webKitWindow);
444 thisEvent.SetState(wxWEBKIT_STATE_STOP);
445 thisEvent.SetURL( wxStringWithNSString( url ) );
446 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
450 - (void)webView:(WebView *)sender didFailLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
452 if (frame == [sender mainFrame]){
453 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
454 wxWebKitStateChangedEvent thisEvent(webKitWindow);
455 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
456 thisEvent.SetURL( wxStringWithNSString( url ) );
457 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
461 - (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
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 );
472 - (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
474 if (frame == [sender mainFrame]){
475 webKitWindow->SetTitle(wxStringWithNSString( title ));
480 #endif //wxUSE_WEBKIT