]> git.saurik.com Git - wxWidgets.git/blame - src/html/htmlctrl/webkit/webkit.mm
Fix crash in wxDC::GetMultiLineTextExtent() after last commit.
[wxWidgets.git] / src / html / htmlctrl / webkit / webkit.mm
CommitLineData
2c990ba6 1/////////////////////////////////////////////////////////////////////////////
80fdcdb9 2// Name: src/html/htmlctrl/webkit/webkit.mm
2c990ba6
KO
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
14ca15f1
SC
25#include "wx/osx/private.h"
26
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"
2c990ba6 33
448f8f12 34#define DEBUG_WEBKIT_SIZING 0
2c990ba6 35
eebb8902
KO
36extern WXDLLEXPORT_DATA(const char) wxWebKitCtrlNameStr[] = "webkitctrl";
37
2c990ba6
KO
38// ----------------------------------------------------------------------------
39// macros
40// ----------------------------------------------------------------------------
41
2c990ba6 42IMPLEMENT_DYNAMIC_CLASS(wxWebKitCtrl, wxControl)
2c990ba6
KO
43
44BEGIN_EVENT_TABLE(wxWebKitCtrl, wxControl)
14ca15f1 45#if defined(__WXMAC__) && wxOSX_USE_CARBON
005198fa 46 EVT_SIZE(wxWebKitCtrl::OnSize)
14ca15f1 47#endif
2c990ba6
KO
48END_EVENT_TABLE()
49
14ca15f1
SC
50#if defined(__WXOSX__) && wxOSX_USE_CARBON
51
448f8f12
KO
52// ----------------------------------------------------------------------------
53// Carbon Events handlers
54// ----------------------------------------------------------------------------
55
233f5738 56// prototype for function in src/osx/carbon/nonownedwnd.cpp
448f8f12
KO
57void SetupMouseEvent( wxMouseEvent &wxevent , wxMacCarbonEvent &cEvent );
58
59static const EventTypeSpec eventList[] =
60{
61 //{ kEventClassControl, kEventControlTrack } ,
62 { kEventClassMouse, kEventMouseUp },
63 { kEventClassMouse, kEventMouseDown },
64 { kEventClassMouse, kEventMouseMoved },
65 { kEventClassMouse, kEventMouseDragged },
8a088306 66
c70aaa09
KO
67 { kEventClassKeyboard, kEventRawKeyDown } ,
68 { kEventClassKeyboard, kEventRawKeyRepeat } ,
69 { kEventClassKeyboard, kEventRawKeyUp } ,
70 { kEventClassKeyboard, kEventRawKeyModifiersChanged } ,
8a088306 71
c70aaa09
KO
72 { kEventClassTextInput, kEventTextInputUnicodeForKeyEvent } ,
73 { kEventClassTextInput, kEventTextInputUpdateActiveInputArea } ,
8a088306 74
448f8f12
KO
75#if DEBUG_WEBKIT_SIZING == 1
76 { kEventClassControl, kEventControlBoundsChanged } ,
77#endif
78};
79
c70aaa09
KO
80// mix this in from window.cpp
81pascal OSStatus wxMacUnicodeTextEventHandler( EventHandlerCallRef handler , EventRef event , void *data ) ;
82
83// NOTE: This is mostly taken from KeyboardEventHandler in toplevel.cpp, but
84// that expects the data pointer is a top-level window, so I needed to change
85// that in this case. However, once 2.8 is out, we should factor out the common logic
86// among the two functions and merge them.
87static pascal OSStatus wxWebKitKeyEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
88{
89 OSStatus result = eventNotHandledErr ;
90 wxMacCarbonEvent cEvent( event ) ;
91
8a088306 92 wxWebKitCtrl* thisWindow = (wxWebKitCtrl*) data ;
c70aaa09
KO
93 wxWindow* focus = thisWindow ;
94
95 unsigned char charCode ;
96 wxChar uniChar[2] ;
97 uniChar[0] = 0;
98 uniChar[1] = 0;
99
100 UInt32 keyCode ;
101 UInt32 modifiers ;
c70aaa09
KO
102 UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ;
103
104#if wxUSE_UNICODE
105 ByteCount dataSize = 0 ;
106 if ( GetEventParameter( event, kEventParamKeyUnicodes, typeUnicodeText, NULL, 0 , &dataSize, NULL ) == noErr )
107 {
108 UniChar buf[2] ;
109 int numChars = dataSize / sizeof( UniChar) + 1;
110
111 UniChar* charBuf = buf ;
112
113 if ( numChars * 2 > 4 )
114 charBuf = new UniChar[ numChars ] ;
115 GetEventParameter( event, kEventParamKeyUnicodes, typeUnicodeText, NULL, dataSize , NULL , charBuf ) ;
116 charBuf[ numChars - 1 ] = 0;
117
118#if SIZEOF_WCHAR_T == 2
119 uniChar = charBuf[0] ;
120#else
121 wxMBConvUTF16 converter ;
122 converter.MB2WC( uniChar , (const char*)charBuf , 2 ) ;
123#endif
124
125 if ( numChars * 2 > 4 )
126 delete[] charBuf ;
127 }
128#endif
129
130 GetEventParameter( event, kEventParamKeyMacCharCodes, typeChar, NULL, sizeof(char), NULL, &charCode );
131 GetEventParameter( event, kEventParamKeyCode, typeUInt32, NULL, sizeof(UInt32), NULL, &keyCode );
132 GetEventParameter( event, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(UInt32), NULL, &modifiers );
c70aaa09
KO
133
134 UInt32 message = (keyCode << 8) + charCode;
135 switch ( GetEventKind( event ) )
136 {
137 case kEventRawKeyRepeat :
138 case kEventRawKeyDown :
139 {
140 WXEVENTREF formerEvent = wxTheApp->MacGetCurrentEvent() ;
141 WXEVENTHANDLERCALLREF formerHandler = wxTheApp->MacGetCurrentEventHandlerCallRef() ;
142 wxTheApp->MacSetCurrentEvent( event , handler ) ;
143 if ( /* focus && */ wxTheApp->MacSendKeyDownEvent(
04dc0a11 144 focus , message , modifiers , when , uniChar[0] ) )
c70aaa09
KO
145 {
146 result = noErr ;
147 }
148 wxTheApp->MacSetCurrentEvent( formerEvent , formerHandler ) ;
149 }
150 break ;
151
152 case kEventRawKeyUp :
153 if ( /* focus && */ wxTheApp->MacSendKeyUpEvent(
04dc0a11 154 focus , message , modifiers , when , uniChar[0] ) )
c70aaa09
KO
155 {
156 result = noErr ;
157 }
158 break ;
159
160 case kEventRawKeyModifiersChanged :
161 {
162 wxKeyEvent event(wxEVT_KEY_DOWN);
163
164 event.m_shiftDown = modifiers & shiftKey;
41749c76 165 event.m_rawControlDown = modifiers & controlKey;
c70aaa09 166 event.m_altDown = modifiers & optionKey;
41749c76 167 event.m_controlDown = modifiers & cmdKey;
c70aaa09
KO
168
169#if wxUSE_UNICODE
170 event.m_uniChar = uniChar[0] ;
171#endif
172
173 event.SetTimestamp(when);
174 event.SetEventObject(focus);
175
176 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & controlKey )
177 {
41749c76 178 event.m_keyCode = WXK_RAW_CONTROL ;
c70aaa09
KO
179 event.SetEventType( ( modifiers & controlKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
180 focus->GetEventHandler()->ProcessEvent( event ) ;
181 }
182 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & shiftKey )
183 {
184 event.m_keyCode = WXK_SHIFT ;
185 event.SetEventType( ( modifiers & shiftKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
186 focus->GetEventHandler()->ProcessEvent( event ) ;
187 }
188 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & optionKey )
189 {
190 event.m_keyCode = WXK_ALT ;
191 event.SetEventType( ( modifiers & optionKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
192 focus->GetEventHandler()->ProcessEvent( event ) ;
193 }
194 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & cmdKey )
195 {
41749c76 196 event.m_keyCode = WXK_CONTROL ;
c70aaa09
KO
197 event.SetEventType( ( modifiers & cmdKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
198 focus->GetEventHandler()->ProcessEvent( event ) ;
199 }
200
201 wxApp::s_lastModifiers = modifiers ;
202 }
203 break ;
204
205 default:
206 break;
207 }
208
209 return result ;
210}
211
448f8f12
KO
212static pascal OSStatus wxWebKitCtrlEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
213{
214 OSStatus result = eventNotHandledErr ;
215
216 wxMacCarbonEvent cEvent( event ) ;
217
218 ControlRef controlRef ;
219 wxWebKitCtrl* thisWindow = (wxWebKitCtrl*) data ;
d0d7f207 220 wxNonOwnedWindow* tlw = NULL;
448f8f12
KO
221 if (thisWindow)
222 tlw = thisWindow->MacGetTopLevelWindow();
223
224 cEvent.GetParameter( kEventParamDirectObject , &controlRef ) ;
8a088306 225
448f8f12 226 wxWindow* currentMouseWindow = thisWindow ;
8a088306 227
448f8f12
KO
228 if ( wxApp::s_captureWindow )
229 currentMouseWindow = wxApp::s_captureWindow;
230
c70aaa09 231 switch ( GetEventClass( event ) )
448f8f12 232 {
c70aaa09
KO
233 case kEventClassKeyboard:
234 {
235 result = wxWebKitKeyEventHandler(handler, event, data);
236 break;
237 }
8a088306 238
c70aaa09
KO
239 case kEventClassTextInput:
240 {
241 result = wxMacUnicodeTextEventHandler(handler, event, data);
242 break;
243 }
8a088306 244
c70aaa09
KO
245 case kEventClassMouse:
246 {
247 switch ( GetEventKind( event ) )
448f8f12 248 {
c70aaa09
KO
249 case kEventMouseDragged :
250 case kEventMouseMoved :
251 case kEventMouseDown :
252 case kEventMouseUp :
448f8f12 253 {
c70aaa09
KO
254 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
255 SetupMouseEvent( wxevent , cEvent ) ;
8a088306 256
c70aaa09
KO
257 currentMouseWindow->ScreenToClient( &wxevent.m_x , &wxevent.m_y ) ;
258 wxevent.SetEventObject( currentMouseWindow ) ;
259 wxevent.SetId( currentMouseWindow->GetId() ) ;
8a088306 260
c70aaa09
KO
261 if ( currentMouseWindow->GetEventHandler()->ProcessEvent(wxevent) )
262 {
263 result = noErr;
264 }
265
266 break; // this should enable WebKit to fire mouse dragged and mouse up events...
267 }
268 default :
269 break ;
448f8f12 270 }
c70aaa09
KO
271 }
272 default:
273 break;
448f8f12
KO
274 }
275
276 result = CallNextEventHandler(handler, event);
277 return result ;
278}
279
280DEFINE_ONE_SHOT_HANDLER_GETTER( wxWebKitCtrlEventHandler )
281
14ca15f1 282#endif
448f8f12 283
2c990ba6
KO
284// ----------------------------------------------------------------------------
285// wxWebKit Events
286// ----------------------------------------------------------------------------
287
288IMPLEMENT_DYNAMIC_CLASS( wxWebKitStateChangedEvent, wxCommandEvent )
289
9b11752c 290wxDEFINE_EVENT( wxEVT_WEBKIT_STATE_CHANGED, wxWebKitStateChangedEvent );
2c990ba6
KO
291
292wxWebKitStateChangedEvent::wxWebKitStateChangedEvent( wxWindow* win )
293{
294 SetEventType( wxEVT_WEBKIT_STATE_CHANGED);
d2fa18d3
SC
295 if ( win )
296 {
297 SetEventObject( win );
298 SetId(win->GetId());
299 }
2c990ba6
KO
300}
301
448f8f12
KO
302IMPLEMENT_DYNAMIC_CLASS( wxWebKitBeforeLoadEvent, wxCommandEvent )
303
9b11752c 304wxDEFINE_EVENT( wxEVT_WEBKIT_BEFORE_LOAD, wxWebKitBeforeLoadEvent );
448f8f12
KO
305
306wxWebKitBeforeLoadEvent::wxWebKitBeforeLoadEvent( wxWindow* win )
307{
308 m_cancelled = false;
309 SetEventType( wxEVT_WEBKIT_BEFORE_LOAD);
d2fa18d3
SC
310 if ( win )
311 {
312 SetEventObject( win );
313 SetId(win->GetId());
314 }
448f8f12
KO
315}
316
fa34bc53
RD
317
318IMPLEMENT_DYNAMIC_CLASS( wxWebKitNewWindowEvent, wxCommandEvent )
319
9b11752c 320wxDEFINE_EVENT( wxEVT_WEBKIT_NEW_WINDOW, wxWebKitNewWindowEvent );
fa34bc53
RD
321
322wxWebKitNewWindowEvent::wxWebKitNewWindowEvent( wxWindow* win )
323{
324 SetEventType( wxEVT_WEBKIT_NEW_WINDOW);
d2fa18d3
SC
325 if ( win )
326 {
327 SetEventObject( win );
328 SetId(win->GetId());
329 }
fa34bc53
RD
330}
331
332
333
2c990ba6
KO
334//---------------------------------------------------------
335// helper functions for NSString<->wxString conversion
336//---------------------------------------------------------
337
338inline wxString wxStringWithNSString(NSString *nsstring)
339{
340#if wxUSE_UNICODE
341 return wxString([nsstring UTF8String], wxConvUTF8);
342#else
343 return wxString([nsstring lossyCString]);
344#endif // wxUSE_UNICODE
345}
346
347inline NSString* wxNSStringWithWxString(const wxString &wxstring)
348{
349#if wxUSE_UNICODE
350 return [NSString stringWithUTF8String: wxstring.mb_str(wxConvUTF8)];
351#else
352 return [NSString stringWithCString: wxstring.c_str() length:wxstring.Len()];
353#endif // wxUSE_UNICODE
354}
355
448f8f12
KO
356inline int wxNavTypeFromWebNavType(int type){
357 if (type == WebNavigationTypeLinkClicked)
358 return wxWEBKIT_NAV_LINK_CLICKED;
8a088306 359
448f8f12
KO
360 if (type == WebNavigationTypeFormSubmitted)
361 return wxWEBKIT_NAV_FORM_SUBMITTED;
8a088306 362
448f8f12
KO
363 if (type == WebNavigationTypeBackForward)
364 return wxWEBKIT_NAV_BACK_NEXT;
8a088306 365
448f8f12
KO
366 if (type == WebNavigationTypeReload)
367 return wxWEBKIT_NAV_RELOAD;
8a088306 368
448f8f12
KO
369 if (type == WebNavigationTypeFormResubmitted)
370 return wxWEBKIT_NAV_FORM_RESUBMITTED;
8a088306 371
448f8f12
KO
372 return wxWEBKIT_NAV_OTHER;
373}
374
2c990ba6
KO
375@interface MyFrameLoadMonitor : NSObject
376{
448f8f12
KO
377 wxWebKitCtrl* webKitWindow;
378}
379
380- initWithWxWindow: (wxWebKitCtrl*)inWindow;
381
382@end
383
384@interface MyPolicyDelegate : NSObject
385{
386 wxWebKitCtrl* webKitWindow;
2c990ba6
KO
387}
388
448f8f12 389- initWithWxWindow: (wxWebKitCtrl*)inWindow;
2c990ba6
KO
390
391@end
392
dac3f6b2
RD
393@interface MyUIDelegate : NSObject
394{
395 wxWebKitCtrl* webKitWindow;
396}
397
398- initWithWxWindow: (wxWebKitCtrl*)inWindow;
399
400@end
401
2c990ba6
KO
402// ----------------------------------------------------------------------------
403// creation/destruction
404// ----------------------------------------------------------------------------
405
406bool wxWebKitCtrl::Create(wxWindow *parent,
3ca7ba74 407 wxWindowID winID,
2c990ba6
KO
408 const wxString& strURL,
409 const wxPoint& pos,
3ca7ba74
RD
410 const wxSize& size, long style,
411 const wxValidator& validator,
412 const wxString& name)
2c990ba6 413{
2c990ba6 414 m_currentURL = strURL;
14f21d6d 415 //m_pageTitle = _("Untitled Page");
9548f380 416
2c990ba6
KO
417 //still needed for wxCocoa??
418/*
419 int width, height;
3ca7ba74 420 wxSize sizeInstance;
9548f380 421 if (size.x == wxDefaultCoord || size.y == wxDefaultCoord)
3ca7ba74
RD
422 {
423 m_parent->GetClientSize(&width, &height);
424 sizeInstance.x = width;
425 sizeInstance.y = height;
426 }
427 else
428 {
429 sizeInstance.x = size.x;
430 sizeInstance.y = size.y;
431 }
9548f380 432*/
3ca7ba74 433 // now create and attach WebKit view...
d15694e8 434 DontCreatePeer();
2c990ba6
KO
435#ifdef __WXCOCOA__
436 wxControl::Create(parent, m_windowID, pos, sizeInstance, style , validator , name);
3ca7ba74 437 SetSize(pos.x, pos.y, sizeInstance.x, sizeInstance.y);
9548f380 438
2c990ba6
KO
439 wxTopLevelWindowCocoa *topWin = wxDynamicCast(this, wxTopLevelWindowCocoa);
440 NSWindow* nsWin = topWin->GetNSWindow();
3ca7ba74 441 NSRect rect;
2c990ba6
KO
442 rect.origin.x = pos.x;
443 rect.origin.y = pos.y;
444 rect.size.width = sizeInstance.x;
445 rect.size.height = sizeInstance.y;
446 m_webView = (WebView*)[[WebView alloc] initWithFrame:rect frameName:@"webkitFrame" groupName:@"webkitGroup"];
447 SetNSView(m_webView);
448 [m_cocoaNSView release];
449
450 if(m_parent) m_parent->CocoaAddChild(this);
451 SetInitialFrameRect(pos,sizeInstance);
452#else
d15694e8 453 DontCreatePeer();
daef4689 454 wxControl::Create(parent, winID, pos, size, style , validator , name);
14ca15f1 455#if wxOSX_USE_CARBON
d15694e8 456 wxMacControl* peer = new wxMacControl(this);
2c990ba6 457 WebInitForCarbon();
d15694e8 458 HIWebViewCreate( peer->GetControlRefAddr() );
9548f380 459
d15694e8 460 m_webView = (WebView*) HIWebViewGetWebView( peer->GetControlRef() );
8a088306 461
b384c0fb 462#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
daef4689 463 if ( UMAGetSystemVersion() >= 0x1030 )
d15694e8 464 HIViewChangeFeatures( peer->GetControlRef() , kHIViewIsOpaque , 0 ) ;
b384c0fb 465#endif
d15694e8 466 InstallControlEventHandler( peer->GetControlRef() , GetwxWebKitCtrlEventHandlerUPP(),
448f8f12
KO
467 GetEventTypeCount(eventList), eventList, this,
468 (EventHandlerRef *)&m_webKitCtrlEventHandler);
d15694e8
SC
469
470 SetPeer(peer);
14ca15f1
SC
471#else
472 NSRect r = wxOSXGetFrameForControl( this, pos , size ) ;
473 m_webView = [[WebView alloc] initWithFrame:r frameName:@"webkitFrame" groupName:@"webkitGroup"];
474
22756322 475 SetPeer(new wxWidgetCocoaImpl( this, m_webView ));
14ca15f1
SC
476#endif
477 MacPostControlCreate(pos, size);
478#if wxOSX_USE_CARBON
22756322 479 HIViewSetVisible( GetPeer()->GetControlRef(), true );
14ca15f1
SC
480#endif
481 [m_webView setHidden:false];
448f8f12 482
2c990ba6
KO
483#endif
484
485 // Register event listener interfaces
448f8f12 486 MyFrameLoadMonitor* myFrameLoadMonitor = [[MyFrameLoadMonitor alloc] initWithWxWindow: this];
3ca7ba74 487 [m_webView setFrameLoadDelegate:myFrameLoadMonitor];
8a088306 488
448f8f12
KO
489 // this is used to veto page loads, etc.
490 MyPolicyDelegate* myPolicyDelegate = [[MyPolicyDelegate alloc] initWithWxWindow: this];
491 [m_webView setPolicyDelegate:myPolicyDelegate];
8a088306 492
dac3f6b2
RD
493 // this is used to provide printing support for JavaScript
494 MyUIDelegate* myUIDelegate = [[MyUIDelegate alloc] initWithWxWindow: this];
495 [m_webView setUIDelegate:myUIDelegate];
496
2c990ba6
KO
497 LoadURL(m_currentURL);
498 return true;
499}
500
501wxWebKitCtrl::~wxWebKitCtrl()
502{
c9f9deab
KO
503 MyFrameLoadMonitor* myFrameLoadMonitor = [m_webView frameLoadDelegate];
504 MyPolicyDelegate* myPolicyDelegate = [m_webView policyDelegate];
dac3f6b2 505 MyUIDelegate* myUIDelegate = [m_webView UIDelegate];
c9f9deab
KO
506 [m_webView setFrameLoadDelegate: nil];
507 [m_webView setPolicyDelegate: nil];
dac3f6b2 508 [m_webView setUIDelegate: nil];
c9f9deab
KO
509
510 if (myFrameLoadMonitor)
511 [myFrameLoadMonitor release];
512
513 if (myPolicyDelegate)
514 [myPolicyDelegate release];
dac3f6b2
RD
515
516 if (myUIDelegate)
517 [myUIDelegate release];
2c990ba6
KO
518}
519
520// ----------------------------------------------------------------------------
521// public methods
522// ----------------------------------------------------------------------------
523
524void wxWebKitCtrl::LoadURL(const wxString &url)
525{
526 if( !m_webView )
527 return;
9548f380 528
7eb8c6ee 529 [[m_webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:wxNSStringWithWxString(url)]]];
2c990ba6
KO
530
531 m_currentURL = url;
532}
533
534bool wxWebKitCtrl::CanGoBack(){
535 if ( !m_webView )
536 return false;
9548f380 537
2c990ba6
KO
538 return [m_webView canGoBack];
539}
540
541bool wxWebKitCtrl::CanGoForward(){
542 if ( !m_webView )
543 return false;
9548f380 544
2c990ba6
KO
545 return [m_webView canGoForward];
546}
547
548bool wxWebKitCtrl::GoBack(){
549 if ( !m_webView )
550 return false;
9548f380 551
8f3b30d5
KO
552 bool result = [(WebView*)m_webView goBack];
553 return result;
2c990ba6
KO
554}
555
9548f380 556bool wxWebKitCtrl::GoForward(){
2c990ba6
KO
557 if ( !m_webView )
558 return false;
9548f380 559
8f3b30d5
KO
560 bool result = [(WebView*)m_webView goForward];
561 return result;
2c990ba6
KO
562}
563
9548f380 564void wxWebKitCtrl::Reload(){
2c990ba6
KO
565 if ( !m_webView )
566 return;
9548f380 567
2c990ba6
KO
568 [[m_webView mainFrame] reload];
569}
570
571void wxWebKitCtrl::Stop(){
572 if ( !m_webView )
573 return;
9548f380 574
2c990ba6
KO
575 [[m_webView mainFrame] stopLoading];
576}
577
578bool wxWebKitCtrl::CanGetPageSource(){
579 if ( !m_webView )
14f21d6d 580 return false;
9548f380 581
2c990ba6
KO
582 WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
583 return ( [[dataSource representation] canProvideDocumentSource] );
584}
585
586wxString wxWebKitCtrl::GetPageSource(){
9548f380 587
2c990ba6
KO
588 if (CanGetPageSource()){
589 WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
590 return wxStringWithNSString( [[dataSource representation] documentSource] );
591 }
9548f380
WS
592
593 return wxEmptyString;
2c990ba6
KO
594}
595
448f8f12
KO
596wxString wxWebKitCtrl::GetSelection(){
597 if ( !m_webView )
598 return wxEmptyString;
8a088306 599
448f8f12
KO
600 NSString* selectedText = [[m_webView selectedDOMRange] toString];
601 return wxStringWithNSString( selectedText );
602}
603
604bool wxWebKitCtrl::CanIncreaseTextSize(){
605 if ( !m_webView )
606 return false;
8a088306 607
448f8f12
KO
608 if ([m_webView canMakeTextLarger])
609 return true;
610 else
611 return false;
612}
613
614void wxWebKitCtrl::IncreaseTextSize(){
615 if ( !m_webView )
616 return;
8a088306 617
448f8f12
KO
618 if (CanIncreaseTextSize())
619 [m_webView makeTextLarger:(WebView*)m_webView];
620}
621
622bool wxWebKitCtrl::CanDecreaseTextSize(){
623 if ( !m_webView )
624 return false;
8a088306 625
448f8f12
KO
626 if ([m_webView canMakeTextSmaller])
627 return true;
628 else
629 return false;
630}
631
632void wxWebKitCtrl::DecreaseTextSize(){
633 if ( !m_webView )
634 return;
8a088306 635
448f8f12
KO
636 if (CanDecreaseTextSize())
637 [m_webView makeTextSmaller:(WebView*)m_webView];
638}
639
640void wxWebKitCtrl::SetPageSource(const wxString& source, const wxString& baseUrl){
2c990ba6
KO
641 if ( !m_webView )
642 return;
9548f380 643
2d837688 644 [[m_webView mainFrame] loadHTMLString:(NSString*)wxNSStringWithWxString( source ) baseURL:[NSURL URLWithString:wxNSStringWithWxString( baseUrl )]];
448f8f12
KO
645
646}
647
648void wxWebKitCtrl::Print(bool showPrompt){
649 if ( !m_webView )
650 return;
8a088306
VZ
651
652 id view = [[[m_webView mainFrame] frameView] documentView];
653 NSPrintOperation *op = [NSPrintOperation printOperationWithView:view printInfo: [NSPrintInfo sharedPrintInfo]];
448f8f12
KO
654 if (showPrompt){
655 [op setShowsPrintPanel: showPrompt];
656 // in my tests, the progress bar always freezes and it stops the whole print operation.
657 // do not turn this to true unless there is a workaround for the bug.
658 [op setShowsProgressPanel: false];
659 }
8a088306
VZ
660 // Print it.
661 [op runOperation];
448f8f12
KO
662}
663
664void wxWebKitCtrl::MakeEditable(bool enable){
665 if ( !m_webView )
666 return;
667
668 [m_webView setEditable:enable ];
669}
670
671bool wxWebKitCtrl::IsEditable(){
672 if ( !m_webView )
673 return false;
674
675 return [m_webView isEditable];
676}
677
678int wxWebKitCtrl::GetScrollPos(){
679 id result = [[m_webView windowScriptObject] evaluateWebScript:@"document.body.scrollTop"];
680 return [result intValue];
8a088306 681}
448f8f12
KO
682
683void wxWebKitCtrl::SetScrollPos(int pos){
684 if ( !m_webView )
685 return;
8a088306
VZ
686
687 wxString javascript;
448f8f12
KO
688 javascript.Printf(wxT("document.body.scrollTop = %d;"), pos);
689 [[m_webView windowScriptObject] evaluateWebScript:(NSString*)wxNSStringWithWxString( javascript )];
690}
691
692wxString wxWebKitCtrl::RunScript(const wxString& javascript){
693 if ( !m_webView )
8a088306
VZ
694 return wxEmptyString;
695
448f8f12 696 id result = [[m_webView windowScriptObject] evaluateWebScript:(NSString*)wxNSStringWithWxString( javascript )];
8a088306 697
448f8f12 698 NSString* resultAsString;
3e1db655
VZ
699 if ([result isKindOfClass:[NSNumber class]]){
700 // __NSCFBoolean is a subclass of NSNumber
701 if (strcmp([result objCType], @encode(BOOL)) == 0){
702 if ([result boolValue])
703 resultAsString = @"true";
704 else
705 resultAsString = @"false";
706 }
448f8f12 707 else
3e1db655 708 resultAsString = [NSString stringWithFormat:@"%@", result];
448f8f12 709 }
3e1db655
VZ
710 else if ([result isKindOfClass:[NSString class]])
711 resultAsString = result;
712 else if ([result isKindOfClass:[WebScriptObject class]])
448f8f12
KO
713 resultAsString = [result stringRepresentation];
714 else
42678722 715 return wxString(); // This can happen, see e.g. #12361.
448f8f12 716
42678722 717 return wxStringWithNSString( resultAsString );
2c990ba6
KO
718}
719
005198fa 720void wxWebKitCtrl::OnSize(wxSizeEvent &event){
06d14de2 721#if defined(__WXMAC__) && wxOSX_USE_CARBON
39104bc1
KO
722 // This is a nasty hack because WebKit seems to lose its position when it is embedded
723 // in a control that is not itself the content view for a TLW.
daef4689
VZ
724 // I put it in OnSize because these calcs are not perfect, and in fact are basically
725 // guesses based on reverse engineering, so it's best to give people the option of
726 // overriding OnSize with their own calcs if need be.
727 // I also left some test debugging print statements as a convenience if a(nother)
728 // problem crops up.
729
daef4689
VZ
730 wxWindow* tlw = MacGetTopLevelWindow();
731
f94a7798
SC
732 NSRect frame = [(WebView*)m_webView frame];
733 NSRect bounds = [(WebView*)m_webView bounds];
8a088306 734
448f8f12
KO
735#if DEBUG_WEBKIT_SIZING
736 fprintf(stderr,"Carbon window x=%d, y=%d, width=%d, height=%d\n", GetPosition().x, GetPosition().y, GetSize().x, GetSize().y);
737 fprintf(stderr, "Cocoa window frame x=%G, y=%G, width=%G, height=%G\n", frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
738 fprintf(stderr, "Cocoa window bounds x=%G, y=%G, width=%G, height=%G\n", bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height);
739#endif
740
daef4689
VZ
741 // This must be the case that Apple tested with, because well, in this one case
742 // we don't need to do anything! It just works. ;)
448f8f12 743 if (GetParent() == tlw){
daef4689
VZ
744 return;
745 }
746
c70aaa09 747 // since we no longer use parent coordinates, we always want 0,0.
8a088306 748 int x = 0;
c70aaa09 749 int y = 0;
8a088306 750
448f8f12
KO
751 HIRect rect;
752 rect.origin.x = x;
753 rect.origin.y = y;
daef4689 754
448f8f12
KO
755#if DEBUG_WEBKIT_SIZING
756 printf("Before conversion, origin is: x = %d, y = %d\n", x, y);
757#endif
daef4689 758
448f8f12
KO
759 // NB: In most cases, when calling HIViewConvertRect, what people want is to use GetRootControl(),
760 // and this tripped me up at first. But in fact, what we want is the root view, because we need to
8a088306
VZ
761 // make the y origin relative to the very top of the window, not its contents, since we later flip
762 // the y coordinate for Cocoa.
22756322 763 HIViewConvertRect (&rect, GetPeer()->GetControlRef(),
448f8f12 764 HIViewGetRoot( (WindowRef) MacGetTopLevelWindowRef() ) );
daef4689 765
448f8f12
KO
766 x = (int)rect.origin.x;
767 y = (int)rect.origin.y;
daef4689 768
448f8f12
KO
769#if DEBUG_WEBKIT_SIZING
770 printf("Moving Cocoa frame origin to: x = %d, y = %d\n", x, y);
771#endif
daef4689
VZ
772
773 if (tlw){
774 //flip the y coordinate to convert to Cocoa coordinates
daef4689
VZ
775 y = tlw->GetSize().y - ((GetSize().y) + y);
776 }
777
448f8f12
KO
778#if DEBUG_WEBKIT_SIZING
779 printf("y = %d after flipping value\n", y);
780#endif
9548f380 781
448f8f12
KO
782 frame.origin.x = x;
783 frame.origin.y = y;
f94a7798 784 [(WebView*)m_webView setFrame:frame];
8a088306 785
fe3fc02e 786 if (IsShown())
14753ffe 787 [(WebView*)m_webView display];
14ca15f1 788#endif
e8fd252e 789 event.Skip();
005198fa 790}
2c990ba6 791
14f21d6d 792void wxWebKitCtrl::MacVisibilityChanged(){
14ca15f1 793#if defined(__WXMAC__) && wxOSX_USE_CARBON
22756322 794 bool isHidden = !IsControlVisible( GetPeer()->GetControlRef());
fe3fc02e 795 if (!isHidden)
14753ffe 796 [(WebView*)m_webView display];
9548f380 797
14f21d6d 798 [m_webView setHidden:isHidden];
14ca15f1 799#endif
14f21d6d
KO
800}
801
2c990ba6
KO
802//------------------------------------------------------------
803// Listener interfaces
804//------------------------------------------------------------
805
c70aaa09
KO
806// NB: I'm still tracking this down, but it appears the Cocoa window
807// still has these events fired on it while the Carbon control is being
808// destroyed. Therefore, we must be careful to check both the existence
809// of the Carbon control and the event handler before firing events.
810
2c990ba6
KO
811@implementation MyFrameLoadMonitor
812
448f8f12 813- initWithWxWindow: (wxWebKitCtrl*)inWindow
2c990ba6 814{
ee442831 815 self = [super init];
3ca7ba74 816 webKitWindow = inWindow; // non retained
2c990ba6
KO
817 return self;
818}
819
820- (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame
821{
c70aaa09 822 if (webKitWindow && frame == [sender mainFrame]){
2c990ba6
KO
823 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
824 wxWebKitStateChangedEvent thisEvent(webKitWindow);
825 thisEvent.SetState(wxWEBKIT_STATE_NEGOTIATING);
826 thisEvent.SetURL( wxStringWithNSString( url ) );
c70aaa09
KO
827 if (webKitWindow->GetEventHandler())
828 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
2c990ba6
KO
829 }
830}
831
832- (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame
833{
c70aaa09 834 if (webKitWindow && frame == [sender mainFrame]){
fba8e95e 835 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
2c990ba6
KO
836 wxWebKitStateChangedEvent thisEvent(webKitWindow);
837 thisEvent.SetState(wxWEBKIT_STATE_TRANSFERRING);
838 thisEvent.SetURL( wxStringWithNSString( url ) );
c70aaa09
KO
839 if (webKitWindow->GetEventHandler())
840 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
2c990ba6
KO
841 }
842}
843
844- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
845{
c70aaa09 846 if (webKitWindow && frame == [sender mainFrame]){
fba8e95e 847 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
2c990ba6
KO
848 wxWebKitStateChangedEvent thisEvent(webKitWindow);
849 thisEvent.SetState(wxWEBKIT_STATE_STOP);
850 thisEvent.SetURL( wxStringWithNSString( url ) );
c70aaa09
KO
851 if (webKitWindow->GetEventHandler())
852 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
2c990ba6
KO
853 }
854}
855
856- (void)webView:(WebView *)sender didFailLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
857{
8a088306
VZ
858 wxUnusedVar(error);
859
c70aaa09 860 if (webKitWindow && frame == [sender mainFrame]){
fba8e95e 861 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
2c990ba6
KO
862 wxWebKitStateChangedEvent thisEvent(webKitWindow);
863 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
864 thisEvent.SetURL( wxStringWithNSString( url ) );
c70aaa09
KO
865 if (webKitWindow->GetEventHandler())
866 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
2c990ba6
KO
867 }
868}
869
870- (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
871{
8a088306
VZ
872 wxUnusedVar(error);
873
c70aaa09 874 if (webKitWindow && frame == [sender mainFrame]){
2c990ba6
KO
875 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
876 wxWebKitStateChangedEvent thisEvent(webKitWindow);
877 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
878 thisEvent.SetURL( wxStringWithNSString( url ) );
c70aaa09
KO
879 if (webKitWindow->GetEventHandler())
880 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
2c990ba6
KO
881 }
882}
883
884- (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
885{
c70aaa09 886 if (webKitWindow && frame == [sender mainFrame]){
448f8f12 887 webKitWindow->SetPageTitle(wxStringWithNSString( title ));
2c990ba6
KO
888 }
889}
3ca7ba74 890@end
5b8f917c 891
448f8f12
KO
892@implementation MyPolicyDelegate
893
894- initWithWxWindow: (wxWebKitCtrl*)inWindow
895{
ee442831 896 self = [super init];
448f8f12
KO
897 webKitWindow = inWindow; // non retained
898 return self;
899}
900
901- (void)webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener
902{
8a088306
VZ
903 wxUnusedVar(sender);
904 wxUnusedVar(frame);
905
448f8f12
KO
906 wxWebKitBeforeLoadEvent thisEvent(webKitWindow);
907
8a088306
VZ
908 // Get the navigation type.
909 NSNumber *n = [actionInformation objectForKey:WebActionNavigationTypeKey];
448f8f12
KO
910 int actionType = [n intValue];
911 thisEvent.SetNavigationType( wxNavTypeFromWebNavType(actionType) );
8a088306 912
448f8f12
KO
913 NSString *url = [[request URL] absoluteString];
914 thisEvent.SetURL( wxStringWithNSString( url ) );
8a088306 915
c70aaa09
KO
916 if (webKitWindow && webKitWindow->GetEventHandler())
917 webKitWindow->GetEventHandler()->ProcessEvent(thisEvent);
448f8f12 918
8a088306 919 if (thisEvent.IsCancelled())
448f8f12
KO
920 [listener ignore];
921 else
922 [listener use];
923}
924
fa34bc53
RD
925- (void)webView:(WebView *)sender decidePolicyForNewWindowAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request newFrameName:(NSString *)frameName decisionListener:(id < WebPolicyDecisionListener >)listener
926{
14ca15f1
SC
927 wxUnusedVar(sender);
928 wxUnusedVar(actionInformation);
fa34bc53
RD
929 wxWebKitNewWindowEvent thisEvent(webKitWindow);
930
931 NSString *url = [[request URL] absoluteString];
932 thisEvent.SetURL( wxStringWithNSString( url ) );
933 thisEvent.SetTargetName( wxStringWithNSString( frameName ) );
934
935 if (webKitWindow && webKitWindow->GetEventHandler())
936 webKitWindow->GetEventHandler()->ProcessEvent(thisEvent);
937
938 [listener use];
939}
448f8f12
KO
940@end
941
dac3f6b2
RD
942
943@implementation MyUIDelegate
944
945- initWithWxWindow: (wxWebKitCtrl*)inWindow
946{
947 self = [super init];
948 webKitWindow = inWindow; // non retained
949 return self;
950}
951
952- (void)webView:(WebView *)sender printFrameView:(WebFrameView *)frameView
953{
954 wxUnusedVar(sender);
955 wxUnusedVar(frameView);
956
957 webKitWindow->Print(true);
958}
959@end
960
948f6c6e 961#endif //wxUSE_WEBKIT