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