]> git.saurik.com Git - wxWidgets.git/blob - src/osx/webview_webkit.mm
build fixes for Universal
[wxWidgets.git] / src / osx / webview_webkit.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/webview_webkit.mm
3 // Purpose: wxWebViewWebKit - embeddable web kit control,
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
15 #include "wx/osx/webview_webkit.h"
16
17 #if wxUSE_WEBVIEW && wxUSE_WEBVIEW_WEBKIT && (defined(__WXOSX_COCOA__) \
18 || defined(__WXOSX_CARBON__))
19
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
27 #include "wx/osx/private.h"
28 #include "wx/cocoa/string.h"
29 #include "wx/hashmap.h"
30 #include "wx/filesys.h"
31
32 #include <WebKit/WebKit.h>
33 #include <WebKit/HIWebView.h>
34 #include <WebKit/CarbonUtils.h>
35
36 #include <Foundation/NSURLError.h>
37
38 #define DEBUG_WEBKIT_SIZING 0
39
40 // ----------------------------------------------------------------------------
41 // macros
42 // ----------------------------------------------------------------------------
43
44 wxIMPLEMENT_DYNAMIC_CLASS(wxWebViewWebKit, wxWebView);
45
46 BEGIN_EVENT_TABLE(wxWebViewWebKit, wxControl)
47 #if defined(__WXMAC__) && wxOSX_USE_CARBON
48 EVT_SIZE(wxWebViewWebKit::OnSize)
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
96 wxWebViewWebKit* thisWindow = (wxWebViewWebKit*) data ;
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
228 static pascal OSStatus wxWebViewWebKitEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
229 {
230 OSStatus result = eventNotHandledErr ;
231
232 wxMacCarbonEvent cEvent( event ) ;
233
234 ControlRef controlRef ;
235 wxWebViewWebKit* thisWindow = (wxWebViewWebKit*) data ;
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
296 DEFINE_ONE_SHOT_HANDLER_GETTER( wxWebViewWebKitEventHandler )
297
298 #endif
299
300 @interface WebViewLoadDelegate : NSObject
301 {
302 wxWebViewWebKit* webKitWindow;
303 }
304
305 - initWithWxWindow: (wxWebViewWebKit*)inWindow;
306
307 @end
308
309 @interface WebViewPolicyDelegate : NSObject
310 {
311 wxWebViewWebKit* webKitWindow;
312 }
313
314 - initWithWxWindow: (wxWebViewWebKit*)inWindow;
315
316 @end
317
318 //We use a hash to map scheme names to wxWebViewHandler
319 WX_DECLARE_STRING_HASH_MAP(wxSharedPtr<wxWebViewHandler>, wxStringToWebHandlerMap);
320
321 static wxStringToWebHandlerMap g_stringHandlerMap;
322
323 @interface WebViewCustomProtocol : NSURLProtocol
324 {
325 }
326 @end
327
328 // ----------------------------------------------------------------------------
329 // creation/destruction
330 // ----------------------------------------------------------------------------
331
332 bool wxWebViewWebKit::Create(wxWindow *parent,
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;
340
341 DontCreatePeer();
342 wxControl::Create(parent, winID, pos, size, style, wxDefaultValidator, name);
343
344 #if wxOSX_USE_CARBON
345 wxMacControl* peer = new wxMacControl(this);
346 WebInitForCarbon();
347 HIWebViewCreate( peer->GetControlRefAddr() );
348
349 m_webView = (WebView*) HIWebViewGetWebView( peer->GetControlRef() );
350
351 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
352 if ( UMAGetSystemVersion() >= 0x1030 )
353 HIViewChangeFeatures( peer->GetControlRef() , kHIViewIsOpaque , 0 ) ;
354 #endif
355 InstallControlEventHandler(peer->GetControlRef(),
356 GetwxWebViewWebKitEventHandlerUPP(),
357 GetEventTypeCount(eventList), eventList, this,
358 (EventHandlerRef *)&m_webKitCtrlEventHandler);
359 SetPeer(peer);
360 #else
361 NSRect r = wxOSXGetFrameForControl( this, pos , size ) ;
362 m_webView = [[WebView alloc] initWithFrame:r
363 frameName:@"webkitFrame"
364 groupName:@"webkitGroup"];
365 SetPeer(new wxWidgetCocoaImpl( this, m_webView ));
366 #endif
367
368 MacPostControlCreate(pos, size);
369
370 #if wxOSX_USE_CARBON
371 HIViewSetVisible( GetPeer()->GetControlRef(), true );
372 #endif
373 [m_webView setHidden:false];
374
375
376
377 // Register event listener interfaces
378 WebViewLoadDelegate* loadDelegate =
379 [[WebViewLoadDelegate alloc] initWithWxWindow: this];
380
381 [m_webView setFrameLoadDelegate:loadDelegate];
382
383 // this is used to veto page loads, etc.
384 WebViewPolicyDelegate* policyDelegate =
385 [[WebViewPolicyDelegate alloc] initWithWxWindow: this];
386
387 [m_webView setPolicyDelegate:policyDelegate];
388
389 //Register our own class for custom scheme handling
390 [NSURLProtocol registerClass:[WebViewCustomProtocol class]];
391
392 LoadURL(strURL);
393 return true;
394 }
395
396 wxWebViewWebKit::~wxWebViewWebKit()
397 {
398 WebViewLoadDelegate* loadDelegate = [m_webView frameLoadDelegate];
399 WebViewPolicyDelegate* policyDelegate = [m_webView policyDelegate];
400 [m_webView setFrameLoadDelegate: nil];
401 [m_webView setPolicyDelegate: nil];
402
403 if (loadDelegate)
404 [loadDelegate release];
405
406 if (policyDelegate)
407 [policyDelegate release];
408 }
409
410 // ----------------------------------------------------------------------------
411 // public methods
412 // ----------------------------------------------------------------------------
413
414 bool wxWebViewWebKit::CanGoBack() const
415 {
416 if ( !m_webView )
417 return false;
418
419 return [m_webView canGoBack];
420 }
421
422 bool wxWebViewWebKit::CanGoForward() const
423 {
424 if ( !m_webView )
425 return false;
426
427 return [m_webView canGoForward];
428 }
429
430 void wxWebViewWebKit::GoBack()
431 {
432 if ( !m_webView )
433 return;
434
435 [(WebView*)m_webView goBack];
436 }
437
438 void wxWebViewWebKit::GoForward()
439 {
440 if ( !m_webView )
441 return;
442
443 [(WebView*)m_webView goForward];
444 }
445
446 void wxWebViewWebKit::Reload(wxWebViewReloadFlags flags)
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
464 void wxWebViewWebKit::Stop()
465 {
466 if ( !m_webView )
467 return;
468
469 [[m_webView mainFrame] stopLoading];
470 }
471
472 bool wxWebViewWebKit::CanGetPageSource() const
473 {
474 if ( !m_webView )
475 return false;
476
477 WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
478 return ( [[dataSource representation] canProvideDocumentSource] );
479 }
480
481 wxString wxWebViewWebKit::GetPageSource() const
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
504 bool wxWebViewWebKit::CanIncreaseTextSize() const
505 {
506 if ( !m_webView )
507 return false;
508
509 if ([m_webView canMakeTextLarger])
510 return true;
511 else
512 return false;
513 }
514
515 void wxWebViewWebKit::IncreaseTextSize()
516 {
517 if ( !m_webView )
518 return;
519
520 if (CanIncreaseTextSize())
521 [m_webView makeTextLarger:(WebView*)m_webView];
522 }
523
524 bool wxWebViewWebKit::CanDecreaseTextSize() const
525 {
526 if ( !m_webView )
527 return false;
528
529 if ([m_webView canMakeTextSmaller])
530 return true;
531 else
532 return false;
533 }
534
535 void wxWebViewWebKit::DecreaseTextSize()
536 {
537 if ( !m_webView )
538 return;
539
540 if (CanDecreaseTextSize())
541 [m_webView makeTextSmaller:(WebView*)m_webView];
542 }
543
544 void wxWebViewWebKit::Print()
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
568 void wxWebViewWebKit::SetEditable(bool enable)
569 {
570 if ( !m_webView )
571 return;
572
573 [m_webView setEditable:enable ];
574 }
575
576 bool wxWebViewWebKit::IsEditable() const
577 {
578 if ( !m_webView )
579 return false;
580
581 return [m_webView isEditable];
582 }
583
584 void wxWebViewWebKit::SetZoomType(wxWebViewZoomType zoomType)
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
591 wxWebViewZoomType wxWebViewWebKit::GetZoomType() const
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
599 bool wxWebViewWebKit::CanSetZoomType(wxWebViewZoomType type) const
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
614 int wxWebViewWebKit::GetScrollPos()
615 {
616 id result = [[m_webView windowScriptObject]
617 evaluateWebScript:@"document.body.scrollTop"];
618 return [result intValue];
619 }
620
621 void wxWebViewWebKit::SetScrollPos(int pos)
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
632 wxString wxWebViewWebKit::GetSelectedText() const
633 {
634 NSString* selection = [[m_webView selectedDOMRange] markupString];
635 if (!selection) return wxEmptyString;
636
637 return wxStringWithNSString(selection);
638 }
639
640 void wxWebViewWebKit::RunScript(const wxString& javascript)
641 {
642 if ( !m_webView )
643 return;
644
645 [[m_webView windowScriptObject] evaluateWebScript:
646 (NSString*)wxNSStringWithWxString( javascript )];
647 }
648
649 void wxWebViewWebKit::OnSize(wxSizeEvent &event)
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
728 void wxWebViewWebKit::MacVisibilityChanged(){
729 #if defined(__WXMAC__) && wxOSX_USE_CARBON
730 bool isHidden = !IsControlVisible( GetPeer()->GetControlRef());
731 if (!isHidden)
732 [(WebView*)m_webView display];
733
734 [m_webView setHidden:isHidden];
735 #endif
736 }
737
738 void wxWebViewWebKit::LoadURL(const wxString& url)
739 {
740 [[m_webView mainFrame] loadRequest:[NSURLRequest requestWithURL:
741 [NSURL URLWithString:wxNSStringWithWxString(url)]]];
742 }
743
744 wxString wxWebViewWebKit::GetCurrentURL() const
745 {
746 return wxStringWithNSString([m_webView mainFrameURL]);
747 }
748
749 wxString wxWebViewWebKit::GetCurrentTitle() const
750 {
751 return wxStringWithNSString([m_webView mainFrameTitle]);
752 }
753
754 float wxWebViewWebKit::GetWebkitZoom() const
755 {
756 return [m_webView textSizeMultiplier];
757 }
758
759 void wxWebViewWebKit::SetWebkitZoom(float zoom)
760 {
761 [m_webView setTextSizeMultiplier:zoom];
762 }
763
764 wxWebViewZoom wxWebViewWebKit::GetZoom() const
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
795 void wxWebViewWebKit::SetZoom(wxWebViewZoom zoom)
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
826 void wxWebViewWebKit::SetPage(const wxString& src, const wxString& baseUrl)
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
836 void wxWebViewWebKit::Cut()
837 {
838 if ( !m_webView )
839 return;
840
841 [(WebView*)m_webView cut:m_webView];
842 }
843
844 void wxWebViewWebKit::Copy()
845 {
846 if ( !m_webView )
847 return;
848
849 [(WebView*)m_webView copy:m_webView];
850 }
851
852 void wxWebViewWebKit::Paste()
853 {
854 if ( !m_webView )
855 return;
856
857 [(WebView*)m_webView paste:m_webView];
858 }
859
860 void wxWebViewWebKit::DeleteSelection()
861 {
862 if ( !m_webView )
863 return;
864
865 [(WebView*)m_webView deleteSelection];
866 }
867
868 bool wxWebViewWebKit::HasSelection() const
869 {
870 DOMRange* range = [m_webView selectedDOMRange];
871 if(!range)
872 {
873 return false;
874 }
875 else
876 {
877 return true;
878 }
879 }
880
881 void wxWebViewWebKit::ClearSelection()
882 {
883 //We use javascript as selection isn't exposed at the moment in webkit
884 RunScript("window.getSelection().removeAllRanges();");
885 }
886
887 void wxWebViewWebKit::SelectAll()
888 {
889 RunScript("window.getSelection().selectAllChildren(document.body);");
890 }
891
892 wxString wxWebViewWebKit::GetSelectedSource() const
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
903 wxString wxWebViewWebKit::GetPageText() const
904 {
905 id result = [[m_webView windowScriptObject]
906 evaluateWebScript:@"document.body.textContent"];
907 return wxStringWithNSString([result stringValue]);
908 }
909
910 void wxWebViewWebKit::EnableHistory(bool enable)
911 {
912 if ( !m_webView )
913 return;
914
915 [m_webView setMaintainsBackForwardList:enable];
916 }
917
918 void wxWebViewWebKit::ClearHistory()
919 {
920 [m_webView setMaintainsBackForwardList:NO];
921 [m_webView setMaintainsBackForwardList:YES];
922 }
923
924 wxVector<wxSharedPtr<wxWebViewHistoryItem> > wxWebViewWebKit::GetBackwardHistory()
925 {
926 wxVector<wxSharedPtr<wxWebViewHistoryItem> > backhist;
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]);
934 wxWebViewHistoryItem* wxitem = new wxWebViewHistoryItem(url, title);
935 wxitem->m_histItem = item;
936 wxSharedPtr<wxWebViewHistoryItem> itemptr(wxitem);
937 backhist.push_back(itemptr);
938 }
939 return backhist;
940 }
941
942 wxVector<wxSharedPtr<wxWebViewHistoryItem> > wxWebViewWebKit::GetForwardHistory()
943 {
944 wxVector<wxSharedPtr<wxWebViewHistoryItem> > forwardhist;
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]);
952 wxWebViewHistoryItem* wxitem = new wxWebViewHistoryItem(url, title);
953 wxitem->m_histItem = item;
954 wxSharedPtr<wxWebViewHistoryItem> itemptr(wxitem);
955 forwardhist.push_back(itemptr);
956 }
957 return forwardhist;
958 }
959
960 void wxWebViewWebKit::LoadHistoryItem(wxSharedPtr<wxWebViewHistoryItem> item)
961 {
962 [m_webView goToBackForwardItem:item->m_histItem];
963 }
964
965 bool wxWebViewWebKit::CanUndo() const
966 {
967 return [[m_webView undoManager] canUndo];
968 }
969
970 bool wxWebViewWebKit::CanRedo() const
971 {
972 return [[m_webView undoManager] canRedo];
973 }
974
975 void wxWebViewWebKit::Undo()
976 {
977 [[m_webView undoManager] undo];
978 }
979
980 void wxWebViewWebKit::Redo()
981 {
982 [[m_webView undoManager] redo];
983 }
984
985 void wxWebViewWebKit::RegisterHandler(wxSharedPtr<wxWebViewHandler> handler)
986 {
987 g_stringHandlerMap[handler->GetName()] = handler;
988 }
989
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
999 @implementation WebViewLoadDelegate
1000
1001 - initWithWxWindow: (wxWebViewWebKit*)inWindow
1002 {
1003 [super init];
1004 webKitWindow = inWindow; // non retained
1005 return self;
1006 }
1007
1008 - (void)webView:(WebView *)sender
1009 didStartProvisionalLoadForFrame:(WebFrame *)frame
1010 {
1011 webKitWindow->m_busy = true;
1012 }
1013
1014 - (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame
1015 {
1016 webKitWindow->m_busy = true;
1017
1018 if (webKitWindow && frame == [sender mainFrame]){
1019 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
1020 wxString target = wxStringWithNSString([frame name]);
1021 wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_NAVIGATED,
1022 webKitWindow->GetId(),
1023 wxStringWithNSString( url ),
1024 target);
1025
1026 if (webKitWindow && webKitWindow->GetEventHandler())
1027 webKitWindow->GetEventHandler()->ProcessEvent(event);
1028 }
1029 }
1030
1031 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
1032 {
1033 webKitWindow->m_busy = false;
1034
1035 if (webKitWindow && frame == [sender mainFrame]){
1036 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
1037
1038 wxString target = wxStringWithNSString([frame name]);
1039 wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_LOADED,
1040 webKitWindow->GetId(),
1041 wxStringWithNSString( url ),
1042 target);
1043
1044 if (webKitWindow && webKitWindow->GetEventHandler())
1045 webKitWindow->GetEventHandler()->ProcessEvent(event);
1046 }
1047 }
1048
1049 wxString nsErrorToWxHtmlError(NSError* error, wxWebViewNavigationError* out)
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:
1065 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
1066 case NSURLErrorDataLengthExceedsMaximum:
1067 #endif
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
1089 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
1090 case NSURLErrorCannotDecodeRawData:
1091 case NSURLErrorCannotDecodeContentData:
1092 case NSURLErrorCannotParseResponse:
1093 #endif
1094 case NSURLErrorBadServerResponse:
1095 *out = wxWEB_NAV_ERR_REQUEST;
1096 break;
1097
1098 case NSURLErrorUserAuthenticationRequired:
1099 case NSURLErrorSecureConnectionFailed:
1100 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
1101 case NSURLErrorClientCertificateRequired:
1102 #endif
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 {
1132 webKitWindow->m_busy = false;
1133
1134 if (webKitWindow && frame == [sender mainFrame]){
1135 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
1136
1137 wxWebViewNavigationError type;
1138 wxString description = nsErrorToWxHtmlError(error, &type);
1139 wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_ERROR,
1140 webKitWindow->GetId(),
1141 wxStringWithNSString( url ),
1142 wxEmptyString);
1143 event.SetString(description);
1144 event.SetInt(type);
1145
1146 if (webKitWindow && webKitWindow->GetEventHandler())
1147 {
1148 webKitWindow->GetEventHandler()->ProcessEvent(event);
1149 }
1150 }
1151 }
1152
1153 - (void)webView:(WebView *)sender
1154 didFailProvisionalLoadWithError:(NSError*)error
1155 forFrame:(WebFrame *)frame
1156 {
1157 webKitWindow->m_busy = false;
1158
1159 if (webKitWindow && frame == [sender mainFrame]){
1160 NSString *url = [[[[frame provisionalDataSource] request] URL]
1161 absoluteString];
1162
1163 wxWebViewNavigationError type;
1164 wxString description = nsErrorToWxHtmlError(error, &type);
1165 wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_ERROR,
1166 webKitWindow->GetId(),
1167 wxStringWithNSString( url ),
1168 wxEmptyString);
1169 event.SetString(description);
1170 event.SetInt(type);
1171
1172 if (webKitWindow && webKitWindow->GetEventHandler())
1173 webKitWindow->GetEventHandler()->ProcessEvent(event);
1174 }
1175 }
1176
1177 - (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title
1178 forFrame:(WebFrame *)frame
1179 {
1180 wxString target = wxStringWithNSString([frame name]);
1181 wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED,
1182 webKitWindow->GetId(),
1183 webKitWindow->GetCurrentURL(),
1184 target);
1185
1186 event.SetString(wxStringWithNSString(title));
1187
1188 if (webKitWindow && webKitWindow->GetEventHandler())
1189 webKitWindow->GetEventHandler()->ProcessEvent(event);
1190 }
1191 @end
1192
1193 @implementation WebViewPolicyDelegate
1194
1195 - initWithWxWindow: (wxWebViewWebKit*)inWindow
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 {
1208 wxUnusedVar(frame);
1209
1210 webKitWindow->m_busy = true;
1211 NSString *url = [[request URL] absoluteString];
1212 wxString target = wxStringWithNSString([frame name]);
1213 wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_NAVIGATING,
1214 webKitWindow->GetId(),
1215 wxStringWithNSString( url ), target);
1216
1217 if (webKitWindow && webKitWindow->GetEventHandler())
1218 webKitWindow->GetEventHandler()->ProcessEvent(event);
1219
1220 if (!event.IsAllowed())
1221 {
1222 webKitWindow->m_busy = false;
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 {
1237 wxUnusedVar(actionInformation);
1238
1239 NSString *url = [[request URL] absoluteString];
1240 wxWebViewEvent event(wxEVT_COMMAND_WEB_VIEW_NEWWINDOW,
1241 webKitWindow->GetId(),
1242 wxStringWithNSString( url ), "");
1243
1244 if (webKitWindow && webKitWindow->GetEventHandler())
1245 webKitWindow->GetEventHandler()->ProcessEvent(event);
1246
1247 [listener ignore];
1248 }
1249 @end
1250
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 {
1271 //We don't do any processing here as the wxWebViewHandler classes do it
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
1318 #endif //wxUSE_WEBVIEW && wxUSE_WEBVIEW_WEBKIT