xref: /haiku/src/apps/icon-o-matic/transformable/ResetTransformationCommand.cpp (revision e1c4049fed1047bdb957b0529e1921e97ef94770)
1 /*
2  * Copyright 2006, Haiku. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Stephan Aßmus <superstippi@gmx.de>
7  */
8 
9 #include "ResetTransformationCommand.h"
10 
11 #include <new>
12 #include <stdio.h>
13 
14 #include <Catalog.h>
15 #include <Locale.h>
16 #include <StringFormat.h>
17 
18 #include "ChannelTransform.h"
19 
20 
21 #undef B_TRANSLATION_CONTEXT
22 #define B_TRANSLATION_CONTEXT "Icon-O-Matic-ResetTransformationCmd"
23 
24 
25 using std::nothrow;
26 
27 // constructor
28 ResetTransformationCommand::ResetTransformationCommand(
29 								Transformable** const objects,
30 								int32 count)
31 	: Command(),
32 	  fObjects(objects && count > 0 ?
33 	  		   new (nothrow) Transformable*[count] : NULL),
34 	  fOriginals(objects && count > 0 ?
35 	  			 new (nothrow) double[
36 	  			 	count * Transformable::matrix_size] : NULL),
37 	  fCount(count)
38 {
39 	if (!fObjects || !fOriginals)
40 		return;
41 
42 	memcpy(fObjects, objects, fCount * sizeof(Transformable*));
43 
44 	int32 matrixSize = Transformable::matrix_size;
45 	for (int32 i = 0; i < fCount; i++) {
46 		fObjects[i]->StoreTo(&fOriginals[matrixSize * i]);
47 	}
48 }
49 
50 // destructor
51 ResetTransformationCommand::~ResetTransformationCommand()
52 {
53 	delete[] fObjects;
54 	delete[] fOriginals;
55 }
56 
57 // InitCheck
58 status_t
59 ResetTransformationCommand::InitCheck()
60 {
61 	return fObjects && fOriginals ? B_OK : B_NO_INIT;
62 }
63 
64 // Perform
65 status_t
66 ResetTransformationCommand::Perform()
67 {
68 	// reset transformations
69 	for (int32 i = 0; i < fCount; i++) {
70 		if (fObjects[i])
71 			fObjects[i]->Reset();
72 	}
73 	return B_OK;
74 }
75 
76 // Undo
77 status_t
78 ResetTransformationCommand::Undo()
79 {
80 	// reset transformations
81 	int32 matrixSize = Transformable::matrix_size;
82 	for (int32 i = 0; i < fCount; i++) {
83 		if (fObjects[i])
84 			fObjects[i]->LoadFrom(&fOriginals[i * matrixSize]);
85 	}
86 	return B_OK;
87 }
88 
89 // GetName
90 void
91 ResetTransformationCommand::GetName(BString& name)
92 {
93 	static BStringFormat format(B_TRANSLATE("Reset {0, plural, "
94 		"one{transformation} other{transformations}}"));
95 	format.Format(name, fCount);
96 }
97