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_html_report_box.h"
24 
25 
26 WX_HTML_REPORT_BOX::WX_HTML_REPORT_BOX( wxWindow* parent, wxWindowID id, const wxPoint& pos,
27  const wxSize& size, long style ) :
28  wxHtmlWindow( parent, id, pos, size, style ),
29  m_units( EDA_UNITS::MILLIMETRES ),
30  m_immediateMode( false )
31 {
32 }
33 
34 
35 REPORTER& WX_HTML_REPORT_BOX::Report( const wxString& aText, SEVERITY aSeverity )
36 {
37  m_messages.push_back( aText );
38 
39  if( m_immediateMode )
40  {
41  Flush();
42  int px, py, x, y;
43  GetScrollPixelsPerUnit( &px, &py );
44  GetVirtualSize( &x, &y );
45  Scroll( -1, y * py );
46  }
47 
48  return *this;
49 }
50 
51 
52 wxString fixLinespacing( wxHtmlCell* aCell, int aMinLinespacing )
53 {
54  // wxWidgets default linespacing is about 110% of font-height (which is way too small),
55  // and the default paragraph spacing is about 200% (which is too big). The heading,
56  // bullet lists, etc. line spacing is fine.
57  //
58  // And of course they provide no way to set it, which leaves us with very few options.
59  // Fortunately we know we're dealing mostly with single lines in the reporter so we apply
60  // an egregious hack and enforce a minimum linespacing for things ending in '.' (which
61  // normally won't include headings or bullet list items).
62 
63  wxString content = aCell->ConvertToText( nullptr );
64  wxHtmlContainerCell* container = dynamic_cast<wxHtmlContainerCell*>( aCell );
65 
66  for( wxHtmlCell* child = aCell->GetFirstChild(); child; child = child->GetNext() )
67  content += fixLinespacing( child, aMinLinespacing );
68 
69  if( container && content.EndsWith( "." ) && container->GetHeight() < aMinLinespacing )
70  container->SetMinHeight( aMinLinespacing );
71 
72  return content;
73 }
74 
75 
77 {
78  wxString html;
79 
80  for( const wxString& line : m_messages )
81  html += generateHtml( line );
82 
83  SetPage( addHeader( html ) );
84 
85  wxFont font = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT );
86  int minLineHeight = KiROUND( font.GetPixelSize().y * 1.3 );
87 
88  fixLinespacing( GetInternalRepresentation(), minLineHeight );
89 }
90 
91 
92 wxString WX_HTML_REPORT_BOX::addHeader( const wxString& aBody )
93 {
94  wxColour bgcolor = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
95  wxColour fgcolor = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
96 
97  return wxString::Format( wxT( "<html>"
98  " <body bgcolor='%s' text='%s'>"
99  " %s"
100  " </body>"
101  "</html>" ),
102  bgcolor.GetAsString( wxC2S_HTML_SYNTAX ),
103  fgcolor.GetAsString( wxC2S_HTML_SYNTAX ),
104  aBody );
105 }
106 
107 
108 wxString WX_HTML_REPORT_BOX::generateHtml( const wxString& aLine )
109 {
110  return aLine + "<br>";
111 }
112 
113 
115 {
116  m_messages.clear();
117 }
118 
wxString addHeader(const wxString &aBody)
SEVERITY
Definition: ui_common.h:83
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
wxString fixLinespacing(wxHtmlCell *aCell, int aMinLinespacing)
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:64
std::vector< wxString > m_messages
bool m_immediateMode
copy of the report, stored for filtering
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:68
The common library.
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)