1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/webview_webkit.mm
3 // Purpose: wxWebViewWebKit - embeddable web kit control,
4 // OS X implementation of web view component
5 // Author: Jethro Grassie / Kevin Ollivier / Marianne Gagnon
9 // Copyright: (c) Jethro Grassie / Kevin Ollivier / Marianne Gagnon
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // http://developer.apple.com/mac/library/documentation/Cocoa/Reference/WebKit/Classes/WebView_Class/Reference/Reference.html
15 #include "wx/osx/webview_webkit.h"
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
24 //#if wxHAVE_WEB_BACKEND_OSX_WEBKIT
27 #include "wx/cocoa/autorelease.h"
29 #include "wx/osx/private.h"
31 #include <WebKit/WebKit.h>
32 #include <WebKit/HIWebView.h>
33 #include <WebKit/CarbonUtils.h>
36 #include <Foundation/NSURLError.h>
38 // FIXME: find cleaner way to find the wxWidgets ID of a webview than this hack
40 std::map<WebView*, wxWebViewWebKit*> wx_webviewctrls;
42 #define DEBUG_WEBKIT_SIZING 0
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 IMPLEMENT_DYNAMIC_CLASS(wxWebViewWebKit, wxControl)
50 BEGIN_EVENT_TABLE(wxWebViewWebKit, wxControl)
51 #if defined(__WXMAC__) && wxOSX_USE_CARBON
52 EVT_SIZE(wxWebViewWebKit::OnSize)
56 #if defined(__WXOSX__) && wxOSX_USE_CARBON
58 // ----------------------------------------------------------------------------
59 // Carbon Events handlers
60 // ----------------------------------------------------------------------------
62 // prototype for function in src/osx/carbon/nonownedwnd.cpp
63 void SetupMouseEvent( wxMouseEvent &wxevent , wxMacCarbonEvent &cEvent );
65 static const EventTypeSpec eventList[] =
67 //{ kEventClassControl, kEventControlTrack } ,
68 { kEventClassMouse, kEventMouseUp },
69 { kEventClassMouse, kEventMouseDown },
70 { kEventClassMouse, kEventMouseMoved },
71 { kEventClassMouse, kEventMouseDragged },
73 { kEventClassKeyboard, kEventRawKeyDown } ,
74 { kEventClassKeyboard, kEventRawKeyRepeat } ,
75 { kEventClassKeyboard, kEventRawKeyUp } ,
76 { kEventClassKeyboard, kEventRawKeyModifiersChanged } ,
78 { kEventClassTextInput, kEventTextInputUnicodeForKeyEvent } ,
79 { kEventClassTextInput, kEventTextInputUpdateActiveInputArea } ,
81 #if DEBUG_WEBKIT_SIZING == 1
82 { kEventClassControl, kEventControlBoundsChanged } ,
86 // mix this in from window.cpp
87 pascal OSStatus wxMacUnicodeTextEventHandler(EventHandlerCallRef handler,
88 EventRef event, void *data) ;
90 // NOTE: This is mostly taken from KeyboardEventHandler in toplevel.cpp, but
91 // that expects the data pointer is a top-level window, so I needed to change
92 // that in this case. However, once 2.8 is out, we should factor out the common
93 // logic among the two functions and merge them.
94 static pascal OSStatus wxWebKitKeyEventHandler(EventHandlerCallRef handler,
95 EventRef event, void *data)
97 OSStatus result = eventNotHandledErr ;
98 wxMacCarbonEvent cEvent( event ) ;
100 wxWebViewWebKit* thisWindow = (wxWebViewWebKit*) data ;
101 wxWindow* focus = thisWindow ;
103 unsigned char charCode ;
111 UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ;
114 ByteCount dataSize = 0 ;
115 if ( GetEventParameter(event, kEventParamKeyUnicodes, typeUnicodeText,
116 NULL, 0 , &dataSize, NULL ) == noErr)
119 int numChars = dataSize / sizeof( UniChar) + 1;
121 UniChar* charBuf = buf ;
123 if ( numChars * 2 > 4 )
124 charBuf = new UniChar[ numChars ] ;
125 GetEventParameter(event, kEventParamKeyUnicodes, typeUnicodeText, NULL,
126 dataSize , NULL , charBuf) ;
127 charBuf[ numChars - 1 ] = 0;
129 #if SIZEOF_WCHAR_T == 2
130 uniChar = charBuf[0] ;
132 wxMBConvUTF16 converter ;
133 converter.MB2WC( uniChar , (const char*)charBuf , 2 ) ;
136 if ( numChars * 2 > 4 )
141 GetEventParameter(event, kEventParamKeyMacCharCodes, typeChar, NULL,
142 sizeof(char), NULL, &charCode );
143 GetEventParameter(event, kEventParamKeyCode, typeUInt32, NULL,
144 sizeof(UInt32), NULL, &keyCode );
145 GetEventParameter(event, kEventParamKeyModifiers, typeUInt32, NULL,
146 sizeof(UInt32), NULL, &modifiers );
147 GetEventParameter(event, kEventParamMouseLocation, typeQDPoint, NULL,
148 sizeof(Point), NULL, &point );
150 UInt32 message = (keyCode << 8) + charCode;
151 switch ( GetEventKind( event ) )
153 case kEventRawKeyRepeat :
154 case kEventRawKeyDown :
156 WXEVENTREF formerEvent = wxTheApp->MacGetCurrentEvent() ;
157 WXEVENTHANDLERCALLREF formerHandler =
158 wxTheApp->MacGetCurrentEventHandlerCallRef() ;
160 wxTheApp->MacSetCurrentEvent( event , handler ) ;
161 if ( /* focus && */ wxTheApp->MacSendKeyDownEvent(
162 focus, message, modifiers, when, point.h, point.v, uniChar[0]))
166 wxTheApp->MacSetCurrentEvent( formerEvent , formerHandler ) ;
170 case kEventRawKeyUp :
171 if ( /* focus && */ wxTheApp->MacSendKeyUpEvent(
172 focus , message , modifiers , when , point.h , point.v , uniChar[0] ) )
178 case kEventRawKeyModifiersChanged :
180 wxKeyEvent event(wxEVT_KEY_DOWN);
182 event.m_shiftDown = modifiers & shiftKey;
183 event.m_controlDown = modifiers & controlKey;
184 event.m_altDown = modifiers & optionKey;
185 event.m_metaDown = modifiers & cmdKey;
190 event.m_uniChar = uniChar[0] ;
193 event.SetTimestamp(when);
194 event.SetEventObject(focus);
196 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & controlKey )
198 event.m_keyCode = WXK_CONTROL ;
199 event.SetEventType( ( modifiers & controlKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
200 focus->GetEventHandler()->ProcessEvent( event ) ;
202 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & shiftKey )
204 event.m_keyCode = WXK_SHIFT ;
205 event.SetEventType( ( modifiers & shiftKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
206 focus->GetEventHandler()->ProcessEvent( event ) ;
208 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & optionKey )
210 event.m_keyCode = WXK_ALT ;
211 event.SetEventType( ( modifiers & optionKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
212 focus->GetEventHandler()->ProcessEvent( event ) ;
214 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & cmdKey )
216 event.m_keyCode = WXK_COMMAND ;
217 event.SetEventType( ( modifiers & cmdKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
218 focus->GetEventHandler()->ProcessEvent( event ) ;
221 wxApp::s_lastModifiers = modifiers ;
232 static pascal OSStatus wxWebViewWebKitEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
234 OSStatus result = eventNotHandledErr ;
236 wxMacCarbonEvent cEvent( event ) ;
238 ControlRef controlRef ;
239 wxWebViewWebKit* thisWindow = (wxWebViewWebKit*) data ;
240 wxNonOwnedWindow* tlw = NULL;
242 tlw = thisWindow->MacGetTopLevelWindow();
244 cEvent.GetParameter( kEventParamDirectObject , &controlRef ) ;
246 wxWindow* currentMouseWindow = thisWindow ;
248 if ( wxApp::s_captureWindow )
249 currentMouseWindow = wxApp::s_captureWindow;
251 switch ( GetEventClass( event ) )
253 case kEventClassKeyboard:
255 result = wxWebKitKeyEventHandler(handler, event, data);
259 case kEventClassTextInput:
261 result = wxMacUnicodeTextEventHandler(handler, event, data);
265 case kEventClassMouse:
267 switch ( GetEventKind( event ) )
269 case kEventMouseDragged :
270 case kEventMouseMoved :
271 case kEventMouseDown :
274 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
275 SetupMouseEvent( wxevent , cEvent ) ;
277 currentMouseWindow->ScreenToClient( &wxevent.m_x , &wxevent.m_y ) ;
278 wxevent.SetEventObject( currentMouseWindow ) ;
279 wxevent.SetId( currentMouseWindow->GetId() ) ;
281 if ( currentMouseWindow->GetEventHandler()->ProcessEvent(wxevent) )
286 break; // this should enable WebKit to fire mouse dragged and mouse up events...
296 result = CallNextEventHandler(handler, event);
300 DEFINE_ONE_SHOT_HANDLER_GETTER( wxWebViewWebKitEventHandler )
304 //---------------------------------------------------------
305 // helper functions for NSString<->wxString conversion
306 //---------------------------------------------------------
308 inline wxString wxStringWithNSString(NSString *nsstring)
311 return wxString([nsstring UTF8String], wxConvUTF8);
313 return wxString([nsstring lossyCString]);
314 #endif // wxUSE_UNICODE
317 inline NSString* wxNSStringWithWxString(const wxString &wxstring)
320 return [NSString stringWithUTF8String: wxstring.mb_str(wxConvUTF8)];
322 return [NSString stringWithCString: wxstring.c_str() length:wxstring.Len()];
323 #endif // wxUSE_UNICODE
326 @interface MyFrameLoadMonitor : NSObject
328 wxWebViewWebKit* webKitWindow;
331 - initWithWxWindow: (wxWebViewWebKit*)inWindow;
335 @interface MyPolicyDelegate : NSObject
337 wxWebViewWebKit* webKitWindow;
340 - initWithWxWindow: (wxWebViewWebKit*)inWindow;
344 // ----------------------------------------------------------------------------
345 // creation/destruction
346 // ----------------------------------------------------------------------------
348 bool wxWebViewWebKit::Create(wxWindow *parent,
350 const wxString& strURL,
352 const wxSize& size, long style,
353 const wxString& name)
356 //m_pageTitle = _("Untitled Page");
358 //still needed for wxCocoa??
362 if (size.x == wxDefaultCoord || size.y == wxDefaultCoord)
364 m_parent->GetClientSize(&width, &height);
365 sizeInstance.x = width;
366 sizeInstance.y = height;
370 sizeInstance.x = size.x;
371 sizeInstance.y = size.y;
374 // now create and attach WebKit view...
376 wxControl::Create(parent, m_windowID, pos, sizeInstance, style, name);
377 SetSize(pos.x, pos.y, sizeInstance.x, sizeInstance.y);
379 wxTopLevelWindowCocoa *topWin = wxDynamicCast(this, wxTopLevelWindowCocoa);
380 NSWindow* nsWin = topWin->GetNSWindow();
382 rect.origin.x = pos.x;
383 rect.origin.y = pos.y;
384 rect.size.width = sizeInstance.x;
385 rect.size.height = sizeInstance.y;
386 m_webView = (WebView*)[[WebView alloc] initWithFrame:rect
387 frameName:@"webkitFrame"
388 groupName:@"webkitGroup"];
389 SetNSView(m_webView);
390 [m_cocoaNSView release];
392 if(m_parent) m_parent->CocoaAddChild(this);
393 SetInitialFrameRect(pos,sizeInstance);
395 wxControl::Create(parent, winID, pos, size, style, wxDefaultValidator, name);
398 m_peer = new wxMacControl(this);
400 HIWebViewCreate( m_peer->GetControlRefAddr() );
402 m_webView = (WebView*) HIWebViewGetWebView( m_peer->GetControlRef() );
404 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
405 if ( UMAGetSystemVersion() >= 0x1030 )
406 HIViewChangeFeatures( m_peer->GetControlRef() , kHIViewIsOpaque , 0 ) ;
408 InstallControlEventHandler(m_peer->GetControlRef(),
409 GetwxWebViewWebKitEventHandlerUPP(),
410 GetEventTypeCount(eventList), eventList, this,
411 (EventHandlerRef *)&m_webKitCtrlEventHandler);
413 NSRect r = wxOSXGetFrameForControl( this, pos , size ) ;
414 m_webView = [[WebView alloc] initWithFrame:r
415 frameName:@"webkitFrame"
416 groupName:@"webkitGroup"];
417 m_peer = new wxWidgetCocoaImpl( this, m_webView );
420 wx_webviewctrls[m_webView] = this;
422 MacPostControlCreate(pos, size);
425 HIViewSetVisible( m_peer->GetControlRef(), true );
427 [m_webView setHidden:false];
431 // Register event listener interfaces
432 MyFrameLoadMonitor* myFrameLoadMonitor =
433 [[MyFrameLoadMonitor alloc] initWithWxWindow: this];
435 [m_webView setFrameLoadDelegate:myFrameLoadMonitor];
437 // this is used to veto page loads, etc.
438 MyPolicyDelegate* myPolicyDelegate =
439 [[MyPolicyDelegate alloc] initWithWxWindow: this];
441 [m_webView setPolicyDelegate:myPolicyDelegate];
443 InternalLoadURL(strURL);
447 wxWebViewWebKit::~wxWebViewWebKit()
449 MyFrameLoadMonitor* myFrameLoadMonitor = [m_webView frameLoadDelegate];
450 MyPolicyDelegate* myPolicyDelegate = [m_webView policyDelegate];
451 [m_webView setFrameLoadDelegate: nil];
452 [m_webView setPolicyDelegate: nil];
454 if (myFrameLoadMonitor)
455 [myFrameLoadMonitor release];
457 if (myPolicyDelegate)
458 [myPolicyDelegate release];
461 // ----------------------------------------------------------------------------
463 // ----------------------------------------------------------------------------
465 void wxWebViewWebKit::InternalLoadURL(const wxString &url)
470 [[m_webView mainFrame] loadRequest:[NSURLRequest requestWithURL:
471 [NSURL URLWithString:wxNSStringWithWxString(url)]]];
474 bool wxWebViewWebKit::CanGoBack()
479 return [m_webView canGoBack];
482 bool wxWebViewWebKit::CanGoForward()
487 return [m_webView canGoForward];
490 void wxWebViewWebKit::GoBack()
495 bool result = [(WebView*)m_webView goBack];
497 // TODO: return result (if it also exists in other backends...)
501 void wxWebViewWebKit::GoForward()
506 bool result = [(WebView*)m_webView goForward];
508 // TODO: return result (if it also exists in other backends...)
512 void wxWebViewWebKit::Reload(wxWebViewReloadFlags flags)
517 if (flags & wxWEB_VIEW_RELOAD_NO_CACHE)
519 // TODO: test this indeed bypasses the cache
520 [[m_webView preferences] setUsesPageCache:NO];
521 [[m_webView mainFrame] reload];
522 [[m_webView preferences] setUsesPageCache:YES];
526 [[m_webView mainFrame] reload];
530 void wxWebViewWebKit::Stop()
535 [[m_webView mainFrame] stopLoading];
538 bool wxWebViewWebKit::CanGetPageSource()
543 WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
544 return ( [[dataSource representation] canProvideDocumentSource] );
547 wxString wxWebViewWebKit::GetPageSource()
550 if (CanGetPageSource())
552 WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
553 wxASSERT (dataSource != nil);
555 id<WebDocumentRepresentation> representation = [dataSource representation];
556 wxASSERT (representation != nil);
558 NSString* source = [representation documentSource];
561 return wxEmptyString;
564 return wxStringWithNSString( source );
567 return wxEmptyString;
570 wxString wxWebViewWebKit::GetSelection()
573 return wxEmptyString;
575 NSString* selectedText = [[m_webView selectedDOMRange] toString];
576 return wxStringWithNSString( selectedText );
579 bool wxWebViewWebKit::CanIncreaseTextSize()
584 if ([m_webView canMakeTextLarger])
590 void wxWebViewWebKit::IncreaseTextSize()
595 if (CanIncreaseTextSize())
596 [m_webView makeTextLarger:(WebView*)m_webView];
599 bool wxWebViewWebKit::CanDecreaseTextSize()
604 if ([m_webView canMakeTextSmaller])
610 void wxWebViewWebKit::DecreaseTextSize()
615 if (CanDecreaseTextSize())
616 [m_webView makeTextSmaller:(WebView*)m_webView];
619 void wxWebViewWebKit::Print()
622 // TODO: allow specifying the "show prompt" parameter in Print() ?
623 bool showPrompt = true;
628 id view = [[[m_webView mainFrame] frameView] documentView];
629 NSPrintOperation *op = [NSPrintOperation printOperationWithView:view
630 printInfo: [NSPrintInfo sharedPrintInfo]];
633 [op setShowsPrintPanel: showPrompt];
634 // in my tests, the progress bar always freezes and it stops the whole
635 // print operation. do not turn this to true unless there is a
636 // workaround for the bug.
637 [op setShowsProgressPanel: false];
643 void wxWebViewWebKit::SetEditable(bool enable)
648 [m_webView setEditable:enable ];
651 bool wxWebViewWebKit::IsEditable()
656 return [m_webView isEditable];
659 void wxWebViewWebKit::SetZoomType(wxWebViewZoomType zoomType)
661 // there is only one supported zoom type at the moment so this setter
662 // does nothing beyond checking sanity
663 wxASSERT(zoomType == wxWEB_VIEW_ZOOM_TYPE_TEXT);
666 wxWebViewZoomType wxWebViewWebKit::GetZoomType() const
668 // for now that's the only one that is supported
669 // FIXME: does the default zoom type change depending on webkit versions? :S
670 // Then this will be wrong
671 return wxWEB_VIEW_ZOOM_TYPE_TEXT;
674 bool wxWebViewWebKit::CanSetZoomType(wxWebViewZoomType type) const
678 // for now that's the only one that is supported
679 // TODO: I know recent versions of webkit support layout zoom too,
680 // check if we can support it
681 case wxWEB_VIEW_ZOOM_TYPE_TEXT:
689 int wxWebViewWebKit::GetScrollPos()
691 id result = [[m_webView windowScriptObject]
692 evaluateWebScript:@"document.body.scrollTop"];
693 return [result intValue];
696 void wxWebViewWebKit::SetScrollPos(int pos)
702 javascript.Printf(wxT("document.body.scrollTop = %d;"), pos);
703 [[m_webView windowScriptObject] evaluateWebScript:
704 (NSString*)wxNSStringWithWxString( javascript )];
707 wxString wxWebViewWebKit::GetSelectedText()
709 NSString* selection = [[m_webView selectedDOMRange] markupString];
710 if (!selection) return wxEmptyString;
712 return wxStringWithNSString(selection);
715 void wxWebViewWebKit::RunScript(const wxString& javascript)
720 [[m_webView windowScriptObject] evaluateWebScript:
721 (NSString*)wxNSStringWithWxString( javascript )];
724 void wxWebViewWebKit::OnSize(wxSizeEvent &event)
726 #if defined(__WXMAC_) && wxOSX_USE_CARBON
727 // This is a nasty hack because WebKit seems to lose its position when it is
728 // embedded in a control that is not itself the content view for a TLW.
729 // I put it in OnSize because these calcs are not perfect, and in fact are
730 // basically guesses based on reverse engineering, so it's best to give
731 // people the option of overriding OnSize with their own calcs if need be.
732 // I also left some test debugging print statements as a convenience if
733 // a(nother) problem crops up.
735 wxWindow* tlw = MacGetTopLevelWindow();
737 NSRect frame = [(WebView*)m_webView frame];
738 NSRect bounds = [(WebView*)m_webView bounds];
740 #if DEBUG_WEBKIT_SIZING
741 fprintf(stderr,"Carbon window x=%d, y=%d, width=%d, height=%d\n",
742 GetPosition().x, GetPosition().y, GetSize().x, GetSize().y);
743 fprintf(stderr, "Cocoa window frame x=%G, y=%G, width=%G, height=%G\n",
744 frame.origin.x, frame.origin.y,
745 frame.size.width, frame.size.height);
746 fprintf(stderr, "Cocoa window bounds x=%G, y=%G, width=%G, height=%G\n",
747 bounds.origin.x, bounds.origin.y,
748 bounds.size.width, bounds.size.height);
751 // This must be the case that Apple tested with, because well, in this one case
752 // we don't need to do anything! It just works. ;)
753 if (GetParent() == tlw) return;
755 // since we no longer use parent coordinates, we always want 0,0.
763 #if DEBUG_WEBKIT_SIZING
764 printf("Before conversion, origin is: x = %d, y = %d\n", x, y);
767 // NB: In most cases, when calling HIViewConvertRect, what people want is to
768 // use GetRootControl(), and this tripped me up at first. But in fact, what
769 // we want is the root view, because we need to make the y origin relative
770 // to the very top of the window, not its contents, since we later flip
771 // the y coordinate for Cocoa.
772 HIViewConvertRect (&rect, m_peer->GetControlRef(),
774 (WindowRef) MacGetTopLevelWindowRef()
777 x = (int)rect.origin.x;
778 y = (int)rect.origin.y;
780 #if DEBUG_WEBKIT_SIZING
781 printf("Moving Cocoa frame origin to: x = %d, y = %d\n", x, y);
785 //flip the y coordinate to convert to Cocoa coordinates
786 y = tlw->GetSize().y - ((GetSize().y) + y);
789 #if DEBUG_WEBKIT_SIZING
790 printf("y = %d after flipping value\n", y);
795 [(WebView*)m_webView setFrame:frame];
798 [(WebView*)m_webView display];
803 void wxWebViewWebKit::MacVisibilityChanged(){
804 #if defined(__WXMAC__) && wxOSX_USE_CARBON
805 bool isHidden = !IsControlVisible( m_peer->GetControlRef());
807 [(WebView*)m_webView display];
809 [m_webView setHidden:isHidden];
813 void wxWebViewWebKit::LoadUrl(const wxString& url)
815 InternalLoadURL(url);
818 wxString wxWebViewWebKit::GetCurrentURL()
820 return wxStringWithNSString([m_webView mainFrameURL]);
823 wxString wxWebViewWebKit::GetCurrentTitle()
825 return GetPageTitle();
828 float wxWebViewWebKit::GetWebkitZoom()
830 return [m_webView textSizeMultiplier];
833 void wxWebViewWebKit::SetWebkitZoom(float zoom)
835 [m_webView setTextSizeMultiplier:zoom];
838 wxWebViewZoom wxWebViewWebKit::GetZoom()
840 float zoom = GetWebkitZoom();
842 // arbitrary way to map float zoom to our common zoom enum
845 return wxWEB_VIEW_ZOOM_TINY;
847 else if (zoom > 0.55 && zoom <= 0.85)
849 return wxWEB_VIEW_ZOOM_SMALL;
851 else if (zoom > 0.85 && zoom <= 1.15)
853 return wxWEB_VIEW_ZOOM_MEDIUM;
855 else if (zoom > 1.15 && zoom <= 1.45)
857 return wxWEB_VIEW_ZOOM_LARGE;
859 else if (zoom > 1.45)
861 return wxWEB_VIEW_ZOOM_LARGEST;
864 // to shut up compilers, this can never be reached logically
866 return wxWEB_VIEW_ZOOM_MEDIUM;
869 void wxWebViewWebKit::SetZoom(wxWebViewZoom zoom)
871 // arbitrary way to map our common zoom enum to float zoom
874 case wxWEB_VIEW_ZOOM_TINY:
878 case wxWEB_VIEW_ZOOM_SMALL:
882 case wxWEB_VIEW_ZOOM_MEDIUM:
886 case wxWEB_VIEW_ZOOM_LARGE:
890 case wxWEB_VIEW_ZOOM_LARGEST:
900 void wxWebViewWebKit::SetPage(const wxString& src, const wxString& baseUrl)
905 [[m_webView mainFrame] loadHTMLString:(NSString*)wxNSStringWithWxString(src)
906 baseURL:[NSURL URLWithString:
907 wxNSStringWithWxString( baseUrl )]];
910 void wxWebViewWebKit::Cut()
915 [(WebView*)m_webView cut:m_webView];
918 void wxWebViewWebKit::Copy()
923 [(WebView*)m_webView copy:m_webView];
926 void wxWebViewWebKit::Paste()
931 [(WebView*)m_webView paste:m_webView];
934 void wxWebViewWebKit::DeleteSelection()
939 [(WebView*)m_webView deleteSelection];
942 //------------------------------------------------------------
943 // Listener interfaces
944 //------------------------------------------------------------
946 // NB: I'm still tracking this down, but it appears the Cocoa window
947 // still has these events fired on it while the Carbon control is being
948 // destroyed. Therefore, we must be careful to check both the existence
949 // of the Carbon control and the event handler before firing events.
951 @implementation MyFrameLoadMonitor
953 - initWithWxWindow: (wxWebViewWebKit*)inWindow
956 webKitWindow = inWindow; // non retained
960 - (void)webView:(WebView *)sender
961 didStartProvisionalLoadForFrame:(WebFrame *)frame
963 wxASSERT(wx_webviewctrls.find(sender) != wx_webviewctrls.end());
964 wx_webviewctrls[sender]->m_busy = true;
967 - (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame
969 wxASSERT(wx_webviewctrls.find(sender) != wx_webviewctrls.end());
970 wx_webviewctrls[sender]->m_busy = true;
972 if (webKitWindow && frame == [sender mainFrame]){
973 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
974 wxString target = wxStringWithNSString([frame name]);
975 wxWebNavigationEvent thisEvent(wxEVT_COMMAND_WEB_VIEW_NAVIGATED,
976 wx_webviewctrls[sender]->GetId(),
977 wxStringWithNSString( url ),
980 if (webKitWindow && webKitWindow->GetEventHandler())
981 webKitWindow->GetEventHandler()->ProcessEvent(thisEvent);
985 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
987 wxASSERT(wx_webviewctrls.find(sender) != wx_webviewctrls.end());
988 wx_webviewctrls[sender]->m_busy = false;
990 if (webKitWindow && frame == [sender mainFrame]){
991 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
993 wxString target = wxStringWithNSString([frame name]);
994 wxWebNavigationEvent thisEvent(wxEVT_COMMAND_WEB_VIEW_LOADED,
995 wx_webviewctrls[sender]->GetId(),
996 wxStringWithNSString( url ),
999 if (webKitWindow && webKitWindow->GetEventHandler())
1000 webKitWindow->GetEventHandler()->ProcessEvent(thisEvent);
1004 wxString nsErrorToWxHtmlError(NSError* error, wxWebNavigationError* out)
1006 *out = wxWEB_NAV_ERR_OTHER;
1008 if ([[error domain] isEqualToString:NSURLErrorDomain])
1010 switch ([error code])
1012 case NSURLErrorCannotFindHost:
1013 case NSURLErrorFileDoesNotExist:
1014 case NSURLErrorRedirectToNonExistentLocation:
1015 *out = wxWEB_NAV_ERR_NOT_FOUND;
1018 case NSURLErrorResourceUnavailable:
1019 case NSURLErrorHTTPTooManyRedirects:
1020 case NSURLErrorDataLengthExceedsMaximum:
1021 case NSURLErrorBadURL:
1022 case NSURLErrorFileIsDirectory:
1023 *out = wxWEB_NAV_ERR_REQUEST;
1026 case NSURLErrorTimedOut:
1027 case NSURLErrorDNSLookupFailed:
1028 case NSURLErrorNetworkConnectionLost:
1029 case NSURLErrorCannotConnectToHost:
1030 case NSURLErrorNotConnectedToInternet:
1031 //case NSURLErrorInternationalRoamingOff:
1032 //case NSURLErrorCallIsActive:
1033 //case NSURLErrorDataNotAllowed:
1034 *out = wxWEB_NAV_ERR_CONNECTION;
1037 case NSURLErrorCancelled:
1038 case NSURLErrorUserCancelledAuthentication:
1039 *out = wxWEB_NAV_ERR_USER_CANCELLED;
1042 case NSURLErrorCannotDecodeRawData:
1043 case NSURLErrorCannotDecodeContentData:
1044 case NSURLErrorBadServerResponse:
1045 case NSURLErrorCannotParseResponse:
1046 *out = wxWEB_NAV_ERR_REQUEST;
1049 case NSURLErrorUserAuthenticationRequired:
1050 case NSURLErrorSecureConnectionFailed:
1051 case NSURLErrorClientCertificateRequired:
1052 *out = wxWEB_NAV_ERR_AUTH;
1055 case NSURLErrorNoPermissionsToReadFile:
1056 *out = wxWEB_NAV_ERR_SECURITY;
1059 case NSURLErrorServerCertificateHasBadDate:
1060 case NSURLErrorServerCertificateUntrusted:
1061 case NSURLErrorServerCertificateHasUnknownRoot:
1062 case NSURLErrorServerCertificateNotYetValid:
1063 case NSURLErrorClientCertificateRejected:
1064 *out = wxWEB_NAV_ERR_CERTIFICATE;
1069 wxString message = wxStringWithNSString([error localizedDescription]);
1070 NSString* detail = [error localizedFailureReason];
1073 message = message + " (" + wxStringWithNSString(detail) + ")";
1078 - (void)webView:(WebView *)sender didFailLoadWithError:(NSError*) error
1079 forFrame:(WebFrame *)frame
1081 wxASSERT(wx_webviewctrls.find(sender) != wx_webviewctrls.end());
1082 wx_webviewctrls[sender]->m_busy = false;
1084 if (webKitWindow && frame == [sender mainFrame]){
1085 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
1087 wxWebNavigationError type;
1088 wxString description = nsErrorToWxHtmlError(error, &type);
1089 wxWebNavigationEvent thisEvent(wxEVT_COMMAND_WEB_VIEW_ERROR,
1090 wx_webviewctrls[sender]->GetId(),
1091 wxStringWithNSString( url ),
1092 wxEmptyString, false);
1093 thisEvent.SetString(description);
1094 thisEvent.SetInt(type);
1096 if (webKitWindow && webKitWindow->GetEventHandler())
1098 webKitWindow->GetEventHandler()->ProcessEvent(thisEvent);
1103 - (void)webView:(WebView *)sender
1104 didFailProvisionalLoadWithError:(NSError*)error
1105 forFrame:(WebFrame *)frame
1107 wxASSERT(wx_webviewctrls.find(sender) != wx_webviewctrls.end());
1108 wx_webviewctrls[sender]->m_busy = false;
1110 if (webKitWindow && frame == [sender mainFrame]){
1111 NSString *url = [[[[frame provisionalDataSource] request] URL]
1114 wxWebNavigationError type;
1115 wxString description = nsErrorToWxHtmlError(error, &type);
1116 wxWebNavigationEvent thisEvent(wxEVT_COMMAND_WEB_VIEW_ERROR,
1117 wx_webviewctrls[sender]->GetId(),
1118 wxStringWithNSString( url ),
1119 wxEmptyString, false);
1120 thisEvent.SetString(description);
1121 thisEvent.SetInt(type);
1123 if (webKitWindow && webKitWindow->GetEventHandler())
1124 webKitWindow->GetEventHandler()->ProcessEvent(thisEvent);
1128 - (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title
1129 forFrame:(WebFrame *)frame
1131 if (webKitWindow && frame == [sender mainFrame])
1133 webKitWindow->SetPageTitle(wxStringWithNSString( title ));
1138 @implementation MyPolicyDelegate
1140 - initWithWxWindow: (wxWebViewWebKit*)inWindow
1143 webKitWindow = inWindow; // non retained
1147 - (void)webView:(WebView *)sender
1148 decidePolicyForNavigationAction:(NSDictionary *)actionInformation
1149 request:(NSURLRequest *)request
1150 frame:(WebFrame *)frame
1151 decisionListener:(id<WebPolicyDecisionListener>)listener
1155 wxASSERT(wx_webviewctrls.find(sender) != wx_webviewctrls.end());
1156 wx_webviewctrls[sender]->m_busy = true;
1157 NSString *url = [[request URL] absoluteString];
1158 wxString target = wxStringWithNSString([frame name]);
1159 wxWebNavigationEvent thisEvent(wxEVT_COMMAND_WEB_VIEW_NAVIGATING,
1160 wx_webviewctrls[sender]->GetId(),
1161 wxStringWithNSString( url ), target, true);
1163 if (webKitWindow && webKitWindow->GetEventHandler())
1164 webKitWindow->GetEventHandler()->ProcessEvent(thisEvent);
1166 if (thisEvent.IsVetoed())
1168 wx_webviewctrls[sender]->m_busy = false;
1177 - (void)webView:(WebView *)sender
1178 decidePolicyForNewWindowAction:(NSDictionary *)actionInformation
1179 request:(NSURLRequest *)request
1180 newFrameName:(NSString *)frameName
1181 decisionListener:(id < WebPolicyDecisionListener >)listener
1183 wxUnusedVar(actionInformation);
1185 wxASSERT(wx_webviewctrls.find(sender) != wx_webviewctrls.end());
1186 NSString *url = [[request URL] absoluteString];
1187 wxString target = wxStringWithNSString([frame name]);
1188 wxWebNavigationEvent thisEvent(wxEVT_COMMAND_WEB_VIEW_NEWWINDOW,
1189 wx_webviewctrls[sender]->GetId(),
1190 wxStringWithNSString( url ), target, true);
1192 if (webKitWindow && webKitWindow->GetEventHandler())
1193 webKitWindow->GetEventHandler()->ProcessEvent(thisEvent);
1199 //#endif //wxHAVE_WEB_BACKEND_OSX_WEBKIT