xref: /haiku/src/apps/icon-o-matic/shape/commands/MoveTransformersCommand.cpp (revision 1deede7388b04dbeec5af85cae7164735ea9e70d)
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 "MoveTransformersCommand.h"
10 
11 #include <new>
12 #include <stdio.h>
13 
14 #include <Catalog.h>
15 #include <Locale.h>
16 
17 #include "Shape.h"
18 #include "Transformer.h"
19 
20 
21 #undef B_TRANSLATION_CONTEXT
22 #define B_TRANSLATION_CONTEXT "Icon-O-Matic-MoveTransformersCmd"
23 
24 
25 using std::nothrow;
26 
27 // constructor
28 MoveTransformersCommand::MoveTransformersCommand(Shape* container,
29 									 Transformer** transformers,
30 									 int32 count,
31 									 int32 toIndex)
32 	: Command(),
33 	  fContainer(container),
34 	  fTransformers(transformers),
35 	  fIndices(count > 0 ? new (nothrow) int32[count] : NULL),
36 	  fToIndex(toIndex),
37 	  fCount(count)
38 {
39 	if (!fContainer || !fTransformers || !fIndices)
40 		return;
41 
42 	// init original shape indices and
43 	// adjust toIndex compensating for items that
44 	// are removed before that index
45 	int32 itemsBeforeIndex = 0;
46 	for (int32 i = 0; i < fCount; i++) {
47 		fIndices[i] = fContainer->IndexOf(fTransformers[i]);
48 		if (fIndices[i] >= 0 && fIndices[i] < fToIndex)
49 			itemsBeforeIndex++;
50 	}
51 	fToIndex -= itemsBeforeIndex;
52 }
53 
54 // destructor
55 MoveTransformersCommand::~MoveTransformersCommand()
56 {
57 	delete[] fTransformers;
58 	delete[] fIndices;
59 }
60 
61 // InitCheck
62 status_t
63 MoveTransformersCommand::InitCheck()
64 {
65 	if (!fContainer || !fTransformers || !fIndices)
66 		return B_NO_INIT;
67 
68 	// analyse the move, don't return B_OK in case
69 	// the container state does not change...
70 
71 	int32 index = fIndices[0];
72 		// NOTE: fIndices == NULL if fCount < 1
73 
74 	if (index != fToIndex) {
75 		// a change is guaranteed
76 		return B_OK;
77 	}
78 
79 	// the insertion index is the same as the index of the first
80 	// moved item, a change only occures if the indices of the
81 	// moved items is not contiguous
82 	bool isContiguous = true;
83 	for (int32 i = 1; i < fCount; i++) {
84 		if (fIndices[i] != index + 1) {
85 			isContiguous = false;
86 			break;
87 		}
88 		index = fIndices[i];
89 	}
90 	if (isContiguous) {
91 		// the container state will not change because of the move
92 		return B_ERROR;
93 	}
94 
95 	return B_OK;
96 }
97 
98 // Perform
99 status_t
100 MoveTransformersCommand::Perform()
101 {
102 	status_t ret = B_OK;
103 
104 	// remove shapes from container
105 	for (int32 i = 0; i < fCount; i++) {
106 		if (fTransformers[i]
107 			&& !fContainer->RemoveTransformer(fTransformers[i])) {
108 			ret = B_ERROR;
109 			break;
110 		}
111 	}
112 	if (ret < B_OK)
113 		return ret;
114 
115 	// add shapes to container at the insertion index
116 	int32 index = fToIndex;
117 	for (int32 i = 0; i < fCount; i++) {
118 		if (fTransformers[i]
119 			&& !fContainer->AddTransformer(fTransformers[i], index++)) {
120 			ret = B_ERROR;
121 			break;
122 		}
123 	}
124 
125 	return ret;
126 }
127 
128 // Undo
129 status_t
130 MoveTransformersCommand::Undo()
131 {
132 	status_t ret = B_OK;
133 
134 	// remove shapes from container
135 	for (int32 i = 0; i < fCount; i++) {
136 		if (fTransformers[i]
137 			&& !fContainer->RemoveTransformer(fTransformers[i])) {
138 			ret = B_ERROR;
139 			break;
140 		}
141 	}
142 	if (ret < B_OK)
143 		return ret;
144 
145 	// add shapes to container at remembered indices
146 	for (int32 i = 0; i < fCount; i++) {
147 		if (fTransformers[i]
148 			&& !fContainer->AddTransformer(fTransformers[i], fIndices[i])) {
149 			ret = B_ERROR;
150 			break;
151 		}
152 	}
153 
154 	return ret;
155 }
156 
157 // GetName
158 void
159 MoveTransformersCommand::GetName(BString& name)
160 {
161 //	if (fCount > 1)
162 //		name << _GetString(MOVE_TRANSFORMERS, "Move Transformers");
163 //	else
164 //		name << _GetString(MOVE_TRANSFORMER, "Move Transformer");
165 	if (fCount > 1)
166 		name << B_TRANSLATE("Move Transformers");
167 	else
168 		name << B_TRANSLATE("Move Transformer");
169 }
170