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 ;
102 UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ;
105 ByteCount dataSize = 0 ;
106 if ( GetEventParameter( event, kEventParamKeyUnicodes, typeUnicodeText, NULL, 0 , &dataSize, NULL ) == noErr )
109 int numChars = dataSize / sizeof( UniChar) + 1;
111 UniChar* charBuf = buf ;
113 if ( numChars * 2 > 4 )
114 charBuf = new UniChar[ numChars ] ;
115 GetEventParameter( event, kEventParamKeyUnicodes, typeUnicodeText, NULL, dataSize , NULL , charBuf ) ;
116 charBuf[ numChars - 1 ] = 0;
118 #if SIZEOF_WCHAR_T == 2
119 uniChar = charBuf[0] ;
121 wxMBConvUTF16 converter ;
122 converter.MB2WC( uniChar , (const char*)charBuf , 2 ) ;
125 if ( numChars * 2 > 4 )
130 GetEventParameter( event, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode );
131 GetEventParameter( event, kEventParamKeyCode, typeUInt32, NULL, sizeof(UInt32), NULL, &keyCode );
132 GetEventParameter( event, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(UInt32), NULL, &modifiers );
134 UInt32 message = (keyCode << 8) + charCode;
135 switch ( GetEventKind( event ) )
137 case kEventRawKeyRepeat :
138 case kEventRawKeyDown :
140 WXEVENTREF formerEvent = wxTheApp->MacGetCurrentEvent() ;
141 WXEVENTHANDLERCALLREF formerHandler = wxTheApp->MacGetCurrentEventHandlerCallRef() ;
142 wxTheApp->MacSetCurrentEvent( event , handler ) ;
143 if ( /* focus && */ wxTheApp->MacSendKeyDownEvent(
144 focus , message , modifiers , when , uniChar[0] ) )
148 wxTheApp->MacSetCurrentEvent( formerEvent , formerHandler ) ;
152 case kEventRawKeyUp :
153 if ( /* focus && */ wxTheApp->MacSendKeyUpEvent(
154 focus , message , modifiers , when , uniChar[0] ) )
160 case kEventRawKeyModifiersChanged :
162 wxKeyEvent event(wxEVT_KEY_DOWN);
164 event.m_shiftDown = modifiers & shiftKey;
165 event.m_rawControlDown = modifiers & controlKey;
166 event.m_altDown = modifiers & optionKey;
167 event.m_controlDown = modifiers & cmdKey;
170 event.m_uniChar = uniChar[0] ;
173 event.SetTimestamp(when);
174 event.SetEventObject(focus);
176 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & controlKey )
178 event.m_keyCode = WXK_RAW_CONTROL ;
179 event.SetEventType( ( modifiers & controlKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
180 focus->GetEventHandler()->ProcessEvent( event ) ;
182 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & shiftKey )
184 event.m_keyCode = WXK_SHIFT ;
185 event.SetEventType( ( modifiers & shiftKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
186 focus->GetEventHandler()->ProcessEvent( event ) ;
188 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & optionKey )
190 event.m_keyCode = WXK_ALT ;
191 event.SetEventType( ( modifiers & optionKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
192 focus->GetEventHandler()->ProcessEvent( event ) ;
194 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & cmdKey )
196 event.m_keyCode = WXK_CONTROL ;
197 event.SetEventType( ( modifiers & cmdKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
198 focus->GetEventHandler()->ProcessEvent( event ) ;
201 wxApp::s_lastModifiers = modifiers ;
212 static pascal OSStatus wxWebKitCtrlEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
214 OSStatus result = eventNotHandledErr ;
216 wxMacCarbonEvent cEvent( event ) ;
218 ControlRef controlRef ;
219 wxWebKitCtrl* thisWindow = (wxWebKitCtrl*) data ;
220 wxNonOwnedWindow* tlw = NULL;
222 tlw = thisWindow->MacGetTopLevelWindow();
224 cEvent.GetParameter( kEventParamDirectObject , &controlRef ) ;
226 wxWindow* currentMouseWindow = thisWindow ;
228 if ( wxApp::s_captureWindow )
229 currentMouseWindow = wxApp::s_captureWindow;
231 switch ( GetEventClass( event ) )
233 case kEventClassKeyboard:
235 result = wxWebKitKeyEventHandler(handler, event, data);
239 case kEventClassTextInput:
241 result = wxMacUnicodeTextEventHandler(handler, event, data);
245 case kEventClassMouse:
247 switch ( GetEventKind( event ) )
249 case kEventMouseDragged :
250 case kEventMouseMoved :
251 case kEventMouseDown :
254 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
255 SetupMouseEvent( wxevent , cEvent ) ;
257 currentMouseWindow->ScreenToClient( &wxevent.m_x , &wxevent.m_y ) ;
258 wxevent.SetEventObject( currentMouseWindow ) ;
259 wxevent.SetId( currentMouseWindow->GetId() ) ;
261 if ( currentMouseWindow->GetEventHandler()->ProcessEvent(wxevent) )
266 break; // this should enable WebKit to fire mouse dragged and mouse up events...
276 result = CallNextEventHandler(handler, event);
280 DEFINE_ONE_SHOT_HANDLER_GETTER( wxWebKitCtrlEventHandler )
284 // ----------------------------------------------------------------------------
286 // ----------------------------------------------------------------------------
288 IMPLEMENT_DYNAMIC_CLASS( wxWebKitStateChangedEvent, wxCommandEvent )
290 wxDEFINE_EVENT( wxEVT_WEBKIT_STATE_CHANGED, wxWebKitStateChangedEvent );
292 wxWebKitStateChangedEvent::wxWebKitStateChangedEvent( wxWindow* win )
294 SetEventType( wxEVT_WEBKIT_STATE_CHANGED);
297 SetEventObject( win );
302 IMPLEMENT_DYNAMIC_CLASS( wxWebKitBeforeLoadEvent, wxCommandEvent )
304 wxDEFINE_EVENT( wxEVT_WEBKIT_BEFORE_LOAD, wxWebKitBeforeLoadEvent );
306 wxWebKitBeforeLoadEvent::wxWebKitBeforeLoadEvent( wxWindow* win )
309 SetEventType( wxEVT_WEBKIT_BEFORE_LOAD);
312 SetEventObject( win );
318 IMPLEMENT_DYNAMIC_CLASS( wxWebKitNewWindowEvent, wxCommandEvent )
320 wxDEFINE_EVENT( wxEVT_WEBKIT_NEW_WINDOW, wxWebKitNewWindowEvent );
322 wxWebKitNewWindowEvent::wxWebKitNewWindowEvent( wxWindow* win )
324 SetEventType( wxEVT_WEBKIT_NEW_WINDOW);
327 SetEventObject( win );
334 //---------------------------------------------------------
335 // helper functions for NSString<->wxString conversion
336 //---------------------------------------------------------
338 inline wxString wxStringWithNSString(NSString *nsstring)
341 return wxString([nsstring UTF8String], wxConvUTF8);
343 return wxString([nsstring lossyCString]);
344 #endif // wxUSE_UNICODE
347 inline NSString* wxNSStringWithWxString(const wxString &wxstring)
350 return [NSString stringWithUTF8String: wxstring.mb_str(wxConvUTF8)];
352 return [NSString stringWithCString: wxstring.c_str() length:wxstring.Len()];
353 #endif // wxUSE_UNICODE
356 inline int wxNavTypeFromWebNavType(int type){
357 if (type == WebNavigationTypeLinkClicked)
358 return wxWEBKIT_NAV_LINK_CLICKED;
360 if (type == WebNavigationTypeFormSubmitted)
361 return wxWEBKIT_NAV_FORM_SUBMITTED;
363 if (type == WebNavigationTypeBackForward)
364 return wxWEBKIT_NAV_BACK_NEXT;
366 if (type == WebNavigationTypeReload)
367 return wxWEBKIT_NAV_RELOAD;
369 if (type == WebNavigationTypeFormResubmitted)
370 return wxWEBKIT_NAV_FORM_RESUBMITTED;
372 return wxWEBKIT_NAV_OTHER;
375 @interface MyFrameLoadMonitor : NSObject
377 wxWebKitCtrl* webKitWindow;
380 - initWithWxWindow: (wxWebKitCtrl*)inWindow;
384 @interface MyPolicyDelegate : NSObject
386 wxWebKitCtrl* webKitWindow;
389 - initWithWxWindow: (wxWebKitCtrl*)inWindow;
393 @interface MyUIDelegate : NSObject
395 wxWebKitCtrl* webKitWindow;
398 - initWithWxWindow: (wxWebKitCtrl*)inWindow;
402 // ----------------------------------------------------------------------------
403 // creation/destruction
404 // ----------------------------------------------------------------------------
406 bool wxWebKitCtrl::Create(wxWindow *parent,
408 const wxString& strURL,
410 const wxSize& size, long style,
411 const wxValidator& validator,
412 const wxString& name)
414 m_currentURL = strURL;
415 //m_pageTitle = _("Untitled Page");
417 //still needed for wxCocoa??
421 if (size.x == wxDefaultCoord || size.y == wxDefaultCoord)
423 m_parent->GetClientSize(&width, &height);
424 sizeInstance.x = width;
425 sizeInstance.y = height;
429 sizeInstance.x = size.x;
430 sizeInstance.y = size.y;
433 // now create and attach WebKit view...
436 wxControl::Create(parent, m_windowID, pos, sizeInstance, style , validator , name);
437 SetSize(pos.x, pos.y, sizeInstance.x, sizeInstance.y);
439 wxTopLevelWindowCocoa *topWin = wxDynamicCast(this, wxTopLevelWindowCocoa);
440 NSWindow* nsWin = topWin->GetNSWindow();
442 rect.origin.x = pos.x;
443 rect.origin.y = pos.y;
444 rect.size.width = sizeInstance.x;
445 rect.size.height = sizeInstance.y;
446 m_webView = (WebView*)[[WebView alloc] initWithFrame:rect frameName:@"webkitFrame" groupName:@"webkitGroup"];
447 SetNSView(m_webView);
448 [m_cocoaNSView release];
450 if(m_parent) m_parent->CocoaAddChild(this);
451 SetInitialFrameRect(pos,sizeInstance);
454 wxControl::Create(parent, winID, pos, size, style , validator , name);
456 wxMacControl* peer = new wxMacControl(this);
458 HIWebViewCreate( peer->GetControlRefAddr() );
460 m_webView = (WebView*) HIWebViewGetWebView( peer->GetControlRef() );
462 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
463 if ( UMAGetSystemVersion() >= 0x1030 )
464 HIViewChangeFeatures( peer->GetControlRef() , kHIViewIsOpaque , 0 ) ;
466 InstallControlEventHandler( peer->GetControlRef() , GetwxWebKitCtrlEventHandlerUPP(),
467 GetEventTypeCount(eventList), eventList, this,
468 (EventHandlerRef *)&m_webKitCtrlEventHandler);
472 NSRect r = wxOSXGetFrameForControl( this, pos , size ) ;
473 m_webView = [[WebView alloc] initWithFrame:r frameName:@"webkitFrame" groupName:@"webkitGroup"];
475 SetPeer(new wxWidgetCocoaImpl( this, m_webView ));
477 MacPostControlCreate(pos, size);
479 HIViewSetVisible( GetPeer()->GetControlRef(), true );
481 [m_webView setHidden:false];
485 // Register event listener interfaces
486 MyFrameLoadMonitor* myFrameLoadMonitor = [[MyFrameLoadMonitor alloc] initWithWxWindow: this];
487 [m_webView setFrameLoadDelegate:myFrameLoadMonitor];
489 // this is used to veto page loads, etc.
490 MyPolicyDelegate* myPolicyDelegate = [[MyPolicyDelegate alloc] initWithWxWindow: this];
491 [m_webView setPolicyDelegate:myPolicyDelegate];
493 // this is used to provide printing support for JavaScript
494 MyUIDelegate* myUIDelegate = [[MyUIDelegate alloc] initWithWxWindow: this];
495 [m_webView setUIDelegate:myUIDelegate];
497 LoadURL(m_currentURL);
501 wxWebKitCtrl::~wxWebKitCtrl()
503 MyFrameLoadMonitor* myFrameLoadMonitor = [m_webView frameLoadDelegate];
504 MyPolicyDelegate* myPolicyDelegate = [m_webView policyDelegate];
505 MyUIDelegate* myUIDelegate = [m_webView UIDelegate];
506 [m_webView setFrameLoadDelegate: nil];
507 [m_webView setPolicyDelegate: nil];
508 [m_webView setUIDelegate: nil];
510 if (myFrameLoadMonitor)
511 [myFrameLoadMonitor release];
513 if (myPolicyDelegate)
514 [myPolicyDelegate release];
517 [myUIDelegate release];
520 // ----------------------------------------------------------------------------
522 // ----------------------------------------------------------------------------
524 void wxWebKitCtrl::LoadURL(const wxString &url)
529 [[m_webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:wxNSStringWithWxString(url)]]];
534 bool wxWebKitCtrl::CanGoBack(){
538 return [m_webView canGoBack];
541 bool wxWebKitCtrl::CanGoForward(){
545 return [m_webView canGoForward];
548 bool wxWebKitCtrl::GoBack(){
552 bool result = [(WebView*)m_webView goBack];
556 bool wxWebKitCtrl::GoForward(){
560 bool result = [(WebView*)m_webView goForward];
564 void wxWebKitCtrl::Reload(){
568 [[m_webView mainFrame] reload];
571 void wxWebKitCtrl::Stop(){
575 [[m_webView mainFrame] stopLoading];
578 bool wxWebKitCtrl::CanGetPageSource(){
582 WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
583 return ( [[dataSource representation] canProvideDocumentSource] );
586 wxString wxWebKitCtrl::GetPageSource(){
588 if (CanGetPageSource()){
589 WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
590 return wxStringWithNSString( [[dataSource representation] documentSource] );
593 return wxEmptyString;
596 wxString wxWebKitCtrl::GetSelection(){
598 return wxEmptyString;
600 NSString* selectedText = [[m_webView selectedDOMRange] toString];
601 return wxStringWithNSString( selectedText );
604 bool wxWebKitCtrl::CanIncreaseTextSize(){
608 if ([m_webView canMakeTextLarger])
614 void wxWebKitCtrl::IncreaseTextSize(){
618 if (CanIncreaseTextSize())
619 [m_webView makeTextLarger:(WebView*)m_webView];
622 bool wxWebKitCtrl::CanDecreaseTextSize(){
626 if ([m_webView canMakeTextSmaller])
632 void wxWebKitCtrl::DecreaseTextSize(){
636 if (CanDecreaseTextSize())
637 [m_webView makeTextSmaller:(WebView*)m_webView];
640 void wxWebKitCtrl::SetPageSource(const wxString& source, const wxString& baseUrl){
644 [[m_webView mainFrame] loadHTMLString:(NSString*)wxNSStringWithWxString( source ) baseURL:[NSURL URLWithString:wxNSStringWithWxString( baseUrl )]];
648 void wxWebKitCtrl::Print(bool showPrompt){
652 id view = [[[m_webView mainFrame] frameView] documentView];
653 NSPrintOperation *op = [NSPrintOperation printOperationWithView:view printInfo: [NSPrintInfo sharedPrintInfo]];
655 [op setShowsPrintPanel: showPrompt];
656 // in my tests, the progress bar always freezes and it stops the whole print operation.
657 // do not turn this to true unless there is a workaround for the bug.
658 [op setShowsProgressPanel: false];
664 void wxWebKitCtrl::MakeEditable(bool enable){
668 [m_webView setEditable:enable ];
671 bool wxWebKitCtrl::IsEditable(){
675 return [m_webView isEditable];
678 int wxWebKitCtrl::GetScrollPos(){
679 id result = [[m_webView windowScriptObject] evaluateWebScript:@"document.body.scrollTop"];
680 return [result intValue];
683 void wxWebKitCtrl::SetScrollPos(int pos){
688 javascript.Printf(wxT("document.body.scrollTop = %d;"), pos);
689 [[m_webView windowScriptObject] evaluateWebScript:(NSString*)wxNSStringWithWxString( javascript )];
692 wxString wxWebKitCtrl::RunScript(const wxString& javascript){
694 return wxEmptyString;
696 id result = [[m_webView windowScriptObject] evaluateWebScript:(NSString*)wxNSStringWithWxString( javascript )];
698 NSString* resultAsString;
699 if ([result isKindOfClass:[NSNumber class]]){
700 // __NSCFBoolean is a subclass of NSNumber
701 if (strcmp([result objCType], @encode(BOOL)) == 0){
702 if ([result boolValue])
703 resultAsString = @"true";
705 resultAsString = @"false";
708 resultAsString = [NSString stringWithFormat:@"%@", result];
710 else if ([result isKindOfClass:[NSString class]])
711 resultAsString = result;
712 else if ([result isKindOfClass:[WebScriptObject class]])
713 resultAsString = [result stringRepresentation];
715 return wxString(); // This can happen, see e.g. #12361.
717 return wxStringWithNSString( resultAsString );
720 void wxWebKitCtrl::OnSize(wxSizeEvent &event){
721 #if defined(__WXMAC__) && wxOSX_USE_CARBON
722 // This is a nasty hack because WebKit seems to lose its position when it is embedded
723 // in a control that is not itself the content view for a TLW.
724 // I put it in OnSize because these calcs are not perfect, and in fact are basically
725 // guesses based on reverse engineering, so it's best to give people the option of
726 // overriding OnSize with their own calcs if need be.
727 // I also left some test debugging print statements as a convenience if a(nother)
730 wxWindow* tlw = MacGetTopLevelWindow();
732 NSRect frame = [(WebView*)m_webView frame];
733 NSRect bounds = [(WebView*)m_webView bounds];
735 #if DEBUG_WEBKIT_SIZING
736 fprintf(stderr,"Carbon window x=%d, y=%d, width=%d, height=%d\n", GetPosition().x, GetPosition().y, GetSize().x, GetSize().y);
737 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);
738 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);
741 // This must be the case that Apple tested with, because well, in this one case
742 // we don't need to do anything! It just works. ;)
743 if (GetParent() == tlw){
747 // since we no longer use parent coordinates, we always want 0,0.
755 #if DEBUG_WEBKIT_SIZING
756 printf("Before conversion, origin is: x = %d, y = %d\n", x, y);
759 // NB: In most cases, when calling HIViewConvertRect, what people want is to use GetRootControl(),
760 // and this tripped me up at first. But in fact, what we want is the root view, because we need to
761 // make the y origin relative to the very top of the window, not its contents, since we later flip
762 // the y coordinate for Cocoa.
763 HIViewConvertRect (&rect, GetPeer()->GetControlRef(),
764 HIViewGetRoot( (WindowRef) MacGetTopLevelWindowRef() ) );
766 x = (int)rect.origin.x;
767 y = (int)rect.origin.y;
769 #if DEBUG_WEBKIT_SIZING
770 printf("Moving Cocoa frame origin to: x = %d, y = %d\n", x, y);
774 //flip the y coordinate to convert to Cocoa coordinates
775 y = tlw->GetSize().y - ((GetSize().y) + y);
778 #if DEBUG_WEBKIT_SIZING
779 printf("y = %d after flipping value\n", y);
784 [(WebView*)m_webView setFrame:frame];
787 [(WebView*)m_webView display];
792 void wxWebKitCtrl::MacVisibilityChanged(){
793 #if defined(__WXMAC__) && wxOSX_USE_CARBON
794 bool isHidden = !IsControlVisible( GetPeer()->GetControlRef());
796 [(WebView*)m_webView display];
798 [m_webView setHidden:isHidden];
802 //------------------------------------------------------------
803 // Listener interfaces
804 //------------------------------------------------------------
806 // NB: I'm still tracking this down, but it appears the Cocoa window
807 // still has these events fired on it while the Carbon control is being
808 // destroyed. Therefore, we must be careful to check both the existence
809 // of the Carbon control and the event handler before firing events.
811 @implementation MyFrameLoadMonitor
813 - initWithWxWindow: (wxWebKitCtrl*)inWindow
816 webKitWindow = inWindow; // non retained
820 - (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame
822 if (webKitWindow && frame == [sender mainFrame]){
823 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
824 wxWebKitStateChangedEvent thisEvent(webKitWindow);
825 thisEvent.SetState(wxWEBKIT_STATE_NEGOTIATING);
826 thisEvent.SetURL( wxStringWithNSString( url ) );
827 if (webKitWindow->GetEventHandler())
828 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
832 - (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame
834 if (webKitWindow && frame == [sender mainFrame]){
835 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
836 wxWebKitStateChangedEvent thisEvent(webKitWindow);
837 thisEvent.SetState(wxWEBKIT_STATE_TRANSFERRING);
838 thisEvent.SetURL( wxStringWithNSString( url ) );
839 if (webKitWindow->GetEventHandler())
840 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
844 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
846 if (webKitWindow && frame == [sender mainFrame]){
847 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
848 wxWebKitStateChangedEvent thisEvent(webKitWindow);
849 thisEvent.SetState(wxWEBKIT_STATE_STOP);
850 thisEvent.SetURL( wxStringWithNSString( url ) );
851 if (webKitWindow->GetEventHandler())
852 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
856 - (void)webView:(WebView *)sender didFailLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
860 if (webKitWindow && frame == [sender mainFrame]){
861 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
862 wxWebKitStateChangedEvent thisEvent(webKitWindow);
863 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
864 thisEvent.SetURL( wxStringWithNSString( url ) );
865 if (webKitWindow->GetEventHandler())
866 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
870 - (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
874 if (webKitWindow && frame == [sender mainFrame]){
875 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
876 wxWebKitStateChangedEvent thisEvent(webKitWindow);
877 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
878 thisEvent.SetURL( wxStringWithNSString( url ) );
879 if (webKitWindow->GetEventHandler())
880 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
884 - (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
886 if (webKitWindow && frame == [sender mainFrame]){
887 webKitWindow->SetPageTitle(wxStringWithNSString( title ));
892 @implementation MyPolicyDelegate
894 - initWithWxWindow: (wxWebKitCtrl*)inWindow
897 webKitWindow = inWindow; // non retained
901 - (void)webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener
906 wxWebKitBeforeLoadEvent thisEvent(webKitWindow);
908 // Get the navigation type.
909 NSNumber *n = [actionInformation objectForKey:WebActionNavigationTypeKey];
910 int actionType = [n intValue];
911 thisEvent.SetNavigationType( wxNavTypeFromWebNavType(actionType) );
913 NSString *url = [[request URL] absoluteString];
914 thisEvent.SetURL( wxStringWithNSString( url ) );
916 if (webKitWindow && webKitWindow->GetEventHandler())
917 webKitWindow->GetEventHandler()->ProcessEvent(thisEvent);
919 if (thisEvent.IsCancelled())
925 - (void)webView:(WebView *)sender decidePolicyForNewWindowAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request newFrameName:(NSString *)frameName decisionListener:(id < WebPolicyDecisionListener >)listener
928 wxUnusedVar(actionInformation);
929 wxWebKitNewWindowEvent thisEvent(webKitWindow);
931 NSString *url = [[request URL] absoluteString];
932 thisEvent.SetURL( wxStringWithNSString( url ) );
933 thisEvent.SetTargetName( wxStringWithNSString( frameName ) );
935 if (webKitWindow && webKitWindow->GetEventHandler())
936 webKitWindow->GetEventHandler()->ProcessEvent(thisEvent);
943 @implementation MyUIDelegate
945 - initWithWxWindow: (wxWebKitCtrl*)inWindow
948 webKitWindow = inWindow; // non retained
952 - (void)webView:(WebView *)sender printFrameView:(WebFrameView *)frameView
955 wxUnusedVar(frameView);
957 webKitWindow->Print(true);
961 #endif //wxUSE_WEBKIT