1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxWebKitCtrl - embeddable web kit control
4 // Author: Jethro Grassie / Kevin Ollivier
8 // Copyright: (c) Jethro Grassie / Kevin Ollivier
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "webkit.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18 #include "wx/splitter.h"
27 #include "wx/cocoa/autorelease.h"
29 #include "wx/mac/uma.h"
30 #include <Carbon/Carbon.h>
31 #include <WebKit/WebKit.h>
32 #include <WebKit/HIWebView.h>
33 #include <WebKit/CarbonUtils.h>
36 #include "wx/html/webkit.h"
38 #define DEBUG_WEBKIT_SIZING 0
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 IMPLEMENT_DYNAMIC_CLASS(wxWebKitCtrl, wxControl)
46 BEGIN_EVENT_TABLE(wxWebKitCtrl, wxControl)
47 EVT_SIZE(wxWebKitCtrl::OnSize)
50 // ----------------------------------------------------------------------------
51 // Carbon Events handlers
52 // ----------------------------------------------------------------------------
54 // prototype for function in src/mac/carbon/toplevel.cpp
55 void SetupMouseEvent( wxMouseEvent &wxevent , wxMacCarbonEvent &cEvent );
57 static const EventTypeSpec eventList[] =
59 //{ kEventClassControl, kEventControlTrack } ,
60 { kEventClassMouse, kEventMouseUp },
61 { kEventClassMouse, kEventMouseDown },
62 { kEventClassMouse, kEventMouseMoved },
63 { kEventClassMouse, kEventMouseDragged },
64 #if DEBUG_WEBKIT_SIZING == 1
65 { kEventClassControl, kEventControlBoundsChanged } ,
69 static pascal OSStatus wxWebKitCtrlEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
71 OSStatus result = eventNotHandledErr ;
73 wxMacCarbonEvent cEvent( event ) ;
75 ControlRef controlRef ;
76 wxWebKitCtrl* thisWindow = (wxWebKitCtrl*) data ;
77 wxTopLevelWindowMac* tlw = NULL;
79 tlw = thisWindow->MacGetTopLevelWindow();
81 cEvent.GetParameter( kEventParamDirectObject , &controlRef ) ;
83 wxWindow* currentMouseWindow = thisWindow ;
85 if ( wxApp::s_captureWindow )
86 currentMouseWindow = wxApp::s_captureWindow;
88 switch ( GetEventKind( event ) )
90 case kEventMouseDragged :
91 case kEventMouseMoved :
93 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
94 SetupMouseEvent( wxevent , cEvent ) ;
96 currentMouseWindow->ScreenToClient( &wxevent.m_x , &wxevent.m_y ) ;
97 wxevent.SetEventObject( currentMouseWindow ) ;
98 wxevent.SetId( currentMouseWindow->GetId() ) ;
100 if ( currentMouseWindow->GetEventHandler()->ProcessEvent(wxevent) )
105 break; // this should enable WebKit to fire mouse dragged and mouse up events...
108 case kEventControlBoundsChanged:
110 // this is just here for debugging, so we can note any differences between
111 // native event sizes and the sizes the wxWindow receives.
112 Rect origBounds = cEvent.GetParameter<Rect>(kEventParamOriginalBounds, typeQDRectangle) ;
113 Rect prevBounds = cEvent.GetParameter<Rect>(kEventParamPreviousBounds, typeQDRectangle) ;
114 Rect curBounds = cEvent.GetParameter<Rect>(kEventParamCurrentBounds, typeQDRectangle) ;
116 fprintf(stderr, "Orig bounds x=%d, y=%d, height=%d, width=%d\n", origBounds.left, origBounds.top, origBounds.bottom -origBounds.top, origBounds.right - origBounds.left);
117 fprintf(stderr, "Prev bounds x=%d, y=%d, height=%d, width=%d\n", prevBounds.left, prevBounds.top, prevBounds.bottom -prevBounds.top, prevBounds.right - prevBounds.left);
118 fprintf(stderr, "Cur bounds x=%d, y=%d, height=%d, width=%d\n", curBounds.left, curBounds.top, curBounds.bottom -curBounds.top, curBounds.right - curBounds.left);
124 result = CallNextEventHandler(handler, event);
128 DEFINE_ONE_SHOT_HANDLER_GETTER( wxWebKitCtrlEventHandler )
131 // ----------------------------------------------------------------------------
133 // ----------------------------------------------------------------------------
135 IMPLEMENT_DYNAMIC_CLASS( wxWebKitStateChangedEvent, wxCommandEvent )
137 DEFINE_EVENT_TYPE( wxEVT_WEBKIT_STATE_CHANGED )
139 wxWebKitStateChangedEvent::wxWebKitStateChangedEvent( wxWindow* win )
141 SetEventType( wxEVT_WEBKIT_STATE_CHANGED);
142 SetEventObject( win );
146 IMPLEMENT_DYNAMIC_CLASS( wxWebKitBeforeLoadEvent, wxCommandEvent )
148 DEFINE_EVENT_TYPE( wxEVT_WEBKIT_BEFORE_LOAD )
150 wxWebKitBeforeLoadEvent::wxWebKitBeforeLoadEvent( wxWindow* win )
153 SetEventType( wxEVT_WEBKIT_BEFORE_LOAD);
154 SetEventObject( win );
158 //---------------------------------------------------------
159 // helper functions for NSString<->wxString conversion
160 //---------------------------------------------------------
162 inline wxString wxStringWithNSString(NSString *nsstring)
165 return wxString([nsstring UTF8String], wxConvUTF8);
167 return wxString([nsstring lossyCString]);
168 #endif // wxUSE_UNICODE
171 inline NSString* wxNSStringWithWxString(const wxString &wxstring)
174 return [NSString stringWithUTF8String: wxstring.mb_str(wxConvUTF8)];
176 return [NSString stringWithCString: wxstring.c_str() length:wxstring.Len()];
177 #endif // wxUSE_UNICODE
180 inline int wxNavTypeFromWebNavType(int type){
181 if (type == WebNavigationTypeLinkClicked)
182 return wxWEBKIT_NAV_LINK_CLICKED;
184 if (type == WebNavigationTypeFormSubmitted)
185 return wxWEBKIT_NAV_FORM_SUBMITTED;
187 if (type == WebNavigationTypeBackForward)
188 return wxWEBKIT_NAV_BACK_NEXT;
190 if (type == WebNavigationTypeReload)
191 return wxWEBKIT_NAV_RELOAD;
193 if (type == WebNavigationTypeFormResubmitted)
194 return wxWEBKIT_NAV_FORM_RESUBMITTED;
196 return wxWEBKIT_NAV_OTHER;
199 @interface MyFrameLoadMonitor : NSObject
201 wxWebKitCtrl* webKitWindow;
204 - initWithWxWindow: (wxWebKitCtrl*)inWindow;
208 @interface MyPolicyDelegate : NSObject
210 wxWebKitCtrl* webKitWindow;
213 - initWithWxWindow: (wxWebKitCtrl*)inWindow;
217 // ----------------------------------------------------------------------------
218 // creation/destruction
219 // ----------------------------------------------------------------------------
221 bool wxWebKitCtrl::Create(wxWindow *parent,
223 const wxString& strURL,
225 const wxSize& size, long style,
226 const wxValidator& validator,
227 const wxString& name)
230 m_currentURL = strURL;
231 //m_pageTitle = _("Untitled Page");
233 //still needed for wxCocoa??
237 if (size.x == wxDefaultCoord || size.y == wxDefaultCoord)
239 m_parent->GetClientSize(&width, &height);
240 sizeInstance.x = width;
241 sizeInstance.y = height;
245 sizeInstance.x = size.x;
246 sizeInstance.y = size.y;
249 // now create and attach WebKit view...
251 wxControl::Create(parent, m_windowID, pos, sizeInstance, style , validator , name);
252 SetSize(pos.x, pos.y, sizeInstance.x, sizeInstance.y);
254 wxTopLevelWindowCocoa *topWin = wxDynamicCast(this, wxTopLevelWindowCocoa);
255 NSWindow* nsWin = topWin->GetNSWindow();
257 rect.origin.x = pos.x;
258 rect.origin.y = pos.y;
259 rect.size.width = sizeInstance.x;
260 rect.size.height = sizeInstance.y;
261 m_webView = (WebView*)[[WebView alloc] initWithFrame:rect frameName:@"webkitFrame" groupName:@"webkitGroup"];
262 SetNSView(m_webView);
263 [m_cocoaNSView release];
265 if(m_parent) m_parent->CocoaAddChild(this);
266 SetInitialFrameRect(pos,sizeInstance);
268 m_macIsUserPane = false;
269 wxControl::Create(parent, winID, pos, size, style , validator , name);
270 m_peer = new wxMacControl(this);
272 HIWebViewCreate( m_peer->GetControlRefAddr() );
274 m_webView = (WebView*) HIWebViewGetWebView( m_peer->GetControlRef() );
276 MacPostControlCreate(pos, size);
277 HIViewSetVisible( m_peer->GetControlRef(), true );
278 [m_webView setHidden:false];
279 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
280 if ( UMAGetSystemVersion() >= 0x1030 )
281 HIViewChangeFeatures( m_peer->GetControlRef() , kHIViewIsOpaque , 0 ) ;
283 InstallControlEventHandler( m_peer->GetControlRef() , GetwxWebKitCtrlEventHandlerUPP(),
284 GetEventTypeCount(eventList), eventList, this,
285 (EventHandlerRef *)&m_webKitCtrlEventHandler);
289 // Register event listener interfaces
290 MyFrameLoadMonitor* myFrameLoadMonitor = [[MyFrameLoadMonitor alloc] initWithWxWindow: this];
291 [m_webView setFrameLoadDelegate:myFrameLoadMonitor];
293 // this is used to veto page loads, etc.
294 MyPolicyDelegate* myPolicyDelegate = [[MyPolicyDelegate alloc] initWithWxWindow: this];
295 [m_webView setPolicyDelegate:myPolicyDelegate];
297 LoadURL(m_currentURL);
301 wxWebKitCtrl::~wxWebKitCtrl()
306 // ----------------------------------------------------------------------------
308 // ----------------------------------------------------------------------------
310 void wxWebKitCtrl::LoadURL(const wxString &url)
315 [[m_webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:wxNSStringWithWxString(url)]]];
320 bool wxWebKitCtrl::CanGoBack(){
324 return [m_webView canGoBack];
327 bool wxWebKitCtrl::CanGoForward(){
331 return [m_webView canGoForward];
334 bool wxWebKitCtrl::GoBack(){
338 bool result = [(WebView*)m_webView goBack];
342 bool wxWebKitCtrl::GoForward(){
346 bool result = [(WebView*)m_webView goForward];
350 void wxWebKitCtrl::Reload(){
354 [[m_webView mainFrame] reload];
357 void wxWebKitCtrl::Stop(){
361 [[m_webView mainFrame] stopLoading];
364 bool wxWebKitCtrl::CanGetPageSource(){
368 WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
369 return ( [[dataSource representation] canProvideDocumentSource] );
372 wxString wxWebKitCtrl::GetPageSource(){
374 if (CanGetPageSource()){
375 WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
376 return wxStringWithNSString( [[dataSource representation] documentSource] );
379 return wxEmptyString;
382 wxString wxWebKitCtrl::GetSelection(){
384 return wxEmptyString;
386 NSString* selectedText = [[m_webView selectedDOMRange] toString];
387 return wxStringWithNSString( selectedText );
390 bool wxWebKitCtrl::CanIncreaseTextSize(){
394 if ([m_webView canMakeTextLarger])
400 void wxWebKitCtrl::IncreaseTextSize(){
404 if (CanIncreaseTextSize())
405 [m_webView makeTextLarger:(WebView*)m_webView];
408 bool wxWebKitCtrl::CanDecreaseTextSize(){
412 if ([m_webView canMakeTextSmaller])
418 void wxWebKitCtrl::DecreaseTextSize(){
422 if (CanDecreaseTextSize())
423 [m_webView makeTextSmaller:(WebView*)m_webView];
426 void wxWebKitCtrl::SetPageSource(const wxString& source, const wxString& baseUrl){
430 [[m_webView mainFrame] loadHTMLString:(NSString*)wxNSStringWithWxString( source ) baseURL:[NSURL URLWithString:wxNSStringWithWxString( baseUrl )]];
434 void wxWebKitCtrl::Print(bool showPrompt){
438 id view = [[[m_webView mainFrame] frameView] documentView];
439 NSPrintOperation *op = [NSPrintOperation printOperationWithView:view printInfo: [NSPrintInfo sharedPrintInfo]];
441 [op setShowsPrintPanel: showPrompt];
442 // in my tests, the progress bar always freezes and it stops the whole print operation.
443 // do not turn this to true unless there is a workaround for the bug.
444 [op setShowsProgressPanel: false];
450 void wxWebKitCtrl::MakeEditable(bool enable){
454 [m_webView setEditable:enable ];
457 bool wxWebKitCtrl::IsEditable(){
461 return [m_webView isEditable];
464 int wxWebKitCtrl::GetScrollPos(){
465 id result = [[m_webView windowScriptObject] evaluateWebScript:@"document.body.scrollTop"];
466 return [result intValue];
469 void wxWebKitCtrl::SetScrollPos(int pos){
474 javascript.Printf(wxT("document.body.scrollTop = %d;"), pos);
475 [[m_webView windowScriptObject] evaluateWebScript:(NSString*)wxNSStringWithWxString( javascript )];
478 wxString wxWebKitCtrl::RunScript(const wxString& javascript){
480 return wxEmptyString;
482 id result = [[m_webView windowScriptObject] evaluateWebScript:(NSString*)wxNSStringWithWxString( javascript )];
484 NSString* resultAsString;
485 wxString resultAsWxString = wxEmptyString;
486 NSString* className = NSStringFromClass([result class]);
487 if ([className isEqualToString:@"NSCFNumber"])
488 resultAsString = [NSString stringWithFormat:@"%@", result];
489 else if ([className isEqualToString:@"NSCFString"])
490 resultAsString = result;
491 else if ([className isEqualToString:@"NSCFBoolean"]){
492 if ([result boolValue])
493 resultAsString = @"true";
495 resultAsString = @"false";
497 else if ([className isEqualToString:@"WebScriptObject"])
498 resultAsString = [result stringRepresentation];
500 fprintf(stderr, "wxWebKitCtrl::RunScript - Unexpected return type: %s!\n", [className UTF8String]);
502 resultAsWxString = wxStringWithNSString( resultAsString );
503 return resultAsWxString;
506 void wxWebKitCtrl::OnSize(wxSizeEvent &event){
507 // This is a nasty hack because WebKit seems to lose its position when it is embedded
508 // in a control that is not itself the content view for a TLW.
509 // I put it in OnSize because these calcs are not perfect, and in fact are basically
510 // guesses based on reverse engineering, so it's best to give people the option of
511 // overriding OnSize with their own calcs if need be.
512 // I also left some test debugging print statements as a convenience if a(nother)
515 wxWindow* tlw = MacGetTopLevelWindow();
517 NSRect frame = [m_webView frame];
518 NSRect bounds = [m_webView bounds];
520 #if DEBUG_WEBKIT_SIZING
521 fprintf(stderr,"Carbon window x=%d, y=%d, width=%d, height=%d\n", GetPosition().x, GetPosition().y, GetSize().x, GetSize().y);
522 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);
523 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);
526 // This must be the case that Apple tested with, because well, in this one case
527 // we don't need to do anything! It just works. ;)
528 if (GetParent() == tlw){
532 int x = GetPosition().x;
533 int y = GetPosition().y;
539 #if DEBUG_WEBKIT_SIZING
540 printf("Before conversion, origin is: x = %d, y = %d\n", x, y);
543 // NB: In most cases, when calling HIViewConvertRect, what people want is to use GetRootControl(),
544 // and this tripped me up at first. But in fact, what we want is the root view, because we need to
545 // make the y origin relative to the very top of the window, not its contents, since we later flip
546 // the y coordinate for Cocoa.
547 HIViewConvertRect (&rect, HIViewGetSuperview( m_peer->GetControlRef() ),
548 HIViewGetRoot( (WindowRef) MacGetTopLevelWindowRef() ) );
550 x = (int)rect.origin.x;
551 y = (int)rect.origin.y;
553 #if DEBUG_WEBKIT_SIZING
554 printf("Moving Cocoa frame origin to: x = %d, y = %d\n", x, y);
558 //flip the y coordinate to convert to Cocoa coordinates
559 y = tlw->GetSize().y - ((GetSize().y) + y);
562 #if DEBUG_WEBKIT_SIZING
563 printf("y = %d after flipping value\n", y);
568 [m_webView setFrame:frame];
571 [(WebView*)m_webView display];
575 void wxWebKitCtrl::MacVisibilityChanged(){
576 bool isHidden = !IsControlVisible( m_peer->GetControlRef());
578 [(WebView*)m_webView display];
580 [m_webView setHidden:isHidden];
583 //------------------------------------------------------------
584 // Listener interfaces
585 //------------------------------------------------------------
587 @implementation MyFrameLoadMonitor
589 - initWithWxWindow: (wxWebKitCtrl*)inWindow
592 webKitWindow = inWindow; // non retained
596 - (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame
598 if (frame == [sender mainFrame]){
599 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
600 wxWebKitStateChangedEvent thisEvent(webKitWindow);
601 thisEvent.SetState(wxWEBKIT_STATE_NEGOTIATING);
602 thisEvent.SetURL( wxStringWithNSString( url ) );
603 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
607 - (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame
609 if (frame == [sender mainFrame]){
610 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
611 wxWebKitStateChangedEvent thisEvent(webKitWindow);
612 thisEvent.SetState(wxWEBKIT_STATE_TRANSFERRING);
613 thisEvent.SetURL( wxStringWithNSString( url ) );
614 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
618 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
620 if (frame == [sender mainFrame]){
621 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
622 wxWebKitStateChangedEvent thisEvent(webKitWindow);
623 thisEvent.SetState(wxWEBKIT_STATE_STOP);
624 thisEvent.SetURL( wxStringWithNSString( url ) );
625 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
629 - (void)webView:(WebView *)sender didFailLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
631 if (frame == [sender mainFrame]){
632 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
633 wxWebKitStateChangedEvent thisEvent(webKitWindow);
634 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
635 thisEvent.SetURL( wxStringWithNSString( url ) );
636 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
640 - (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
642 if (frame == [sender mainFrame]){
643 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
644 wxWebKitStateChangedEvent thisEvent(webKitWindow);
645 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
646 thisEvent.SetURL( wxStringWithNSString( url ) );
647 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
651 - (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
653 if (frame == [sender mainFrame]){
654 webKitWindow->SetPageTitle(wxStringWithNSString( title ));
659 @implementation MyPolicyDelegate
661 - initWithWxWindow: (wxWebKitCtrl*)inWindow
664 webKitWindow = inWindow; // non retained
668 - (void)webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener
670 wxWebKitBeforeLoadEvent thisEvent(webKitWindow);
672 // Get the navigation type.
673 NSNumber *n = [actionInformation objectForKey:WebActionNavigationTypeKey];
674 int actionType = [n intValue];
675 thisEvent.SetNavigationType( wxNavTypeFromWebNavType(actionType) );
677 NSString *url = [[request URL] absoluteString];
678 thisEvent.SetURL( wxStringWithNSString( url ) );
680 webKitWindow->GetEventHandler()->ProcessEvent(thisEvent);
682 if (thisEvent.IsCancelled())
690 #endif //wxUSE_WEBKIT