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/html/wklisten.h"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 #if !USE_SHARED_LIBRARY
41 IMPLEMENT_DYNAMIC_CLASS(wxWebKitCtrl, wxControl)
44 BEGIN_EVENT_TABLE(wxWebKitCtrl, wxControl)
45 //EVT_SIZE(wxWebKitCtrl::OnSize)
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 IMPLEMENT_DYNAMIC_CLASS( wxWebKitStateChangedEvent, wxCommandEvent )
54 DEFINE_EVENT_TYPE( wxEVT_WEBKIT_STATE_CHANGED )
56 wxWebKitStateChangedEvent::wxWebKitStateChangedEvent( wxWindow* win )
58 SetEventType( wxEVT_WEBKIT_STATE_CHANGED);
59 SetEventObject( win );
63 //---------------------------------------------------------
64 // helper functions for NSString<->wxString conversion
65 //---------------------------------------------------------
67 inline wxString wxStringWithNSString(NSString *nsstring)
70 return wxString([nsstring UTF8String], wxConvUTF8);
72 return wxString([nsstring lossyCString]);
73 #endif // wxUSE_UNICODE
76 inline NSString* wxNSStringWithWxString(const wxString &wxstring)
79 return [NSString stringWithUTF8String: wxstring.mb_str(wxConvUTF8)];
81 return [NSString stringWithCString: wxstring.c_str() length:wxstring.Len()];
82 #endif // wxUSE_UNICODE
85 @interface MyFrameLoadMonitor : NSObject
87 wxWindow* webKitWindow;
90 - initWithWxWindow: (wxWindow*)inWindow;
94 // ----------------------------------------------------------------------------
95 // creation/destruction
96 // ----------------------------------------------------------------------------
98 bool wxWebKitCtrl::Create(wxWindow *parent,
100 const wxString& strURL,
102 const wxSize& size, long style,
103 const wxValidator& validator,
104 const wxString& name)
107 m_currentURL = strURL;
108 m_pageTitle = wxT("");
110 //still needed for wxCocoa??
114 if (size.x == -1 || size.y == -1)
116 m_parent->GetClientSize(&width, &height);
117 sizeInstance.x = width;
118 sizeInstance.y = height;
122 sizeInstance.x = size.x;
123 sizeInstance.y = size.y;
126 // now create and attach WebKit view...
128 wxControl::Create(parent, m_windowID, pos, sizeInstance, style , validator , name);
129 SetSize(pos.x, pos.y, sizeInstance.x, sizeInstance.y);
131 wxTopLevelWindowCocoa *topWin = wxDynamicCast(this, wxTopLevelWindowCocoa);
132 NSWindow* nsWin = topWin->GetNSWindow();
134 rect.origin.x = pos.x;
135 rect.origin.y = pos.y;
136 rect.size.width = sizeInstance.x;
137 rect.size.height = sizeInstance.y;
138 m_webView = (WebView*)[[WebView alloc] initWithFrame:rect frameName:@"webkitFrame" groupName:@"webkitGroup"];
139 SetNSView(m_webView);
140 [m_cocoaNSView release];
142 if(m_parent) m_parent->CocoaAddChild(this);
143 SetInitialFrameRect(pos,sizeInstance);
145 m_macIsUserPane = false;
146 wxControl::Create(parent, m_windowID, pos, size, style , validator , name);
148 HIWebViewCreate( (HIViewRef*) &m_macControl );
149 MacPostControlCreate(pos, size);
151 HIViewSetVisible( (HIViewRef) m_macControl, true );
153 m_webView = (WebView*) HIWebViewGetWebView( (HIViewRef) m_macControl );
156 // Register event listener interfaces
157 MyFrameLoadMonitor* myFrameLoadMonitor = [[MyFrameLoadMonitor alloc] initWithWxWindow: (wxWindow*)this];
158 [m_webView setFrameLoadDelegate:myFrameLoadMonitor];
159 LoadURL(m_currentURL);
163 wxWebKitCtrl::~wxWebKitCtrl()
168 // ----------------------------------------------------------------------------
170 // ----------------------------------------------------------------------------
172 void wxWebKitCtrl::LoadURL(const wxString &url)
177 [[m_webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:wxNSStringWithWxString(url)]]];
182 bool wxWebKitCtrl::CanGoBack(){
186 return [m_webView canGoBack];
189 bool wxWebKitCtrl::CanGoForward(){
193 return [m_webView canGoForward];
196 bool wxWebKitCtrl::GoBack(){
204 bool wxWebKitCtrl::GoForward(){
208 [m_webView goForward];
212 void wxWebKitCtrl::Reload(){
216 [[m_webView mainFrame] reload];
219 void wxWebKitCtrl::Stop(){
223 [[m_webView mainFrame] stopLoading];
226 bool wxWebKitCtrl::CanGetPageSource(){
230 WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
231 return ( [[dataSource representation] canProvideDocumentSource] );
234 wxString wxWebKitCtrl::GetPageSource(){
238 if (CanGetPageSource()){
239 WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
240 return wxStringWithNSString( [[dataSource representation] documentSource] );
245 void wxWebKitCtrl::SetPageSource(wxString& source, const wxString& baseUrl){
249 if (CanGetPageSource()){
250 [[m_webView mainFrame] loadHTMLString:(NSString*)wxNSStringWithWxString( source ) baseURL:[NSURL URLWithString:wxNSStringWithWxString( baseUrl )]];
256 //------------------------------------------------------------
257 // Listener interfaces
258 //------------------------------------------------------------
260 @implementation MyFrameLoadMonitor
262 - initWithWxWindow: (wxWindow*)inWindow
265 webKitWindow = inWindow; // non retained
269 - (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame
271 if (frame == [sender mainFrame]){
272 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
273 wxWebKitStateChangedEvent thisEvent(webKitWindow);
274 thisEvent.SetState(wxWEBKIT_STATE_NEGOTIATING);
275 thisEvent.SetURL( wxStringWithNSString( url ) );
276 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
280 - (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame
282 if (frame == [sender mainFrame]){
283 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
284 wxWebKitStateChangedEvent thisEvent(webKitWindow);
285 thisEvent.SetState(wxWEBKIT_STATE_TRANSFERRING);
286 thisEvent.SetURL( wxStringWithNSString( url ) );
287 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
291 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
293 if (frame == [sender mainFrame]){
294 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
295 wxWebKitStateChangedEvent thisEvent(webKitWindow);
296 thisEvent.SetState(wxWEBKIT_STATE_STOP);
297 thisEvent.SetURL( wxStringWithNSString( url ) );
298 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
302 - (void)webView:(WebView *)sender didFailLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
304 if (frame == [sender mainFrame]){
305 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
306 wxWebKitStateChangedEvent thisEvent(webKitWindow);
307 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
308 thisEvent.SetURL( wxStringWithNSString( url ) );
309 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
313 - (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
315 if (frame == [sender mainFrame]){
316 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
317 wxWebKitStateChangedEvent thisEvent(webKitWindow);
318 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
319 thisEvent.SetURL( wxStringWithNSString( url ) );
320 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
324 - (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
326 if (frame == [sender mainFrame]){
327 webKitWindow->SetTitle(wxStringWithNSString( title ));