]> git.saurik.com Git - wxWidgets.git/blame - src/html/htmlctrl/webkit/webkit.mm
Missed conditional compilation.
[wxWidgets.git] / src / html / htmlctrl / webkit / webkit.mm
CommitLineData
2c990ba6
KO
1/////////////////////////////////////////////////////////////////////////////
2// Name: webkit.mm
3// Purpose: wxWebKitCtrl - embeddable web kit control
4// Author: Jethro Grassie / Kevin Ollivier
5// Modified by:
6// Created: 2004-4-16
7// RCS-ID: $Id$
8// Copyright: (c) Jethro Grassie / Kevin Ollivier
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
3ca7ba74 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
7eb8c6ee 13 #pragma implementation "webkit.h"
2c990ba6
KO
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
2c990ba6
KO
19#ifndef WX_PRECOMP
20 #include "wx/wx.h"
21#endif
22
948f6c6e
JS
23#if wxUSE_WEBKIT
24
2c990ba6
KO
25#ifdef __WXCOCOA__
26#include "wx/cocoa/autorelease.h"
27#else
28#include "wx/mac/uma.h"
29#include <Carbon/Carbon.h>
30#include <WebKit/HIWebView.h>
31#include <WebKit/CarbonUtils.h>
32#endif
33
34#include "wx/html/webkit.h"
14f21d6d 35#include "wx/notebook.h"
2c990ba6
KO
36
37
38// ----------------------------------------------------------------------------
39// macros
40// ----------------------------------------------------------------------------
41
42#if !USE_SHARED_LIBRARY
43IMPLEMENT_DYNAMIC_CLASS(wxWebKitCtrl, wxControl)
44#endif
45
46BEGIN_EVENT_TABLE(wxWebKitCtrl, wxControl)
005198fa 47 EVT_SIZE(wxWebKitCtrl::OnSize)
2c990ba6
KO
48END_EVENT_TABLE()
49
50// ----------------------------------------------------------------------------
51// wxWebKit Events
52// ----------------------------------------------------------------------------
53
54IMPLEMENT_DYNAMIC_CLASS( wxWebKitStateChangedEvent, wxCommandEvent )
55
56DEFINE_EVENT_TYPE( wxEVT_WEBKIT_STATE_CHANGED )
57
58wxWebKitStateChangedEvent::wxWebKitStateChangedEvent( wxWindow* win )
59{
60 SetEventType( wxEVT_WEBKIT_STATE_CHANGED);
61 SetEventObject( win );
3ca7ba74 62 SetId(win->GetId());
2c990ba6
KO
63}
64
65//---------------------------------------------------------
66// helper functions for NSString<->wxString conversion
67//---------------------------------------------------------
68
69inline wxString wxStringWithNSString(NSString *nsstring)
70{
71#if wxUSE_UNICODE
72 return wxString([nsstring UTF8String], wxConvUTF8);
73#else
74 return wxString([nsstring lossyCString]);
75#endif // wxUSE_UNICODE
76}
77
78inline NSString* wxNSStringWithWxString(const wxString &wxstring)
79{
80#if wxUSE_UNICODE
81 return [NSString stringWithUTF8String: wxstring.mb_str(wxConvUTF8)];
82#else
83 return [NSString stringWithCString: wxstring.c_str() length:wxstring.Len()];
84#endif // wxUSE_UNICODE
85}
86
87@interface MyFrameLoadMonitor : NSObject
88{
89 wxWindow* webKitWindow;
90}
91
92- initWithWxWindow: (wxWindow*)inWindow;
93
94@end
95
96// ----------------------------------------------------------------------------
97// creation/destruction
98// ----------------------------------------------------------------------------
99
100bool wxWebKitCtrl::Create(wxWindow *parent,
3ca7ba74 101 wxWindowID winID,
2c990ba6
KO
102 const wxString& strURL,
103 const wxPoint& pos,
3ca7ba74
RD
104 const wxSize& size, long style,
105 const wxValidator& validator,
106 const wxString& name)
2c990ba6
KO
107{
108
109 m_currentURL = strURL;
14f21d6d 110 //m_pageTitle = _("Untitled Page");
2c990ba6
KO
111
112 //still needed for wxCocoa??
113/*
114 int width, height;
3ca7ba74
RD
115 wxSize sizeInstance;
116 if (size.x == -1 || size.y == -1)
117 {
118 m_parent->GetClientSize(&width, &height);
119 sizeInstance.x = width;
120 sizeInstance.y = height;
121 }
122 else
123 {
124 sizeInstance.x = size.x;
125 sizeInstance.y = size.y;
126 }
127*/
128 // now create and attach WebKit view...
2c990ba6
KO
129#ifdef __WXCOCOA__
130 wxControl::Create(parent, m_windowID, pos, sizeInstance, style , validator , name);
3ca7ba74 131 SetSize(pos.x, pos.y, sizeInstance.x, sizeInstance.y);
2c990ba6
KO
132
133 wxTopLevelWindowCocoa *topWin = wxDynamicCast(this, wxTopLevelWindowCocoa);
134 NSWindow* nsWin = topWin->GetNSWindow();
3ca7ba74 135 NSRect rect;
2c990ba6
KO
136 rect.origin.x = pos.x;
137 rect.origin.y = pos.y;
138 rect.size.width = sizeInstance.x;
139 rect.size.height = sizeInstance.y;
140 m_webView = (WebView*)[[WebView alloc] initWithFrame:rect frameName:@"webkitFrame" groupName:@"webkitGroup"];
141 SetNSView(m_webView);
142 [m_cocoaNSView release];
143
144 if(m_parent) m_parent->CocoaAddChild(this);
145 SetInitialFrameRect(pos,sizeInstance);
146#else
147 m_macIsUserPane = false;
8de5b24e 148 m_peer = new wxMacControl();
2c990ba6
KO
149 wxControl::Create(parent, m_windowID, pos, size, style , validator , name);
150 WebInitForCarbon();
f8405d6e 151 HIWebViewCreate( m_peer->GetControlRefAddr() );
2c990ba6 152
f8405d6e 153 m_webView = (WebView*) HIWebViewGetWebView( m_peer->GetControlRef() );
14f21d6d 154 MacPostControlCreate(pos, size);
f8405d6e 155 HIViewSetVisible( m_peer->GetControlRef(), true );
5b8f917c 156 [m_webView setHidden:false];
2c990ba6
KO
157#endif
158
159 // Register event listener interfaces
160 MyFrameLoadMonitor* myFrameLoadMonitor = [[MyFrameLoadMonitor alloc] initWithWxWindow: (wxWindow*)this];
3ca7ba74 161 [m_webView setFrameLoadDelegate:myFrameLoadMonitor];
14f21d6d 162
2c990ba6
KO
163 LoadURL(m_currentURL);
164 return true;
165}
166
167wxWebKitCtrl::~wxWebKitCtrl()
168{
169
170}
171
172// ----------------------------------------------------------------------------
173// public methods
174// ----------------------------------------------------------------------------
175
176void wxWebKitCtrl::LoadURL(const wxString &url)
177{
178 if( !m_webView )
179 return;
180
7eb8c6ee 181 [[m_webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:wxNSStringWithWxString(url)]]];
2c990ba6
KO
182
183 m_currentURL = url;
184}
185
186bool wxWebKitCtrl::CanGoBack(){
187 if ( !m_webView )
188 return false;
189
190 return [m_webView canGoBack];
191}
192
193bool wxWebKitCtrl::CanGoForward(){
194 if ( !m_webView )
195 return false;
196
197 return [m_webView canGoForward];
198}
199
200bool wxWebKitCtrl::GoBack(){
201 if ( !m_webView )
202 return false;
203
204 [m_webView goBack];
205 return true;
206}
207
208bool wxWebKitCtrl::GoForward(){
209 if ( !m_webView )
210 return false;
211
212 [m_webView goForward];
213 return true;
214}
215
216void wxWebKitCtrl::Reload(){
217 if ( !m_webView )
218 return;
219
220 [[m_webView mainFrame] reload];
221}
222
223void wxWebKitCtrl::Stop(){
224 if ( !m_webView )
225 return;
226
227 [[m_webView mainFrame] stopLoading];
228}
229
230bool wxWebKitCtrl::CanGetPageSource(){
231 if ( !m_webView )
14f21d6d 232 return false;
2c990ba6
KO
233
234 WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
235 return ( [[dataSource representation] canProvideDocumentSource] );
236}
237
238wxString wxWebKitCtrl::GetPageSource(){
239 if ( !m_webView )
14f21d6d 240 return wxT("");
2c990ba6
KO
241
242 if (CanGetPageSource()){
243 WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
244 return wxStringWithNSString( [[dataSource representation] documentSource] );
245 }
246
247}
248
249void wxWebKitCtrl::SetPageSource(wxString& source, const wxString& baseUrl){
250 if ( !m_webView )
251 return;
252
253 if (CanGetPageSource()){
254 [[m_webView mainFrame] loadHTMLString:(NSString*)wxNSStringWithWxString( source ) baseURL:[NSURL URLWithString:wxNSStringWithWxString( baseUrl )]];
255 }
256
257}
258
005198fa 259void wxWebKitCtrl::OnSize(wxSizeEvent &event){
5b8f917c
KO
260 // This is a nasty hack because WebKit does not seem to recognize a Tabs control as its parent.
261 // Therefore, coordinates must be relative to the left-hand side of the screen, rather than
262 // relative to the Tabs control.
e69d1775
KO
263 wxWindow* parent = GetParent();
264 bool inNotebook = false;
be98622c
KO
265 int x = 0;
266 int y = 18;
e69d1775
KO
267 while(parent != NULL)
268 {
5b8f917c
KO
269 // keep adding the position until we hit the notebook
270 if (!inNotebook){
271 x += parent->GetPosition().x;
272 y += parent->GetPosition().y;
273 }
274
275 if ( parent->GetClassInfo()->GetClassName() == wxT("wxSplitterWindow") ){
276 x += 3;
277 }
278
e69d1775 279 if( parent->IsKindOf( CLASSINFO( wxNotebook ) ) ){
be98622c 280 inNotebook = true;
e69d1775
KO
281 }
282 parent = parent->GetParent();
283 }
284
be98622c
KO
285 if (inNotebook){
286 NSRect bounds = [m_webView frame];
287 bounds.origin.x += x;
288 bounds.origin.y += y;
289 [m_webView setFrame:bounds];
290 }
291
005198fa
KO
292 [m_webView display];
293 event.Skip();
294}
2c990ba6 295
14f21d6d 296void wxWebKitCtrl::MacVisibilityChanged(){
f8405d6e 297 bool isHidden = !IsControlVisible( m_peer->GetControlRef());
14f21d6d
KO
298 [m_webView setHidden:isHidden];
299}
300
2c990ba6
KO
301//------------------------------------------------------------
302// Listener interfaces
303//------------------------------------------------------------
304
305@implementation MyFrameLoadMonitor
306
307- initWithWxWindow: (wxWindow*)inWindow
308{
309 [super init];
3ca7ba74 310 webKitWindow = inWindow; // non retained
2c990ba6
KO
311 return self;
312}
313
314- (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame
315{
316 if (frame == [sender mainFrame]){
317 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
318 wxWebKitStateChangedEvent thisEvent(webKitWindow);
319 thisEvent.SetState(wxWEBKIT_STATE_NEGOTIATING);
320 thisEvent.SetURL( wxStringWithNSString( url ) );
321 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
322 }
323}
324
325- (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame
326{
327 if (frame == [sender mainFrame]){
fba8e95e 328 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
2c990ba6
KO
329 wxWebKitStateChangedEvent thisEvent(webKitWindow);
330 thisEvent.SetState(wxWEBKIT_STATE_TRANSFERRING);
331 thisEvent.SetURL( wxStringWithNSString( url ) );
332 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
333 }
334}
335
336- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
337{
338 if (frame == [sender mainFrame]){
fba8e95e 339 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
2c990ba6
KO
340 wxWebKitStateChangedEvent thisEvent(webKitWindow);
341 thisEvent.SetState(wxWEBKIT_STATE_STOP);
342 thisEvent.SetURL( wxStringWithNSString( url ) );
343 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
344 }
345}
346
347- (void)webView:(WebView *)sender didFailLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
348{
349 if (frame == [sender mainFrame]){
fba8e95e 350 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
2c990ba6
KO
351 wxWebKitStateChangedEvent thisEvent(webKitWindow);
352 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
353 thisEvent.SetURL( wxStringWithNSString( url ) );
354 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
355 }
356}
357
358- (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
359{
360 if (frame == [sender mainFrame]){
361 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
362 wxWebKitStateChangedEvent thisEvent(webKitWindow);
363 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
364 thisEvent.SetURL( wxStringWithNSString( url ) );
365 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
366 }
367}
368
369- (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
370{
371 if (frame == [sender mainFrame]){
372 webKitWindow->SetTitle(wxStringWithNSString( title ));
373 }
374}
3ca7ba74 375@end
5b8f917c 376
948f6c6e 377#endif //wxUSE_WEBKIT