]>
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/mac/uma.h" | |
30 | #include <Carbon/Carbon.h> | |
31 | #include <WebKit/WebKit.h> | |
32 | #include <WebKit/HIWebView.h> | |
33 | #include <WebKit/CarbonUtils.h> | |
34 | #endif | |
35 | ||
36 | #include "wx/html/webkit.h" | |
37 | ||
38 | #define DEBUG_WEBKIT_SIZING 0 | |
39 | ||
40 | // ---------------------------------------------------------------------------- | |
41 | // macros | |
42 | // ---------------------------------------------------------------------------- | |
43 | ||
44 | IMPLEMENT_DYNAMIC_CLASS(wxWebKitCtrl, wxControl) | |
45 | ||
46 | BEGIN_EVENT_TABLE(wxWebKitCtrl, wxControl) | |
47 | EVT_SIZE(wxWebKitCtrl::OnSize) | |
48 | END_EVENT_TABLE() | |
49 | ||
50 | // ---------------------------------------------------------------------------- | |
51 | // Carbon Events handlers | |
52 | // ---------------------------------------------------------------------------- | |
53 | ||
54 | // prototype for function in src/mac/carbon/toplevel.cpp | |
55 | void SetupMouseEvent( wxMouseEvent &wxevent , wxMacCarbonEvent &cEvent ); | |
56 | ||
57 | static const EventTypeSpec eventList[] = | |
58 | { | |
59 | //{ kEventClassControl, kEventControlTrack } , | |
60 | { kEventClassMouse, kEventMouseUp }, | |
61 | { kEventClassMouse, kEventMouseDown }, | |
62 | { kEventClassMouse, kEventMouseMoved }, | |
63 | { kEventClassMouse, kEventMouseDragged }, | |
64 | #if DEBUG_WEBKIT_SIZING == 1 | |
65 | { kEventClassControl, kEventControlBoundsChanged } , | |
66 | #endif | |
67 | }; | |
68 | ||
69 | static pascal OSStatus wxWebKitCtrlEventHandler( EventHandlerCallRef handler , EventRef event , void *data ) | |
70 | { | |
71 | OSStatus result = eventNotHandledErr ; | |
72 | ||
73 | wxMacCarbonEvent cEvent( event ) ; | |
74 | ||
75 | ControlRef controlRef ; | |
76 | wxWebKitCtrl* thisWindow = (wxWebKitCtrl*) data ; | |
77 | wxTopLevelWindowMac* tlw = NULL; | |
78 | if (thisWindow) | |
79 | tlw = thisWindow->MacGetTopLevelWindow(); | |
80 | ||
81 | cEvent.GetParameter( kEventParamDirectObject , &controlRef ) ; | |
82 | ||
83 | wxWindow* currentMouseWindow = thisWindow ; | |
84 | ||
85 | if ( wxApp::s_captureWindow ) | |
86 | currentMouseWindow = wxApp::s_captureWindow; | |
87 | ||
88 | switch ( GetEventKind( event ) ) | |
89 | { | |
90 | case kEventMouseDragged : | |
91 | case kEventMouseMoved : | |
92 | { | |
93 | wxMouseEvent wxevent(wxEVT_LEFT_DOWN); | |
94 | SetupMouseEvent( wxevent , cEvent ) ; | |
95 | ||
96 | currentMouseWindow->ScreenToClient( &wxevent.m_x , &wxevent.m_y ) ; | |
97 | wxevent.SetEventObject( currentMouseWindow ) ; | |
98 | wxevent.SetId( currentMouseWindow->GetId() ) ; | |
99 | ||
100 | if ( currentMouseWindow->GetEventHandler()->ProcessEvent(wxevent) ) | |
101 | { | |
102 | result = noErr; | |
103 | } | |
104 | ||
105 | break; // this should enable WebKit to fire mouse dragged and mouse up events... | |
106 | } | |
107 | ||
108 | case kEventControlBoundsChanged: | |
109 | { | |
110 | // this is just here for debugging, so we can note any differences between | |
111 | // native event sizes and the sizes the wxWindow receives. | |
112 | Rect origBounds = cEvent.GetParameter<Rect>(kEventParamOriginalBounds, typeQDRectangle) ; | |
113 | Rect prevBounds = cEvent.GetParameter<Rect>(kEventParamPreviousBounds, typeQDRectangle) ; | |
114 | Rect curBounds = cEvent.GetParameter<Rect>(kEventParamCurrentBounds, typeQDRectangle) ; | |
115 | ||
116 | fprintf(stderr, "Orig bounds x=%d, y=%d, height=%d, width=%d\n", origBounds.left, origBounds.top, origBounds.bottom -origBounds.top, origBounds.right - origBounds.left); | |
117 | fprintf(stderr, "Prev bounds x=%d, y=%d, height=%d, width=%d\n", prevBounds.left, prevBounds.top, prevBounds.bottom -prevBounds.top, prevBounds.right - prevBounds.left); | |
118 | fprintf(stderr, "Cur bounds x=%d, y=%d, height=%d, width=%d\n", curBounds.left, curBounds.top, curBounds.bottom -curBounds.top, curBounds.right - curBounds.left); | |
119 | } | |
120 | default : | |
121 | break ; | |
122 | } | |
123 | ||
124 | result = CallNextEventHandler(handler, event); | |
125 | return result ; | |
126 | } | |
127 | ||
128 | DEFINE_ONE_SHOT_HANDLER_GETTER( wxWebKitCtrlEventHandler ) | |
129 | ||
130 | ||
131 | // ---------------------------------------------------------------------------- | |
132 | // wxWebKit Events | |
133 | // ---------------------------------------------------------------------------- | |
134 | ||
135 | IMPLEMENT_DYNAMIC_CLASS( wxWebKitStateChangedEvent, wxCommandEvent ) | |
136 | ||
137 | DEFINE_EVENT_TYPE( wxEVT_WEBKIT_STATE_CHANGED ) | |
138 | ||
139 | wxWebKitStateChangedEvent::wxWebKitStateChangedEvent( wxWindow* win ) | |
140 | { | |
141 | SetEventType( wxEVT_WEBKIT_STATE_CHANGED); | |
142 | SetEventObject( win ); | |
143 | SetId(win->GetId()); | |
144 | } | |
145 | ||
146 | IMPLEMENT_DYNAMIC_CLASS( wxWebKitBeforeLoadEvent, wxCommandEvent ) | |
147 | ||
148 | DEFINE_EVENT_TYPE( wxEVT_WEBKIT_BEFORE_LOAD ) | |
149 | ||
150 | wxWebKitBeforeLoadEvent::wxWebKitBeforeLoadEvent( wxWindow* win ) | |
151 | { | |
152 | m_cancelled = false; | |
153 | SetEventType( wxEVT_WEBKIT_BEFORE_LOAD); | |
154 | SetEventObject( win ); | |
155 | SetId(win->GetId()); | |
156 | } | |
157 | ||
158 | //--------------------------------------------------------- | |
159 | // helper functions for NSString<->wxString conversion | |
160 | //--------------------------------------------------------- | |
161 | ||
162 | inline wxString wxStringWithNSString(NSString *nsstring) | |
163 | { | |
164 | #if wxUSE_UNICODE | |
165 | return wxString([nsstring UTF8String], wxConvUTF8); | |
166 | #else | |
167 | return wxString([nsstring lossyCString]); | |
168 | #endif // wxUSE_UNICODE | |
169 | } | |
170 | ||
171 | inline NSString* wxNSStringWithWxString(const wxString &wxstring) | |
172 | { | |
173 | #if wxUSE_UNICODE | |
174 | return [NSString stringWithUTF8String: wxstring.mb_str(wxConvUTF8)]; | |
175 | #else | |
176 | return [NSString stringWithCString: wxstring.c_str() length:wxstring.Len()]; | |
177 | #endif // wxUSE_UNICODE | |
178 | } | |
179 | ||
180 | inline int wxNavTypeFromWebNavType(int type){ | |
181 | if (type == WebNavigationTypeLinkClicked) | |
182 | return wxWEBKIT_NAV_LINK_CLICKED; | |
183 | ||
184 | if (type == WebNavigationTypeFormSubmitted) | |
185 | return wxWEBKIT_NAV_FORM_SUBMITTED; | |
186 | ||
187 | if (type == WebNavigationTypeBackForward) | |
188 | return wxWEBKIT_NAV_BACK_NEXT; | |
189 | ||
190 | if (type == WebNavigationTypeReload) | |
191 | return wxWEBKIT_NAV_RELOAD; | |
192 | ||
193 | if (type == WebNavigationTypeFormResubmitted) | |
194 | return wxWEBKIT_NAV_FORM_RESUBMITTED; | |
195 | ||
196 | return wxWEBKIT_NAV_OTHER; | |
197 | } | |
198 | ||
199 | @interface MyFrameLoadMonitor : NSObject | |
200 | { | |
201 | wxWebKitCtrl* webKitWindow; | |
202 | } | |
203 | ||
204 | - initWithWxWindow: (wxWebKitCtrl*)inWindow; | |
205 | ||
206 | @end | |
207 | ||
208 | @interface MyPolicyDelegate : NSObject | |
209 | { | |
210 | wxWebKitCtrl* webKitWindow; | |
211 | } | |
212 | ||
213 | - initWithWxWindow: (wxWebKitCtrl*)inWindow; | |
214 | ||
215 | @end | |
216 | ||
217 | // ---------------------------------------------------------------------------- | |
218 | // creation/destruction | |
219 | // ---------------------------------------------------------------------------- | |
220 | ||
221 | bool wxWebKitCtrl::Create(wxWindow *parent, | |
222 | wxWindowID winID, | |
223 | const wxString& strURL, | |
224 | const wxPoint& pos, | |
225 | const wxSize& size, long style, | |
226 | const wxValidator& validator, | |
227 | const wxString& name) | |
228 | { | |
229 | ||
230 | m_currentURL = strURL; | |
231 | //m_pageTitle = _("Untitled Page"); | |
232 | ||
233 | //still needed for wxCocoa?? | |
234 | /* | |
235 | int width, height; | |
236 | wxSize sizeInstance; | |
237 | if (size.x == wxDefaultCoord || size.y == wxDefaultCoord) | |
238 | { | |
239 | m_parent->GetClientSize(&width, &height); | |
240 | sizeInstance.x = width; | |
241 | sizeInstance.y = height; | |
242 | } | |
243 | else | |
244 | { | |
245 | sizeInstance.x = size.x; | |
246 | sizeInstance.y = size.y; | |
247 | } | |
248 | */ | |
249 | // now create and attach WebKit view... | |
250 | #ifdef __WXCOCOA__ | |
251 | wxControl::Create(parent, m_windowID, pos, sizeInstance, style , validator , name); | |
252 | SetSize(pos.x, pos.y, sizeInstance.x, sizeInstance.y); | |
253 | ||
254 | wxTopLevelWindowCocoa *topWin = wxDynamicCast(this, wxTopLevelWindowCocoa); | |
255 | NSWindow* nsWin = topWin->GetNSWindow(); | |
256 | NSRect rect; | |
257 | rect.origin.x = pos.x; | |
258 | rect.origin.y = pos.y; | |
259 | rect.size.width = sizeInstance.x; | |
260 | rect.size.height = sizeInstance.y; | |
261 | m_webView = (WebView*)[[WebView alloc] initWithFrame:rect frameName:@"webkitFrame" groupName:@"webkitGroup"]; | |
262 | SetNSView(m_webView); | |
263 | [m_cocoaNSView release]; | |
264 | ||
265 | if(m_parent) m_parent->CocoaAddChild(this); | |
266 | SetInitialFrameRect(pos,sizeInstance); | |
267 | #else | |
268 | m_macIsUserPane = false; | |
269 | wxControl::Create(parent, winID, pos, size, style , validator , name); | |
270 | m_peer = new wxMacControl(this); | |
271 | WebInitForCarbon(); | |
272 | HIWebViewCreate( m_peer->GetControlRefAddr() ); | |
273 | ||
274 | m_webView = (WebView*) HIWebViewGetWebView( m_peer->GetControlRef() ); | |
275 | ||
276 | MacPostControlCreate(pos, size); | |
277 | HIViewSetVisible( m_peer->GetControlRef(), true ); | |
278 | [m_webView setHidden:false]; | |
279 | #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3 | |
280 | if ( UMAGetSystemVersion() >= 0x1030 ) | |
281 | HIViewChangeFeatures( m_peer->GetControlRef() , kHIViewIsOpaque , 0 ) ; | |
282 | #endif | |
283 | InstallControlEventHandler( m_peer->GetControlRef() , GetwxWebKitCtrlEventHandlerUPP(), | |
284 | GetEventTypeCount(eventList), eventList, this, | |
285 | (EventHandlerRef *)&m_webKitCtrlEventHandler); | |
286 | ||
287 | #endif | |
288 | ||
289 | // Register event listener interfaces | |
290 | MyFrameLoadMonitor* myFrameLoadMonitor = [[MyFrameLoadMonitor alloc] initWithWxWindow: this]; | |
291 | [m_webView setFrameLoadDelegate:myFrameLoadMonitor]; | |
292 | ||
293 | // this is used to veto page loads, etc. | |
294 | MyPolicyDelegate* myPolicyDelegate = [[MyPolicyDelegate alloc] initWithWxWindow: this]; | |
295 | [m_webView setPolicyDelegate:myPolicyDelegate]; | |
296 | ||
297 | LoadURL(m_currentURL); | |
298 | return true; | |
299 | } | |
300 | ||
301 | wxWebKitCtrl::~wxWebKitCtrl() | |
302 | { | |
303 | ||
304 | } | |
305 | ||
306 | // ---------------------------------------------------------------------------- | |
307 | // public methods | |
308 | // ---------------------------------------------------------------------------- | |
309 | ||
310 | void wxWebKitCtrl::LoadURL(const wxString &url) | |
311 | { | |
312 | if( !m_webView ) | |
313 | return; | |
314 | ||
315 | [[m_webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:wxNSStringWithWxString(url)]]]; | |
316 | ||
317 | m_currentURL = url; | |
318 | } | |
319 | ||
320 | bool wxWebKitCtrl::CanGoBack(){ | |
321 | if ( !m_webView ) | |
322 | return false; | |
323 | ||
324 | return [m_webView canGoBack]; | |
325 | } | |
326 | ||
327 | bool wxWebKitCtrl::CanGoForward(){ | |
328 | if ( !m_webView ) | |
329 | return false; | |
330 | ||
331 | return [m_webView canGoForward]; | |
332 | } | |
333 | ||
334 | bool wxWebKitCtrl::GoBack(){ | |
335 | if ( !m_webView ) | |
336 | return false; | |
337 | ||
338 | bool result = [(WebView*)m_webView goBack]; | |
339 | return result; | |
340 | } | |
341 | ||
342 | bool wxWebKitCtrl::GoForward(){ | |
343 | if ( !m_webView ) | |
344 | return false; | |
345 | ||
346 | bool result = [(WebView*)m_webView goForward]; | |
347 | return result; | |
348 | } | |
349 | ||
350 | void wxWebKitCtrl::Reload(){ | |
351 | if ( !m_webView ) | |
352 | return; | |
353 | ||
354 | [[m_webView mainFrame] reload]; | |
355 | } | |
356 | ||
357 | void wxWebKitCtrl::Stop(){ | |
358 | if ( !m_webView ) | |
359 | return; | |
360 | ||
361 | [[m_webView mainFrame] stopLoading]; | |
362 | } | |
363 | ||
364 | bool wxWebKitCtrl::CanGetPageSource(){ | |
365 | if ( !m_webView ) | |
366 | return false; | |
367 | ||
368 | WebDataSource* dataSource = [[m_webView mainFrame] dataSource]; | |
369 | return ( [[dataSource representation] canProvideDocumentSource] ); | |
370 | } | |
371 | ||
372 | wxString wxWebKitCtrl::GetPageSource(){ | |
373 | ||
374 | if (CanGetPageSource()){ | |
375 | WebDataSource* dataSource = [[m_webView mainFrame] dataSource]; | |
376 | return wxStringWithNSString( [[dataSource representation] documentSource] ); | |
377 | } | |
378 | ||
379 | return wxEmptyString; | |
380 | } | |
381 | ||
382 | wxString wxWebKitCtrl::GetSelection(){ | |
383 | if ( !m_webView ) | |
384 | return wxEmptyString; | |
385 | ||
386 | NSString* selectedText = [[m_webView selectedDOMRange] toString]; | |
387 | return wxStringWithNSString( selectedText ); | |
388 | } | |
389 | ||
390 | bool wxWebKitCtrl::CanIncreaseTextSize(){ | |
391 | if ( !m_webView ) | |
392 | return false; | |
393 | ||
394 | if ([m_webView canMakeTextLarger]) | |
395 | return true; | |
396 | else | |
397 | return false; | |
398 | } | |
399 | ||
400 | void wxWebKitCtrl::IncreaseTextSize(){ | |
401 | if ( !m_webView ) | |
402 | return; | |
403 | ||
404 | if (CanIncreaseTextSize()) | |
405 | [m_webView makeTextLarger:(WebView*)m_webView]; | |
406 | } | |
407 | ||
408 | bool wxWebKitCtrl::CanDecreaseTextSize(){ | |
409 | if ( !m_webView ) | |
410 | return false; | |
411 | ||
412 | if ([m_webView canMakeTextSmaller]) | |
413 | return true; | |
414 | else | |
415 | return false; | |
416 | } | |
417 | ||
418 | void wxWebKitCtrl::DecreaseTextSize(){ | |
419 | if ( !m_webView ) | |
420 | return; | |
421 | ||
422 | if (CanDecreaseTextSize()) | |
423 | [m_webView makeTextSmaller:(WebView*)m_webView]; | |
424 | } | |
425 | ||
426 | void wxWebKitCtrl::SetPageSource(const wxString& source, const wxString& baseUrl){ | |
427 | if ( !m_webView ) | |
428 | return; | |
429 | ||
430 | [[m_webView mainFrame] loadHTMLString:(NSString*)wxNSStringWithWxString( source ) baseURL:[NSURL URLWithString:wxNSStringWithWxString( baseUrl )]]; | |
431 | ||
432 | } | |
433 | ||
434 | void wxWebKitCtrl::Print(bool showPrompt){ | |
435 | if ( !m_webView ) | |
436 | return; | |
437 | ||
438 | id view = [[[m_webView mainFrame] frameView] documentView]; | |
439 | NSPrintOperation *op = [NSPrintOperation printOperationWithView:view printInfo: [NSPrintInfo sharedPrintInfo]]; | |
440 | if (showPrompt){ | |
441 | [op setShowsPrintPanel: showPrompt]; | |
442 | // in my tests, the progress bar always freezes and it stops the whole print operation. | |
443 | // do not turn this to true unless there is a workaround for the bug. | |
444 | [op setShowsProgressPanel: false]; | |
445 | } | |
446 | // Print it. | |
447 | [op runOperation]; | |
448 | } | |
449 | ||
450 | void wxWebKitCtrl::MakeEditable(bool enable){ | |
451 | if ( !m_webView ) | |
452 | return; | |
453 | ||
454 | [m_webView setEditable:enable ]; | |
455 | } | |
456 | ||
457 | bool wxWebKitCtrl::IsEditable(){ | |
458 | if ( !m_webView ) | |
459 | return false; | |
460 | ||
461 | return [m_webView isEditable]; | |
462 | } | |
463 | ||
464 | int wxWebKitCtrl::GetScrollPos(){ | |
465 | id result = [[m_webView windowScriptObject] evaluateWebScript:@"document.body.scrollTop"]; | |
466 | return [result intValue]; | |
467 | } | |
468 | ||
469 | void wxWebKitCtrl::SetScrollPos(int pos){ | |
470 | if ( !m_webView ) | |
471 | return; | |
472 | ||
473 | wxString javascript; | |
474 | javascript.Printf(wxT("document.body.scrollTop = %d;"), pos); | |
475 | [[m_webView windowScriptObject] evaluateWebScript:(NSString*)wxNSStringWithWxString( javascript )]; | |
476 | } | |
477 | ||
478 | wxString wxWebKitCtrl::RunScript(const wxString& javascript){ | |
479 | if ( !m_webView ) | |
480 | return wxEmptyString; | |
481 | ||
482 | id result = [[m_webView windowScriptObject] evaluateWebScript:(NSString*)wxNSStringWithWxString( javascript )]; | |
483 | ||
484 | NSString* resultAsString; | |
485 | wxString resultAsWxString = wxEmptyString; | |
486 | NSString* className = NSStringFromClass([result class]); | |
487 | if ([className isEqualToString:@"NSCFNumber"]) | |
488 | resultAsString = [NSString stringWithFormat:@"%@", result]; | |
489 | else if ([className isEqualToString:@"NSCFString"]) | |
490 | resultAsString = result; | |
491 | else if ([className isEqualToString:@"NSCFBoolean"]){ | |
492 | if ([result boolValue]) | |
493 | resultAsString = @"true"; | |
494 | else | |
495 | resultAsString = @"false"; | |
496 | } | |
497 | else if ([className isEqualToString:@"WebScriptObject"]) | |
498 | resultAsString = [result stringRepresentation]; | |
499 | else | |
500 | fprintf(stderr, "wxWebKitCtrl::RunScript - Unexpected return type: %s!\n", [className UTF8String]); | |
501 | ||
502 | resultAsWxString = wxStringWithNSString( resultAsString ); | |
503 | return resultAsWxString; | |
504 | } | |
505 | ||
506 | void wxWebKitCtrl::OnSize(wxSizeEvent &event){ | |
507 | // This is a nasty hack because WebKit seems to lose its position when it is embedded | |
508 | // in a control that is not itself the content view for a TLW. | |
509 | // I put it in OnSize because these calcs are not perfect, and in fact are basically | |
510 | // guesses based on reverse engineering, so it's best to give people the option of | |
511 | // overriding OnSize with their own calcs if need be. | |
512 | // I also left some test debugging print statements as a convenience if a(nother) | |
513 | // problem crops up. | |
514 | ||
515 | wxWindow* tlw = MacGetTopLevelWindow(); | |
516 | ||
517 | NSRect frame = [m_webView frame]; | |
518 | NSRect bounds = [m_webView bounds]; | |
519 | ||
520 | #if DEBUG_WEBKIT_SIZING | |
521 | fprintf(stderr,"Carbon window x=%d, y=%d, width=%d, height=%d\n", GetPosition().x, GetPosition().y, GetSize().x, GetSize().y); | |
522 | 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); | |
523 | 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); | |
524 | #endif | |
525 | ||
526 | // This must be the case that Apple tested with, because well, in this one case | |
527 | // we don't need to do anything! It just works. ;) | |
528 | if (GetParent() == tlw){ | |
529 | return; | |
530 | } | |
531 | ||
532 | int x = GetPosition().x; | |
533 | int y = GetPosition().y; | |
534 | ||
535 | HIRect rect; | |
536 | rect.origin.x = x; | |
537 | rect.origin.y = y; | |
538 | ||
539 | #if DEBUG_WEBKIT_SIZING | |
540 | printf("Before conversion, origin is: x = %d, y = %d\n", x, y); | |
541 | #endif | |
542 | ||
543 | // NB: In most cases, when calling HIViewConvertRect, what people want is to use GetRootControl(), | |
544 | // and this tripped me up at first. But in fact, what we want is the root view, because we need to | |
545 | // make the y origin relative to the very top of the window, not its contents, since we later flip | |
546 | // the y coordinate for Cocoa. | |
547 | HIViewConvertRect (&rect, HIViewGetSuperview( m_peer->GetControlRef() ), | |
548 | HIViewGetRoot( (WindowRef) MacGetTopLevelWindowRef() ) ); | |
549 | ||
550 | x = (int)rect.origin.x; | |
551 | y = (int)rect.origin.y; | |
552 | ||
553 | #if DEBUG_WEBKIT_SIZING | |
554 | printf("Moving Cocoa frame origin to: x = %d, y = %d\n", x, y); | |
555 | #endif | |
556 | ||
557 | if (tlw){ | |
558 | //flip the y coordinate to convert to Cocoa coordinates | |
559 | y = tlw->GetSize().y - ((GetSize().y) + y); | |
560 | } | |
561 | ||
562 | #if DEBUG_WEBKIT_SIZING | |
563 | printf("y = %d after flipping value\n", y); | |
564 | #endif | |
565 | ||
566 | frame.origin.x = x; | |
567 | frame.origin.y = y; | |
568 | [m_webView setFrame:frame]; | |
569 | ||
570 | if (IsShown()) | |
571 | [(WebView*)m_webView display]; | |
572 | event.Skip(); | |
573 | } | |
574 | ||
575 | void wxWebKitCtrl::MacVisibilityChanged(){ | |
576 | bool isHidden = !IsControlVisible( m_peer->GetControlRef()); | |
577 | if (!isHidden) | |
578 | [(WebView*)m_webView display]; | |
579 | ||
580 | [m_webView setHidden:isHidden]; | |
581 | } | |
582 | ||
583 | //------------------------------------------------------------ | |
584 | // Listener interfaces | |
585 | //------------------------------------------------------------ | |
586 | ||
587 | @implementation MyFrameLoadMonitor | |
588 | ||
589 | - initWithWxWindow: (wxWebKitCtrl*)inWindow | |
590 | { | |
591 | [super init]; | |
592 | webKitWindow = inWindow; // non retained | |
593 | return self; | |
594 | } | |
595 | ||
596 | - (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame | |
597 | { | |
598 | if (frame == [sender mainFrame]){ | |
599 | NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString]; | |
600 | wxWebKitStateChangedEvent thisEvent(webKitWindow); | |
601 | thisEvent.SetState(wxWEBKIT_STATE_NEGOTIATING); | |
602 | thisEvent.SetURL( wxStringWithNSString( url ) ); | |
603 | webKitWindow->GetEventHandler()->ProcessEvent( thisEvent ); | |
604 | } | |
605 | } | |
606 | ||
607 | - (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame | |
608 | { | |
609 | if (frame == [sender mainFrame]){ | |
610 | NSString *url = [[[[frame dataSource] request] URL] absoluteString]; | |
611 | wxWebKitStateChangedEvent thisEvent(webKitWindow); | |
612 | thisEvent.SetState(wxWEBKIT_STATE_TRANSFERRING); | |
613 | thisEvent.SetURL( wxStringWithNSString( url ) ); | |
614 | webKitWindow->GetEventHandler()->ProcessEvent( thisEvent ); | |
615 | } | |
616 | } | |
617 | ||
618 | - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame | |
619 | { | |
620 | if (frame == [sender mainFrame]){ | |
621 | NSString *url = [[[[frame dataSource] request] URL] absoluteString]; | |
622 | wxWebKitStateChangedEvent thisEvent(webKitWindow); | |
623 | thisEvent.SetState(wxWEBKIT_STATE_STOP); | |
624 | thisEvent.SetURL( wxStringWithNSString( url ) ); | |
625 | webKitWindow->GetEventHandler()->ProcessEvent( thisEvent ); | |
626 | } | |
627 | } | |
628 | ||
629 | - (void)webView:(WebView *)sender didFailLoadWithError:(NSError*) error forFrame:(WebFrame *)frame | |
630 | { | |
631 | if (frame == [sender mainFrame]){ | |
632 | NSString *url = [[[[frame dataSource] request] URL] absoluteString]; | |
633 | wxWebKitStateChangedEvent thisEvent(webKitWindow); | |
634 | thisEvent.SetState(wxWEBKIT_STATE_FAILED); | |
635 | thisEvent.SetURL( wxStringWithNSString( url ) ); | |
636 | webKitWindow->GetEventHandler()->ProcessEvent( thisEvent ); | |
637 | } | |
638 | } | |
639 | ||
640 | - (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError*) error forFrame:(WebFrame *)frame | |
641 | { | |
642 | if (frame == [sender mainFrame]){ | |
643 | NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString]; | |
644 | wxWebKitStateChangedEvent thisEvent(webKitWindow); | |
645 | thisEvent.SetState(wxWEBKIT_STATE_FAILED); | |
646 | thisEvent.SetURL( wxStringWithNSString( url ) ); | |
647 | webKitWindow->GetEventHandler()->ProcessEvent( thisEvent ); | |
648 | } | |
649 | } | |
650 | ||
651 | - (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame | |
652 | { | |
653 | if (frame == [sender mainFrame]){ | |
654 | webKitWindow->SetPageTitle(wxStringWithNSString( title )); | |
655 | } | |
656 | } | |
657 | @end | |
658 | ||
659 | @implementation MyPolicyDelegate | |
660 | ||
661 | - initWithWxWindow: (wxWebKitCtrl*)inWindow | |
662 | { | |
663 | [super init]; | |
664 | webKitWindow = inWindow; // non retained | |
665 | return self; | |
666 | } | |
667 | ||
668 | - (void)webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener | |
669 | { | |
670 | wxWebKitBeforeLoadEvent thisEvent(webKitWindow); | |
671 | ||
672 | // Get the navigation type. | |
673 | NSNumber *n = [actionInformation objectForKey:WebActionNavigationTypeKey]; | |
674 | int actionType = [n intValue]; | |
675 | thisEvent.SetNavigationType( wxNavTypeFromWebNavType(actionType) ); | |
676 | ||
677 | NSString *url = [[request URL] absoluteString]; | |
678 | thisEvent.SetURL( wxStringWithNSString( url ) ); | |
679 | ||
680 | webKitWindow->GetEventHandler()->ProcessEvent(thisEvent); | |
681 | ||
682 | if (thisEvent.IsCancelled()) | |
683 | [listener ignore]; | |
684 | else | |
685 | [listener use]; | |
686 | } | |
687 | ||
688 | @end | |
689 | ||
690 | #endif //wxUSE_WEBKIT |