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