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