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"
37 #include "wx/notebook.h"
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 #if !USE_SHARED_LIBRARY
45 IMPLEMENT_DYNAMIC_CLASS(wxWebKitCtrl, wxControl)
48 BEGIN_EVENT_TABLE(wxWebKitCtrl, wxControl)
49 EVT_SIZE(wxWebKitCtrl::OnSize)
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 IMPLEMENT_DYNAMIC_CLASS( wxWebKitStateChangedEvent, wxCommandEvent )
58 DEFINE_EVENT_TYPE( wxEVT_WEBKIT_STATE_CHANGED )
60 wxWebKitStateChangedEvent::wxWebKitStateChangedEvent( wxWindow* win )
62 SetEventType( wxEVT_WEBKIT_STATE_CHANGED);
63 SetEventObject( win );
67 //---------------------------------------------------------
68 // helper functions for NSString<->wxString conversion
69 //---------------------------------------------------------
71 inline wxString wxStringWithNSString(NSString *nsstring)
74 return wxString([nsstring UTF8String], wxConvUTF8);
76 return wxString([nsstring lossyCString]);
77 #endif // wxUSE_UNICODE
80 inline NSString* wxNSStringWithWxString(const wxString &wxstring)
83 return [NSString stringWithUTF8String: wxstring.mb_str(wxConvUTF8)];
85 return [NSString stringWithCString: wxstring.c_str() length:wxstring.Len()];
86 #endif // wxUSE_UNICODE
89 @interface MyFrameLoadMonitor : NSObject
91 wxWindow* webKitWindow;
94 - initWithWxWindow: (wxWindow*)inWindow;
98 // ----------------------------------------------------------------------------
99 // creation/destruction
100 // ----------------------------------------------------------------------------
102 bool wxWebKitCtrl::Create(wxWindow *parent,
104 const wxString& strURL,
106 const wxSize& size, long style,
107 const wxValidator& validator,
108 const wxString& name)
111 m_currentURL = strURL;
112 //m_pageTitle = _("Untitled Page");
114 //still needed for wxCocoa??
118 if (size.x == wxDefaultCoord || size.y == wxDefaultCoord)
120 m_parent->GetClientSize(&width, &height);
121 sizeInstance.x = width;
122 sizeInstance.y = height;
126 sizeInstance.x = size.x;
127 sizeInstance.y = size.y;
130 // now create and attach WebKit view...
132 wxControl::Create(parent, m_windowID, pos, sizeInstance, style , validator , name);
133 SetSize(pos.x, pos.y, sizeInstance.x, sizeInstance.y);
135 wxTopLevelWindowCocoa *topWin = wxDynamicCast(this, wxTopLevelWindowCocoa);
136 NSWindow* nsWin = topWin->GetNSWindow();
138 rect.origin.x = pos.x;
139 rect.origin.y = pos.y;
140 rect.size.width = sizeInstance.x;
141 rect.size.height = sizeInstance.y;
142 m_webView = (WebView*)[[WebView alloc] initWithFrame:rect frameName:@"webkitFrame" groupName:@"webkitGroup"];
143 SetNSView(m_webView);
144 [m_cocoaNSView release];
146 if(m_parent) m_parent->CocoaAddChild(this);
147 SetInitialFrameRect(pos,sizeInstance);
149 m_macIsUserPane = false;
150 wxControl::Create(parent, winID, pos, size, style , validator , name);
151 m_peer = new wxMacControl(this);
153 HIWebViewCreate( m_peer->GetControlRefAddr() );
155 m_webView = (WebView*) HIWebViewGetWebView( m_peer->GetControlRef() );
156 MacPostControlCreate(pos, size);
157 HIViewSetVisible( m_peer->GetControlRef(), true );
158 [m_webView setHidden:false];
159 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
160 if ( UMAGetSystemVersion() >= 0x1030 )
161 HIViewChangeFeatures( m_peer->GetControlRef() , kHIViewIsOpaque , 0 ) ;
165 // Register event listener interfaces
166 MyFrameLoadMonitor* myFrameLoadMonitor = [[MyFrameLoadMonitor alloc] initWithWxWindow: (wxWindow*)this];
167 [m_webView setFrameLoadDelegate:myFrameLoadMonitor];
169 LoadURL(m_currentURL);
173 wxWebKitCtrl::~wxWebKitCtrl()
178 // ----------------------------------------------------------------------------
180 // ----------------------------------------------------------------------------
182 void wxWebKitCtrl::LoadURL(const wxString &url)
187 [[m_webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:wxNSStringWithWxString(url)]]];
192 bool wxWebKitCtrl::CanGoBack(){
196 return [m_webView canGoBack];
199 bool wxWebKitCtrl::CanGoForward(){
203 return [m_webView canGoForward];
206 bool wxWebKitCtrl::GoBack(){
210 bool result = [(WebView*)m_webView goBack];
214 bool wxWebKitCtrl::GoForward(){
218 bool result = [(WebView*)m_webView goForward];
222 void wxWebKitCtrl::Reload(){
226 [[m_webView mainFrame] reload];
229 void wxWebKitCtrl::Stop(){
233 [[m_webView mainFrame] stopLoading];
236 bool wxWebKitCtrl::CanGetPageSource(){
240 WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
241 return ( [[dataSource representation] canProvideDocumentSource] );
244 wxString wxWebKitCtrl::GetPageSource(){
246 if (CanGetPageSource()){
247 WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
248 return wxStringWithNSString( [[dataSource representation] documentSource] );
251 return wxEmptyString;
254 void wxWebKitCtrl::SetPageSource(wxString& source, const wxString& baseUrl){
258 if (CanGetPageSource()){
259 [[m_webView mainFrame] loadHTMLString:(NSString*)wxNSStringWithWxString( source ) baseURL:[NSURL URLWithString:wxNSStringWithWxString( baseUrl )]];
264 void wxWebKitCtrl::OnSize(wxSizeEvent &event){
265 // This is a nasty hack because WebKit seems to lose its position when it is embedded
266 // in a control that is not itself the content view for a TLW.
267 // I put it in OnSize because these calcs are not perfect, and in fact are basically
268 // guesses based on reverse engineering, so it's best to give people the option of
269 // overriding OnSize with their own calcs if need be.
270 // I also left some test debugging print statements as a convenience if a(nother)
273 // Let's hope that Tiger fixes this mess...
279 bool isParentTopLevel = true;
281 wxWindow* parent = GetParent();
283 wxWindow* tlw = MacGetTopLevelWindow();
285 // This must be the case that Apple tested with, because well, in this one case
286 // we don't need to do anything! It just works. ;)
291 while(parent != NULL)
293 if ( parent->IsKindOf( CLASSINFO( wxSplitterWindow ) ) && GetParent()->IsKindOf( CLASSINFO( wxSplitterWindow ) ) ){
294 // When parent is not a wxSplitterWindow, we can rely on it's GetPosition() to give us the correct
295 // coordinates, but when the parent is a wxSplitterWindow, we need to manually calculate
296 // the sash position of it and any parent wxSplitterWindows into the webkit's position.
297 wxSplitterWindow* splitter;
298 splitter = dynamic_cast<wxSplitterWindow*>(parent);
299 if (splitter->GetSplitMode() == wxSPLIT_HORIZONTAL){
300 if (splitter->GetPosition().y > 0)
301 y += splitter->GetPosition().y;
303 if (splitter->GetSashSize() > 0)
304 y += splitter->GetSashSize();
306 if (splitter->GetSashPosition() > 0)
307 y += splitter->GetSashPosition();
310 if (splitter->GetPosition().x > 0)
311 x += splitter->GetPosition().x;
313 if (splitter->GetSashSize() > 0)
314 x += splitter->GetSashSize();
316 if (splitter->GetSashPosition() > 0)
317 x += splitter->GetSashPosition();
321 if (!parent->IsTopLevel()) {
322 //printf("Parent: %s\n", parent->GetClassInfo()->GetClassName());
324 plusx = parent->GetClientAreaOrigin().x + parent->GetPosition().x;
327 //printf("Parent: %s Added x: %d\n", parent->GetClassInfo()->GetClassName(), parent->GetClientAreaOrigin().x + parent->GetPosition().x);
331 plusy = parent->GetClientAreaOrigin().y + parent->GetPosition().y;
334 //printf("Parent: %s Added y: %d\n", parent->GetClassInfo()->GetClassName(), parent->GetClientAreaOrigin().y + parent->GetPosition().y);
337 //printf("Parent: %s Origin: %d Position:%d\n", parent->GetClassInfo()->GetClassName(), parent->GetClientAreaOrigin().y, parent->GetPosition().y);
343 x += parent->GetClientAreaOrigin().x;
344 // calculate the title bar height (26 pixels) into the top offset.
345 // This becomes important later when we must flip the y coordinate
346 // to convert to Cocoa's coordinate system.
347 y += parent->GetClientAreaOrigin().y += 26;
348 //printf("x: %d, y:%d\n", x, y);
350 //we still need to add the y, because we have to convert/flip coordinates for Cocoa
352 if ( parent->IsKindOf( CLASSINFO( wxNotebook ) ) ){
353 //Not sure why calcs are off in this one scenario...
355 //printf("x: %d, y:%d\n", x, y);
358 if (parent->IsKindOf( CLASSINFO( wxPanel ) ) ){
359 // Another strange case. Adding a wxPanel to the parent heirarchy
360 // causes wxWebKitCtrl's Cocoa y origin to be 4 pixels off
361 // for some reason, even if the panel has a position and origin of 0.
362 // This corrects that. Man, I wish I could debug Carbon/HIWebView!! ;)
367 parent = parent->GetParent();
370 // Tried using MacWindowToRootWindow both for wxWebKitCtrl and its parent,
371 // but coordinates were off by a significant amount.
372 // Am leaving the code here if anyone wants to play with it.
376 // GetParent()->MacWindowToRootWindow(&x2, &y2);
377 //printf("x = %d, y = %d\n", x, y);
378 //printf("x2 = %d, y2 = %d\n", x2, y2);
383 //flip the y coordinate to convert to Cocoa coordinates
384 //printf("tlw y: %d, y: %d\n", tlw->GetSize().y, (GetSize().y + y));
385 y = tlw->GetSize().y - ((GetSize().y) + y);
388 //printf("Added to bounds x=%d, y=%d\n", x, y);
389 NSRect bounds = [m_webView frame];
392 [m_webView setFrame:bounds];
394 //printf("Carbon position x=%d, y=%d\n", GetPosition().x, GetPosition().y);
396 [(WebView*)m_webView display];
400 void wxWebKitCtrl::MacVisibilityChanged(){
401 bool isHidden = !IsControlVisible( m_peer->GetControlRef());
403 [(WebView*)m_webView display];
405 [m_webView setHidden:isHidden];
408 //------------------------------------------------------------
409 // Listener interfaces
410 //------------------------------------------------------------
412 @implementation MyFrameLoadMonitor
414 - initWithWxWindow: (wxWindow*)inWindow
417 webKitWindow = inWindow; // non retained
421 - (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame
423 if (frame == [sender mainFrame]){
424 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
425 wxWebKitStateChangedEvent thisEvent(webKitWindow);
426 thisEvent.SetState(wxWEBKIT_STATE_NEGOTIATING);
427 thisEvent.SetURL( wxStringWithNSString( url ) );
428 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
432 - (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame
434 if (frame == [sender mainFrame]){
435 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
436 wxWebKitStateChangedEvent thisEvent(webKitWindow);
437 thisEvent.SetState(wxWEBKIT_STATE_TRANSFERRING);
438 thisEvent.SetURL( wxStringWithNSString( url ) );
439 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
443 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
445 if (frame == [sender mainFrame]){
446 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
447 wxWebKitStateChangedEvent thisEvent(webKitWindow);
448 thisEvent.SetState(wxWEBKIT_STATE_STOP);
449 thisEvent.SetURL( wxStringWithNSString( url ) );
450 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
454 - (void)webView:(WebView *)sender didFailLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
456 if (frame == [sender mainFrame]){
457 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
458 wxWebKitStateChangedEvent thisEvent(webKitWindow);
459 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
460 thisEvent.SetURL( wxStringWithNSString( url ) );
461 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
465 - (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
467 if (frame == [sender mainFrame]){
468 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
469 wxWebKitStateChangedEvent thisEvent(webKitWindow);
470 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
471 thisEvent.SetURL( wxStringWithNSString( url ) );
472 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
476 - (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
478 if (frame == [sender mainFrame]){
479 webKitWindow->SetTitle(wxStringWithNSString( title ));
484 #endif //wxUSE_WEBKIT