]> git.saurik.com Git - wxWidgets.git/blame - src/osx/webview_webkit.mm
fixing reentrancy problem under iOS 5
[wxWidgets.git] / src / osx / webview_webkit.mm
CommitLineData
61b98a2d 1/////////////////////////////////////////////////////////////////////////////
8290e3cd 2// Name: src/osx/webview_webkit.mm
0a6c8516 3// Purpose: wxWebViewWebKit - embeddable web kit control,
61b98a2d
SL
4// OS X implementation of web view component
5// Author: Jethro Grassie / Kevin Ollivier / Marianne Gagnon
6// Modified by:
7// Created: 2004-4-16
8// RCS-ID: $Id$
9// Copyright: (c) Jethro Grassie / Kevin Ollivier / Marianne Gagnon
10// Licence: wxWindows licence
11/////////////////////////////////////////////////////////////////////////////
12
13// http://developer.apple.com/mac/library/documentation/Cocoa/Reference/WebKit/Classes/WebView_Class/Reference/Reference.html
14
8290e3cd 15#include "wx/osx/webview_webkit.h"
61b98a2d 16
9d2f31db
SL
17#if wxUSE_WEBVIEW && wxUSE_WEBVIEW_WEBKIT && (defined(__WXOSX_COCOA__) \
18 || defined(__WXOSX_CARBON__))
fdc255cf 19
61b98a2d
SL
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifndef WX_PRECOMP
24 #include "wx/wx.h"
25#endif
26
61b98a2d 27#include "wx/osx/private.h"
fc351912 28#include "wx/cocoa/string.h"
5e3e62bc
SL
29#include "wx/hashmap.h"
30#include "wx/filesys.h"
61b98a2d
SL
31
32#include <WebKit/WebKit.h>
33#include <WebKit/HIWebView.h>
34#include <WebKit/CarbonUtils.h>
61b98a2d
SL
35
36#include <Foundation/NSURLError.h>
37
61b98a2d
SL
38#define DEBUG_WEBKIT_SIZING 0
39
40// ----------------------------------------------------------------------------
41// macros
42// ----------------------------------------------------------------------------
43
fdc255cf 44wxIMPLEMENT_DYNAMIC_CLASS(wxWebViewWebKit, wxWebView);
61b98a2d 45
0a6c8516 46BEGIN_EVENT_TABLE(wxWebViewWebKit, wxControl)
61b98a2d 47#if defined(__WXMAC__) && wxOSX_USE_CARBON
0a6c8516 48 EVT_SIZE(wxWebViewWebKit::OnSize)
61b98a2d
SL
49#endif
50END_EVENT_TABLE()
51
52#if defined(__WXOSX__) && wxOSX_USE_CARBON
53
54// ----------------------------------------------------------------------------
55// Carbon Events handlers
56// ----------------------------------------------------------------------------
57
58// prototype for function in src/osx/carbon/nonownedwnd.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 },
68
69 { kEventClassKeyboard, kEventRawKeyDown } ,
70 { kEventClassKeyboard, kEventRawKeyRepeat } ,
71 { kEventClassKeyboard, kEventRawKeyUp } ,
72 { kEventClassKeyboard, kEventRawKeyModifiersChanged } ,
73
74 { kEventClassTextInput, kEventTextInputUnicodeForKeyEvent } ,
75 { kEventClassTextInput, kEventTextInputUpdateActiveInputArea } ,
76
77#if DEBUG_WEBKIT_SIZING == 1
78 { kEventClassControl, kEventControlBoundsChanged } ,
79#endif
80};
81
82// mix this in from window.cpp
83pascal OSStatus wxMacUnicodeTextEventHandler(EventHandlerCallRef handler,
84 EventRef event, void *data) ;
85
86// NOTE: This is mostly taken from KeyboardEventHandler in toplevel.cpp, but
87// that expects the data pointer is a top-level window, so I needed to change
88// that in this case. However, once 2.8 is out, we should factor out the common
89// logic among the two functions and merge them.
90static pascal OSStatus wxWebKitKeyEventHandler(EventHandlerCallRef handler,
91 EventRef event, void *data)
92{
93 OSStatus result = eventNotHandledErr ;
94 wxMacCarbonEvent cEvent( event ) ;
95
0a6c8516 96 wxWebViewWebKit* thisWindow = (wxWebViewWebKit*) data ;
61b98a2d
SL
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,
112 NULL, 0 , &dataSize, NULL ) == noErr)
113 {
114 UniChar buf[2] ;
115 int numChars = dataSize / sizeof( UniChar) + 1;
116
117 UniChar* charBuf = buf ;
118
119 if ( numChars * 2 > 4 )
120 charBuf = new UniChar[ numChars ] ;
121 GetEventParameter(event, kEventParamKeyUnicodes, typeUnicodeText, NULL,
122 dataSize , NULL , charBuf) ;
123 charBuf[ numChars - 1 ] = 0;
124
125#if SIZEOF_WCHAR_T == 2
126 uniChar = charBuf[0] ;
127#else
128 wxMBConvUTF16 converter ;
129 converter.MB2WC( uniChar , (const char*)charBuf , 2 ) ;
130#endif
131
132 if ( numChars * 2 > 4 )
133 delete[] charBuf ;
134 }
135#endif
136
137 GetEventParameter(event, kEventParamKeyMacCharCodes, typeChar, NULL,
138 sizeof(char), NULL, &charCode );
139 GetEventParameter(event, kEventParamKeyCode, typeUInt32, NULL,
140 sizeof(UInt32), NULL, &keyCode );
141 GetEventParameter(event, kEventParamKeyModifiers, typeUInt32, NULL,
142 sizeof(UInt32), NULL, &modifiers );
143 GetEventParameter(event, kEventParamMouseLocation, typeQDPoint, NULL,
144 sizeof(Point), NULL, &point );
145
146 UInt32 message = (keyCode << 8) + charCode;
147 switch ( GetEventKind( event ) )
148 {
149 case kEventRawKeyRepeat :
150 case kEventRawKeyDown :
151 {
152 WXEVENTREF formerEvent = wxTheApp->MacGetCurrentEvent() ;
153 WXEVENTHANDLERCALLREF formerHandler =
154 wxTheApp->MacGetCurrentEventHandlerCallRef() ;
155
156 wxTheApp->MacSetCurrentEvent( event , handler ) ;
157 if ( /* focus && */ wxTheApp->MacSendKeyDownEvent(
158 focus, message, modifiers, when, point.h, point.v, uniChar[0]))
159 {
160 result = noErr ;
161 }
162 wxTheApp->MacSetCurrentEvent( formerEvent , formerHandler ) ;
163 }
164 break ;
165
166 case kEventRawKeyUp :
167 if ( /* focus && */ wxTheApp->MacSendKeyUpEvent(
168 focus , message , modifiers , when , point.h , point.v , uniChar[0] ) )
169 {
170 result = noErr ;
171 }
172 break ;
173
174 case kEventRawKeyModifiersChanged :
175 {
176 wxKeyEvent event(wxEVT_KEY_DOWN);
177
178 event.m_shiftDown = modifiers & shiftKey;
179 event.m_controlDown = modifiers & controlKey;
180 event.m_altDown = modifiers & optionKey;
181 event.m_metaDown = modifiers & cmdKey;
182 event.m_x = point.h;
183 event.m_y = point.v;
184
185#if wxUSE_UNICODE
186 event.m_uniChar = uniChar[0] ;
187#endif
188
189 event.SetTimestamp(when);
190 event.SetEventObject(focus);
191
192 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & controlKey )
193 {
194 event.m_keyCode = WXK_CONTROL ;
195 event.SetEventType( ( modifiers & controlKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
196 focus->GetEventHandler()->ProcessEvent( event ) ;
197 }
198 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & shiftKey )
199 {
200 event.m_keyCode = WXK_SHIFT ;
201 event.SetEventType( ( modifiers & shiftKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
202 focus->GetEventHandler()->ProcessEvent( event ) ;
203 }
204 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & optionKey )
205 {
206 event.m_keyCode = WXK_ALT ;
207 event.SetEventType( ( modifiers & optionKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
208 focus->GetEventHandler()->ProcessEvent( event ) ;
209 }
210 if ( /* focus && */ (modifiers ^ wxApp::s_lastModifiers ) & cmdKey )
211 {
212 event.m_keyCode = WXK_COMMAND ;
213 event.SetEventType( ( modifiers & cmdKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
214 focus->GetEventHandler()->ProcessEvent( event ) ;
215 }
216
217 wxApp::s_lastModifiers = modifiers ;
218 }
219 break ;
220
221 default:
222 break;
223 }
224
225 return result ;
226}
227
0a6c8516 228static pascal OSStatus wxWebViewWebKitEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
61b98a2d
SL
229{
230 OSStatus result = eventNotHandledErr ;
231
232 wxMacCarbonEvent cEvent( event ) ;
233
234 ControlRef controlRef ;
0a6c8516 235 wxWebViewWebKit* thisWindow = (wxWebViewWebKit*) data ;
61b98a2d
SL
236 wxNonOwnedWindow* tlw = NULL;
237 if (thisWindow)
238 tlw = thisWindow->MacGetTopLevelWindow();
239
240 cEvent.GetParameter( kEventParamDirectObject , &controlRef ) ;
241
242 wxWindow* currentMouseWindow = thisWindow ;
243
244 if ( wxApp::s_captureWindow )
245 currentMouseWindow = wxApp::s_captureWindow;
246
247 switch ( GetEventClass( event ) )
248 {
249 case kEventClassKeyboard:
250 {
251 result = wxWebKitKeyEventHandler(handler, event, data);
252 break;
253 }
254
255 case kEventClassTextInput:
256 {
257 result = wxMacUnicodeTextEventHandler(handler, event, data);
258 break;
259 }
260
261 case kEventClassMouse:
262 {
263 switch ( GetEventKind( event ) )
264 {
265 case kEventMouseDragged :
266 case kEventMouseMoved :
267 case kEventMouseDown :
268 case kEventMouseUp :
269 {
270 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
271 SetupMouseEvent( wxevent , cEvent ) ;
272
273 currentMouseWindow->ScreenToClient( &wxevent.m_x , &wxevent.m_y ) ;
274 wxevent.SetEventObject( currentMouseWindow ) ;
275 wxevent.SetId( currentMouseWindow->GetId() ) ;
276
277 if ( currentMouseWindow->GetEventHandler()->ProcessEvent(wxevent) )
278 {
279 result = noErr;
280 }
281
282 break; // this should enable WebKit to fire mouse dragged and mouse up events...
283 }
284 default :
285 break ;
286 }
287 }
288 default:
289 break;
290 }
291
292 result = CallNextEventHandler(handler, event);
293 return result ;
294}
295
0a6c8516 296DEFINE_ONE_SHOT_HANDLER_GETTER( wxWebViewWebKitEventHandler )
61b98a2d
SL
297
298#endif
299
d2e66707 300@interface WebViewLoadDelegate : NSObject
61b98a2d 301{
0a6c8516 302 wxWebViewWebKit* webKitWindow;
61b98a2d
SL
303}
304
0a6c8516 305- initWithWxWindow: (wxWebViewWebKit*)inWindow;
61b98a2d
SL
306
307@end
308
d2e66707 309@interface WebViewPolicyDelegate : NSObject
61b98a2d 310{
0a6c8516 311 wxWebViewWebKit* webKitWindow;
61b98a2d
SL
312}
313
0a6c8516 314- initWithWxWindow: (wxWebViewWebKit*)inWindow;
61b98a2d
SL
315
316@end
317
7d8d6163
SL
318//We use a hash to map scheme names to wxWebViewHandler
319WX_DECLARE_STRING_HASH_MAP(wxSharedPtr<wxWebViewHandler>, wxStringToWebHandlerMap);
5e3e62bc
SL
320
321static wxStringToWebHandlerMap g_stringHandlerMap;
322
323@interface WebViewCustomProtocol : NSURLProtocol
324{
325}
326@end
327
61b98a2d
SL
328// ----------------------------------------------------------------------------
329// creation/destruction
330// ----------------------------------------------------------------------------
331
0a6c8516 332bool wxWebViewWebKit::Create(wxWindow *parent,
61b98a2d
SL
333 wxWindowID winID,
334 const wxString& strURL,
335 const wxPoint& pos,
336 const wxSize& size, long style,
337 const wxString& name)
338{
339 m_busy = false;
61b98a2d 340
b404a8f3 341 DontCreatePeer();
61b98a2d
SL
342 wxControl::Create(parent, winID, pos, size, style, wxDefaultValidator, name);
343
344#if wxOSX_USE_CARBON
2e140660 345 wxMacControl* peer = new wxMacControl(this);
61b98a2d 346 WebInitForCarbon();
2e140660 347 HIWebViewCreate( peer->GetControlRefAddr() );
61b98a2d 348
2e140660 349 m_webView = (WebView*) HIWebViewGetWebView( peer->GetControlRef() );
61b98a2d
SL
350
351#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
352 if ( UMAGetSystemVersion() >= 0x1030 )
2e140660 353 HIViewChangeFeatures( peer->GetControlRef() , kHIViewIsOpaque , 0 ) ;
61b98a2d 354#endif
2e140660 355 InstallControlEventHandler(peer->GetControlRef(),
0a6c8516 356 GetwxWebViewWebKitEventHandlerUPP(),
61b98a2d
SL
357 GetEventTypeCount(eventList), eventList, this,
358 (EventHandlerRef *)&m_webKitCtrlEventHandler);
2e140660 359 SetPeer(peer);
61b98a2d
SL
360#else
361 NSRect r = wxOSXGetFrameForControl( this, pos , size ) ;
362 m_webView = [[WebView alloc] initWithFrame:r
363 frameName:@"webkitFrame"
364 groupName:@"webkitGroup"];
b404a8f3 365 SetPeer(new wxWidgetCocoaImpl( this, m_webView ));
61b98a2d
SL
366#endif
367
61b98a2d
SL
368 MacPostControlCreate(pos, size);
369
370#if wxOSX_USE_CARBON
2e140660 371 HIViewSetVisible( GetPeer()->GetControlRef(), true );
61b98a2d
SL
372#endif
373 [m_webView setHidden:false];
374
ca897b51 375
61b98a2d
SL
376
377 // Register event listener interfaces
d2e66707 378 WebViewLoadDelegate* loadDelegate =
abd474ea 379 [[WebViewLoadDelegate alloc] initWithWxWindow: this];
61b98a2d 380
d2e66707 381 [m_webView setFrameLoadDelegate:loadDelegate];
61b98a2d
SL
382
383 // this is used to veto page loads, etc.
d2e66707
SL
384 WebViewPolicyDelegate* policyDelegate =
385 [[WebViewPolicyDelegate alloc] initWithWxWindow: this];
61b98a2d 386
d2e66707 387 [m_webView setPolicyDelegate:policyDelegate];
61b98a2d 388
5e3e62bc
SL
389 //Register our own class for custom scheme handling
390 [NSURLProtocol registerClass:[WebViewCustomProtocol class]];
391
4d0dddc7 392 LoadURL(strURL);
61b98a2d
SL
393 return true;
394}
395
0a6c8516 396wxWebViewWebKit::~wxWebViewWebKit()
61b98a2d 397{
d2e66707
SL
398 WebViewLoadDelegate* loadDelegate = [m_webView frameLoadDelegate];
399 WebViewPolicyDelegate* policyDelegate = [m_webView policyDelegate];
61b98a2d
SL
400 [m_webView setFrameLoadDelegate: nil];
401 [m_webView setPolicyDelegate: nil];
402
d2e66707
SL
403 if (loadDelegate)
404 [loadDelegate release];
61b98a2d 405
d2e66707
SL
406 if (policyDelegate)
407 [policyDelegate release];
61b98a2d
SL
408}
409
410// ----------------------------------------------------------------------------
411// public methods
412// ----------------------------------------------------------------------------
413
e669ddde 414bool wxWebViewWebKit::CanGoBack() const
61b98a2d
SL
415{
416 if ( !m_webView )
417 return false;
418
419 return [m_webView canGoBack];
420}
421
e669ddde 422bool wxWebViewWebKit::CanGoForward() const
61b98a2d
SL
423{
424 if ( !m_webView )
425 return false;
426
427 return [m_webView canGoForward];
428}
429
0a6c8516 430void wxWebViewWebKit::GoBack()
61b98a2d
SL
431{
432 if ( !m_webView )
433 return;
434
1680c356 435 [(WebView*)m_webView goBack];
61b98a2d
SL
436}
437
0a6c8516 438void wxWebViewWebKit::GoForward()
61b98a2d
SL
439{
440 if ( !m_webView )
441 return;
442
1680c356 443 [(WebView*)m_webView goForward];
61b98a2d
SL
444}
445
0a6c8516 446void wxWebViewWebKit::Reload(wxWebViewReloadFlags flags)
61b98a2d
SL
447{
448 if ( !m_webView )
449 return;
450
451 if (flags & wxWEB_VIEW_RELOAD_NO_CACHE)
452 {
453 // TODO: test this indeed bypasses the cache
454 [[m_webView preferences] setUsesPageCache:NO];
455 [[m_webView mainFrame] reload];
456 [[m_webView preferences] setUsesPageCache:YES];
457 }
458 else
459 {
460 [[m_webView mainFrame] reload];
461 }
462}
463
0a6c8516 464void wxWebViewWebKit::Stop()
61b98a2d
SL
465{
466 if ( !m_webView )
467 return;
468
469 [[m_webView mainFrame] stopLoading];
470}
471
e669ddde 472bool wxWebViewWebKit::CanGetPageSource() const
61b98a2d
SL
473{
474 if ( !m_webView )
475 return false;
476
477 WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
478 return ( [[dataSource representation] canProvideDocumentSource] );
479}
480
e669ddde 481wxString wxWebViewWebKit::GetPageSource() const
61b98a2d
SL
482{
483
484 if (CanGetPageSource())
485 {
486 WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
487 wxASSERT (dataSource != nil);
488
489 id<WebDocumentRepresentation> representation = [dataSource representation];
490 wxASSERT (representation != nil);
491
492 NSString* source = [representation documentSource];
493 if (source == nil)
494 {
495 return wxEmptyString;
496 }
497
498 return wxStringWithNSString( source );
499 }
500
501 return wxEmptyString;
502}
503
e669ddde 504bool wxWebViewWebKit::CanIncreaseTextSize() const
61b98a2d
SL
505{
506 if ( !m_webView )
507 return false;
508
509 if ([m_webView canMakeTextLarger])
510 return true;
511 else
512 return false;
513}
514
0a6c8516 515void wxWebViewWebKit::IncreaseTextSize()
61b98a2d
SL
516{
517 if ( !m_webView )
518 return;
519
520 if (CanIncreaseTextSize())
521 [m_webView makeTextLarger:(WebView*)m_webView];
522}
523
e669ddde 524bool wxWebViewWebKit::CanDecreaseTextSize() const
61b98a2d
SL
525{
526 if ( !m_webView )
527 return false;
528
529 if ([m_webView canMakeTextSmaller])
530 return true;
531 else
532 return false;
533}
534
0a6c8516 535void wxWebViewWebKit::DecreaseTextSize()
61b98a2d
SL
536{
537 if ( !m_webView )
538 return;
539
540 if (CanDecreaseTextSize())
541 [m_webView makeTextSmaller:(WebView*)m_webView];
542}
543
0a6c8516 544void wxWebViewWebKit::Print()
61b98a2d
SL
545{
546
547 // TODO: allow specifying the "show prompt" parameter in Print() ?
548 bool showPrompt = true;
549
550 if ( !m_webView )
551 return;
552
553 id view = [[[m_webView mainFrame] frameView] documentView];
554 NSPrintOperation *op = [NSPrintOperation printOperationWithView:view
555 printInfo: [NSPrintInfo sharedPrintInfo]];
556 if (showPrompt)
557 {
558 [op setShowsPrintPanel: showPrompt];
559 // in my tests, the progress bar always freezes and it stops the whole
560 // print operation. do not turn this to true unless there is a
561 // workaround for the bug.
562 [op setShowsProgressPanel: false];
563 }
564 // Print it.
565 [op runOperation];
566}
567
c7cbe308 568void wxWebViewWebKit::SetEditable(bool enable)
61b98a2d
SL
569{
570 if ( !m_webView )
571 return;
572
573 [m_webView setEditable:enable ];
574}
575
e669ddde 576bool wxWebViewWebKit::IsEditable() const
61b98a2d
SL
577{
578 if ( !m_webView )
579 return false;
580
581 return [m_webView isEditable];
582}
583
0a6c8516 584void wxWebViewWebKit::SetZoomType(wxWebViewZoomType zoomType)
61b98a2d
SL
585{
586 // there is only one supported zoom type at the moment so this setter
587 // does nothing beyond checking sanity
588 wxASSERT(zoomType == wxWEB_VIEW_ZOOM_TYPE_TEXT);
589}
590
0a6c8516 591wxWebViewZoomType wxWebViewWebKit::GetZoomType() const
61b98a2d
SL
592{
593 // for now that's the only one that is supported
594 // FIXME: does the default zoom type change depending on webkit versions? :S
595 // Then this will be wrong
596 return wxWEB_VIEW_ZOOM_TYPE_TEXT;
597}
598
0a6c8516 599bool wxWebViewWebKit::CanSetZoomType(wxWebViewZoomType type) const
61b98a2d
SL
600{
601 switch (type)
602 {
603 // for now that's the only one that is supported
604 // TODO: I know recent versions of webkit support layout zoom too,
605 // check if we can support it
606 case wxWEB_VIEW_ZOOM_TYPE_TEXT:
607 return true;
608
609 default:
610 return false;
611 }
612}
613
0a6c8516 614int wxWebViewWebKit::GetScrollPos()
61b98a2d
SL
615{
616 id result = [[m_webView windowScriptObject]
617 evaluateWebScript:@"document.body.scrollTop"];
618 return [result intValue];
619}
620
0a6c8516 621void wxWebViewWebKit::SetScrollPos(int pos)
61b98a2d
SL
622{
623 if ( !m_webView )
624 return;
625
626 wxString javascript;
627 javascript.Printf(wxT("document.body.scrollTop = %d;"), pos);
628 [[m_webView windowScriptObject] evaluateWebScript:
629 (NSString*)wxNSStringWithWxString( javascript )];
630}
631
e669ddde 632wxString wxWebViewWebKit::GetSelectedText() const
61b98a2d
SL
633{
634 NSString* selection = [[m_webView selectedDOMRange] markupString];
635 if (!selection) return wxEmptyString;
636
637 return wxStringWithNSString(selection);
638}
639
c9ccc09c 640void wxWebViewWebKit::RunScript(const wxString& javascript)
61b98a2d
SL
641{
642 if ( !m_webView )
b132a83b 643 return;
61b98a2d 644
c9ccc09c 645 [[m_webView windowScriptObject] evaluateWebScript:
61b98a2d 646 (NSString*)wxNSStringWithWxString( javascript )];
61b98a2d
SL
647}
648
0a6c8516 649void wxWebViewWebKit::OnSize(wxSizeEvent &event)
61b98a2d
SL
650{
651#if defined(__WXMAC_) && wxOSX_USE_CARBON
652 // This is a nasty hack because WebKit seems to lose its position when it is
653 // embedded in a control that is not itself the content view for a TLW.
654 // I put it in OnSize because these calcs are not perfect, and in fact are
655 // basically guesses based on reverse engineering, so it's best to give
656 // people the option of overriding OnSize with their own calcs if need be.
657 // I also left some test debugging print statements as a convenience if
658 // a(nother) problem crops up.
659
660 wxWindow* tlw = MacGetTopLevelWindow();
661
662 NSRect frame = [(WebView*)m_webView frame];
663 NSRect bounds = [(WebView*)m_webView bounds];
664
665#if DEBUG_WEBKIT_SIZING
666 fprintf(stderr,"Carbon window x=%d, y=%d, width=%d, height=%d\n",
667 GetPosition().x, GetPosition().y, GetSize().x, GetSize().y);
668 fprintf(stderr, "Cocoa window frame x=%G, y=%G, width=%G, height=%G\n",
669 frame.origin.x, frame.origin.y,
670 frame.size.width, frame.size.height);
671 fprintf(stderr, "Cocoa window bounds x=%G, y=%G, width=%G, height=%G\n",
672 bounds.origin.x, bounds.origin.y,
673 bounds.size.width, bounds.size.height);
674#endif
675
676 // This must be the case that Apple tested with, because well, in this one case
677 // we don't need to do anything! It just works. ;)
678 if (GetParent() == tlw) return;
679
680 // since we no longer use parent coordinates, we always want 0,0.
681 int x = 0;
682 int y = 0;
683
684 HIRect rect;
685 rect.origin.x = x;
686 rect.origin.y = y;
687
688#if DEBUG_WEBKIT_SIZING
689 printf("Before conversion, origin is: x = %d, y = %d\n", x, y);
690#endif
691
692 // NB: In most cases, when calling HIViewConvertRect, what people want is to
693 // use GetRootControl(), and this tripped me up at first. But in fact, what
694 // we want is the root view, because we need to make the y origin relative
695 // to the very top of the window, not its contents, since we later flip
696 // the y coordinate for Cocoa.
697 HIViewConvertRect (&rect, m_peer->GetControlRef(),
698 HIViewGetRoot(
699 (WindowRef) MacGetTopLevelWindowRef()
700 ));
701
702 x = (int)rect.origin.x;
703 y = (int)rect.origin.y;
704
705#if DEBUG_WEBKIT_SIZING
706 printf("Moving Cocoa frame origin to: x = %d, y = %d\n", x, y);
707#endif
708
709 if (tlw){
710 //flip the y coordinate to convert to Cocoa coordinates
711 y = tlw->GetSize().y - ((GetSize().y) + y);
712 }
713
714#if DEBUG_WEBKIT_SIZING
715 printf("y = %d after flipping value\n", y);
716#endif
717
718 frame.origin.x = x;
719 frame.origin.y = y;
720 [(WebView*)m_webView setFrame:frame];
721
722 if (IsShown())
723 [(WebView*)m_webView display];
724 event.Skip();
725#endif
726}
727
0a6c8516 728void wxWebViewWebKit::MacVisibilityChanged(){
61b98a2d 729#if defined(__WXMAC__) && wxOSX_USE_CARBON
2e140660 730 bool isHidden = !IsControlVisible( GetPeer()->GetControlRef());
61b98a2d
SL
731 if (!isHidden)
732 [(WebView*)m_webView display];
733
734 [m_webView setHidden:isHidden];
735#endif
736}
737
4d0dddc7 738void wxWebViewWebKit::LoadURL(const wxString& url)
61b98a2d 739{
895e112c
SL
740 [[m_webView mainFrame] loadRequest:[NSURLRequest requestWithURL:
741 [NSURL URLWithString:wxNSStringWithWxString(url)]]];
61b98a2d
SL
742}
743
e669ddde 744wxString wxWebViewWebKit::GetCurrentURL() const
61b98a2d
SL
745{
746 return wxStringWithNSString([m_webView mainFrameURL]);
747}
748
e669ddde 749wxString wxWebViewWebKit::GetCurrentTitle() const
61b98a2d 750{
1f0acb43 751 return wxStringWithNSString([m_webView mainFrameTitle]);
61b98a2d
SL
752}
753
e669ddde 754float wxWebViewWebKit::GetWebkitZoom() const
61b98a2d
SL
755{
756 return [m_webView textSizeMultiplier];
757}
758
0a6c8516 759void wxWebViewWebKit::SetWebkitZoom(float zoom)
61b98a2d
SL
760{
761 [m_webView setTextSizeMultiplier:zoom];
762}
763
e669ddde 764wxWebViewZoom wxWebViewWebKit::GetZoom() const
61b98a2d
SL
765{
766 float zoom = GetWebkitZoom();
767
768 // arbitrary way to map float zoom to our common zoom enum
769 if (zoom <= 0.55)
770 {
771 return wxWEB_VIEW_ZOOM_TINY;
772 }
773 else if (zoom > 0.55 && zoom <= 0.85)
774 {
775 return wxWEB_VIEW_ZOOM_SMALL;
776 }
777 else if (zoom > 0.85 && zoom <= 1.15)
778 {
779 return wxWEB_VIEW_ZOOM_MEDIUM;
780 }
781 else if (zoom > 1.15 && zoom <= 1.45)
782 {
783 return wxWEB_VIEW_ZOOM_LARGE;
784 }
785 else if (zoom > 1.45)
786 {
787 return wxWEB_VIEW_ZOOM_LARGEST;
788 }
789
790 // to shut up compilers, this can never be reached logically
791 wxASSERT(false);
792 return wxWEB_VIEW_ZOOM_MEDIUM;
793}
794
0a6c8516 795void wxWebViewWebKit::SetZoom(wxWebViewZoom zoom)
61b98a2d
SL
796{
797 // arbitrary way to map our common zoom enum to float zoom
798 switch (zoom)
799 {
800 case wxWEB_VIEW_ZOOM_TINY:
801 SetWebkitZoom(0.4f);
802 break;
803
804 case wxWEB_VIEW_ZOOM_SMALL:
805 SetWebkitZoom(0.7f);
806 break;
807
808 case wxWEB_VIEW_ZOOM_MEDIUM:
809 SetWebkitZoom(1.0f);
810 break;
811
812 case wxWEB_VIEW_ZOOM_LARGE:
813 SetWebkitZoom(1.3);
814 break;
815
816 case wxWEB_VIEW_ZOOM_LARGEST:
817 SetWebkitZoom(1.6);
818 break;
819
820 default:
821 wxASSERT(false);
822 }
823
824}
825
0a6c8516 826void wxWebViewWebKit::SetPage(const wxString& src, const wxString& baseUrl)
61b98a2d
SL
827{
828 if ( !m_webView )
829 return;
830
831 [[m_webView mainFrame] loadHTMLString:(NSString*)wxNSStringWithWxString(src)
832 baseURL:[NSURL URLWithString:
833 wxNSStringWithWxString( baseUrl )]];
834}
835
028894b4
SL
836void wxWebViewWebKit::Cut()
837{
838 if ( !m_webView )
839 return;
840
b132a83b 841 [(WebView*)m_webView cut:m_webView];
028894b4
SL
842}
843
844void wxWebViewWebKit::Copy()
845{
846 if ( !m_webView )
847 return;
848
b132a83b 849 [(WebView*)m_webView copy:m_webView];
028894b4
SL
850}
851
852void wxWebViewWebKit::Paste()
853{
854 if ( !m_webView )
855 return;
856
b132a83b 857 [(WebView*)m_webView paste:m_webView];
028894b4
SL
858}
859
63a65070
SL
860void wxWebViewWebKit::DeleteSelection()
861{
862 if ( !m_webView )
863 return;
864
865 [(WebView*)m_webView deleteSelection];
866}
867
e669ddde 868bool wxWebViewWebKit::HasSelection() const
7bd30a12
SL
869{
870 DOMRange* range = [m_webView selectedDOMRange];
871 if(!range)
872 {
873 return false;
874 }
875 else
876 {
877 return true;
878 }
879}
880
ef53bf2a
SL
881void wxWebViewWebKit::ClearSelection()
882{
883 //We use javascript as selection isn't exposed at the moment in webkit
884 RunScript("window.getSelection().removeAllRanges();");
885}
886
835165d5
SL
887void wxWebViewWebKit::SelectAll()
888{
889 RunScript("window.getSelection().selectAllChildren(document.body);");
890}
891
e669ddde 892wxString wxWebViewWebKit::GetSelectedSource() const
94ec73c7
SL
893{
894 wxString script = ("var range = window.getSelection().getRangeAt(0);"
895 "var element = document.createElement('div');"
896 "element.appendChild(range.cloneContents());"
897 "return element.innerHTML;");
898 id result = [[m_webView windowScriptObject]
899 evaluateWebScript:wxNSStringWithWxString(script)];
900 return wxStringWithNSString([result stringValue]);
901}
902
e669ddde 903wxString wxWebViewWebKit::GetPageText() const
c6ea1fda
SL
904{
905 id result = [[m_webView windowScriptObject]
906 evaluateWebScript:@"document.body.textContent"];
907 return wxStringWithNSString([result stringValue]);
908}
909
8ba4faba
SL
910void wxWebViewWebKit::EnableHistory(bool enable)
911{
912 if ( !m_webView )
913 return;
7bd30a12 914
8ba4faba
SL
915 [m_webView setMaintainsBackForwardList:enable];
916}
917
7c36630b
SL
918void wxWebViewWebKit::ClearHistory()
919{
6a504e88
SL
920 [m_webView setMaintainsBackForwardList:NO];
921 [m_webView setMaintainsBackForwardList:YES];
7c36630b
SL
922}
923
c13d6ac1 924wxVector<wxSharedPtr<wxWebViewHistoryItem> > wxWebViewWebKit::GetBackwardHistory()
515fd027 925{
c13d6ac1 926 wxVector<wxSharedPtr<wxWebViewHistoryItem> > backhist;
515fd027
SL
927 WebBackForwardList* history = [m_webView backForwardList];
928 int count = [history backListCount];
929 for(int i = -count; i < 0; i++)
930 {
931 WebHistoryItem* item = [history itemAtIndex:i];
932 wxString url = wxStringWithNSString([item URLString]);
933 wxString title = wxStringWithNSString([item title]);
c13d6ac1 934 wxWebViewHistoryItem* wxitem = new wxWebViewHistoryItem(url, title);
515fd027 935 wxitem->m_histItem = item;
c13d6ac1 936 wxSharedPtr<wxWebViewHistoryItem> itemptr(wxitem);
515fd027
SL
937 backhist.push_back(itemptr);
938 }
939 return backhist;
940}
941
c13d6ac1 942wxVector<wxSharedPtr<wxWebViewHistoryItem> > wxWebViewWebKit::GetForwardHistory()
515fd027 943{
c13d6ac1 944 wxVector<wxSharedPtr<wxWebViewHistoryItem> > forwardhist;
515fd027
SL
945 WebBackForwardList* history = [m_webView backForwardList];
946 int count = [history forwardListCount];
947 for(int i = 1; i <= count; i++)
948 {
949 WebHistoryItem* item = [history itemAtIndex:i];
950 wxString url = wxStringWithNSString([item URLString]);
951 wxString title = wxStringWithNSString([item title]);
c13d6ac1 952 wxWebViewHistoryItem* wxitem = new wxWebViewHistoryItem(url, title);
515fd027 953 wxitem->m_histItem = item;
c13d6ac1 954 wxSharedPtr<wxWebViewHistoryItem> itemptr(wxitem);
515fd027
SL
955 forwardhist.push_back(itemptr);
956 }
957 return forwardhist;
958}
959
c13d6ac1 960void wxWebViewWebKit::LoadHistoryItem(wxSharedPtr<wxWebViewHistoryItem> item)
515fd027
SL
961{
962 [m_webView goToBackForwardItem:item->m_histItem];
963}
964
e669ddde 965bool wxWebViewWebKit::CanUndo() const
6368d965
SL
966{
967 return [[m_webView undoManager] canUndo];
968}
969
e669ddde 970bool wxWebViewWebKit::CanRedo() const
6368d965
SL
971{
972 return [[m_webView undoManager] canRedo];
973}
974
975void wxWebViewWebKit::Undo()
976{
977 [[m_webView undoManager] undo];
978}
979
980void wxWebViewWebKit::Redo()
981{
982 [[m_webView undoManager] redo];
983}
984
7d8d6163 985void wxWebViewWebKit::RegisterHandler(wxSharedPtr<wxWebViewHandler> handler)
5e3e62bc
SL
986{
987 g_stringHandlerMap[handler->GetName()] = handler;
988}
989
61b98a2d
SL
990//------------------------------------------------------------
991// Listener interfaces
992//------------------------------------------------------------
993
994// NB: I'm still tracking this down, but it appears the Cocoa window
995// still has these events fired on it while the Carbon control is being
996// destroyed. Therefore, we must be careful to check both the existence
997// of the Carbon control and the event handler before firing events.
998
d2e66707 999@implementation WebViewLoadDelegate
61b98a2d 1000
0a6c8516 1001- initWithWxWindow: (wxWebViewWebKit*)inWindow
61b98a2d
SL
1002{
1003 [super init];
1004 webKitWindow = inWindow; // non retained
1005 return self;
1006}
1007
1008- (void)webView:(WebView *)sender
1009 didStartProvisionalLoadForFrame:(WebFrame *)frame
1010{
c75400ea 1011 webKitWindow->m_busy = true;
61b98a2d
SL
1012}
1013
1014- (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame
1015{
c75400ea 1016 webKitWindow->m_busy = true;
61b98a2d
SL
1017
1018 if (webKitWindow && frame == [sender mainFrame]){
1019 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
1020 wxString target = wxStringWithNSString([frame name]);
3225a4b8
SL
1021 wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_NAVIGATED,
1022 webKitWindow->GetId(),
1023 wxStringWithNSString( url ),
1024 target);
61b98a2d
SL
1025
1026 if (webKitWindow && webKitWindow->GetEventHandler())
3225a4b8 1027 webKitWindow->GetEventHandler()->ProcessEvent(event);
61b98a2d
SL
1028 }
1029}
1030
1031- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
1032{
c75400ea 1033 webKitWindow->m_busy = false;
61b98a2d
SL
1034
1035 if (webKitWindow && frame == [sender mainFrame]){
1036 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
1037
1038 wxString target = wxStringWithNSString([frame name]);
3225a4b8
SL
1039 wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_LOADED,
1040 webKitWindow->GetId(),
1041 wxStringWithNSString( url ),
1042 target);
61b98a2d
SL
1043
1044 if (webKitWindow && webKitWindow->GetEventHandler())
3225a4b8 1045 webKitWindow->GetEventHandler()->ProcessEvent(event);
61b98a2d
SL
1046 }
1047}
1048
04fa04d8 1049wxString nsErrorToWxHtmlError(NSError* error, wxWebViewNavigationError* out)
61b98a2d
SL
1050{
1051 *out = wxWEB_NAV_ERR_OTHER;
1052
1053 if ([[error domain] isEqualToString:NSURLErrorDomain])
1054 {
1055 switch ([error code])
1056 {
1057 case NSURLErrorCannotFindHost:
1058 case NSURLErrorFileDoesNotExist:
1059 case NSURLErrorRedirectToNonExistentLocation:
1060 *out = wxWEB_NAV_ERR_NOT_FOUND;
1061 break;
1062
1063 case NSURLErrorResourceUnavailable:
1064 case NSURLErrorHTTPTooManyRedirects:
392594bb 1065#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
61b98a2d 1066 case NSURLErrorDataLengthExceedsMaximum:
392594bb 1067#endif
61b98a2d
SL
1068 case NSURLErrorBadURL:
1069 case NSURLErrorFileIsDirectory:
1070 *out = wxWEB_NAV_ERR_REQUEST;
1071 break;
1072
1073 case NSURLErrorTimedOut:
1074 case NSURLErrorDNSLookupFailed:
1075 case NSURLErrorNetworkConnectionLost:
1076 case NSURLErrorCannotConnectToHost:
1077 case NSURLErrorNotConnectedToInternet:
1078 //case NSURLErrorInternationalRoamingOff:
1079 //case NSURLErrorCallIsActive:
1080 //case NSURLErrorDataNotAllowed:
1081 *out = wxWEB_NAV_ERR_CONNECTION;
1082 break;
1083
1084 case NSURLErrorCancelled:
1085 case NSURLErrorUserCancelledAuthentication:
1086 *out = wxWEB_NAV_ERR_USER_CANCELLED;
1087 break;
1088
74a8f67d 1089#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
61b98a2d
SL
1090 case NSURLErrorCannotDecodeRawData:
1091 case NSURLErrorCannotDecodeContentData:
61b98a2d 1092 case NSURLErrorCannotParseResponse:
74a8f67d
SL
1093#endif
1094 case NSURLErrorBadServerResponse:
61b98a2d
SL
1095 *out = wxWEB_NAV_ERR_REQUEST;
1096 break;
1097
1098 case NSURLErrorUserAuthenticationRequired:
1099 case NSURLErrorSecureConnectionFailed:
74a8f67d 1100#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
61b98a2d 1101 case NSURLErrorClientCertificateRequired:
74a8f67d 1102#endif
61b98a2d
SL
1103 *out = wxWEB_NAV_ERR_AUTH;
1104 break;
1105
1106 case NSURLErrorNoPermissionsToReadFile:
1107 *out = wxWEB_NAV_ERR_SECURITY;
1108 break;
1109
1110 case NSURLErrorServerCertificateHasBadDate:
1111 case NSURLErrorServerCertificateUntrusted:
1112 case NSURLErrorServerCertificateHasUnknownRoot:
1113 case NSURLErrorServerCertificateNotYetValid:
1114 case NSURLErrorClientCertificateRejected:
1115 *out = wxWEB_NAV_ERR_CERTIFICATE;
1116 break;
1117 }
1118 }
1119
1120 wxString message = wxStringWithNSString([error localizedDescription]);
1121 NSString* detail = [error localizedFailureReason];
1122 if (detail != NULL)
1123 {
1124 message = message + " (" + wxStringWithNSString(detail) + ")";
1125 }
1126 return message;
1127}
1128
1129- (void)webView:(WebView *)sender didFailLoadWithError:(NSError*) error
1130 forFrame:(WebFrame *)frame
1131{
c75400ea 1132 webKitWindow->m_busy = false;
61b98a2d
SL
1133
1134 if (webKitWindow && frame == [sender mainFrame]){
1135 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
1136
04fa04d8 1137 wxWebViewNavigationError type;
61b98a2d 1138 wxString description = nsErrorToWxHtmlError(error, &type);
3225a4b8
SL
1139 wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_ERROR,
1140 webKitWindow->GetId(),
1141 wxStringWithNSString( url ),
1142 wxEmptyString);
1143 event.SetString(description);
1144 event.SetInt(type);
61b98a2d
SL
1145
1146 if (webKitWindow && webKitWindow->GetEventHandler())
1147 {
3225a4b8 1148 webKitWindow->GetEventHandler()->ProcessEvent(event);
61b98a2d
SL
1149 }
1150 }
1151}
1152
1153- (void)webView:(WebView *)sender
1154 didFailProvisionalLoadWithError:(NSError*)error
1155 forFrame:(WebFrame *)frame
1156{
c75400ea 1157 webKitWindow->m_busy = false;
61b98a2d
SL
1158
1159 if (webKitWindow && frame == [sender mainFrame]){
1160 NSString *url = [[[[frame provisionalDataSource] request] URL]
1161 absoluteString];
1162
04fa04d8 1163 wxWebViewNavigationError type;
61b98a2d 1164 wxString description = nsErrorToWxHtmlError(error, &type);
3225a4b8
SL
1165 wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_ERROR,
1166 webKitWindow->GetId(),
1167 wxStringWithNSString( url ),
1168 wxEmptyString);
1169 event.SetString(description);
1170 event.SetInt(type);
61b98a2d
SL
1171
1172 if (webKitWindow && webKitWindow->GetEventHandler())
3225a4b8 1173 webKitWindow->GetEventHandler()->ProcessEvent(event);
61b98a2d
SL
1174 }
1175}
1176
1177- (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title
1178 forFrame:(WebFrame *)frame
1179{
153530af 1180 wxString target = wxStringWithNSString([frame name]);
3225a4b8
SL
1181 wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED,
1182 webKitWindow->GetId(),
1183 webKitWindow->GetCurrentURL(),
1184 target);
153530af 1185
3225a4b8 1186 event.SetString(wxStringWithNSString(title));
153530af
SL
1187
1188 if (webKitWindow && webKitWindow->GetEventHandler())
3225a4b8 1189 webKitWindow->GetEventHandler()->ProcessEvent(event);
61b98a2d
SL
1190}
1191@end
1192
d2e66707 1193@implementation WebViewPolicyDelegate
61b98a2d 1194
0a6c8516 1195- initWithWxWindow: (wxWebViewWebKit*)inWindow
61b98a2d
SL
1196{
1197 [super init];
1198 webKitWindow = inWindow; // non retained
1199 return self;
1200}
1201
1202- (void)webView:(WebView *)sender
1203 decidePolicyForNavigationAction:(NSDictionary *)actionInformation
1204 request:(NSURLRequest *)request
1205 frame:(WebFrame *)frame
1206 decisionListener:(id<WebPolicyDecisionListener>)listener
1207{
61b98a2d
SL
1208 wxUnusedVar(frame);
1209
c75400ea 1210 webKitWindow->m_busy = true;
61b98a2d
SL
1211 NSString *url = [[request URL] absoluteString];
1212 wxString target = wxStringWithNSString([frame name]);
3225a4b8
SL
1213 wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_NAVIGATING,
1214 webKitWindow->GetId(),
1215 wxStringWithNSString( url ), target);
61b98a2d
SL
1216
1217 if (webKitWindow && webKitWindow->GetEventHandler())
3225a4b8 1218 webKitWindow->GetEventHandler()->ProcessEvent(event);
61b98a2d 1219
3225a4b8 1220 if (!event.IsAllowed())
61b98a2d 1221 {
c75400ea 1222 webKitWindow->m_busy = false;
61b98a2d
SL
1223 [listener ignore];
1224 }
1225 else
1226 {
1227 [listener use];
1228 }
1229}
1230
1231- (void)webView:(WebView *)sender
1232 decidePolicyForNewWindowAction:(NSDictionary *)actionInformation
1233 request:(NSURLRequest *)request
1234 newFrameName:(NSString *)frameName
1235 decisionListener:(id < WebPolicyDecisionListener >)listener
1236{
61b98a2d 1237 wxUnusedVar(actionInformation);
c75400ea 1238
b236f090 1239 NSString *url = [[request URL] absoluteString];
3225a4b8
SL
1240 wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_NEWWINDOW,
1241 webKitWindow->GetId(),
1242 wxStringWithNSString( url ), "");
b236f090
SL
1243
1244 if (webKitWindow && webKitWindow->GetEventHandler())
3225a4b8 1245 webKitWindow->GetEventHandler()->ProcessEvent(event);
61b98a2d
SL
1246
1247 [listener ignore];
1248}
1249@end
1250
5e3e62bc
SL
1251@implementation WebViewCustomProtocol
1252
1253+ (BOOL)canInitWithRequest:(NSURLRequest *)request
1254{
1255 NSString *scheme = [[request URL] scheme];
1256
1257 wxStringToWebHandlerMap::const_iterator it;
1258 for( it = g_stringHandlerMap.begin(); it != g_stringHandlerMap.end(); ++it )
1259 {
1260 if(it->first.IsSameAs(wxStringWithNSString(scheme)))
1261 {
1262 return YES;
1263 }
1264 }
1265
1266 return NO;
1267}
1268
1269+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request
1270{
7d8d6163 1271 //We don't do any processing here as the wxWebViewHandler classes do it
5e3e62bc
SL
1272 return request;
1273}
1274
1275- (void)startLoading
1276{
1277 NSURLRequest *request = [self request];
1278 NSString* path = [[request URL] absoluteString];
1279
1280 wxString wxpath = wxStringWithNSString(path);
1281 wxString scheme = wxStringWithNSString([[request URL] scheme]);
1282 wxFSFile* file = g_stringHandlerMap[scheme]->GetFile(wxpath);
1283 size_t length = file->GetStream()->GetLength();
1284
1285
1286 NSURLResponse *response = [[NSURLResponse alloc] initWithURL:[request URL]
1287 MIMEType:wxNSStringWithWxString(file->GetMimeType())
1288 expectedContentLength:length
1289 textEncodingName:nil];
1290
1291 //Load the data, we malloc it so it is tidied up properly
1292 void* buffer = malloc(length);
1293 file->GetStream()->Read(buffer, length);
1294 NSData *data = [[NSData alloc] initWithBytesNoCopy:buffer length:length];
1295
1296 id<NSURLProtocolClient> client = [self client];
1297
1298 //We do not support caching anything yet
1299 [client URLProtocol:self didReceiveResponse:response
1300 cacheStoragePolicy:NSURLCacheStorageNotAllowed];
1301
1302 //Set the data
1303 [client URLProtocol:self didLoadData:data];
1304
1305 //Notify that we have finished
1306 [client URLProtocolDidFinishLoading:self];
1307
1308 [response release];
1309}
1310
1311- (void)stopLoading
1312{
1313
1314}
1315
1316@end
1317
9d2f31db 1318#endif //wxUSE_WEBVIEW && wxUSE_WEBVIEW_WEBKIT