2014-10-19 16:20:16 -04:00
|
|
|
|
/*
|
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2014 Jean-Pierre Charras, jp.charras at wanadoo.fr
|
2025-01-01 13:30:11 -08:00
|
|
|
|
* Copyright The KiCad Developers, see AUTHORS.txt for contributors.
|
2014-10-19 16:20:16 -04:00
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
|
*/
|
|
|
|
|
|
2021-03-02 11:54:13 +00:00
|
|
|
|
#include <wx/clipbrd.h>
|
2021-06-03 08:11:15 -04:00
|
|
|
|
#include <wx/log.h>
|
2021-09-14 19:26:03 +01:00
|
|
|
|
#include <wx/textctrl.h>
|
2023-02-01 23:23:23 +00:00
|
|
|
|
#include <wx/uri.h>
|
2025-08-29 10:27:48 -07:00
|
|
|
|
#include <wx/panel.h>
|
|
|
|
|
#include <wx/sizer.h>
|
|
|
|
|
#include <wx/button.h>
|
|
|
|
|
#include <wx/stattext.h>
|
|
|
|
|
#include <algorithm>
|
2021-07-29 10:56:22 +01:00
|
|
|
|
#include <string_utils.h>
|
2021-09-14 19:26:03 +01:00
|
|
|
|
#include <dialogs/html_message_box.h>
|
2023-02-01 23:23:23 +00:00
|
|
|
|
#include <build_version.h>
|
2021-09-14 19:26:03 +01:00
|
|
|
|
|
|
|
|
|
|
2019-09-06 14:57:04 -04:00
|
|
|
|
HTML_MESSAGE_BOX::HTML_MESSAGE_BOX( wxWindow* aParent, const wxString& aTitle,
|
|
|
|
|
const wxPoint& aPosition, const wxSize& aSize ) :
|
2021-10-18 00:29:40 +01:00
|
|
|
|
DIALOG_DISPLAY_HTML_TEXT_BASE( aParent, wxID_ANY, aTitle, aPosition, aSize )
|
2009-07-12 18:27:30 +00:00
|
|
|
|
{
|
2016-03-22 14:53:50 -04:00
|
|
|
|
m_htmlWindow->SetLayoutDirection( wxLayout_LeftToRight );
|
2009-07-13 06:20:18 +00:00
|
|
|
|
ListClear();
|
2018-01-11 09:08:55 +01:00
|
|
|
|
|
2025-08-29 10:27:48 -07:00
|
|
|
|
m_searchPanel = new wxPanel( this );
|
|
|
|
|
wxBoxSizer* searchSizer = new wxBoxSizer( wxHORIZONTAL );
|
|
|
|
|
m_matchCount = new wxStaticText( m_searchPanel, wxID_ANY, wxEmptyString );
|
|
|
|
|
m_searchCtrl = new wxTextCtrl( m_searchPanel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER );
|
|
|
|
|
m_prevBtn = new wxButton( m_searchPanel, wxID_ANY, wxS( "∧" ), wxDefaultPosition, wxDefaultSize, wxBORDER_NONE );
|
|
|
|
|
m_nextBtn = new wxButton( m_searchPanel, wxID_ANY, wxS( "∨" ), wxDefaultPosition, wxDefaultSize, wxBORDER_NONE );
|
|
|
|
|
|
|
|
|
|
// Set minimum size for buttons to make them thinner
|
|
|
|
|
m_prevBtn->SetMinSize( wxSize( 25, -1 ) );
|
|
|
|
|
m_nextBtn->SetMinSize( wxSize( 25, -1 ) );
|
|
|
|
|
|
|
|
|
|
// Set minimum width for match count to ensure it's visible
|
|
|
|
|
m_matchCount->SetMinSize( wxSize( 60, -1 ) );
|
|
|
|
|
|
|
|
|
|
searchSizer->Add( m_matchCount, 0, wxALIGN_CENTER_VERTICAL|wxRIGHT, 5 );
|
|
|
|
|
searchSizer->Add( m_searchCtrl, 1, wxALIGN_CENTER_VERTICAL|wxRIGHT, 5 );
|
|
|
|
|
searchSizer->Add( m_prevBtn, 0, wxALIGN_CENTER_VERTICAL|wxRIGHT, 2 );
|
|
|
|
|
searchSizer->Add( m_nextBtn, 0, wxALIGN_CENTER_VERTICAL );
|
|
|
|
|
m_searchPanel->SetSizer( searchSizer );
|
|
|
|
|
m_searchPanel->Hide();
|
|
|
|
|
GetSizer()->Insert( 0, m_searchPanel, 0, wxALIGN_RIGHT|wxTOP|wxRIGHT, 5 );
|
|
|
|
|
|
|
|
|
|
m_searchCtrl->Bind( wxEVT_TEXT, &HTML_MESSAGE_BOX::OnSearchText, this );
|
|
|
|
|
m_searchCtrl->Bind( wxEVT_TEXT_ENTER, &HTML_MESSAGE_BOX::OnNext, this );
|
|
|
|
|
m_prevBtn->Bind( wxEVT_BUTTON, &HTML_MESSAGE_BOX::OnPrev, this );
|
|
|
|
|
m_nextBtn->Bind( wxEVT_BUTTON, &HTML_MESSAGE_BOX::OnNext, this );
|
|
|
|
|
|
2018-01-11 09:08:55 +01:00
|
|
|
|
// Gives a default logical size (the actual size depends on the display definition)
|
2019-09-06 14:57:04 -04:00
|
|
|
|
if( aSize != wxDefaultSize )
|
2020-11-16 11:16:44 +00:00
|
|
|
|
setSizeInDU( aSize.x, aSize.y );
|
2018-01-11 09:08:55 +01:00
|
|
|
|
|
2013-06-19 18:11:12 +02:00
|
|
|
|
Center();
|
2019-09-06 14:57:04 -04:00
|
|
|
|
|
2021-11-16 19:39:58 +00:00
|
|
|
|
SetupStandardButtons();
|
2021-09-14 19:26:03 +01:00
|
|
|
|
|
|
|
|
|
reload();
|
|
|
|
|
|
|
|
|
|
Bind( wxEVT_SYS_COLOUR_CHANGED,
|
|
|
|
|
wxSysColourChangedEventHandler( HTML_MESSAGE_BOX::onThemeChanged ), this );
|
2009-07-12 18:27:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-09 12:09:58 -06:00
|
|
|
|
|
2018-05-21 12:23:28 +01:00
|
|
|
|
HTML_MESSAGE_BOX::~HTML_MESSAGE_BOX()
|
|
|
|
|
{
|
|
|
|
|
// Prevent wxWidgets bug which fails to release when closing the window on an <esc>.
|
|
|
|
|
if( m_htmlWindow->HasCapture() )
|
|
|
|
|
m_htmlWindow->ReleaseMouse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-09-14 19:26:03 +01:00
|
|
|
|
void HTML_MESSAGE_BOX::reload()
|
|
|
|
|
{
|
2021-10-10 14:52:37 +02:00
|
|
|
|
m_htmlWindow->SetPage( m_source );
|
2021-09-14 19:26:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void HTML_MESSAGE_BOX::onThemeChanged( wxSysColourChangedEvent &aEvent )
|
|
|
|
|
{
|
|
|
|
|
reload();
|
|
|
|
|
|
|
|
|
|
aEvent.Skip();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-12-09 12:09:58 -06:00
|
|
|
|
void HTML_MESSAGE_BOX::ListClear()
|
2009-07-12 18:27:30 +00:00
|
|
|
|
{
|
2021-09-14 19:26:03 +01:00
|
|
|
|
m_source.clear();
|
|
|
|
|
reload();
|
2009-07-12 18:27:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-09 12:09:58 -06:00
|
|
|
|
|
|
|
|
|
void HTML_MESSAGE_BOX::ListSet( const wxString& aList )
|
2009-07-12 18:27:30 +00:00
|
|
|
|
{
|
2015-01-15 21:01:53 +01:00
|
|
|
|
wxArrayString strings_list;
|
|
|
|
|
wxStringSplit( aList, strings_list, wxChar( '\n' ) );
|
2009-07-13 06:20:18 +00:00
|
|
|
|
|
2013-12-09 12:09:58 -06:00
|
|
|
|
wxString msg = wxT( "<ul>" );
|
|
|
|
|
|
2015-01-15 21:01:53 +01:00
|
|
|
|
for ( unsigned ii = 0; ii < strings_list.GetCount(); ii++ )
|
2009-07-13 06:20:18 +00:00
|
|
|
|
{
|
2013-12-09 12:09:58 -06:00
|
|
|
|
msg += wxT( "<li>" );
|
2015-01-15 21:01:53 +01:00
|
|
|
|
msg += strings_list.Item( ii ) + wxT( "</li>" );
|
2009-07-13 06:20:18 +00:00
|
|
|
|
}
|
2013-12-09 12:09:58 -06:00
|
|
|
|
|
|
|
|
|
msg += wxT( "</ul>" );
|
|
|
|
|
|
2021-09-14 19:26:03 +01:00
|
|
|
|
m_source += msg;
|
|
|
|
|
reload();
|
2009-07-12 18:27:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-09 12:09:58 -06:00
|
|
|
|
|
|
|
|
|
void HTML_MESSAGE_BOX::ListSet( const wxArrayString& aList )
|
2010-10-05 21:54:27 +02:00
|
|
|
|
{
|
2013-12-09 12:09:58 -06:00
|
|
|
|
wxString msg = wxT( "<ul>" );
|
|
|
|
|
|
|
|
|
|
for( unsigned ii = 0; ii < aList.GetCount(); ii++ )
|
2010-10-05 21:54:27 +02:00
|
|
|
|
{
|
2013-12-09 12:09:58 -06:00
|
|
|
|
msg += wxT( "<li>" );
|
|
|
|
|
msg += aList.Item( ii ) + wxT( "</li>" );
|
2010-10-05 21:54:27 +02:00
|
|
|
|
}
|
2013-12-09 12:09:58 -06:00
|
|
|
|
|
|
|
|
|
msg += wxT( "</ul>" );
|
|
|
|
|
|
2021-09-14 19:26:03 +01:00
|
|
|
|
m_source += msg;
|
|
|
|
|
reload();
|
2010-10-05 21:54:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-09 12:09:58 -06:00
|
|
|
|
|
|
|
|
|
void HTML_MESSAGE_BOX::MessageSet( const wxString& message )
|
2009-07-12 18:27:30 +00:00
|
|
|
|
{
|
2021-09-14 19:26:03 +01:00
|
|
|
|
wxString message_value = wxString::Format( wxT( "<b>%s</b><br>" ), message );
|
2013-12-09 12:09:58 -06:00
|
|
|
|
|
2021-09-14 19:26:03 +01:00
|
|
|
|
m_source += message_value;
|
|
|
|
|
reload();
|
2009-07-12 18:27:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-09 12:09:58 -06:00
|
|
|
|
|
|
|
|
|
void HTML_MESSAGE_BOX::AddHTML_Text( const wxString& message )
|
2011-09-04 20:35:14 +02:00
|
|
|
|
{
|
2021-09-14 19:26:03 +01:00
|
|
|
|
m_source += message;
|
|
|
|
|
reload();
|
2011-09-04 20:35:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-24 23:17:14 +01:00
|
|
|
|
|
|
|
|
|
void HTML_MESSAGE_BOX::ShowModeless()
|
|
|
|
|
{
|
2021-09-14 19:26:03 +01:00
|
|
|
|
reload();
|
|
|
|
|
|
2020-08-24 23:17:14 +01:00
|
|
|
|
m_sdbSizer1->Show( false );
|
|
|
|
|
Layout();
|
|
|
|
|
|
|
|
|
|
Show( true );
|
|
|
|
|
}
|
2020-10-24 15:58:13 -04:00
|
|
|
|
|
|
|
|
|
|
2023-02-01 23:23:23 +00:00
|
|
|
|
void HTML_MESSAGE_BOX::OnHTMLLinkClicked( wxHtmlLinkEvent& event )
|
|
|
|
|
{
|
|
|
|
|
wxString href = event.GetLinkInfo().GetHref();
|
|
|
|
|
|
2025-01-10 15:12:49 -08:00
|
|
|
|
if( href.StartsWith( wxS( "https://go.kicad.org/docs" ) ) )
|
2023-02-01 23:23:23 +00:00
|
|
|
|
{
|
|
|
|
|
href.Replace( wxS( "GetMajorMinorVersion" ), GetMajorMinorVersion() );
|
|
|
|
|
}
|
2025-01-10 15:12:49 -08:00
|
|
|
|
|
|
|
|
|
wxURI uri( href );
|
|
|
|
|
wxLaunchDefaultBrowser( uri.BuildURI() );
|
2023-02-01 23:23:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-10-24 15:58:13 -04:00
|
|
|
|
void HTML_MESSAGE_BOX::OnCharHook( wxKeyEvent& aEvent )
|
|
|
|
|
{
|
2025-08-29 10:27:48 -07:00
|
|
|
|
if( m_searchPanel->IsShown() )
|
|
|
|
|
{
|
|
|
|
|
if( aEvent.GetKeyCode() == WXK_ESCAPE )
|
|
|
|
|
{
|
|
|
|
|
HideSearchBar();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else if( aEvent.GetKeyCode() == WXK_RETURN || aEvent.GetKeyCode() == WXK_NUMPAD_ENTER )
|
|
|
|
|
{
|
|
|
|
|
wxCommandEvent evt;
|
|
|
|
|
OnNext( evt );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else if( aEvent.GetKeyCode() == WXK_BACK )
|
|
|
|
|
{
|
|
|
|
|
long from, to;
|
|
|
|
|
m_searchCtrl->GetSelection( &from, &to );
|
|
|
|
|
|
|
|
|
|
if( from == to )
|
|
|
|
|
m_searchCtrl->Remove( std::max( 0L, from - 1 ), from );
|
|
|
|
|
else
|
|
|
|
|
m_searchCtrl->Remove( from, to );
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else if( !aEvent.HasModifiers() && wxIsprint( aEvent.GetUnicodeKey() ) )
|
|
|
|
|
{
|
|
|
|
|
m_searchCtrl->AppendText( wxString( (wxChar) aEvent.GetUnicodeKey() ) );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if( !aEvent.HasModifiers() && wxIsprint( aEvent.GetUnicodeKey() ) )
|
|
|
|
|
{
|
|
|
|
|
ShowSearchBar();
|
|
|
|
|
m_searchCtrl->SetValue( wxString( (wxChar) aEvent.GetUnicodeKey() ) );
|
|
|
|
|
m_currentMatch = 0;
|
|
|
|
|
updateSearch();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-24 15:58:13 -04:00
|
|
|
|
if( aEvent.GetKeyCode() == WXK_ESCAPE )
|
|
|
|
|
{
|
|
|
|
|
wxPostEvent( this, wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK ) );
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-03-02 11:54:13 +00:00
|
|
|
|
else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() == 'A' )
|
|
|
|
|
{
|
|
|
|
|
m_htmlWindow->SelectAll();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else if( aEvent.GetModifiers() == wxMOD_CONTROL && aEvent.GetKeyCode() == 'C' )
|
|
|
|
|
{
|
|
|
|
|
wxLogNull doNotLog; // disable logging of failed clipboard actions
|
|
|
|
|
|
|
|
|
|
if( wxTheClipboard->Open() )
|
|
|
|
|
{
|
|
|
|
|
wxTheClipboard->SetData( new wxTextDataObject( m_htmlWindow->SelectionToText() ) );
|
2021-05-01 23:00:08 +01:00
|
|
|
|
wxTheClipboard->Flush(); // Allow data to be available after closing KiCad
|
2021-03-02 11:54:13 +00:00
|
|
|
|
wxTheClipboard->Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-10-24 15:58:13 -04:00
|
|
|
|
|
|
|
|
|
aEvent.Skip();
|
|
|
|
|
}
|
2025-08-29 10:27:48 -07:00
|
|
|
|
|
|
|
|
|
void HTML_MESSAGE_BOX::OnSearchText( wxCommandEvent& aEvent )
|
|
|
|
|
{
|
|
|
|
|
m_currentMatch = 0;
|
|
|
|
|
updateSearch();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HTML_MESSAGE_BOX::OnNext( wxCommandEvent& aEvent )
|
|
|
|
|
{
|
|
|
|
|
if( !m_matchPos.empty() )
|
|
|
|
|
{
|
|
|
|
|
m_currentMatch = ( m_currentMatch + 1 ) % m_matchPos.size();
|
|
|
|
|
updateSearch();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HTML_MESSAGE_BOX::OnPrev( wxCommandEvent& aEvent )
|
|
|
|
|
{
|
|
|
|
|
if( !m_matchPos.empty() )
|
|
|
|
|
{
|
|
|
|
|
m_currentMatch = ( m_currentMatch + m_matchPos.size() - 1 ) % m_matchPos.size();
|
|
|
|
|
updateSearch();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HTML_MESSAGE_BOX::ShowSearchBar()
|
|
|
|
|
{
|
|
|
|
|
if( !m_searchPanel->IsShown() )
|
|
|
|
|
{
|
|
|
|
|
m_originalSource = m_source;
|
|
|
|
|
m_searchPanel->Show();
|
|
|
|
|
Layout();
|
|
|
|
|
m_searchCtrl->SetFocus();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HTML_MESSAGE_BOX::HideSearchBar()
|
|
|
|
|
{
|
|
|
|
|
if( m_searchPanel->IsShown() )
|
|
|
|
|
{
|
|
|
|
|
m_searchPanel->Hide();
|
|
|
|
|
Layout();
|
|
|
|
|
m_source = m_originalSource;
|
|
|
|
|
reload();
|
|
|
|
|
|
|
|
|
|
// Refocus on the main HTML window so user can press Escape again to close the dialog
|
|
|
|
|
m_htmlWindow->SetFocus();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HTML_MESSAGE_BOX::updateSearch()
|
|
|
|
|
{
|
|
|
|
|
wxString term = m_searchCtrl->GetValue();
|
|
|
|
|
|
|
|
|
|
if( term.IsEmpty() )
|
|
|
|
|
{
|
|
|
|
|
m_source = m_originalSource;
|
|
|
|
|
reload();
|
|
|
|
|
m_matchPos.clear();
|
|
|
|
|
m_matchCount->SetLabel( wxEmptyString );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_matchPos.clear();
|
|
|
|
|
|
|
|
|
|
// Search only in text content, not in HTML tags
|
|
|
|
|
wxString termLower = term.Lower();
|
|
|
|
|
size_t pos = 0;
|
|
|
|
|
bool insideTag = false;
|
|
|
|
|
|
|
|
|
|
while( pos < m_originalSource.length() )
|
|
|
|
|
{
|
|
|
|
|
wxChar ch = m_originalSource[pos];
|
|
|
|
|
|
|
|
|
|
if( ch == '<' )
|
|
|
|
|
{
|
|
|
|
|
insideTag = true;
|
|
|
|
|
}
|
|
|
|
|
else if( ch == '>' )
|
|
|
|
|
{
|
|
|
|
|
insideTag = false;
|
|
|
|
|
pos++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Only search for matches when we're not inside an HTML tag
|
|
|
|
|
if( !insideTag )
|
|
|
|
|
{
|
|
|
|
|
// Check if we have a match starting at this position
|
|
|
|
|
if( pos + termLower.length() <= m_originalSource.length() )
|
|
|
|
|
{
|
|
|
|
|
wxString candidate = m_originalSource.Mid( pos, termLower.length() ).Lower();
|
|
|
|
|
if( candidate == termLower )
|
|
|
|
|
{
|
|
|
|
|
// Verify that this match doesn't span into an HTML tag
|
|
|
|
|
bool validMatch = true;
|
|
|
|
|
for( size_t i = 0; i < termLower.length(); i++ )
|
|
|
|
|
{
|
|
|
|
|
if( pos + i < m_originalSource.length() && m_originalSource[pos + i] == '<' )
|
|
|
|
|
{
|
|
|
|
|
validMatch = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( validMatch )
|
|
|
|
|
{
|
|
|
|
|
m_matchPos.push_back( pos );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pos++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( m_matchPos.empty() )
|
|
|
|
|
{
|
|
|
|
|
m_source = m_originalSource;
|
|
|
|
|
reload();
|
|
|
|
|
m_matchCount->SetLabel( wxS( "0/0" ) );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( m_currentMatch >= (int) m_matchPos.size() )
|
|
|
|
|
m_currentMatch = 0;
|
|
|
|
|
|
|
|
|
|
wxString out;
|
|
|
|
|
size_t start = 0;
|
|
|
|
|
|
|
|
|
|
for( size_t i = 0; i < m_matchPos.size(); ++i )
|
|
|
|
|
{
|
|
|
|
|
size_t idx = m_matchPos[i];
|
|
|
|
|
out += m_originalSource.Mid( start, idx - start );
|
|
|
|
|
wxString matchStr = m_originalSource.Mid( idx, term.length() );
|
|
|
|
|
|
|
|
|
|
// HTML-escape the match string to prevent HTML parsing issues
|
|
|
|
|
wxString escapedMatchStr = matchStr;
|
|
|
|
|
escapedMatchStr.Replace( wxS( "&" ), wxS( "&" ) );
|
|
|
|
|
escapedMatchStr.Replace( wxS( "<" ), wxS( "<" ) );
|
|
|
|
|
escapedMatchStr.Replace( wxS( ">" ), wxS( ">" ) );
|
|
|
|
|
escapedMatchStr.Replace( wxS( "\"" ), wxS( """ ) );
|
|
|
|
|
|
|
|
|
|
if( (int) i == m_currentMatch )
|
|
|
|
|
{
|
|
|
|
|
// Use a unique anchor name for each search to avoid conflicts
|
|
|
|
|
wxString anchorName = wxString::Format( wxS( "kicad_search_%d" ), m_currentMatch );
|
|
|
|
|
out += wxString::Format( wxS( "<a name=\"%s\"></a><span style=\"background-color:#DDAAFF;\">%s</span>" ),
|
|
|
|
|
anchorName, escapedMatchStr );
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
out += wxString::Format( wxS( "<span style=\"background-color:#FFFFAA;\">%s</span>" ), escapedMatchStr );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
start = idx + term.length();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out += m_originalSource.Mid( start );
|
|
|
|
|
m_source = out;
|
|
|
|
|
reload();
|
|
|
|
|
|
|
|
|
|
// Use CallAfter to ensure the HTML is fully loaded before scrolling
|
|
|
|
|
// Only scroll if we have matches and a valid current match
|
|
|
|
|
if( !m_matchPos.empty() && m_currentMatch >= 0 && m_currentMatch < (int)m_matchPos.size() )
|
|
|
|
|
{
|
|
|
|
|
CallAfter( [this]()
|
|
|
|
|
{
|
|
|
|
|
// Try to scroll to the anchor, with fallback if it fails
|
|
|
|
|
wxString anchorName = wxString::Format( wxS( "kicad_search_%d" ), m_currentMatch );
|
|
|
|
|
if( !m_htmlWindow->ScrollToAnchor( anchorName ) )
|
|
|
|
|
{
|
|
|
|
|
// If anchor scrolling fails, try to scroll to approximate position
|
|
|
|
|
// Calculate approximate scroll position based on match location
|
|
|
|
|
if( !m_matchPos.empty() && m_currentMatch < (int)m_matchPos.size() )
|
|
|
|
|
{
|
|
|
|
|
size_t matchPos = m_matchPos[m_currentMatch];
|
|
|
|
|
size_t totalLength = m_originalSource.length();
|
|
|
|
|
if( totalLength > 0 )
|
|
|
|
|
{
|
|
|
|
|
// Scroll to approximate percentage of document
|
|
|
|
|
double ratio = (double)matchPos / (double)totalLength;
|
|
|
|
|
int scrollPos = (int)(ratio * m_htmlWindow->GetScrollRange( wxVERTICAL ));
|
|
|
|
|
m_htmlWindow->Scroll( 0, scrollPos );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_matchCount->SetLabel( wxString::Format( wxS( "%d/%zu" ), m_currentMatch + 1, m_matchPos.size() ) );
|
|
|
|
|
}
|