xref: /haiku/src/apps/icon-o-matic/shape/commands/RemoveTransformersCommand.cpp (revision 8195a5a835117ab2da405e0d477153570b75d921)
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 "RemoveTransformersCommand.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 RemoveTransformersCommand::RemoveTransformersCommand(Shape* container,
22 													 const int32* indices,
23 													 int32 count)
24 	: Command(),
25 	  fContainer(container),
26 	  fTransformers(count > 0 ? new (nothrow) Transformer*[count] : NULL),
27 	  fIndices(count > 0 ? new (nothrow) int32[count] : NULL),
28 	  fCount(count),
29 	  fTransformersRemoved(false)
30 {
31 	if (!fContainer || !fTransformers || !fIndices)
32 		return;
33 
34 	memcpy(fIndices, indices, sizeof(int32) * fCount);
35 	for (int32 i = 0; i < fCount; i++)
36 		fTransformers[i] = fContainer->TransformerAt(fIndices[i]);
37 }
38 
39 // destructor
40 RemoveTransformersCommand::~RemoveTransformersCommand()
41 {
42 	if (fTransformersRemoved && fTransformers) {
43 		for (int32 i = 0; i < fCount; i++)
44 			delete fTransformers[i];
45 	}
46 	delete[] fTransformers;
47 	delete[] fIndices;
48 }
49 
50 // InitCheck
51 status_t
52 RemoveTransformersCommand::InitCheck()
53 {
54 	return fContainer && fTransformers && fIndices ? B_OK : B_NO_INIT;
55 }
56 
57 // Perform
58 status_t
59 RemoveTransformersCommand::Perform()
60 {
61 	status_t ret = B_OK;
62 
63 	// remove shapes from container
64 	for (int32 i = 0; i < fCount; i++) {
65 		if (fTransformers[i]
66 			&& !fContainer->RemoveTransformer(fTransformers[i])) {
67 			ret = B_ERROR;
68 			break;
69 		}
70 	}
71 	fTransformersRemoved = true;
72 
73 	return ret;
74 }
75 
76 // Undo
77 status_t
78 RemoveTransformersCommand::Undo()
79 {
80 	status_t ret = B_OK;
81 
82 	// add shapes to container at remembered indices
83 	for (int32 i = 0; i < fCount; i++) {
84 		if (fTransformers[i]
85 			&& !fContainer->AddTransformer(fTransformers[i], fIndices[i])) {
86 			ret = B_ERROR;
87 			break;
88 		}
89 	}
90 	fTransformersRemoved = false;
91 
92 	return ret;
93 }
94 
95 // GetName
96 void
97 RemoveTransformersCommand::GetName(BString& name)
98 {
99 //	if (fCount > 1)
100 //		name << _GetString(MOVE_TRANSFORMERS, "Move Transformers");
101 //	else
102 //		name << _GetString(MOVE_TRANSFORMER, "Move Transformer");
103 	if (fCount > 1)
104 		name << "Remove Transformers";
105 	else
106 		name << "Remove Transformer";
107 }
108