2013-01-26 02:57:46 +01:00
|
|
|
// Copyright (c) 2013, Thomas Goyne <plorkyeran@aegisub.org>
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
|
|
|
#include "subs_controller.h"
|
|
|
|
|
2014-03-04 17:32:29 +01:00
|
|
|
#include "ass_attachment.h"
|
2013-01-26 02:57:46 +01:00
|
|
|
#include "ass_dialogue.h"
|
|
|
|
#include "ass_file.h"
|
2014-03-04 17:32:29 +01:00
|
|
|
#include "ass_info.h"
|
2013-01-26 02:57:46 +01:00
|
|
|
#include "ass_style.h"
|
2014-05-03 18:35:39 +02:00
|
|
|
#include "ass_style_storage.h"
|
2013-02-01 16:28:57 +01:00
|
|
|
#include "charset_detect.h"
|
2013-01-26 02:57:46 +01:00
|
|
|
#include "compat.h"
|
|
|
|
#include "command/command.h"
|
2014-03-27 14:45:53 +01:00
|
|
|
#include "frame_main.h"
|
2013-01-26 02:57:46 +01:00
|
|
|
#include "include/aegisub/context.h"
|
|
|
|
#include "options.h"
|
2014-03-05 02:36:02 +01:00
|
|
|
#include "selection_controller.h"
|
2013-01-26 02:57:46 +01:00
|
|
|
#include "subtitle_format.h"
|
|
|
|
#include "text_file_reader.h"
|
2014-04-17 03:27:54 +02:00
|
|
|
#include "text_selection_controller.h"
|
2013-01-30 15:44:51 +01:00
|
|
|
#include "utils.h"
|
2013-01-26 02:57:46 +01:00
|
|
|
#include "video_context.h"
|
|
|
|
|
|
|
|
#include <libaegisub/fs.h>
|
2013-01-30 04:35:37 +01:00
|
|
|
#include <libaegisub/path.h>
|
2013-01-26 02:57:46 +01:00
|
|
|
#include <libaegisub/util.h>
|
|
|
|
|
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
|
|
|
#include <boost/format.hpp>
|
|
|
|
#include <wx/msgdlg.h>
|
|
|
|
|
2013-01-30 15:40:02 +01:00
|
|
|
namespace {
|
|
|
|
void autosave_timer_changed(wxTimer *timer) {
|
|
|
|
int freq = OPT_GET("App/Auto/Save Every Seconds")->GetInt();
|
|
|
|
if (freq > 0 && OPT_GET("App/Auto/Save")->GetBool())
|
|
|
|
timer->Start(freq * 1000);
|
|
|
|
else
|
|
|
|
timer->Stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-26 02:57:46 +01:00
|
|
|
struct SubsController::UndoInfo {
|
2014-03-08 02:13:23 +01:00
|
|
|
wxString undo_description;
|
|
|
|
int commit_id;
|
|
|
|
|
2014-03-04 17:32:29 +01:00
|
|
|
std::vector<std::pair<std::string, std::string>> script_info;
|
|
|
|
std::vector<AssStyle> styles;
|
|
|
|
std::vector<AssDialogueBase> events;
|
2014-03-08 02:13:23 +01:00
|
|
|
std::vector<AssAttachment> attachments;
|
2014-04-23 16:54:41 +02:00
|
|
|
AegisubExtradataMap extradata;
|
2014-03-04 17:32:29 +01:00
|
|
|
|
2014-03-05 02:36:02 +01:00
|
|
|
mutable std::vector<int> selection;
|
|
|
|
int active_line_id = 0;
|
2014-04-17 03:27:54 +02:00
|
|
|
int pos = 0, sel_start = 0, sel_end = 0;
|
2014-03-05 02:36:02 +01:00
|
|
|
|
|
|
|
UndoInfo(const agi::Context *c, wxString const& d, int commit_id)
|
2014-03-08 02:13:23 +01:00
|
|
|
: undo_description(d)
|
|
|
|
, commit_id(commit_id)
|
|
|
|
, attachments(c->ass->Attachments)
|
2014-04-23 16:54:41 +02:00
|
|
|
, extradata(c->ass->Extradata)
|
2014-03-04 17:32:29 +01:00
|
|
|
{
|
2014-03-07 18:02:24 +01:00
|
|
|
script_info.reserve(c->ass->Info.size());
|
2014-03-07 19:58:51 +01:00
|
|
|
for (auto const& info : c->ass->Info)
|
|
|
|
script_info.emplace_back(info.Key(), info.Value());
|
2014-03-04 17:32:29 +01:00
|
|
|
|
2014-03-07 18:02:24 +01:00
|
|
|
styles.reserve(c->ass->Styles.size());
|
2014-03-07 19:58:51 +01:00
|
|
|
styles.assign(c->ass->Styles.begin(), c->ass->Styles.end());
|
2014-03-07 18:02:24 +01:00
|
|
|
|
|
|
|
events.reserve(c->ass->Events.size());
|
2014-03-07 19:58:51 +01:00
|
|
|
events.assign(c->ass->Events.begin(), c->ass->Events.end());
|
2014-03-04 17:32:29 +01:00
|
|
|
|
2014-03-05 02:36:02 +01:00
|
|
|
UpdateActiveLine(c);
|
|
|
|
UpdateSelection(c);
|
2014-04-17 03:27:54 +02:00
|
|
|
UpdateTextSelection(c);
|
2014-03-04 17:32:29 +01:00
|
|
|
}
|
|
|
|
|
2014-03-05 02:36:02 +01:00
|
|
|
void Apply(agi::Context *c) const {
|
|
|
|
// Keep old lines alive until after the commit is complete
|
|
|
|
AssFile old;
|
|
|
|
old.swap(*c->ass);
|
|
|
|
|
|
|
|
sort(begin(selection), end(selection));
|
|
|
|
|
|
|
|
AssDialogue *active_line = nullptr;
|
2014-03-25 01:15:14 +01:00
|
|
|
Selection new_sel;
|
2014-03-05 02:36:02 +01:00
|
|
|
|
2014-03-04 17:32:29 +01:00
|
|
|
for (auto const& info : script_info)
|
2014-03-07 18:02:24 +01:00
|
|
|
c->ass->Info.push_back(*new AssInfo(info.first, info.second));
|
2014-03-04 17:32:29 +01:00
|
|
|
for (auto const& style : styles)
|
2014-03-07 18:02:24 +01:00
|
|
|
c->ass->Styles.push_back(*new AssStyle(style));
|
2014-03-08 02:13:23 +01:00
|
|
|
c->ass->Attachments = attachments;
|
2014-03-05 02:36:02 +01:00
|
|
|
for (auto const& event : events) {
|
|
|
|
auto copy = new AssDialogue(event);
|
2014-03-07 18:02:24 +01:00
|
|
|
c->ass->Events.push_back(*copy);
|
2014-03-05 02:36:02 +01:00
|
|
|
if (copy->Id == active_line_id)
|
|
|
|
active_line = copy;
|
|
|
|
if (binary_search(begin(selection), end(selection), copy->Id))
|
|
|
|
new_sel.insert(copy);
|
|
|
|
}
|
2014-04-23 16:54:41 +02:00
|
|
|
c->ass->Extradata = extradata;
|
2014-03-05 02:36:02 +01:00
|
|
|
|
|
|
|
c->ass->Commit("", AssFile::COMMIT_NEW);
|
2014-03-12 23:10:47 +01:00
|
|
|
c->selectionController->SetSelectionAndActive(std::move(new_sel), active_line);
|
2014-04-17 03:27:54 +02:00
|
|
|
|
|
|
|
c->textSelectionController->SetInsertionPoint(pos);
|
|
|
|
c->textSelectionController->SetSelection(sel_start, sel_end);
|
2014-03-05 02:36:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateActiveLine(const agi::Context *c) {
|
|
|
|
auto line = c->selectionController->GetActiveLine();
|
|
|
|
if (line)
|
|
|
|
active_line_id = line->Id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateSelection(const agi::Context *c) {
|
|
|
|
auto const& sel = c->selectionController->GetSelectedSet();
|
|
|
|
selection.clear();
|
|
|
|
selection.reserve(sel.size());
|
|
|
|
for (const auto diag : sel)
|
|
|
|
selection.push_back(diag->Id);
|
2014-03-04 17:32:29 +01:00
|
|
|
}
|
2014-04-17 03:27:54 +02:00
|
|
|
|
|
|
|
void UpdateTextSelection(const agi::Context *c) {
|
|
|
|
pos = c->textSelectionController->GetInsertionPoint();
|
|
|
|
sel_start = c->textSelectionController->GetSelectionStart();
|
|
|
|
sel_end = c->textSelectionController->GetSelectionEnd();
|
|
|
|
}
|
2013-01-26 02:57:46 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
SubsController::SubsController(agi::Context *context)
|
|
|
|
: context(context)
|
|
|
|
, undo_connection(context->ass->AddUndoManager(&SubsController::OnCommit, this))
|
2014-04-17 03:27:54 +02:00
|
|
|
, text_selection_connection(context->textSelectionController->AddSelectionListener(&SubsController::OnTextSelectionChanged, this))
|
2013-01-26 02:57:46 +01:00
|
|
|
{
|
2013-01-30 15:40:02 +01:00
|
|
|
autosave_timer_changed(&autosave_timer);
|
2014-04-17 17:50:08 +02:00
|
|
|
OPT_SUB("App/Auto/Save", [=] { autosave_timer_changed(&autosave_timer); });
|
|
|
|
OPT_SUB("App/Auto/Save Every Seconds", [=] { autosave_timer_changed(&autosave_timer); });
|
2013-01-30 15:40:02 +01:00
|
|
|
|
|
|
|
autosave_timer.Bind(wxEVT_TIMER, [=](wxTimerEvent&) {
|
|
|
|
try {
|
|
|
|
auto fn = AutoSave();
|
|
|
|
if (!fn.empty())
|
2014-03-25 17:51:38 +01:00
|
|
|
context->frame->StatusTimeout(wxString::Format(_("File backup saved as \"%s\"."), fn.wstring()));
|
2013-01-30 15:40:02 +01:00
|
|
|
}
|
|
|
|
catch (const agi::Exception& err) {
|
2014-03-25 17:51:38 +01:00
|
|
|
context->frame->StatusTimeout(to_wx("Exception when attempting to autosave file: " + err.GetMessage()));
|
2013-01-30 15:40:02 +01:00
|
|
|
}
|
|
|
|
catch (...) {
|
2014-03-25 17:51:38 +01:00
|
|
|
context->frame->StatusTimeout("Unhandled exception when attempting to autosave file.");
|
2013-01-30 15:40:02 +01:00
|
|
|
}
|
|
|
|
});
|
2013-01-26 02:57:46 +01:00
|
|
|
}
|
|
|
|
|
2014-03-25 22:49:26 +01:00
|
|
|
SubsController::~SubsController() { }
|
|
|
|
|
2014-03-25 01:15:14 +01:00
|
|
|
void SubsController::SetSelectionController(SelectionController *selection_controller) {
|
2014-03-05 02:36:02 +01:00
|
|
|
active_line_connection = context->selectionController->AddActiveLineListener(&SubsController::OnActiveLineChanged, this);
|
|
|
|
selection_connection = context->selectionController->AddSelectionListener(&SubsController::OnSelectionChanged, this);
|
|
|
|
}
|
|
|
|
|
2013-02-01 16:28:57 +01:00
|
|
|
void SubsController::Load(agi::fs::path const& filename, std::string charset) {
|
2013-02-05 04:26:54 +01:00
|
|
|
try {
|
2013-02-01 16:28:57 +01:00
|
|
|
try {
|
2013-11-10 00:56:09 +01:00
|
|
|
if (charset.empty())
|
|
|
|
charset = CharSetDetect::GetEncoding(filename);
|
2013-02-01 16:28:57 +01:00
|
|
|
}
|
2013-11-10 00:56:09 +01:00
|
|
|
catch (agi::UserCancelException const&) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that file isn't actually a timecode file
|
|
|
|
if (charset != "binary") {
|
|
|
|
try {
|
|
|
|
TextFileReader testSubs(filename, charset);
|
|
|
|
std::string cur = testSubs.ReadLineFromFile();
|
|
|
|
if (boost::starts_with(cur, "# timecode")) {
|
|
|
|
context->videoController->LoadTimecodes(filename);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
// if trying to load the file as timecodes fails it's fairly
|
|
|
|
// safe to assume that it is in fact not a timecode file
|
|
|
|
}
|
2013-01-26 02:57:46 +01:00
|
|
|
}
|
|
|
|
|
2013-11-10 00:56:09 +01:00
|
|
|
const SubtitleFormat *reader = SubtitleFormat::GetReader(filename, charset);
|
2013-01-26 02:57:46 +01:00
|
|
|
|
|
|
|
AssFile temp;
|
2014-03-26 16:14:08 +01:00
|
|
|
reader->ReadFile(&temp, filename, context->videoController->FPS(), charset);
|
2013-01-26 02:57:46 +01:00
|
|
|
|
2014-03-07 18:02:24 +01:00
|
|
|
// Make sure the file has at least one style and one dialogue line
|
|
|
|
if (temp.Styles.empty())
|
|
|
|
temp.Styles.push_back(*new AssStyle);
|
|
|
|
if (temp.Events.empty())
|
|
|
|
temp.Events.push_back(*new AssDialogue);
|
2013-01-26 02:57:46 +01:00
|
|
|
|
|
|
|
context->ass->swap(temp);
|
|
|
|
}
|
|
|
|
catch (agi::UserCancelException const&) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
catch (agi::fs::FileNotFound const&) {
|
|
|
|
wxMessageBox(filename.wstring() + " not found.", "Error", wxOK | wxICON_ERROR | wxCENTER, context->parent);
|
|
|
|
config::mru->Remove("Subtitle", filename);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
catch (agi::Exception const& err) {
|
|
|
|
wxMessageBox(to_wx(err.GetChainedMessage()), "Error", wxOK | wxICON_ERROR | wxCENTER, context->parent);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
catch (std::exception const& err) {
|
|
|
|
wxMessageBox(to_wx(err.what()), "Error", wxOK | wxICON_ERROR | wxCENTER, context->parent);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
wxMessageBox("Unknown error", "Error", wxOK | wxICON_ERROR | wxCENTER, context->parent);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetFileName(filename);
|
|
|
|
|
|
|
|
// Push the initial state of the file onto the undo stack
|
|
|
|
undo_stack.clear();
|
|
|
|
redo_stack.clear();
|
|
|
|
autosaved_commit_id = saved_commit_id = commit_id + 1;
|
|
|
|
context->ass->Commit("", AssFile::COMMIT_NEW);
|
|
|
|
|
|
|
|
// Save backup of file
|
|
|
|
if (CanSave() && OPT_GET("App/Auto/Backup")->GetBool()) {
|
|
|
|
auto path_str = OPT_GET("Path/Auto/Backup")->GetString();
|
|
|
|
agi::fs::path path;
|
|
|
|
if (path_str.empty())
|
|
|
|
path = filename.parent_path();
|
|
|
|
else
|
2013-01-30 04:35:37 +01:00
|
|
|
path = config::path->Decode(path_str);
|
2013-01-26 02:57:46 +01:00
|
|
|
agi::fs::CreateDirectory(path);
|
|
|
|
agi::fs::Copy(filename, path/(filename.stem().string() + ".ORIGINAL" + filename.extension().string()));
|
|
|
|
}
|
|
|
|
|
|
|
|
FileOpen(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SubsController::Save(agi::fs::path const& filename, std::string const& encoding) {
|
|
|
|
const SubtitleFormat *writer = SubtitleFormat::GetWriter(filename);
|
|
|
|
if (!writer)
|
|
|
|
throw "Unknown file type.";
|
|
|
|
|
|
|
|
int old_autosaved_commit_id = autosaved_commit_id, old_saved_commit_id = saved_commit_id;
|
|
|
|
try {
|
|
|
|
autosaved_commit_id = saved_commit_id = commit_id;
|
|
|
|
|
2013-01-30 04:35:37 +01:00
|
|
|
// Have to set this now for the sake of things that want to save paths
|
2013-01-26 02:57:46 +01:00
|
|
|
// relative to the script in the header
|
|
|
|
this->filename = filename;
|
2013-01-30 04:35:37 +01:00
|
|
|
config::path->SetToken("?script", filename.parent_path());
|
2013-01-26 02:57:46 +01:00
|
|
|
|
|
|
|
FileSave();
|
|
|
|
|
2014-04-23 22:00:38 +02:00
|
|
|
context->ass->CleanExtradata();
|
|
|
|
|
2014-03-26 16:14:08 +01:00
|
|
|
writer->WriteFile(context->ass.get(), filename, 0, encoding);
|
2013-01-26 02:57:46 +01:00
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
autosaved_commit_id = old_autosaved_commit_id;
|
|
|
|
saved_commit_id = old_saved_commit_id;
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetFileName(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SubsController::Close() {
|
|
|
|
undo_stack.clear();
|
|
|
|
redo_stack.clear();
|
|
|
|
autosaved_commit_id = saved_commit_id = commit_id + 1;
|
|
|
|
filename.clear();
|
2013-10-27 14:59:56 +01:00
|
|
|
AssFile blank;
|
2014-03-07 18:02:24 +01:00
|
|
|
blank.swap(*context->ass);
|
2014-05-03 21:08:37 +02:00
|
|
|
context->ass->LoadDefault(true, config::path->Decode("?user/catalog/" + OPT_GET("Subtitle Format/ASS/Default Style Catalog")->GetString() + ".sty"));
|
2013-01-26 02:57:46 +01:00
|
|
|
context->ass->Commit("", AssFile::COMMIT_NEW);
|
2014-03-25 01:15:14 +01:00
|
|
|
FileOpen(filename);
|
2013-01-26 02:57:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int SubsController::TryToClose(bool allow_cancel) const {
|
|
|
|
if (!IsModified())
|
|
|
|
return wxYES;
|
|
|
|
|
|
|
|
int flags = wxYES_NO;
|
|
|
|
if (allow_cancel)
|
|
|
|
flags |= wxCANCEL;
|
|
|
|
int result = wxMessageBox(wxString::Format(_("Do you want to save changes to %s?"), Filename().wstring()), _("Unsaved changes"), flags, context->parent);
|
|
|
|
if (result == wxYES) {
|
|
|
|
cmd::call("subtitle/save", context);
|
|
|
|
// If it fails saving, return cancel anyway
|
|
|
|
return IsModified() ? wxCANCEL : wxYES;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
agi::fs::path SubsController::AutoSave() {
|
|
|
|
if (commit_id == autosaved_commit_id)
|
|
|
|
return "";
|
|
|
|
|
2013-01-30 04:35:37 +01:00
|
|
|
auto path = config::path->Decode(OPT_GET("Path/Auto/Save")->GetString());
|
2013-01-26 02:57:46 +01:00
|
|
|
if (path.empty())
|
|
|
|
path = filename.parent_path();
|
|
|
|
|
|
|
|
agi::fs::CreateDirectory(path);
|
|
|
|
|
|
|
|
auto name = filename.filename();
|
|
|
|
if (name.empty())
|
|
|
|
name = "Untitled";
|
|
|
|
|
|
|
|
path /= str(boost::format("%s.%s.AUTOSAVE.ass") % name.string() % agi::util::strftime("%Y-%m-%d-%H-%M-%S"));
|
|
|
|
|
2014-03-26 16:14:08 +01:00
|
|
|
SubtitleFormat::GetWriter(path)->WriteFile(context->ass.get(), path, 0);
|
2013-01-26 02:57:46 +01:00
|
|
|
autosaved_commit_id = commit_id;
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SubsController::CanSave() const {
|
|
|
|
try {
|
2014-03-25 22:49:26 +01:00
|
|
|
return SubtitleFormat::GetWriter(filename)->CanSave(context->ass.get());
|
2013-01-26 02:57:46 +01:00
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SubsController::SetFileName(agi::fs::path const& path) {
|
|
|
|
filename = path;
|
2013-01-30 04:35:37 +01:00
|
|
|
config::path->SetToken("?script", path.parent_path());
|
2013-01-26 02:57:46 +01:00
|
|
|
config::mru->Add("Subtitle", path);
|
|
|
|
OPT_SET("Path/Last/Subtitles")->SetString(filename.parent_path().string());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SubsController::OnCommit(AssFileCommit c) {
|
|
|
|
if (c.message.empty() && !undo_stack.empty()) return;
|
|
|
|
|
2013-05-06 06:01:02 +02:00
|
|
|
static int next_commit_id = 1;
|
|
|
|
|
|
|
|
commit_id = next_commit_id++;
|
2013-01-26 02:57:46 +01:00
|
|
|
// Allow coalescing only if it's the last change and the file has not been
|
|
|
|
// saved since the last change
|
|
|
|
if (commit_id == *c.commit_id+1 && redo_stack.empty() && saved_commit_id+1 != commit_id && autosaved_commit_id+1 != commit_id) {
|
|
|
|
// If only one line changed just modify it instead of copying the file
|
2014-03-04 17:32:29 +01:00
|
|
|
if (c.single_line && c.single_line->Group() == AssEntryGroup::DIALOGUE) {
|
|
|
|
for (auto& diag : undo_stack.back().events) {
|
2014-03-07 19:58:51 +01:00
|
|
|
if (diag.Id == c.single_line->Id) {
|
|
|
|
diag = *c.single_line;
|
2014-03-04 17:32:29 +01:00
|
|
|
break;
|
|
|
|
}
|
2013-01-26 02:57:46 +01:00
|
|
|
}
|
2014-03-04 17:32:29 +01:00
|
|
|
*c.commit_id = commit_id;
|
|
|
|
return;
|
2013-01-26 02:57:46 +01:00
|
|
|
}
|
|
|
|
|
2014-03-04 17:32:29 +01:00
|
|
|
undo_stack.pop_back();
|
2013-01-26 02:57:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
redo_stack.clear();
|
|
|
|
|
2014-03-05 02:36:02 +01:00
|
|
|
undo_stack.emplace_back(context, c.message, commit_id);
|
2013-01-26 02:57:46 +01:00
|
|
|
|
|
|
|
int depth = std::max<int>(OPT_GET("Limits/Undo Levels")->GetInt(), 2);
|
|
|
|
while ((int)undo_stack.size() > depth)
|
|
|
|
undo_stack.pop_front();
|
|
|
|
|
|
|
|
if (undo_stack.size() > 1 && OPT_GET("App/Auto/Save on Every Change")->GetBool() && !filename.empty() && CanSave())
|
|
|
|
Save(filename);
|
|
|
|
|
|
|
|
*c.commit_id = commit_id;
|
|
|
|
}
|
|
|
|
|
2014-03-05 02:36:02 +01:00
|
|
|
void SubsController::OnActiveLineChanged() {
|
|
|
|
if (!undo_stack.empty())
|
|
|
|
undo_stack.back().UpdateActiveLine(context);
|
|
|
|
}
|
2013-01-26 02:57:46 +01:00
|
|
|
|
2014-03-05 02:36:02 +01:00
|
|
|
void SubsController::OnSelectionChanged() {
|
|
|
|
if (!undo_stack.empty())
|
|
|
|
undo_stack.back().UpdateSelection(context);
|
2013-01-26 02:57:46 +01:00
|
|
|
}
|
|
|
|
|
2014-04-17 03:27:54 +02:00
|
|
|
void SubsController::OnTextSelectionChanged() {
|
|
|
|
if (!undo_stack.empty())
|
|
|
|
undo_stack.back().UpdateTextSelection(context);
|
|
|
|
}
|
|
|
|
|
2014-03-04 17:32:29 +01:00
|
|
|
void SubsController::Undo() {
|
|
|
|
if (undo_stack.size() <= 1) return;
|
|
|
|
redo_stack.splice(redo_stack.end(), undo_stack, std::prev(undo_stack.end()));
|
2014-03-05 02:36:02 +01:00
|
|
|
|
|
|
|
commit_id = undo_stack.back().commit_id;
|
2014-04-17 03:27:54 +02:00
|
|
|
|
|
|
|
text_selection_connection.Block();
|
2014-03-05 02:36:02 +01:00
|
|
|
undo_stack.back().Apply(context);
|
2014-04-17 03:27:54 +02:00
|
|
|
text_selection_connection.Unblock();
|
2014-03-04 17:32:29 +01:00
|
|
|
}
|
|
|
|
|
2013-01-26 02:57:46 +01:00
|
|
|
void SubsController::Redo() {
|
|
|
|
if (redo_stack.empty()) return;
|
2014-03-04 17:32:29 +01:00
|
|
|
undo_stack.splice(undo_stack.end(), redo_stack, std::prev(redo_stack.end()));
|
2014-03-05 02:36:02 +01:00
|
|
|
|
|
|
|
commit_id = undo_stack.back().commit_id;
|
2014-04-17 03:27:54 +02:00
|
|
|
|
|
|
|
text_selection_connection.Block();
|
2014-03-05 02:36:02 +01:00
|
|
|
undo_stack.back().Apply(context);
|
2014-04-17 03:27:54 +02:00
|
|
|
text_selection_connection.Unblock();
|
2013-01-26 02:57:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
wxString SubsController::GetUndoDescription() const {
|
|
|
|
return IsUndoStackEmpty() ? "" : undo_stack.back().undo_description;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxString SubsController::GetRedoDescription() const {
|
|
|
|
return IsRedoStackEmpty() ? "" : redo_stack.back().undo_description;
|
|
|
|
}
|
|
|
|
|
|
|
|
agi::fs::path SubsController::Filename() const {
|
|
|
|
if (!filename.empty()) return filename;
|
|
|
|
|
|
|
|
// Apple HIG says "untitled" should not be capitalised
|
|
|
|
#ifndef __WXMAC__
|
|
|
|
return _("Untitled").wx_str();
|
|
|
|
#else
|
|
|
|
return _("untitled").wx_str();
|
|
|
|
#endif
|
|
|
|
}
|