2013-01-11 18:35:39 +01:00
|
|
|
// Copyright (c) 2013, Thomas Goyne <plorkyeran@aegisub.org>
|
2006-01-16 22:02:54 +01:00
|
|
|
//
|
2013-01-11 18:35:39 +01:00
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
2006-01-16 22:02:54 +01:00
|
|
|
//
|
2013-01-11 18:35:39 +01:00
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
2006-01-16 22:02:54 +01:00
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
|
|
|
/// @file dialog_search_replace.cpp
|
|
|
|
/// @brief Find and Search/replace dialogue box and logic
|
|
|
|
/// @ingroup secondary_ui
|
|
|
|
///
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2013-01-11 05:45:59 +01:00
|
|
|
#include "dialog_search_replace.h"
|
2009-09-10 15:06:40 +02:00
|
|
|
|
2011-07-15 06:25:23 +02:00
|
|
|
#include "compat.h"
|
2011-01-16 08:17:36 +01:00
|
|
|
#include "include/aegisub/context.h"
|
2013-01-07 02:50:09 +01:00
|
|
|
#include "options.h"
|
2013-01-11 18:35:39 +01:00
|
|
|
#include "search_replace_engine.h"
|
2013-01-11 05:45:59 +01:00
|
|
|
#include "utils.h"
|
2013-01-11 18:35:39 +01:00
|
|
|
#include "validators.h"
|
2012-11-05 17:20:58 +01:00
|
|
|
|
2013-10-23 23:43:05 +02:00
|
|
|
#include <libaegisub/util.h>
|
|
|
|
|
2013-01-11 05:45:59 +01:00
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
#include <wx/button.h>
|
2013-01-11 06:22:10 +01:00
|
|
|
#include <wx/checkbox.h>
|
|
|
|
#include <wx/combobox.h>
|
|
|
|
#include <wx/radiobox.h>
|
2013-01-30 16:26:16 +01:00
|
|
|
#include <wx/msgdlg.h>
|
2013-01-11 05:45:59 +01:00
|
|
|
#include <wx/sizer.h>
|
|
|
|
#include <wx/stattext.h>
|
2013-01-11 06:22:10 +01:00
|
|
|
#include <wx/textctrl.h>
|
2013-01-11 18:35:39 +01:00
|
|
|
#include <wx/valgen.h>
|
2013-01-11 05:45:59 +01:00
|
|
|
|
2013-01-11 18:35:39 +01:00
|
|
|
DialogSearchReplace::DialogSearchReplace(agi::Context* c, bool replace)
|
|
|
|
: wxDialog(c->parent, -1, replace ? _("Replace") : _("Find"))
|
2013-01-11 17:53:36 +01:00
|
|
|
, c(c)
|
2013-10-23 23:43:05 +02:00
|
|
|
, settings(agi::util::make_unique<SearchReplaceSettings>())
|
2013-01-11 18:35:39 +01:00
|
|
|
, has_replace(replace)
|
2006-01-16 22:02:54 +01:00
|
|
|
{
|
2013-01-11 18:35:39 +01:00
|
|
|
auto recent_find(lagi_MRU_wxAS("Find"));
|
|
|
|
auto recent_replace(lagi_MRU_wxAS("Replace"));
|
|
|
|
|
|
|
|
settings->field = static_cast<SearchReplaceSettings::Field>(OPT_GET("Tool/Search Replace/Field")->GetInt());
|
|
|
|
settings->limit_to = static_cast<SearchReplaceSettings::Limit>(OPT_GET("Tool/Search Replace/Affect")->GetInt());
|
2013-01-04 16:01:50 +01:00
|
|
|
settings->find = recent_find.empty() ? std::string() : from_wx(recent_find.front());
|
|
|
|
settings->replace_with = recent_replace.empty() ? std::string() : from_wx(recent_replace.front());
|
2013-01-11 18:35:39 +01:00
|
|
|
settings->match_case = OPT_GET("Tool/Search Replace/Match Case")->GetBool();
|
|
|
|
settings->use_regex = OPT_GET("Tool/Search Replace/RegExp")->GetBool();
|
2013-01-12 04:35:28 +01:00
|
|
|
settings->ignore_comments = OPT_GET("Tool/Search Replace/Skip Comments")->GetBool();
|
2013-01-12 04:58:09 +01:00
|
|
|
settings->skip_tags = OPT_GET("Tool/Search Replace/Skip Tags")->GetBool();
|
2013-01-13 17:23:50 +01:00
|
|
|
settings->exact_match = false;
|
2013-01-11 18:35:39 +01:00
|
|
|
|
|
|
|
auto find_sizer = new wxFlexGridSizer(2, 2, 5, 15);
|
2013-01-04 16:01:50 +01:00
|
|
|
find_edit = new wxComboBox(this, -1, "", wxDefaultPosition, wxSize(300, -1), recent_find, wxCB_DROPDOWN, StringBinder(&settings->find));
|
2013-01-11 18:35:39 +01:00
|
|
|
find_sizer->Add(new wxStaticText(this, -1, _("Find what:")), wxSizerFlags().Center().Left());
|
|
|
|
find_sizer->Add(find_edit);
|
|
|
|
|
|
|
|
if (has_replace) {
|
2013-01-04 16:01:50 +01:00
|
|
|
replace_edit = new wxComboBox(this, -1, "", wxDefaultPosition, wxSize(300, -1), lagi_MRU_wxAS("Replace"), wxCB_DROPDOWN, StringBinder(&settings->replace_with));
|
2013-01-11 18:35:39 +01:00
|
|
|
find_sizer->Add(new wxStaticText(this, -1, _("Replace with:")), wxSizerFlags().Center().Left());
|
|
|
|
find_sizer->Add(replace_edit);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2013-01-11 18:35:39 +01:00
|
|
|
auto options_sizer = new wxBoxSizer(wxVERTICAL);
|
|
|
|
options_sizer->Add(new wxCheckBox(this, -1, _("&Match case"), wxDefaultPosition, wxDefaultSize, 0, wxGenericValidator(&settings->match_case)), wxSizerFlags().Border(wxBOTTOM));
|
2013-01-12 04:35:28 +01:00
|
|
|
options_sizer->Add(new wxCheckBox(this, -1, _("&Use regular expressions"), wxDefaultPosition, wxDefaultSize, 0, wxGenericValidator(&settings->use_regex)), wxSizerFlags().Border(wxBOTTOM));
|
2013-01-12 04:58:09 +01:00
|
|
|
options_sizer->Add(new wxCheckBox(this, -1, _("&Skip Comments"), wxDefaultPosition, wxDefaultSize, 0, wxGenericValidator(&settings->ignore_comments)), wxSizerFlags().Border(wxBOTTOM));
|
|
|
|
options_sizer->Add(new wxCheckBox(this, -1, _("S&kip Override Tags"), wxDefaultPosition, wxDefaultSize, 0, wxGenericValidator(&settings->skip_tags)));
|
2013-01-11 06:22:10 +01:00
|
|
|
|
2013-01-11 18:35:39 +01:00
|
|
|
auto left_sizer = new wxBoxSizer(wxVERTICAL);
|
|
|
|
left_sizer->Add(find_sizer, wxSizerFlags().DoubleBorder(wxBOTTOM));
|
|
|
|
left_sizer->Add(options_sizer);
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2013-01-11 05:45:59 +01:00
|
|
|
wxString field[] = { _("Text"), _("Style"), _("Actor"), _("Effect") };
|
|
|
|
wxString affect[] = { _("All rows"), _("Selected rows") };
|
2013-01-11 18:35:39 +01:00
|
|
|
auto limit_sizer = new wxBoxSizer(wxHORIZONTAL);
|
|
|
|
limit_sizer->Add(new wxRadioBox(this, -1, _("In Field"), wxDefaultPosition, wxDefaultSize, countof(field), field, 0, wxRA_SPECIFY_COLS, MakeEnumBinder(&settings->field)), wxSizerFlags().Border(wxRIGHT));
|
|
|
|
limit_sizer->Add(new wxRadioBox(this, -1, _("Limit to"), wxDefaultPosition, wxDefaultSize, countof(affect), affect, 0, wxRA_SPECIFY_COLS, MakeEnumBinder(&settings->limit_to)));
|
|
|
|
|
|
|
|
auto find_next = new wxButton(this, -1, _("&Find next"));
|
|
|
|
auto replace_next = new wxButton(this, -1, _("Replace &next"));
|
|
|
|
auto replace_all = new wxButton(this, -1, _("Replace &all"));
|
|
|
|
find_next->SetDefault();
|
|
|
|
|
|
|
|
auto button_sizer = new wxBoxSizer(wxVERTICAL);
|
|
|
|
button_sizer->Add(find_next, wxSizerFlags().Border(wxBOTTOM));
|
|
|
|
button_sizer->Add(replace_next, wxSizerFlags().Border(wxBOTTOM));
|
|
|
|
button_sizer->Add(replace_all, wxSizerFlags().Border(wxBOTTOM));
|
|
|
|
button_sizer->Add(new wxButton(this, wxID_CANCEL));
|
|
|
|
|
|
|
|
if (!has_replace) {
|
|
|
|
button_sizer->Hide(replace_next);
|
|
|
|
button_sizer->Hide(replace_all);
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
2013-01-11 06:22:10 +01:00
|
|
|
|
2013-01-11 18:35:39 +01:00
|
|
|
auto top_sizer = new wxBoxSizer(wxHORIZONTAL);
|
|
|
|
top_sizer->Add(left_sizer, wxSizerFlags().Border());
|
|
|
|
top_sizer->Add(button_sizer, wxSizerFlags().Border());
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2013-01-11 18:35:39 +01:00
|
|
|
auto main_sizer = new wxBoxSizer(wxVERTICAL);
|
|
|
|
main_sizer->Add(top_sizer);
|
|
|
|
main_sizer->Add(limit_sizer, wxSizerFlags().Border());
|
|
|
|
SetSizerAndFit(main_sizer);
|
2006-01-16 22:02:54 +01:00
|
|
|
CenterOnParent();
|
2006-12-30 21:58:24 +01:00
|
|
|
|
2013-01-11 18:35:39 +01:00
|
|
|
find_next->Bind(wxEVT_COMMAND_BUTTON_CLICKED, std::bind(&DialogSearchReplace::FindReplace, this, &SearchReplaceEngine::FindNext));
|
|
|
|
replace_next->Bind(wxEVT_COMMAND_BUTTON_CLICKED, std::bind(&DialogSearchReplace::FindReplace, this, &SearchReplaceEngine::ReplaceNext));
|
|
|
|
replace_all->Bind(wxEVT_COMMAND_BUTTON_CLICKED, std::bind(&DialogSearchReplace::FindReplace, this, &SearchReplaceEngine::ReplaceAll));
|
2011-12-22 22:14:32 +01:00
|
|
|
}
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2006-01-16 22:02:54 +01:00
|
|
|
DialogSearchReplace::~DialogSearchReplace() {
|
|
|
|
}
|
|
|
|
|
2013-01-11 18:35:39 +01:00
|
|
|
void DialogSearchReplace::FindReplace(bool (SearchReplaceEngine::*func)()) {
|
|
|
|
TransferDataFromWindow();
|
2007-03-28 03:11:52 +02:00
|
|
|
|
2013-01-11 18:35:39 +01:00
|
|
|
if (settings->find.empty())
|
|
|
|
return;
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2013-01-11 18:35:39 +01:00
|
|
|
c->search->Configure(*settings);
|
2013-01-04 16:01:50 +01:00
|
|
|
try {
|
|
|
|
(c->search->*func)();
|
|
|
|
}
|
|
|
|
catch (std::exception const& e) {
|
|
|
|
wxMessageBox(to_wx(e.what()), "Error", wxOK | wxICON_ERROR | wxCENTER, this);
|
|
|
|
return;
|
|
|
|
}
|
2007-03-28 03:11:52 +02:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
config::mru->Add("Find", settings->find);
|
2013-01-11 18:35:39 +01:00
|
|
|
if (has_replace)
|
2013-01-04 16:01:50 +01:00
|
|
|
config::mru->Add("Replace", settings->replace_with);
|
2012-03-25 06:05:06 +02:00
|
|
|
|
2013-01-11 18:35:39 +01:00
|
|
|
OPT_SET("Tool/Search Replace/Match Case")->SetBool(settings->match_case);
|
|
|
|
OPT_SET("Tool/Search Replace/RegExp")->SetBool(settings->use_regex);
|
2013-01-12 04:35:28 +01:00
|
|
|
OPT_SET("Tool/Search Replace/Skip Comments")->SetBool(settings->ignore_comments);
|
2013-01-12 04:58:09 +01:00
|
|
|
OPT_SET("Tool/Search Replace/Skip Tags")->SetBool(settings->skip_tags);
|
2013-01-11 18:35:39 +01:00
|
|
|
OPT_SET("Tool/Search Replace/Field")->SetInt(static_cast<int>(settings->field));
|
|
|
|
OPT_SET("Tool/Search Replace/Affect")->SetInt(static_cast<int>(settings->limit_to));
|
2013-01-11 06:31:34 +01:00
|
|
|
|
2006-01-16 22:02:54 +01:00
|
|
|
UpdateDropDowns();
|
|
|
|
}
|
|
|
|
|
2013-01-11 06:31:34 +01:00
|
|
|
static void update_mru(wxComboBox *cb, const char *mru_name) {
|
|
|
|
cb->Freeze();
|
|
|
|
cb->Clear();
|
|
|
|
cb->Append(lagi_MRU_wxAS(mru_name));
|
|
|
|
if (!cb->IsListEmpty())
|
|
|
|
cb->SetSelection(0);
|
|
|
|
cb->Thaw();
|
|
|
|
}
|
|
|
|
|
2006-01-16 22:02:54 +01:00
|
|
|
void DialogSearchReplace::UpdateDropDowns() {
|
2013-01-11 18:35:39 +01:00
|
|
|
update_mru(find_edit, "Find");
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2013-01-11 18:35:39 +01:00
|
|
|
if (has_replace)
|
|
|
|
update_mru(replace_edit, "Replace");
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
2013-01-11 18:35:39 +01:00
|
|
|
void DialogSearchReplace::Show(agi::Context *context, bool replace) {
|
2012-11-13 17:51:01 +01:00
|
|
|
static DialogSearchReplace *diag = nullptr;
|
2006-03-05 21:42:38 +01:00
|
|
|
|
2013-01-11 18:35:39 +01:00
|
|
|
if (diag && replace != diag->has_replace) {
|
2013-01-11 06:31:34 +01:00
|
|
|
// Already opened, but wrong type - destroy and create the right one
|
2006-03-05 21:42:38 +01:00
|
|
|
diag->Destroy();
|
2013-01-11 06:31:34 +01:00
|
|
|
diag = nullptr;
|
2006-03-05 21:42:38 +01:00
|
|
|
}
|
2013-01-11 06:31:34 +01:00
|
|
|
|
|
|
|
if (!diag)
|
|
|
|
diag = new DialogSearchReplace(context, replace);
|
|
|
|
|
2013-01-11 18:35:39 +01:00
|
|
|
diag->find_edit->SetFocus();
|
2013-03-30 01:21:07 +01:00
|
|
|
diag->find_edit->SelectAll();
|
2013-01-11 18:35:39 +01:00
|
|
|
diag->wxDialog::Show();
|
2006-01-16 22:02:54 +01:00
|
|
|
}
|