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"
 
  26 #include "wx/cocoa/autorelease.h"
 
  28 #include "wx/mac/uma.h"
 
  29 #include <Carbon/Carbon.h>
 
  30 #include <WebKit/HIWebView.h>
 
  31 #include <WebKit/CarbonUtils.h>
 
  34 #include "wx/html/webkit.h"
 
  35 #include "wx/notebook.h"
 
  38 // ----------------------------------------------------------------------------
 
  40 // ----------------------------------------------------------------------------
 
  42 #if !USE_SHARED_LIBRARY
 
  43 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     m_peer = new wxMacControl();
 
 149     wxControl::Create(parent, m_windowID, pos, size, style , validator , name);
 
 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];
 
 159     // Register event listener interfaces
 
 160     MyFrameLoadMonitor* myFrameLoadMonitor = [[MyFrameLoadMonitor alloc] initWithWxWindow: (wxWindow*)this];
 
 161     [m_webView setFrameLoadDelegate:myFrameLoadMonitor];
 
 163     LoadURL(m_currentURL);
 
 167 wxWebKitCtrl::~wxWebKitCtrl()
 
 172 // ----------------------------------------------------------------------------
 
 174 // ----------------------------------------------------------------------------
 
 176 void wxWebKitCtrl::LoadURL(const wxString &url)
 
 181     [[m_webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:wxNSStringWithWxString(url)]]];
 
 186 bool wxWebKitCtrl::CanGoBack(){
 
 190     return [m_webView canGoBack];
 
 193 bool wxWebKitCtrl::CanGoForward(){
 
 197     return [m_webView canGoForward];
 
 200 bool wxWebKitCtrl::GoBack(){
 
 204     bool result = [(WebView*)m_webView goBack];
 
 208 bool wxWebKitCtrl::GoForward(){
 
 212     bool result = [(WebView*)m_webView goForward];
 
 216 void wxWebKitCtrl::Reload(){
 
 220     [[m_webView mainFrame] reload];
 
 223 void wxWebKitCtrl::Stop(){
 
 227     [[m_webView mainFrame] stopLoading];
 
 230 bool wxWebKitCtrl::CanGetPageSource(){
 
 234     WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
 
 235     return ( [[dataSource representation] canProvideDocumentSource] );
 
 238 wxString wxWebKitCtrl::GetPageSource(){
 
 240     if (CanGetPageSource()){
 
 241         WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
 
 242         return wxStringWithNSString( [[dataSource representation] documentSource] );
 
 245     return wxEmptyString;
 
 248 void wxWebKitCtrl::SetPageSource(wxString& source, const wxString& baseUrl){
 
 252     if (CanGetPageSource()){
 
 253         [[m_webView mainFrame] loadHTMLString:(NSString*)wxNSStringWithWxString( source ) baseURL:[NSURL URLWithString:wxNSStringWithWxString( baseUrl )]];
 
 258 void wxWebKitCtrl::OnSize(wxSizeEvent &event){
 
 259     // This is a nasty hack because WebKit seems to lose its position when it is embedded
 
 260     // in a control that is not itself the content view for a TLW.
 
 262     wxWindow* parent = GetParent();
 
 263     bool isParentTopLevel = true;
 
 264     if (!parent->IsTopLevel())
 
 265         isParentTopLevel = false;
 
 267     int x = GetPosition().x;
 
 268     // we must take into account the title bar size as well, which is 26 pixels
 
 269     int y = GetPosition().y + 26;
 
 271     NSRect bounds = [m_webView frame];
 
 272     wxWindow* tlw = NULL;
 
 274     while(parent != NULL)
 
 276         if (parent->IsTopLevel())
 
 279         x += parent->GetPosition().x;
 
 280         y += parent->GetPosition().y;
 
 282         if ( parent->IsKindOf( CLASSINFO( wxNotebook ) )  ){
 
 283             //manually account for the size the tabs take up
 
 287         //if ( parent->GetClassInfo()->GetClassName() == wxT("wxSplitterWindow") ){
 
 291         parent = parent->GetParent();
 
 294     if (!isParentTopLevel){
 
 296             //x = tlw->GetSize().x - (GetSize().x + x);
 
 297             y = tlw->GetSize().y - (GetSize().y + y);
 
 299         NSRect bounds = [m_webView frame];
 
 300         bounds.origin.x += x;
 
 301         bounds.origin.y += y;
 
 302         //leaving debug checks in until I know it works everywhere ;-)
 
 303         //printf("Added to bounds x=%d, y=%d\n", x, y);
 
 304         [m_webView setFrame:bounds];
 
 307     //printf("Carbon position x=%d, y=%d\n", GetPosition().x, GetPosition().y);
 
 313 void wxWebKitCtrl::MacVisibilityChanged(){
 
 314     bool isHidden = !IsControlVisible( m_peer->GetControlRef());
 
 318     [m_webView setHidden:isHidden];
 
 321 //------------------------------------------------------------
 
 322 // Listener interfaces
 
 323 //------------------------------------------------------------
 
 325 @implementation MyFrameLoadMonitor
 
 327 - initWithWxWindow: (wxWindow*)inWindow
 
 330     webKitWindow = inWindow;    // non retained
 
 334 - (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame
 
 336     if (frame == [sender mainFrame]){
 
 337         NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
 
 338         wxWebKitStateChangedEvent thisEvent(webKitWindow);
 
 339         thisEvent.SetState(wxWEBKIT_STATE_NEGOTIATING);
 
 340         thisEvent.SetURL( wxStringWithNSString( url ) );
 
 341         webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
 
 345 - (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame
 
 347     if (frame == [sender mainFrame]){
 
 348         NSString *url = [[[[frame dataSource] request] URL] absoluteString];
 
 349         wxWebKitStateChangedEvent thisEvent(webKitWindow);
 
 350         thisEvent.SetState(wxWEBKIT_STATE_TRANSFERRING);
 
 351         thisEvent.SetURL( wxStringWithNSString( url ) );
 
 352         webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
 
 356 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
 
 358     if (frame == [sender mainFrame]){
 
 359         NSString *url = [[[[frame dataSource] request] URL] absoluteString];
 
 360         wxWebKitStateChangedEvent thisEvent(webKitWindow);
 
 361         thisEvent.SetState(wxWEBKIT_STATE_STOP);
 
 362         thisEvent.SetURL( wxStringWithNSString( url ) );
 
 363         webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
 
 367 - (void)webView:(WebView *)sender didFailLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
 
 369     if (frame == [sender mainFrame]){
 
 370         NSString *url = [[[[frame dataSource] request] URL] absoluteString];
 
 371         wxWebKitStateChangedEvent thisEvent(webKitWindow);
 
 372         thisEvent.SetState(wxWEBKIT_STATE_FAILED);
 
 373         thisEvent.SetURL( wxStringWithNSString( url ) );
 
 374         webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
 
 378 - (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
 
 380     if (frame == [sender mainFrame]){
 
 381         NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
 
 382         wxWebKitStateChangedEvent thisEvent(webKitWindow);
 
 383         thisEvent.SetState(wxWEBKIT_STATE_FAILED);
 
 384         thisEvent.SetURL( wxStringWithNSString( url ) );
 
 385         webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
 
 389 - (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
 
 391     if (frame == [sender mainFrame]){
 
 392         webKitWindow->SetTitle(wxStringWithNSString( title ));
 
 397 #endif //wxUSE_WEBKIT