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 // 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/mac/uma.h"
26 #include <Carbon/Carbon.h>
27 #include <WebKit/WebKit.h>
28 #include <WebKit/HIWebView.h>
29 #include <WebKit/CarbonUtils.h>
32 #include "wx/html/webkit.h"
33 #include "wx/notebook.h"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 IMPLEMENT_DYNAMIC_CLASS(wxWebKitCtrl, wxControl)
42 BEGIN_EVENT_TABLE(wxWebKitCtrl, wxControl)
43 EVT_SIZE(wxWebKitCtrl::OnSize)
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 IMPLEMENT_DYNAMIC_CLASS( wxWebKitStateChangedEvent, wxCommandEvent )
52 DEFINE_EVENT_TYPE( wxEVT_WEBKIT_STATE_CHANGED )
54 wxWebKitStateChangedEvent::wxWebKitStateChangedEvent( wxWindow* win )
56 SetEventType( wxEVT_WEBKIT_STATE_CHANGED);
57 SetEventObject( win );
61 //---------------------------------------------------------
62 // helper functions for NSString<->wxString conversion
63 //---------------------------------------------------------
65 inline wxString wxStringWithNSString(NSString *nsstring)
68 return wxString([nsstring UTF8String], wxConvUTF8);
70 return wxString([nsstring lossyCString]);
71 #endif // wxUSE_UNICODE
74 inline NSString* wxNSStringWithWxString(const wxString &wxstring)
77 return [NSString stringWithUTF8String: wxstring.mb_str(wxConvUTF8)];
79 return [NSString stringWithCString: wxstring.c_str() length:wxstring.Len()];
80 #endif // wxUSE_UNICODE
83 @interface MyFrameLoadMonitor : NSObject
85 wxWindow* webKitWindow;
88 - initWithWxWindow: (wxWindow*)inWindow;
92 // ----------------------------------------------------------------------------
93 // creation/destruction
94 // ----------------------------------------------------------------------------
96 bool wxWebKitCtrl::Create(wxWindow *parent,
98 const wxString& strURL,
100 const wxSize& size, long style,
101 const wxValidator& validator,
102 const wxString& name)
105 m_currentURL = strURL;
106 //m_pageTitle = _("Untitled Page");
108 //still needed for wxCocoa??
112 if (size.x == wxDefaultCoord || size.y == wxDefaultCoord)
114 m_parent->GetClientSize(&width, &height);
115 sizeInstance.x = width;
116 sizeInstance.y = height;
120 sizeInstance.x = size.x;
121 sizeInstance.y = size.y;
124 // now create and attach WebKit view...
126 wxControl::Create(parent, m_windowID, pos, sizeInstance, style , validator , name);
127 SetSize(pos.x, pos.y, sizeInstance.x, sizeInstance.y);
129 wxTopLevelWindowCocoa *topWin = wxDynamicCast(this, wxTopLevelWindowCocoa);
130 NSWindow* nsWin = topWin->GetNSWindow();
132 rect.origin.x = pos.x;
133 rect.origin.y = pos.y;
134 rect.size.width = sizeInstance.x;
135 rect.size.height = sizeInstance.y;
136 m_webView = (WebView*)[[WebView alloc] initWithFrame:rect frameName:@"webkitFrame" groupName:@"webkitGroup"];
137 SetNSView(m_webView);
138 [m_cocoaNSView release];
140 if(m_parent) m_parent->CocoaAddChild(this);
141 SetInitialFrameRect(pos,sizeInstance);
143 m_macIsUserPane = false;
144 wxControl::Create(parent, winID, pos, size, style , validator , name);
145 m_peer = new wxMacControl(this);
147 HIWebViewCreate( m_peer->GetControlRefAddr() );
149 m_webView = (WebView*) HIWebViewGetWebView( m_peer->GetControlRef() );
150 MacPostControlCreate(pos, size);
151 HIViewSetVisible( m_peer->GetControlRef(), true );
152 [m_webView setHidden:false];
153 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
154 if ( UMAGetSystemVersion() >= 0x1030 )
155 HIViewChangeFeatures( m_peer->GetControlRef() , kHIViewIsOpaque , 0 ) ;
159 // Register event listener interfaces
160 MyFrameLoadMonitor* myFrameLoadMonitor = [[MyFrameLoadMonitor alloc] initWithWxWindow: (wxWindow*)this];
161 [m_webView setFrameLoadDelegate:myFrameLoadMonitor];
163 LoadURL(m_currentURL);
167 wxWebKitCtrl::~wxWebKitCtrl()
172 // ----------------------------------------------------------------------------
174 // ----------------------------------------------------------------------------
176 void wxWebKitCtrl::LoadURL(const wxString &url)
181 [[m_webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:wxNSStringWithWxString(url)]]];
186 bool wxWebKitCtrl::CanGoBack(){
190 return [m_webView canGoBack];
193 bool wxWebKitCtrl::CanGoForward(){
197 return [m_webView canGoForward];
200 bool wxWebKitCtrl::GoBack(){
204 bool result = [(WebView*)m_webView goBack];
208 bool wxWebKitCtrl::GoForward(){
212 bool result = [(WebView*)m_webView goForward];
216 void wxWebKitCtrl::Reload(){
220 [[m_webView mainFrame] reload];
223 void wxWebKitCtrl::Stop(){
227 [[m_webView mainFrame] stopLoading];
230 bool wxWebKitCtrl::CanGetPageSource(){
234 WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
235 return ( [[dataSource representation] canProvideDocumentSource] );
238 wxString wxWebKitCtrl::GetPageSource(){
240 if (CanGetPageSource()){
241 WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
242 return wxStringWithNSString( [[dataSource representation] documentSource] );
245 return wxEmptyString;
248 void wxWebKitCtrl::SetPageSource(wxString& source, const wxString& baseUrl){
252 [[m_webView mainFrame] loadHTMLString:(NSString*)wxNSStringWithWxString( source ) baseURL:[NSURL URLWithString:wxNSStringWithWxString( baseUrl )]];
255 void wxWebKitCtrl::OnSize(wxSizeEvent &event){
256 // This is a nasty hack because WebKit seems to lose its position when it is embedded
257 // in a control that is not itself the content view for a TLW.
258 // I put it in OnSize because these calcs are not perfect, and in fact are basically
259 // guesses based on reverse engineering, so it's best to give people the option of
260 // overriding OnSize with their own calcs if need be.
261 // I also left some test debugging print statements as a convenience if a(nother)
264 // Let's hope that Tiger fixes this mess...
270 wxWindow* parent = GetParent();
272 wxWindow* tlw = MacGetTopLevelWindow();
274 // This must be the case that Apple tested with, because well, in this one case
275 // we don't need to do anything! It just works. ;)
280 while(parent != NULL)
282 if ( parent->IsKindOf( CLASSINFO( wxSplitterWindow ) ) && GetParent()->IsKindOf( CLASSINFO( wxSplitterWindow ) ) ){
283 // When parent is not a wxSplitterWindow, we can rely on it's GetPosition() to give us the correct
284 // coordinates, but when the parent is a wxSplitterWindow, we need to manually calculate
285 // the sash position of it and any parent wxSplitterWindows into the webkit's position.
286 wxSplitterWindow* splitter;
287 splitter = dynamic_cast<wxSplitterWindow*>(parent);
288 if (splitter->GetSplitMode() == wxSPLIT_HORIZONTAL){
289 if (splitter->GetPosition().y > 0)
290 y += splitter->GetPosition().y;
292 if (splitter->GetSashSize() > 0)
293 y += splitter->GetSashSize();
295 if (splitter->GetSashPosition() > 0)
296 y += splitter->GetSashPosition();
299 if (splitter->GetPosition().x > 0)
300 x += splitter->GetPosition().x;
302 if (splitter->GetSashSize() > 0)
303 x += splitter->GetSashSize();
305 if (splitter->GetSashPosition() > 0)
306 x += splitter->GetSashPosition();
310 if (!parent->IsTopLevel()) {
311 //printf("Parent: %s\n", parent->GetClassInfo()->GetClassName());
313 plusx = parent->GetClientAreaOrigin().x + parent->GetPosition().x;
316 //printf("Parent: %s Added x: %d\n", parent->GetClassInfo()->GetClassName(), parent->GetClientAreaOrigin().x + parent->GetPosition().x);
320 plusy = parent->GetClientAreaOrigin().y + parent->GetPosition().y;
323 //printf("Parent: %s Added y: %d\n", parent->GetClassInfo()->GetClassName(), parent->GetClientAreaOrigin().y + parent->GetPosition().y);
326 //printf("Parent: %s Origin: %d Position:%d\n", parent->GetClassInfo()->GetClassName(), parent->GetClientAreaOrigin().y, parent->GetPosition().y);
332 x += parent->GetClientAreaOrigin().x;
333 // calculate the title bar height (26 pixels) into the top offset.
334 // This becomes important later when we must flip the y coordinate
335 // to convert to Cocoa's coordinate system.
336 y += parent->GetClientAreaOrigin().y += 26;
337 //printf("x: %d, y:%d\n", x, y);
339 //we still need to add the y, because we have to convert/flip coordinates for Cocoa
341 if ( parent->IsKindOf( CLASSINFO( wxNotebook ) ) ){
342 //Not sure why calcs are off in this one scenario...
344 //printf("x: %d, y:%d\n", x, y);
347 if (parent->IsKindOf( CLASSINFO( wxPanel ) ) ){
348 // Another strange case. Adding a wxPanel to the parent heirarchy
349 // causes wxWebKitCtrl's Cocoa y origin to be 4 pixels off
350 // for some reason, even if the panel has a position and origin of 0.
351 // This corrects that. Man, I wish I could debug Carbon/HIWebView!! ;)
356 parent = parent->GetParent();
359 // Tried using MacWindowToRootWindow both for wxWebKitCtrl and its parent,
360 // but coordinates were off by a significant amount.
361 // Am leaving the code here if anyone wants to play with it.
365 // GetParent()->MacWindowToRootWindow(&x2, &y2);
366 //printf("x = %d, y = %d\n", x, y);
367 //printf("x2 = %d, y2 = %d\n", x2, y2);
372 //flip the y coordinate to convert to Cocoa coordinates
373 //printf("tlw y: %d, y: %d\n", tlw->GetSize().y, (GetSize().y + y));
374 y = tlw->GetSize().y - ((GetSize().y) + y);
377 //printf("Added to bounds x=%d, y=%d\n", x, y);
378 NSRect bounds = [m_webView frame];
381 [m_webView setFrame:bounds];
383 //printf("Carbon position x=%d, y=%d\n", GetPosition().x, GetPosition().y);
385 [(WebView*)m_webView display];
389 void wxWebKitCtrl::MacVisibilityChanged(){
390 bool isHidden = !IsControlVisible( m_peer->GetControlRef());
392 [(WebView*)m_webView display];
394 [m_webView setHidden:isHidden];
397 //------------------------------------------------------------
398 // Listener interfaces
399 //------------------------------------------------------------
401 @implementation MyFrameLoadMonitor
403 - initWithWxWindow: (wxWindow*)inWindow
406 webKitWindow = inWindow; // non retained
410 - (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame
412 if (frame == [sender mainFrame]){
413 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
414 wxWebKitStateChangedEvent thisEvent(webKitWindow);
415 thisEvent.SetState(wxWEBKIT_STATE_NEGOTIATING);
416 thisEvent.SetURL( wxStringWithNSString( url ) );
417 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
421 - (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame
423 if (frame == [sender mainFrame]){
424 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
425 wxWebKitStateChangedEvent thisEvent(webKitWindow);
426 thisEvent.SetState(wxWEBKIT_STATE_TRANSFERRING);
427 thisEvent.SetURL( wxStringWithNSString( url ) );
428 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
432 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
434 if (frame == [sender mainFrame]){
435 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
436 wxWebKitStateChangedEvent thisEvent(webKitWindow);
437 thisEvent.SetState(wxWEBKIT_STATE_STOP);
438 thisEvent.SetURL( wxStringWithNSString( url ) );
439 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
443 - (void)webView:(WebView *)sender didFailLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
445 if (frame == [sender mainFrame]){
446 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
447 wxWebKitStateChangedEvent thisEvent(webKitWindow);
448 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
449 thisEvent.SetURL( wxStringWithNSString( url ) );
450 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
454 - (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
456 if (frame == [sender mainFrame]){
457 NSString *url = [[[[frame provisionalDataSource] 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 didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
467 if (frame == [sender mainFrame]){
468 webKitWindow->SetLabel(wxStringWithNSString( title ));
473 #endif //wxUSE_WEBKIT