xref: /haiku/src/apps/icon-o-matic/shape/commands/RemoveShapesCommand.cpp (revision b55a57da7173b9af0432bd3e148d03f06161d036)
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 "RemoveShapesCommand.h"
10 
11 #include <new>
12 #include <stdio.h>
13 #include <string.h>
14 
15 #include "ShapeContainer.h"
16 #include "Shape.h"
17 
18 using std::nothrow;
19 
20 // constructor
21 RemoveShapesCommand::RemoveShapesCommand(ShapeContainer* container,
22 										 int32* const indices,
23 										 int32 count)
24 	: Command(),
25 	  fContainer(container),
26 	  fShapes(count > 0 ? new (nothrow) Shape*[count] : NULL),
27 	  fIndices(count > 0 ? new (nothrow) int32[count] : NULL),
28 	  fCount(count),
29 	  fShapesRemoved(false)
30 {
31 	if (!fContainer || !fShapes || !fIndices)
32 		return;
33 
34 	memcpy(fIndices, indices, sizeof(int32) * fCount);
35 	for (int32 i = 0; i < fCount; i++)
36 		fShapes[i] = fContainer->ShapeAt(fIndices[i]);
37 }
38 
39 // destructor
40 RemoveShapesCommand::~RemoveShapesCommand()
41 {
42 	if (fShapesRemoved && fShapes) {
43 		for (int32 i = 0; i < fCount; i++)
44 			fShapes[i]->Release();
45 	}
46 	delete[] fShapes;
47 	delete[] fIndices;
48 }
49 
50 // InitCheck
51 status_t
52 RemoveShapesCommand::InitCheck()
53 {
54 	return fContainer && fShapes && fIndices ? B_OK : B_NO_INIT;
55 }
56 
57 // Perform
58 status_t
59 RemoveShapesCommand::Perform()
60 {
61 	status_t ret = B_OK;
62 
63 	// remove shapes from container
64 	for (int32 i = 0; i < fCount; i++) {
65 		if (fShapes[i] && !fContainer->RemoveShape(fShapes[i])) {
66 			ret = B_ERROR;
67 			break;
68 		}
69 	}
70 	fShapesRemoved = true;
71 
72 	return ret;
73 }
74 
75 // Undo
76 status_t
77 RemoveShapesCommand::Undo()
78 {
79 	status_t ret = B_OK;
80 
81 	// add shapes to container at remembered indices
82 	for (int32 i = 0; i < fCount; i++) {
83 		if (fShapes[i] && !fContainer->AddShape(fShapes[i], fIndices[i])) {
84 			ret = B_ERROR;
85 			break;
86 		}
87 	}
88 	fShapesRemoved = false;
89 
90 	return ret;
91 }
92 
93 // GetName
94 void
95 RemoveShapesCommand::GetName(BString& name)
96 {
97 //	if (fCount > 1)
98 //		name << _GetString(MOVE_MODIFIERS, "Move Shapes");
99 //	else
100 //		name << _GetString(MOVE_MODIFIER, "Move Shape");
101 	if (fCount > 1)
102 		name << "Remove Shapes";
103 	else
104 		name << "Remove Shape";
105 }
106