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 Bind( wxEVT_CHAR_HOOK, wxKeyEventHandler( HTML_WINDOW::onCharHook ), this );
42}
43
44
46{
47 // Disconnect Events
48 Disconnect( wxEVT_RIGHT_UP, wxMouseEventHandler( HTML_WINDOW::onRightClick ), nullptr, this );
49 Disconnect( wxEVT_COMMAND_MENU_SELECTED, wxMenuEventHandler( HTML_WINDOW::onMenuEvent ), nullptr, this );
50 Unbind( wxEVT_CHAR_HOOK, wxKeyEventHandler( HTML_WINDOW::onCharHook ), this );
51}
52
53
54bool HTML_WINDOW::SetPage( const wxString& aSource )
55{
56 m_pageSource = aSource;
57
58 wxColour fgColor = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
59 wxColour bgColor = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
60 wxColour linkColor = wxSystemSettings::GetColour( wxSYS_COLOUR_HOTLIGHT );
61
62 wxString html = wxString::Format( wxT( "<html>\n<body text='%s' bgcolor='%s' link='%s'>\n" ),
63 fgColor.GetAsString( wxC2S_HTML_SYNTAX ),
64 bgColor.GetAsString( wxC2S_HTML_SYNTAX ),
65 linkColor.GetAsString( wxC2S_HTML_SYNTAX ) );
66 html.Append( aSource );
67 html.Append( wxT( "\n</body>\n</html>" ) );
68
69 return wxHtmlWindow::SetPage( html );
70}
71
72
73bool HTML_WINDOW::AppendToPage( const wxString& aSource )
74{
75 return SetPage( m_pageSource + aSource );
76}
77
78
83
84
85bool HTML_WINDOW::ScrollToAnchor( const wxString& aAnchor )
86{
87 // Check if we have content loaded
88 if( !GetInternalRepresentation() )
89 return false;
90
91 // Try to scroll to the anchor
92 bool result = wxHtmlWindow::ScrollToAnchor( aAnchor );
93 return result;
94}
95
96
97void HTML_WINDOW::onThemeChanged( wxSysColourChangedEvent &aEvent )
98{
100}
101
102
103void HTML_WINDOW::onRightClick( wxMouseEvent& event )
104{
105 wxMenu popup;
106 popup.Append( ID_COPY_SELECTION, _( "Copy" ) );
107 popup.Append( ID_SELECT_ALL, _( "Select All" ) );
108 PopupMenu( &popup );
109}
110
111
112void HTML_WINDOW::onMenuEvent( wxMenuEvent& event )
113{
114 if( event.GetId() == ID_COPY_SELECTION )
116 else if( event.GetId() == ID_SELECT_ALL )
117 SelectAll();
118}
119
120
121void HTML_WINDOW::onCharHook( wxKeyEvent& aEvent )
122{
123 if( aEvent.CmdDown() || aEvent.ControlDown() )
124 {
125 switch( aEvent.GetKeyCode() )
126 {
127 case 'A':
128 SelectAll();
129 return;
130
131 case 'C':
133 return;
134
135 default:
136 break;
137 }
138 }
139
140 aEvent.Skip();
141}
142
143
145{
146 wxString selectedText = SelectionToText();
147
148 if( selectedText.IsEmpty() )
149 return;
150
151 wxLogNull doNotLog; // disable logging of failed clipboard actions
152
153 if( wxTheClipboard->Open() )
154 {
155 bool primarySelection = wxTheClipboard->IsUsingPrimarySelection();
156 wxTheClipboard->UsePrimarySelection( false ); // required to use the main clipboard
157 wxTheClipboard->SetData( new wxTextDataObject( selectedText ) );
158 wxTheClipboard->Flush(); // Allow data to be available after closing KiCad
159 wxTheClipboard->Close();
160 wxTheClipboard->UsePrimarySelection( primarySelection );
161 }
162}
163
164
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:69
void onMenuEvent(wxMenuEvent &event)
void onCharHook(wxKeyEvent &aEvent)
void onThemeChanged(wxSysColourChangedEvent &aEvent)
bool ScrollToAnchor(const wxString &aAnchor)
bool AppendToPage(const wxString &aSource)
void doCopySelection()
void ThemeChanged()
#define _(s)
wxString result
Test unit parsing edge cases and error handling.