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