* Copyright 2006, Haiku.
* Distributed under the terms of the MIT License.
*
* Authors:
* Stephan Aßmus <superstippi@gmx.de>
*/
#include "InsertPointCommand.h"
#include <new>
#include <stdio.h>
#include <Catalog.h>
#include <Locale.h>
#include "VectorPath.h"
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "Icon-O-Matic-InsertPointCmd"
using std::nothrow;
InsertPointCommand::InsertPointCommand(VectorPath* path,
int32 index,
const int32* selected,
int32 count)
: PathCommand(path),
fIndex(index),
fOldSelection(NULL),
fOldSelectionCount(count)
{
if (fPath && (!fPath->GetPointsAt(fIndex, fPoint, fPointIn, fPointOut)
|| !fPath->GetPointOutAt(fIndex - 1, fPreviousOut)
|| !fPath->GetPointInAt(fIndex + 1, fNextIn))) {
fPath = NULL;
}
if (fOldSelectionCount > 0 && selected) {
fOldSelection = new (nothrow) int32[count];
memcpy(fOldSelection, selected, count * sizeof(int32));
}
}
InsertPointCommand::~InsertPointCommand()
{
delete[] fOldSelection;
}
status_t
InsertPointCommand::Perform()
{
status_t status = InitCheck();
if (status < B_OK)
return status;
fPath->GetPointInAt(fIndex, fPointIn);
fPath->GetPointOutAt(fIndex, fPointOut);
return status;
}
status_t
InsertPointCommand::Undo()
{
status_t status = InitCheck();
if (status < B_OK)
return status;
AutoNotificationSuspender _(fPath);
if (fPath->RemovePoint(fIndex)) {
BPoint previousOut = fPreviousOut;
fPath->GetPointOutAt(fIndex - 1, fPreviousOut);
fPath->SetPointOut(fIndex - 1, previousOut);
BPoint nextIn = fNextIn;
fPath->GetPointInAt(fIndex, fNextIn);
fPath->SetPointIn(fIndex, nextIn);
_Select(fOldSelection, fOldSelectionCount);
} else {
status = B_ERROR;
}
return status;
}
status_t
InsertPointCommand::Redo()
{
status_t status = InitCheck();
if (status < B_OK)
return status;
AutoNotificationSuspender _(fPath);
if (fPath->AddPoint(fPoint, fIndex)) {
fPath->SetPoint(fIndex, fPoint, fPointIn, fPointOut, true);
BPoint previousOut = fPreviousOut;
fPath->GetPointOutAt(fIndex - 1, fPreviousOut);
fPath->SetPointOut(fIndex - 1, previousOut);
BPoint nextIn = fNextIn;
fPath->GetPointInAt(fIndex + 1, fNextIn);
fPath->SetPointIn(fIndex + 1, nextIn);
_Select(&fIndex, 1);
} else {
status = B_ERROR;
}
return status;
}
void
InsertPointCommand::GetName(BString& name)
{
name << B_TRANSLATE("Insert vertex");
}