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/WebKit.h>
 
  32 #include <WebKit/HIWebView.h>
 
  33 #include <WebKit/CarbonUtils.h>
 
  36 #include "wx/html/webkit.h"
 
  37 #include "wx/notebook.h"
 
  40 // ----------------------------------------------------------------------------
 
  42 // ----------------------------------------------------------------------------
 
  44 IMPLEMENT_DYNAMIC_CLASS(wxWebKitCtrl, wxControl)
 
  46 BEGIN_EVENT_TABLE(wxWebKitCtrl, wxControl)
 
  47     EVT_SIZE(wxWebKitCtrl::OnSize)
 
  50 // ----------------------------------------------------------------------------
 
  52 // ----------------------------------------------------------------------------
 
  54 IMPLEMENT_DYNAMIC_CLASS( wxWebKitStateChangedEvent, wxCommandEvent )
 
  56 DEFINE_EVENT_TYPE( wxEVT_WEBKIT_STATE_CHANGED )
 
  58 wxWebKitStateChangedEvent::wxWebKitStateChangedEvent( wxWindow* win )
 
  60     SetEventType( wxEVT_WEBKIT_STATE_CHANGED);
 
  61     SetEventObject( win );
 
  65 //---------------------------------------------------------
 
  66 // helper functions for NSString<->wxString conversion
 
  67 //---------------------------------------------------------
 
  69 inline wxString wxStringWithNSString(NSString *nsstring)
 
  72     return wxString([nsstring UTF8String], wxConvUTF8);
 
  74     return wxString([nsstring lossyCString]);
 
  75 #endif // wxUSE_UNICODE
 
  78 inline NSString* wxNSStringWithWxString(const wxString &wxstring)
 
  81     return [NSString stringWithUTF8String: wxstring.mb_str(wxConvUTF8)];
 
  83     return [NSString stringWithCString: wxstring.c_str() length:wxstring.Len()];
 
  84 #endif // wxUSE_UNICODE
 
  87 @interface MyFrameLoadMonitor : NSObject
 
  89     wxWindow* webKitWindow;
 
  92 - initWithWxWindow: (wxWindow*)inWindow;
 
  96 // ----------------------------------------------------------------------------
 
  97 // creation/destruction
 
  98 // ----------------------------------------------------------------------------
 
 100 bool wxWebKitCtrl::Create(wxWindow *parent,
 
 102                                  const wxString& strURL,
 
 104                                  const wxSize& size, long style,
 
 105                                  const wxValidator& validator,
 
 106                                  const wxString& name)
 
 109     m_currentURL = strURL;
 
 110     //m_pageTitle = _("Untitled Page");
 
 112  //still needed for wxCocoa??
 
 116     if (size.x == wxDefaultCoord || size.y == wxDefaultCoord)
 
 118         m_parent->GetClientSize(&width, &height);
 
 119         sizeInstance.x = width;
 
 120         sizeInstance.y = height;
 
 124         sizeInstance.x = size.x;
 
 125         sizeInstance.y = size.y;
 
 128     // now create and attach WebKit view...
 
 130     wxControl::Create(parent, m_windowID, pos, sizeInstance, style , validator , name);
 
 131     SetSize(pos.x, pos.y, sizeInstance.x, sizeInstance.y);
 
 133     wxTopLevelWindowCocoa *topWin = wxDynamicCast(this, wxTopLevelWindowCocoa);
 
 134     NSWindow* nsWin = topWin->GetNSWindow();
 
 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];
 
 144     if(m_parent) m_parent->CocoaAddChild(this);
 
 145     SetInitialFrameRect(pos,sizeInstance);
 
 147     m_macIsUserPane = false;
 
 148         wxControl::Create(parent, winID, pos, size, style , validator , name);
 
 149     m_peer = new wxMacControl(this);
 
 151     HIWebViewCreate( m_peer->GetControlRefAddr() );
 
 153     m_webView = (WebView*) HIWebViewGetWebView( m_peer->GetControlRef() );
 
 154     MacPostControlCreate(pos, size);
 
 155     HIViewSetVisible( m_peer->GetControlRef(), true );
 
 156     [m_webView setHidden:false];
 
 157 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
 
 158         if ( UMAGetSystemVersion() >= 0x1030 )
 
 159     HIViewChangeFeatures( m_peer->GetControlRef() , kHIViewIsOpaque , 0 ) ;
 
 163     // Register event listener interfaces
 
 164     MyFrameLoadMonitor* myFrameLoadMonitor = [[MyFrameLoadMonitor alloc] initWithWxWindow: (wxWindow*)this];
 
 165     [m_webView setFrameLoadDelegate:myFrameLoadMonitor];
 
 167     LoadURL(m_currentURL);
 
 171 wxWebKitCtrl::~wxWebKitCtrl()
 
 176 // ----------------------------------------------------------------------------
 
 178 // ----------------------------------------------------------------------------
 
 180 void wxWebKitCtrl::LoadURL(const wxString &url)
 
 185     [[m_webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:wxNSStringWithWxString(url)]]];
 
 190 bool wxWebKitCtrl::CanGoBack(){
 
 194     return [m_webView canGoBack];
 
 197 bool wxWebKitCtrl::CanGoForward(){
 
 201     return [m_webView canGoForward];
 
 204 bool wxWebKitCtrl::GoBack(){
 
 208     bool result = [(WebView*)m_webView goBack];
 
 212 bool wxWebKitCtrl::GoForward(){
 
 216     bool result = [(WebView*)m_webView goForward];
 
 220 void wxWebKitCtrl::Reload(){
 
 224     [[m_webView mainFrame] reload];
 
 227 void wxWebKitCtrl::Stop(){
 
 231     [[m_webView mainFrame] stopLoading];
 
 234 bool wxWebKitCtrl::CanGetPageSource(){
 
 238     WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
 
 239     return ( [[dataSource representation] canProvideDocumentSource] );
 
 242 wxString wxWebKitCtrl::GetPageSource(){
 
 244     if (CanGetPageSource()){
 
 245         WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
 
 246         return wxStringWithNSString( [[dataSource representation] documentSource] );
 
 249     return wxEmptyString;
 
 252 void wxWebKitCtrl::SetPageSource(wxString& source, const wxString& baseUrl){
 
 256     if (CanGetPageSource()){
 
 257         [[m_webView mainFrame] loadHTMLString:(NSString*)wxNSStringWithWxString( source ) baseURL:[NSURL URLWithString:wxNSStringWithWxString( baseUrl )]];
 
 262 void wxWebKitCtrl::OnSize(wxSizeEvent &event){
 
 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.
 
 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)
 
 271         // Let's hope that Tiger fixes this mess...
 
 277         bool isParentTopLevel = true;
 
 279         wxWindow* parent = GetParent();
 
 281         wxWindow* tlw = MacGetTopLevelWindow();
 
 283         // This must be the case that Apple tested with, because well, in this one case
 
 284         // we don't need to do anything! It just works. ;)
 
 289         while(parent != NULL)
 
 291                 if ( parent->IsKindOf( CLASSINFO( wxSplitterWindow ) ) && GetParent()->IsKindOf( CLASSINFO( wxSplitterWindow ) ) ){
 
 292                                 // When parent is not a wxSplitterWindow, we can rely on it's GetPosition() to give us the correct
 
 293                                 // coordinates, but when the parent is a wxSplitterWindow, we need to manually calculate 
 
 294                                 // the sash position of it and any parent wxSplitterWindows into the webkit's position.
 
 295                                 wxSplitterWindow* splitter;
 
 296                                 splitter = dynamic_cast<wxSplitterWindow*>(parent);
 
 297                                 if (splitter->GetSplitMode() == wxSPLIT_HORIZONTAL){
 
 298                                         if (splitter->GetPosition().y > 0)
 
 299                                                 y += splitter->GetPosition().y;
 
 301                                         if (splitter->GetSashSize() > 0)
 
 302                                                 y += splitter->GetSashSize();
 
 304                                         if (splitter->GetSashPosition() > 0)
 
 305                                                 y += splitter->GetSashPosition();
 
 308                                         if (splitter->GetPosition().x > 0)
 
 309                                                 x += splitter->GetPosition().x;
 
 311                                         if (splitter->GetSashSize() > 0)
 
 312                                                 x += splitter->GetSashSize();
 
 314                                         if (splitter->GetSashPosition() > 0)
 
 315                                                 x += splitter->GetSashPosition();
 
 319                         if (!parent->IsTopLevel()) {
 
 320                                 //printf("Parent: %s\n", parent->GetClassInfo()->GetClassName());
 
 322                                 plusx = parent->GetClientAreaOrigin().x + parent->GetPosition().x; 
 
 325                                         //printf("Parent: %s Added x: %d\n", parent->GetClassInfo()->GetClassName(), parent->GetClientAreaOrigin().x + parent->GetPosition().x);
 
 329                                 plusy = parent->GetClientAreaOrigin().y + parent->GetPosition().y;
 
 332                                         //printf("Parent: %s Added y: %d\n", parent->GetClassInfo()->GetClassName(), parent->GetClientAreaOrigin().y + parent->GetPosition().y);
 
 335                                         //printf("Parent: %s Origin: %d Position:%d\n", parent->GetClassInfo()->GetClassName(), parent->GetClientAreaOrigin().y, parent->GetPosition().y);
 
 341                                 x += parent->GetClientAreaOrigin().x;
 
 342                                 // calculate the title bar height (26 pixels) into the top offset.
 
 343                                 // This becomes important later when we must flip the y coordinate
 
 344                                 // to convert to Cocoa's coordinate system.
 
 345                                 y += parent->GetClientAreaOrigin().y += 26;
 
 346                                 //printf("x: %d, y:%d\n", x, y);
 
 348                         //we still need to add the y, because we have to convert/flip coordinates for Cocoa
 
 350                         if ( parent->IsKindOf( CLASSINFO( wxNotebook ) )  ){
 
 351                                 //Not sure why calcs are off in this one scenario...
 
 353                                 //printf("x: %d, y:%d\n", x, y);
 
 356                         if (parent->IsKindOf( CLASSINFO( wxPanel ) ) ){
 
 357                                 // Another strange case. Adding a wxPanel to the parent heirarchy
 
 358                                 // causes wxWebKitCtrl's Cocoa y origin to be 4 pixels off 
 
 359                                 // for some reason, even if the panel has a position and origin of 0. 
 
 360                                 // This corrects that. Man, I wish I could debug Carbon/HIWebView!! ;)
 
 365                 parent = parent->GetParent();
 
 368         // Tried using MacWindowToRootWindow both for wxWebKitCtrl and its parent,
 
 369         // but coordinates were off by a significant amount.
 
 370         // Am leaving the code here if anyone wants to play with it.
 
 374         //      GetParent()->MacWindowToRootWindow(&x2, &y2);
 
 375         //printf("x = %d, y = %d\n", x, y);
 
 376         //printf("x2 = %d, y2 = %d\n", x2, y2);
 
 381                 //flip the y coordinate to convert to Cocoa coordinates
 
 382                 //printf("tlw y: %d, y: %d\n", tlw->GetSize().y, (GetSize().y + y));
 
 383                 y = tlw->GetSize().y - ((GetSize().y) + y);
 
 386         //printf("Added to bounds x=%d, y=%d\n", x, y);
 
 387         NSRect bounds = [m_webView frame];
 
 390         [m_webView setFrame:bounds];
 
 392     //printf("Carbon position x=%d, y=%d\n", GetPosition().x, GetPosition().y);
 
 394         [(WebView*)m_webView display];
 
 398 void wxWebKitCtrl::MacVisibilityChanged(){
 
 399     bool isHidden = !IsControlVisible( m_peer->GetControlRef());
 
 401         [(WebView*)m_webView display];
 
 403     [m_webView setHidden:isHidden];
 
 406 //------------------------------------------------------------
 
 407 // Listener interfaces
 
 408 //------------------------------------------------------------
 
 410 @implementation MyFrameLoadMonitor
 
 412 - initWithWxWindow: (wxWindow*)inWindow
 
 415     webKitWindow = inWindow;    // non retained
 
 419 - (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame
 
 421     if (frame == [sender mainFrame]){
 
 422         NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
 
 423         wxWebKitStateChangedEvent thisEvent(webKitWindow);
 
 424         thisEvent.SetState(wxWEBKIT_STATE_NEGOTIATING);
 
 425         thisEvent.SetURL( wxStringWithNSString( url ) );
 
 426         webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
 
 430 - (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame
 
 432     if (frame == [sender mainFrame]){
 
 433         NSString *url = [[[[frame dataSource] request] URL] absoluteString];
 
 434         wxWebKitStateChangedEvent thisEvent(webKitWindow);
 
 435         thisEvent.SetState(wxWEBKIT_STATE_TRANSFERRING);
 
 436         thisEvent.SetURL( wxStringWithNSString( url ) );
 
 437         webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
 
 441 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
 
 443     if (frame == [sender mainFrame]){
 
 444         NSString *url = [[[[frame dataSource] request] URL] absoluteString];
 
 445         wxWebKitStateChangedEvent thisEvent(webKitWindow);
 
 446         thisEvent.SetState(wxWEBKIT_STATE_STOP);
 
 447         thisEvent.SetURL( wxStringWithNSString( url ) );
 
 448         webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
 
 452 - (void)webView:(WebView *)sender didFailLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
 
 454     if (frame == [sender mainFrame]){
 
 455         NSString *url = [[[[frame dataSource] request] URL] absoluteString];
 
 456         wxWebKitStateChangedEvent thisEvent(webKitWindow);
 
 457         thisEvent.SetState(wxWEBKIT_STATE_FAILED);
 
 458         thisEvent.SetURL( wxStringWithNSString( url ) );
 
 459         webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
 
 463 - (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
 
 465     if (frame == [sender mainFrame]){
 
 466         NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
 
 467         wxWebKitStateChangedEvent thisEvent(webKitWindow);
 
 468         thisEvent.SetState(wxWEBKIT_STATE_FAILED);
 
 469         thisEvent.SetURL( wxStringWithNSString( url ) );
 
 470         webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
 
 474 - (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
 
 476     if (frame == [sender mainFrame]){
 
 477         webKitWindow->SetTitle(wxStringWithNSString( title ));
 
 482 #endif //wxUSE_WEBKIT