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