]> git.saurik.com Git - wxWidgets.git/blame - src/html/htmlctrl/webkit/webkit.mm
update from Janos
[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");
9548f380 111
2c990ba6
KO
112 //still needed for wxCocoa??
113/*
114 int width, height;
3ca7ba74 115 wxSize sizeInstance;
9548f380 116 if (size.x == wxDefaultCoord || size.y == wxDefaultCoord)
3ca7ba74
RD
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 }
9548f380 127*/
3ca7ba74 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);
9548f380 132
2c990ba6
KO
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;
b4fd164d 148 wxControl::Create(parent, m_windowID, pos, size, style , validator , name);
637013eb 149 m_peer = new wxMacControl(this);
2c990ba6 150 WebInitForCarbon();
f8405d6e 151 HIWebViewCreate( m_peer->GetControlRefAddr() );
9548f380 152
f8405d6e 153 m_webView = (WebView*) HIWebViewGetWebView( m_peer->GetControlRef() );
14f21d6d 154 MacPostControlCreate(pos, size);
9548f380 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];
9548f380 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;
9548f380 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;
9548f380 189
2c990ba6
KO
190 return [m_webView canGoBack];
191}
192
193bool wxWebKitCtrl::CanGoForward(){
194 if ( !m_webView )
195 return false;
9548f380 196
2c990ba6
KO
197 return [m_webView canGoForward];
198}
199
200bool wxWebKitCtrl::GoBack(){
201 if ( !m_webView )
202 return false;
9548f380 203
8f3b30d5
KO
204 bool result = [(WebView*)m_webView goBack];
205 return result;
2c990ba6
KO
206}
207
9548f380 208bool wxWebKitCtrl::GoForward(){
2c990ba6
KO
209 if ( !m_webView )
210 return false;
9548f380 211
8f3b30d5
KO
212 bool result = [(WebView*)m_webView goForward];
213 return result;
2c990ba6
KO
214}
215
9548f380 216void wxWebKitCtrl::Reload(){
2c990ba6
KO
217 if ( !m_webView )
218 return;
9548f380 219
2c990ba6
KO
220 [[m_webView mainFrame] reload];
221}
222
223void wxWebKitCtrl::Stop(){
224 if ( !m_webView )
225 return;
9548f380 226
2c990ba6
KO
227 [[m_webView mainFrame] stopLoading];
228}
229
230bool wxWebKitCtrl::CanGetPageSource(){
231 if ( !m_webView )
14f21d6d 232 return false;
9548f380 233
2c990ba6
KO
234 WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
235 return ( [[dataSource representation] canProvideDocumentSource] );
236}
237
238wxString wxWebKitCtrl::GetPageSource(){
9548f380 239
2c990ba6
KO
240 if (CanGetPageSource()){
241 WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
242 return wxStringWithNSString( [[dataSource representation] documentSource] );
243 }
9548f380
WS
244
245 return wxEmptyString;
2c990ba6
KO
246}
247
248void wxWebKitCtrl::SetPageSource(wxString& source, const wxString& baseUrl){
249 if ( !m_webView )
250 return;
9548f380 251
2c990ba6
KO
252 if (CanGetPageSource()){
253 [[m_webView mainFrame] loadHTMLString:(NSString*)wxNSStringWithWxString( source ) baseURL:[NSURL URLWithString:wxNSStringWithWxString( baseUrl )]];
254 }
255
256}
257
005198fa 258void wxWebKitCtrl::OnSize(wxSizeEvent &event){
39104bc1
KO
259 // This is a nasty hack because WebKit seems to lose its position when it is embedded
260 // in a control that is not itself the content view for a TLW.
b4fd164d
KO
261 // I put it in OnSize because these calcs are not perfect, and in fact are basically
262 // guesses based on reverse engineering, so it's best to give people the option of
263 // overriding OnSize with their own calcs if need be.
264 // I also left some test debugging print statements as a convenience if a(nother)
265 // problem crops up.
266
267 // Let's hope that Tiger fixes this mess...
268
269 int x, y;
270 x = 0;
271 y = 0;
272
273 bool isParentTopLevel = true;
274
275 wxWindow* parent = GetParent();
276
277 wxWindow* tlw = MacGetTopLevelWindow();
278
279 // This must be the case that Apple tested with, because well, in this one case
280 // we don't need to do anything! It just works. ;)
281 if (parent == tlw){
282 return;
283 }
284
285 while(parent != NULL)
286 {
287 if ( parent->GetClassInfo()->GetClassName() == wxT("wxSplitterWindow") ){
288 //do nothing in this case
289 }
290 else{
291 if (!parent->IsTopLevel()) {
292 //printf("Parent: %s\n", parent->GetClassInfo()->GetClassName());
293 int plusx = 0;
294 plusx = parent->GetClientAreaOrigin().x + parent->GetPosition().x;
295 if (plusx > 0){
296 x += plusx;
297 //printf("Parent: %s Added x: %d\n", parent->GetClassInfo()->GetClassName(), parent->GetClientAreaOrigin().x + parent->GetPosition().x);
298 }
299
300 int plusy = 0;
301 plusy = parent->GetClientAreaOrigin().y + parent->GetPosition().y;
302 if (plusy > 0){
303 y += plusy;
304 //printf("Parent: %s Added y: %d\n", parent->GetClassInfo()->GetClassName(), parent->GetClientAreaOrigin().y + parent->GetPosition().y);
305 }
306 else{
307 //printf("Parent: %s Origin: %d Position:%d\n", parent->GetClassInfo()->GetClassName(), parent->GetClientAreaOrigin().y, parent->GetPosition().y);
308 }
309
310 }
311 else{
312 //
313 x += parent->GetClientAreaOrigin().x;
314 // calculate the title bar height (26 pixels) into the top offset.
315 // This becomes important later when we must flip the y coordinate
316 // to convert to Cocoa's coordinate system.
317 y += parent->GetClientAreaOrigin().y += 26;
318 //printf("x: %d, y:%d\n", x, y);
319 }
320 //we still need to add the y, because we have to convert/flip coordinates for Cocoa
321
322 if ( parent->IsKindOf( CLASSINFO( wxNotebook ) ) ){
323 //Not sure why calcs are off in this one scenario...
324 x -= 3;
325 //printf("x: %d, y:%d\n", x, y);
326 }
327
328 if (parent->IsKindOf( CLASSINFO( wxPanel ) ) ){
329 // Another strange case. Adding a wxPanel to the parent heirarchy
330 // causes wxWebKitCtrl's Cocoa y origin to be 4 pixels off
331 // for some reason, even if the panel has a position and origin of 0.
332 // This corrects that. Man, I wish I could debug Carbon/HIWebView!! ;)
333 y -= 4;
334 }
335 }
336
337 parent = parent->GetParent();
338 }
339
340 // Tried using MacWindowToRootWindow both for wxWebKitCtrl and its parent,
341 // but coordinates were off by a significant amount.
342 // Am leaving the code here if anyone wants to play with it.
343
344 //int x2, y2 = 0;
345 //if (GetParent())
346 // GetParent()->MacWindowToRootWindow(&x2, &y2);
347 //printf("x = %d, y = %d\n", x, y);
348 //printf("x2 = %d, y2 = %d\n", x2, y2);
349 //x = x2;
350 //y = y2;
351
352 if (tlw){
353 //flip the y coordinate to convert to Cocoa coordinates
354 //printf("tlw y: %d, y: %d\n", tlw->GetSize().y, (GetSize().y + y));
355 y = tlw->GetSize().y - ((GetSize().y) + y);
356 }
357
358 //printf("Added to bounds x=%d, y=%d\n", x, y);
359 NSRect bounds = [m_webView frame];
360 bounds.origin.x = x;
361 bounds.origin.y = y;
362 [m_webView setFrame:bounds];
9548f380 363
39104bc1 364 //printf("Carbon position x=%d, y=%d\n", GetPosition().x, GetPosition().y);
fe3fc02e
KO
365 if (IsShown())
366 [m_webView display];
005198fa
KO
367 event.Skip();
368}
2c990ba6 369
14f21d6d 370void wxWebKitCtrl::MacVisibilityChanged(){
f8405d6e 371 bool isHidden = !IsControlVisible( m_peer->GetControlRef());
fe3fc02e
KO
372 if (!isHidden)
373 [m_webView display];
9548f380 374
14f21d6d
KO
375 [m_webView setHidden:isHidden];
376}
377
2c990ba6
KO
378//------------------------------------------------------------
379// Listener interfaces
380//------------------------------------------------------------
381
382@implementation MyFrameLoadMonitor
383
384- initWithWxWindow: (wxWindow*)inWindow
385{
386 [super init];
3ca7ba74 387 webKitWindow = inWindow; // non retained
2c990ba6
KO
388 return self;
389}
390
391- (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame
392{
393 if (frame == [sender mainFrame]){
394 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
395 wxWebKitStateChangedEvent thisEvent(webKitWindow);
396 thisEvent.SetState(wxWEBKIT_STATE_NEGOTIATING);
397 thisEvent.SetURL( wxStringWithNSString( url ) );
398 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
399 }
400}
401
402- (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame
403{
404 if (frame == [sender mainFrame]){
fba8e95e 405 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
2c990ba6
KO
406 wxWebKitStateChangedEvent thisEvent(webKitWindow);
407 thisEvent.SetState(wxWEBKIT_STATE_TRANSFERRING);
408 thisEvent.SetURL( wxStringWithNSString( url ) );
409 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
410 }
411}
412
413- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
414{
415 if (frame == [sender mainFrame]){
fba8e95e 416 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
2c990ba6
KO
417 wxWebKitStateChangedEvent thisEvent(webKitWindow);
418 thisEvent.SetState(wxWEBKIT_STATE_STOP);
419 thisEvent.SetURL( wxStringWithNSString( url ) );
420 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
421 }
422}
423
424- (void)webView:(WebView *)sender didFailLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
425{
426 if (frame == [sender mainFrame]){
fba8e95e 427 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
2c990ba6
KO
428 wxWebKitStateChangedEvent thisEvent(webKitWindow);
429 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
430 thisEvent.SetURL( wxStringWithNSString( url ) );
431 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
432 }
433}
434
435- (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
436{
437 if (frame == [sender mainFrame]){
438 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
439 wxWebKitStateChangedEvent thisEvent(webKitWindow);
440 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
441 thisEvent.SetURL( wxStringWithNSString( url ) );
442 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
443 }
444}
445
446- (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
447{
448 if (frame == [sender mainFrame]){
449 webKitWindow->SetTitle(wxStringWithNSString( title ));
450 }
451}
3ca7ba74 452@end
5b8f917c 453
948f6c6e 454#endif //wxUSE_WEBKIT