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 )]];
255 void wxWebKitCtrl::OnSize(wxSizeEvent &event){
260 //------------------------------------------------------------
261 // Listener interfaces
262 //------------------------------------------------------------
264 @implementation MyFrameLoadMonitor
266 - initWithWxWindow: (wxWindow*)inWindow
269 webKitWindow = inWindow; // non retained
273 - (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame
275 if (frame == [sender mainFrame]){
276 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
277 wxWebKitStateChangedEvent thisEvent(webKitWindow);
278 thisEvent.SetState(wxWEBKIT_STATE_NEGOTIATING);
279 thisEvent.SetURL( wxStringWithNSString( url ) );
280 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
284 - (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame
286 if (frame == [sender mainFrame]){
287 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
288 wxWebKitStateChangedEvent thisEvent(webKitWindow);
289 thisEvent.SetState(wxWEBKIT_STATE_TRANSFERRING);
290 thisEvent.SetURL( wxStringWithNSString( url ) );
291 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
295 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
297 if (frame == [sender mainFrame]){
298 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
299 wxWebKitStateChangedEvent thisEvent(webKitWindow);
300 thisEvent.SetState(wxWEBKIT_STATE_STOP);
301 thisEvent.SetURL( wxStringWithNSString( url ) );
302 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
306 - (void)webView:(WebView *)sender didFailLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
308 if (frame == [sender mainFrame]){
309 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
310 wxWebKitStateChangedEvent thisEvent(webKitWindow);
311 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
312 thisEvent.SetURL( wxStringWithNSString( url ) );
313 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
317 - (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
319 if (frame == [sender mainFrame]){
320 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
321 wxWebKitStateChangedEvent thisEvent(webKitWindow);
322 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
323 thisEvent.SetURL( wxStringWithNSString( url ) );
324 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
328 - (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
330 if (frame == [sender mainFrame]){
331 webKitWindow->SetTitle(wxStringWithNSString( title ));