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