Move the wxWebFileProtocolHandler from the IE backend to the common source, add the...
[wxWidgets.git] / src / common / webview.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: webview.cpp
3 // Purpose: Common interface and events for web view component
4 // Author: Marianne Gagnon
5 // Id: $Id$
6 // Copyright: (c) 2010 Marianne Gagnon
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #if wxUSE_WEB
14
15 #if defined(__BORLANDC__)
16 #pragma hdrstop
17 #endif
18
19 #include "wx/webview.h"
20
21 #include "wx/osx/webview_webkit.h"
22 #include "wx/gtk/webview_webkit.h"
23 #include "wx/msw/webview_ie.h"
24 #include "wx/filesys.h"
25 #include "wx/tokenzr.h"
26
27 // DLL options compatibility check:
28 #include "wx/app.h"
29 WX_CHECK_BUILD_OPTIONS("wxWEB")
30
31 extern WXDLLIMPEXP_DATA_WEB(const char) wxWebViewNameStr[] = "wxWebView";
32 extern WXDLLIMPEXP_DATA_WEB(const char) wxWebViewDefaultURLStr[] = "about:blank";
33
34 IMPLEMENT_DYNAMIC_CLASS(wxWebNavigationEvent, wxCommandEvent)
35
36 wxDEFINE_EVENT( wxEVT_COMMAND_WEB_VIEW_NAVIGATING, wxWebNavigationEvent );
37 wxDEFINE_EVENT( wxEVT_COMMAND_WEB_VIEW_NAVIGATED, wxWebNavigationEvent );
38 wxDEFINE_EVENT( wxEVT_COMMAND_WEB_VIEW_LOADED, wxWebNavigationEvent );
39 wxDEFINE_EVENT( wxEVT_COMMAND_WEB_VIEW_ERROR, wxWebNavigationEvent );
40 wxDEFINE_EVENT( wxEVT_COMMAND_WEB_VIEW_NEWWINDOW, wxWebNavigationEvent );
41
42 //Taken from wx/filesys.cpp
43 static wxString EscapeFileNameCharsInURL(const char *in)
44 {
45 wxString s;
46
47 for ( const unsigned char *p = (const unsigned char*)in; *p; ++p )
48 {
49 const unsigned char c = *p;
50
51 if ( c == '/' || c == '-' || c == '.' || c == '_' || c == '~' ||
52 (c >= '0' && c <= '9') ||
53 (c >= 'a' && c <= 'z') ||
54 (c >= 'A' && c <= 'Z') )
55 {
56 s << c;
57 }
58 else
59 {
60 s << wxString::Format("%%%02x", c);
61 }
62 }
63
64 return s;
65 }
66
67 wxWebFileProtocolHandler::wxWebFileProtocolHandler()
68 {
69 m_protocol = "test";
70 m_fileSystem = new wxFileSystem();
71 }
72
73 wxFSFile* wxWebFileProtocolHandler::GetFile(const wxString &uri)
74 {
75 size_t pos = uri.find('?');
76 //There is no query string so we can load the file directly
77 if(pos == wxString::npos)
78 {
79 size_t doubleslash = uri.find("//");
80 //The path is incorrectly formed without // after the first protocol
81 if(doubleslash == wxString::npos)
82 return NULL;
83
84 wxString fspath = "file:" +
85 EscapeFileNameCharsInURL(uri.substr(doubleslash + 2));
86 return m_fileSystem->OpenFile(fspath);
87 }
88 //Otherwise we have a query string of some kind that we need to extract
89 else{
90 //First we extract the query string, this should have two parameters,
91 //protocol=type and path=path
92 wxString query = uri.substr(pos + 1), protocol, path;
93 //We also trim the query off the end as we handle it alone
94 wxString lefturi = uri.substr(0, pos);
95 wxStringTokenizer tokenizer(query, ";");
96 while(tokenizer.HasMoreTokens() && (protocol == "" || path == ""))
97 {
98 wxString token = tokenizer.GetNextToken();
99 if(token.substr(0, 9) == "protocol=")
100 {
101 protocol = token.substr(9);
102 }
103 else if(token.substr(0, 5) == "path=")
104 {
105 path = token.substr(5);
106 }
107 }
108 if(protocol == "" || path == "")
109 return NULL;
110
111 //We now have the path and the protocol and so can format a correct uri
112 //to pass to wxFileSystem to get a wxFSFile
113 size_t doubleslash = uri.find("//");
114 //The path is incorrectly formed without // after the first protocol
115 if(doubleslash == wxString::npos)
116 return NULL;
117
118 wxString fspath = "file:" +
119 EscapeFileNameCharsInURL(lefturi.substr(doubleslash + 2))
120 + "#" + protocol +":" + path;
121 return m_fileSystem->OpenFile(fspath);
122 }
123 }
124
125 wxString wxWebFileProtocolHandler::CombineURIs(const wxString &baseuri,
126 const wxString &newuri)
127 {
128 //Still need to be implemented correctly
129 return newuri;
130 }
131
132 // static
133 wxWebView* wxWebView::New(wxWebViewBackend backend)
134 {
135 switch (backend)
136 {
137 #if defined(wxUSE_WEBVIEW_WEBKIT) && \
138 (defined(__WXGTK__) || defined(__WXOSX__))
139 case wxWEB_VIEW_BACKEND_WEBKIT:
140 return new wxWebViewWebKit();
141 #endif
142
143 #if wxUSE_WEBVIEW_IE
144 case wxWEB_VIEW_BACKEND_IE:
145 return new wxWebViewIE();
146 #endif
147
148 case wxWEB_VIEW_BACKEND_DEFAULT:
149
150 #if defined(wxUSE_WEBVIEW_WEBKIT) && \
151 (defined(__WXGTK__) || defined(__WXOSX__))
152 return new wxWebViewWebKit();
153 #endif
154
155 #if wxUSE_WEBVIEW_IE
156 return new wxWebViewIE();
157 #endif
158
159 // fall-through intended
160 default:
161 return NULL;
162 }
163 }
164
165 // static
166 wxWebView* wxWebView::New(wxWindow* parent,
167 wxWindowID id,
168 const wxString& url,
169 const wxPoint& pos,
170 const wxSize& size,
171 wxWebViewBackend backend,
172 long style,
173 const wxString& name)
174 {
175 switch (backend)
176 {
177 #if defined(wxUSE_WEBVIEW_WEBKIT) && \
178 (defined(__WXGTK__) || defined(__WXOSX__))
179 case wxWEB_VIEW_BACKEND_WEBKIT:
180 return new wxWebViewWebKit(parent, id, url, pos, size, style, name);
181 #endif
182
183 #if wxUSE_WEBVIEW_IE
184 case wxWEB_VIEW_BACKEND_IE:
185 return new wxWebViewIE(parent, id, url, pos, size, style, name);
186 #endif
187
188 case wxWEB_VIEW_BACKEND_DEFAULT:
189
190 #if defined(wxUSE_WEBVIEW_WEBKIT) && \
191 (defined(__WXGTK__) || defined(__WXOSX__))
192 return new wxWebViewWebKit(parent, id, url, pos, size, style, name);
193 #endif
194
195 #if wxUSE_WEBVIEW_IE
196 return new wxWebViewIE(parent, id, url, pos, size, style, name);
197 #endif
198
199 // fall-through intended
200 default:
201 return NULL;
202 }
203 }
204
205 #endif // wxUSE_WEB