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