KiCad PCB EDA Suite
Loading...
Searching...
No Matches
html_window.cpp
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 (C) 2021 MikoĊ‚aj Wielgus <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.TXT for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25#include <wx/menu.h>
26#include <wx/clipbrd.h>
27#include <wx/log.h>
28#include <wx/settings.h>
29#include <widgets/html_window.h>
30
31
32HTML_WINDOW::HTML_WINDOW( wxWindow* aParent, wxWindowID aId, const wxPoint& aPos,
33 const wxSize& aSize, long aStyle, const wxString& aName ) :
34 wxHtmlWindow( aParent, aId, aPos, aSize, aStyle, aName )
35{
36 Bind( wxEVT_SYS_COLOUR_CHANGED,
37 wxSysColourChangedEventHandler( HTML_WINDOW::onThemeChanged ), this );
38
39 Connect( wxEVT_RIGHT_UP, wxMouseEventHandler( HTML_WINDOW::onRightClick ), nullptr, this );
40 Connect( wxEVT_COMMAND_MENU_SELECTED, wxMenuEventHandler( HTML_WINDOW::onMenuEvent ), nullptr, this );
41}
42
43
45{
46 // Disconnect Events
47 Disconnect( wxEVT_RIGHT_UP, wxMouseEventHandler( HTML_WINDOW::onRightClick ), nullptr, this );
48 Disconnect( wxEVT_COMMAND_MENU_SELECTED, wxMenuEventHandler( HTML_WINDOW::onMenuEvent ), nullptr, this );
49}
50
51
52bool HTML_WINDOW::SetPage( const wxString& aSource )
53{
54 m_pageSource = aSource;
55
56 wxColour fgColor = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
57 wxColour bgColor = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
58 wxColour linkColor = wxSystemSettings::GetColour( wxSYS_COLOUR_HOTLIGHT );
59
60 wxString html = wxString::Format( wxT( "<html>\n<body text='%s' bgcolor='%s' link='%s'>\n" ),
61 fgColor.GetAsString( wxC2S_HTML_SYNTAX ),
62 bgColor.GetAsString( wxC2S_HTML_SYNTAX ),
63 linkColor.GetAsString( wxC2S_HTML_SYNTAX ) );
64 html.Append( aSource );
65 html.Append( wxT( "\n</body>\n</html>" ) );
66
67 return wxHtmlWindow::SetPage( html );
68}
69
70
71bool HTML_WINDOW::AppendToPage( const wxString& aSource )
72{
73 return SetPage( m_pageSource + aSource );
74}
75
76
81
82
83bool HTML_WINDOW::ScrollToAnchor( const wxString& aAnchor )
84{
85 // Check if we have content loaded
86 if( !GetInternalRepresentation() )
87 return false;
88
89 // Try to scroll to the anchor
90 bool result = wxHtmlWindow::ScrollToAnchor( aAnchor );
91 return result;
92}
93
94
95void HTML_WINDOW::onThemeChanged( wxSysColourChangedEvent &aEvent )
96{
98}
99
100
101void HTML_WINDOW::onRightClick( wxMouseEvent& event )
102{
103 wxMenu popup;
104 popup.Append( wxID_COPY, _( "Copy" ) );
105 popup.Append( wxID_SELECTALL, _( "Select All" ) );
106 PopupMenu( &popup );
107}
108
109
110void HTML_WINDOW::onMenuEvent( wxMenuEvent& event )
111{
112 if( event.GetId() == wxID_COPY )
113 {
114 wxLogNull doNotLog; // disable logging of failed clipboard actions
115
116 if( wxTheClipboard->Open() )
117 {
118 bool primarySelection = wxTheClipboard->IsUsingPrimarySelection();
119 wxTheClipboard->UsePrimarySelection( false ); // required to use the main clipboard
120 wxTheClipboard->SetData( new wxTextDataObject( SelectionToText() ) );
121 wxTheClipboard->Flush(); // Allow data to be available after closing KiCad
122 wxTheClipboard->Close();
123 wxTheClipboard->UsePrimarySelection( primarySelection );
124 }
125 }
126 else if( event.GetId() == wxID_SELECTALL )
127 {
128 SelectAll();
129 }
130}
131
132
bool SetPage(const wxString &aSource) override
HTML_WINDOW(wxWindow *aParent, wxWindowID aId=wxID_ANY, const wxPoint &aPos=wxDefaultPosition, const wxSize &aSize=wxDefaultSize, long aStyle=wxHW_DEFAULT_STYLE, const wxString &aName=wxT("htmlWindow"))
void onRightClick(wxMouseEvent &event)
wxString m_pageSource
Definition html_window.h:61
void onMenuEvent(wxMenuEvent &event)
void onThemeChanged(wxSysColourChangedEvent &aEvent)
bool ScrollToAnchor(const wxString &aAnchor)
bool AppendToPage(const wxString &aSource)
void ThemeChanged()
#define _(s)
wxString result
Test unit parsing edge cases and error handling.