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 "AddShapesCommand.h" 10 11 #include <new> 12 #include <stdio.h> 13 #include <string.h> 14 15 #include "Selection.h" 16 #include "ShapeContainer.h" 17 #include "Shape.h" 18 19 using std::nothrow; 20 21 // constructor 22 AddShapesCommand::AddShapesCommand(ShapeContainer* container, 23 Shape** const shapes, 24 int32 count, 25 int32 index, 26 Selection* selection) 27 : Command(), 28 fContainer(container), 29 fShapes(shapes && count > 0 ? new (nothrow) Shape*[count] : NULL), 30 fCount(count), 31 fIndex(index), 32 fShapesAdded(false), 33 34 fSelection(selection) 35 { 36 if (!fContainer || !fShapes) 37 return; 38 39 memcpy(fShapes, shapes, sizeof(Shape*) * fCount); 40 } 41 42 // destructor 43 AddShapesCommand::~AddShapesCommand() 44 { 45 if (!fShapesAdded && fShapes) { 46 for (int32 i = 0; i < fCount; i++) 47 fShapes[i]->Release(); 48 } 49 delete[] fShapes; 50 } 51 52 // InitCheck 53 status_t 54 AddShapesCommand::InitCheck() 55 { 56 return fContainer && fShapes ? B_OK : B_NO_INIT; 57 } 58 59 // Perform 60 status_t 61 AddShapesCommand::Perform() 62 { 63 status_t ret = B_OK; 64 65 // add shapes to container 66 int32 index = fIndex; 67 for (int32 i = 0; i < fCount; i++) { 68 if (fShapes[i] && !fContainer->AddShape(fShapes[i], index)) { 69 ret = B_ERROR; 70 // roll back 71 for (int32 j = i - 1; j >= 0; j--) 72 fContainer->RemoveShape(fShapes[j]); 73 break; 74 } 75 index++; 76 } 77 fShapesAdded = true; 78 79 return ret; 80 } 81 82 // Undo 83 status_t 84 AddShapesCommand::Undo() 85 { 86 // remove shapes from container 87 for (int32 i = 0; i < fCount; i++) { 88 if (fSelection) 89 fSelection->Deselect(fShapes[i]); 90 fContainer->RemoveShape(fShapes[i]); 91 } 92 fShapesAdded = false; 93 94 return B_OK; 95 } 96 97 // GetName 98 void 99 AddShapesCommand::GetName(BString& name) 100 { 101 if (fCount > 1) 102 name << "Add Shapes"; 103 else 104 name << "Add Shape"; 105 } 106