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/HIWebView.h>
32 #include <WebKit/CarbonUtils.h>
35 #include "wx/html/webkit.h"
36 #include "wx/notebook.h"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 #if !USE_SHARED_LIBRARY
44 IMPLEMENT_DYNAMIC_CLASS(wxWebKitCtrl, wxControl)
47 BEGIN_EVENT_TABLE(wxWebKitCtrl, wxControl)
48 EVT_SIZE(wxWebKitCtrl::OnSize)
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 IMPLEMENT_DYNAMIC_CLASS( wxWebKitStateChangedEvent, wxCommandEvent )
57 DEFINE_EVENT_TYPE( wxEVT_WEBKIT_STATE_CHANGED )
59 wxWebKitStateChangedEvent::wxWebKitStateChangedEvent( wxWindow* win )
61 SetEventType( wxEVT_WEBKIT_STATE_CHANGED);
62 SetEventObject( win );
66 //---------------------------------------------------------
67 // helper functions for NSString<->wxString conversion
68 //---------------------------------------------------------
70 inline wxString wxStringWithNSString(NSString *nsstring)
73 return wxString([nsstring UTF8String], wxConvUTF8);
75 return wxString([nsstring lossyCString]);
76 #endif // wxUSE_UNICODE
79 inline NSString* wxNSStringWithWxString(const wxString &wxstring)
82 return [NSString stringWithUTF8String: wxstring.mb_str(wxConvUTF8)];
84 return [NSString stringWithCString: wxstring.c_str() length:wxstring.Len()];
85 #endif // wxUSE_UNICODE
88 @interface MyFrameLoadMonitor : NSObject
90 wxWindow* webKitWindow;
93 - initWithWxWindow: (wxWindow*)inWindow;
97 // ----------------------------------------------------------------------------
98 // creation/destruction
99 // ----------------------------------------------------------------------------
101 bool wxWebKitCtrl::Create(wxWindow *parent,
103 const wxString& strURL,
105 const wxSize& size, long style,
106 const wxValidator& validator,
107 const wxString& name)
110 m_currentURL = strURL;
111 //m_pageTitle = _("Untitled Page");
113 //still needed for wxCocoa??
117 if (size.x == wxDefaultCoord || size.y == wxDefaultCoord)
119 m_parent->GetClientSize(&width, &height);
120 sizeInstance.x = width;
121 sizeInstance.y = height;
125 sizeInstance.x = size.x;
126 sizeInstance.y = size.y;
129 // now create and attach WebKit view...
131 wxControl::Create(parent, m_windowID, pos, sizeInstance, style , validator , name);
132 SetSize(pos.x, pos.y, sizeInstance.x, sizeInstance.y);
134 wxTopLevelWindowCocoa *topWin = wxDynamicCast(this, wxTopLevelWindowCocoa);
135 NSWindow* nsWin = topWin->GetNSWindow();
137 rect.origin.x = pos.x;
138 rect.origin.y = pos.y;
139 rect.size.width = sizeInstance.x;
140 rect.size.height = sizeInstance.y;
141 m_webView = (WebView*)[[WebView alloc] initWithFrame:rect frameName:@"webkitFrame" groupName:@"webkitGroup"];
142 SetNSView(m_webView);
143 [m_cocoaNSView release];
145 if(m_parent) m_parent->CocoaAddChild(this);
146 SetInitialFrameRect(pos,sizeInstance);
148 m_macIsUserPane = false;
149 wxControl::Create(parent, winID, pos, size, style , validator , name);
150 m_peer = new wxMacControl(this);
152 HIWebViewCreate( m_peer->GetControlRefAddr() );
154 m_webView = (WebView*) HIWebViewGetWebView( m_peer->GetControlRef() );
155 MacPostControlCreate(pos, size);
156 HIViewSetVisible( m_peer->GetControlRef(), true );
157 [m_webView setHidden:false];
160 // Register event listener interfaces
161 MyFrameLoadMonitor* myFrameLoadMonitor = [[MyFrameLoadMonitor alloc] initWithWxWindow: (wxWindow*)this];
162 [m_webView setFrameLoadDelegate:myFrameLoadMonitor];
164 LoadURL(m_currentURL);
168 wxWebKitCtrl::~wxWebKitCtrl()
173 // ----------------------------------------------------------------------------
175 // ----------------------------------------------------------------------------
177 void wxWebKitCtrl::LoadURL(const wxString &url)
182 [[m_webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:wxNSStringWithWxString(url)]]];
187 bool wxWebKitCtrl::CanGoBack(){
191 return [m_webView canGoBack];
194 bool wxWebKitCtrl::CanGoForward(){
198 return [m_webView canGoForward];
201 bool wxWebKitCtrl::GoBack(){
205 bool result = [(WebView*)m_webView goBack];
209 bool wxWebKitCtrl::GoForward(){
213 bool result = [(WebView*)m_webView goForward];
217 void wxWebKitCtrl::Reload(){
221 [[m_webView mainFrame] reload];
224 void wxWebKitCtrl::Stop(){
228 [[m_webView mainFrame] stopLoading];
231 bool wxWebKitCtrl::CanGetPageSource(){
235 WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
236 return ( [[dataSource representation] canProvideDocumentSource] );
239 wxString wxWebKitCtrl::GetPageSource(){
241 if (CanGetPageSource()){
242 WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
243 return wxStringWithNSString( [[dataSource representation] documentSource] );
246 return wxEmptyString;
249 void wxWebKitCtrl::SetPageSource(wxString& source, const wxString& baseUrl){
253 if (CanGetPageSource()){
254 [[m_webView mainFrame] loadHTMLString:(NSString*)wxNSStringWithWxString( source ) baseURL:[NSURL URLWithString:wxNSStringWithWxString( baseUrl )]];
259 void wxWebKitCtrl::OnSize(wxSizeEvent &event){
260 // This is a nasty hack because WebKit seems to lose its position when it is embedded
261 // in a control that is not itself the content view for a TLW.
262 // I put it in OnSize because these calcs are not perfect, and in fact are basically
263 // guesses based on reverse engineering, so it's best to give people the option of
264 // overriding OnSize with their own calcs if need be.
265 // I also left some test debugging print statements as a convenience if a(nother)
268 // Let's hope that Tiger fixes this mess...
274 bool isParentTopLevel = true;
276 wxWindow* parent = GetParent();
278 wxWindow* tlw = MacGetTopLevelWindow();
280 // This must be the case that Apple tested with, because well, in this one case
281 // we don't need to do anything! It just works. ;)
286 while(parent != NULL)
288 if ( parent->IsKindOf( CLASSINFO( wxSplitterWindow ) ) && GetParent()->IsKindOf( CLASSINFO( wxSplitterWindow ) ) ){
289 // When parent is not a wxSplitterWindow, we can rely on it's GetPosition() to give us the correct
290 // coordinates, but when the parent is a wxSplitterWindow, we need to manually calculate
291 // the sash position of it and any parent wxSplitterWindows into the webkit's position.
292 wxSplitterWindow* splitter;
293 splitter = dynamic_cast<wxSplitterWindow*>(parent);
294 if (splitter->GetSplitMode() == wxSPLIT_HORIZONTAL){
295 if (splitter->GetPosition().y > 0)
296 y += splitter->GetPosition().y;
298 if (splitter->GetSashSize() > 0)
299 y += splitter->GetSashSize();
301 if (splitter->GetSashPosition() > 0)
302 y += splitter->GetSashPosition();
305 if (splitter->GetPosition().x > 0)
306 x += splitter->GetPosition().x;
308 if (splitter->GetSashSize() > 0)
309 x += splitter->GetSashSize();
311 if (splitter->GetSashPosition() > 0)
312 x += splitter->GetSashPosition();
316 if (!parent->IsTopLevel()) {
317 //printf("Parent: %s\n", parent->GetClassInfo()->GetClassName());
319 plusx = parent->GetClientAreaOrigin().x + parent->GetPosition().x;
322 //printf("Parent: %s Added x: %d\n", parent->GetClassInfo()->GetClassName(), parent->GetClientAreaOrigin().x + parent->GetPosition().x);
326 plusy = parent->GetClientAreaOrigin().y + parent->GetPosition().y;
329 //printf("Parent: %s Added y: %d\n", parent->GetClassInfo()->GetClassName(), parent->GetClientAreaOrigin().y + parent->GetPosition().y);
332 //printf("Parent: %s Origin: %d Position:%d\n", parent->GetClassInfo()->GetClassName(), parent->GetClientAreaOrigin().y, parent->GetPosition().y);
338 x += parent->GetClientAreaOrigin().x;
339 // calculate the title bar height (26 pixels) into the top offset.
340 // This becomes important later when we must flip the y coordinate
341 // to convert to Cocoa's coordinate system.
342 y += parent->GetClientAreaOrigin().y += 26;
343 //printf("x: %d, y:%d\n", x, y);
345 //we still need to add the y, because we have to convert/flip coordinates for Cocoa
347 if ( parent->IsKindOf( CLASSINFO( wxNotebook ) ) ){
348 //Not sure why calcs are off in this one scenario...
350 //printf("x: %d, y:%d\n", x, y);
353 if (parent->IsKindOf( CLASSINFO( wxPanel ) ) ){
354 // Another strange case. Adding a wxPanel to the parent heirarchy
355 // causes wxWebKitCtrl's Cocoa y origin to be 4 pixels off
356 // for some reason, even if the panel has a position and origin of 0.
357 // This corrects that. Man, I wish I could debug Carbon/HIWebView!! ;)
362 parent = parent->GetParent();
365 // Tried using MacWindowToRootWindow both for wxWebKitCtrl and its parent,
366 // but coordinates were off by a significant amount.
367 // Am leaving the code here if anyone wants to play with it.
371 // GetParent()->MacWindowToRootWindow(&x2, &y2);
372 //printf("x = %d, y = %d\n", x, y);
373 //printf("x2 = %d, y2 = %d\n", x2, y2);
378 //flip the y coordinate to convert to Cocoa coordinates
379 //printf("tlw y: %d, y: %d\n", tlw->GetSize().y, (GetSize().y + y));
380 y = tlw->GetSize().y - ((GetSize().y) + y);
383 //printf("Added to bounds x=%d, y=%d\n", x, y);
384 NSRect bounds = [m_webView frame];
387 [m_webView setFrame:bounds];
389 //printf("Carbon position x=%d, y=%d\n", GetPosition().x, GetPosition().y);
395 void wxWebKitCtrl::MacVisibilityChanged(){
396 bool isHidden = !IsControlVisible( m_peer->GetControlRef());
400 [m_webView setHidden:isHidden];
403 //------------------------------------------------------------
404 // Listener interfaces
405 //------------------------------------------------------------
407 @implementation MyFrameLoadMonitor
409 - initWithWxWindow: (wxWindow*)inWindow
412 webKitWindow = inWindow; // non retained
416 - (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame
418 if (frame == [sender mainFrame]){
419 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
420 wxWebKitStateChangedEvent thisEvent(webKitWindow);
421 thisEvent.SetState(wxWEBKIT_STATE_NEGOTIATING);
422 thisEvent.SetURL( wxStringWithNSString( url ) );
423 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
427 - (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame
429 if (frame == [sender mainFrame]){
430 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
431 wxWebKitStateChangedEvent thisEvent(webKitWindow);
432 thisEvent.SetState(wxWEBKIT_STATE_TRANSFERRING);
433 thisEvent.SetURL( wxStringWithNSString( url ) );
434 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
438 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
440 if (frame == [sender mainFrame]){
441 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
442 wxWebKitStateChangedEvent thisEvent(webKitWindow);
443 thisEvent.SetState(wxWEBKIT_STATE_STOP);
444 thisEvent.SetURL( wxStringWithNSString( url ) );
445 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
449 - (void)webView:(WebView *)sender didFailLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
451 if (frame == [sender mainFrame]){
452 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
453 wxWebKitStateChangedEvent thisEvent(webKitWindow);
454 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
455 thisEvent.SetURL( wxStringWithNSString( url ) );
456 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
460 - (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
462 if (frame == [sender mainFrame]){
463 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
464 wxWebKitStateChangedEvent thisEvent(webKitWindow);
465 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
466 thisEvent.SetURL( wxStringWithNSString( url ) );
467 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
471 - (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
473 if (frame == [sender mainFrame]){
474 webKitWindow->SetTitle(wxStringWithNSString( title ));
479 #endif //wxUSE_WEBKIT