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
WX_HTML_REPORT_BOX(wxWindow * parent,wxWindowID id,const wxPoint & pos,const wxSize & size,long style)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
onThemeChanged(wxSysColourChangedEvent & aEvent)40 void WX_HTML_REPORT_BOX::onThemeChanged( wxSysColourChangedEvent &aEvent )
41 {
42 Flush();
43
44 aEvent.Skip();
45 }
46
47
Report(const wxString & aText,SEVERITY aSeverity)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
Flush()64 void WX_HTML_REPORT_BOX::Flush()
65 {
66 wxString html;
67
68 for( const wxString& line : m_messages )
69 html += generateHtml( line );
70
71 SetPage( html );
72 }
73
74
generateHtml(const wxString & aLine)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
Clear()94 void WX_HTML_REPORT_BOX::Clear()
95 {
96 m_messages.clear();
97 }
98
99