Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
/**
* EditorCommands.cpp
*
* Author: Jamey Calabrese
*/
#include "EditorCommands.h"
EditorCommand :: EditorCommand(EditorDataController *edc, FormatterInterface *formatter)
: edc(edc), formatter(formatter)
{
}
EditorCommand :: ~EditorCommand()
{
}
BackspaceCommand :: BackspaceCommand(EDC *edc, FormatterInterface *formatter)
: EditorCommand(edc,formatter)
{
formatter->GetPosition(row,col);
if (!row && !col) {
mode = DUD;
} else if (!col) {
mode = NEWLINE;
record = edc->GetLineLength(row);
} else {
mode = STANDARD;
record = edc->GetCharAt(row,col-1);
}
}
void BackspaceCommand :: Execute()
{
switch (mode)
{
case NEWLINE:
formatter->SetPosition(row-1,edc->GetLineLength(row-1));
edc->DeleteNewLine(row);
break;
case STANDARD:
edc->DeleteCharacter(row, col-1);
formatter->SetPosition(row,col-1);
case DUD:
default:
break;
}
}
void BackspaceCommand :: UnExecute()
{
switch (mode)
{
case NEWLINE:
edc->InsertNewLine(row-1,edc->GetLineLength(row-1)-record);
formatter->SetPosition(row,0);
break;
case STANDARD:
edc->InsertCharacter((char)record,row,col-1);
formatter->SetPosition(row,col);
case DUD:
default:
break;
}
}
DeleteCommand :: DeleteCommand(EDC *edc, FormatterInterface *formatter)
: EditorCommand(edc,formatter)
{
formatter->GetPosition(row,col);
int numRows = edc->GetNumLines();
int lineLen = edc->GetLineLength(row);
if (numRows == (row+1) && col == lineLen) {
mode = DUD;
} else if (col < lineLen) {
mode = MIDDLEOFLINE;
record = edc->GetCharAt(row,col);
} else {
mode = ENDOFLINE;
}
}
void DeleteCommand :: Execute()
{
switch (mode)
{
case MIDDLEOFLINE:
edc->DeleteCharacter(row,col);
formatter->SetPosition(row,col);
break;
case ENDOFLINE:
edc->DeleteNewLine(row+1);
formatter->SetPosition(row,col);
break;
case DUD:
default:
break;
}
}
void DeleteCommand :: UnExecute()
{
switch (mode)
{
case MIDDLEOFLINE:
edc->InsertCharacter(record,row,col);
formatter->SetPosition(row,col);
break;
case ENDOFLINE:
edc->InsertNewLine(row,col);
formatter->SetPosition(row,col);
case DUD:
default:
break;
}
}
InsertCommand :: InsertCommand(EDC *edc, FormatterInterface *formatter, char c)
: EditorCommand(edc,formatter), c(c)
{
formatter->GetPosition(row,col);
}
void InsertCommand :: Execute()
{
edc->InsertCharacter(c,row,col);
formatter->SetPosition(row,col+1);
}
void InsertCommand :: UnExecute()
{
edc->DeleteCharacter(row,col);
formatter->SetPosition(row,col);
}
NewLineCommand :: NewLineCommand(EDC *edc, FormatterInterface *formatter)
: EditorCommand(edc,formatter)
{
formatter->GetPosition(row,col);
}
void NewLineCommand :: Execute()
{
edc->InsertNewLine(row,col);
formatter->SetPosition(row+1,0);
}
void NewLineCommand :: UnExecute()
{
formatter->SetPosition(row,edc->GetLineLength(row));
edc->DeleteNewLine(row+1);
}