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