]> git.saurik.com Git - wxWidgets.git/blob - src/html/htmlctrl/webkit/webkit.mm
Test commit
[wxWidgets.git] / src / html / htmlctrl / webkit / webkit.mm
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 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14 #include "wx/splitter.h"
15
16 #ifndef WX_PRECOMP
17 #include "wx/wx.h"
18 #endif
19
20 #if wxUSE_WEBKIT
21
22 #ifdef __WXCOCOA__
23 #include "wx/cocoa/autorelease.h"
24 #else
25 #include "wx/mac/uma.h"
26 #include <Carbon/Carbon.h>
27 #include <WebKit/WebKit.h>
28 #include <WebKit/HIWebView.h>
29 #include <WebKit/CarbonUtils.h>
30 #endif
31
32 #include "wx/html/webkit.h"
33 #include "wx/notebook.h"
34
35
36 // ----------------------------------------------------------------------------
37 // macros
38 // ----------------------------------------------------------------------------
39
40 IMPLEMENT_DYNAMIC_CLASS(wxWebKitCtrl, wxControl)
41
42 BEGIN_EVENT_TABLE(wxWebKitCtrl, wxControl)
43 EVT_SIZE(wxWebKitCtrl::OnSize)
44 END_EVENT_TABLE()
45
46 // ----------------------------------------------------------------------------
47 // wxWebKit Events
48 // ----------------------------------------------------------------------------
49
50 IMPLEMENT_DYNAMIC_CLASS( wxWebKitStateChangedEvent, wxCommandEvent )
51
52 DEFINE_EVENT_TYPE( wxEVT_WEBKIT_STATE_CHANGED )
53
54 wxWebKitStateChangedEvent::wxWebKitStateChangedEvent( wxWindow* win )
55 {
56 SetEventType( wxEVT_WEBKIT_STATE_CHANGED);
57 SetEventObject( win );
58 SetId(win->GetId());
59 }
60
61 //---------------------------------------------------------
62 // helper functions for NSString<->wxString conversion
63 //---------------------------------------------------------
64
65 inline wxString wxStringWithNSString(NSString *nsstring)
66 {
67 #if wxUSE_UNICODE
68 return wxString([nsstring UTF8String], wxConvUTF8);
69 #else
70 return wxString([nsstring lossyCString]);
71 #endif // wxUSE_UNICODE
72 }
73
74 inline NSString* wxNSStringWithWxString(const wxString &wxstring)
75 {
76 #if wxUSE_UNICODE
77 return [NSString stringWithUTF8String: wxstring.mb_str(wxConvUTF8)];
78 #else
79 return [NSString stringWithCString: wxstring.c_str() length:wxstring.Len()];
80 #endif // wxUSE_UNICODE
81 }
82
83 @interface MyFrameLoadMonitor : NSObject
84 {
85 wxWindow* webKitWindow;
86 }
87
88 - initWithWxWindow: (wxWindow*)inWindow;
89
90 @end
91
92 // ----------------------------------------------------------------------------
93 // creation/destruction
94 // ----------------------------------------------------------------------------
95
96 bool wxWebKitCtrl::Create(wxWindow *parent,
97 wxWindowID winID,
98 const wxString& strURL,
99 const wxPoint& pos,
100 const wxSize& size, long style,
101 const wxValidator& validator,
102 const wxString& name)
103 {
104
105 m_currentURL = strURL;
106 //m_pageTitle = _("Untitled Page");
107
108 //still needed for wxCocoa??
109 /*
110 int width, height;
111 wxSize sizeInstance;
112 if (size.x == wxDefaultCoord || size.y == wxDefaultCoord)
113 {
114 m_parent->GetClientSize(&width, &height);
115 sizeInstance.x = width;
116 sizeInstance.y = height;
117 }
118 else
119 {
120 sizeInstance.x = size.x;
121 sizeInstance.y = size.y;
122 }
123 */
124 // now create and attach WebKit view...
125 #ifdef __WXCOCOA__
126 wxControl::Create(parent, m_windowID, pos, sizeInstance, style , validator , name);
127 SetSize(pos.x, pos.y, sizeInstance.x, sizeInstance.y);
128
129 wxTopLevelWindowCocoa *topWin = wxDynamicCast(this, wxTopLevelWindowCocoa);
130 NSWindow* nsWin = topWin->GetNSWindow();
131 NSRect rect;
132 rect.origin.x = pos.x;
133 rect.origin.y = pos.y;
134 rect.size.width = sizeInstance.x;
135 rect.size.height = sizeInstance.y;
136 m_webView = (WebView*)[[WebView alloc] initWithFrame:rect frameName:@"webkitFrame" groupName:@"webkitGroup"];
137 SetNSView(m_webView);
138 [m_cocoaNSView release];
139
140 if(m_parent) m_parent->CocoaAddChild(this);
141 SetInitialFrameRect(pos,sizeInstance);
142 #else
143 m_macIsUserPane = false;
144 wxControl::Create(parent, winID, pos, size, style , validator , name);
145 m_peer = new wxMacControl(this);
146 WebInitForCarbon();
147 HIWebViewCreate( m_peer->GetControlRefAddr() );
148
149 m_webView = (WebView*) HIWebViewGetWebView( m_peer->GetControlRef() );
150 MacPostControlCreate(pos, size);
151 HIViewSetVisible( m_peer->GetControlRef(), true );
152 [m_webView setHidden:false];
153 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
154 if ( UMAGetSystemVersion() >= 0x1030 )
155 HIViewChangeFeatures( m_peer->GetControlRef() , kHIViewIsOpaque , 0 ) ;
156 #endif
157 #endif
158
159 // Register event listener interfaces
160 MyFrameLoadMonitor* myFrameLoadMonitor = [[MyFrameLoadMonitor alloc] initWithWxWindow: (wxWindow*)this];
161 [m_webView setFrameLoadDelegate:myFrameLoadMonitor];
162
163 LoadURL(m_currentURL);
164 return true;
165 }
166
167 wxWebKitCtrl::~wxWebKitCtrl()
168 {
169
170 }
171
172 // ----------------------------------------------------------------------------
173 // public methods
174 // ----------------------------------------------------------------------------
175
176 void wxWebKitCtrl::LoadURL(const wxString &url)
177 {
178 if( !m_webView )
179 return;
180
181 [[m_webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:wxNSStringWithWxString(url)]]];
182
183 m_currentURL = url;
184 }
185
186 bool wxWebKitCtrl::CanGoBack(){
187 if ( !m_webView )
188 return false;
189
190 return [m_webView canGoBack];
191 }
192
193 bool wxWebKitCtrl::CanGoForward(){
194 if ( !m_webView )
195 return false;
196
197 return [m_webView canGoForward];
198 }
199
200 bool wxWebKitCtrl::GoBack(){
201 if ( !m_webView )
202 return false;
203
204 bool result = [(WebView*)m_webView goBack];
205 return result;
206 }
207
208 bool wxWebKitCtrl::GoForward(){
209 if ( !m_webView )
210 return false;
211
212 bool result = [(WebView*)m_webView goForward];
213 return result;
214 }
215
216 void wxWebKitCtrl::Reload(){
217 if ( !m_webView )
218 return;
219
220 [[m_webView mainFrame] reload];
221 }
222
223 void wxWebKitCtrl::Stop(){
224 if ( !m_webView )
225 return;
226
227 [[m_webView mainFrame] stopLoading];
228 }
229
230 bool wxWebKitCtrl::CanGetPageSource(){
231 if ( !m_webView )
232 return false;
233
234 WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
235 return ( [[dataSource representation] canProvideDocumentSource] );
236 }
237
238 wxString wxWebKitCtrl::GetPageSource(){
239
240 if (CanGetPageSource()){
241 WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
242 return wxStringWithNSString( [[dataSource representation] documentSource] );
243 }
244
245 return wxEmptyString;
246 }
247
248 void wxWebKitCtrl::SetPageSource(wxString& source, const wxString& baseUrl){
249 if ( !m_webView )
250 return;
251
252 if (CanGetPageSource()){
253 [[m_webView mainFrame] loadHTMLString:(NSString*)wxNSStringWithWxString( source ) baseURL:[NSURL URLWithString:wxNSStringWithWxString( baseUrl )]];
254 }
255
256 }
257
258 void wxWebKitCtrl::OnSize(wxSizeEvent &event){
259 // This is a nasty hack because WebKit seems to lose its position when it is embedded
260 // in a control that is not itself the content view for a TLW.
261 // I put it in OnSize because these calcs are not perfect, and in fact are basically
262 // guesses based on reverse engineering, so it's best to give people the option of
263 // overriding OnSize with their own calcs if need be.
264 // I also left some test debugging print statements as a convenience if a(nother)
265 // problem crops up.
266
267 // Let's hope that Tiger fixes this mess...
268
269 int x, y;
270 x = 0;
271 y = 0;
272
273 wxWindow* parent = GetParent();
274
275 wxWindow* tlw = MacGetTopLevelWindow();
276
277 // This must be the case that Apple tested with, because well, in this one case
278 // we don't need to do anything! It just works. ;)
279 if (parent == tlw){
280 return;
281 }
282
283 while(parent != NULL)
284 {
285 if ( parent->IsKindOf( CLASSINFO( wxSplitterWindow ) ) && GetParent()->IsKindOf( CLASSINFO( wxSplitterWindow ) ) ){
286 // When parent is not a wxSplitterWindow, we can rely on it's GetPosition() to give us the correct
287 // coordinates, but when the parent is a wxSplitterWindow, we need to manually calculate
288 // the sash position of it and any parent wxSplitterWindows into the webkit's position.
289 wxSplitterWindow* splitter;
290 splitter = dynamic_cast<wxSplitterWindow*>(parent);
291 if (splitter->GetSplitMode() == wxSPLIT_HORIZONTAL){
292 if (splitter->GetPosition().y > 0)
293 y += splitter->GetPosition().y;
294
295 if (splitter->GetSashSize() > 0)
296 y += splitter->GetSashSize();
297
298 if (splitter->GetSashPosition() > 0)
299 y += splitter->GetSashPosition();
300 }
301 else{
302 if (splitter->GetPosition().x > 0)
303 x += splitter->GetPosition().x;
304
305 if (splitter->GetSashSize() > 0)
306 x += splitter->GetSashSize();
307
308 if (splitter->GetSashPosition() > 0)
309 x += splitter->GetSashPosition();
310 }
311 }
312 else{
313 if (!parent->IsTopLevel()) {
314 //printf("Parent: %s\n", parent->GetClassInfo()->GetClassName());
315 int plusx = 0;
316 plusx = parent->GetClientAreaOrigin().x + parent->GetPosition().x;
317 if (plusx > 0){
318 x += plusx;
319 //printf("Parent: %s Added x: %d\n", parent->GetClassInfo()->GetClassName(), parent->GetClientAreaOrigin().x + parent->GetPosition().x);
320 }
321
322 int plusy = 0;
323 plusy = parent->GetClientAreaOrigin().y + parent->GetPosition().y;
324 if (plusy > 0){
325 y += plusy;
326 //printf("Parent: %s Added y: %d\n", parent->GetClassInfo()->GetClassName(), parent->GetClientAreaOrigin().y + parent->GetPosition().y);
327 }
328 else{
329 //printf("Parent: %s Origin: %d Position:%d\n", parent->GetClassInfo()->GetClassName(), parent->GetClientAreaOrigin().y, parent->GetPosition().y);
330 }
331
332 }
333 else{
334 //
335 x += parent->GetClientAreaOrigin().x;
336 // calculate the title bar height (26 pixels) into the top offset.
337 // This becomes important later when we must flip the y coordinate
338 // to convert to Cocoa's coordinate system.
339 y += parent->GetClientAreaOrigin().y += 26;
340 //printf("x: %d, y:%d\n", x, y);
341 }
342 //we still need to add the y, because we have to convert/flip coordinates for Cocoa
343
344 if ( parent->IsKindOf( CLASSINFO( wxNotebook ) ) ){
345 //Not sure why calcs are off in this one scenario...
346 y -= 4;
347 //printf("x: %d, y:%d\n", x, y);
348 }
349
350 if (parent->IsKindOf( CLASSINFO( wxPanel ) ) ){
351 // Another strange case. Adding a wxPanel to the parent heirarchy
352 // causes wxWebKitCtrl's Cocoa y origin to be 4 pixels off
353 // for some reason, even if the panel has a position and origin of 0.
354 // This corrects that. Man, I wish I could debug Carbon/HIWebView!! ;)
355 y -= 4;
356 }
357 }
358
359 parent = parent->GetParent();
360 }
361
362 // Tried using MacWindowToRootWindow both for wxWebKitCtrl and its parent,
363 // but coordinates were off by a significant amount.
364 // Am leaving the code here if anyone wants to play with it.
365
366 //int x2, y2 = 0;
367 //if (GetParent())
368 // GetParent()->MacWindowToRootWindow(&x2, &y2);
369 //printf("x = %d, y = %d\n", x, y);
370 //printf("x2 = %d, y2 = %d\n", x2, y2);
371 //x = x2;
372 //y = y2;
373
374 if (tlw){
375 //flip the y coordinate to convert to Cocoa coordinates
376 //printf("tlw y: %d, y: %d\n", tlw->GetSize().y, (GetSize().y + y));
377 y = tlw->GetSize().y - ((GetSize().y) + y);
378 }
379
380 //printf("Added to bounds x=%d, y=%d\n", x, y);
381 NSRect bounds = [m_webView frame];
382 bounds.origin.x = x;
383 bounds.origin.y = y;
384 [m_webView setFrame:bounds];
385
386 //printf("Carbon position x=%d, y=%d\n", GetPosition().x, GetPosition().y);
387 if (IsShown())
388 [(WebView*)m_webView display];
389 event.Skip();
390 }
391
392 void wxWebKitCtrl::MacVisibilityChanged(){
393 bool isHidden = !IsControlVisible( m_peer->GetControlRef());
394 if (!isHidden)
395 [(WebView*)m_webView display];
396
397 [m_webView setHidden:isHidden];
398 }
399
400 //------------------------------------------------------------
401 // Listener interfaces
402 //------------------------------------------------------------
403
404 @implementation MyFrameLoadMonitor
405
406 - initWithWxWindow: (wxWindow*)inWindow
407 {
408 [super init];
409 webKitWindow = inWindow; // non retained
410 return self;
411 }
412
413 - (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame
414 {
415 if (frame == [sender mainFrame]){
416 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
417 wxWebKitStateChangedEvent thisEvent(webKitWindow);
418 thisEvent.SetState(wxWEBKIT_STATE_NEGOTIATING);
419 thisEvent.SetURL( wxStringWithNSString( url ) );
420 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
421 }
422 }
423
424 - (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame
425 {
426 if (frame == [sender mainFrame]){
427 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
428 wxWebKitStateChangedEvent thisEvent(webKitWindow);
429 thisEvent.SetState(wxWEBKIT_STATE_TRANSFERRING);
430 thisEvent.SetURL( wxStringWithNSString( url ) );
431 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
432 }
433 }
434
435 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
436 {
437 if (frame == [sender mainFrame]){
438 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
439 wxWebKitStateChangedEvent thisEvent(webKitWindow);
440 thisEvent.SetState(wxWEBKIT_STATE_STOP);
441 thisEvent.SetURL( wxStringWithNSString( url ) );
442 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
443 }
444 }
445
446 - (void)webView:(WebView *)sender didFailLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
447 {
448 if (frame == [sender mainFrame]){
449 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
450 wxWebKitStateChangedEvent thisEvent(webKitWindow);
451 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
452 thisEvent.SetURL( wxStringWithNSString( url ) );
453 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
454 }
455 }
456
457 - (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
458 {
459 if (frame == [sender mainFrame]){
460 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
461 wxWebKitStateChangedEvent thisEvent(webKitWindow);
462 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
463 thisEvent.SetURL( wxStringWithNSString( url ) );
464 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
465 }
466 }
467
468 - (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
469 {
470 if (frame == [sender mainFrame]){
471 webKitWindow->SetLabel(wxStringWithNSString( title ));
472 }
473 }
474 @end
475
476 #endif //wxUSE_WEBKIT