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