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