1/* 2 * Copyright 2006, 2023, Haiku. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Stephan A��mus <superstippi@gmx.de> 7 * Zardshard 8 */ 9 10#include "RemoveStylesCommand.h" 11 12#include <new> 13 14#include <Catalog.h> 15#include <Locale.h> 16#include <StringFormat.h> 17 18#include "PathSourceShape.h" 19#include "Style.h" 20 21 22#undef B_TRANSLATION_CONTEXT 23#define B_TRANSLATION_CONTEXT "Icon-O-Matic-RemoveStylesCmd" 24 25 26using std::nothrow; 27 28 29RemoveStylesCommand::RemoveStylesCommand(Container<Style>* container, 30 int32* const indices, int32 count) 31 : RemoveCommand<Style>(container, indices, count), 32 fShapes(indices && count > 0 ? new (nothrow) BList[count] : NULL) 33{ 34 if (!fShapes) 35 return; 36 37 // get the shapes associated with each style 38 for (int32 i = 0; i < fCount; i++) { 39 if (fItems[i]) { 40 int32 listenerCount = fItems[i]->CountObservers(); 41 for (int32 j = 0; j < listenerCount; j++) { 42 PathSourceShape* shape 43 = dynamic_cast<PathSourceShape*>(fItems[i]->ObserverAtFast(j)); 44 if (shape != NULL) 45 fShapes[i].AddItem((void*)shape); 46 } 47 } 48 } 49} 50 51 52RemoveStylesCommand::~RemoveStylesCommand() 53{ 54 delete[] fShapes; 55} 56 57 58status_t 59RemoveStylesCommand::InitCheck() 60{ 61 return fShapes ? B_OK : B_NO_INIT; 62} 63 64 65status_t 66RemoveStylesCommand::Perform() 67{ 68 // remove styles from the container 69 status_t ret = RemoveCommand<Style>::Perform(); 70 if (ret != B_OK) 71 return ret; 72 fItemsRemoved = false; // We're not done yet! 73 74 // remove styles from shapes that reference them 75 for (int32 i = 0; i < fCount; i++) { 76 if (!fItems[i]) 77 continue; 78 int32 shapeCount = fShapes[i].CountItems(); 79 for (int32 j = 0; j < shapeCount; j++) { 80 PathSourceShape* shape = (PathSourceShape*)fShapes[i].ItemAtFast(j); 81 shape->SetStyle(fContainer->ItemAt(0)); 82 } 83 } 84 85 fItemsRemoved = true; 86 87 return B_OK; 88} 89 90 91status_t 92RemoveStylesCommand::Undo() 93{ 94 // add styles to the container 95 status_t ret = RemoveCommand<Style>::Undo(); 96 if (ret != B_OK) 97 return ret; 98 fItemsRemoved = true; // We're not done yet! 99 100 // add styles to the shapes which previously referenced them 101 for (int32 i = 0; i < fCount; i++) { 102 if (!fItems[i]) 103 continue; 104 int32 shapeCount = fShapes[i].CountItems(); 105 for (int32 j = 0; j < shapeCount; j++) { 106 PathSourceShape* shape = (PathSourceShape*)fShapes[i].ItemAtFast(j); 107 shape->SetStyle(fItems[i]); 108 } 109 } 110 111 fItemsRemoved = false; 112 113 return ret; 114} 115 116 117void 118RemoveStylesCommand::GetName(BString& name) 119{ 120 static BStringFormat format(B_TRANSLATE("Remove {0, plural, " 121 "one{style} other{styles}}")); 122 format.Format(name, fCount); 123} 124