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 if ( GetParent()->IsKindOf( CLASSINFO( wxNotebook) ) ){
259 NSRect bounds = [m_webView frame];
260 bounds.origin.x += GetParent()->GetPosition().x;
261 bounds.origin.y += 18;
262 [m_webView setFrame:bounds];
268 void wxWebKitCtrl::MacVisibilityChanged(){
269 bool isHidden = !IsControlVisible( (HIViewRef)m_macControl);
270 [m_webView setHidden:isHidden];
273 //------------------------------------------------------------
274 // Listener interfaces
275 //------------------------------------------------------------
277 @implementation MyFrameLoadMonitor
279 - initWithWxWindow: (wxWindow*)inWindow
282 webKitWindow = inWindow; // non retained
286 - (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame
288 if (frame == [sender mainFrame]){
289 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
290 wxWebKitStateChangedEvent thisEvent(webKitWindow);
291 thisEvent.SetState(wxWEBKIT_STATE_NEGOTIATING);
292 thisEvent.SetURL( wxStringWithNSString( url ) );
293 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
297 - (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame
299 if (frame == [sender mainFrame]){
300 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
301 wxWebKitStateChangedEvent thisEvent(webKitWindow);
302 thisEvent.SetState(wxWEBKIT_STATE_TRANSFERRING);
303 thisEvent.SetURL( wxStringWithNSString( url ) );
304 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
308 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
310 if (frame == [sender mainFrame]){
311 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
312 wxWebKitStateChangedEvent thisEvent(webKitWindow);
313 thisEvent.SetState(wxWEBKIT_STATE_STOP);
314 thisEvent.SetURL( wxStringWithNSString( url ) );
315 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
319 - (void)webView:(WebView *)sender didFailLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
321 if (frame == [sender mainFrame]){
322 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
323 wxWebKitStateChangedEvent thisEvent(webKitWindow);
324 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
325 thisEvent.SetURL( wxStringWithNSString( url ) );
326 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
330 - (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
332 if (frame == [sender mainFrame]){
333 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
334 wxWebKitStateChangedEvent thisEvent(webKitWindow);
335 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
336 thisEvent.SetURL( wxStringWithNSString( url ) );
337 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
341 - (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
343 if (frame == [sender mainFrame]){
344 webKitWindow->SetTitle(wxStringWithNSString( title ));