1 /////////////////////////////////////////////////////////////////////////////
 
   2 // Name:        src/html/htmlctrl/webkit/webkit.mm
 
   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 // For compilers that support precompilation, includes "wx.h".
 
  13 #include "wx/wxprec.h"
 
  14 #include "wx/splitter.h"
 
  23 #include "wx/cocoa/autorelease.h"
 
  25 #include "wx/osx/private.h"
 
  27 #include <WebKit/WebKit.h>
 
  28 #include <WebKit/HIWebView.h>
 
  29 #include <WebKit/CarbonUtils.h>
 
  32 #include "wx/html/webkit.h"
 
  34 #define DEBUG_WEBKIT_SIZING 0
 
  36 extern WXDLLEXPORT_DATA(const char) wxWebKitCtrlNameStr[] = "webkitctrl";
 
  38 // ----------------------------------------------------------------------------
 
  40 // ----------------------------------------------------------------------------
 
  42 IMPLEMENT_DYNAMIC_CLASS(wxWebKitCtrl, wxControl)
 
  44 BEGIN_EVENT_TABLE(wxWebKitCtrl, wxControl)
 
  45 #if defined(__WXMAC__) && wxOSX_USE_CARBON
 
  46     EVT_SIZE(wxWebKitCtrl::OnSize)
 
  50 #if defined(__WXOSX__) && wxOSX_USE_CARBON
 
  52 // ----------------------------------------------------------------------------
 
  53 // Carbon Events handlers
 
  54 // ----------------------------------------------------------------------------
 
  56 // prototype for function in src/osx/carbon/nonownedwnd.cpp
 
  57 void SetupMouseEvent( wxMouseEvent &wxevent , wxMacCarbonEvent &cEvent );
 
  59 static const EventTypeSpec eventList[] =
 
  61     //{ kEventClassControl, kEventControlTrack } ,
 
  62     { kEventClassMouse, kEventMouseUp },
 
  63     { kEventClassMouse, kEventMouseDown },
 
  64     { kEventClassMouse, kEventMouseMoved },
 
  65     { kEventClassMouse, kEventMouseDragged },
 
  67     { kEventClassKeyboard, kEventRawKeyDown } ,
 
  68     { kEventClassKeyboard, kEventRawKeyRepeat } ,
 
  69     { kEventClassKeyboard, kEventRawKeyUp } ,
 
  70     { kEventClassKeyboard, kEventRawKeyModifiersChanged } ,
 
  72     { kEventClassTextInput, kEventTextInputUnicodeForKeyEvent } ,
 
  73     { kEventClassTextInput, kEventTextInputUpdateActiveInputArea } ,
 
  75 #if DEBUG_WEBKIT_SIZING == 1
 
  76     { kEventClassControl, kEventControlBoundsChanged } ,
 
  80 // mix this in from window.cpp
 
  81 pascal OSStatus wxMacUnicodeTextEventHandler( EventHandlerCallRef handler , EventRef event , void *data ) ;
 
  83 // NOTE: This is mostly taken from KeyboardEventHandler in toplevel.cpp, but
 
  84 // that expects the data pointer is a top-level window, so I needed to change
 
  85 // that in this case. However, once 2.8 is out, we should factor out the common logic
 
  86 // among the two functions and merge them.
 
  87 static pascal OSStatus wxWebKitKeyEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
 
  89     OSStatus result = eventNotHandledErr ;
 
  90     wxMacCarbonEvent cEvent( event ) ;
 
  92     wxWebKitCtrl* thisWindow = (wxWebKitCtrl*) data ;
 
  93     wxWindow* focus = thisWindow ;
 
  95     unsigned char charCode ;
 
 103     UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ;
 
 106     ByteCount dataSize = 0 ;
 
 107     if ( GetEventParameter( event, kEventParamKeyUnicodes, typeUnicodeText, NULL, 0 , &dataSize, NULL ) == noErr )
 
 110         int numChars = dataSize / sizeof( UniChar) + 1;
 
 112         UniChar* charBuf = buf ;
 
 114         if ( numChars * 2 > 4 )
 
 115             charBuf = new UniChar[ numChars ] ;
 
 116         GetEventParameter( event, kEventParamKeyUnicodes, typeUnicodeText, NULL, dataSize , NULL , charBuf ) ;
 
 117         charBuf[ numChars - 1 ] = 0;
 
 119 #if SIZEOF_WCHAR_T == 2
 
 120         uniChar = charBuf[0] ;
 
 122         wxMBConvUTF16 converter ;
 
 123         converter.MB2WC( uniChar , (const char*)charBuf , 2 ) ;
 
 126         if ( numChars * 2 > 4 )
 
 131     GetEventParameter( event, kEventParamKeyMacCharCodes, typeChar, NULL, sizeof(char), NULL, &charCode );
 
 132     GetEventParameter( event, kEventParamKeyCode, typeUInt32, NULL, sizeof(UInt32), NULL, &keyCode );
 
 133     GetEventParameter( event, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(UInt32), NULL, &modifiers );
 
 134     GetEventParameter( event, kEventParamMouseLocation, typeQDPoint, NULL, sizeof(Point), NULL, &point );
 
 136     UInt32 message = (keyCode << 8) + charCode;
 
 137     switch ( GetEventKind( event ) )
 
 139         case kEventRawKeyRepeat :
 
 140         case kEventRawKeyDown :
 
 142                 WXEVENTREF formerEvent = wxTheApp->MacGetCurrentEvent() ;
 
 143                 WXEVENTHANDLERCALLREF formerHandler = wxTheApp->MacGetCurrentEventHandlerCallRef() ;
 
 144                 wxTheApp->MacSetCurrentEvent( event , handler ) ;
 
 145                 if ( /* focus && */ wxTheApp->MacSendKeyDownEvent(
 
 146                     focus , message , modifiers , when , point.h , point.v , uniChar[0] ) )
 
 150                 wxTheApp->MacSetCurrentEvent( formerEvent , formerHandler ) ;
 
 154         case kEventRawKeyUp :
 
 155             if ( /* focus && */ wxTheApp->MacSendKeyUpEvent(
 
 156                 focus , message , modifiers , when , point.h , point.v , uniChar[0] ) )
 
 162         case kEventRawKeyModifiersChanged :
 
 164                 wxKeyEvent event(wxEVT_KEY_DOWN);
 
 166                 event.m_shiftDown = modifiers & shiftKey;
 
 167                 event.m_controlDown = modifiers & controlKey;
 
 168                 event.m_altDown = modifiers & optionKey;
 
 169                 event.m_metaDown = modifiers & cmdKey;
 
 174                 event.m_uniChar = uniChar[0] ;
 
 177                 event.SetTimestamp(when);
 
 178                 event.SetEventObject(focus);
 
 180                 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & controlKey )
 
 182                     event.m_keyCode = WXK_CONTROL ;
 
 183                     event.SetEventType( ( modifiers & controlKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
 
 184                     focus->GetEventHandler()->ProcessEvent( event ) ;
 
 186                 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & shiftKey )
 
 188                     event.m_keyCode = WXK_SHIFT ;
 
 189                     event.SetEventType( ( modifiers & shiftKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
 
 190                     focus->GetEventHandler()->ProcessEvent( event ) ;
 
 192                 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & optionKey )
 
 194                     event.m_keyCode = WXK_ALT ;
 
 195                     event.SetEventType( ( modifiers & optionKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
 
 196                     focus->GetEventHandler()->ProcessEvent( event ) ;
 
 198                 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & cmdKey )
 
 200                     event.m_keyCode = WXK_COMMAND ;
 
 201                     event.SetEventType( ( modifiers & cmdKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
 
 202                     focus->GetEventHandler()->ProcessEvent( event ) ;
 
 205                 wxApp::s_lastModifiers = modifiers ;
 
 216 static pascal OSStatus wxWebKitCtrlEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
 
 218     OSStatus result = eventNotHandledErr ;
 
 220     wxMacCarbonEvent cEvent( event ) ;
 
 222     ControlRef controlRef ;
 
 223     wxWebKitCtrl* thisWindow = (wxWebKitCtrl*) data ;
 
 224     wxNonOwnedWindow* tlw = NULL;
 
 226         tlw = thisWindow->MacGetTopLevelWindow();
 
 228     cEvent.GetParameter( kEventParamDirectObject , &controlRef ) ;
 
 230     wxWindow* currentMouseWindow = thisWindow ;
 
 232     if ( wxApp::s_captureWindow )
 
 233         currentMouseWindow = wxApp::s_captureWindow;
 
 235     switch ( GetEventClass( event ) )
 
 237         case kEventClassKeyboard:
 
 239             result = wxWebKitKeyEventHandler(handler, event, data);
 
 243         case kEventClassTextInput:
 
 245             result = wxMacUnicodeTextEventHandler(handler, event, data);
 
 249         case kEventClassMouse:
 
 251             switch ( GetEventKind( event ) )
 
 253                 case kEventMouseDragged :
 
 254                 case kEventMouseMoved :
 
 255                 case kEventMouseDown :
 
 258                     wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
 
 259                     SetupMouseEvent( wxevent , cEvent ) ;
 
 261                     currentMouseWindow->ScreenToClient( &wxevent.m_x , &wxevent.m_y ) ;
 
 262                     wxevent.SetEventObject( currentMouseWindow ) ;
 
 263                     wxevent.SetId( currentMouseWindow->GetId() ) ;
 
 265                     if ( currentMouseWindow->GetEventHandler()->ProcessEvent(wxevent) )
 
 270                     break; // this should enable WebKit to fire mouse dragged and mouse up events...
 
 280     result = CallNextEventHandler(handler, event);
 
 284 DEFINE_ONE_SHOT_HANDLER_GETTER( wxWebKitCtrlEventHandler )
 
 288 // ----------------------------------------------------------------------------
 
 290 // ----------------------------------------------------------------------------
 
 292 IMPLEMENT_DYNAMIC_CLASS( wxWebKitStateChangedEvent, wxCommandEvent )
 
 294 wxDEFINE_EVENT( wxEVT_WEBKIT_STATE_CHANGED, wxWebKitStateChangedEvent );
 
 296 wxWebKitStateChangedEvent::wxWebKitStateChangedEvent( wxWindow* win )
 
 298     SetEventType( wxEVT_WEBKIT_STATE_CHANGED);
 
 301         SetEventObject( win );
 
 306 IMPLEMENT_DYNAMIC_CLASS( wxWebKitBeforeLoadEvent, wxCommandEvent )
 
 308 wxDEFINE_EVENT( wxEVT_WEBKIT_BEFORE_LOAD, wxWebKitBeforeLoadEvent );
 
 310 wxWebKitBeforeLoadEvent::wxWebKitBeforeLoadEvent( wxWindow* win )
 
 313     SetEventType( wxEVT_WEBKIT_BEFORE_LOAD);
 
 316         SetEventObject( win );
 
 322 IMPLEMENT_DYNAMIC_CLASS( wxWebKitNewWindowEvent, wxCommandEvent )
 
 324 wxDEFINE_EVENT( wxEVT_WEBKIT_NEW_WINDOW, wxWebKitNewWindowEvent );
 
 326 wxWebKitNewWindowEvent::wxWebKitNewWindowEvent( wxWindow* win )
 
 328     SetEventType( wxEVT_WEBKIT_NEW_WINDOW);
 
 331         SetEventObject( win );
 
 338 //---------------------------------------------------------
 
 339 // helper functions for NSString<->wxString conversion
 
 340 //---------------------------------------------------------
 
 342 inline wxString wxStringWithNSString(NSString *nsstring)
 
 345     return wxString([nsstring UTF8String], wxConvUTF8);
 
 347     return wxString([nsstring lossyCString]);
 
 348 #endif // wxUSE_UNICODE
 
 351 inline NSString* wxNSStringWithWxString(const wxString &wxstring)
 
 354     return [NSString stringWithUTF8String: wxstring.mb_str(wxConvUTF8)];
 
 356     return [NSString stringWithCString: wxstring.c_str() length:wxstring.Len()];
 
 357 #endif // wxUSE_UNICODE
 
 360 inline int wxNavTypeFromWebNavType(int type){
 
 361     if (type == WebNavigationTypeLinkClicked)
 
 362         return wxWEBKIT_NAV_LINK_CLICKED;
 
 364     if (type == WebNavigationTypeFormSubmitted)
 
 365         return wxWEBKIT_NAV_FORM_SUBMITTED;
 
 367     if (type == WebNavigationTypeBackForward)
 
 368         return wxWEBKIT_NAV_BACK_NEXT;
 
 370     if (type == WebNavigationTypeReload)
 
 371         return wxWEBKIT_NAV_RELOAD;
 
 373     if (type == WebNavigationTypeFormResubmitted)
 
 374         return wxWEBKIT_NAV_FORM_RESUBMITTED;
 
 376     return wxWEBKIT_NAV_OTHER;
 
 379 @interface MyFrameLoadMonitor : NSObject
 
 381     wxWebKitCtrl* webKitWindow;
 
 384 - initWithWxWindow: (wxWebKitCtrl*)inWindow;
 
 388 @interface MyPolicyDelegate : NSObject
 
 390     wxWebKitCtrl* webKitWindow;
 
 393 - initWithWxWindow: (wxWebKitCtrl*)inWindow;
 
 397 // ----------------------------------------------------------------------------
 
 398 // creation/destruction
 
 399 // ----------------------------------------------------------------------------
 
 401 bool wxWebKitCtrl::Create(wxWindow *parent,
 
 403                                  const wxString& strURL,
 
 405                                  const wxSize& size, long style,
 
 406                                  const wxValidator& validator,
 
 407                                  const wxString& name)
 
 409     m_currentURL = strURL;
 
 410     //m_pageTitle = _("Untitled Page");
 
 412  //still needed for wxCocoa??
 
 416     if (size.x == wxDefaultCoord || size.y == wxDefaultCoord)
 
 418         m_parent->GetClientSize(&width, &height);
 
 419         sizeInstance.x = width;
 
 420         sizeInstance.y = height;
 
 424         sizeInstance.x = size.x;
 
 425         sizeInstance.y = size.y;
 
 428     // now create and attach WebKit view...
 
 431     wxControl::Create(parent, m_windowID, pos, sizeInstance, style , validator , name);
 
 432     SetSize(pos.x, pos.y, sizeInstance.x, sizeInstance.y);
 
 434     wxTopLevelWindowCocoa *topWin = wxDynamicCast(this, wxTopLevelWindowCocoa);
 
 435     NSWindow* nsWin = topWin->GetNSWindow();
 
 437     rect.origin.x = pos.x;
 
 438     rect.origin.y = pos.y;
 
 439     rect.size.width = sizeInstance.x;
 
 440     rect.size.height = sizeInstance.y;
 
 441     m_webView = (WebView*)[[WebView alloc] initWithFrame:rect frameName:@"webkitFrame" groupName:@"webkitGroup"];
 
 442     SetNSView(m_webView);
 
 443     [m_cocoaNSView release];
 
 445     if(m_parent) m_parent->CocoaAddChild(this);
 
 446     SetInitialFrameRect(pos,sizeInstance);
 
 449     wxControl::Create(parent, winID, pos, size, style , validator , name);
 
 451     wxMacControl* peer = new wxMacControl(this);
 
 453     HIWebViewCreate( peer->GetControlRefAddr() );
 
 455     m_webView = (WebView*) HIWebViewGetWebView( peer->GetControlRef() );
 
 457 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
 
 458     if ( UMAGetSystemVersion() >= 0x1030 )
 
 459         HIViewChangeFeatures( peer->GetControlRef() , kHIViewIsOpaque , 0 ) ;
 
 461     InstallControlEventHandler( peer->GetControlRef() , GetwxWebKitCtrlEventHandlerUPP(),
 
 462         GetEventTypeCount(eventList), eventList, this,
 
 463         (EventHandlerRef *)&m_webKitCtrlEventHandler);
 
 467     NSRect r = wxOSXGetFrameForControl( this, pos , size ) ;
 
 468     m_webView = [[WebView alloc] initWithFrame:r frameName:@"webkitFrame" groupName:@"webkitGroup"];
 
 470     SetPeer(new wxWidgetCocoaImpl( this, m_webView ));
 
 472     MacPostControlCreate(pos, size);
 
 474     HIViewSetVisible( GetPeer()->GetControlRef(), true );
 
 476     [m_webView setHidden:false];
 
 480     // Register event listener interfaces
 
 481     MyFrameLoadMonitor* myFrameLoadMonitor = [[MyFrameLoadMonitor alloc] initWithWxWindow: this];
 
 482     [m_webView setFrameLoadDelegate:myFrameLoadMonitor];
 
 484     // this is used to veto page loads, etc.
 
 485     MyPolicyDelegate* myPolicyDelegate = [[MyPolicyDelegate alloc] initWithWxWindow: this];
 
 486     [m_webView setPolicyDelegate:myPolicyDelegate];
 
 488     LoadURL(m_currentURL);
 
 492 wxWebKitCtrl::~wxWebKitCtrl()
 
 494     MyFrameLoadMonitor* myFrameLoadMonitor = [m_webView frameLoadDelegate];
 
 495     MyPolicyDelegate* myPolicyDelegate = [m_webView policyDelegate];
 
 496     [m_webView setFrameLoadDelegate: nil];
 
 497     [m_webView setPolicyDelegate: nil];
 
 499     if (myFrameLoadMonitor)
 
 500         [myFrameLoadMonitor release];
 
 502     if (myPolicyDelegate)
 
 503         [myPolicyDelegate release];
 
 506 // ----------------------------------------------------------------------------
 
 508 // ----------------------------------------------------------------------------
 
 510 void wxWebKitCtrl::LoadURL(const wxString &url)
 
 515     [[m_webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:wxNSStringWithWxString(url)]]];
 
 520 bool wxWebKitCtrl::CanGoBack(){
 
 524     return [m_webView canGoBack];
 
 527 bool wxWebKitCtrl::CanGoForward(){
 
 531     return [m_webView canGoForward];
 
 534 bool wxWebKitCtrl::GoBack(){
 
 538     bool result = [(WebView*)m_webView goBack];
 
 542 bool wxWebKitCtrl::GoForward(){
 
 546     bool result = [(WebView*)m_webView goForward];
 
 550 void wxWebKitCtrl::Reload(){
 
 554     [[m_webView mainFrame] reload];
 
 557 void wxWebKitCtrl::Stop(){
 
 561     [[m_webView mainFrame] stopLoading];
 
 564 bool wxWebKitCtrl::CanGetPageSource(){
 
 568     WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
 
 569     return ( [[dataSource representation] canProvideDocumentSource] );
 
 572 wxString wxWebKitCtrl::GetPageSource(){
 
 574     if (CanGetPageSource()){
 
 575         WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
 
 576         return wxStringWithNSString( [[dataSource representation] documentSource] );
 
 579     return wxEmptyString;
 
 582 wxString wxWebKitCtrl::GetSelection(){
 
 584         return wxEmptyString;
 
 586     NSString* selectedText = [[m_webView selectedDOMRange] toString];
 
 587     return wxStringWithNSString( selectedText );
 
 590 bool wxWebKitCtrl::CanIncreaseTextSize(){
 
 594     if ([m_webView canMakeTextLarger])
 
 600 void wxWebKitCtrl::IncreaseTextSize(){
 
 604     if (CanIncreaseTextSize())
 
 605         [m_webView makeTextLarger:(WebView*)m_webView];
 
 608 bool wxWebKitCtrl::CanDecreaseTextSize(){
 
 612     if ([m_webView canMakeTextSmaller])
 
 618 void wxWebKitCtrl::DecreaseTextSize(){
 
 622     if (CanDecreaseTextSize())
 
 623         [m_webView makeTextSmaller:(WebView*)m_webView];
 
 626 void wxWebKitCtrl::SetPageSource(const wxString& source, const wxString& baseUrl){
 
 630     [[m_webView mainFrame] loadHTMLString:(NSString*)wxNSStringWithWxString( source ) baseURL:[NSURL URLWithString:wxNSStringWithWxString( baseUrl )]];
 
 634 void wxWebKitCtrl::Print(bool showPrompt){
 
 638     id view = [[[m_webView mainFrame] frameView] documentView];
 
 639     NSPrintOperation *op = [NSPrintOperation printOperationWithView:view printInfo: [NSPrintInfo sharedPrintInfo]];
 
 641         [op setShowsPrintPanel: showPrompt];
 
 642         // in my tests, the progress bar always freezes and it stops the whole print operation.
 
 643         // do not turn this to true unless there is a workaround for the bug.
 
 644         [op setShowsProgressPanel: false];
 
 650 void wxWebKitCtrl::MakeEditable(bool enable){
 
 654     [m_webView setEditable:enable ];
 
 657 bool wxWebKitCtrl::IsEditable(){
 
 661     return [m_webView isEditable];
 
 664 int wxWebKitCtrl::GetScrollPos(){
 
 665     id result = [[m_webView windowScriptObject] evaluateWebScript:@"document.body.scrollTop"];
 
 666     return [result intValue];
 
 669 void wxWebKitCtrl::SetScrollPos(int pos){
 
 674     javascript.Printf(wxT("document.body.scrollTop = %d;"), pos);
 
 675     [[m_webView windowScriptObject] evaluateWebScript:(NSString*)wxNSStringWithWxString( javascript )];
 
 678 wxString wxWebKitCtrl::RunScript(const wxString& javascript){
 
 680         return wxEmptyString;
 
 682     id result = [[m_webView windowScriptObject] evaluateWebScript:(NSString*)wxNSStringWithWxString( javascript )];
 
 684     NSString* resultAsString;
 
 685     NSString* className = NSStringFromClass([result class]);
 
 686     if ([className isEqualToString:@"NSCFNumber"])
 
 687         resultAsString = [NSString stringWithFormat:@"%@", result];
 
 688     else if ([className isEqualToString:@"NSCFString"])
 
 689         resultAsString = result;
 
 690     else if ([className isEqualToString:@"NSCFBoolean"]){
 
 691         if ([result boolValue])
 
 692             resultAsString = @"true";
 
 694             resultAsString = @"false";
 
 696     else if ([className isEqualToString:@"WebScriptObject"])
 
 697         resultAsString = [result stringRepresentation];
 
 699         return wxString(); // This can happen, see e.g. #12361.
 
 701     return wxStringWithNSString( resultAsString );
 
 704 void wxWebKitCtrl::OnSize(wxSizeEvent &event){
 
 705 #if defined(__WXMAC_) && wxOSX_USE_CARBON
 
 706     // This is a nasty hack because WebKit seems to lose its position when it is embedded
 
 707     // in a control that is not itself the content view for a TLW.
 
 708     // I put it in OnSize because these calcs are not perfect, and in fact are basically
 
 709     // guesses based on reverse engineering, so it's best to give people the option of
 
 710     // overriding OnSize with their own calcs if need be.
 
 711     // I also left some test debugging print statements as a convenience if a(nother)
 
 714     wxWindow* tlw = MacGetTopLevelWindow();
 
 716     NSRect frame = [(WebView*)m_webView frame];
 
 717     NSRect bounds = [(WebView*)m_webView bounds];
 
 719 #if DEBUG_WEBKIT_SIZING
 
 720     fprintf(stderr,"Carbon window x=%d, y=%d, width=%d, height=%d\n", GetPosition().x, GetPosition().y, GetSize().x, GetSize().y);
 
 721     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);
 
 722     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);
 
 725     // This must be the case that Apple tested with, because well, in this one case
 
 726     // we don't need to do anything! It just works. ;)
 
 727     if (GetParent() == tlw){
 
 731     // since we no longer use parent coordinates, we always want 0,0.
 
 739 #if DEBUG_WEBKIT_SIZING
 
 740     printf("Before conversion, origin is: x = %d, y = %d\n", x, y);
 
 743     // NB: In most cases, when calling HIViewConvertRect, what people want is to use GetRootControl(),
 
 744     // and this tripped me up at first. But in fact, what we want is the root view, because we need to
 
 745     // make the y origin relative to the very top of the window, not its contents, since we later flip
 
 746     // the y coordinate for Cocoa.
 
 747     HIViewConvertRect (&rect, GetPeer()->GetControlRef(),
 
 748                                 HIViewGetRoot( (WindowRef) MacGetTopLevelWindowRef() ) );
 
 750     x = (int)rect.origin.x;
 
 751     y = (int)rect.origin.y;
 
 753 #if DEBUG_WEBKIT_SIZING
 
 754     printf("Moving Cocoa frame origin to: x = %d, y = %d\n", x, y);
 
 758         //flip the y coordinate to convert to Cocoa coordinates
 
 759         y = tlw->GetSize().y - ((GetSize().y) + y);
 
 762 #if DEBUG_WEBKIT_SIZING
 
 763     printf("y = %d after flipping value\n", y);
 
 768     [(WebView*)m_webView setFrame:frame];
 
 771         [(WebView*)m_webView display];
 
 776 void wxWebKitCtrl::MacVisibilityChanged(){
 
 777 #if defined(__WXMAC__) && wxOSX_USE_CARBON
 
 778     bool isHidden = !IsControlVisible( GetPeer()->GetControlRef());
 
 780         [(WebView*)m_webView display];
 
 782     [m_webView setHidden:isHidden];
 
 786 //------------------------------------------------------------
 
 787 // Listener interfaces
 
 788 //------------------------------------------------------------
 
 790 // NB: I'm still tracking this down, but it appears the Cocoa window
 
 791 // still has these events fired on it while the Carbon control is being
 
 792 // destroyed. Therefore, we must be careful to check both the existence
 
 793 // of the Carbon control and the event handler before firing events.
 
 795 @implementation MyFrameLoadMonitor
 
 797 - initWithWxWindow: (wxWebKitCtrl*)inWindow
 
 800     webKitWindow = inWindow;    // non retained
 
 804 - (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame
 
 806     if (webKitWindow && frame == [sender mainFrame]){
 
 807         NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
 
 808         wxWebKitStateChangedEvent thisEvent(webKitWindow);
 
 809         thisEvent.SetState(wxWEBKIT_STATE_NEGOTIATING);
 
 810         thisEvent.SetURL( wxStringWithNSString( url ) );
 
 811         if (webKitWindow->GetEventHandler())
 
 812             webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
 
 816 - (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame
 
 818     if (webKitWindow && frame == [sender mainFrame]){
 
 819         NSString *url = [[[[frame dataSource] request] URL] absoluteString];
 
 820         wxWebKitStateChangedEvent thisEvent(webKitWindow);
 
 821         thisEvent.SetState(wxWEBKIT_STATE_TRANSFERRING);
 
 822         thisEvent.SetURL( wxStringWithNSString( url ) );
 
 823         if (webKitWindow->GetEventHandler())
 
 824             webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
 
 828 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
 
 830     if (webKitWindow && frame == [sender mainFrame]){
 
 831         NSString *url = [[[[frame dataSource] request] URL] absoluteString];
 
 832         wxWebKitStateChangedEvent thisEvent(webKitWindow);
 
 833         thisEvent.SetState(wxWEBKIT_STATE_STOP);
 
 834         thisEvent.SetURL( wxStringWithNSString( url ) );
 
 835         if (webKitWindow->GetEventHandler())
 
 836             webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
 
 840 - (void)webView:(WebView *)sender didFailLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
 
 844     if (webKitWindow && frame == [sender mainFrame]){
 
 845         NSString *url = [[[[frame dataSource] request] URL] absoluteString];
 
 846         wxWebKitStateChangedEvent thisEvent(webKitWindow);
 
 847         thisEvent.SetState(wxWEBKIT_STATE_FAILED);
 
 848         thisEvent.SetURL( wxStringWithNSString( url ) );
 
 849         if (webKitWindow->GetEventHandler())
 
 850             webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
 
 854 - (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
 
 858     if (webKitWindow && frame == [sender mainFrame]){
 
 859         NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
 
 860         wxWebKitStateChangedEvent thisEvent(webKitWindow);
 
 861         thisEvent.SetState(wxWEBKIT_STATE_FAILED);
 
 862         thisEvent.SetURL( wxStringWithNSString( url ) );
 
 863         if (webKitWindow->GetEventHandler())
 
 864             webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
 
 868 - (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
 
 870     if (webKitWindow && frame == [sender mainFrame]){
 
 871         webKitWindow->SetPageTitle(wxStringWithNSString( title ));
 
 876 @implementation MyPolicyDelegate
 
 878 - initWithWxWindow: (wxWebKitCtrl*)inWindow
 
 881     webKitWindow = inWindow;    // non retained
 
 885 - (void)webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener
 
 890     wxWebKitBeforeLoadEvent thisEvent(webKitWindow);
 
 892     // Get the navigation type.
 
 893     NSNumber *n = [actionInformation objectForKey:WebActionNavigationTypeKey];
 
 894     int actionType = [n intValue];
 
 895     thisEvent.SetNavigationType( wxNavTypeFromWebNavType(actionType) );
 
 897     NSString *url = [[request URL] absoluteString];
 
 898     thisEvent.SetURL( wxStringWithNSString( url ) );
 
 900     if (webKitWindow && webKitWindow->GetEventHandler())
 
 901         webKitWindow->GetEventHandler()->ProcessEvent(thisEvent);
 
 903     if (thisEvent.IsCancelled())
 
 909 - (void)webView:(WebView *)sender decidePolicyForNewWindowAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request newFrameName:(NSString *)frameName decisionListener:(id < WebPolicyDecisionListener >)listener
 
 912     wxUnusedVar(actionInformation);
 
 913     wxWebKitNewWindowEvent thisEvent(webKitWindow);
 
 915     NSString *url = [[request URL] absoluteString];
 
 916     thisEvent.SetURL( wxStringWithNSString( url ) );
 
 917     thisEvent.SetTargetName( wxStringWithNSString( frameName ) );
 
 919     if (webKitWindow && webKitWindow->GetEventHandler())
 
 920         webKitWindow->GetEventHandler()->ProcessEvent(thisEvent);
 
 926 #endif //wxUSE_WEBKIT