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"
24 #include "wx/cocoa/autorelease.h"
26 #include "wx/mac/uma.h"
27 #include <Carbon/Carbon.h>
28 #include <WebKit/HIWebView.h>
29 #include <WebKit/CarbonUtils.h>
32 #include "wx/html/webkit.h"
33 #include "wx/notebook.h"
34 //#include "wx/html/wklisten.h"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 #if !USE_SHARED_LIBRARY
42 IMPLEMENT_DYNAMIC_CLASS(wxWebKitCtrl, wxControl)
45 BEGIN_EVENT_TABLE(wxWebKitCtrl, wxControl)
46 EVT_SIZE(wxWebKitCtrl::OnSize)
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 IMPLEMENT_DYNAMIC_CLASS( wxWebKitStateChangedEvent, wxCommandEvent )
55 DEFINE_EVENT_TYPE( wxEVT_WEBKIT_STATE_CHANGED )
57 wxWebKitStateChangedEvent::wxWebKitStateChangedEvent( wxWindow* win )
59 SetEventType( wxEVT_WEBKIT_STATE_CHANGED);
60 SetEventObject( win );
64 //---------------------------------------------------------
65 // helper functions for NSString<->wxString conversion
66 //---------------------------------------------------------
68 inline wxString wxStringWithNSString(NSString *nsstring)
71 return wxString([nsstring UTF8String], wxConvUTF8);
73 return wxString([nsstring lossyCString]);
74 #endif // wxUSE_UNICODE
77 inline NSString* wxNSStringWithWxString(const wxString &wxstring)
80 return [NSString stringWithUTF8String: wxstring.mb_str(wxConvUTF8)];
82 return [NSString stringWithCString: wxstring.c_str() length:wxstring.Len()];
83 #endif // wxUSE_UNICODE
86 @interface MyFrameLoadMonitor : NSObject
88 wxWindow* webKitWindow;
91 - initWithWxWindow: (wxWindow*)inWindow;
95 // ----------------------------------------------------------------------------
96 // creation/destruction
97 // ----------------------------------------------------------------------------
99 bool wxWebKitCtrl::Create(wxWindow *parent,
101 const wxString& strURL,
103 const wxSize& size, long style,
104 const wxValidator& validator,
105 const wxString& name)
108 m_currentURL = strURL;
109 //m_pageTitle = _("Untitled Page");
111 //still needed for wxCocoa??
115 if (size.x == -1 || size.y == -1)
117 m_parent->GetClientSize(&width, &height);
118 sizeInstance.x = width;
119 sizeInstance.y = height;
123 sizeInstance.x = size.x;
124 sizeInstance.y = size.y;
127 // now create and attach WebKit view...
129 wxControl::Create(parent, m_windowID, pos, sizeInstance, style , validator , name);
130 SetSize(pos.x, pos.y, sizeInstance.x, sizeInstance.y);
132 wxTopLevelWindowCocoa *topWin = wxDynamicCast(this, wxTopLevelWindowCocoa);
133 NSWindow* nsWin = topWin->GetNSWindow();
135 rect.origin.x = pos.x;
136 rect.origin.y = pos.y;
137 rect.size.width = sizeInstance.x;
138 rect.size.height = sizeInstance.y;
139 m_webView = (WebView*)[[WebView alloc] initWithFrame:rect frameName:@"webkitFrame" groupName:@"webkitGroup"];
140 SetNSView(m_webView);
141 [m_cocoaNSView release];
143 if(m_parent) m_parent->CocoaAddChild(this);
144 SetInitialFrameRect(pos,sizeInstance);
146 m_macIsUserPane = false;
147 wxControl::Create(parent, m_windowID, pos, size, style , validator , name);
149 HIWebViewCreate( (HIViewRef*) &m_macControl );
151 m_webView = (WebView*) HIWebViewGetWebView( (HIViewRef) m_macControl );
152 MacPostControlCreate(pos, size);
154 HIViewSetVisible( (HIViewRef) m_macControl, true );
157 // Register event listener interfaces
158 MyFrameLoadMonitor* myFrameLoadMonitor = [[MyFrameLoadMonitor alloc] initWithWxWindow: (wxWindow*)this];
159 [m_webView setFrameLoadDelegate:myFrameLoadMonitor];
161 LoadURL(m_currentURL);
165 wxWebKitCtrl::~wxWebKitCtrl()
170 // ----------------------------------------------------------------------------
172 // ----------------------------------------------------------------------------
174 void wxWebKitCtrl::LoadURL(const wxString &url)
179 [[m_webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:wxNSStringWithWxString(url)]]];
184 bool wxWebKitCtrl::CanGoBack(){
188 return [m_webView canGoBack];
191 bool wxWebKitCtrl::CanGoForward(){
195 return [m_webView canGoForward];
198 bool wxWebKitCtrl::GoBack(){
206 bool wxWebKitCtrl::GoForward(){
210 [m_webView goForward];
214 void wxWebKitCtrl::Reload(){
218 [[m_webView mainFrame] reload];
221 void wxWebKitCtrl::Stop(){
225 [[m_webView mainFrame] stopLoading];
228 bool wxWebKitCtrl::CanGetPageSource(){
232 WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
233 return ( [[dataSource representation] canProvideDocumentSource] );
236 wxString wxWebKitCtrl::GetPageSource(){
240 if (CanGetPageSource()){
241 WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
242 return wxStringWithNSString( [[dataSource representation] documentSource] );
247 void wxWebKitCtrl::SetPageSource(wxString& source, const wxString& baseUrl){
251 if (CanGetPageSource()){
252 [[m_webView mainFrame] loadHTMLString:(NSString*)wxNSStringWithWxString( source ) baseURL:[NSURL URLWithString:wxNSStringWithWxString( baseUrl )]];
257 void wxWebKitCtrl::OnSize(wxSizeEvent &event){
258 wxWindow* parent = GetParent();
259 bool inNotebook = false;
262 while(parent != NULL)
264 x += parent->GetPosition().x;
265 y += parent->GetPosition().y;
266 if( parent->IsKindOf( CLASSINFO( wxNotebook ) ) ){
270 parent = parent->GetParent();
274 NSRect bounds = [m_webView frame];
275 bounds.origin.x += x;
276 bounds.origin.y += y;
277 [m_webView setFrame:bounds];
284 void wxWebKitCtrl::MacVisibilityChanged(){
285 bool isHidden = !IsControlVisible( (HIViewRef)m_macControl);
286 [m_webView setHidden:isHidden];
289 //------------------------------------------------------------
290 // Listener interfaces
291 //------------------------------------------------------------
293 @implementation MyFrameLoadMonitor
295 - initWithWxWindow: (wxWindow*)inWindow
298 webKitWindow = inWindow; // non retained
302 - (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame
304 if (frame == [sender mainFrame]){
305 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
306 wxWebKitStateChangedEvent thisEvent(webKitWindow);
307 thisEvent.SetState(wxWEBKIT_STATE_NEGOTIATING);
308 thisEvent.SetURL( wxStringWithNSString( url ) );
309 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
313 - (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame
315 if (frame == [sender mainFrame]){
316 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
317 wxWebKitStateChangedEvent thisEvent(webKitWindow);
318 thisEvent.SetState(wxWEBKIT_STATE_TRANSFERRING);
319 thisEvent.SetURL( wxStringWithNSString( url ) );
320 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
324 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
326 if (frame == [sender mainFrame]){
327 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
328 wxWebKitStateChangedEvent thisEvent(webKitWindow);
329 thisEvent.SetState(wxWEBKIT_STATE_STOP);
330 thisEvent.SetURL( wxStringWithNSString( url ) );
331 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
335 - (void)webView:(WebView *)sender didFailLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
337 if (frame == [sender mainFrame]){
338 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
339 wxWebKitStateChangedEvent thisEvent(webKitWindow);
340 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
341 thisEvent.SetURL( wxStringWithNSString( url ) );
342 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
346 - (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
348 if (frame == [sender mainFrame]){
349 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
350 wxWebKitStateChangedEvent thisEvent(webKitWindow);
351 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
352 thisEvent.SetURL( wxStringWithNSString( url ) );
353 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
357 - (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
359 if (frame == [sender mainFrame]){
360 webKitWindow->SetTitle(wxStringWithNSString( title ));