You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.2 KiB

  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. #include <math/util.h>
  20. #include <common.h>
  21. #include "wx_html_report_box.h"
  22. WX_HTML_REPORT_BOX::WX_HTML_REPORT_BOX( wxWindow* parent, wxWindowID id, const wxPoint& pos,
  23. const wxSize& size, long style ) :
  24. wxHtmlWindow( parent, id, pos, size, style ),
  25. m_units( EDA_UNITS::MILLIMETRES ),
  26. m_immediateMode( false )
  27. {
  28. }
  29. REPORTER& WX_HTML_REPORT_BOX::Report( const wxString& aText, SEVERITY aSeverity )
  30. {
  31. m_messages.push_back( aText );
  32. if( m_immediateMode )
  33. {
  34. Flush();
  35. int px, py, x, y;
  36. GetScrollPixelsPerUnit( &px, &py );
  37. GetVirtualSize( &x, &y );
  38. Scroll( -1, y * py );
  39. }
  40. return *this;
  41. }
  42. void WX_HTML_REPORT_BOX::Flush()
  43. {
  44. wxString html;
  45. for( const wxString& line : m_messages )
  46. html += generateHtml( line );
  47. SetPage( addHeader( html ) );
  48. }
  49. wxString WX_HTML_REPORT_BOX::addHeader( const wxString& aBody )
  50. {
  51. wxColour bgcolor = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
  52. wxColour fgcolor = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
  53. return wxString::Format( wxT( "<html>"
  54. " <body bgcolor='%s' text='%s'>"
  55. " %s"
  56. " </body>"
  57. "</html>" ),
  58. bgcolor.GetAsString( wxC2S_HTML_SYNTAX ),
  59. fgcolor.GetAsString( wxC2S_HTML_SYNTAX ),
  60. aBody );
  61. }
  62. wxString WX_HTML_REPORT_BOX::generateHtml( const wxString& aLine )
  63. {
  64. // wxWidgets default linespacing is about 110% of font-height (which is way too small),
  65. // and the default paragraph spacing is about 200% (which is too big). The heading,
  66. // bullet lists, etc. line spacing is fine.
  67. //
  68. // And of course they provide no way to set it, which leaves us with very few options.
  69. // Fortunately we know we're dealing mostly with single lines in the reporter so we apply
  70. // an egregious hack and enforce a minimum linespacing by inserting an invisible img
  71. // element with appropriate height
  72. wxFont font = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT );
  73. int additionalLineSpacing = KiROUND( font.GetPixelSize().y * 0.6 );
  74. return wxString::Format( wxT( "<img align=texttop height=%d width=0 src=#>%s<br>" ),
  75. additionalLineSpacing, aLine );
  76. }
  77. void WX_HTML_REPORT_BOX::Clear()
  78. {
  79. m_messages.clear();
  80. }