]> git.saurik.com Git - wxWidgets.git/blame - src/html/htmlctrl/webkit/webkit.mm
Enable the wxMediaCtrl backend for wxOSX-cocoa in the build and fix some bugs that...
[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
393// ----------------------------------------------------------------------------
394// creation/destruction
395// ----------------------------------------------------------------------------
396
397bool wxWebKitCtrl::Create(wxWindow *parent,
3ca7ba74 398 wxWindowID winID,
2c990ba6
KO
399 const wxString& strURL,
400 const wxPoint& pos,
3ca7ba74
RD
401 const wxSize& size, long style,
402 const wxValidator& validator,
403 const wxString& name)
2c990ba6 404{
2c990ba6 405 m_currentURL = strURL;
14f21d6d 406 //m_pageTitle = _("Untitled Page");
9548f380 407
2c990ba6
KO
408 //still needed for wxCocoa??
409/*
410 int width, height;
3ca7ba74 411 wxSize sizeInstance;
9548f380 412 if (size.x == wxDefaultCoord || size.y == wxDefaultCoord)
3ca7ba74
RD
413 {
414 m_parent->GetClientSize(&width, &height);
415 sizeInstance.x = width;
416 sizeInstance.y = height;
417 }
418 else
419 {
420 sizeInstance.x = size.x;
421 sizeInstance.y = size.y;
422 }
9548f380 423*/
3ca7ba74 424 // now create and attach WebKit view...
d15694e8 425 DontCreatePeer();
2c990ba6
KO
426#ifdef __WXCOCOA__
427 wxControl::Create(parent, m_windowID, pos, sizeInstance, style , validator , name);
3ca7ba74 428 SetSize(pos.x, pos.y, sizeInstance.x, sizeInstance.y);
9548f380 429
2c990ba6
KO
430 wxTopLevelWindowCocoa *topWin = wxDynamicCast(this, wxTopLevelWindowCocoa);
431 NSWindow* nsWin = topWin->GetNSWindow();
3ca7ba74 432 NSRect rect;
2c990ba6
KO
433 rect.origin.x = pos.x;
434 rect.origin.y = pos.y;
435 rect.size.width = sizeInstance.x;
436 rect.size.height = sizeInstance.y;
437 m_webView = (WebView*)[[WebView alloc] initWithFrame:rect frameName:@"webkitFrame" groupName:@"webkitGroup"];
438 SetNSView(m_webView);
439 [m_cocoaNSView release];
440
441 if(m_parent) m_parent->CocoaAddChild(this);
442 SetInitialFrameRect(pos,sizeInstance);
443#else
d15694e8 444 DontCreatePeer();
daef4689 445 wxControl::Create(parent, winID, pos, size, style , validator , name);
14ca15f1 446#if wxOSX_USE_CARBON
d15694e8 447 wxMacControl* peer = new wxMacControl(this);
2c990ba6 448 WebInitForCarbon();
d15694e8 449 HIWebViewCreate( peer->GetControlRefAddr() );
9548f380 450
d15694e8 451 m_webView = (WebView*) HIWebViewGetWebView( peer->GetControlRef() );
8a088306 452
b384c0fb 453#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
daef4689 454 if ( UMAGetSystemVersion() >= 0x1030 )
d15694e8 455 HIViewChangeFeatures( peer->GetControlRef() , kHIViewIsOpaque , 0 ) ;
b384c0fb 456#endif
d15694e8 457 InstallControlEventHandler( peer->GetControlRef() , GetwxWebKitCtrlEventHandlerUPP(),
448f8f12
KO
458 GetEventTypeCount(eventList), eventList, this,
459 (EventHandlerRef *)&m_webKitCtrlEventHandler);
d15694e8
SC
460
461 SetPeer(peer);
14ca15f1
SC
462#else
463 NSRect r = wxOSXGetFrameForControl( this, pos , size ) ;
464 m_webView = [[WebView alloc] initWithFrame:r frameName:@"webkitFrame" groupName:@"webkitGroup"];
465
22756322 466 SetPeer(new wxWidgetCocoaImpl( this, m_webView ));
14ca15f1
SC
467#endif
468 MacPostControlCreate(pos, size);
469#if wxOSX_USE_CARBON
22756322 470 HIViewSetVisible( GetPeer()->GetControlRef(), true );
14ca15f1
SC
471#endif
472 [m_webView setHidden:false];
448f8f12 473
2c990ba6
KO
474#endif
475
476 // Register event listener interfaces
448f8f12 477 MyFrameLoadMonitor* myFrameLoadMonitor = [[MyFrameLoadMonitor alloc] initWithWxWindow: this];
3ca7ba74 478 [m_webView setFrameLoadDelegate:myFrameLoadMonitor];
8a088306 479
448f8f12
KO
480 // this is used to veto page loads, etc.
481 MyPolicyDelegate* myPolicyDelegate = [[MyPolicyDelegate alloc] initWithWxWindow: this];
482 [m_webView setPolicyDelegate:myPolicyDelegate];
8a088306 483
2c990ba6
KO
484 LoadURL(m_currentURL);
485 return true;
486}
487
488wxWebKitCtrl::~wxWebKitCtrl()
489{
c9f9deab
KO
490 MyFrameLoadMonitor* myFrameLoadMonitor = [m_webView frameLoadDelegate];
491 MyPolicyDelegate* myPolicyDelegate = [m_webView policyDelegate];
492 [m_webView setFrameLoadDelegate: nil];
493 [m_webView setPolicyDelegate: nil];
494
495 if (myFrameLoadMonitor)
496 [myFrameLoadMonitor release];
497
498 if (myPolicyDelegate)
499 [myPolicyDelegate release];
2c990ba6
KO
500}
501
502// ----------------------------------------------------------------------------
503// public methods
504// ----------------------------------------------------------------------------
505
506void wxWebKitCtrl::LoadURL(const wxString &url)
507{
508 if( !m_webView )
509 return;
9548f380 510
7eb8c6ee 511 [[m_webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:wxNSStringWithWxString(url)]]];
2c990ba6
KO
512
513 m_currentURL = url;
514}
515
516bool wxWebKitCtrl::CanGoBack(){
517 if ( !m_webView )
518 return false;
9548f380 519
2c990ba6
KO
520 return [m_webView canGoBack];
521}
522
523bool wxWebKitCtrl::CanGoForward(){
524 if ( !m_webView )
525 return false;
9548f380 526
2c990ba6
KO
527 return [m_webView canGoForward];
528}
529
530bool wxWebKitCtrl::GoBack(){
531 if ( !m_webView )
532 return false;
9548f380 533
8f3b30d5
KO
534 bool result = [(WebView*)m_webView goBack];
535 return result;
2c990ba6
KO
536}
537
9548f380 538bool wxWebKitCtrl::GoForward(){
2c990ba6
KO
539 if ( !m_webView )
540 return false;
9548f380 541
8f3b30d5
KO
542 bool result = [(WebView*)m_webView goForward];
543 return result;
2c990ba6
KO
544}
545
9548f380 546void wxWebKitCtrl::Reload(){
2c990ba6
KO
547 if ( !m_webView )
548 return;
9548f380 549
2c990ba6
KO
550 [[m_webView mainFrame] reload];
551}
552
553void wxWebKitCtrl::Stop(){
554 if ( !m_webView )
555 return;
9548f380 556
2c990ba6
KO
557 [[m_webView mainFrame] stopLoading];
558}
559
560bool wxWebKitCtrl::CanGetPageSource(){
561 if ( !m_webView )
14f21d6d 562 return false;
9548f380 563
2c990ba6
KO
564 WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
565 return ( [[dataSource representation] canProvideDocumentSource] );
566}
567
568wxString wxWebKitCtrl::GetPageSource(){
9548f380 569
2c990ba6
KO
570 if (CanGetPageSource()){
571 WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
572 return wxStringWithNSString( [[dataSource representation] documentSource] );
573 }
9548f380
WS
574
575 return wxEmptyString;
2c990ba6
KO
576}
577
448f8f12
KO
578wxString wxWebKitCtrl::GetSelection(){
579 if ( !m_webView )
580 return wxEmptyString;
8a088306 581
448f8f12
KO
582 NSString* selectedText = [[m_webView selectedDOMRange] toString];
583 return wxStringWithNSString( selectedText );
584}
585
586bool wxWebKitCtrl::CanIncreaseTextSize(){
587 if ( !m_webView )
588 return false;
8a088306 589
448f8f12
KO
590 if ([m_webView canMakeTextLarger])
591 return true;
592 else
593 return false;
594}
595
596void wxWebKitCtrl::IncreaseTextSize(){
597 if ( !m_webView )
598 return;
8a088306 599
448f8f12
KO
600 if (CanIncreaseTextSize())
601 [m_webView makeTextLarger:(WebView*)m_webView];
602}
603
604bool wxWebKitCtrl::CanDecreaseTextSize(){
605 if ( !m_webView )
606 return false;
8a088306 607
448f8f12
KO
608 if ([m_webView canMakeTextSmaller])
609 return true;
610 else
611 return false;
612}
613
614void wxWebKitCtrl::DecreaseTextSize(){
615 if ( !m_webView )
616 return;
8a088306 617
448f8f12
KO
618 if (CanDecreaseTextSize())
619 [m_webView makeTextSmaller:(WebView*)m_webView];
620}
621
622void wxWebKitCtrl::SetPageSource(const wxString& source, const wxString& baseUrl){
2c990ba6
KO
623 if ( !m_webView )
624 return;
9548f380 625
2d837688 626 [[m_webView mainFrame] loadHTMLString:(NSString*)wxNSStringWithWxString( source ) baseURL:[NSURL URLWithString:wxNSStringWithWxString( baseUrl )]];
448f8f12
KO
627
628}
629
630void wxWebKitCtrl::Print(bool showPrompt){
631 if ( !m_webView )
632 return;
8a088306
VZ
633
634 id view = [[[m_webView mainFrame] frameView] documentView];
635 NSPrintOperation *op = [NSPrintOperation printOperationWithView:view printInfo: [NSPrintInfo sharedPrintInfo]];
448f8f12
KO
636 if (showPrompt){
637 [op setShowsPrintPanel: showPrompt];
638 // in my tests, the progress bar always freezes and it stops the whole print operation.
639 // do not turn this to true unless there is a workaround for the bug.
640 [op setShowsProgressPanel: false];
641 }
8a088306
VZ
642 // Print it.
643 [op runOperation];
448f8f12
KO
644}
645
646void wxWebKitCtrl::MakeEditable(bool enable){
647 if ( !m_webView )
648 return;
649
650 [m_webView setEditable:enable ];
651}
652
653bool wxWebKitCtrl::IsEditable(){
654 if ( !m_webView )
655 return false;
656
657 return [m_webView isEditable];
658}
659
660int wxWebKitCtrl::GetScrollPos(){
661 id result = [[m_webView windowScriptObject] evaluateWebScript:@"document.body.scrollTop"];
662 return [result intValue];
8a088306 663}
448f8f12
KO
664
665void wxWebKitCtrl::SetScrollPos(int pos){
666 if ( !m_webView )
667 return;
8a088306
VZ
668
669 wxString javascript;
448f8f12
KO
670 javascript.Printf(wxT("document.body.scrollTop = %d;"), pos);
671 [[m_webView windowScriptObject] evaluateWebScript:(NSString*)wxNSStringWithWxString( javascript )];
672}
673
674wxString wxWebKitCtrl::RunScript(const wxString& javascript){
675 if ( !m_webView )
8a088306
VZ
676 return wxEmptyString;
677
448f8f12 678 id result = [[m_webView windowScriptObject] evaluateWebScript:(NSString*)wxNSStringWithWxString( javascript )];
8a088306 679
448f8f12 680 NSString* resultAsString;
3e1db655
VZ
681 if ([result isKindOfClass:[NSNumber class]]){
682 // __NSCFBoolean is a subclass of NSNumber
683 if (strcmp([result objCType], @encode(BOOL)) == 0){
684 if ([result boolValue])
685 resultAsString = @"true";
686 else
687 resultAsString = @"false";
688 }
448f8f12 689 else
3e1db655 690 resultAsString = [NSString stringWithFormat:@"%@", result];
448f8f12 691 }
3e1db655
VZ
692 else if ([result isKindOfClass:[NSString class]])
693 resultAsString = result;
694 else if ([result isKindOfClass:[WebScriptObject class]])
448f8f12
KO
695 resultAsString = [result stringRepresentation];
696 else
42678722 697 return wxString(); // This can happen, see e.g. #12361.
448f8f12 698
42678722 699 return wxStringWithNSString( resultAsString );
2c990ba6
KO
700}
701
005198fa 702void wxWebKitCtrl::OnSize(wxSizeEvent &event){
06d14de2 703#if defined(__WXMAC__) && wxOSX_USE_CARBON
39104bc1
KO
704 // This is a nasty hack because WebKit seems to lose its position when it is embedded
705 // in a control that is not itself the content view for a TLW.
daef4689
VZ
706 // I put it in OnSize because these calcs are not perfect, and in fact are basically
707 // guesses based on reverse engineering, so it's best to give people the option of
708 // overriding OnSize with their own calcs if need be.
709 // I also left some test debugging print statements as a convenience if a(nother)
710 // problem crops up.
711
daef4689
VZ
712 wxWindow* tlw = MacGetTopLevelWindow();
713
f94a7798
SC
714 NSRect frame = [(WebView*)m_webView frame];
715 NSRect bounds = [(WebView*)m_webView bounds];
8a088306 716
448f8f12
KO
717#if DEBUG_WEBKIT_SIZING
718 fprintf(stderr,"Carbon window x=%d, y=%d, width=%d, height=%d\n", GetPosition().x, GetPosition().y, GetSize().x, GetSize().y);
719 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);
720 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);
721#endif
722
daef4689
VZ
723 // This must be the case that Apple tested with, because well, in this one case
724 // we don't need to do anything! It just works. ;)
448f8f12 725 if (GetParent() == tlw){
daef4689
VZ
726 return;
727 }
728
c70aaa09 729 // since we no longer use parent coordinates, we always want 0,0.
8a088306 730 int x = 0;
c70aaa09 731 int y = 0;
8a088306 732
448f8f12
KO
733 HIRect rect;
734 rect.origin.x = x;
735 rect.origin.y = y;
daef4689 736
448f8f12
KO
737#if DEBUG_WEBKIT_SIZING
738 printf("Before conversion, origin is: x = %d, y = %d\n", x, y);
739#endif
daef4689 740
448f8f12
KO
741 // NB: In most cases, when calling HIViewConvertRect, what people want is to use GetRootControl(),
742 // and this tripped me up at first. But in fact, what we want is the root view, because we need to
8a088306
VZ
743 // make the y origin relative to the very top of the window, not its contents, since we later flip
744 // the y coordinate for Cocoa.
22756322 745 HIViewConvertRect (&rect, GetPeer()->GetControlRef(),
448f8f12 746 HIViewGetRoot( (WindowRef) MacGetTopLevelWindowRef() ) );
daef4689 747
448f8f12
KO
748 x = (int)rect.origin.x;
749 y = (int)rect.origin.y;
daef4689 750
448f8f12
KO
751#if DEBUG_WEBKIT_SIZING
752 printf("Moving Cocoa frame origin to: x = %d, y = %d\n", x, y);
753#endif
daef4689
VZ
754
755 if (tlw){
756 //flip the y coordinate to convert to Cocoa coordinates
daef4689
VZ
757 y = tlw->GetSize().y - ((GetSize().y) + y);
758 }
759
448f8f12
KO
760#if DEBUG_WEBKIT_SIZING
761 printf("y = %d after flipping value\n", y);
762#endif
9548f380 763
448f8f12
KO
764 frame.origin.x = x;
765 frame.origin.y = y;
f94a7798 766 [(WebView*)m_webView setFrame:frame];
8a088306 767
fe3fc02e 768 if (IsShown())
14753ffe 769 [(WebView*)m_webView display];
14ca15f1 770#endif
e8fd252e 771 event.Skip();
005198fa 772}
2c990ba6 773
14f21d6d 774void wxWebKitCtrl::MacVisibilityChanged(){
14ca15f1 775#if defined(__WXMAC__) && wxOSX_USE_CARBON
22756322 776 bool isHidden = !IsControlVisible( GetPeer()->GetControlRef());
fe3fc02e 777 if (!isHidden)
14753ffe 778 [(WebView*)m_webView display];
9548f380 779
14f21d6d 780 [m_webView setHidden:isHidden];
14ca15f1 781#endif
14f21d6d
KO
782}
783
2c990ba6
KO
784//------------------------------------------------------------
785// Listener interfaces
786//------------------------------------------------------------
787
c70aaa09
KO
788// NB: I'm still tracking this down, but it appears the Cocoa window
789// still has these events fired on it while the Carbon control is being
790// destroyed. Therefore, we must be careful to check both the existence
791// of the Carbon control and the event handler before firing events.
792
2c990ba6
KO
793@implementation MyFrameLoadMonitor
794
448f8f12 795- initWithWxWindow: (wxWebKitCtrl*)inWindow
2c990ba6 796{
ee442831 797 self = [super init];
3ca7ba74 798 webKitWindow = inWindow; // non retained
2c990ba6
KO
799 return self;
800}
801
802- (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame
803{
c70aaa09 804 if (webKitWindow && frame == [sender mainFrame]){
2c990ba6
KO
805 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
806 wxWebKitStateChangedEvent thisEvent(webKitWindow);
807 thisEvent.SetState(wxWEBKIT_STATE_NEGOTIATING);
808 thisEvent.SetURL( wxStringWithNSString( url ) );
c70aaa09
KO
809 if (webKitWindow->GetEventHandler())
810 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
2c990ba6
KO
811 }
812}
813
814- (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame
815{
c70aaa09 816 if (webKitWindow && frame == [sender mainFrame]){
fba8e95e 817 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
2c990ba6
KO
818 wxWebKitStateChangedEvent thisEvent(webKitWindow);
819 thisEvent.SetState(wxWEBKIT_STATE_TRANSFERRING);
820 thisEvent.SetURL( wxStringWithNSString( url ) );
c70aaa09
KO
821 if (webKitWindow->GetEventHandler())
822 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
2c990ba6
KO
823 }
824}
825
826- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
827{
c70aaa09 828 if (webKitWindow && frame == [sender mainFrame]){
fba8e95e 829 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
2c990ba6
KO
830 wxWebKitStateChangedEvent thisEvent(webKitWindow);
831 thisEvent.SetState(wxWEBKIT_STATE_STOP);
832 thisEvent.SetURL( wxStringWithNSString( url ) );
c70aaa09
KO
833 if (webKitWindow->GetEventHandler())
834 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
2c990ba6
KO
835 }
836}
837
838- (void)webView:(WebView *)sender didFailLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
839{
8a088306
VZ
840 wxUnusedVar(error);
841
c70aaa09 842 if (webKitWindow && frame == [sender mainFrame]){
fba8e95e 843 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
2c990ba6
KO
844 wxWebKitStateChangedEvent thisEvent(webKitWindow);
845 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
846 thisEvent.SetURL( wxStringWithNSString( url ) );
c70aaa09
KO
847 if (webKitWindow->GetEventHandler())
848 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
2c990ba6
KO
849 }
850}
851
852- (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
853{
8a088306
VZ
854 wxUnusedVar(error);
855
c70aaa09 856 if (webKitWindow && frame == [sender mainFrame]){
2c990ba6
KO
857 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
858 wxWebKitStateChangedEvent thisEvent(webKitWindow);
859 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
860 thisEvent.SetURL( wxStringWithNSString( url ) );
c70aaa09
KO
861 if (webKitWindow->GetEventHandler())
862 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
2c990ba6
KO
863 }
864}
865
866- (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
867{
c70aaa09 868 if (webKitWindow && frame == [sender mainFrame]){
448f8f12 869 webKitWindow->SetPageTitle(wxStringWithNSString( title ));
2c990ba6
KO
870 }
871}
3ca7ba74 872@end
5b8f917c 873
448f8f12
KO
874@implementation MyPolicyDelegate
875
876- initWithWxWindow: (wxWebKitCtrl*)inWindow
877{
ee442831 878 self = [super init];
448f8f12
KO
879 webKitWindow = inWindow; // non retained
880 return self;
881}
882
883- (void)webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener
884{
8a088306
VZ
885 wxUnusedVar(sender);
886 wxUnusedVar(frame);
887
448f8f12
KO
888 wxWebKitBeforeLoadEvent thisEvent(webKitWindow);
889
8a088306
VZ
890 // Get the navigation type.
891 NSNumber *n = [actionInformation objectForKey:WebActionNavigationTypeKey];
448f8f12
KO
892 int actionType = [n intValue];
893 thisEvent.SetNavigationType( wxNavTypeFromWebNavType(actionType) );
8a088306 894
448f8f12
KO
895 NSString *url = [[request URL] absoluteString];
896 thisEvent.SetURL( wxStringWithNSString( url ) );
8a088306 897
c70aaa09
KO
898 if (webKitWindow && webKitWindow->GetEventHandler())
899 webKitWindow->GetEventHandler()->ProcessEvent(thisEvent);
448f8f12 900
8a088306 901 if (thisEvent.IsCancelled())
448f8f12
KO
902 [listener ignore];
903 else
904 [listener use];
905}
906
fa34bc53
RD
907- (void)webView:(WebView *)sender decidePolicyForNewWindowAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request newFrameName:(NSString *)frameName decisionListener:(id < WebPolicyDecisionListener >)listener
908{
14ca15f1
SC
909 wxUnusedVar(sender);
910 wxUnusedVar(actionInformation);
fa34bc53
RD
911 wxWebKitNewWindowEvent thisEvent(webKitWindow);
912
913 NSString *url = [[request URL] absoluteString];
914 thisEvent.SetURL( wxStringWithNSString( url ) );
915 thisEvent.SetTargetName( wxStringWithNSString( frameName ) );
916
917 if (webKitWindow && webKitWindow->GetEventHandler())
918 webKitWindow->GetEventHandler()->ProcessEvent(thisEvent);
919
920 [listener use];
921}
448f8f12
KO
922@end
923
948f6c6e 924#endif //wxUSE_WEBKIT