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