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.

191 lines
4.8 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2014 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include <wx/clipbrd.h>
  25. #include <wx/log.h>
  26. #include <wx/textctrl.h>
  27. #include <wx/uri.h>
  28. #include <string_utils.h>
  29. #include <dialogs/html_message_box.h>
  30. #include <build_version.h>
  31. HTML_MESSAGE_BOX::HTML_MESSAGE_BOX( wxWindow* aParent, const wxString& aTitle,
  32. const wxPoint& aPosition, const wxSize& aSize ) :
  33. DIALOG_DISPLAY_HTML_TEXT_BASE( aParent, wxID_ANY, aTitle, aPosition, aSize )
  34. {
  35. m_htmlWindow->SetLayoutDirection( wxLayout_LeftToRight );
  36. ListClear();
  37. // Gives a default logical size (the actual size depends on the display definition)
  38. if( aSize != wxDefaultSize )
  39. setSizeInDU( aSize.x, aSize.y );
  40. Center();
  41. SetupStandardButtons();
  42. reload();
  43. Bind( wxEVT_SYS_COLOUR_CHANGED,
  44. wxSysColourChangedEventHandler( HTML_MESSAGE_BOX::onThemeChanged ), this );
  45. }
  46. HTML_MESSAGE_BOX::~HTML_MESSAGE_BOX()
  47. {
  48. // Prevent wxWidgets bug which fails to release when closing the window on an <esc>.
  49. if( m_htmlWindow->HasCapture() )
  50. m_htmlWindow->ReleaseMouse();
  51. }
  52. void HTML_MESSAGE_BOX::reload()
  53. {
  54. m_htmlWindow->SetPage( m_source );
  55. }
  56. void HTML_MESSAGE_BOX::onThemeChanged( wxSysColourChangedEvent &aEvent )
  57. {
  58. reload();
  59. aEvent.Skip();
  60. }
  61. void HTML_MESSAGE_BOX::ListClear()
  62. {
  63. m_source.clear();
  64. reload();
  65. }
  66. void HTML_MESSAGE_BOX::ListSet( const wxString& aList )
  67. {
  68. wxArrayString strings_list;
  69. wxStringSplit( aList, strings_list, wxChar( '\n' ) );
  70. wxString msg = wxT( "<ul>" );
  71. for ( unsigned ii = 0; ii < strings_list.GetCount(); ii++ )
  72. {
  73. msg += wxT( "<li>" );
  74. msg += strings_list.Item( ii ) + wxT( "</li>" );
  75. }
  76. msg += wxT( "</ul>" );
  77. m_source += msg;
  78. reload();
  79. }
  80. void HTML_MESSAGE_BOX::ListSet( const wxArrayString& aList )
  81. {
  82. wxString msg = wxT( "<ul>" );
  83. for( unsigned ii = 0; ii < aList.GetCount(); ii++ )
  84. {
  85. msg += wxT( "<li>" );
  86. msg += aList.Item( ii ) + wxT( "</li>" );
  87. }
  88. msg += wxT( "</ul>" );
  89. m_source += msg;
  90. reload();
  91. }
  92. void HTML_MESSAGE_BOX::MessageSet( const wxString& message )
  93. {
  94. wxString message_value = wxString::Format( wxT( "<b>%s</b><br>" ), message );
  95. m_source += message_value;
  96. reload();
  97. }
  98. void HTML_MESSAGE_BOX::AddHTML_Text( const wxString& message )
  99. {
  100. m_source += message;
  101. reload();
  102. }
  103. void HTML_MESSAGE_BOX::ShowModeless()
  104. {
  105. reload();
  106. m_sdbSizer1->Show( false );
  107. Layout();
  108. Show( true );
  109. }
  110. void HTML_MESSAGE_BOX::OnHTMLLinkClicked( wxHtmlLinkEvent& event )
  111. {
  112. wxString href = event.GetLinkInfo().GetHref();
  113. if( href.StartsWith( wxS( "https://go.kicad.org/docs" ) ) )
  114. {
  115. href.Replace( wxS( "GetMajorMinorVersion" ), GetMajorMinorVersion() );
  116. }
  117. wxURI uri( href );
  118. wxLaunchDefaultBrowser( uri.BuildURI() );
  119. }
  120. void HTML_MESSAGE_BOX::OnCharHook( wxKeyEvent& aEvent )
  121. {
  122. // shift-return (Mac default) or Ctrl-Return (GTK) for OK
  123. if( aEvent.GetKeyCode() == WXK_ESCAPE )
  124. {
  125. wxPostEvent( this, wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK ) );
  126. return;
  127. }
  128. else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() == 'A' )
  129. {
  130. m_htmlWindow->SelectAll();
  131. return;
  132. }
  133. else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() == 'C' )
  134. {
  135. wxLogNull doNotLog; // disable logging of failed clipboard actions
  136. if( wxTheClipboard->Open() )
  137. {
  138. wxTheClipboard->SetData( new wxTextDataObject( m_htmlWindow->SelectionToText() ) );
  139. wxTheClipboard->Flush(); // Allow data to be available after closing KiCad
  140. wxTheClipboard->Close();
  141. }
  142. return;
  143. }
  144. aEvent.Skip();
  145. }