2 * Copyright 2006, Haiku.
3 * Distributed under the terms of the MIT License.
6 * Stephan Aßmus <superstippi@gmx.de>
9 #include "MoveTransformersCommand.h"
18 #include "Transformer.h"
21 #undef B_TRANSLATION_CONTEXT
22 #define B_TRANSLATION_CONTEXT "Icon-O-Matic-MoveTransformersCmd"
28 MoveTransformersCommand::MoveTransformersCommand(Shape
* container
,
29 Transformer
** transformers
,
33 fContainer(container
),
34 fTransformers(transformers
),
35 fIndices(count
> 0 ? new (nothrow
) int32
[count
] : NULL
),
39 if (!fContainer
|| !fTransformers
|| !fIndices
)
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
)
51 fToIndex
-= itemsBeforeIndex
;
55 MoveTransformersCommand::~MoveTransformersCommand()
57 delete[] fTransformers
;
63 MoveTransformersCommand::InitCheck()
65 if (!fContainer
|| !fTransformers
|| !fIndices
)
68 // analyse the move, don't return B_OK in case
69 // the container state does not change...
71 int32 index
= fIndices
[0];
72 // NOTE: fIndices == NULL if fCount < 1
74 if (index
!= fToIndex
) {
75 // a change is guaranteed
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) {
91 // the container state will not change because of the move
100 MoveTransformersCommand::Perform()
104 // remove shapes from container
105 for (int32 i
= 0; i
< fCount
; i
++) {
107 && !fContainer
->RemoveTransformer(fTransformers
[i
])) {
115 // add shapes to container at the insertion index
116 int32 index
= fToIndex
;
117 for (int32 i
= 0; i
< fCount
; i
++) {
119 && !fContainer
->AddTransformer(fTransformers
[i
], index
++)) {
130 MoveTransformersCommand::Undo()
134 // remove shapes from container
135 for (int32 i
= 0; i
< fCount
; i
++) {
137 && !fContainer
->RemoveTransformer(fTransformers
[i
])) {
145 // add shapes to container at remembered indices
146 for (int32 i
= 0; i
< fCount
; i
++) {
148 && !fContainer
->AddTransformer(fTransformers
[i
], fIndices
[i
])) {
159 MoveTransformersCommand::GetName(BString
& name
)
162 // name << _GetString(MOVE_TRANSFORMERS, "Move Transformers");
164 // name << _GetString(MOVE_TRANSFORMER, "Move Transformer");
166 name
<< B_TRANSLATE("Move Transformers");
168 name
<< B_TRANSLATE("Move Transformer");