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, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <wx/menu.h>
22#include <wx/clipbrd.h>
23#include <wx/log.h>
24#include <wx/settings.h>
25#include <widgets/html_window.h>
26
27
28HTML_WINDOW::HTML_WINDOW( wxWindow* aParent, wxWindowID aId, const wxPoint& aPos,
29 const wxSize& aSize, long aStyle, const wxString& aName ) :
30 wxHtmlWindow( aParent, aId, aPos, aSize, aStyle, aName )
31{
32 Bind( wxEVT_SYS_COLOUR_CHANGED,
33 wxSysColourChangedEventHandler( HTML_WINDOW::onThemeChanged ), this );
34
35 Connect( wxEVT_RIGHT_UP, wxMouseEventHandler( HTML_WINDOW::onRightClick ), nullptr, this );
36 Connect( wxEVT_COMMAND_MENU_SELECTED, wxMenuEventHandler( HTML_WINDOW::onMenuEvent ), nullptr, this );
37 Bind( wxEVT_CHAR_HOOK, wxKeyEventHandler( HTML_WINDOW::onCharHook ), this );
38}
39
40
42{
43 // Disconnect Events
44 Disconnect( wxEVT_RIGHT_UP, wxMouseEventHandler( HTML_WINDOW::onRightClick ), nullptr, this );
45 Disconnect( wxEVT_COMMAND_MENU_SELECTED, wxMenuEventHandler( HTML_WINDOW::onMenuEvent ), nullptr, this );
46 Unbind( wxEVT_CHAR_HOOK, wxKeyEventHandler( HTML_WINDOW::onCharHook ), this );
47}
48
49
50bool HTML_WINDOW::SetPage( const wxString& aSource )
51{
52 m_pageSource = aSource;
53
54 wxColour fgColor = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
55 wxColour bgColor = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
56 wxColour linkColor = wxSystemSettings::GetColour( wxSYS_COLOUR_HOTLIGHT );
57
58 wxString html = wxString::Format( wxT( "<html>\n<body text='%s' bgcolor='%s' link='%s'>\n" ),
59 fgColor.GetAsString( wxC2S_HTML_SYNTAX ),
60 bgColor.GetAsString( wxC2S_HTML_SYNTAX ),
61 linkColor.GetAsString( wxC2S_HTML_SYNTAX ) );
62 html.Append( aSource );
63 html.Append( wxT( "\n</body>\n</html>" ) );
64
65 return wxHtmlWindow::SetPage( html );
66}
67
68
69bool HTML_WINDOW::AppendToPage( const wxString& aSource )
70{
71 return SetPage( m_pageSource + aSource );
72}
73
74
79
80
81bool HTML_WINDOW::ScrollToAnchor( const wxString& aAnchor )
82{
83 // Check if we have content loaded
84 if( !GetInternalRepresentation() )
85 return false;
86
87 // Try to scroll to the anchor
88 bool result = wxHtmlWindow::ScrollToAnchor( aAnchor );
89 return result;
90}
91
92
93void HTML_WINDOW::onThemeChanged( wxSysColourChangedEvent &aEvent )
94{
96}
97
98
99void HTML_WINDOW::onRightClick( wxMouseEvent& event )
100{
101 wxMenu popup;
102 popup.Append( ID_COPY_SELECTION, _( "Copy" ) );
103 popup.Append( ID_SELECT_ALL, _( "Select All" ) );
104 PopupMenu( &popup );
105}
106
107
108void HTML_WINDOW::onMenuEvent( wxMenuEvent& event )
109{
110 if( event.GetId() == ID_COPY_SELECTION )
112 else if( event.GetId() == ID_SELECT_ALL )
113 SelectAll();
114}
115
116
117void HTML_WINDOW::onCharHook( wxKeyEvent& aEvent )
118{
119 if( aEvent.CmdDown() || aEvent.ControlDown() )
120 {
121 switch( aEvent.GetKeyCode() )
122 {
123 case 'A':
124 SelectAll();
125 return;
126
127 case 'C':
129 return;
130
131 default:
132 break;
133 }
134 }
135
136 aEvent.Skip();
137}
138
139
141{
142 wxString selectedText = SelectionToText();
143
144 if( selectedText.IsEmpty() )
145 return;
146
147 wxLogNull doNotLog; // disable logging of failed clipboard actions
148
149 if( wxTheClipboard->Open() )
150 {
151 bool primarySelection = wxTheClipboard->IsUsingPrimarySelection();
152 wxTheClipboard->UsePrimarySelection( false ); // required to use the main clipboard
153 wxTheClipboard->SetData( new wxTextDataObject( selectedText ) );
154 wxTheClipboard->Flush(); // Allow data to be available after closing KiCad
155 wxTheClipboard->Close();
156 wxTheClipboard->UsePrimarySelection( primarySelection );
157 }
158}
159
160
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:65
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.