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