KiCad PCB EDA Suite
Loading...
Searching...
No Matches
webview_panel.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef WEBVIEW_PANEL_H
21#define WEBVIEW_PANEL_H
22
23#include <wx/panel.h>
24#include <wx/timer.h>
25#include <wx/webview.h>
26#include <wx/weakref.h>
27#include <functional>
28#include <map>
29
30class TOOL_MANAGER;
31class TOOL_BASE;
32
34{
35template<typename T>
36T* GetLiveWindow( const wxWeakRef<T>& aWindowRef )
37{
38 T* window = aWindowRef.get();
39
40 if( window && !window->IsBeingDeleted() )
41 return window;
42
43 return nullptr;
44}
45} // namespace WEBVIEW_PANEL_DETAIL
46
47class WEBVIEW_PANEL : public wxPanel
48{
49public:
50 using MESSAGE_HANDLER = std::function<void( const wxString& )>;
51
52 explicit WEBVIEW_PANEL( wxWindow* parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition,
53 const wxSize& size = wxDefaultSize, const int style = 0,
54 TOOL_MANAGER* aToolManager = nullptr, TOOL_BASE* aTool = nullptr );
55 ~WEBVIEW_PANEL() override;
56
58 const wxString& GetBackend() const { return m_backend; }
59
60 void LoadURL( const wxString& url );
61 void SetPage( const wxString& htmlContent );
62
63 bool AddMessageHandler( const wxString& name, MESSAGE_HANDLER handler );
65
66 void SetHandleExternalLinks( bool aHandle ) { m_handleExternalLinks = aHandle; }
68
69 void RunScriptAsync( const wxString& aScript, void* aClientData = nullptr ) const
70 {
71 if( wxWebView* browser = GetWebView() )
72 browser->RunScriptAsync( aScript, aClientData );
73 }
74
75 bool HasLoadError() const { return m_loadError; }
76
77 void BindLoadedEvent();
78
79protected:
80 void OnNavigationRequest( wxWebViewEvent& evt );
81 void OnWebViewLoaded( wxWebViewEvent& evt );
82 void OnNewWindow( wxWebViewEvent& evt );
83 void OnScriptMessage( wxWebViewEvent& evt );
84 void OnScriptResult( wxWebViewEvent& evt );
85 void OnError( wxWebViewEvent& evt );
86
87 void DoInitHandlers();
88
89private:
90
95 wxWeakRef<wxWebView> m_browser;
96 wxString m_backend;
97 std::map<wxString, MESSAGE_HANDLER> m_msgHandlers;
101};
102
103#endif // WEBVIEW_PANEL_H
const char * name
Base abstract interface for all kinds of tools.
Definition tool_base.h:66
Master controller class:
WEBVIEW_PANEL(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, const int style=0, TOOL_MANAGER *aToolManager=nullptr, TOOL_BASE *aTool=nullptr)
TOOL_MANAGER * m_toolManager
wxString m_backend
wxTimer m_initRetryTimer
bool m_loadedEventBound
void OnScriptMessage(wxWebViewEvent &evt)
void OnNewWindow(wxWebViewEvent &evt)
std::map< wxString, MESSAGE_HANDLER > m_msgHandlers
void SetPage(const wxString &htmlContent)
const wxString & GetBackend() const
void OnWebViewLoaded(wxWebViewEvent &evt)
bool HasLoadError() const
void OnNavigationRequest(wxWebViewEvent &evt)
TOOL_BASE * m_tool
wxWeakRef< wxWebView > m_browser
void SetHandleExternalLinks(bool aHandle)
void ClearMessageHandlers()
bool AddMessageHandler(const wxString &name, MESSAGE_HANDLER handler)
void RunScriptAsync(const wxString &aScript, void *aClientData=nullptr) const
bool m_handleExternalLinks
std::function< void(const wxString &)> MESSAGE_HANDLER
bool GetHandleExternalLinks() const
void OnScriptResult(wxWebViewEvent &evt)
wxWebView * GetWebView() const
void OnError(wxWebViewEvent &evt)
~WEBVIEW_PANEL() override
void LoadURL(const wxString &url)
T * GetLiveWindow(const wxWeakRef< T > &aWindowRef)