]> git.saurik.com Git - wxWidgets.git/blob - src/html/htmlctrl/webkit/webkit.mm
added src/mac/carbon/display.cpp to project
[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/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 );
60 SetId(win->GetId());
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,
99 wxWindowID winID,
100 const wxString& strURL,
101 const wxPoint& pos,
102 const wxSize& size, long style,
103 const wxValidator& validator,
104 const wxString& name)
105 {
106
107 m_currentURL = strURL;
108 m_pageTitle = wxT("");
109
110 //still needed for wxCocoa??
111 /*
112 int width, height;
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...
127 #ifdef __WXCOCOA__
128 wxControl::Create(parent, m_windowID, pos, sizeInstance, style , validator , name);
129 SetSize(pos.x, pos.y, sizeInstance.x, sizeInstance.y);
130
131 wxTopLevelWindowCocoa *topWin = wxDynamicCast(this, wxTopLevelWindowCocoa);
132 NSWindow* nsWin = topWin->GetNSWindow();
133 NSRect rect;
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];
158 [m_webView setFrameLoadDelegate:myFrameLoadMonitor];
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
177 [[m_webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:wxNSStringWithWxString(url)]]];
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 void wxWebKitCtrl::OnSize(wxSizeEvent &event){
256 [m_webView display];
257 event.Skip();
258 }
259
260 //------------------------------------------------------------
261 // Listener interfaces
262 //------------------------------------------------------------
263
264 @implementation MyFrameLoadMonitor
265
266 - initWithWxWindow: (wxWindow*)inWindow
267 {
268 [super init];
269 webKitWindow = inWindow; // non retained
270 return self;
271 }
272
273 - (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame
274 {
275 if (frame == [sender mainFrame]){
276 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
277 wxWebKitStateChangedEvent thisEvent(webKitWindow);
278 thisEvent.SetState(wxWEBKIT_STATE_NEGOTIATING);
279 thisEvent.SetURL( wxStringWithNSString( url ) );
280 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
281 }
282 }
283
284 - (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame
285 {
286 if (frame == [sender mainFrame]){
287 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
288 wxWebKitStateChangedEvent thisEvent(webKitWindow);
289 thisEvent.SetState(wxWEBKIT_STATE_TRANSFERRING);
290 thisEvent.SetURL( wxStringWithNSString( url ) );
291 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
292 }
293 }
294
295 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
296 {
297 if (frame == [sender mainFrame]){
298 NSString *url = [[[[frame dataSource] request] URL] absoluteString];
299 wxWebKitStateChangedEvent thisEvent(webKitWindow);
300 thisEvent.SetState(wxWEBKIT_STATE_STOP);
301 thisEvent.SetURL( wxStringWithNSString( url ) );
302 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
303 }
304 }
305
306 - (void)webView:(WebView *)sender didFailLoadWithError:(NSError*) error forFrame:(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_FAILED);
312 thisEvent.SetURL( wxStringWithNSString( url ) );
313 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
314 }
315 }
316
317 - (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError*) error forFrame:(WebFrame *)frame
318 {
319 if (frame == [sender mainFrame]){
320 NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
321 wxWebKitStateChangedEvent thisEvent(webKitWindow);
322 thisEvent.SetState(wxWEBKIT_STATE_FAILED);
323 thisEvent.SetURL( wxStringWithNSString( url ) );
324 webKitWindow->GetEventHandler()->ProcessEvent( thisEvent );
325 }
326 }
327
328 - (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
329 {
330 if (frame == [sender mainFrame]){
331 webKitWindow->SetTitle(wxStringWithNSString( title ));
332 }
333 }
334 @end