xml-copy-editor-code/src/findreplacepanel.cpp

266 lines
8.0 KiB
C++
Raw Normal View History

2007-09-07 23:17:30 +02:00
#include <wx/fdrepdlg.h>
#include "findreplacepanel.h"
#include "nocasecompare.h"
#include "xmlcopyeditor.h"
2007-09-08 00:25:30 +02:00
BEGIN_EVENT_TABLE ( FindReplacePanel, wxPanel )
EVT_BUTTON ( ID_FINDREPLACE_FIND_NEXT, FindReplacePanel::OnFindNext )
EVT_BUTTON ( ID_FINDREPLACE_REPLACE, FindReplacePanel::OnReplace )
EVT_BUTTON ( ID_FINDREPLACE_REPLACE_ALL, FindReplacePanel::OnReplaceAll )
EVT_IDLE ( FindReplacePanel::OnIdle )
2007-09-07 23:17:30 +02:00
END_EVENT_TABLE()
2007-09-08 00:25:30 +02:00
FindReplacePanel::FindReplacePanel (
wxWindow *parentParameter,
int id,
wxFindReplaceData *findDataParameter,
bool isReplacePanel,
bool isRegexParameter ) : wxPanel ( parentParameter, id )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
parent = parentParameter;
findData = findDataParameter;
incrementalFind = notFoundSet = false;
isRegex = isRegexParameter;
matchCaseMemory = ( findData->GetFlags() ) & wxFR_MATCHCASE;
regexMemory = isRegex;
label1 = new wxStaticText ( this, wxID_ANY, _ ( "Find:" ) );
spacer1 = new wxStaticText ( this, wxID_ANY, _ ( " " ) );
spacer2 = new wxStaticText ( this, wxID_ANY, _ ( " " ) );
int editWidth = 140;
findEdit = new wxTextCtrl (
this,
ID_FINDREPLACE_FIND_NEXT,
_T ( "" ),
wxDefaultPosition,
wxSize ( editWidth, -1 )
);
findEdit->SetValue ( findData->GetFindString() );
label2 = new wxStaticText ( this, wxID_ANY, _ ( "Replace with:" ) );
replaceEdit = new wxTextCtrl (
this,
ID_FINDREPLACE_REPLACE,
_T ( "" ),
wxDefaultPosition,
wxSize ( editWidth, -1 ) );
replaceEdit->SetValue ( findData->GetReplaceString() );
findNextButton = new wxButton (
this,
ID_FINDREPLACE_FIND_NEXT,
_ ( "Find &Next" ),
wxDefaultPosition,
wxDefaultSize,
wxBU_EXACTFIT | wxNO_BORDER );
replaceButton = new wxButton (
this,
ID_FINDREPLACE_REPLACE,
_ ( "&Replace" ),
wxDefaultPosition,
wxDefaultSize,
wxBU_EXACTFIT | wxNO_BORDER );
replaceAllButton = new wxButton (
this,
ID_FINDREPLACE_REPLACE_ALL,
_ ( "Replace &All" ),
wxDefaultPosition,
wxDefaultSize,
wxBU_EXACTFIT | wxNO_BORDER );
matchCaseBox = new wxCheckBox (
this,
ID_FINDREPLACE_MATCH_CASE,
_ ( "&Match case" ) );
size_t flags = findData->GetFlags();
matchCaseBox->SetValue ( flags & wxFR_MATCHCASE );
regexBox = new wxCheckBox (
this,
ID_FINDREPLACE_REGEX,
_ ( "Re&gex" ) );
int sizerOffset = 2;
sizer = new wxBoxSizer ( wxHORIZONTAL );
sizer->Add ( label1, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, sizerOffset );
sizer->Add ( findEdit, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, sizerOffset );
sizer->Add ( label2, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, sizerOffset );
sizer->Add ( replaceEdit, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, sizerOffset );
sizer->Add ( spacer1, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, sizerOffset );
sizer->Add ( findNextButton, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, sizerOffset );
sizer->Add ( replaceButton, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, sizerOffset );
sizer->Add ( replaceAllButton, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, sizerOffset );
sizer->Add ( spacer2, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, sizerOffset );
sizer->Add ( matchCaseBox, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, sizerOffset );
sizer->Add ( regexBox, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, sizerOffset );
sizer->Layout();
this->SetSizer ( sizer );
this->SetSize ( -1, findNextButton->GetSize().GetHeight() + 10 );
findEditLength = findEdit->GetValue().Length();
if ( !isReplacePanel )
{
label2->Hide();
replaceEdit->Hide();
replaceButton->Hide();
replaceAllButton->Hide();
}
refresh();
2007-09-07 23:17:30 +02:00
}
FindReplacePanel::~FindReplacePanel()
2007-09-08 00:25:30 +02:00
{}
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
void FindReplacePanel::OnFindNext ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
findData->SetFindString ( findEdit->GetValue() );
findData->SetReplaceString ( replaceEdit->GetValue() );
incrementalFind = false;
size_t flags = 0;
flags |= wxFR_DOWN;
if ( matchCaseBox->GetValue() )
flags |= wxFR_MATCHCASE;
sendFindEvent ( flags );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void FindReplacePanel::OnReplace ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
wxFindDialogEvent replaceEvent ( wxEVT_COMMAND_FIND_REPLACE, 0 );
replaceEvent.SetFlags ( wxFR_DOWN );
replaceEvent.SetFindString ( findEdit->GetValue() );
replaceEvent.SetReplaceString ( replaceEdit->GetValue() );
parent->ProcessEvent ( replaceEvent );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void FindReplacePanel::OnReplaceAll ( wxCommandEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
wxFindDialogEvent replaceAllEvent ( wxEVT_COMMAND_FIND_REPLACE_ALL, 0 );
replaceAllEvent.SetFlags ( wxFR_DOWN );
replaceAllEvent.SetFindString ( findEdit->GetValue() );
replaceAllEvent.SetReplaceString ( replaceEdit->GetValue() );
parent->ProcessEvent ( replaceAllEvent );
2007-09-07 23:17:30 +02:00
}
void FindReplacePanel::focusOnFind()
{
2007-09-08 00:25:30 +02:00
findEdit->SelectAll();
findEdit->SetFocus();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void FindReplacePanel::OnIdle ( wxIdleEvent& event )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
size_t newLength = findEdit->GetValue().Length();
enableButtons ( ( !newLength ) ? false : true );
bool settingsChanged = false;
if ( matchCaseMemory != matchCaseBox->GetValue() ||
regexMemory != regexBox->GetValue() )
{
settingsChanged = true;
matchCaseMemory = matchCaseBox->GetValue();
regexMemory = regexBox->GetValue();
}
if ( newLength != findEditLength || settingsChanged )
{
incrementalFind = true;
size_t flags = 0;
flags |= wxFR_DOWN;
if ( matchCaseBox->GetValue() )
flags |= wxFR_MATCHCASE;
sendFindEvent ( flags );
findEditLength = newLength;
findData->SetFlags ( flags );
}
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void FindReplacePanel::sendFindEvent ( size_t flags )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
wxFindDialogEvent findEvent ( wxEVT_COMMAND_FIND_NEXT, 0 );
findEvent.SetFlags ( flags );
findEvent.SetFindString ( findEdit->GetValue() );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
MyFrame *frame = ( MyFrame * ) parent;
frame->setStrictScrolling ( true );
frame->ProcessEvent ( findEvent ); // was parent->
frame->setStrictScrolling ( false );
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
findData->SetFindString ( findEdit->GetValue() );
findData->SetReplaceString ( replaceEdit->GetValue() );
2007-09-07 23:17:30 +02:00
}
bool FindReplacePanel::getIncrementalFind()
{
2007-09-08 00:25:30 +02:00
return incrementalFind;
2007-09-07 23:17:30 +02:00
}
void FindReplacePanel::refresh()
{
2007-09-08 00:25:30 +02:00
incrementalFind = false;
findEdit->SetValue ( findData->GetFindString() );
replaceEdit->SetValue ( findData->GetReplaceString() );
size_t flags = findData->GetFlags();
bool matchCase;
matchCase = flags & wxFR_MATCHCASE;
matchCaseBox->SetValue ( matchCase );
matchCaseMemory = matchCase;
regexBox->SetValue ( isRegex );
regexMemory = isRegex;
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void FindReplacePanel::setReplaceVisible ( bool b )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
label2->Show ( b );
replaceEdit->Show ( b );
replaceButton->Show ( b );
replaceAllButton->Show ( b );
sizer->Layout();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void FindReplacePanel::flagNotFound ( bool b )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
if ( notFoundSet && b || !notFoundSet && !b )
return;
2007-09-07 23:17:30 +02:00
2007-09-08 00:25:30 +02:00
notFoundSet = b;
2007-09-07 23:17:30 +02:00
}
bool FindReplacePanel::getRegex()
{
2007-09-08 00:25:30 +02:00
return regexBox->GetValue();
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void FindReplacePanel::setMatchCase ( bool b )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
matchCaseBox->SetValue ( b );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void FindReplacePanel::setRegex ( bool b )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
regexBox->SetValue ( b );
2007-09-07 23:17:30 +02:00
}
2007-09-08 00:25:30 +02:00
void FindReplacePanel::enableButtons ( bool b )
2007-09-07 23:17:30 +02:00
{
2007-09-08 00:25:30 +02:00
findNextButton->Enable ( b );
replaceButton->Enable ( b );
replaceAllButton->Enable ( b );
2007-09-07 23:17:30 +02:00
}