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