1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Common interface and events for web view component
4 // Author: Marianne Gagnon
6 // Copyright: (c) 2010 Marianne Gagnon
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
15 #if defined(__BORLANDC__)
19 #include "wx/webview.h"
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"
27 // DLL options compatibility check:
29 WX_CHECK_BUILD_OPTIONS("wxWEB")
31 extern WXDLLIMPEXP_DATA_WEB(const char) wxWebViewNameStr
[] = "wxWebView";
32 extern WXDLLIMPEXP_DATA_WEB(const char) wxWebViewDefaultURLStr
[] = "about:blank";
34 IMPLEMENT_DYNAMIC_CLASS(wxWebNavigationEvent
, wxCommandEvent
)
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
);
42 //Taken from wx/filesys.cpp
43 static wxString
EscapeFileNameCharsInURL(const char *in
)
47 for ( const unsigned char *p
= (const unsigned char*)in
; *p
; ++p
)
49 const unsigned char c
= *p
;
51 if ( c
== '/' || c
== '-' || c
== '.' || c
== '_' || c
== '~' ||
52 (c
>= '0' && c
<= '9') ||
53 (c
>= 'a' && c
<= 'z') ||
54 (c
>= 'A' && c
<= 'Z') )
60 s
<< wxString::Format("%%%02x", c
);
67 wxWebFileProtocolHandler::wxWebFileProtocolHandler()
70 m_fileSystem
= new wxFileSystem();
73 wxFSFile
* wxWebFileProtocolHandler::GetFile(const wxString
&uri
)
75 size_t pos
= uri
.find('?');
76 //There is no query string so we can load the file directly
77 if(pos
== wxString::npos
)
79 size_t doubleslash
= uri
.find("//");
80 //The path is incorrectly formed without // after the first protocol
81 if(doubleslash
== wxString::npos
)
84 wxString fspath
= "file:" +
85 EscapeFileNameCharsInURL(uri
.substr(doubleslash
+ 2));
86 return m_fileSystem
->OpenFile(fspath
);
88 //Otherwise we have a query string of some kind that we need to extract
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
== ""))
98 wxString token
= tokenizer
.GetNextToken();
99 if(token
.substr(0, 9) == "protocol=")
101 protocol
= token
.substr(9);
103 else if(token
.substr(0, 5) == "path=")
105 path
= token
.substr(5);
108 if(protocol
== "" || path
== "")
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
)
118 wxString fspath
= "file:" +
119 EscapeFileNameCharsInURL(lefturi
.substr(doubleslash
+ 2))
120 + "#" + protocol
+":" + path
;
121 return m_fileSystem
->OpenFile(fspath
);
125 wxString
wxWebFileProtocolHandler::CombineURIs(const wxString
&baseuri
,
126 const wxString
&newuri
)
128 //Still need to be implemented correctly
133 wxWebView
* wxWebView::New(wxWebViewBackend backend
)
137 #if defined(wxUSE_WEBVIEW_WEBKIT) && \
138 (defined(__WXGTK__) || defined(__WXOSX__))
139 case wxWEB_VIEW_BACKEND_WEBKIT
:
140 return new wxWebViewWebKit();
144 case wxWEB_VIEW_BACKEND_IE
:
145 return new wxWebViewIE();
148 case wxWEB_VIEW_BACKEND_DEFAULT
:
150 #if defined(wxUSE_WEBVIEW_WEBKIT) && \
151 (defined(__WXGTK__) || defined(__WXOSX__))
152 return new wxWebViewWebKit();
156 return new wxWebViewIE();
159 // fall-through intended
166 wxWebView
* wxWebView::New(wxWindow
* parent
,
171 wxWebViewBackend backend
,
173 const wxString
& name
)
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
);
184 case wxWEB_VIEW_BACKEND_IE
:
185 return new wxWebViewIE(parent
, id
, url
, pos
, size
, style
, name
);
188 case wxWEB_VIEW_BACKEND_DEFAULT
:
190 #if defined(wxUSE_WEBVIEW_WEBKIT) && \
191 (defined(__WXGTK__) || defined(__WXOSX__))
192 return new wxWebViewWebKit(parent
, id
, url
, pos
, size
, style
, name
);
196 return new wxWebViewIE(parent
, id
, url
, pos
, size
, style
, name
);
199 // fall-through intended