xref: /haiku/src/apps/icon-o-matic/shape/commands/ChangePointCommand.cpp (revision 3cb015b1ee509d69c643506e8ff573808c86dcfc)
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 "ChangePointCommand.h"
10 
11 #include <new>
12 #include <stdio.h>
13 
14 #include "VectorPath.h"
15 
16 using std::nothrow;
17 
18 // constructor
19 ChangePointCommand::ChangePointCommand(VectorPath* path,
20 									  int32 index,
21  									  const int32* selected,
22 									  int32 count)
23 	: PathCommand(path),
24 	  fIndex(index),
25 	  fOldSelection(NULL),
26 	  fOldSelectionCount(count)
27 {
28 	if (fPath && !fPath->GetPointsAt(fIndex, fPoint, fPointIn, fPointOut, &fConnected))
29 		fPath = NULL;
30 	if (fOldSelectionCount > 0 && selected) {
31 		fOldSelection = new (nothrow) int32[fOldSelectionCount];
32 		memcpy(fOldSelection, selected, fOldSelectionCount * sizeof(int32));
33 	}
34 }
35 
36 // destructor
37 ChangePointCommand::~ChangePointCommand()
38 {
39 	delete[] fOldSelection;
40 }
41 
42 // Perform
43 status_t
44 ChangePointCommand::Perform()
45 {
46 	// path point is already changed
47 	return B_OK;
48 }
49 
50 // Undo
51 status_t
52 ChangePointCommand::Undo()
53 {
54 	status_t status = InitCheck();
55 	if (status < B_OK)
56 		return status;
57 
58 	// set the point to the remembered state and
59 	// save the previous state of the point
60 	BPoint point;
61 	BPoint pointIn;
62 	BPoint pointOut;
63 	bool connected;
64 	if (fPath->GetPointsAt(fIndex, point, pointIn, pointOut, &connected)
65 		&& fPath->SetPoint(fIndex, fPoint, fPointIn, fPointOut, fConnected)) {
66 		// toggle the remembered settings
67 		fPoint = point;
68 		fPointIn = pointIn;
69 		fPointOut = pointOut;
70 		fConnected = connected;
71 		// restore old selection
72 		_Select(fOldSelection, fOldSelectionCount);
73 	} else {
74 		status = B_ERROR;
75 	}
76 
77 	return status;
78 }
79 
80 // Redo
81 status_t
82 ChangePointCommand::Redo()
83 {
84 	status_t status = Undo();
85 	if (status >= B_OK)
86 		_Select(&fIndex, 1);
87 	return status;
88 }
89 
90 // GetName
91 void
92 ChangePointCommand::GetName(BString& name)
93 {
94 //	name << _GetString(MODIFY_CONTROL_POINT, "Modify Control Point");
95 	name << "Modify Control Point";
96 }
97