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