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