diff --git a/aegilib/aegilib.vcproj b/aegilib/aegilib.vcproj index 646b213f8..bdf9a8b9e 100644 --- a/aegilib/aegilib.vcproj +++ b/aegilib/aegilib.vcproj @@ -154,44 +154,12 @@ - - - - - - - - - - - - - - - - @@ -201,7 +169,23 @@ > + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -280,6 +208,10 @@ /> + + + + + + - - - - @@ -353,18 +285,34 @@ RelativePath=".\src\tokenizer.cpp" > + + + + + + + + @@ -381,6 +329,66 @@ RelativePath=".\src\formats\format_ass_style.cpp" > + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/aegilib/include/aegilib/action.h b/aegilib/include/aegilib/action.h index c910101a1..0f44e4149 100644 --- a/aegilib/include/aegilib/action.h +++ b/aegilib/include/aegilib/action.h @@ -37,28 +37,51 @@ #include "gorgonstring.h" namespace Gorgonsub { - // The different types of actions available - enum ActionType { - ACTION_INSERT, - ACTION_REMOVE - }; + // Prototypes + class Model; + class SectionEntry; + class Action; + class Section; + typedef shared_ptr ActionPtr; + typedef shared_ptr
SectionPtr; - // Action class - // This is a modification event + // Action interface class Action { - private: - ActionType type; - shared_ptr data; - String section; - int par1; + protected: + SectionPtr GetSection(const Model &model,const String &name) const; public: - Action(); - Action(ActionType type,shared_ptr data,const String §ion,int par1); + virtual ~Action() {} + virtual ActionPtr GetAntiAction(const Model &model) const =0; + virtual void Execute(Model &model) =0; + }; - ActionType GetType() const { return type; } - shared_ptr GetData() const { return data; } - int GetLineNumber() const { return par1; } - String GetSection() const { return section; } + // Insert line + class ActionInsert : public Action { + private: + shared_ptr entry; + const String section; + int lineNumber; + + public: + ActionInsert(shared_ptr entry,int line,const String §ion); + virtual ~ActionInsert() {} + + ActionPtr GetAntiAction(const Model &model) const; + void Execute(Model &model); + }; + + // Remove line + class ActionRemove : public Action { + private: + const String section; + int lineNumber; + + public: + ActionRemove(int line,const String §ion); + virtual ~ActionRemove() {} + + ActionPtr GetAntiAction(const Model &model) const; + void Execute(Model &model); }; }; diff --git a/aegilib/include/aegilib/actionlist.h b/aegilib/include/aegilib/actionlist.h index 17570f639..67be70986 100644 --- a/aegilib/include/aegilib/actionlist.h +++ b/aegilib/include/aegilib/actionlist.h @@ -53,14 +53,14 @@ namespace Gorgonsub { String actionName; String owner; Model &model; - std::list actions; + std::list actions; bool valid; bool undoAble; ActionList(Model &model,const String actionName,const String owner,bool undoAble); void Start(const String actionName); - void AddAction(const Action &action); - void AddActionStart(const Action &action); + void AddAction(const ActionPtr action); + void AddActionStart(const ActionPtr action); public: ~ActionList(); diff --git a/aegilib/include/aegilib/model.h b/aegilib/include/aegilib/model.h index 32b112120..389250a9d 100644 --- a/aegilib/include/aegilib/model.h +++ b/aegilib/include/aegilib/model.h @@ -55,6 +55,7 @@ namespace Gorgonsub { friend class FormatHandler; friend class ActionList; friend class Controller; + friend class Action; typedef std::list ViewList; typedef std::stack > ActionStack; @@ -69,8 +70,6 @@ namespace Gorgonsub { FormatPtr format; void ProcessActionList(const ActionList &actionList,int type=0); - void DoAction(const Action &action); - Action GetAntiAction(const Action &action); bool CanUndo(const String owner=L"") const; bool CanRedo(const String owner=L"") const; diff --git a/aegilib/src/action.cpp b/aegilib/src/action.cpp index ae50dc701..13b1b3eee 100644 --- a/aegilib/src/action.cpp +++ b/aegilib/src/action.cpp @@ -33,23 +33,80 @@ // Contact: mailto:amz@aegisub.net // -#include "action.h" +#include "gorgonsub.h" using namespace Gorgonsub; -/////////////////////// -// Default constructor -Action::Action() +//////////////////////////////// +// Get a section from the model +SectionPtr Action::GetSection(const Model &model,const String &name) const { + return model.GetSection(name); } -////////////////////////////// -// Initialization constructor -Action::Action(ActionType _type,shared_ptr _data,const String &_section,int _par1) +/////////////////// Insert line ///////////////////////////// + +/////////////// +// Constructor +ActionInsert::ActionInsert(shared_ptr data,int line,const String &sName) +: entry(data), lineNumber(line), section(sName) {} + + +///////////////////////////////// +// Create anti-action for insert +ActionPtr ActionInsert::GetAntiAction(const Model &model) const { - type = _type; - data = _data; - par1 = _par1; - section = _section; + (void) model; + String sect = section; + if (section.IsEmpty()) sect = entry->GetDefaultGroup(); + return ActionPtr(new ActionRemove(lineNumber,sect)); +} + + +///////////////////// +// Execute insertion +void ActionInsert::Execute(Model &model) +{ + // Find the section to insert it on + String sectionName = section; + if (sectionName.IsEmpty()) sectionName = entry->GetDefaultGroup(); + SectionPtr sect = GetSection(model,sectionName); + + // Insert the line + sect->AddEntry(entry,lineNumber); +} + + + +/////////////////// Remove line ///////////////////////////// + + +/////////////// +// Constructor +ActionRemove::ActionRemove(int line,const String &sName) +: lineNumber(line), section(sName) {} + + +///////////////////////////////// +// Create anti-action for remove +ActionPtr ActionRemove::GetAntiAction(const Model &model) const +{ + SectionPtr sect = GetSection(model,section); + SectionEntryPtr entry = sect->GetEntry(lineNumber); + return ActionPtr(new ActionInsert(entry,lineNumber,section)); +} + + +/////////////////// +// Execute removal +void ActionRemove::Execute(Model &model) +{ + // Find the section to remote it from + String sect = section; + if (sect.IsEmpty()) throw Exception(Exception::TODO); // TODO + SectionPtr section = GetSection(model,sect); + + // Remove the line + section->RemoveEntryByIndex(lineNumber); } diff --git a/aegilib/src/actionlist.cpp b/aegilib/src/actionlist.cpp index 9b6fbc733..14aadf6d1 100644 --- a/aegilib/src/actionlist.cpp +++ b/aegilib/src/actionlist.cpp @@ -56,7 +56,7 @@ ActionList::~ActionList() ////////////////////////////// // Add an action to the queue -void ActionList::AddAction(const Action &action) +void ActionList::AddAction(const ActionPtr action) { if (!valid) throw Exception(Exception::Invalid_ActionList); actions.push_back(action); @@ -69,7 +69,7 @@ void ActionList::AddAction(const Action &action) /////////////////////////////////////////// // Add an action to the start of the queue -void ActionList::AddActionStart(const Action &action) +void ActionList::AddActionStart(const ActionPtr action) { if (!valid) throw Exception(Exception::Invalid_ActionList); actions.push_front(action); @@ -106,7 +106,7 @@ void ActionList::Finish() // Create an "insert line" action void ActionList::InsertLine(SectionEntryPtr line,int position,const String section) { - Action action = Action(ACTION_INSERT,line,section,position); + ActionPtr action = ActionPtr (new ActionInsert(line,position,section)); AddAction(action); } @@ -115,6 +115,6 @@ void ActionList::InsertLine(SectionEntryPtr line,int position,const String secti // Create a "remove line" action void ActionList::RemoveLine(int position,const String section) { - Action action = Action(ACTION_REMOVE,SectionEntryPtr(),section,position); + ActionPtr action = ActionPtr (new ActionRemove(position,section)); AddAction(action); } diff --git a/aegilib/src/model.cpp b/aegilib/src/model.cpp index 1b557e779..d8dd0875c 100644 --- a/aegilib/src/model.cpp +++ b/aegilib/src/model.cpp @@ -70,13 +70,13 @@ void Model::ProcessActionList(const ActionList &_actionList,int type) else stack = &undoStack; // Execute actions - std::list::const_iterator cur; + std::list::const_iterator cur; for (cur=actions->actions.begin();cur!=actions->actions.end();cur++) { // Inserts the opposite into the undo action first - if (actions->undoAble) undo->AddActionStart(GetAntiAction(*cur)); + if (actions->undoAble) undo->AddActionStart((*cur)->GetAntiAction(*this)); // Execute the action itself - DoAction(*cur); + (*cur)->Execute(*this); } // Insert into undo stack @@ -90,71 +90,6 @@ void Model::ProcessActionList(const ActionList &_actionList,int type) } -///////////////////// -// Execute an action -void Model::DoAction(const Action &action) -{ - switch (action.GetType()) { - // Insert a line - case ACTION_INSERT: { - // Get the line - SectionEntryPtr entry = static_pointer_cast(action.GetData()); - - // Find the section to insert it on - String sectionName = action.GetSection(); - if (sectionName.IsEmpty()) sectionName = entry->GetDefaultGroup(); - SectionPtr section = GetSection(sectionName); - - // Insert the line - section->AddEntry(entry,action.GetLineNumber()); - return; - } - - // Delete a line - case ACTION_REMOVE: { - // Find the section to remote it from - String sectionName = action.GetSection(); - if (sectionName.IsEmpty()) throw Exception(Exception::TODO); // TODO - SectionPtr section = GetSection(sectionName); - - // Remove the line - section->RemoveEntryByIndex(action.GetLineNumber()); - return; - } - } -} - - -/////////////////////////////////////////// -// Create the action opposite to the input -Action Model::GetAntiAction(const Action &action) -{ - switch (action.GetType()) { - // Create a remove - case ACTION_INSERT: { - // Find the section to insert it on - String section = action.GetSection(); - if (section.IsEmpty()) { - SectionEntryPtr entry = static_pointer_cast(action.GetData()); - section = entry->GetDefaultGroup(); - } - - return Action(ACTION_REMOVE,SectionEntryPtr(),section,action.GetLineNumber()); - } - - // Create an insert - case ACTION_REMOVE: { - int line = action.GetLineNumber(); - const String &sName = action.GetSection(); - SectionPtr section = GetSection(sName); - return Action(ACTION_INSERT,section->GetEntry(line),sName,line); - } - } - - throw Exception(Exception::Invalid_ActionList); -} - - ////////////////// // Load subtitles void Model::Load(wxInputStream &input,const FormatPtr _format,const String encoding) diff --git a/aegilib/test/src/main.cpp b/aegilib/test/src/main.cpp index 601046a1a..8a200fd49 100644 --- a/aegilib/test/src/main.cpp +++ b/aegilib/test/src/main.cpp @@ -97,6 +97,7 @@ int main() { } timer.Pause(); cout << "Done in " << timer.Time() << " ms.\n"; + control.SaveFile(L"subs_out2.ass",L"UTF-8"); } catch (std::exception &e) {