]> git.saurik.com Git - wxWidgets.git/blame - src/html/htmlctrl/webkit/webkit.mm
fix leak introduced by r53753
[wxWidgets.git] / src / html / htmlctrl / webkit / webkit.mm
CommitLineData
2c990ba6
KO
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
448f8f12
KO
12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "webkit.h"
14#endif
15
2c990ba6
KO
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
216e968a 18#include "wx/splitter.h"
2c990ba6 19
2c990ba6
KO
20#ifndef WX_PRECOMP
21 #include "wx/wx.h"
22#endif
23
948f6c6e
JS
24#if wxUSE_WEBKIT
25
2c990ba6
KO
26#ifdef __WXCOCOA__
27#include "wx/cocoa/autorelease.h"
28#else
29#include "wx/mac/uma.h"
30#include <Carbon/Carbon.h>
b384c0fb 31#include <WebKit/WebKit.h>
2c990ba6
KO
32#include <WebKit/HIWebView.h>
33#include <WebKit/CarbonUtils.h>
34#endif
35
36#include "wx/html/webkit.h"
2c990ba6 37
448f8f12 38#define DEBUG_WEBKIT_SIZING 0
2c990ba6
KO
39
40// ----------------------------------------------------------------------------
41// macros
42// ----------------------------------------------------------------------------
43
2c990ba6 44IMPLEMENT_DYNAMIC_CLASS(wxWebKitCtrl, wxControl)
2c990ba6
KO
45
46BEGIN_EVENT_TABLE(wxWebKitCtrl, wxControl)
005198fa 47 EVT_SIZE(wxWebKitCtrl::OnSize)
2c990ba6
KO
48END_EVENT_TABLE()
49
448f8f12
KO
50// ----------------------------------------------------------------------------
51// Carbon Events handlers
52// ----------------------------------------------------------------------------
53
54// prototype for function in src/mac/carbon/toplevel.cpp
55void SetupMouseEvent( wxMouseEvent &wxevent , wxMacCarbonEvent &cEvent );
56
57static const EventTypeSpec eventList[] =
58{
59 //{ kEventClassControl, kEventControlTrack } ,
60 { kEventClassMouse, kEventMouseUp },
61 { kEventClassMouse, kEventMouseDown },
62 { kEventClassMouse, kEventMouseMoved },
63 { kEventClassMouse, kEventMouseDragged },
8a088306 64
c70aaa09
KO
65 { kEventClassKeyboard, kEventRawKeyDown } ,
66 { kEventClassKeyboard, kEventRawKeyRepeat } ,
67 { kEventClassKeyboard, kEventRawKeyUp } ,
68 { kEventClassKeyboard, kEventRawKeyModifiersChanged } ,
8a088306 69
c70aaa09
KO
70 { kEventClassTextInput, kEventTextInputUnicodeForKeyEvent } ,
71 { kEventClassTextInput, kEventTextInputUpdateActiveInputArea } ,
8a088306 72
448f8f12
KO
73#if DEBUG_WEBKIT_SIZING == 1
74 { kEventClassControl, kEventControlBoundsChanged } ,
75#endif
76};
77
c70aaa09
KO
78// mix this in from window.cpp
79pascal 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.
85static pascal OSStatus wxWebKitKeyEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
86{
87 OSStatus result = eventNotHandledErr ;
88 wxMacCarbonEvent cEvent( event ) ;
89
8a088306 90 wxWebKitCtrl* thisWindow = (wxWebKitCtrl*) data ;
c70aaa09
KO
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
448f8f12
KO
214static 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 ;
d0d7f207 222 wxNonOwnedWindow* tlw = NULL;
448f8f12
KO
223 if (thisWindow)
224 tlw = thisWindow->MacGetTopLevelWindow();
225
226 cEvent.GetParameter( kEventParamDirectObject , &controlRef ) ;
8a088306 227
448f8f12 228 wxWindow* currentMouseWindow = thisWindow ;
8a088306 229
448f8f12
KO
230 if ( wxApp::s_captureWindow )
231 currentMouseWindow = wxApp::s_captureWindow;
232
c70aaa09 233 switch ( GetEventClass( event ) )
448f8f12 234 {
c70aaa09
KO
235 case kEventClassKeyboard:
236 {
237 result = wxWebKitKeyEventHandler(handler, event, data);
238 break;
239 }
8a088306 240
c70aaa09
KO
241 case kEventClassTextInput:
242 {
243 result = wxMacUnicodeTextEventHandler(handler, event, data);
244 break;
245 }
8a088306 246
c70aaa09
KO
247 case kEventClassMouse:
248 {
249 switch ( GetEventKind( event ) )
448f8f12 250 {
c70aaa09
KO
251 case kEventMouseDragged :
252 case kEventMouseMoved :
253 case kEventMouseDown :
254 case kEventMouseUp :
448f8f12 255 {
c70aaa09
KO
256 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
257 SetupMouseEvent( wxevent , cEvent ) ;
8a088306 258
c70aaa09
KO
259 currentMouseWindow->ScreenToClient( &wxevent.m_x , &wxevent.m_y ) ;
260 wxevent.SetEventObject( currentMouseWindow ) ;
261 wxevent.SetId( currentMouseWindow->GetId() ) ;
8a088306 262
c70aaa09
KO
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 ;
448f8f12 272 }
c70aaa09
KO
273 }
274 default:
275 break;
448f8f12
KO
276 }
277
278 result = CallNextEventHandler(handler, event);
279 return result ;
280}
281
282DEFINE_ONE_SHOT_HANDLER_GETTER( wxWebKitCtrlEventHandler )
283
284
2c990ba6
KO
285// ----------------------------------------------------------------------------
286// wxWebKit Events
287// ----------------------------------------------------------------------------
288
289IMPLEMENT_DYNAMIC_CLASS( wxWebKitStateChangedEvent, wxCommandEvent )
290
291DEFINE_EVENT_TYPE( wxEVT_WEBKIT_STATE_CHANGED )
292
293wxWebKitStateChangedEvent::wxWebKitStateChangedEvent( wxWindow* win )
294{
295 SetEventType( wxEVT_WEBKIT_STATE_CHANGED);
296 SetEventObject( win );
3ca7ba74 297 SetId(win->GetId());
2c990ba6
KO
298}
299
448f8f12
KO
300IMPLEMENT_DYNAMIC_CLASS( wxWebKitBeforeLoadEvent, wxCommandEvent )
301
302DEFINE_EVENT_TYPE( wxEVT_WEBKIT_BEFORE_LOAD )
303
304wxWebKitBeforeLoadEvent::wxWebKitBeforeLoadEvent( wxWindow* win )
305{
306 m_cancelled = false;
307 SetEventType( wxEVT_WEBKIT_BEFORE_LOAD);
308 SetEventObject( win );
309 SetId(win->GetId());
310}
311
2c990ba6
KO
312//---------------------------------------------------------
313// helper functions for NSString<->wxString conversion
314//---------------------------------------------------------
315
316inline wxString wxStringWithNSString(NSString *nsstring)
317{
318#if wxUSE_UNICODE
319 return wxString([nsstring UTF8String], wxConvUTF8);
320#else
321 return wxString([nsstring lossyCString]);
322#endif // wxUSE_UNICODE
323}
324
325inline NSString* wxNSStringWithWxString(const wxString &wxstring)
326{
327#if wxUSE_UNICODE
328 return [NSString stringWithUTF8String: wxstring.mb_str(wxConvUTF8)];
329#else
330 return [NSString stringWithCString: wxstring.c_str() length:wxstring.Len()];
331#endif // wxUSE_UNICODE
332}
333
448f8f12
KO
334inline int wxNavTypeFromWebNavType(int type){
335 if (type == WebNavigationTypeLinkClicked)
336 return wxWEBKIT_NAV_LINK_CLICKED;
8a088306 337
448f8f12
KO
338 if (type == WebNavigationTypeFormSubmitted)
339 return wxWEBKIT_NAV_FORM_SUBMITTED;
8a088306 340
448f8f12
KO
341 if (type == WebNavigationTypeBackForward)
342 return wxWEBKIT_NAV_BACK_NEXT;
8a088306 343
448f8f12
KO
344 if (type == WebNavigationTypeReload)
345 return wxWEBKIT_NAV_RELOAD;
8a088306 346
448f8f12
KO
347 if (type == WebNavigationTypeFormResubmitted)
348 return wxWEBKIT_NAV_FORM_RESUBMITTED;
8a088306 349
448f8f12
KO
350 return wxWEBKIT_NAV_OTHER;
351}
352
2c990ba6
KO
353@interface MyFrameLoadMonitor : NSObject
354{
448f8f12
KO
355 wxWebKitCtrl* webKitWindow;
356}
357
358- initWithWxWindow: (wxWebKitCtrl*)inWindow;
359
360@end
361
362@interface MyPolicyDelegate : NSObject
363{
364 wxWebKitCtrl* webKitWindow;
2c990ba6
KO
365}
366
448f8f12 367- initWithWxWindow: (wxWebKitCtrl*)inWindow;
2c990ba6
KO
368
369@end
370
371// ----------------------------------------------------------------------------
372// creation/destruction
373// ----------------------------------------------------------------------------
374
375bool wxWebKitCtrl::Create(wxWindow *parent,
3ca7ba74 376 wxWindowID winID,
2c990ba6
KO
377 const wxString& strURL,
378 const wxPoint& pos,
3ca7ba74
RD
379 const wxSize& size, long style,
380 const wxValidator& validator,
381 const wxString& name)
2c990ba6
KO
382{
383
384 m_currentURL = strURL;
14f21d6d 385 //m_pageTitle = _("Untitled Page");
9548f380 386
2c990ba6
KO
387 //still needed for wxCocoa??
388/*
389 int width, height;
3ca7ba74 390 wxSize sizeInstance;
9548f380 391 if (size.x == wxDefaultCoord || size.y == wxDefaultCoord)
3ca7ba74
RD
392 {
393 m_parent->GetClientSize(&width, &height);
394 sizeInstance.x = width;
395 sizeInstance.y = height;
396 }
397 else
398 {
399 sizeInstance.x = size.x;
400 sizeInstance.y = size.y;
401 }
9548f380 402*/
3ca7ba74 403 // now create and attach WebKit view...
2c990ba6
KO
404#ifdef __WXCOCOA__
405 wxControl::Create(parent, m_windowID, pos, sizeInstance, style , validator , name);
3ca7ba74 406 SetSize(pos.x, pos.y, sizeInstance.x, sizeInstance.y);
9548f380 407
2c990ba6
KO
408 wxTopLevelWindowCocoa *topWin = wxDynamicCast(this, wxTopLevelWindowCocoa);
409 NSWindow* nsWin = topWin->GetNSWindow();
3ca7ba74 410 NSRect rect;
2c990ba6
KO
411 rect.origin.x = pos.x;
412 rect.origin.y = pos.y;
413 rect.size.width = sizeInstance.x;
414 rect.size.height = sizeInstance.y;
415 m_webView = (WebView*)[[WebView alloc] initWithFrame:rect frameName:@"webkitFrame" groupName:@"webkitGroup"];
416 SetNSView(m_webView);
417 [m_cocoaNSView release];
418
419 if(m_parent) m_parent->CocoaAddChild(this);
420 SetInitialFrameRect(pos,sizeInstance);
421#else
422 m_macIsUserPane = false;
daef4689 423 wxControl::Create(parent, winID, pos, size, style , validator , name);
637013eb 424 m_peer = new wxMacControl(this);
2c990ba6 425 WebInitForCarbon();
f8405d6e 426 HIWebViewCreate( m_peer->GetControlRefAddr() );
9548f380 427
f8405d6e 428 m_webView = (WebView*) HIWebViewGetWebView( m_peer->GetControlRef() );
8a088306 429
14f21d6d 430 MacPostControlCreate(pos, size);
9548f380 431 HIViewSetVisible( m_peer->GetControlRef(), true );
5b8f917c 432 [m_webView setHidden:false];
b384c0fb 433#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
daef4689 434 if ( UMAGetSystemVersion() >= 0x1030 )
448f8f12 435 HIViewChangeFeatures( m_peer->GetControlRef() , kHIViewIsOpaque , 0 ) ;
b384c0fb 436#endif
448f8f12
KO
437 InstallControlEventHandler( m_peer->GetControlRef() , GetwxWebKitCtrlEventHandlerUPP(),
438 GetEventTypeCount(eventList), eventList, this,
439 (EventHandlerRef *)&m_webKitCtrlEventHandler);
440
2c990ba6
KO
441#endif
442
443 // Register event listener interfaces
448f8f12 444 MyFrameLoadMonitor* myFrameLoadMonitor = [[MyFrameLoadMonitor alloc] initWithWxWindow: this];
3ca7ba74 445 [m_webView setFrameLoadDelegate:myFrameLoadMonitor];
8a088306 446
448f8f12
KO
447 // this is used to veto page loads, etc.
448 MyPolicyDelegate* myPolicyDelegate = [[MyPolicyDelegate alloc] initWithWxWindow: this];
449 [m_webView setPolicyDelegate:myPolicyDelegate];
8a088306 450
2c990ba6
KO
451 LoadURL(m_currentURL);
452 return true;
453}
454
455wxWebKitCtrl::~wxWebKitCtrl()
456{
457
458}
459
460// ----------------------------------------------------------------------------
461// public methods
462// ----------------------------------------------------------------------------
463
464void wxWebKitCtrl::LoadURL(const wxString &url)
465{
466 if( !m_webView )
467 return;
9548f380 468
7eb8c6ee 469 [[m_webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:wxNSStringWithWxString(url)]]];
2c990ba6
KO
470
471 m_currentURL = url;
472}
473
474bool wxWebKitCtrl::CanGoBack(){
475 if ( !m_webView )
476 return false;
9548f380 477
2c990ba6
KO
478 return [m_webView canGoBack];
479}
480
481bool wxWebKitCtrl::CanGoForward(){
482 if ( !m_webView )
483 return false;
9548f380 484
2c990ba6
KO
485 return [m_webView canGoForward];
486}
487
488bool wxWebKitCtrl::GoBack(){
489 if ( !m_webView )
490 return false;
9548f380 491
8f3b30d5
KO
492 bool result = [(WebView*)m_webView goBack];
493 return result;
2c990ba6
KO
494}
495
9548f380 496bool wxWebKitCtrl::GoForward(){
2c990ba6
KO
497 if ( !m_webView )
498 return false;
9548f380 499
8f3b30d5
KO
500 bool result = [(WebView*)m_webView goForward];
501 return result;
2c990ba6
KO
502}
503
9548f380 504void wxWebKitCtrl::Reload(){
2c990ba6
KO
505 if ( !m_webView )
506 return;
9548f380 507
2c990ba6
KO
508 [[m_webView mainFrame] reload];
509}
510
511void wxWebKitCtrl::Stop(){
512 if ( !m_webView )
513 return;
9548f380 514
2c990ba6
KO
515 [[m_webView mainFrame] stopLoading];
516}
517
518bool wxWebKitCtrl::CanGetPageSource(){
519 if ( !m_webView )
14f21d6d 520 return false;
9548f380 521
2c990ba6
KO
522 WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
523 return ( [[dataSource representation] canProvideDocumentSource] );
524}
525
526wxString wxWebKitCtrl::GetPageSource(){
9548f380 527
2c990ba6
KO
528 if (CanGetPageSource()){
529 WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
530 return wxStringWithNSString( [[dataSource representation] documentSource] );
531 }
9548f380
WS
532
533 return wxEmptyString;
2c990ba6
KO
534}
535
448f8f12
KO
536wxString wxWebKitCtrl::GetSelection(){
537 if ( !m_webView )
538 return wxEmptyString;
8a088306 539
448f8f12
KO
540 NSString* selectedText = [[m_webView selectedDOMRange] toString];
541 return wxStringWithNSString( selectedText );
542}
543
544bool wxWebKitCtrl::CanIncreaseTextSize(){
545 if ( !m_webView )
546 return false;
8a088306 547
448f8f12
KO
548 if ([m_webView canMakeTextLarger])
549 return true;
550 else
551 return false;
552}
553
554void wxWebKitCtrl::IncreaseTextSize(){
555 if ( !m_webView )
556 return;
8a088306 557
448f8f12
KO
558 if (CanIncreaseTextSize())
559 [m_webView makeTextLarger:(WebView*)m_webView];
560}
561
562bool wxWebKitCtrl::CanDecreaseTextSize(){
563 if ( !m_webView )
564 return false;
8a088306 565
448f8f12
KO
566 if ([m_webView canMakeTextSmaller])
567 return true;
568 else
569 return false;
570}
571
572void wxWebKitCtrl::DecreaseTextSize(){
573 if ( !m_webView )
574 return;
8a088306 575
448f8f12
KO
576 if (CanDecreaseTextSize())
577 [m_webView makeTextSmaller:(WebView*)m_webView];
578}
579
580void wxWebKitCtrl::SetPageSource(const wxString& source, const wxString& baseUrl){
2c990ba6
KO
581 if ( !m_webView )
582 return;
9548f380 583
2d837688 584 [[m_webView mainFrame] loadHTMLString:(NSString*)wxNSStringWithWxString( source ) baseURL:[NSURL URLWithString:wxNSStringWithWxString( baseUrl )]];
448f8f12
KO
585
586}
587
588void wxWebKitCtrl::Print(bool showPrompt){
589 if ( !m_webView )
590 return;
8a088306
VZ
591
592 id view = [[[m_webView mainFrame] frameView] documentView];
593 NSPrintOperation *op = [NSPrintOperation printOperationWithView:view printInfo: [NSPrintInfo sharedPrintInfo]];
448f8f12
KO
594 if (showPrompt){
595 [op setShowsPrintPanel: showPrompt];
596 // in my tests, the progress bar always freezes and it stops the whole print operation.
597 // do not turn this to true unless there is a workaround for the bug.
598 [op setShowsProgressPanel: false];
599 }
8a088306
VZ
600 // Print it.
601 [op runOperation];
448f8f12
KO
602}
603
604void wxWebKitCtrl::MakeEditable(bool enable){
605 if ( !m_webView )
606 return;
607
608 [m_webView setEditable:enable ];
609}
610
611bool wxWebKitCtrl::IsEditable(){
612 if ( !m_webView )
613 return false;
614
615 return [m_webView isEditable];
616}
617
618int wxWebKitCtrl::GetScrollPos(){
619 id result = [[m_webView windowScriptObject] evaluateWebScript:@"document.body.scrollTop"];
620 return [result intValue];
8a088306 621}
448f8f12
KO
622
623void wxWebKitCtrl::SetScrollPos(int pos){
624 if ( !m_webView )
625 return;
8a088306
VZ
626
627 wxString javascript;
448f8f12
KO
628 javascript.Printf(wxT("document.body.scrollTop = %d;"), pos);
629 [[m_webView windowScriptObject] evaluateWebScript:(NSString*)wxNSStringWithWxString( javascript )];
630}
631
632wxString wxWebKitCtrl::RunScript(const wxString& javascript){
633 if ( !m_webView )
8a088306
VZ
634 return wxEmptyString;
635
448f8f12 636 id result = [[m_webView windowScriptObject] evaluateWebScript:(NSString*)wxNSStringWithWxString( javascript )];
8a088306 637
448f8f12
KO
638 NSString* resultAsString;
639 wxString resultAsWxString = wxEmptyString;
640 NSString* className = NSStringFromClass([result class]);
641 if ([className isEqualToString:@"NSCFNumber"])
642 resultAsString = [NSString stringWithFormat:@"%@", result];
643 else if ([className isEqualToString:@"NSCFString"])
644 resultAsString = result;
645 else if ([className isEqualToString:@"NSCFBoolean"]){
646 if ([result boolValue])
647 resultAsString = @"true";
648 else
649 resultAsString = @"false";
650 }
651 else if ([className isEqualToString:@"WebScriptObject"])
652 resultAsString = [result stringRepresentation];
653 else
654 fprintf(stderr, "wxWebKitCtrl::RunScript - Unexpected return type: %s!\n", [className UTF8String]);
655
656 resultAsWxString = wxStringWithNSString( resultAsString );
657 return resultAsWxString;
2c990ba6
KO
658}
659
005198fa 660void wxWebKitCtrl::OnSize(wxSizeEvent &event){
39104bc1
KO
661 // This is a nasty hack because WebKit seems to lose its position when it is embedded
662 // in a control that is not itself the content view for a TLW.
daef4689
VZ
663 // I put it in OnSize because these calcs are not perfect, and in fact are basically
664 // guesses based on reverse engineering, so it's best to give people the option of
665 // overriding OnSize with their own calcs if need be.
666 // I also left some test debugging print statements as a convenience if a(nother)
667 // problem crops up.
668
daef4689
VZ
669 wxWindow* tlw = MacGetTopLevelWindow();
670
448f8f12
KO
671 NSRect frame = [m_webView frame];
672 NSRect bounds = [m_webView bounds];
8a088306 673
448f8f12
KO
674#if DEBUG_WEBKIT_SIZING
675 fprintf(stderr,"Carbon window x=%d, y=%d, width=%d, height=%d\n", GetPosition().x, GetPosition().y, GetSize().x, GetSize().y);
676 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);
677 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);
678#endif
679
daef4689
VZ
680 // This must be the case that Apple tested with, because well, in this one case
681 // we don't need to do anything! It just works. ;)
448f8f12 682 if (GetParent() == tlw){
daef4689
VZ
683 return;
684 }
685
c70aaa09 686 // since we no longer use parent coordinates, we always want 0,0.
8a088306 687 int x = 0;
c70aaa09 688 int y = 0;
8a088306 689
448f8f12
KO
690 HIRect rect;
691 rect.origin.x = x;
692 rect.origin.y = y;
daef4689 693
448f8f12
KO
694#if DEBUG_WEBKIT_SIZING
695 printf("Before conversion, origin is: x = %d, y = %d\n", x, y);
696#endif
daef4689 697
448f8f12
KO
698 // NB: In most cases, when calling HIViewConvertRect, what people want is to use GetRootControl(),
699 // and this tripped me up at first. But in fact, what we want is the root view, because we need to
8a088306
VZ
700 // make the y origin relative to the very top of the window, not its contents, since we later flip
701 // the y coordinate for Cocoa.
702 HIViewConvertRect (&rect, m_peer->GetControlRef(),
448f8f12 703 HIViewGetRoot( (WindowRef) MacGetTopLevelWindowRef() ) );
daef4689 704
448f8f12
KO
705 x = (int)rect.origin.x;
706 y = (int)rect.origin.y;
daef4689 707
448f8f12
KO
708#if DEBUG_WEBKIT_SIZING
709 printf("Moving Cocoa frame origin to: x = %d, y = %d\n", x, y);
710#endif
daef4689
VZ
711
712 if (tlw){
713 //flip the y coordinate to convert to Cocoa coordinates
daef4689
VZ
714 y = tlw->GetSize().y - ((GetSize().y) + y);
715 }
716
448f8f12
KO
717#if DEBUG_WEBKIT_SIZING
718 printf("y = %d after flipping value\n", y);
719#endif
9548f380 720
448f8f12
KO
721 frame.origin.x = x;
722 frame.origin.y = y;
723 [m_webView setFrame:frame];
8a088306 724
fe3fc02e 725 if (IsShown())
14753ffe 726 [(WebView*)m_webView display];
005198fa
KO
727 event.Skip();
728}
2c990ba6 729
14f21d6d 730void wxWebKitCtrl::MacVisibilityChanged(){
f8405d6e 731 bool isHidden = !IsControlVisible( m_peer->GetControlRef());
fe3fc02e 732 if (!isHidden)
14753ffe 733 [(WebView*)m_webView display];
9548f380 734
14f21d6d
KO
735 [m_webView setHidden:isHidden];
736}
737
2c990ba6
KO
738//------------------------------------------------------------
739// Listener interfaces
740//------------------------------------------------------------
741
c70aaa09
KO
742// NB: I'm still tracking this down, but it appears the Cocoa window
743// still has these events fired on it while the Carbon control is being
744// destroyed. Therefore, we must be careful to check both the existence
745// of the Carbon control and the event handler before firing events.
746
2c990ba6
KO
747@implementation MyFrameLoadMonitor
748
448f8f12 749- initWithWxWindow: (wxWebKitCtrl*)inWindow
2c990ba6
KO
750{
751 [super init];
3ca7ba74 752 webKitWindow = inWindow; // non retained
2c990ba6
KO
753 return self;
754}
755
756- (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame
757{
c70aaa09 758 if (webKitWindow && frame == [sender mainFrame]){
2c990ba6
KO
759 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
760 wxWebKitStateChangedEvent thisEvent(webKitWindow);
761 thisEvent.SetState(wxWEBKIT_STATE_NEGOTIATING);
762 thisEvent.SetURL( wxStringWithNSString( url ) );
c70aaa09
KO
763 if (webKitWindow->GetEventHandler())
764 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
2c990ba6
KO
765 }
766}
767
768- (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame
769{
c70aaa09 770 if (webKitWindow && frame == [sender mainFrame]){
fba8e95e 771 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
2c990ba6
KO
772 wxWebKitStateChangedEvent thisEvent(webKitWindow);
773 thisEvent.SetState(wxWEBKIT_STATE_TRANSFERRING);
774 thisEvent.SetURL( wxStringWithNSString( url ) );
c70aaa09
KO
775 if (webKitWindow->GetEventHandler())
776 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
2c990ba6
KO
777 }
778}
779
780- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
781{
c70aaa09 782 if (webKitWindow && frame == [sender mainFrame]){
fba8e95e 783 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
2c990ba6
KO
784 wxWebKitStateChangedEvent thisEvent(webKitWindow);
785 thisEvent.SetState(wxWEBKIT_STATE_STOP);
786 thisEvent.SetURL( wxStringWithNSString( url ) );
c70aaa09
KO
787 if (webKitWindow->GetEventHandler())
788 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
2c990ba6
KO
789 }
790}
791
792- (void)webView:(WebView *)sender didFailLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
793{
8a088306
VZ
794 wxUnusedVar(error);
795
c70aaa09 796 if (webKitWindow && frame == [sender mainFrame]){
fba8e95e 797 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
2c990ba6
KO
798 wxWebKitStateChangedEvent thisEvent(webKitWindow);
799 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
800 thisEvent.SetURL( wxStringWithNSString( url ) );
c70aaa09
KO
801 if (webKitWindow->GetEventHandler())
802 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
2c990ba6
KO
803 }
804}
805
806- (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
807{
8a088306
VZ
808 wxUnusedVar(error);
809
c70aaa09 810 if (webKitWindow && frame == [sender mainFrame]){
2c990ba6
KO
811 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
812 wxWebKitStateChangedEvent thisEvent(webKitWindow);
813 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
814 thisEvent.SetURL( wxStringWithNSString( url ) );
c70aaa09
KO
815 if (webKitWindow->GetEventHandler())
816 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
2c990ba6
KO
817 }
818}
819
820- (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
821{
c70aaa09 822 if (webKitWindow && frame == [sender mainFrame]){
448f8f12 823 webKitWindow->SetPageTitle(wxStringWithNSString( title ));
2c990ba6
KO
824 }
825}
3ca7ba74 826@end
5b8f917c 827
448f8f12
KO
828@implementation MyPolicyDelegate
829
830- initWithWxWindow: (wxWebKitCtrl*)inWindow
831{
832 [super init];
833 webKitWindow = inWindow; // non retained
834 return self;
835}
836
837- (void)webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener
838{
8a088306
VZ
839 wxUnusedVar(sender);
840 wxUnusedVar(frame);
841
448f8f12
KO
842 wxWebKitBeforeLoadEvent thisEvent(webKitWindow);
843
8a088306
VZ
844 // Get the navigation type.
845 NSNumber *n = [actionInformation objectForKey:WebActionNavigationTypeKey];
448f8f12
KO
846 int actionType = [n intValue];
847 thisEvent.SetNavigationType( wxNavTypeFromWebNavType(actionType) );
8a088306 848
448f8f12
KO
849 NSString *url = [[request URL] absoluteString];
850 thisEvent.SetURL( wxStringWithNSString( url ) );
8a088306 851
c70aaa09
KO
852 if (webKitWindow && webKitWindow->GetEventHandler())
853 webKitWindow->GetEventHandler()->ProcessEvent(thisEvent);
448f8f12 854
8a088306 855 if (thisEvent.IsCancelled())
448f8f12
KO
856 [listener ignore];
857 else
858 [listener use];
859}
860
861@end
862
948f6c6e 863#endif //wxUSE_WEBKIT