KiCad PCB EDA Suite
Loading...
Searching...
No Matches
wx_html_report_box.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) 2020-2024 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 2 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
21#include <math/util.h>
22#include <common.h>
23#include <wx/settings.h>
24#include <wx/textctrl.h>
25#include <wx/menu.h>
26#include <wx/clipbrd.h>
27#include <wx/log.h>
28#include "wx_html_report_box.h"
29
30
31WX_HTML_REPORT_BOX::WX_HTML_REPORT_BOX( wxWindow* parent, wxWindowID id, const wxPoint& pos,
32 const wxSize& size, long style ) :
33 HTML_WINDOW( parent, id, pos, size, style ),
34 m_units( EDA_UNITS::MILLIMETRES ),
35 m_immediateMode( false )
36{
37 Flush();
38
39 Bind( wxEVT_SYS_COLOUR_CHANGED,
40 wxSysColourChangedEventHandler( WX_HTML_REPORT_BOX::onThemeChanged ), this );
41
42 Connect( wxEVT_RIGHT_UP, wxMouseEventHandler( WX_HTML_REPORT_BOX::onRightClick ), nullptr,
43 this );
44}
45
46
48{
49 // Disconnect Events
50 Disconnect( wxEVT_RIGHT_UP, wxMouseEventHandler( WX_HTML_REPORT_BOX::onRightClick ), nullptr,
51 this );
52}
53
54
55void WX_HTML_REPORT_BOX::onThemeChanged( wxSysColourChangedEvent &aEvent )
56{
57 Flush();
58
59 aEvent.Skip();
60}
61
62
63void WX_HTML_REPORT_BOX::onRightClick( wxMouseEvent& event )
64{
65 wxMenu popup;
66 popup.Append( wxID_COPY, "Copy" );
67 PopupMenu( &popup );
68}
69
70
71void WX_HTML_REPORT_BOX::onMenuEvent( wxMenuEvent& event )
72{
73 if( event.GetId() == wxID_COPY )
74 {
75 wxLogNull doNotLog; // disable logging of failed clipboard actions
76
77 if( wxTheClipboard->Open() )
78 {
79 bool primarySelection = wxTheClipboard->IsUsingPrimarySelection();
80 wxTheClipboard->UsePrimarySelection( false ); // required to use the main clipboard
81 wxTheClipboard->SetData( new wxTextDataObject( SelectionToText() ) );
82 wxTheClipboard->Flush(); // Allow data to be available after closing KiCad
83 wxTheClipboard->Close();
84 wxTheClipboard->UsePrimarySelection( primarySelection );
85 }
86 }
87}
88
89
90REPORTER& WX_HTML_REPORT_BOX::Report( const wxString& aText, SEVERITY aSeverity )
91{
92 m_messages.push_back( aText );
93
94 if( m_immediateMode )
95 {
96 Flush();
97 int px, py, x, y;
98 GetScrollPixelsPerUnit( &px, &py );
99 GetVirtualSize( &x, &y );
100 Scroll( -1, y * py );
101 }
102
103 return *this;
104}
105
107{
108 wxString html;
109
110 for( const wxString& line : m_messages )
111 html += generateHtml( line );
112
113 SetPage( html );
114}
115
116
117wxString WX_HTML_REPORT_BOX::generateHtml( const wxString& aLine )
118{
119 // wxWidgets default linespacing is about 110% of font-height (which is way too small),
120 // and the default paragraph spacing is about 200% (which is too big). The heading,
121 // bullet lists, etc. line spacing is fine.
122 //
123 // And of course they provide no way to set it, which leaves us with very few options.
124 // Fortunately we know we're dealing mostly with single lines in the reporter so we apply
125 // an egregious hack and enforce a minimum linespacing by inserting an invisible img
126 // element with appropriate height
127
128 wxFont font = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT );
129 int additionalLineSpacing = KiROUND( font.GetPixelSize().y * 0.6 );
130
131 return wxString::Format( wxT( "<img align=texttop height=%d width=0 src=#>%s<br>" ),
132 additionalLineSpacing, aLine );
133}
134
135
137{
138 m_messages.clear();
139}
140
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition: box2.h:990
Add dark theme support to wxHtmlWindow.
Definition: html_window.h:35
bool SetPage(const wxString &aSource) override
Definition: html_window.cpp:50
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:72
void onMenuEvent(wxMenuEvent &event)
void onRightClick(wxMouseEvent &event)
std::vector< wxString > m_messages
void Clear()
Delete the stored messages.
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
bool m_immediateMode
copy of the report, stored for filtering
void Flush()
Build the HTML messages page.
WX_HTML_REPORT_BOX(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(500, 300), long style=wxTAB_TRAVERSAL)
wxString generateHtml(const wxString &aLine)
void onThemeChanged(wxSysColourChangedEvent &aEvent)
The common library.
EDA_UNITS
Definition: eda_units.h:46
SEVERITY