]>
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 | |
29 | #include "wx/mac/uma.h" | |
30 | #include <Carbon/Carbon.h> | |
b384c0fb | 31 | #include <WebKit/WebKit.h> |
2c990ba6 KO |
32 | #include <WebKit/HIWebView.h> |
33 | #include <WebKit/CarbonUtils.h> | |
34 | #endif | |
35 | ||
36 | #include "wx/html/webkit.h" | |
2c990ba6 | 37 | |
448f8f12 | 38 | #define DEBUG_WEBKIT_SIZING 0 |
2c990ba6 KO |
39 | |
40 | // ---------------------------------------------------------------------------- | |
41 | // macros | |
42 | // ---------------------------------------------------------------------------- | |
43 | ||
2c990ba6 | 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 }, | |
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 | ||
2c990ba6 KO |
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 ); | |
3ca7ba74 | 143 | SetId(win->GetId()); |
2c990ba6 KO |
144 | } |
145 | ||
448f8f12 KO |
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 | ||
2c990ba6 KO |
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 | ||
448f8f12 KO |
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 | ||
2c990ba6 KO |
199 | @interface MyFrameLoadMonitor : NSObject |
200 | { | |
448f8f12 KO |
201 | wxWebKitCtrl* webKitWindow; |
202 | } | |
203 | ||
204 | - initWithWxWindow: (wxWebKitCtrl*)inWindow; | |
205 | ||
206 | @end | |
207 | ||
208 | @interface MyPolicyDelegate : NSObject | |
209 | { | |
210 | wxWebKitCtrl* webKitWindow; | |
2c990ba6 KO |
211 | } |
212 | ||
448f8f12 | 213 | - initWithWxWindow: (wxWebKitCtrl*)inWindow; |
2c990ba6 KO |
214 | |
215 | @end | |
216 | ||
217 | // ---------------------------------------------------------------------------- | |
218 | // creation/destruction | |
219 | // ---------------------------------------------------------------------------- | |
220 | ||
221 | bool wxWebKitCtrl::Create(wxWindow *parent, | |
3ca7ba74 | 222 | wxWindowID winID, |
2c990ba6 KO |
223 | const wxString& strURL, |
224 | const wxPoint& pos, | |
3ca7ba74 RD |
225 | const wxSize& size, long style, |
226 | const wxValidator& validator, | |
227 | const wxString& name) | |
2c990ba6 KO |
228 | { |
229 | ||
230 | m_currentURL = strURL; | |
14f21d6d | 231 | //m_pageTitle = _("Untitled Page"); |
9548f380 | 232 | |
2c990ba6 KO |
233 | //still needed for wxCocoa?? |
234 | /* | |
235 | int width, height; | |
3ca7ba74 | 236 | wxSize sizeInstance; |
9548f380 | 237 | if (size.x == wxDefaultCoord || size.y == wxDefaultCoord) |
3ca7ba74 RD |
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 | } | |
9548f380 | 248 | */ |
3ca7ba74 | 249 | // now create and attach WebKit view... |
2c990ba6 KO |
250 | #ifdef __WXCOCOA__ |
251 | wxControl::Create(parent, m_windowID, pos, sizeInstance, style , validator , name); | |
3ca7ba74 | 252 | SetSize(pos.x, pos.y, sizeInstance.x, sizeInstance.y); |
9548f380 | 253 | |
2c990ba6 KO |
254 | wxTopLevelWindowCocoa *topWin = wxDynamicCast(this, wxTopLevelWindowCocoa); |
255 | NSWindow* nsWin = topWin->GetNSWindow(); | |
3ca7ba74 | 256 | NSRect rect; |
2c990ba6 KO |
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; | |
daef4689 | 269 | wxControl::Create(parent, winID, pos, size, style , validator , name); |
637013eb | 270 | m_peer = new wxMacControl(this); |
2c990ba6 | 271 | WebInitForCarbon(); |
f8405d6e | 272 | HIWebViewCreate( m_peer->GetControlRefAddr() ); |
9548f380 | 273 | |
f8405d6e | 274 | m_webView = (WebView*) HIWebViewGetWebView( m_peer->GetControlRef() ); |
448f8f12 | 275 | |
14f21d6d | 276 | MacPostControlCreate(pos, size); |
9548f380 | 277 | HIViewSetVisible( m_peer->GetControlRef(), true ); |
5b8f917c | 278 | [m_webView setHidden:false]; |
b384c0fb | 279 | #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3 |
daef4689 | 280 | if ( UMAGetSystemVersion() >= 0x1030 ) |
448f8f12 | 281 | HIViewChangeFeatures( m_peer->GetControlRef() , kHIViewIsOpaque , 0 ) ; |
b384c0fb | 282 | #endif |
448f8f12 KO |
283 | InstallControlEventHandler( m_peer->GetControlRef() , GetwxWebKitCtrlEventHandlerUPP(), |
284 | GetEventTypeCount(eventList), eventList, this, | |
285 | (EventHandlerRef *)&m_webKitCtrlEventHandler); | |
286 | ||
2c990ba6 KO |
287 | #endif |
288 | ||
289 | // Register event listener interfaces | |
448f8f12 | 290 | MyFrameLoadMonitor* myFrameLoadMonitor = [[MyFrameLoadMonitor alloc] initWithWxWindow: this]; |
3ca7ba74 | 291 | [m_webView setFrameLoadDelegate:myFrameLoadMonitor]; |
448f8f12 KO |
292 | |
293 | // this is used to veto page loads, etc. | |
294 | MyPolicyDelegate* myPolicyDelegate = [[MyPolicyDelegate alloc] initWithWxWindow: this]; | |
295 | [m_webView setPolicyDelegate:myPolicyDelegate]; | |
296 | ||
2c990ba6 KO |
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; | |
9548f380 | 314 | |
7eb8c6ee | 315 | [[m_webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:wxNSStringWithWxString(url)]]]; |
2c990ba6 KO |
316 | |
317 | m_currentURL = url; | |
318 | } | |
319 | ||
320 | bool wxWebKitCtrl::CanGoBack(){ | |
321 | if ( !m_webView ) | |
322 | return false; | |
9548f380 | 323 | |
2c990ba6 KO |
324 | return [m_webView canGoBack]; |
325 | } | |
326 | ||
327 | bool wxWebKitCtrl::CanGoForward(){ | |
328 | if ( !m_webView ) | |
329 | return false; | |
9548f380 | 330 | |
2c990ba6 KO |
331 | return [m_webView canGoForward]; |
332 | } | |
333 | ||
334 | bool wxWebKitCtrl::GoBack(){ | |
335 | if ( !m_webView ) | |
336 | return false; | |
9548f380 | 337 | |
8f3b30d5 KO |
338 | bool result = [(WebView*)m_webView goBack]; |
339 | return result; | |
2c990ba6 KO |
340 | } |
341 | ||
9548f380 | 342 | bool wxWebKitCtrl::GoForward(){ |
2c990ba6 KO |
343 | if ( !m_webView ) |
344 | return false; | |
9548f380 | 345 | |
8f3b30d5 KO |
346 | bool result = [(WebView*)m_webView goForward]; |
347 | return result; | |
2c990ba6 KO |
348 | } |
349 | ||
9548f380 | 350 | void wxWebKitCtrl::Reload(){ |
2c990ba6 KO |
351 | if ( !m_webView ) |
352 | return; | |
9548f380 | 353 | |
2c990ba6 KO |
354 | [[m_webView mainFrame] reload]; |
355 | } | |
356 | ||
357 | void wxWebKitCtrl::Stop(){ | |
358 | if ( !m_webView ) | |
359 | return; | |
9548f380 | 360 | |
2c990ba6 KO |
361 | [[m_webView mainFrame] stopLoading]; |
362 | } | |
363 | ||
364 | bool wxWebKitCtrl::CanGetPageSource(){ | |
365 | if ( !m_webView ) | |
14f21d6d | 366 | return false; |
9548f380 | 367 | |
2c990ba6 KO |
368 | WebDataSource* dataSource = [[m_webView mainFrame] dataSource]; |
369 | return ( [[dataSource representation] canProvideDocumentSource] ); | |
370 | } | |
371 | ||
372 | wxString wxWebKitCtrl::GetPageSource(){ | |
9548f380 | 373 | |
2c990ba6 KO |
374 | if (CanGetPageSource()){ |
375 | WebDataSource* dataSource = [[m_webView mainFrame] dataSource]; | |
376 | return wxStringWithNSString( [[dataSource representation] documentSource] ); | |
377 | } | |
9548f380 WS |
378 | |
379 | return wxEmptyString; | |
2c990ba6 KO |
380 | } |
381 | ||
448f8f12 KO |
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){ | |
2c990ba6 KO |
427 | if ( !m_webView ) |
428 | return; | |
9548f380 | 429 | |
2d837688 | 430 | [[m_webView mainFrame] loadHTMLString:(NSString*)wxNSStringWithWxString( source ) baseURL:[NSURL URLWithString:wxNSStringWithWxString( baseUrl )]]; |
448f8f12 KO |
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; | |
2c990ba6 KO |
504 | } |
505 | ||
005198fa | 506 | void wxWebKitCtrl::OnSize(wxSizeEvent &event){ |
39104bc1 KO |
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. | |
daef4689 VZ |
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 | ||
daef4689 VZ |
515 | wxWindow* tlw = MacGetTopLevelWindow(); |
516 | ||
448f8f12 KO |
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 | ||
daef4689 VZ |
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. ;) | |
448f8f12 | 528 | if (GetParent() == tlw){ |
daef4689 VZ |
529 | return; |
530 | } | |
531 | ||
448f8f12 KO |
532 | int x = GetPosition().x; |
533 | int y = GetPosition().y; | |
534 | ||
535 | HIRect rect; | |
536 | rect.origin.x = x; | |
537 | rect.origin.y = y; | |
daef4689 | 538 | |
448f8f12 KO |
539 | #if DEBUG_WEBKIT_SIZING |
540 | printf("Before conversion, origin is: x = %d, y = %d\n", x, y); | |
541 | #endif | |
daef4689 | 542 | |
448f8f12 KO |
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. | |
959d1785 | 547 | HIViewConvertRect (&rect, m_peer->GetControlRef(), |
448f8f12 | 548 | HIViewGetRoot( (WindowRef) MacGetTopLevelWindowRef() ) ); |
daef4689 | 549 | |
448f8f12 KO |
550 | x = (int)rect.origin.x; |
551 | y = (int)rect.origin.y; | |
daef4689 | 552 | |
448f8f12 KO |
553 | #if DEBUG_WEBKIT_SIZING |
554 | printf("Moving Cocoa frame origin to: x = %d, y = %d\n", x, y); | |
555 | #endif | |
daef4689 VZ |
556 | |
557 | if (tlw){ | |
558 | //flip the y coordinate to convert to Cocoa coordinates | |
daef4689 VZ |
559 | y = tlw->GetSize().y - ((GetSize().y) + y); |
560 | } | |
561 | ||
448f8f12 KO |
562 | #if DEBUG_WEBKIT_SIZING |
563 | printf("y = %d after flipping value\n", y); | |
564 | #endif | |
9548f380 | 565 | |
448f8f12 KO |
566 | frame.origin.x = x; |
567 | frame.origin.y = y; | |
568 | [m_webView setFrame:frame]; | |
569 | ||
fe3fc02e | 570 | if (IsShown()) |
14753ffe | 571 | [(WebView*)m_webView display]; |
005198fa KO |
572 | event.Skip(); |
573 | } | |
2c990ba6 | 574 | |
14f21d6d | 575 | void wxWebKitCtrl::MacVisibilityChanged(){ |
f8405d6e | 576 | bool isHidden = !IsControlVisible( m_peer->GetControlRef()); |
fe3fc02e | 577 | if (!isHidden) |
14753ffe | 578 | [(WebView*)m_webView display]; |
9548f380 | 579 | |
14f21d6d KO |
580 | [m_webView setHidden:isHidden]; |
581 | } | |
582 | ||
2c990ba6 KO |
583 | //------------------------------------------------------------ |
584 | // Listener interfaces | |
585 | //------------------------------------------------------------ | |
586 | ||
587 | @implementation MyFrameLoadMonitor | |
588 | ||
448f8f12 | 589 | - initWithWxWindow: (wxWebKitCtrl*)inWindow |
2c990ba6 KO |
590 | { |
591 | [super init]; | |
3ca7ba74 | 592 | webKitWindow = inWindow; // non retained |
2c990ba6 KO |
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]){ | |
fba8e95e | 610 | NSString *url = [[[[frame dataSource] request] URL] absoluteString]; |
2c990ba6 KO |
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]){ | |
fba8e95e | 621 | NSString *url = [[[[frame dataSource] request] URL] absoluteString]; |
2c990ba6 KO |
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]){ | |
fba8e95e | 632 | NSString *url = [[[[frame dataSource] request] URL] absoluteString]; |
2c990ba6 KO |
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]){ | |
448f8f12 | 654 | webKitWindow->SetPageTitle(wxStringWithNSString( title )); |
2c990ba6 KO |
655 | } |
656 | } | |
3ca7ba74 | 657 | @end |
5b8f917c | 658 | |
448f8f12 KO |
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 | ||
948f6c6e | 690 | #endif //wxUSE_WEBKIT |