]> git.saurik.com Git - wxWidgets.git/blob - src/html/htmlctrl/webkit/webkit.mm
Yet more fixes for wxNotebook compatibility...
[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
19 #ifndef WX_PRECOMP
20 #include "wx/wx.h"
21 #endif
22
23 #ifdef __WXCOCOA__
24 #include "wx/cocoa/autorelease.h"
25 #else
26 #include "wx/mac/uma.h"
27 #include <Carbon/Carbon.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 //#include "wx/html/wklisten.h"
35
36
37 // ----------------------------------------------------------------------------
38 // macros
39 // ----------------------------------------------------------------------------
40
41 #if !USE_SHARED_LIBRARY
42 IMPLEMENT_DYNAMIC_CLASS(wxWebKitCtrl, wxControl)
43 #endif
44
45 BEGIN_EVENT_TABLE(wxWebKitCtrl, wxControl)
46 EVT_SIZE(wxWebKitCtrl::OnSize)
47 END_EVENT_TABLE()
48
49 // ----------------------------------------------------------------------------
50 // wxWebKit Events
51 // ----------------------------------------------------------------------------
52
53 IMPLEMENT_DYNAMIC_CLASS( wxWebKitStateChangedEvent, wxCommandEvent )
54
55 DEFINE_EVENT_TYPE( wxEVT_WEBKIT_STATE_CHANGED )
56
57 wxWebKitStateChangedEvent::wxWebKitStateChangedEvent( wxWindow* win )
58 {
59 SetEventType( wxEVT_WEBKIT_STATE_CHANGED);
60 SetEventObject( win );
61 SetId(win->GetId());
62 }
63
64 //---------------------------------------------------------
65 // helper functions for NSString<->wxString conversion
66 //---------------------------------------------------------
67
68 inline wxString wxStringWithNSString(NSString *nsstring)
69 {
70 #if wxUSE_UNICODE
71 return wxString([nsstring UTF8String], wxConvUTF8);
72 #else
73 return wxString([nsstring lossyCString]);
74 #endif // wxUSE_UNICODE
75 }
76
77 inline NSString* wxNSStringWithWxString(const wxString &wxstring)
78 {
79 #if wxUSE_UNICODE
80 return [NSString stringWithUTF8String: wxstring.mb_str(wxConvUTF8)];
81 #else
82 return [NSString stringWithCString: wxstring.c_str() length:wxstring.Len()];
83 #endif // wxUSE_UNICODE
84 }
85
86 @interface MyFrameLoadMonitor : NSObject
87 {
88 wxWindow* webKitWindow;
89 }
90
91 - initWithWxWindow: (wxWindow*)inWindow;
92
93 @end
94
95 // ----------------------------------------------------------------------------
96 // creation/destruction
97 // ----------------------------------------------------------------------------
98
99 bool wxWebKitCtrl::Create(wxWindow *parent,
100 wxWindowID winID,
101 const wxString& strURL,
102 const wxPoint& pos,
103 const wxSize& size, long style,
104 const wxValidator& validator,
105 const wxString& name)
106 {
107
108 m_currentURL = strURL;
109 //m_pageTitle = _("Untitled Page");
110
111 //still needed for wxCocoa??
112 /*
113 int width, height;
114 wxSize sizeInstance;
115 if (size.x == -1 || size.y == -1)
116 {
117 m_parent->GetClientSize(&width, &height);
118 sizeInstance.x = width;
119 sizeInstance.y = height;
120 }
121 else
122 {
123 sizeInstance.x = size.x;
124 sizeInstance.y = size.y;
125 }
126 */
127 // now create and attach WebKit view...
128 #ifdef __WXCOCOA__
129 wxControl::Create(parent, m_windowID, pos, sizeInstance, style , validator , name);
130 SetSize(pos.x, pos.y, sizeInstance.x, sizeInstance.y);
131
132 wxTopLevelWindowCocoa *topWin = wxDynamicCast(this, wxTopLevelWindowCocoa);
133 NSWindow* nsWin = topWin->GetNSWindow();
134 NSRect rect;
135 rect.origin.x = pos.x;
136 rect.origin.y = pos.y;
137 rect.size.width = sizeInstance.x;
138 rect.size.height = sizeInstance.y;
139 m_webView = (WebView*)[[WebView alloc] initWithFrame:rect frameName:@"webkitFrame" groupName:@"webkitGroup"];
140 SetNSView(m_webView);
141 [m_cocoaNSView release];
142
143 if(m_parent) m_parent->CocoaAddChild(this);
144 SetInitialFrameRect(pos,sizeInstance);
145 #else
146 m_macIsUserPane = false;
147 wxControl::Create(parent, m_windowID, pos, size, style , validator , name);
148 WebInitForCarbon();
149 HIWebViewCreate( (HIViewRef*) &m_macControl );
150
151 m_webView = (WebView*) HIWebViewGetWebView( (HIViewRef) m_macControl );
152 MacPostControlCreate(pos, size);
153
154 HIViewSetVisible( (HIViewRef) m_macControl, true );
155 #endif
156
157 // Register event listener interfaces
158 MyFrameLoadMonitor* myFrameLoadMonitor = [[MyFrameLoadMonitor alloc] initWithWxWindow: (wxWindow*)this];
159 [m_webView setFrameLoadDelegate:myFrameLoadMonitor];
160
161 LoadURL(m_currentURL);
162 return true;
163 }
164
165 wxWebKitCtrl::~wxWebKitCtrl()
166 {
167
168 }
169
170 // ----------------------------------------------------------------------------
171 // public methods
172 // ----------------------------------------------------------------------------
173
174 void wxWebKitCtrl::LoadURL(const wxString &url)
175 {
176 if( !m_webView )
177 return;
178
179 [[m_webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:wxNSStringWithWxString(url)]]];
180
181 m_currentURL = url;
182 }
183
184 bool wxWebKitCtrl::CanGoBack(){
185 if ( !m_webView )
186 return false;
187
188 return [m_webView canGoBack];
189 }
190
191 bool wxWebKitCtrl::CanGoForward(){
192 if ( !m_webView )
193 return false;
194
195 return [m_webView canGoForward];
196 }
197
198 bool wxWebKitCtrl::GoBack(){
199 if ( !m_webView )
200 return false;
201
202 [m_webView goBack];
203 return true;
204 }
205
206 bool wxWebKitCtrl::GoForward(){
207 if ( !m_webView )
208 return false;
209
210 [m_webView goForward];
211 return true;
212 }
213
214 void wxWebKitCtrl::Reload(){
215 if ( !m_webView )
216 return;
217
218 [[m_webView mainFrame] reload];
219 }
220
221 void wxWebKitCtrl::Stop(){
222 if ( !m_webView )
223 return;
224
225 [[m_webView mainFrame] stopLoading];
226 }
227
228 bool wxWebKitCtrl::CanGetPageSource(){
229 if ( !m_webView )
230 return false;
231
232 WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
233 return ( [[dataSource representation] canProvideDocumentSource] );
234 }
235
236 wxString wxWebKitCtrl::GetPageSource(){
237 if ( !m_webView )
238 return wxT("");
239
240 if (CanGetPageSource()){
241 WebDataSource* dataSource = [[m_webView mainFrame] dataSource];
242 return wxStringWithNSString( [[dataSource representation] documentSource] );
243 }
244
245 }
246
247 void wxWebKitCtrl::SetPageSource(wxString& source, const wxString& baseUrl){
248 if ( !m_webView )
249 return;
250
251 if (CanGetPageSource()){
252 [[m_webView mainFrame] loadHTMLString:(NSString*)wxNSStringWithWxString( source ) baseURL:[NSURL URLWithString:wxNSStringWithWxString( baseUrl )]];
253 }
254
255 }
256
257 void wxWebKitCtrl::OnSize(wxSizeEvent &event){
258 wxWindow* parent = GetParent();
259 bool inNotebook = false;
260 int x = 0;
261 int y = 18;
262 while(parent != NULL)
263 {
264 x += parent->GetPosition().x;
265 y += parent->GetPosition().y;
266 if( parent->IsKindOf( CLASSINFO( wxNotebook ) ) ){
267 inNotebook = true;
268 break;
269 }
270 parent = parent->GetParent();
271 }
272
273 if (inNotebook){
274 NSRect bounds = [m_webView frame];
275 bounds.origin.x += x;
276 bounds.origin.y += y;
277 [m_webView setFrame:bounds];
278 }
279
280 [m_webView display];
281 event.Skip();
282 }
283
284 void wxWebKitCtrl::MacVisibilityChanged(){
285 bool isHidden = !IsControlVisible( (HIViewRef)m_macControl);
286 [m_webView setHidden:isHidden];
287 }
288
289 //------------------------------------------------------------
290 // Listener interfaces
291 //------------------------------------------------------------
292
293 @implementation MyFrameLoadMonitor
294
295 - initWithWxWindow: (wxWindow*)inWindow
296 {
297 [super init];
298 webKitWindow = inWindow; // non retained
299 return self;
300 }
301
302 - (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame
303 {
304 if (frame == [sender mainFrame]){
305 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
306 wxWebKitStateChangedEvent thisEvent(webKitWindow);
307 thisEvent.SetState(wxWEBKIT_STATE_NEGOTIATING);
308 thisEvent.SetURL( wxStringWithNSString( url ) );
309 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
310 }
311 }
312
313 - (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame
314 {
315 if (frame == [sender mainFrame]){
316 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
317 wxWebKitStateChangedEvent thisEvent(webKitWindow);
318 thisEvent.SetState(wxWEBKIT_STATE_TRANSFERRING);
319 thisEvent.SetURL( wxStringWithNSString( url ) );
320 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
321 }
322 }
323
324 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
325 {
326 if (frame == [sender mainFrame]){
327 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
328 wxWebKitStateChangedEvent thisEvent(webKitWindow);
329 thisEvent.SetState(wxWEBKIT_STATE_STOP);
330 thisEvent.SetURL( wxStringWithNSString( url ) );
331 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
332 }
333 }
334
335 - (void)webView:(WebView *)sender didFailLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
336 {
337 if (frame == [sender mainFrame]){
338 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
339 wxWebKitStateChangedEvent thisEvent(webKitWindow);
340 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
341 thisEvent.SetURL( wxStringWithNSString( url ) );
342 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
343 }
344 }
345
346 - (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
347 {
348 if (frame == [sender mainFrame]){
349 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
350 wxWebKitStateChangedEvent thisEvent(webKitWindow);
351 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
352 thisEvent.SetURL( wxStringWithNSString( url ) );
353 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
354 }
355 }
356
357 - (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
358 {
359 if (frame == [sender mainFrame]){
360 webKitWindow->SetTitle(wxStringWithNSString( title ));
361 }
362 }
363 @end