xref: /haiku/src/apps/icon-o-matic/shape/commands/InsertPointCommand.cpp (revision 16d5c24e533eb14b7b8a99ee9f3ec9ba66335b1e)
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 "InsertPointCommand.h"
10 
11 #include <new>
12 #include <stdio.h>
13 
14 #include "VectorPath.h"
15 
16 using std::nothrow;
17 
18 // constructor
19 InsertPointCommand::InsertPointCommand(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)
29 				  || !fPath->GetPointOutAt(fIndex - 1, fPreviousOut)
30 				  || !fPath->GetPointInAt(fIndex + 1, fNextIn))) {
31 		fPath = NULL;
32 	}
33 	if (fOldSelectionCount > 0 && selected) {
34 		fOldSelection = new (nothrow) int32[count];
35 		memcpy(fOldSelection, selected, count * sizeof(int32));
36 	}
37 }
38 
39 // destructor
40 InsertPointCommand::~InsertPointCommand()
41 {
42 	delete[] fOldSelection;
43 }
44 
45 // Perform
46 status_t
47 InsertPointCommand::Perform()
48 {
49 	status_t status = InitCheck();
50 	if (status < B_OK)
51 		return status;
52 
53 	// path point is already added
54 	// but in/out points might still have changed
55 	fPath->GetPointInAt(fIndex, fPointIn);
56 	fPath->GetPointOutAt(fIndex, fPointOut);
57 
58 	return status;
59 }
60 
61 // Undo
62 status_t
63 InsertPointCommand::Undo()
64 {
65 	status_t status = InitCheck();
66 	if (status < B_OK)
67 		return status;
68 
69 	AutoNotificationSuspender _(fPath);
70 
71 	// remove the inserted point
72 	if (fPath->RemovePoint(fIndex)) {
73 		// remember current previous "out" and restore it
74 		BPoint previousOut = fPreviousOut;
75 		fPath->GetPointOutAt(fIndex - 1, fPreviousOut);
76 		fPath->SetPointOut(fIndex - 1, previousOut);
77 		// remember current next "in" and restore it
78 		BPoint nextIn = fNextIn;
79 		fPath->GetPointInAt(fIndex, fNextIn);
80 		fPath->SetPointIn(fIndex, nextIn);
81 		// restore previous selection
82 		_Select(fOldSelection, fOldSelectionCount);
83 	} else {
84 		status = B_ERROR;
85 	}
86 
87 	return status;
88 }
89 
90 // Redo
91 status_t
92 InsertPointCommand::Redo()
93 {
94 	status_t status = InitCheck();
95 	if (status < B_OK)
96 		return status;
97 
98 
99 	AutoNotificationSuspender _(fPath);
100 
101 	// insert point again
102 	if (fPath->AddPoint(fPoint, fIndex)) {
103 		fPath->SetPoint(fIndex, fPoint, fPointIn, fPointOut, true);
104 		// remember current previous "out" and restore it
105 		BPoint previousOut = fPreviousOut;
106 		fPath->GetPointOutAt(fIndex - 1, fPreviousOut);
107 		fPath->SetPointOut(fIndex - 1, previousOut);
108 		// remember current next "in" and restore it
109 		BPoint nextIn = fNextIn;
110 		fPath->GetPointInAt(fIndex + 1, fNextIn);
111 		fPath->SetPointIn(fIndex + 1, nextIn);
112 		// select inserted point
113 		_Select(&fIndex, 1);
114 	} else {
115 		status = B_ERROR;
116 	}
117 
118 	return status;
119 }
120 
121 // GetName
122 void
123 InsertPointCommand::GetName(BString& name)
124 {
125 //	name << _GetString(INSERT_CONTROL_POINT, "Insert Control Point");
126 	name << "Insert Control Point";
127 }
128 
129