]> git.saurik.com Git - wxWidgets.git/blame - src/html/htmlctrl/webkit/webkit.mm
replace use of deprecated GtkPixmap, gtk_container_border_width
[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
2c990ba6
KO
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
216e968a 14#include "wx/splitter.h"
2c990ba6 15
2c990ba6
KO
16#ifndef WX_PRECOMP
17 #include "wx/wx.h"
18#endif
19
948f6c6e
JS
20#if wxUSE_WEBKIT
21
2c990ba6
KO
22#ifdef __WXCOCOA__
23#include "wx/cocoa/autorelease.h"
24#else
25#include "wx/mac/uma.h"
26#include <Carbon/Carbon.h>
b384c0fb 27#include <WebKit/WebKit.h>
2c990ba6
KO
28#include <WebKit/HIWebView.h>
29#include <WebKit/CarbonUtils.h>
30#endif
31
32#include "wx/html/webkit.h"
14f21d6d 33#include "wx/notebook.h"
2c990ba6
KO
34
35
36// ----------------------------------------------------------------------------
37// macros
38// ----------------------------------------------------------------------------
39
2c990ba6 40IMPLEMENT_DYNAMIC_CLASS(wxWebKitCtrl, wxControl)
2c990ba6
KO
41
42BEGIN_EVENT_TABLE(wxWebKitCtrl, wxControl)
005198fa 43 EVT_SIZE(wxWebKitCtrl::OnSize)
2c990ba6
KO
44END_EVENT_TABLE()
45
46// ----------------------------------------------------------------------------
47// wxWebKit Events
48// ----------------------------------------------------------------------------
49
50IMPLEMENT_DYNAMIC_CLASS( wxWebKitStateChangedEvent, wxCommandEvent )
51
52DEFINE_EVENT_TYPE( wxEVT_WEBKIT_STATE_CHANGED )
53
54wxWebKitStateChangedEvent::wxWebKitStateChangedEvent( wxWindow* win )
55{
56 SetEventType( wxEVT_WEBKIT_STATE_CHANGED);
57 SetEventObject( win );
3ca7ba74 58 SetId(win->GetId());
2c990ba6
KO
59}
60
61//---------------------------------------------------------
62// helper functions for NSString<->wxString conversion
63//---------------------------------------------------------
64
65inline wxString wxStringWithNSString(NSString *nsstring)
66{
67#if wxUSE_UNICODE
68 return wxString([nsstring UTF8String], wxConvUTF8);
69#else
70 return wxString([nsstring lossyCString]);
71#endif // wxUSE_UNICODE
72}
73
74inline NSString* wxNSStringWithWxString(const wxString &wxstring)
75{
76#if wxUSE_UNICODE
77 return [NSString stringWithUTF8String: wxstring.mb_str(wxConvUTF8)];
78#else
79 return [NSString stringWithCString: wxstring.c_str() length:wxstring.Len()];
80#endif // wxUSE_UNICODE
81}
82
83@interface MyFrameLoadMonitor : NSObject
84{
85 wxWindow* webKitWindow;
86}
87
88- initWithWxWindow: (wxWindow*)inWindow;
89
90@end
91
92// ----------------------------------------------------------------------------
93// creation/destruction
94// ----------------------------------------------------------------------------
95
96bool wxWebKitCtrl::Create(wxWindow *parent,
3ca7ba74 97 wxWindowID winID,
2c990ba6
KO
98 const wxString& strURL,
99 const wxPoint& pos,
3ca7ba74
RD
100 const wxSize& size, long style,
101 const wxValidator& validator,
102 const wxString& name)
2c990ba6
KO
103{
104
105 m_currentURL = strURL;
14f21d6d 106 //m_pageTitle = _("Untitled Page");
9548f380 107
2c990ba6
KO
108 //still needed for wxCocoa??
109/*
110 int width, height;
3ca7ba74 111 wxSize sizeInstance;
9548f380 112 if (size.x == wxDefaultCoord || size.y == wxDefaultCoord)
3ca7ba74
RD
113 {
114 m_parent->GetClientSize(&width, &height);
115 sizeInstance.x = width;
116 sizeInstance.y = height;
117 }
118 else
119 {
120 sizeInstance.x = size.x;
121 sizeInstance.y = size.y;
122 }
9548f380 123*/
3ca7ba74 124 // now create and attach WebKit view...
2c990ba6
KO
125#ifdef __WXCOCOA__
126 wxControl::Create(parent, m_windowID, pos, sizeInstance, style , validator , name);
3ca7ba74 127 SetSize(pos.x, pos.y, sizeInstance.x, sizeInstance.y);
9548f380 128
2c990ba6
KO
129 wxTopLevelWindowCocoa *topWin = wxDynamicCast(this, wxTopLevelWindowCocoa);
130 NSWindow* nsWin = topWin->GetNSWindow();
3ca7ba74 131 NSRect rect;
2c990ba6
KO
132 rect.origin.x = pos.x;
133 rect.origin.y = pos.y;
134 rect.size.width = sizeInstance.x;
135 rect.size.height = sizeInstance.y;
136 m_webView = (WebView*)[[WebView alloc] initWithFrame:rect frameName:@"webkitFrame" groupName:@"webkitGroup"];
137 SetNSView(m_webView);
138 [m_cocoaNSView release];
139
140 if(m_parent) m_parent->CocoaAddChild(this);
141 SetInitialFrameRect(pos,sizeInstance);
142#else
143 m_macIsUserPane = false;
daef4689 144 wxControl::Create(parent, winID, pos, size, style , validator , name);
637013eb 145 m_peer = new wxMacControl(this);
2c990ba6 146 WebInitForCarbon();
f8405d6e 147 HIWebViewCreate( m_peer->GetControlRefAddr() );
9548f380 148
f8405d6e 149 m_webView = (WebView*) HIWebViewGetWebView( m_peer->GetControlRef() );
14f21d6d 150 MacPostControlCreate(pos, size);
9548f380 151 HIViewSetVisible( m_peer->GetControlRef(), true );
5b8f917c 152 [m_webView setHidden:false];
b384c0fb 153#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
daef4689 154 if ( UMAGetSystemVersion() >= 0x1030 )
b384c0fb
SC
155 HIViewChangeFeatures( m_peer->GetControlRef() , kHIViewIsOpaque , 0 ) ;
156#endif
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
2d837688 252 [[m_webView mainFrame] loadHTMLString:(NSString*)wxNSStringWithWxString( source ) baseURL:[NSURL URLWithString:wxNSStringWithWxString( baseUrl )]];
2c990ba6
KO
253}
254
005198fa 255void wxWebKitCtrl::OnSize(wxSizeEvent &event){
39104bc1
KO
256 // This is a nasty hack because WebKit seems to lose its position when it is embedded
257 // in a control that is not itself the content view for a TLW.
daef4689
VZ
258 // I put it in OnSize because these calcs are not perfect, and in fact are basically
259 // guesses based on reverse engineering, so it's best to give people the option of
260 // overriding OnSize with their own calcs if need be.
261 // I also left some test debugging print statements as a convenience if a(nother)
262 // problem crops up.
263
264 // Let's hope that Tiger fixes this mess...
265
266 int x, y;
267 x = 0;
268 y = 0;
269
270 wxWindow* parent = GetParent();
271
272 wxWindow* tlw = MacGetTopLevelWindow();
273
274 // This must be the case that Apple tested with, because well, in this one case
275 // we don't need to do anything! It just works. ;)
276 if (parent == tlw){
277 return;
278 }
279
280 while(parent != NULL)
281 {
282 if ( parent->IsKindOf( CLASSINFO( wxSplitterWindow ) ) && GetParent()->IsKindOf( CLASSINFO( wxSplitterWindow ) ) ){
283 // When parent is not a wxSplitterWindow, we can rely on it's GetPosition() to give us the correct
284 // coordinates, but when the parent is a wxSplitterWindow, we need to manually calculate
285 // the sash position of it and any parent wxSplitterWindows into the webkit's position.
286 wxSplitterWindow* splitter;
287 splitter = dynamic_cast<wxSplitterWindow*>(parent);
288 if (splitter->GetSplitMode() == wxSPLIT_HORIZONTAL){
289 if (splitter->GetPosition().y > 0)
290 y += splitter->GetPosition().y;
291
292 if (splitter->GetSashSize() > 0)
293 y += splitter->GetSashSize();
294
295 if (splitter->GetSashPosition() > 0)
296 y += splitter->GetSashPosition();
297 }
298 else{
299 if (splitter->GetPosition().x > 0)
300 x += splitter->GetPosition().x;
301
302 if (splitter->GetSashSize() > 0)
303 x += splitter->GetSashSize();
304
305 if (splitter->GetSashPosition() > 0)
306 x += splitter->GetSashPosition();
307 }
308 }
309 else{
310 if (!parent->IsTopLevel()) {
311 //printf("Parent: %s\n", parent->GetClassInfo()->GetClassName());
312 int plusx = 0;
313 plusx = parent->GetClientAreaOrigin().x + parent->GetPosition().x;
314 if (plusx > 0){
315 x += plusx;
316 //printf("Parent: %s Added x: %d\n", parent->GetClassInfo()->GetClassName(), parent->GetClientAreaOrigin().x + parent->GetPosition().x);
317 }
318
319 int plusy = 0;
320 plusy = parent->GetClientAreaOrigin().y + parent->GetPosition().y;
321 if (plusy > 0){
322 y += plusy;
323 //printf("Parent: %s Added y: %d\n", parent->GetClassInfo()->GetClassName(), parent->GetClientAreaOrigin().y + parent->GetPosition().y);
324 }
325 else{
326 //printf("Parent: %s Origin: %d Position:%d\n", parent->GetClassInfo()->GetClassName(), parent->GetClientAreaOrigin().y, parent->GetPosition().y);
327 }
328
329 }
330 else{
331 //
332 x += parent->GetClientAreaOrigin().x;
333 // calculate the title bar height (26 pixels) into the top offset.
334 // This becomes important later when we must flip the y coordinate
335 // to convert to Cocoa's coordinate system.
336 y += parent->GetClientAreaOrigin().y += 26;
337 //printf("x: %d, y:%d\n", x, y);
338 }
339 //we still need to add the y, because we have to convert/flip coordinates for Cocoa
340
341 if ( parent->IsKindOf( CLASSINFO( wxNotebook ) ) ){
342 //Not sure why calcs are off in this one scenario...
343 y -= 4;
344 //printf("x: %d, y:%d\n", x, y);
345 }
346
347 if (parent->IsKindOf( CLASSINFO( wxPanel ) ) ){
348 // Another strange case. Adding a wxPanel to the parent heirarchy
349 // causes wxWebKitCtrl's Cocoa y origin to be 4 pixels off
350 // for some reason, even if the panel has a position and origin of 0.
351 // This corrects that. Man, I wish I could debug Carbon/HIWebView!! ;)
352 y -= 4;
353 }
354 }
355
356 parent = parent->GetParent();
357 }
358
359 // Tried using MacWindowToRootWindow both for wxWebKitCtrl and its parent,
360 // but coordinates were off by a significant amount.
361 // Am leaving the code here if anyone wants to play with it.
362
363 //int x2, y2 = 0;
364 //if (GetParent())
365 // GetParent()->MacWindowToRootWindow(&x2, &y2);
366 //printf("x = %d, y = %d\n", x, y);
367 //printf("x2 = %d, y2 = %d\n", x2, y2);
368 //x = x2;
369 //y = y2;
370
371 if (tlw){
372 //flip the y coordinate to convert to Cocoa coordinates
373 //printf("tlw y: %d, y: %d\n", tlw->GetSize().y, (GetSize().y + y));
374 y = tlw->GetSize().y - ((GetSize().y) + y);
375 }
376
377 //printf("Added to bounds x=%d, y=%d\n", x, y);
378 NSRect bounds = [m_webView frame];
379 bounds.origin.x = x;
380 bounds.origin.y = y;
381 [m_webView setFrame:bounds];
9548f380 382
39104bc1 383 //printf("Carbon position x=%d, y=%d\n", GetPosition().x, GetPosition().y);
fe3fc02e 384 if (IsShown())
14753ffe 385 [(WebView*)m_webView display];
005198fa
KO
386 event.Skip();
387}
2c990ba6 388
14f21d6d 389void wxWebKitCtrl::MacVisibilityChanged(){
f8405d6e 390 bool isHidden = !IsControlVisible( m_peer->GetControlRef());
fe3fc02e 391 if (!isHidden)
14753ffe 392 [(WebView*)m_webView display];
9548f380 393
14f21d6d
KO
394 [m_webView setHidden:isHidden];
395}
396
2c990ba6
KO
397//------------------------------------------------------------
398// Listener interfaces
399//------------------------------------------------------------
400
401@implementation MyFrameLoadMonitor
402
403- initWithWxWindow: (wxWindow*)inWindow
404{
405 [super init];
3ca7ba74 406 webKitWindow = inWindow; // non retained
2c990ba6
KO
407 return self;
408}
409
410- (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame
411{
412 if (frame == [sender mainFrame]){
413 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
414 wxWebKitStateChangedEvent thisEvent(webKitWindow);
415 thisEvent.SetState(wxWEBKIT_STATE_NEGOTIATING);
416 thisEvent.SetURL( wxStringWithNSString( url ) );
417 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
418 }
419}
420
421- (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame
422{
423 if (frame == [sender mainFrame]){
fba8e95e 424 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
2c990ba6
KO
425 wxWebKitStateChangedEvent thisEvent(webKitWindow);
426 thisEvent.SetState(wxWEBKIT_STATE_TRANSFERRING);
427 thisEvent.SetURL( wxStringWithNSString( url ) );
428 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
429 }
430}
431
432- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
433{
434 if (frame == [sender mainFrame]){
fba8e95e 435 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
2c990ba6
KO
436 wxWebKitStateChangedEvent thisEvent(webKitWindow);
437 thisEvent.SetState(wxWEBKIT_STATE_STOP);
438 thisEvent.SetURL( wxStringWithNSString( url ) );
439 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
440 }
441}
442
443- (void)webView:(WebView *)sender didFailLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
444{
445 if (frame == [sender mainFrame]){
fba8e95e 446 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
2c990ba6
KO
447 wxWebKitStateChangedEvent thisEvent(webKitWindow);
448 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
449 thisEvent.SetURL( wxStringWithNSString( url ) );
450 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
451 }
452}
453
454- (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
455{
456 if (frame == [sender mainFrame]){
457 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
458 wxWebKitStateChangedEvent thisEvent(webKitWindow);
459 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
460 thisEvent.SetURL( wxStringWithNSString( url ) );
461 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
462 }
463}
464
465- (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
466{
467 if (frame == [sender mainFrame]){
44f9d0aa 468 webKitWindow->SetLabel(wxStringWithNSString( title ));
2c990ba6
KO
469 }
470}
3ca7ba74 471@end
5b8f917c 472
948f6c6e 473#endif //wxUSE_WEBKIT