1/*
2 * Copyright 2006, Haiku.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Stephan A��mus <superstippi@gmx.de>
7 */
8
9#include "AddPointCommand.h"
10
11#include <stdio.h>
12
13#include <Catalog.h>
14#include <Locale.h>
15
16#include "VectorPath.h"
17
18
19#undef B_TRANSLATION_CONTEXT
20#define B_TRANSLATION_CONTEXT "Icon-O-Matic-AddPointCmd"
21
22
23// constructor
24AddPointCommand::AddPointCommand(VectorPath* path,
25								 int32 index,
26								 const int32* selected,
27								 int32 count)
28	: PathCommand(path),
29	  fIndex(index),
30	  fOldSelection(NULL),
31	  fOldSelectionCount(count)
32{
33	if (fOldSelectionCount > 0 && selected) {
34		fOldSelection = new int32[fOldSelectionCount];
35		memcpy(fOldSelection, selected, fOldSelectionCount * sizeof(int32));
36	}
37}
38
39// destructor
40AddPointCommand::~AddPointCommand()
41{
42	delete[] fOldSelection;
43}
44
45// Perform
46status_t
47AddPointCommand::Perform()
48{
49	status_t status = InitCheck();
50	if (status < B_OK)
51		return status;
52
53	// path point is already added,
54	// but we don't know the parameters yet
55	if (!fPath->GetPointsAt(fIndex, fPoint, fPointIn, fPointOut))
56		status = B_NO_INIT;
57
58	return status;
59}
60
61// Undo
62status_t
63AddPointCommand::Undo()
64{
65	status_t status = InitCheck();
66	if (status < B_OK)
67		return status;
68
69	// remove point
70	if (fPath->RemovePoint(fIndex)) {
71		// restore selection before adding point
72		_Select(fOldSelection, fOldSelectionCount);
73	} else {
74		status = B_ERROR;
75	}
76
77	return status;
78}
79
80// Redo
81status_t
82AddPointCommand::Redo()
83{
84	status_t status = InitCheck();
85	if (status < B_OK)
86		return status;
87
88	AutoNotificationSuspender _(fPath);
89
90	// add point again
91	if (fPath->AddPoint(fPoint, fIndex)) {
92		fPath->SetPoint(fIndex, fPoint, fPointIn, fPointOut, true);
93		// select added point
94		_Select(&fIndex, 1);
95	} else {
96		status = B_ERROR;
97	}
98
99	return status;
100}
101
102// GetName
103void
104AddPointCommand::GetName(BString& name)
105{
106	name << B_TRANSLATE("Add vertex");
107}
108