xref: /haiku/src/apps/icon-o-matic/shape/commands/AddPointCommand.cpp (revision 1e36cfc2721ef13a187c6f7354dc9cbc485e89d3)
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 "VectorPath.h"
14 
15 // constructor
16 AddPointCommand::AddPointCommand(VectorPath* path,
17 								 int32 index,
18 								 const int32* selected,
19 								 int32 count)
20 	: PathCommand(path),
21 	  fIndex(index),
22 	  fOldSelection(NULL),
23 	  fOldSelectionCount(count)
24 {
25 	if (fOldSelectionCount > 0 && selected) {
26 		fOldSelection = new int32[fOldSelectionCount];
27 		memcpy(fOldSelection, selected, fOldSelectionCount * sizeof(int32));
28 	}
29 }
30 
31 // destructor
32 AddPointCommand::~AddPointCommand()
33 {
34 	delete[] fOldSelection;
35 }
36 
37 // Perform
38 status_t
39 AddPointCommand::Perform()
40 {
41 	status_t status = InitCheck();
42 	if (status < B_OK)
43 		return status;
44 
45 	// path point is already added,
46 	// but we don't know the parameters yet
47 	if (!fPath->GetPointsAt(fIndex, fPoint, fPointIn, fPointOut))
48 		status = B_NO_INIT;
49 
50 	return status;
51 }
52 
53 // Undo
54 status_t
55 AddPointCommand::Undo()
56 {
57 	status_t status = InitCheck();
58 	if (status < B_OK)
59 		return status;
60 
61 	// remove point
62 	if (fPath->RemovePoint(fIndex)) {
63 		// restore selection before adding point
64 		_Select(fOldSelection, fOldSelectionCount);
65 	} else {
66 		status = B_ERROR;
67 	}
68 
69 	return status;
70 }
71 
72 // Redo
73 status_t
74 AddPointCommand::Redo()
75 {
76 	status_t status = InitCheck();
77 	if (status < B_OK)
78 		return status;
79 
80 	AutoNotificationSuspender _(fPath);
81 
82 	// add point again
83 	if (fPath->AddPoint(fPoint, fIndex)) {
84 		fPath->SetPoint(fIndex, fPoint, fPointIn, fPointOut, true);
85 		// select added point
86 		_Select(&fIndex, 1);
87 	} else {
88 		status = B_ERROR;
89 	}
90 
91 	return status;
92 }
93 
94 // GetName
95 void
96 AddPointCommand::GetName(BString& name)
97 {
98 //	name << _GetString(ADD_CONTROL_POINT, "Add Control Point");
99 	name << "Add Control Point";
100 }
101