Skip to content

Commit

Permalink
Removed the old style 'Replace' dialog with the quick find bar that s…
Browse files Browse the repository at this point in the history
…hows the 'Replace' section

Quick Find Bar: added missing functionality to the 'Replace': "Replace in selected text only"
  • Loading branch information
eranif committed Mar 10, 2016
1 parent ba6a079 commit 960dfe3
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 89 deletions.
1 change: 1 addition & 0 deletions LiteEditor/fileview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2560,6 +2560,7 @@ void FileViewTree::DoCreateProjectContextMenu(wxMenu& menu, const wxString& proj

item = new wxMenuItem(&menu, XRCID("stop_build"), _("Stop Build"), _("Stop Build"));
menu.Append(item);
menu.AppendSeparator();

wxMenu* projectOnly = new wxMenu();
projectOnly->Append(XRCID("build_project_only"), _("Build"));
Expand Down
6 changes: 3 additions & 3 deletions LiteEditor/frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ EVT_UPDATE_UI_RANGE(viewAsMenuItemID, viewAsMenuItemMaxID, clMainFrame::Dispatch
// Search menu
//-------------------------------------------------------
EVT_MENU(wxID_FIND, clMainFrame::DispatchCommandEvent)
EVT_MENU(wxID_REPLACE, clMainFrame::DispatchCommandEvent)
EVT_MENU(wxID_REPLACE, clMainFrame::OnIncrementalReplace)
EVT_MENU(XRCID("find_resource"), clMainFrame::OnFindResourceXXX)
EVT_MENU(XRCID("incremental_search"), clMainFrame::OnIncrementalSearch)
EVT_MENU(XRCID("incremental_replace"), clMainFrame::OnIncrementalReplace)
Expand Down Expand Up @@ -4631,13 +4631,13 @@ void clMainFrame::OnNextPrevTab_UI(wxUpdateUIEvent& e)
void clMainFrame::OnIncrementalSearch(wxCommandEvent& event)
{
wxUnusedVar(event);
GetMainBook()->ShowQuickBar(true);
GetMainBook()->ShowQuickBar(true, false);
}

void clMainFrame::OnIncrementalReplace(wxCommandEvent& event)
{
wxUnusedVar(event);
GetMainBook()->ToggleQuickReplaceBar();
GetMainBook()->ShowQuickBar(true, true);
}

void clMainFrame::OnRetagWorkspace(wxCommandEvent& event)
Expand Down
37 changes: 11 additions & 26 deletions LiteEditor/mainbook.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,12 @@ class MainBook : public wxPanel
void GetRecentlyOpenedFiles(wxArrayString& files);
FileHistory& GetRecentlyOpenedFilesClass() { return m_recentFiles; }
void ShowQuickBarForPlugins() { m_quickFindBar->ShowForPlugins(); }
void ShowQuickBar(bool s = true) { m_quickFindBar->Show(s); }
void ShowQuickBar(const wxString& findWhat) { m_quickFindBar->Show(findWhat); }
void ShowQuickBar(bool s, bool replaceBar = false) { m_quickFindBar->Show(s, replaceBar); }
void ShowQuickBar(const wxString& findWhat, bool replaceBar = false) { m_quickFindBar->Show(findWhat, replaceBar); }
void ShowQuickReplaceBar(bool show) { m_quickFindBar->ShowReplacebar(show); }
void ToggleQuickReplaceBar() { m_quickFindBar->ToggleReplacebar(); }
void ShowMessage(const wxString& message,
bool showHideButton = true,
const wxBitmap& bmp = wxNullBitmap,
const ButtonDetails& btn1 = ButtonDetails(),
const ButtonDetails& btn2 = ButtonDetails(),
const ButtonDetails& btn3 = ButtonDetails(),
const CheckboxDetails& cb = CheckboxDetails());
void ShowMessage(const wxString& message, bool showHideButton = true, const wxBitmap& bmp = wxNullBitmap,
const ButtonDetails& btn1 = ButtonDetails(), const ButtonDetails& btn2 = ButtonDetails(),
const ButtonDetails& btn3 = ButtonDetails(), const CheckboxDetails& cb = CheckboxDetails());

void ShowTabBar(bool b);
void ShowNavBar(bool s = true);
Expand Down Expand Up @@ -152,29 +147,19 @@ class MainBook : public wxPanel

LEditor* NewEditor();

LEditor* OpenFile(const wxString& file_name,
const wxString& projectName = wxEmptyString,
int lineno = wxNOT_FOUND,
long position = wxNOT_FOUND,
OF_extra extra = OF_AddJump,
bool preserveSelection = true);
LEditor* OpenFile(const wxString& file_name, const wxString& projectName = wxEmptyString, int lineno = wxNOT_FOUND,
long position = wxNOT_FOUND, OF_extra extra = OF_AddJump, bool preserveSelection = true);
LEditor* OpenFile(const BrowseRecord& rec)
{
return OpenFile(rec.filename, rec.project, rec.lineno, rec.position, OF_None, false);
}

bool AddPage(wxWindow* win,
const wxString& text,
const wxString& tooltip = wxEmptyString,
const wxBitmap& bmp = wxNullBitmap,
bool selected = false,
int insert_at_index = wxNOT_FOUND);
bool AddPage(wxWindow* win, const wxString& text, const wxString& tooltip = wxEmptyString,
const wxBitmap& bmp = wxNullBitmap, bool selected = false, int insert_at_index = wxNOT_FOUND);
bool SelectPage(wxWindow* win);

bool UserSelectFiles(std::vector<std::pair<wxFileName, bool> >& files,
const wxString& title,
const wxString& caption,
bool cancellable = true);
bool UserSelectFiles(std::vector<std::pair<wxFileName, bool> >& files, const wxString& title,
const wxString& caption, bool cancellable = true);

bool SaveAll(bool askUser, bool includeUntitled);

Expand Down
109 changes: 57 additions & 52 deletions LiteEditor/quickfindbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ QuickFindBar::QuickFindBar(wxWindow* parent, wxWindowID id)
, m_eventsConnected(false)
, m_optionsWindow(NULL)
, m_regexType(kRegexNone)
, m_disableTextUpdateEvent(false)
, m_replaceInSelection(false)
{
m_bar = new wxFlatButtonBar(this, wxFlatButton::kThemeNormal, 0, 10);

//-------------------------------------------------------------
// Find / Replace bar
//-------------------------------------------------------------
// [x][A]["][*][/][..............][find][find prev][find all]
// [-][-][-][-][-][..............][replace][replace all]
// [x][A]["][*][/][!][..............][find][find prev][find all]
// [-][-][-][-][|][-][..............][replace][replace all]
//-------------------------------------------------------------
m_bar->SetExpandableColumn(6);
GetSizer()->Add(m_bar, 1, wxEXPAND | wxALL, 2);
Expand Down Expand Up @@ -178,7 +178,13 @@ QuickFindBar::QuickFindBar(wxWindow* parent, wxWindowID id)
m_bar->AddSpacer(0);
m_bar->AddSpacer(0);
m_bar->AddSpacer(0);
m_bar->AddSpacer(0);
// Replace in selection
m_replaceInSelectionButton = m_bar->AddButton("", bmps->LoadBitmap("text_selection"), wxSize(24, -1));
m_replaceInSelectionButton->SetTogglable(true);
m_replaceInSelectionButton->Bind(wxEVT_CMD_FLATBUTTON_CLICK, &QuickFindBar::OnReplaceInSelection, this);
m_replaceInSelectionButton->Bind(wxEVT_UPDATE_UI, &QuickFindBar::OnReplaceInSelectionUI, this);
m_replaceInSelectionButton->Bind(wxEVT_KEY_DOWN, &QuickFindBar::OnKeyDown, this);
m_replaceInSelectionButton->SetToolTip(_("Replace in selected text"));

// This spacer is for alinging with the "no match" bitmap
m_bar->AddSpacer(0);
Expand All @@ -202,7 +208,7 @@ QuickFindBar::QuickFindBar(wxWindow* parent, wxWindowID id)
m_buttonReplace->Bind(wxEVT_KEY_DOWN, &QuickFindBar::OnKeyDown, this);

m_buttonReplaceAll->Bind(wxEVT_BUTTON, &QuickFindBar::OnReplaceAll, this);
m_buttonReplaceAll->Bind(wxEVT_UPDATE_UI, &QuickFindBar::OnButtonReplaceUI, this);
m_buttonReplaceAll->Bind(wxEVT_UPDATE_UI, &QuickFindBar::OnButtonReplaceAllUI, this);
m_buttonReplaceAll->Bind(wxEVT_KEY_DOWN, &QuickFindBar::OnKeyDown, this);

bool showreplace = EditorConfigST::Get()->GetOptions()->GetShowReplaceBar();
Expand Down Expand Up @@ -239,20 +245,12 @@ QuickFindBar::QuickFindBar(wxWindow* parent, wxWindowID id)
m_replaceWith->Append(clConfig::Get().GetQuickFindReplaceItems());
}

bool QuickFindBar::Show(bool show)
bool QuickFindBar::Show(bool show, bool replaceBar)
{
if(!m_sci && show) {
return false;
}
return DoShow(show, wxEmptyString);
}

void QuickFindBar::ToggleReplacebar()
{
if(!m_sci || !IsShown()) {
return;
}
DoToggleReplacebar();
return DoShow(show, wxEmptyString, replaceBar);
}

void QuickFindBar::DoSearch(size_t searchFlags)
Expand Down Expand Up @@ -366,13 +364,7 @@ void QuickFindBar::OnPrev(wxCommandEvent& e)
DoSearch(flags);
}

void QuickFindBar::OnText(wxCommandEvent& e)
{
e.Skip();
if(!m_disableTextUpdateEvent) {
CallAfter(&QuickFindBar::DoSearch, kSearchForward);
}
}
void QuickFindBar::OnText(wxCommandEvent& e) { e.Skip(); }

void QuickFindBar::OnKeyDown(wxKeyEvent& e)
{
Expand Down Expand Up @@ -550,12 +542,6 @@ void QuickFindBar::OnReplace(wxCommandEvent& event)
DoSearch(kSearchForward);
}

void QuickFindBar::OnReplaceUI(wxUpdateUIEvent& e)
{
e.Enable(ManagerST::Get()->IsShutdownInProgress() == false && m_sci && !m_sci->GetReadOnly() &&
m_sci->GetLength() > 0 && !m_findWhat->GetValue().IsEmpty());
}

void QuickFindBar::OnReplaceEnter(wxCommandEvent& e)
{
wxUnusedVar(e);
Expand All @@ -567,22 +553,22 @@ void QuickFindBar::SetEditor(wxStyledTextCtrl* sci)
{
m_sci = sci;
if(!m_sci) {
DoShow(false, "");
DoShow(false, "", false);
return;
}
}

int QuickFindBar::GetCloseButtonId() { return ID_TOOL_CLOSE; }

bool QuickFindBar::Show(const wxString& findWhat)
bool QuickFindBar::Show(const wxString& findWhat, bool replaceBar)
{
// Same as Show() but set the 'findWhat' field with findWhat
if(!m_sci) return false;

return DoShow(true, findWhat);
return DoShow(true, findWhat, replaceBar);
}

bool QuickFindBar::DoShow(bool s, const wxString& findWhat)
bool QuickFindBar::DoShow(bool s, const wxString& findWhat, bool replaceBar)
{
bool res = wxPanel::Show(s);

Expand Down Expand Up @@ -639,25 +625,16 @@ bool QuickFindBar::DoShow(bool s, const wxString& findWhat)
m_noMatchBmp->Refresh();
m_noMatchBmp->SetToolTip(wxEmptyString);
}
ShowReplacebar(replaceBar);
return res;
}

void QuickFindBar::DoToggleReplacebar()
{
OptionsConfigPtr options = EditorConfigST::Get()->GetOptions();
bool show = !options->GetShowReplaceBar();

options->SetShowReplaceBar(show);
EditorConfigST::Get()->SetOptions(options);

ShowReplacebar(show);
}

void QuickFindBar::ShowReplacebar(bool show)
{
m_replaceWith->Show(show);
m_buttonReplace->Show(show);
m_buttonReplaceAll->Show(show);
m_replaceInSelectionButton->Show(show);
m_bar->GetSizer()->Layout();
if(IsShown()) {
clMainFrame::Get()->SendSizeEvent(); // Needed to show/hide the 'replace' bar itself
Expand Down Expand Up @@ -950,9 +927,9 @@ bool QuickFindBar::ShowForPlugins()
{
m_sci = DoCheckPlugins();
if(!m_sci) {
return DoShow(false, "");
return DoShow(false, "", false);
} else {
return DoShow(true, "");
return DoShow(true, "", false);
}
}

Expand Down Expand Up @@ -1048,21 +1025,17 @@ void QuickFindBar::DoUpdateSearchHistory()
{
wxString findWhat = m_findWhat->GetValue();
if(findWhat.IsEmpty()) return;
m_disableTextUpdateEvent = true;
m_findWhat->Clear();
m_findWhat->ChangeValue(findWhat);
m_findWhat->Append(clConfig::Get().GetQuickFindSearchItems());
m_disableTextUpdateEvent = false;
}

void QuickFindBar::DoUpdateReplaceHistory()
{
m_disableTextUpdateEvent = true;
int where = m_replaceWith->FindString(m_replaceWith->GetValue());
if(where == wxNOT_FOUND) {
m_replaceWith->Insert(m_replaceWith->GetValue(), 0);
}
m_disableTextUpdateEvent = false;
}

void QuickFindBar::OnButtonNext(wxCommandEvent& e) { OnNext(e); }
Expand All @@ -1080,7 +1053,12 @@ size_t QuickFindBar::DoGetSearchFlags()

void QuickFindBar::OnFindAll(wxCommandEvent& e) { DoSelectAll(true); }
void QuickFindBar::OnButtonReplace(wxCommandEvent& e) { OnReplace(e); }
void QuickFindBar::OnButtonReplaceUI(wxUpdateUIEvent& e) { e.Enable(!m_findWhat->GetValue().IsEmpty()); }

void QuickFindBar::OnButtonReplaceUI(wxUpdateUIEvent& e)
{
e.Enable(!m_findWhat->GetValue().IsEmpty() && !m_replaceInSelection);
}

void QuickFindBar::OnHideBar(wxFlatButtonEvent& e) { OnHide(e); }
void QuickFindBar::OnFindMouseWheel(wxMouseEvent& e)
{
Expand Down Expand Up @@ -1142,6 +1120,12 @@ void QuickFindBar::DoFixRegexParen(wxString& findwhat)
void QuickFindBar::DoSetCaretAtEndOfText() { m_findWhat->SetInsertionPointEnd(); }

void QuickFindBar::OnReplaceAll(wxCommandEvent& e)
{
wxUnusedVar(e);
DoReplaceAll(m_replaceInSelection);
}

void QuickFindBar::DoReplaceAll(bool selectionOnly)
{
if(!m_sci || m_sci->GetLength() == 0 || m_findWhat->GetValue().IsEmpty()) return;
clGetManager()->SetStatusMessage(wxEmptyString);
Expand All @@ -1155,16 +1139,25 @@ void QuickFindBar::OnReplaceAll(wxCommandEvent& e)
DoFixRegexParen(findwhat);
}

int from, to;
if(selectionOnly && m_sci->GetSelectedText().IsEmpty()) return;
if(selectionOnly) {
m_sci->GetSelection(&from, &to);
} else {
from = 0;
to = m_sci->GetLastPosition();
}

// Ensure that we have at least one match before we continue
if(m_sci->FindText(0, m_sci->GetLastPosition(), findwhat, searchFlags) == wxNOT_FOUND) {
if(m_sci->FindText(from, to, findwhat, searchFlags) == wxNOT_FOUND) {
clGetManager()->SetStatusMessage(_("No match found"), 2);
return;
}

int curpos = m_sci->GetCurrentPos();

// We got at least one match
m_sci->SetCurrentPos(0);
m_sci->SetCurrentPos(from);
m_sci->SetSelectionEnd(0);
m_sci->SetSelectionStart(0);

Expand Down Expand Up @@ -1222,6 +1215,12 @@ void QuickFindBar::OnReplaceAll(wxCommandEvent& e)
m_sci->SetSelectionEnd(newpos);
m_sci->SearchAnchor();
pos = m_sci->SearchNext(searchFlags, findwhat);

// When replacing in "Selected Text" don't count matches
// that are out of range
if(selectionOnly && (pos > to)) {
break;
}
++matchesCount;
}
m_sci->EndUndoAction();
Expand All @@ -1243,6 +1242,12 @@ void QuickFindBar::OnReplaceAll(wxCommandEvent& e)
clGetManager()->SetStatusMessage(message, 5);
}

void QuickFindBar::OnReplaceInSelection(wxFlatButtonEvent& e) { m_replaceInSelection = e.IsChecked(); }

void QuickFindBar::OnReplaceInSelectionUI(wxUpdateUIEvent& event) {}

void QuickFindBar::OnButtonReplaceAllUI(wxUpdateUIEvent& e) { e.Enable(!m_findWhat->GetValue().IsEmpty()); }

clNoMatchBitmap::clNoMatchBitmap(wxWindow* parent)
: wxPanel(parent)
, m_visible(false)
Expand Down
Loading

0 comments on commit 960dfe3

Please sign in to comment.