1 /////////////////////////////////////////////////////////////////////////////
 
   2 // Name:        src/html/htmlctrl/webkit/webkit.mm
 
   3 // Purpose:     wxWebKitCtrl - embeddable web kit control
 
   4 // Author:      Jethro Grassie / Kevin Ollivier
 
   7 // Copyright:   (c) Jethro Grassie / Kevin Ollivier
 
   8 // Licence:     wxWindows licence
 
   9 /////////////////////////////////////////////////////////////////////////////
 
  11 // For compilers that support precompilation, includes "wx.h".
 
  12 #include "wx/wxprec.h"
 
  13 #include "wx/splitter.h"
 
  22 #include "wx/cocoa/autorelease.h"
 
  24 #include "wx/osx/private.h"
 
  26 #include <WebKit/WebKit.h>
 
  27 #include <WebKit/HIWebView.h>
 
  28 #include <WebKit/CarbonUtils.h>
 
  31 #include "wx/html/webkit.h"
 
  33 #define DEBUG_WEBKIT_SIZING 0
 
  35 extern WXDLLEXPORT_DATA(const char) wxWebKitCtrlNameStr[] = "webkitctrl";
 
  37 // ----------------------------------------------------------------------------
 
  39 // ----------------------------------------------------------------------------
 
  41 IMPLEMENT_DYNAMIC_CLASS(wxWebKitCtrl, wxControl)
 
  43 BEGIN_EVENT_TABLE(wxWebKitCtrl, wxControl)
 
  44 #if defined(__WXMAC__) && wxOSX_USE_CARBON
 
  45     EVT_SIZE(wxWebKitCtrl::OnSize)
 
  49 #if defined(__WXOSX__) && wxOSX_USE_CARBON
 
  51 // ----------------------------------------------------------------------------
 
  52 // Carbon Events handlers
 
  53 // ----------------------------------------------------------------------------
 
  55 // prototype for function in src/osx/carbon/nonownedwnd.cpp
 
  56 void SetupMouseEvent( wxMouseEvent &wxevent , wxMacCarbonEvent &cEvent );
 
  58 static const EventTypeSpec eventList[] =
 
  60     //{ kEventClassControl, kEventControlTrack } ,
 
  61     { kEventClassMouse, kEventMouseUp },
 
  62     { kEventClassMouse, kEventMouseDown },
 
  63     { kEventClassMouse, kEventMouseMoved },
 
  64     { kEventClassMouse, kEventMouseDragged },
 
  66     { kEventClassKeyboard, kEventRawKeyDown } ,
 
  67     { kEventClassKeyboard, kEventRawKeyRepeat } ,
 
  68     { kEventClassKeyboard, kEventRawKeyUp } ,
 
  69     { kEventClassKeyboard, kEventRawKeyModifiersChanged } ,
 
  71     { kEventClassTextInput, kEventTextInputUnicodeForKeyEvent } ,
 
  72     { kEventClassTextInput, kEventTextInputUpdateActiveInputArea } ,
 
  74 #if DEBUG_WEBKIT_SIZING == 1
 
  75     { kEventClassControl, kEventControlBoundsChanged } ,
 
  79 // mix this in from window.cpp
 
  80 pascal OSStatus wxMacUnicodeTextEventHandler( EventHandlerCallRef handler , EventRef event , void *data ) ;
 
  82 // NOTE: This is mostly taken from KeyboardEventHandler in toplevel.cpp, but
 
  83 // that expects the data pointer is a top-level window, so I needed to change
 
  84 // that in this case. However, once 2.8 is out, we should factor out the common logic
 
  85 // among the two functions and merge them.
 
  86 static pascal OSStatus wxWebKitKeyEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
 
  88     OSStatus result = eventNotHandledErr ;
 
  89     wxMacCarbonEvent cEvent( event ) ;
 
  91     wxWebKitCtrl* thisWindow = (wxWebKitCtrl*) data ;
 
  92     wxWindow* focus = thisWindow ;
 
  94     unsigned char charCode ;
 
 101     UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ;
 
 104     ByteCount dataSize = 0 ;
 
 105     if ( GetEventParameter( event, kEventParamKeyUnicodes, typeUnicodeText, NULL, 0 , &dataSize, NULL ) == noErr )
 
 108         int numChars = dataSize / sizeof( UniChar) + 1;
 
 110         UniChar* charBuf = buf ;
 
 112         if ( numChars * 2 > 4 )
 
 113             charBuf = new UniChar[ numChars ] ;
 
 114         GetEventParameter( event, kEventParamKeyUnicodes, typeUnicodeText, NULL, dataSize , NULL , charBuf ) ;
 
 115         charBuf[ numChars - 1 ] = 0;
 
 117 #if SIZEOF_WCHAR_T == 2
 
 118         uniChar = charBuf[0] ;
 
 120         wxMBConvUTF16 converter ;
 
 121         converter.MB2WC( uniChar , (const char*)charBuf , 2 ) ;
 
 124         if ( numChars * 2 > 4 )
 
 129     GetEventParameter( event, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode );
 
 130     GetEventParameter( event, kEventParamKeyCode, typeUInt32, NULL, sizeof(UInt32), NULL, &keyCode );
 
 131     GetEventParameter( event, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(UInt32), NULL, &modifiers );
 
 133     UInt32 message = (keyCode << 8) + charCode;
 
 134     switch ( GetEventKind( event ) )
 
 136         case kEventRawKeyRepeat :
 
 137         case kEventRawKeyDown :
 
 139                 WXEVENTREF formerEvent = wxTheApp->MacGetCurrentEvent() ;
 
 140                 WXEVENTHANDLERCALLREF formerHandler = wxTheApp->MacGetCurrentEventHandlerCallRef() ;
 
 141                 wxTheApp->MacSetCurrentEvent( event , handler ) ;
 
 142                 if ( /* focus && */ wxTheApp->MacSendKeyDownEvent(
 
 143                     focus , message , modifiers , when , uniChar[0] ) )
 
 147                 wxTheApp->MacSetCurrentEvent( formerEvent , formerHandler ) ;
 
 151         case kEventRawKeyUp :
 
 152             if ( /* focus && */ wxTheApp->MacSendKeyUpEvent(
 
 153                 focus , message , modifiers , when , uniChar[0] ) )
 
 159         case kEventRawKeyModifiersChanged :
 
 161                 wxKeyEvent event(wxEVT_KEY_DOWN);
 
 163                 event.m_shiftDown = modifiers & shiftKey;
 
 164                 event.m_rawControlDown = modifiers & controlKey;
 
 165                 event.m_altDown = modifiers & optionKey;
 
 166                 event.m_controlDown = modifiers & cmdKey;
 
 169                 event.m_uniChar = uniChar[0] ;
 
 172                 event.SetTimestamp(when);
 
 173                 event.SetEventObject(focus);
 
 175                 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & controlKey )
 
 177                     event.m_keyCode = WXK_RAW_CONTROL ;
 
 178                     event.SetEventType( ( modifiers & controlKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
 
 179                     focus->GetEventHandler()->ProcessEvent( event ) ;
 
 181                 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & shiftKey )
 
 183                     event.m_keyCode = WXK_SHIFT ;
 
 184                     event.SetEventType( ( modifiers & shiftKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
 
 185                     focus->GetEventHandler()->ProcessEvent( event ) ;
 
 187                 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & optionKey )
 
 189                     event.m_keyCode = WXK_ALT ;
 
 190                     event.SetEventType( ( modifiers & optionKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
 
 191                     focus->GetEventHandler()->ProcessEvent( event ) ;
 
 193                 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & cmdKey )
 
 195                     event.m_keyCode = WXK_CONTROL ;
 
 196                     event.SetEventType( ( modifiers & cmdKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
 
 197                     focus->GetEventHandler()->ProcessEvent( event ) ;
 
 200                 wxApp::s_lastModifiers = modifiers ;
 
 211 static pascal OSStatus wxWebKitCtrlEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
 
 213     OSStatus result = eventNotHandledErr ;
 
 215     wxMacCarbonEvent cEvent( event ) ;
 
 217     ControlRef controlRef ;
 
 218     wxWebKitCtrl* thisWindow = (wxWebKitCtrl*) data ;
 
 219     wxNonOwnedWindow* tlw = NULL;
 
 221         tlw = thisWindow->MacGetTopLevelWindow();
 
 223     cEvent.GetParameter( kEventParamDirectObject , &controlRef ) ;
 
 225     wxWindow* currentMouseWindow = thisWindow ;
 
 227     if ( wxApp::s_captureWindow )
 
 228         currentMouseWindow = wxApp::s_captureWindow;
 
 230     switch ( GetEventClass( event ) )
 
 232         case kEventClassKeyboard:
 
 234             result = wxWebKitKeyEventHandler(handler, event, data);
 
 238         case kEventClassTextInput:
 
 240             result = wxMacUnicodeTextEventHandler(handler, event, data);
 
 244         case kEventClassMouse:
 
 246             switch ( GetEventKind( event ) )
 
 248                 case kEventMouseDragged :
 
 249                 case kEventMouseMoved :
 
 250                 case kEventMouseDown :
 
 253                     wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
 
 254                     SetupMouseEvent( wxevent , cEvent ) ;
 
 256                     currentMouseWindow->ScreenToClient( &wxevent.m_x , &wxevent.m_y ) ;
 
 257                     wxevent.SetEventObject( currentMouseWindow ) ;
 
 258                     wxevent.SetId( currentMouseWindow->GetId() ) ;
 
 260                     if ( currentMouseWindow->GetEventHandler()->ProcessEvent(wxevent) )
 
 265                     break; // this should enable WebKit to fire mouse dragged and mouse up events...
 
 275     result = CallNextEventHandler(handler, event);
 
 279 DEFINE_ONE_SHOT_HANDLER_GETTER( wxWebKitCtrlEventHandler )
 
 283 // ----------------------------------------------------------------------------
 
 285 // ----------------------------------------------------------------------------
 
 287 IMPLEMENT_DYNAMIC_CLASS( wxWebKitStateChangedEvent, wxCommandEvent )
 
 289 wxDEFINE_EVENT( wxEVT_WEBKIT_STATE_CHANGED, wxWebKitStateChangedEvent );
 
 291 wxWebKitStateChangedEvent::wxWebKitStateChangedEvent( wxWindow* win )
 
 293     SetEventType( wxEVT_WEBKIT_STATE_CHANGED);
 
 296         SetEventObject( win );
 
 301 IMPLEMENT_DYNAMIC_CLASS( wxWebKitBeforeLoadEvent, wxCommandEvent )
 
 303 wxDEFINE_EVENT( wxEVT_WEBKIT_BEFORE_LOAD, wxWebKitBeforeLoadEvent );
 
 305 wxWebKitBeforeLoadEvent::wxWebKitBeforeLoadEvent( wxWindow* win )
 
 308     SetEventType( wxEVT_WEBKIT_BEFORE_LOAD);
 
 311         SetEventObject( win );
 
 317 IMPLEMENT_DYNAMIC_CLASS( wxWebKitNewWindowEvent, wxCommandEvent )
 
 319 wxDEFINE_EVENT( wxEVT_WEBKIT_NEW_WINDOW, wxWebKitNewWindowEvent );
 
 321 wxWebKitNewWindowEvent::wxWebKitNewWindowEvent( wxWindow* win )
 
 323     SetEventType( wxEVT_WEBKIT_NEW_WINDOW);
 
 326         SetEventObject( win );
 
 333 //---------------------------------------------------------
 
 334 // helper functions for NSString<->wxString conversion
 
 335 //---------------------------------------------------------
 
 337 inline wxString wxStringWithNSString(NSString *nsstring)
 
 340     return wxString([nsstring UTF8String], wxConvUTF8);
 
 342     return wxString([nsstring lossyCString]);
 
 343 #endif // wxUSE_UNICODE
 
 346 inline NSString* wxNSStringWithWxString(const wxString &wxstring)
 
 349     return [NSString stringWithUTF8String: wxstring.mb_str(wxConvUTF8)];
 
 351     return [NSString stringWithCString: wxstring.c_str() length:wxstring.Len()];
 
 352 #endif // wxUSE_UNICODE
 
 355 inline int wxNavTypeFromWebNavType(int type){
 
 356     if (type == WebNavigationTypeLinkClicked)
 
 357         return wxWEBKIT_NAV_LINK_CLICKED;
 
 359     if (type == WebNavigationTypeFormSubmitted)
 
 360         return wxWEBKIT_NAV_FORM_SUBMITTED;
 
 362     if (type == WebNavigationTypeBackForward)
 
 363         return wxWEBKIT_NAV_BACK_NEXT;
 
 365     if (type == WebNavigationTypeReload)
 
 366         return wxWEBKIT_NAV_RELOAD;
 
 368     if (type == WebNavigationTypeFormResubmitted)
 
 369         return wxWEBKIT_NAV_FORM_RESUBMITTED;
 
 371     return wxWEBKIT_NAV_OTHER;
 
 374 @interface MyFrameLoadMonitor : NSObject
 
 376     wxWebKitCtrl* webKitWindow;
 
 379 - initWithWxWindow: (wxWebKitCtrl*)inWindow;
 
 383 @interface MyPolicyDelegate : NSObject
 
 385     wxWebKitCtrl* webKitWindow;
 
 388 - initWithWxWindow: (wxWebKitCtrl*)inWindow;
 
 392 @interface MyUIDelegate : NSObject
 
 394     wxWebKitCtrl* webKitWindow;
 
 397 - initWithWxWindow: (wxWebKitCtrl*)inWindow;
 
 401 // ----------------------------------------------------------------------------
 
 402 // creation/destruction
 
 403 // ----------------------------------------------------------------------------
 
 405 bool wxWebKitCtrl::Create(wxWindow *parent,
 
 407                                  const wxString& strURL,
 
 409                                  const wxSize& size, long style,
 
 410                                  const wxValidator& validator,
 
 411                                  const wxString& name)
 
 413     m_currentURL = strURL;
 
 414     //m_pageTitle = _("Untitled Page");
 
 416  //still needed for wxCocoa??
 
 420     if (size.x == wxDefaultCoord || size.y == wxDefaultCoord)
 
 422         m_parent->GetClientSize(&width, &height);
 
 423         sizeInstance.x = width;
 
 424         sizeInstance.y = height;
 
 428         sizeInstance.x = size.x;
 
 429         sizeInstance.y = size.y;
 
 432     // now create and attach WebKit view...
 
 435     wxControl::Create(parent, m_windowID, pos, sizeInstance, style , validator , name);
 
 436     SetSize(pos.x, pos.y, sizeInstance.x, sizeInstance.y);
 
 438     wxTopLevelWindowCocoa *topWin = wxDynamicCast(this, wxTopLevelWindowCocoa);
 
 439     NSWindow* nsWin = topWin->GetNSWindow();
 
 441     rect.origin.x = pos.x;
 
 442     rect.origin.y = pos.y;
 
 443     rect.size.width = sizeInstance.x;
 
 444     rect.size.height = sizeInstance.y;
 
 445     m_webView = (WebView*)[[WebView alloc] initWithFrame:rect frameName:@"webkitFrame" groupName:@"webkitGroup"];
 
 446     SetNSView(m_webView);
 
 447     [m_cocoaNSView release];
 
 449     if(m_parent) m_parent->CocoaAddChild(this);
 
 450     SetInitialFrameRect(pos,sizeInstance);
 
 453     wxControl::Create(parent, winID, pos, size, style , validator , name);
 
 455     wxMacControl* peer = new wxMacControl(this);
 
 457     HIWebViewCreate( peer->GetControlRefAddr() );
 
 459     m_webView = (WebView*) HIWebViewGetWebView( peer->GetControlRef() );
 
 461 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
 
 462     if ( UMAGetSystemVersion() >= 0x1030 )
 
 463         HIViewChangeFeatures( peer->GetControlRef() , kHIViewIsOpaque , 0 ) ;
 
 465     InstallControlEventHandler( peer->GetControlRef() , GetwxWebKitCtrlEventHandlerUPP(),
 
 466         GetEventTypeCount(eventList), eventList, this,
 
 467         (EventHandlerRef *)&m_webKitCtrlEventHandler);
 
 471     NSRect r = wxOSXGetFrameForControl( this, pos , size ) ;
 
 472     m_webView = [[WebView alloc] initWithFrame:r frameName:@"webkitFrame" groupName:@"webkitGroup"];
 
 474     SetPeer(new wxWidgetCocoaImpl( this, m_webView ));
 
 476     MacPostControlCreate(pos, size);
 
 478     HIViewSetVisible( GetPeer()->GetControlRef(), true );
 
 480     [m_webView setHidden:false];
 
 484     // Register event listener interfaces
 
 485     MyFrameLoadMonitor* myFrameLoadMonitor = [[MyFrameLoadMonitor alloc] initWithWxWindow: this];
 
 486     [m_webView setFrameLoadDelegate:myFrameLoadMonitor];
 
 488     // this is used to veto page loads, etc.
 
 489     MyPolicyDelegate* myPolicyDelegate = [[MyPolicyDelegate alloc] initWithWxWindow: this];
 
 490     [m_webView setPolicyDelegate:myPolicyDelegate];
 
 492     // this is used to provide printing support for JavaScript
 
 493     MyUIDelegate* myUIDelegate = [[MyUIDelegate alloc] initWithWxWindow: this];
 
 494     [m_webView setUIDelegate:myUIDelegate];
 
 496     LoadURL(m_currentURL);
 
 500 wxWebKitCtrl::~wxWebKitCtrl()
 
 502     MyFrameLoadMonitor* myFrameLoadMonitor = [m_webView frameLoadDelegate];
 
 503     MyPolicyDelegate* myPolicyDelegate = [m_webView policyDelegate];
 
 504     MyUIDelegate* myUIDelegate = [m_webView UIDelegate];
 
 505     [m_webView setFrameLoadDelegate: nil];
 
 506     [m_webView setPolicyDelegate: nil];
 
 507     [m_webView setUIDelegate: nil];
 
 509     if (myFrameLoadMonitor)
 
 510         [myFrameLoadMonitor release];
 
 512     if (myPolicyDelegate)
 
 513         [myPolicyDelegate release];
 
 516         [myUIDelegate release];
 
 519 // ----------------------------------------------------------------------------
 
 521 // ----------------------------------------------------------------------------
 
 523 void wxWebKitCtrl::LoadURL(const wxString &url)
 
 528     [[m_webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:wxNSStringWithWxString(url)]]];
 
 533 bool wxWebKitCtrl::CanGoBack(){
 
 537     return [m_webView canGoBack];
 
 540 bool wxWebKitCtrl::CanGoForward(){
 
 544     return [m_webView canGoForward];
 
 547 bool wxWebKitCtrl::GoBack(){
 
 551     bool result = [(WebView*)m_webView goBack];
 
 555 bool wxWebKitCtrl::GoForward(){
 
 559     bool result = [(WebView*)m_webView goForward];
 
 563 void wxWebKitCtrl::Reload(){
 
 567     [[m_webView mainFrame] reload];
 
 570 void wxWebKitCtrl::Stop(){
 
 574     [[m_webView mainFrame] stopLoading];
 
 577 bool wxWebKitCtrl::CanGetPageSource(){
 
 581     WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
 
 582     return ( [[dataSource representation] canProvideDocumentSource] );
 
 585 wxString wxWebKitCtrl::GetPageSource(){
 
 587     if (CanGetPageSource()){
 
 588         WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
 
 589         return wxStringWithNSString( [[dataSource representation] documentSource] );
 
 592     return wxEmptyString;
 
 595 wxString wxWebKitCtrl::GetSelection(){
 
 597         return wxEmptyString;
 
 599     NSString* selectedText = [[m_webView selectedDOMRange] toString];
 
 600     return wxStringWithNSString( selectedText );
 
 603 bool wxWebKitCtrl::CanIncreaseTextSize(){
 
 607     if ([m_webView canMakeTextLarger])
 
 613 void wxWebKitCtrl::IncreaseTextSize(){
 
 617     if (CanIncreaseTextSize())
 
 618         [m_webView makeTextLarger:(WebView*)m_webView];
 
 621 bool wxWebKitCtrl::CanDecreaseTextSize(){
 
 625     if ([m_webView canMakeTextSmaller])
 
 631 void wxWebKitCtrl::DecreaseTextSize(){
 
 635     if (CanDecreaseTextSize())
 
 636         [m_webView makeTextSmaller:(WebView*)m_webView];
 
 639 void wxWebKitCtrl::SetPageSource(const wxString& source, const wxString& baseUrl){
 
 643     [[m_webView mainFrame] loadHTMLString:(NSString*)wxNSStringWithWxString( source ) baseURL:[NSURL URLWithString:wxNSStringWithWxString( baseUrl )]];
 
 647 void wxWebKitCtrl::Print(bool showPrompt){
 
 651     id view = [[[m_webView mainFrame] frameView] documentView];
 
 652     NSPrintOperation *op = [NSPrintOperation printOperationWithView:view printInfo: [NSPrintInfo sharedPrintInfo]];
 
 654         [op setShowsPrintPanel: showPrompt];
 
 655         // in my tests, the progress bar always freezes and it stops the whole print operation.
 
 656         // do not turn this to true unless there is a workaround for the bug.
 
 657         [op setShowsProgressPanel: false];
 
 663 void wxWebKitCtrl::MakeEditable(bool enable){
 
 667     [m_webView setEditable:enable ];
 
 670 bool wxWebKitCtrl::IsEditable(){
 
 674     return [m_webView isEditable];
 
 677 int wxWebKitCtrl::GetScrollPos(){
 
 678     id result = [[m_webView windowScriptObject] evaluateWebScript:@"document.body.scrollTop"];
 
 679     return [result intValue];
 
 682 void wxWebKitCtrl::SetScrollPos(int pos){
 
 687     javascript.Printf(wxT("document.body.scrollTop = %d;"), pos);
 
 688     [[m_webView windowScriptObject] evaluateWebScript:(NSString*)wxNSStringWithWxString( javascript )];
 
 691 wxString wxWebKitCtrl::RunScript(const wxString& javascript){
 
 693         return wxEmptyString;
 
 695     id result = [[m_webView windowScriptObject] evaluateWebScript:(NSString*)wxNSStringWithWxString( javascript )];
 
 697     NSString* resultAsString;
 
 698     if ([result isKindOfClass:[NSNumber class]]){
 
 699         // __NSCFBoolean is a subclass of NSNumber
 
 700         if (strcmp([result objCType], @encode(BOOL)) == 0){
 
 701             if ([result boolValue])
 
 702                 resultAsString = @"true";
 
 704                 resultAsString = @"false";
 
 707             resultAsString = [NSString stringWithFormat:@"%@", result];
 
 709     else if ([result isKindOfClass:[NSString class]])
 
 710         resultAsString = result;
 
 711     else if ([result isKindOfClass:[WebScriptObject class]])
 
 712         resultAsString = [result stringRepresentation];
 
 714         return wxString(); // This can happen, see e.g. #12361.
 
 716     return wxStringWithNSString( resultAsString );
 
 719 void wxWebKitCtrl::OnSize(wxSizeEvent &event){
 
 720 #if defined(__WXMAC__) && wxOSX_USE_CARBON
 
 721     // This is a nasty hack because WebKit seems to lose its position when it is embedded
 
 722     // in a control that is not itself the content view for a TLW.
 
 723     // I put it in OnSize because these calcs are not perfect, and in fact are basically
 
 724     // guesses based on reverse engineering, so it's best to give people the option of
 
 725     // overriding OnSize with their own calcs if need be.
 
 726     // I also left some test debugging print statements as a convenience if a(nother)
 
 729     wxWindow* tlw = MacGetTopLevelWindow();
 
 731     NSRect frame = [(WebView*)m_webView frame];
 
 732     NSRect bounds = [(WebView*)m_webView bounds];
 
 734 #if DEBUG_WEBKIT_SIZING
 
 735     fprintf(stderr,"Carbon window x=%d, y=%d, width=%d, height=%d\n", GetPosition().x, GetPosition().y, GetSize().x, GetSize().y);
 
 736     fprintf(stderr, "Cocoa window frame x=%G, y=%G, width=%G, height=%G\n", frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
 
 737     fprintf(stderr, "Cocoa window bounds x=%G, y=%G, width=%G, height=%G\n", bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height);
 
 740     // This must be the case that Apple tested with, because well, in this one case
 
 741     // we don't need to do anything! It just works. ;)
 
 742     if (GetParent() == tlw){
 
 746     // since we no longer use parent coordinates, we always want 0,0.
 
 754 #if DEBUG_WEBKIT_SIZING
 
 755     printf("Before conversion, origin is: x = %d, y = %d\n", x, y);
 
 758     // NB: In most cases, when calling HIViewConvertRect, what people want is to use GetRootControl(),
 
 759     // and this tripped me up at first. But in fact, what we want is the root view, because we need to
 
 760     // make the y origin relative to the very top of the window, not its contents, since we later flip
 
 761     // the y coordinate for Cocoa.
 
 762     HIViewConvertRect (&rect, GetPeer()->GetControlRef(),
 
 763                                 HIViewGetRoot( (WindowRef) MacGetTopLevelWindowRef() ) );
 
 765     x = (int)rect.origin.x;
 
 766     y = (int)rect.origin.y;
 
 768 #if DEBUG_WEBKIT_SIZING
 
 769     printf("Moving Cocoa frame origin to: x = %d, y = %d\n", x, y);
 
 773         //flip the y coordinate to convert to Cocoa coordinates
 
 774         y = tlw->GetSize().y - ((GetSize().y) + y);
 
 777 #if DEBUG_WEBKIT_SIZING
 
 778     printf("y = %d after flipping value\n", y);
 
 783     [(WebView*)m_webView setFrame:frame];
 
 786         [(WebView*)m_webView display];
 
 791 void wxWebKitCtrl::MacVisibilityChanged(){
 
 792 #if defined(__WXMAC__) && wxOSX_USE_CARBON
 
 793     bool isHidden = !IsControlVisible( GetPeer()->GetControlRef());
 
 795         [(WebView*)m_webView display];
 
 797     [m_webView setHidden:isHidden];
 
 801 //------------------------------------------------------------
 
 802 // Listener interfaces
 
 803 //------------------------------------------------------------
 
 805 // NB: I'm still tracking this down, but it appears the Cocoa window
 
 806 // still has these events fired on it while the Carbon control is being
 
 807 // destroyed. Therefore, we must be careful to check both the existence
 
 808 // of the Carbon control and the event handler before firing events.
 
 810 @implementation MyFrameLoadMonitor
 
 812 - initWithWxWindow: (wxWebKitCtrl*)inWindow
 
 815     webKitWindow = inWindow;    // non retained
 
 819 - (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame
 
 821     if (webKitWindow && frame == [sender mainFrame]){
 
 822         NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
 
 823         wxWebKitStateChangedEvent thisEvent(webKitWindow);
 
 824         thisEvent.SetState(wxWEBKIT_STATE_NEGOTIATING);
 
 825         thisEvent.SetURL( wxStringWithNSString( url ) );
 
 826         if (webKitWindow->GetEventHandler())
 
 827             webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
 
 831 - (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame
 
 833     if (webKitWindow && frame == [sender mainFrame]){
 
 834         NSString *url = [[[[frame dataSource] request] URL] absoluteString];
 
 835         wxWebKitStateChangedEvent thisEvent(webKitWindow);
 
 836         thisEvent.SetState(wxWEBKIT_STATE_TRANSFERRING);
 
 837         thisEvent.SetURL( wxStringWithNSString( url ) );
 
 838         if (webKitWindow->GetEventHandler())
 
 839             webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
 
 843 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
 
 845     if (webKitWindow && frame == [sender mainFrame]){
 
 846         NSString *url = [[[[frame dataSource] request] URL] absoluteString];
 
 847         wxWebKitStateChangedEvent thisEvent(webKitWindow);
 
 848         thisEvent.SetState(wxWEBKIT_STATE_STOP);
 
 849         thisEvent.SetURL( wxStringWithNSString( url ) );
 
 850         if (webKitWindow->GetEventHandler())
 
 851             webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
 
 855 - (void)webView:(WebView *)sender didFailLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
 
 859     if (webKitWindow && frame == [sender mainFrame]){
 
 860         NSString *url = [[[[frame dataSource] request] URL] absoluteString];
 
 861         wxWebKitStateChangedEvent thisEvent(webKitWindow);
 
 862         thisEvent.SetState(wxWEBKIT_STATE_FAILED);
 
 863         thisEvent.SetURL( wxStringWithNSString( url ) );
 
 864         if (webKitWindow->GetEventHandler())
 
 865             webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
 
 869 - (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
 
 873     if (webKitWindow && frame == [sender mainFrame]){
 
 874         NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
 
 875         wxWebKitStateChangedEvent thisEvent(webKitWindow);
 
 876         thisEvent.SetState(wxWEBKIT_STATE_FAILED);
 
 877         thisEvent.SetURL( wxStringWithNSString( url ) );
 
 878         if (webKitWindow->GetEventHandler())
 
 879             webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
 
 883 - (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
 
 885     if (webKitWindow && frame == [sender mainFrame]){
 
 886         webKitWindow->SetPageTitle(wxStringWithNSString( title ));
 
 891 @implementation MyPolicyDelegate
 
 893 - initWithWxWindow: (wxWebKitCtrl*)inWindow
 
 896     webKitWindow = inWindow;    // non retained
 
 900 - (void)webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener
 
 905     wxWebKitBeforeLoadEvent thisEvent(webKitWindow);
 
 907     // Get the navigation type.
 
 908     NSNumber *n = [actionInformation objectForKey:WebActionNavigationTypeKey];
 
 909     int actionType = [n intValue];
 
 910     thisEvent.SetNavigationType( wxNavTypeFromWebNavType(actionType) );
 
 912     NSString *url = [[request URL] absoluteString];
 
 913     thisEvent.SetURL( wxStringWithNSString( url ) );
 
 915     if (webKitWindow && webKitWindow->GetEventHandler())
 
 916         webKitWindow->GetEventHandler()->ProcessEvent(thisEvent);
 
 918     if (thisEvent.IsCancelled())
 
 924 - (void)webView:(WebView *)sender decidePolicyForNewWindowAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request newFrameName:(NSString *)frameName decisionListener:(id < WebPolicyDecisionListener >)listener
 
 927     wxUnusedVar(actionInformation);
 
 928     wxWebKitNewWindowEvent thisEvent(webKitWindow);
 
 930     NSString *url = [[request URL] absoluteString];
 
 931     thisEvent.SetURL( wxStringWithNSString( url ) );
 
 932     thisEvent.SetTargetName( wxStringWithNSString( frameName ) );
 
 934     if (webKitWindow && webKitWindow->GetEventHandler())
 
 935         webKitWindow->GetEventHandler()->ProcessEvent(thisEvent);
 
 942 @implementation MyUIDelegate
 
 944 - initWithWxWindow: (wxWebKitCtrl*)inWindow
 
 947     webKitWindow = inWindow;    // non retained
 
 951 - (void)webView:(WebView *)sender printFrameView:(WebFrameView *)frameView
 
 954     wxUnusedVar(frameView);
 
 956     webKitWindow->Print(true);
 
 960 #endif //wxUSE_WEBKIT