]> git.saurik.com Git - wxWidgets.git/blame - src/html/htmlctrl/webkit/webkit.mm
Virtualize wxSocketImpl creation by routing it via wxSocketManager.
[wxWidgets.git] / src / html / htmlctrl / webkit / webkit.mm
CommitLineData
2c990ba6
KO
1/////////////////////////////////////////////////////////////////////////////
2// Name: webkit.mm
3// Purpose: wxWebKitCtrl - embeddable web kit control
4// Author: Jethro Grassie / Kevin Ollivier
5// Modified by:
6// Created: 2004-4-16
7// RCS-ID: $Id$
8// Copyright: (c) Jethro Grassie / Kevin Ollivier
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
448f8f12
KO
12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "webkit.h"
14#endif
15
2c990ba6
KO
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
216e968a 18#include "wx/splitter.h"
2c990ba6 19
2c990ba6
KO
20#ifndef WX_PRECOMP
21 #include "wx/wx.h"
22#endif
23
948f6c6e
JS
24#if wxUSE_WEBKIT
25
2c990ba6
KO
26#ifdef __WXCOCOA__
27#include "wx/cocoa/autorelease.h"
28#else
14ca15f1
SC
29#include "wx/osx/private.h"
30
b384c0fb 31#include <WebKit/WebKit.h>
2c990ba6
KO
32#include <WebKit/HIWebView.h>
33#include <WebKit/CarbonUtils.h>
34#endif
35
36#include "wx/html/webkit.h"
2c990ba6 37
448f8f12 38#define DEBUG_WEBKIT_SIZING 0
2c990ba6
KO
39
40// ----------------------------------------------------------------------------
41// macros
42// ----------------------------------------------------------------------------
43
2c990ba6 44IMPLEMENT_DYNAMIC_CLASS(wxWebKitCtrl, wxControl)
2c990ba6
KO
45
46BEGIN_EVENT_TABLE(wxWebKitCtrl, wxControl)
14ca15f1 47#if defined(__WXMAC__) && wxOSX_USE_CARBON
005198fa 48 EVT_SIZE(wxWebKitCtrl::OnSize)
14ca15f1 49#endif
2c990ba6
KO
50END_EVENT_TABLE()
51
14ca15f1
SC
52#if defined(__WXOSX__) && wxOSX_USE_CARBON
53
448f8f12
KO
54// ----------------------------------------------------------------------------
55// Carbon Events handlers
56// ----------------------------------------------------------------------------
57
58// prototype for function in src/mac/carbon/toplevel.cpp
59void SetupMouseEvent( wxMouseEvent &wxevent , wxMacCarbonEvent &cEvent );
60
61static const EventTypeSpec eventList[] =
62{
63 //{ kEventClassControl, kEventControlTrack } ,
64 { kEventClassMouse, kEventMouseUp },
65 { kEventClassMouse, kEventMouseDown },
66 { kEventClassMouse, kEventMouseMoved },
67 { kEventClassMouse, kEventMouseDragged },
8a088306 68
c70aaa09
KO
69 { kEventClassKeyboard, kEventRawKeyDown } ,
70 { kEventClassKeyboard, kEventRawKeyRepeat } ,
71 { kEventClassKeyboard, kEventRawKeyUp } ,
72 { kEventClassKeyboard, kEventRawKeyModifiersChanged } ,
8a088306 73
c70aaa09
KO
74 { kEventClassTextInput, kEventTextInputUnicodeForKeyEvent } ,
75 { kEventClassTextInput, kEventTextInputUpdateActiveInputArea } ,
8a088306 76
448f8f12
KO
77#if DEBUG_WEBKIT_SIZING == 1
78 { kEventClassControl, kEventControlBoundsChanged } ,
79#endif
80};
81
c70aaa09
KO
82// mix this in from window.cpp
83pascal OSStatus wxMacUnicodeTextEventHandler( EventHandlerCallRef handler , EventRef event , void *data ) ;
84
85// NOTE: This is mostly taken from KeyboardEventHandler in toplevel.cpp, but
86// that expects the data pointer is a top-level window, so I needed to change
87// that in this case. However, once 2.8 is out, we should factor out the common logic
88// among the two functions and merge them.
89static pascal OSStatus wxWebKitKeyEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
90{
91 OSStatus result = eventNotHandledErr ;
92 wxMacCarbonEvent cEvent( event ) ;
93
8a088306 94 wxWebKitCtrl* thisWindow = (wxWebKitCtrl*) data ;
c70aaa09
KO
95 wxWindow* focus = thisWindow ;
96
97 unsigned char charCode ;
98 wxChar uniChar[2] ;
99 uniChar[0] = 0;
100 uniChar[1] = 0;
101
102 UInt32 keyCode ;
103 UInt32 modifiers ;
104 Point point ;
105 UInt32 when = EventTimeToTicks( GetEventTime( event ) ) ;
106
107#if wxUSE_UNICODE
108 ByteCount dataSize = 0 ;
109 if ( GetEventParameter( event, kEventParamKeyUnicodes, typeUnicodeText, NULL, 0 , &dataSize, NULL ) == noErr )
110 {
111 UniChar buf[2] ;
112 int numChars = dataSize / sizeof( UniChar) + 1;
113
114 UniChar* charBuf = buf ;
115
116 if ( numChars * 2 > 4 )
117 charBuf = new UniChar[ numChars ] ;
118 GetEventParameter( event, kEventParamKeyUnicodes, typeUnicodeText, NULL, dataSize , NULL , charBuf ) ;
119 charBuf[ numChars - 1 ] = 0;
120
121#if SIZEOF_WCHAR_T == 2
122 uniChar = charBuf[0] ;
123#else
124 wxMBConvUTF16 converter ;
125 converter.MB2WC( uniChar , (const char*)charBuf , 2 ) ;
126#endif
127
128 if ( numChars * 2 > 4 )
129 delete[] charBuf ;
130 }
131#endif
132
133 GetEventParameter( event, kEventParamKeyMacCharCodes, typeChar, NULL, sizeof(char), NULL, &charCode );
134 GetEventParameter( event, kEventParamKeyCode, typeUInt32, NULL, sizeof(UInt32), NULL, &keyCode );
135 GetEventParameter( event, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(UInt32), NULL, &modifiers );
136 GetEventParameter( event, kEventParamMouseLocation, typeQDPoint, NULL, sizeof(Point), NULL, &point );
137
138 UInt32 message = (keyCode << 8) + charCode;
139 switch ( GetEventKind( event ) )
140 {
141 case kEventRawKeyRepeat :
142 case kEventRawKeyDown :
143 {
144 WXEVENTREF formerEvent = wxTheApp->MacGetCurrentEvent() ;
145 WXEVENTHANDLERCALLREF formerHandler = wxTheApp->MacGetCurrentEventHandlerCallRef() ;
146 wxTheApp->MacSetCurrentEvent( event , handler ) ;
147 if ( /* focus && */ wxTheApp->MacSendKeyDownEvent(
148 focus , message , modifiers , when , point.h , point.v , uniChar[0] ) )
149 {
150 result = noErr ;
151 }
152 wxTheApp->MacSetCurrentEvent( formerEvent , formerHandler ) ;
153 }
154 break ;
155
156 case kEventRawKeyUp :
157 if ( /* focus && */ wxTheApp->MacSendKeyUpEvent(
158 focus , message , modifiers , when , point.h , point.v , uniChar[0] ) )
159 {
160 result = noErr ;
161 }
162 break ;
163
164 case kEventRawKeyModifiersChanged :
165 {
166 wxKeyEvent event(wxEVT_KEY_DOWN);
167
168 event.m_shiftDown = modifiers & shiftKey;
169 event.m_controlDown = modifiers & controlKey;
170 event.m_altDown = modifiers & optionKey;
171 event.m_metaDown = modifiers & cmdKey;
172 event.m_x = point.h;
173 event.m_y = point.v;
174
175#if wxUSE_UNICODE
176 event.m_uniChar = uniChar[0] ;
177#endif
178
179 event.SetTimestamp(when);
180 event.SetEventObject(focus);
181
182 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & controlKey )
183 {
184 event.m_keyCode = WXK_CONTROL ;
185 event.SetEventType( ( modifiers & controlKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
186 focus->GetEventHandler()->ProcessEvent( event ) ;
187 }
188 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & shiftKey )
189 {
190 event.m_keyCode = WXK_SHIFT ;
191 event.SetEventType( ( modifiers & shiftKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
192 focus->GetEventHandler()->ProcessEvent( event ) ;
193 }
194 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & optionKey )
195 {
196 event.m_keyCode = WXK_ALT ;
197 event.SetEventType( ( modifiers & optionKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
198 focus->GetEventHandler()->ProcessEvent( event ) ;
199 }
200 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & cmdKey )
201 {
202 event.m_keyCode = WXK_COMMAND ;
203 event.SetEventType( ( modifiers & cmdKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
204 focus->GetEventHandler()->ProcessEvent( event ) ;
205 }
206
207 wxApp::s_lastModifiers = modifiers ;
208 }
209 break ;
210
211 default:
212 break;
213 }
214
215 return result ;
216}
217
448f8f12
KO
218static pascal OSStatus wxWebKitCtrlEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
219{
220 OSStatus result = eventNotHandledErr ;
221
222 wxMacCarbonEvent cEvent( event ) ;
223
224 ControlRef controlRef ;
225 wxWebKitCtrl* thisWindow = (wxWebKitCtrl*) data ;
d0d7f207 226 wxNonOwnedWindow* tlw = NULL;
448f8f12
KO
227 if (thisWindow)
228 tlw = thisWindow->MacGetTopLevelWindow();
229
230 cEvent.GetParameter( kEventParamDirectObject , &controlRef ) ;
8a088306 231
448f8f12 232 wxWindow* currentMouseWindow = thisWindow ;
8a088306 233
448f8f12
KO
234 if ( wxApp::s_captureWindow )
235 currentMouseWindow = wxApp::s_captureWindow;
236
c70aaa09 237 switch ( GetEventClass( event ) )
448f8f12 238 {
c70aaa09
KO
239 case kEventClassKeyboard:
240 {
241 result = wxWebKitKeyEventHandler(handler, event, data);
242 break;
243 }
8a088306 244
c70aaa09
KO
245 case kEventClassTextInput:
246 {
247 result = wxMacUnicodeTextEventHandler(handler, event, data);
248 break;
249 }
8a088306 250
c70aaa09
KO
251 case kEventClassMouse:
252 {
253 switch ( GetEventKind( event ) )
448f8f12 254 {
c70aaa09
KO
255 case kEventMouseDragged :
256 case kEventMouseMoved :
257 case kEventMouseDown :
258 case kEventMouseUp :
448f8f12 259 {
c70aaa09
KO
260 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
261 SetupMouseEvent( wxevent , cEvent ) ;
8a088306 262
c70aaa09
KO
263 currentMouseWindow->ScreenToClient( &wxevent.m_x , &wxevent.m_y ) ;
264 wxevent.SetEventObject( currentMouseWindow ) ;
265 wxevent.SetId( currentMouseWindow->GetId() ) ;
8a088306 266
c70aaa09
KO
267 if ( currentMouseWindow->GetEventHandler()->ProcessEvent(wxevent) )
268 {
269 result = noErr;
270 }
271
272 break; // this should enable WebKit to fire mouse dragged and mouse up events...
273 }
274 default :
275 break ;
448f8f12 276 }
c70aaa09
KO
277 }
278 default:
279 break;
448f8f12
KO
280 }
281
282 result = CallNextEventHandler(handler, event);
283 return result ;
284}
285
286DEFINE_ONE_SHOT_HANDLER_GETTER( wxWebKitCtrlEventHandler )
287
14ca15f1 288#endif
448f8f12 289
2c990ba6
KO
290// ----------------------------------------------------------------------------
291// wxWebKit Events
292// ----------------------------------------------------------------------------
293
294IMPLEMENT_DYNAMIC_CLASS( wxWebKitStateChangedEvent, wxCommandEvent )
295
9b11752c 296wxDEFINE_EVENT( wxEVT_WEBKIT_STATE_CHANGED, wxWebKitStateChangedEvent );
2c990ba6
KO
297
298wxWebKitStateChangedEvent::wxWebKitStateChangedEvent( wxWindow* win )
299{
300 SetEventType( wxEVT_WEBKIT_STATE_CHANGED);
d2fa18d3
SC
301 if ( win )
302 {
303 SetEventObject( win );
304 SetId(win->GetId());
305 }
2c990ba6
KO
306}
307
448f8f12
KO
308IMPLEMENT_DYNAMIC_CLASS( wxWebKitBeforeLoadEvent, wxCommandEvent )
309
9b11752c 310wxDEFINE_EVENT( wxEVT_WEBKIT_BEFORE_LOAD, wxWebKitBeforeLoadEvent );
448f8f12
KO
311
312wxWebKitBeforeLoadEvent::wxWebKitBeforeLoadEvent( wxWindow* win )
313{
314 m_cancelled = false;
315 SetEventType( wxEVT_WEBKIT_BEFORE_LOAD);
d2fa18d3
SC
316 if ( win )
317 {
318 SetEventObject( win );
319 SetId(win->GetId());
320 }
448f8f12
KO
321}
322
fa34bc53
RD
323
324IMPLEMENT_DYNAMIC_CLASS( wxWebKitNewWindowEvent, wxCommandEvent )
325
9b11752c 326wxDEFINE_EVENT( wxEVT_WEBKIT_NEW_WINDOW, wxWebKitNewWindowEvent );
fa34bc53
RD
327
328wxWebKitNewWindowEvent::wxWebKitNewWindowEvent( wxWindow* win )
329{
330 SetEventType( wxEVT_WEBKIT_NEW_WINDOW);
d2fa18d3
SC
331 if ( win )
332 {
333 SetEventObject( win );
334 SetId(win->GetId());
335 }
fa34bc53
RD
336}
337
338
339
2c990ba6
KO
340//---------------------------------------------------------
341// helper functions for NSString<->wxString conversion
342//---------------------------------------------------------
343
344inline wxString wxStringWithNSString(NSString *nsstring)
345{
346#if wxUSE_UNICODE
347 return wxString([nsstring UTF8String], wxConvUTF8);
348#else
349 return wxString([nsstring lossyCString]);
350#endif // wxUSE_UNICODE
351}
352
353inline NSString* wxNSStringWithWxString(const wxString &wxstring)
354{
355#if wxUSE_UNICODE
356 return [NSString stringWithUTF8String: wxstring.mb_str(wxConvUTF8)];
357#else
358 return [NSString stringWithCString: wxstring.c_str() length:wxstring.Len()];
359#endif // wxUSE_UNICODE
360}
361
448f8f12
KO
362inline int wxNavTypeFromWebNavType(int type){
363 if (type == WebNavigationTypeLinkClicked)
364 return wxWEBKIT_NAV_LINK_CLICKED;
8a088306 365
448f8f12
KO
366 if (type == WebNavigationTypeFormSubmitted)
367 return wxWEBKIT_NAV_FORM_SUBMITTED;
8a088306 368
448f8f12
KO
369 if (type == WebNavigationTypeBackForward)
370 return wxWEBKIT_NAV_BACK_NEXT;
8a088306 371
448f8f12
KO
372 if (type == WebNavigationTypeReload)
373 return wxWEBKIT_NAV_RELOAD;
8a088306 374
448f8f12
KO
375 if (type == WebNavigationTypeFormResubmitted)
376 return wxWEBKIT_NAV_FORM_RESUBMITTED;
8a088306 377
448f8f12
KO
378 return wxWEBKIT_NAV_OTHER;
379}
380
2c990ba6
KO
381@interface MyFrameLoadMonitor : NSObject
382{
448f8f12
KO
383 wxWebKitCtrl* webKitWindow;
384}
385
386- initWithWxWindow: (wxWebKitCtrl*)inWindow;
387
388@end
389
390@interface MyPolicyDelegate : NSObject
391{
392 wxWebKitCtrl* webKitWindow;
2c990ba6
KO
393}
394
448f8f12 395- initWithWxWindow: (wxWebKitCtrl*)inWindow;
2c990ba6
KO
396
397@end
398
399// ----------------------------------------------------------------------------
400// creation/destruction
401// ----------------------------------------------------------------------------
402
403bool wxWebKitCtrl::Create(wxWindow *parent,
3ca7ba74 404 wxWindowID winID,
2c990ba6
KO
405 const wxString& strURL,
406 const wxPoint& pos,
3ca7ba74
RD
407 const wxSize& size, long style,
408 const wxValidator& validator,
409 const wxString& name)
2c990ba6
KO
410{
411
412 m_currentURL = strURL;
14f21d6d 413 //m_pageTitle = _("Untitled Page");
9548f380 414
2c990ba6
KO
415 //still needed for wxCocoa??
416/*
417 int width, height;
3ca7ba74 418 wxSize sizeInstance;
9548f380 419 if (size.x == wxDefaultCoord || size.y == wxDefaultCoord)
3ca7ba74
RD
420 {
421 m_parent->GetClientSize(&width, &height);
422 sizeInstance.x = width;
423 sizeInstance.y = height;
424 }
425 else
426 {
427 sizeInstance.x = size.x;
428 sizeInstance.y = size.y;
429 }
9548f380 430*/
3ca7ba74 431 // now create and attach WebKit view...
2c990ba6
KO
432#ifdef __WXCOCOA__
433 wxControl::Create(parent, m_windowID, pos, sizeInstance, style , validator , name);
3ca7ba74 434 SetSize(pos.x, pos.y, sizeInstance.x, sizeInstance.y);
9548f380 435
2c990ba6
KO
436 wxTopLevelWindowCocoa *topWin = wxDynamicCast(this, wxTopLevelWindowCocoa);
437 NSWindow* nsWin = topWin->GetNSWindow();
3ca7ba74 438 NSRect rect;
2c990ba6
KO
439 rect.origin.x = pos.x;
440 rect.origin.y = pos.y;
441 rect.size.width = sizeInstance.x;
442 rect.size.height = sizeInstance.y;
443 m_webView = (WebView*)[[WebView alloc] initWithFrame:rect frameName:@"webkitFrame" groupName:@"webkitGroup"];
444 SetNSView(m_webView);
445 [m_cocoaNSView release];
446
447 if(m_parent) m_parent->CocoaAddChild(this);
448 SetInitialFrameRect(pos,sizeInstance);
449#else
450 m_macIsUserPane = false;
daef4689 451 wxControl::Create(parent, winID, pos, size, style , validator , name);
14ca15f1 452#if wxOSX_USE_CARBON
637013eb 453 m_peer = new wxMacControl(this);
2c990ba6 454 WebInitForCarbon();
f8405d6e 455 HIWebViewCreate( m_peer->GetControlRefAddr() );
9548f380 456
f8405d6e 457 m_webView = (WebView*) HIWebViewGetWebView( m_peer->GetControlRef() );
8a088306 458
b384c0fb 459#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
daef4689 460 if ( UMAGetSystemVersion() >= 0x1030 )
448f8f12 461 HIViewChangeFeatures( m_peer->GetControlRef() , kHIViewIsOpaque , 0 ) ;
b384c0fb 462#endif
448f8f12
KO
463 InstallControlEventHandler( m_peer->GetControlRef() , GetwxWebKitCtrlEventHandlerUPP(),
464 GetEventTypeCount(eventList), eventList, this,
465 (EventHandlerRef *)&m_webKitCtrlEventHandler);
14ca15f1
SC
466#else
467 NSRect r = wxOSXGetFrameForControl( this, pos , size ) ;
468 m_webView = [[WebView alloc] initWithFrame:r frameName:@"webkitFrame" groupName:@"webkitGroup"];
469
470 m_peer = new wxWidgetCocoaImpl( this, m_webView );
471#endif
472 MacPostControlCreate(pos, size);
473#if wxOSX_USE_CARBON
474 HIViewSetVisible( m_peer->GetControlRef(), true );
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
KO
684 NSString* resultAsString;
685 wxString resultAsWxString = wxEmptyString;
686 NSString* className = NSStringFromClass([result class]);
687 if ([className isEqualToString:@"NSCFNumber"])
688 resultAsString = [NSString stringWithFormat:@"%@", result];
689 else if ([className isEqualToString:@"NSCFString"])
690 resultAsString = result;
691 else if ([className isEqualToString:@"NSCFBoolean"]){
692 if ([result boolValue])
693 resultAsString = @"true";
694 else
695 resultAsString = @"false";
696 }
697 else if ([className isEqualToString:@"WebScriptObject"])
698 resultAsString = [result stringRepresentation];
699 else
700 fprintf(stderr, "wxWebKitCtrl::RunScript - Unexpected return type: %s!\n", [className UTF8String]);
701
702 resultAsWxString = wxStringWithNSString( resultAsString );
703 return resultAsWxString;
2c990ba6
KO
704}
705
005198fa 706void wxWebKitCtrl::OnSize(wxSizeEvent &event){
14ca15f1 707#if defined(__WXMAC_) && wxOSX_USE_CARBON
39104bc1
KO
708 // This is a nasty hack because WebKit seems to lose its position when it is embedded
709 // in a control that is not itself the content view for a TLW.
daef4689
VZ
710 // I put it in OnSize because these calcs are not perfect, and in fact are basically
711 // guesses based on reverse engineering, so it's best to give people the option of
712 // overriding OnSize with their own calcs if need be.
713 // I also left some test debugging print statements as a convenience if a(nother)
714 // problem crops up.
715
daef4689
VZ
716 wxWindow* tlw = MacGetTopLevelWindow();
717
f94a7798
SC
718 NSRect frame = [(WebView*)m_webView frame];
719 NSRect bounds = [(WebView*)m_webView bounds];
8a088306 720
448f8f12
KO
721#if DEBUG_WEBKIT_SIZING
722 fprintf(stderr,"Carbon window x=%d, y=%d, width=%d, height=%d\n", GetPosition().x, GetPosition().y, GetSize().x, GetSize().y);
723 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);
724 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);
725#endif
726
daef4689
VZ
727 // This must be the case that Apple tested with, because well, in this one case
728 // we don't need to do anything! It just works. ;)
448f8f12 729 if (GetParent() == tlw){
daef4689
VZ
730 return;
731 }
732
c70aaa09 733 // since we no longer use parent coordinates, we always want 0,0.
8a088306 734 int x = 0;
c70aaa09 735 int y = 0;
8a088306 736
448f8f12
KO
737 HIRect rect;
738 rect.origin.x = x;
739 rect.origin.y = y;
daef4689 740
448f8f12
KO
741#if DEBUG_WEBKIT_SIZING
742 printf("Before conversion, origin is: x = %d, y = %d\n", x, y);
743#endif
daef4689 744
448f8f12
KO
745 // NB: In most cases, when calling HIViewConvertRect, what people want is to use GetRootControl(),
746 // and this tripped me up at first. But in fact, what we want is the root view, because we need to
8a088306
VZ
747 // make the y origin relative to the very top of the window, not its contents, since we later flip
748 // the y coordinate for Cocoa.
749 HIViewConvertRect (&rect, m_peer->GetControlRef(),
448f8f12 750 HIViewGetRoot( (WindowRef) MacGetTopLevelWindowRef() ) );
daef4689 751
448f8f12
KO
752 x = (int)rect.origin.x;
753 y = (int)rect.origin.y;
daef4689 754
448f8f12
KO
755#if DEBUG_WEBKIT_SIZING
756 printf("Moving Cocoa frame origin to: x = %d, y = %d\n", x, y);
757#endif
daef4689
VZ
758
759 if (tlw){
760 //flip the y coordinate to convert to Cocoa coordinates
daef4689
VZ
761 y = tlw->GetSize().y - ((GetSize().y) + y);
762 }
763
448f8f12
KO
764#if DEBUG_WEBKIT_SIZING
765 printf("y = %d after flipping value\n", y);
766#endif
9548f380 767
448f8f12
KO
768 frame.origin.x = x;
769 frame.origin.y = y;
f94a7798 770 [(WebView*)m_webView setFrame:frame];
8a088306 771
fe3fc02e 772 if (IsShown())
14753ffe 773 [(WebView*)m_webView display];
005198fa 774 event.Skip();
14ca15f1 775#endif
005198fa 776}
2c990ba6 777
14f21d6d 778void wxWebKitCtrl::MacVisibilityChanged(){
14ca15f1 779#if defined(__WXMAC__) && wxOSX_USE_CARBON
f8405d6e 780 bool isHidden = !IsControlVisible( m_peer->GetControlRef());
fe3fc02e 781 if (!isHidden)
14753ffe 782 [(WebView*)m_webView display];
9548f380 783
14f21d6d 784 [m_webView setHidden:isHidden];
14ca15f1 785#endif
14f21d6d
KO
786}
787
2c990ba6
KO
788//------------------------------------------------------------
789// Listener interfaces
790//------------------------------------------------------------
791
c70aaa09
KO
792// NB: I'm still tracking this down, but it appears the Cocoa window
793// still has these events fired on it while the Carbon control is being
794// destroyed. Therefore, we must be careful to check both the existence
795// of the Carbon control and the event handler before firing events.
796
2c990ba6
KO
797@implementation MyFrameLoadMonitor
798
448f8f12 799- initWithWxWindow: (wxWebKitCtrl*)inWindow
2c990ba6
KO
800{
801 [super init];
3ca7ba74 802 webKitWindow = inWindow; // non retained
2c990ba6
KO
803 return self;
804}
805
806- (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame
807{
c70aaa09 808 if (webKitWindow && frame == [sender mainFrame]){
2c990ba6
KO
809 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
810 wxWebKitStateChangedEvent thisEvent(webKitWindow);
811 thisEvent.SetState(wxWEBKIT_STATE_NEGOTIATING);
812 thisEvent.SetURL( wxStringWithNSString( url ) );
c70aaa09
KO
813 if (webKitWindow->GetEventHandler())
814 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
2c990ba6
KO
815 }
816}
817
818- (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame
819{
c70aaa09 820 if (webKitWindow && frame == [sender mainFrame]){
fba8e95e 821 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
2c990ba6
KO
822 wxWebKitStateChangedEvent thisEvent(webKitWindow);
823 thisEvent.SetState(wxWEBKIT_STATE_TRANSFERRING);
824 thisEvent.SetURL( wxStringWithNSString( url ) );
c70aaa09
KO
825 if (webKitWindow->GetEventHandler())
826 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
2c990ba6
KO
827 }
828}
829
830- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
831{
c70aaa09 832 if (webKitWindow && frame == [sender mainFrame]){
fba8e95e 833 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
2c990ba6
KO
834 wxWebKitStateChangedEvent thisEvent(webKitWindow);
835 thisEvent.SetState(wxWEBKIT_STATE_STOP);
836 thisEvent.SetURL( wxStringWithNSString( url ) );
c70aaa09
KO
837 if (webKitWindow->GetEventHandler())
838 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
2c990ba6
KO
839 }
840}
841
842- (void)webView:(WebView *)sender didFailLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
843{
8a088306
VZ
844 wxUnusedVar(error);
845
c70aaa09 846 if (webKitWindow && frame == [sender mainFrame]){
fba8e95e 847 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
2c990ba6
KO
848 wxWebKitStateChangedEvent thisEvent(webKitWindow);
849 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
850 thisEvent.SetURL( wxStringWithNSString( url ) );
c70aaa09
KO
851 if (webKitWindow->GetEventHandler())
852 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
2c990ba6
KO
853 }
854}
855
856- (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
857{
8a088306
VZ
858 wxUnusedVar(error);
859
c70aaa09 860 if (webKitWindow && frame == [sender mainFrame]){
2c990ba6
KO
861 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
862 wxWebKitStateChangedEvent thisEvent(webKitWindow);
863 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
864 thisEvent.SetURL( wxStringWithNSString( url ) );
c70aaa09
KO
865 if (webKitWindow->GetEventHandler())
866 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
2c990ba6
KO
867 }
868}
869
870- (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
871{
c70aaa09 872 if (webKitWindow && frame == [sender mainFrame]){
448f8f12 873 webKitWindow->SetPageTitle(wxStringWithNSString( title ));
2c990ba6
KO
874 }
875}
3ca7ba74 876@end
5b8f917c 877
448f8f12
KO
878@implementation MyPolicyDelegate
879
880- initWithWxWindow: (wxWebKitCtrl*)inWindow
881{
882 [super init];
883 webKitWindow = inWindow; // non retained
884 return self;
885}
886
887- (void)webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener
888{
8a088306
VZ
889 wxUnusedVar(sender);
890 wxUnusedVar(frame);
891
448f8f12
KO
892 wxWebKitBeforeLoadEvent thisEvent(webKitWindow);
893
8a088306
VZ
894 // Get the navigation type.
895 NSNumber *n = [actionInformation objectForKey:WebActionNavigationTypeKey];
448f8f12
KO
896 int actionType = [n intValue];
897 thisEvent.SetNavigationType( wxNavTypeFromWebNavType(actionType) );
8a088306 898
448f8f12
KO
899 NSString *url = [[request URL] absoluteString];
900 thisEvent.SetURL( wxStringWithNSString( url ) );
8a088306 901
c70aaa09
KO
902 if (webKitWindow && webKitWindow->GetEventHandler())
903 webKitWindow->GetEventHandler()->ProcessEvent(thisEvent);
448f8f12 904
8a088306 905 if (thisEvent.IsCancelled())
448f8f12
KO
906 [listener ignore];
907 else
908 [listener use];
909}
910
fa34bc53
RD
911- (void)webView:(WebView *)sender decidePolicyForNewWindowAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request newFrameName:(NSString *)frameName decisionListener:(id < WebPolicyDecisionListener >)listener
912{
14ca15f1
SC
913 wxUnusedVar(sender);
914 wxUnusedVar(actionInformation);
fa34bc53
RD
915 wxWebKitNewWindowEvent thisEvent(webKitWindow);
916
917 NSString *url = [[request URL] absoluteString];
918 thisEvent.SetURL( wxStringWithNSString( url ) );
919 thisEvent.SetTargetName( wxStringWithNSString( frameName ) );
920
921 if (webKitWindow && webKitWindow->GetEventHandler())
922 webKitWindow->GetEventHandler()->ProcessEvent(thisEvent);
923
924 [listener use];
925}
448f8f12
KO
926@end
927
948f6c6e 928#endif //wxUSE_WEBKIT