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