X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/2c990ba6d4f88e18e6666f61efe55c7a8b5cc948..99f0dc688742156982e526639d17df4cb9003588:/src/html/htmlctrl/webkit/webkit.mm diff --git a/src/html/htmlctrl/webkit/webkit.mm b/src/html/htmlctrl/webkit/webkit.mm index df80d539e6..b25b47c315 100755 --- a/src/html/htmlctrl/webkit/webkit.mm +++ b/src/html/htmlctrl/webkit/webkit.mm @@ -9,44 +9,42 @@ // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// -#ifdef __GNUG__ - #pragma implementation "wxWebKit.h" +#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) + #pragma implementation "webkit.h" #endif // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" - -#ifdef __BORLANDC__ - #pragma hdrstop -#endif +#include "wx/splitter.h" #ifndef WX_PRECOMP #include "wx/wx.h" #endif +#if wxUSE_WEBKIT + #ifdef __WXCOCOA__ #include "wx/cocoa/autorelease.h" #else #include "wx/mac/uma.h" #include +#include #include #include #endif #include "wx/html/webkit.h" -//#include "wx/html/wklisten.h" +#include "wx/notebook.h" // ---------------------------------------------------------------------------- // macros // ---------------------------------------------------------------------------- -#if !USE_SHARED_LIBRARY IMPLEMENT_DYNAMIC_CLASS(wxWebKitCtrl, wxControl) -#endif BEGIN_EVENT_TABLE(wxWebKitCtrl, wxControl) - //EVT_SIZE(wxWebKitCtrl::OnSize) + EVT_SIZE(wxWebKitCtrl::OnSize) END_EVENT_TABLE() // ---------------------------------------------------------------------------- @@ -61,7 +59,7 @@ wxWebKitStateChangedEvent::wxWebKitStateChangedEvent( wxWindow* win ) { SetEventType( wxEVT_WEBKIT_STATE_CHANGED); SetEventObject( win ); - SetId(win->GetId()); + SetId(win->GetId()); } //--------------------------------------------------------- @@ -100,41 +98,41 @@ inline NSString* wxNSStringWithWxString(const wxString &wxstring) // ---------------------------------------------------------------------------- bool wxWebKitCtrl::Create(wxWindow *parent, - wxWindowID winID, + wxWindowID winID, const wxString& strURL, const wxPoint& pos, - const wxSize& size, long style, - const wxValidator& validator, - const wxString& name) + const wxSize& size, long style, + const wxValidator& validator, + const wxString& name) { m_currentURL = strURL; - m_pageTitle = ""; - + //m_pageTitle = _("Untitled Page"); + //still needed for wxCocoa?? /* int width, height; - wxSize sizeInstance; - if (size.x == -1 || size.y == -1) - { - m_parent->GetClientSize(&width, &height); - sizeInstance.x = width; - sizeInstance.y = height; - } - else - { - sizeInstance.x = size.x; - sizeInstance.y = size.y; - } -*/ - // now create and attach WebKit view... + wxSize sizeInstance; + if (size.x == wxDefaultCoord || size.y == wxDefaultCoord) + { + m_parent->GetClientSize(&width, &height); + sizeInstance.x = width; + sizeInstance.y = height; + } + else + { + sizeInstance.x = size.x; + sizeInstance.y = size.y; + } +*/ + // now create and attach WebKit view... #ifdef __WXCOCOA__ wxControl::Create(parent, m_windowID, pos, sizeInstance, style , validator , name); - SetSize(pos.x, pos.y, sizeInstance.x, sizeInstance.y); - + SetSize(pos.x, pos.y, sizeInstance.x, sizeInstance.y); + wxTopLevelWindowCocoa *topWin = wxDynamicCast(this, wxTopLevelWindowCocoa); NSWindow* nsWin = topWin->GetNSWindow(); - NSRect rect; + NSRect rect; rect.origin.x = pos.x; rect.origin.y = pos.y; rect.size.width = sizeInstance.x; @@ -147,19 +145,25 @@ bool wxWebKitCtrl::Create(wxWindow *parent, SetInitialFrameRect(pos,sizeInstance); #else m_macIsUserPane = false; - wxControl::Create(parent, m_windowID, pos, size, style , validator , name); + wxControl::Create(parent, winID, pos, size, style , validator , name); + m_peer = new wxMacControl(this); WebInitForCarbon(); - HIWebViewCreate( (HIViewRef*) &m_macControl ); - MacPostControlCreate(pos, size); + HIWebViewCreate( m_peer->GetControlRefAddr() ); - HIViewSetVisible( (HIViewRef) m_macControl, true ); - - m_webView = (WebView*) HIWebViewGetWebView( (HIViewRef) m_macControl ); + m_webView = (WebView*) HIWebViewGetWebView( m_peer->GetControlRef() ); + MacPostControlCreate(pos, size); + HIViewSetVisible( m_peer->GetControlRef(), true ); + [m_webView setHidden:false]; +#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3 + if ( UMAGetSystemVersion() >= 0x1030 ) + HIViewChangeFeatures( m_peer->GetControlRef() , kHIViewIsOpaque , 0 ) ; +#endif #endif // Register event listener interfaces MyFrameLoadMonitor* myFrameLoadMonitor = [[MyFrameLoadMonitor alloc] initWithWxWindow: (wxWindow*)this]; - [m_webView setFrameLoadDelegate:myFrameLoadMonitor]; + [m_webView setFrameLoadDelegate:myFrameLoadMonitor]; + LoadURL(m_currentURL); return true; } @@ -177,8 +181,8 @@ void wxWebKitCtrl::LoadURL(const wxString &url) { if( !m_webView ) return; - - [[m_webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithCString:url.c_str()]]]]; + + [[m_webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:wxNSStringWithWxString(url)]]]; m_currentURL = url; } @@ -186,76 +190,218 @@ void wxWebKitCtrl::LoadURL(const wxString &url) bool wxWebKitCtrl::CanGoBack(){ if ( !m_webView ) return false; - + return [m_webView canGoBack]; } bool wxWebKitCtrl::CanGoForward(){ if ( !m_webView ) return false; - + return [m_webView canGoForward]; } bool wxWebKitCtrl::GoBack(){ if ( !m_webView ) return false; - - [m_webView goBack]; - return true; + + bool result = [(WebView*)m_webView goBack]; + return result; } -bool wxWebKitCtrl::GoForward(){ +bool wxWebKitCtrl::GoForward(){ if ( !m_webView ) return false; - - [m_webView goForward]; - return true; + + bool result = [(WebView*)m_webView goForward]; + return result; } -void wxWebKitCtrl::Reload(){ +void wxWebKitCtrl::Reload(){ if ( !m_webView ) return; - + [[m_webView mainFrame] reload]; } void wxWebKitCtrl::Stop(){ if ( !m_webView ) return; - + [[m_webView mainFrame] stopLoading]; } bool wxWebKitCtrl::CanGetPageSource(){ if ( !m_webView ) - return; - + return false; + WebDataSource* dataSource = [[m_webView mainFrame] dataSource]; return ( [[dataSource representation] canProvideDocumentSource] ); } wxString wxWebKitCtrl::GetPageSource(){ - if ( !m_webView ) - return; - + if (CanGetPageSource()){ WebDataSource* dataSource = [[m_webView mainFrame] dataSource]; return wxStringWithNSString( [[dataSource representation] documentSource] ); } - + + return wxEmptyString; } void wxWebKitCtrl::SetPageSource(wxString& source, const wxString& baseUrl){ if ( !m_webView ) return; - + if (CanGetPageSource()){ [[m_webView mainFrame] loadHTMLString:(NSString*)wxNSStringWithWxString( source ) baseURL:[NSURL URLWithString:wxNSStringWithWxString( baseUrl )]]; } } +void wxWebKitCtrl::OnSize(wxSizeEvent &event){ + // This is a nasty hack because WebKit seems to lose its position when it is embedded + // in a control that is not itself the content view for a TLW. + // I put it in OnSize because these calcs are not perfect, and in fact are basically + // guesses based on reverse engineering, so it's best to give people the option of + // overriding OnSize with their own calcs if need be. + // I also left some test debugging print statements as a convenience if a(nother) + // problem crops up. + + // Let's hope that Tiger fixes this mess... + + int x, y; + x = 0; + y = 0; + + bool isParentTopLevel = true; + + wxWindow* parent = GetParent(); + + wxWindow* tlw = MacGetTopLevelWindow(); + + // This must be the case that Apple tested with, because well, in this one case + // we don't need to do anything! It just works. ;) + if (parent == tlw){ + return; + } + + while(parent != NULL) + { + if ( parent->IsKindOf( CLASSINFO( wxSplitterWindow ) ) && GetParent()->IsKindOf( CLASSINFO( wxSplitterWindow ) ) ){ + // When parent is not a wxSplitterWindow, we can rely on it's GetPosition() to give us the correct + // coordinates, but when the parent is a wxSplitterWindow, we need to manually calculate + // the sash position of it and any parent wxSplitterWindows into the webkit's position. + wxSplitterWindow* splitter; + splitter = dynamic_cast(parent); + if (splitter->GetSplitMode() == wxSPLIT_HORIZONTAL){ + if (splitter->GetPosition().y > 0) + y += splitter->GetPosition().y; + + if (splitter->GetSashSize() > 0) + y += splitter->GetSashSize(); + + if (splitter->GetSashPosition() > 0) + y += splitter->GetSashPosition(); + } + else{ + if (splitter->GetPosition().x > 0) + x += splitter->GetPosition().x; + + if (splitter->GetSashSize() > 0) + x += splitter->GetSashSize(); + + if (splitter->GetSashPosition() > 0) + x += splitter->GetSashPosition(); + } + } + else{ + if (!parent->IsTopLevel()) { + //printf("Parent: %s\n", parent->GetClassInfo()->GetClassName()); + int plusx = 0; + plusx = parent->GetClientAreaOrigin().x + parent->GetPosition().x; + if (plusx > 0){ + x += plusx; + //printf("Parent: %s Added x: %d\n", parent->GetClassInfo()->GetClassName(), parent->GetClientAreaOrigin().x + parent->GetPosition().x); + } + + int plusy = 0; + plusy = parent->GetClientAreaOrigin().y + parent->GetPosition().y; + if (plusy > 0){ + y += plusy; + //printf("Parent: %s Added y: %d\n", parent->GetClassInfo()->GetClassName(), parent->GetClientAreaOrigin().y + parent->GetPosition().y); + } + else{ + //printf("Parent: %s Origin: %d Position:%d\n", parent->GetClassInfo()->GetClassName(), parent->GetClientAreaOrigin().y, parent->GetPosition().y); + } + + } + else{ + // + x += parent->GetClientAreaOrigin().x; + // calculate the title bar height (26 pixels) into the top offset. + // This becomes important later when we must flip the y coordinate + // to convert to Cocoa's coordinate system. + y += parent->GetClientAreaOrigin().y += 26; + //printf("x: %d, y:%d\n", x, y); + } + //we still need to add the y, because we have to convert/flip coordinates for Cocoa + + if ( parent->IsKindOf( CLASSINFO( wxNotebook ) ) ){ + //Not sure why calcs are off in this one scenario... + y -= 4; + //printf("x: %d, y:%d\n", x, y); + } + + if (parent->IsKindOf( CLASSINFO( wxPanel ) ) ){ + // Another strange case. Adding a wxPanel to the parent heirarchy + // causes wxWebKitCtrl's Cocoa y origin to be 4 pixels off + // for some reason, even if the panel has a position and origin of 0. + // This corrects that. Man, I wish I could debug Carbon/HIWebView!! ;) + y -= 4; + } + } + + parent = parent->GetParent(); + } + + // Tried using MacWindowToRootWindow both for wxWebKitCtrl and its parent, + // but coordinates were off by a significant amount. + // Am leaving the code here if anyone wants to play with it. + + //int x2, y2 = 0; + //if (GetParent()) + // GetParent()->MacWindowToRootWindow(&x2, &y2); + //printf("x = %d, y = %d\n", x, y); + //printf("x2 = %d, y2 = %d\n", x2, y2); + //x = x2; + //y = y2; + + if (tlw){ + //flip the y coordinate to convert to Cocoa coordinates + //printf("tlw y: %d, y: %d\n", tlw->GetSize().y, (GetSize().y + y)); + y = tlw->GetSize().y - ((GetSize().y) + y); + } + + //printf("Added to bounds x=%d, y=%d\n", x, y); + NSRect bounds = [m_webView frame]; + bounds.origin.x = x; + bounds.origin.y = y; + [m_webView setFrame:bounds]; + + //printf("Carbon position x=%d, y=%d\n", GetPosition().x, GetPosition().y); + if (IsShown()) + [(WebView*)m_webView display]; + event.Skip(); +} + +void wxWebKitCtrl::MacVisibilityChanged(){ + bool isHidden = !IsControlVisible( m_peer->GetControlRef()); + if (!isHidden) + [(WebView*)m_webView display]; + + [m_webView setHidden:isHidden]; +} //------------------------------------------------------------ // Listener interfaces @@ -266,7 +412,7 @@ void wxWebKitCtrl::SetPageSource(wxString& source, const wxString& baseUrl){ - initWithWxWindow: (wxWindow*)inWindow { [super init]; - webKitWindow = inWindow; // non retained + webKitWindow = inWindow; // non retained return self; } @@ -284,7 +430,7 @@ void wxWebKitCtrl::SetPageSource(wxString& source, const wxString& baseUrl){ - (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame { if (frame == [sender mainFrame]){ - NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString]; + NSString *url = [[[[frame dataSource] request] URL] absoluteString]; wxWebKitStateChangedEvent thisEvent(webKitWindow); thisEvent.SetState(wxWEBKIT_STATE_TRANSFERRING); thisEvent.SetURL( wxStringWithNSString( url ) ); @@ -295,7 +441,7 @@ void wxWebKitCtrl::SetPageSource(wxString& source, const wxString& baseUrl){ - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame { if (frame == [sender mainFrame]){ - NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString]; + NSString *url = [[[[frame dataSource] request] URL] absoluteString]; wxWebKitStateChangedEvent thisEvent(webKitWindow); thisEvent.SetState(wxWEBKIT_STATE_STOP); thisEvent.SetURL( wxStringWithNSString( url ) ); @@ -306,7 +452,7 @@ void wxWebKitCtrl::SetPageSource(wxString& source, const wxString& baseUrl){ - (void)webView:(WebView *)sender didFailLoadWithError:(NSError*) error forFrame:(WebFrame *)frame { if (frame == [sender mainFrame]){ - NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString]; + NSString *url = [[[[frame dataSource] request] URL] absoluteString]; wxWebKitStateChangedEvent thisEvent(webKitWindow); thisEvent.SetState(wxWEBKIT_STATE_FAILED); thisEvent.SetURL( wxStringWithNSString( url ) ); @@ -331,4 +477,6 @@ void wxWebKitCtrl::SetPageSource(wxString& source, const wxString& baseUrl){ webKitWindow->SetTitle(wxStringWithNSString( title )); } } -@end \ No newline at end of file +@end + +#endif //wxUSE_WEBKIT