]> git.saurik.com Git - wxWidgets.git/blob - src/html/htmlctrl/webkit/webkit.mm
adb86342ff202e5147157df251482d51e1d43a64
[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, y;
261 while(parent != NULL)
262 {
263 if( parent->IsKindOf( CLASSINFO( wxNotebook ) ) ){
264 NSRect bounds = [m_webView frame];
265 bounds.origin.x += parent->GetPosition().x;
266 bounds.origin.y += 18;
267 [m_webView setFrame:bounds];
268 break;
269 }
270 parent = parent->GetParent();
271 }
272
273 [m_webView display];
274 event.Skip();
275 }
276
277 void wxWebKitCtrl::MacVisibilityChanged(){
278 bool isHidden = !IsControlVisible( (HIViewRef)m_macControl);
279 [m_webView setHidden:isHidden];
280 }
281
282 //------------------------------------------------------------
283 // Listener interfaces
284 //------------------------------------------------------------
285
286 @implementation MyFrameLoadMonitor
287
288 - initWithWxWindow: (wxWindow*)inWindow
289 {
290 [super init];
291 webKitWindow = inWindow; // non retained
292 return self;
293 }
294
295 - (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame
296 {
297 if (frame == [sender mainFrame]){
298 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
299 wxWebKitStateChangedEvent thisEvent(webKitWindow);
300 thisEvent.SetState(wxWEBKIT_STATE_NEGOTIATING);
301 thisEvent.SetURL( wxStringWithNSString( url ) );
302 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
303 }
304 }
305
306 - (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame
307 {
308 if (frame == [sender mainFrame]){
309 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
310 wxWebKitStateChangedEvent thisEvent(webKitWindow);
311 thisEvent.SetState(wxWEBKIT_STATE_TRANSFERRING);
312 thisEvent.SetURL( wxStringWithNSString( url ) );
313 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
314 }
315 }
316
317 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
318 {
319 if (frame == [sender mainFrame]){
320 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
321 wxWebKitStateChangedEvent thisEvent(webKitWindow);
322 thisEvent.SetState(wxWEBKIT_STATE_STOP);
323 thisEvent.SetURL( wxStringWithNSString( url ) );
324 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
325 }
326 }
327
328 - (void)webView:(WebView *)sender didFailLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
329 {
330 if (frame == [sender mainFrame]){
331 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
332 wxWebKitStateChangedEvent thisEvent(webKitWindow);
333 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
334 thisEvent.SetURL( wxStringWithNSString( url ) );
335 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
336 }
337 }
338
339 - (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
340 {
341 if (frame == [sender mainFrame]){
342 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
343 wxWebKitStateChangedEvent thisEvent(webKitWindow);
344 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
345 thisEvent.SetURL( wxStringWithNSString( url ) );
346 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
347 }
348 }
349
350 - (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
351 {
352 if (frame == [sender mainFrame]){
353 webKitWindow->SetTitle(wxStringWithNSString( title ));
354 }
355 }
356 @end