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