KiCad PCB EDA Suite
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 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_html_report_box.h"
26 
27 
28 WX_HTML_REPORT_BOX::WX_HTML_REPORT_BOX( wxWindow* parent, wxWindowID id, const wxPoint& pos,
29  const wxSize& size, long style ) :
30  HTML_WINDOW( parent, id, pos, size, style ),
31  m_units( EDA_UNITS::MILLIMETRES ), m_immediateMode( false )
32 {
33  Flush();
34 
35  Bind( wxEVT_SYS_COLOUR_CHANGED,
36  wxSysColourChangedEventHandler( WX_HTML_REPORT_BOX::onThemeChanged ), this );
37 }
38 
39 
40 void WX_HTML_REPORT_BOX::onThemeChanged( wxSysColourChangedEvent &aEvent )
41 {
42  Flush();
43 
44  aEvent.Skip();
45 }
46 
47 
48 REPORTER& WX_HTML_REPORT_BOX::Report( const wxString& aText, SEVERITY aSeverity )
49 {
50  m_messages.push_back( aText );
51 
52  if( m_immediateMode )
53  {
54  Flush();
55  int px, py, x, y;
56  GetScrollPixelsPerUnit( &px, &py );
57  GetVirtualSize( &x, &y );
58  Scroll( -1, y * py );
59  }
60 
61  return *this;
62 }
63 
65 {
66  wxString html;
67 
68  for( const wxString& line : m_messages )
69  html += generateHtml( line );
70 
71  SetPage( html );
72 }
73 
74 
75 wxString WX_HTML_REPORT_BOX::generateHtml( const wxString& aLine )
76 {
77  // wxWidgets default linespacing is about 110% of font-height (which is way too small),
78  // and the default paragraph spacing is about 200% (which is too big). The heading,
79  // bullet lists, etc. line spacing is fine.
80  //
81  // And of course they provide no way to set it, which leaves us with very few options.
82  // Fortunately we know we're dealing mostly with single lines in the reporter so we apply
83  // an egregious hack and enforce a minimum linespacing by inserting an invisible img
84  // element with appropriate height
85 
86  wxFont font = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT );
87  int additionalLineSpacing = KiROUND( font.GetPixelSize().y * 0.6 );
88 
89  return wxString::Format( wxT( "<img align=texttop height=%d width=0 src=#>%s<br>" ),
90  additionalLineSpacing, aLine );
91 }
92 
93 
95 {
96  m_messages.clear();
97 }
98 
void Clear()
Delete the stored messages.
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
void Flush()
Build the HTML messages page.
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:70
std::vector< wxString > m_messages
bool m_immediateMode
copy of the report, stored for filtering
bool SetPage(const wxString &aSource) override
Definition: html_window.cpp:38
SEVERITY
Add dark theme support to wxHtmlWindow.
Definition: html_window.h:33
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
EDA_UNITS
Definition: eda_units.h:38
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:73
The common library.
void onThemeChanged(wxSysColourChangedEvent &aEvent)
wxString generateHtml(const wxString &aLine)
WX_HTML_REPORT_BOX(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(500, 300), long style=wxTAB_TRAVERSAL)