]> git.saurik.com Git - wxWidgets.git/blob - src/html/htmlctrl/webkit/webkit.mm
Making wxUSE_WEBKIT for optionally building wxWebKitCtrl.
[wxWidgets.git] / src / html / htmlctrl / webkit / webkit.mm
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
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "webkit.h"
14 #endif
15
16 #if wxUSE_WEBKIT
17
18 // For compilers that support precompilation, includes "wx.h".
19 #include "wx/wxprec.h"
20
21 #ifndef WX_PRECOMP
22 #include "wx/wx.h"
23 #endif
24
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"
35 #include "wx/notebook.h"
36
37
38 // ----------------------------------------------------------------------------
39 // macros
40 // ----------------------------------------------------------------------------
41
42 #if !USE_SHARED_LIBRARY
43 IMPLEMENT_DYNAMIC_CLASS(wxWebKitCtrl, wxControl)
44 #endif
45
46 BEGIN_EVENT_TABLE(wxWebKitCtrl, wxControl)
47 EVT_SIZE(wxWebKitCtrl::OnSize)
48 END_EVENT_TABLE()
49
50 // ----------------------------------------------------------------------------
51 // wxWebKit Events
52 // ----------------------------------------------------------------------------
53
54 IMPLEMENT_DYNAMIC_CLASS( wxWebKitStateChangedEvent, wxCommandEvent )
55
56 DEFINE_EVENT_TYPE( wxEVT_WEBKIT_STATE_CHANGED )
57
58 wxWebKitStateChangedEvent::wxWebKitStateChangedEvent( wxWindow* win )
59 {
60 SetEventType( wxEVT_WEBKIT_STATE_CHANGED);
61 SetEventObject( win );
62 SetId(win->GetId());
63 }
64
65 //---------------------------------------------------------
66 // helper functions for NSString<->wxString conversion
67 //---------------------------------------------------------
68
69 inline 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
78 inline 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
100 bool wxWebKitCtrl::Create(wxWindow *parent,
101 wxWindowID winID,
102 const wxString& strURL,
103 const wxPoint& pos,
104 const wxSize& size, long style,
105 const wxValidator& validator,
106 const wxString& name)
107 {
108
109 m_currentURL = strURL;
110 //m_pageTitle = _("Untitled Page");
111
112 //still needed for wxCocoa??
113 /*
114 int width, height;
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...
129 #ifdef __WXCOCOA__
130 wxControl::Create(parent, m_windowID, pos, sizeInstance, style , validator , name);
131 SetSize(pos.x, pos.y, sizeInstance.x, sizeInstance.y);
132
133 wxTopLevelWindowCocoa *topWin = wxDynamicCast(this, wxTopLevelWindowCocoa);
134 NSWindow* nsWin = topWin->GetNSWindow();
135 NSRect rect;
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;
148 wxControl::Create(parent, m_windowID, pos, size, style , validator , name);
149 WebInitForCarbon();
150 HIWebViewCreate( (HIViewRef*) &m_macControl );
151
152 m_webView = (WebView*) HIWebViewGetWebView( (HIViewRef) m_macControl );
153 MacPostControlCreate(pos, size);
154 HIViewSetVisible( (HIViewRef) m_macControl, true );
155 [m_webView setHidden:false];
156 #endif
157
158 // Register event listener interfaces
159 MyFrameLoadMonitor* myFrameLoadMonitor = [[MyFrameLoadMonitor alloc] initWithWxWindow: (wxWindow*)this];
160 [m_webView setFrameLoadDelegate:myFrameLoadMonitor];
161
162 LoadURL(m_currentURL);
163 return true;
164 }
165
166 wxWebKitCtrl::~wxWebKitCtrl()
167 {
168
169 }
170
171 // ----------------------------------------------------------------------------
172 // public methods
173 // ----------------------------------------------------------------------------
174
175 void wxWebKitCtrl::LoadURL(const wxString &url)
176 {
177 if( !m_webView )
178 return;
179
180 [[m_webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:wxNSStringWithWxString(url)]]];
181
182 m_currentURL = url;
183 }
184
185 bool wxWebKitCtrl::CanGoBack(){
186 if ( !m_webView )
187 return false;
188
189 return [m_webView canGoBack];
190 }
191
192 bool wxWebKitCtrl::CanGoForward(){
193 if ( !m_webView )
194 return false;
195
196 return [m_webView canGoForward];
197 }
198
199 bool wxWebKitCtrl::GoBack(){
200 if ( !m_webView )
201 return false;
202
203 [m_webView goBack];
204 return true;
205 }
206
207 bool wxWebKitCtrl::GoForward(){
208 if ( !m_webView )
209 return false;
210
211 [m_webView goForward];
212 return true;
213 }
214
215 void wxWebKitCtrl::Reload(){
216 if ( !m_webView )
217 return;
218
219 [[m_webView mainFrame] reload];
220 }
221
222 void wxWebKitCtrl::Stop(){
223 if ( !m_webView )
224 return;
225
226 [[m_webView mainFrame] stopLoading];
227 }
228
229 bool wxWebKitCtrl::CanGetPageSource(){
230 if ( !m_webView )
231 return false;
232
233 WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
234 return ( [[dataSource representation] canProvideDocumentSource] );
235 }
236
237 wxString wxWebKitCtrl::GetPageSource(){
238 if ( !m_webView )
239 return wxT("");
240
241 if (CanGetPageSource()){
242 WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
243 return wxStringWithNSString( [[dataSource representation] documentSource] );
244 }
245
246 }
247
248 void wxWebKitCtrl::SetPageSource(wxString& source, const wxString& baseUrl){
249 if ( !m_webView )
250 return;
251
252 if (CanGetPageSource()){
253 [[m_webView mainFrame] loadHTMLString:(NSString*)wxNSStringWithWxString( source ) baseURL:[NSURL URLWithString:wxNSStringWithWxString( baseUrl )]];
254 }
255
256 }
257
258 void wxWebKitCtrl::OnSize(wxSizeEvent &event){
259 // This is a nasty hack because WebKit does not seem to recognize a Tabs control as its parent.
260 // Therefore, coordinates must be relative to the left-hand side of the screen, rather than
261 // relative to the Tabs control.
262 wxWindow* parent = GetParent();
263 bool inNotebook = false;
264 int x = 0;
265 int y = 18;
266 while(parent != NULL)
267 {
268 // keep adding the position until we hit the notebook
269 if (!inNotebook){
270 x += parent->GetPosition().x;
271 y += parent->GetPosition().y;
272 }
273
274 if ( parent->GetClassInfo()->GetClassName() == wxT("wxSplitterWindow") ){
275 x += 3;
276 }
277
278 if( parent->IsKindOf( CLASSINFO( wxNotebook ) ) ){
279 inNotebook = true;
280 }
281 parent = parent->GetParent();
282 }
283
284 if (inNotebook){
285 NSRect bounds = [m_webView frame];
286 bounds.origin.x += x;
287 bounds.origin.y += y;
288 [m_webView setFrame:bounds];
289 }
290
291 [m_webView display];
292 event.Skip();
293 }
294
295 void wxWebKitCtrl::MacVisibilityChanged(){
296 bool isHidden = !IsControlVisible( (HIViewRef)m_macControl);
297 [m_webView setHidden:isHidden];
298 }
299
300 //------------------------------------------------------------
301 // Listener interfaces
302 //------------------------------------------------------------
303
304 @implementation MyFrameLoadMonitor
305
306 - initWithWxWindow: (wxWindow*)inWindow
307 {
308 [super init];
309 webKitWindow = inWindow; // non retained
310 return self;
311 }
312
313 - (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame
314 {
315 if (frame == [sender mainFrame]){
316 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
317 wxWebKitStateChangedEvent thisEvent(webKitWindow);
318 thisEvent.SetState(wxWEBKIT_STATE_NEGOTIATING);
319 thisEvent.SetURL( wxStringWithNSString( url ) );
320 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
321 }
322 }
323
324 - (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame
325 {
326 if (frame == [sender mainFrame]){
327 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
328 wxWebKitStateChangedEvent thisEvent(webKitWindow);
329 thisEvent.SetState(wxWEBKIT_STATE_TRANSFERRING);
330 thisEvent.SetURL( wxStringWithNSString( url ) );
331 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
332 }
333 }
334
335 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
336 {
337 if (frame == [sender mainFrame]){
338 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
339 wxWebKitStateChangedEvent thisEvent(webKitWindow);
340 thisEvent.SetState(wxWEBKIT_STATE_STOP);
341 thisEvent.SetURL( wxStringWithNSString( url ) );
342 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
343 }
344 }
345
346 - (void)webView:(WebView *)sender didFailLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
347 {
348 if (frame == [sender mainFrame]){
349 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
350 wxWebKitStateChangedEvent thisEvent(webKitWindow);
351 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
352 thisEvent.SetURL( wxStringWithNSString( url ) );
353 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
354 }
355 }
356
357 - (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
358 {
359 if (frame == [sender mainFrame]){
360 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
361 wxWebKitStateChangedEvent thisEvent(webKitWindow);
362 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
363 thisEvent.SetURL( wxStringWithNSString( url ) );
364 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
365 }
366 }
367
368 - (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
369 {
370 if (frame == [sender mainFrame]){
371 webKitWindow->SetTitle(wxStringWithNSString( title ));
372 }
373 }
374 @end
375
376 #endif //wxUSE_WEBKIT