xref: /haiku/src/apps/icon-o-matic/style/SetColorCommand.cpp (revision 1c09002cbee8e797a0f8bbfc5678dfadd39ee1a7)
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 "SetColorCommand.h"
10 
11 #include <new>
12 #include <stdio.h>
13 
14 #include <Catalog.h>
15 #include <Locale.h>
16 
17 #include "GradientTransformable.h"
18 #include "Style.h"
19 
20 
21 #undef B_TRANSLATE_CONTEXT
22 #define B_TRANSLATE_CONTEXT "Icon-O-Matic-SetColorCmd"
23 
24 
25 using std::nothrow;
26 
27 // constructor
28 SetColorCommand::SetColorCommand(Style* style,
29 								 const rgb_color& color)
30 	: Command(),
31 	  fStyle(style),
32 	  fColor(color)
33 {
34 }
35 
36 // destructor
37 SetColorCommand::~SetColorCommand()
38 {
39 }
40 
41 // InitCheck
42 status_t
43 SetColorCommand::InitCheck()
44 {
45 #ifdef __HAIKU__
46 	return fStyle && fStyle->Color() != fColor ? B_OK : B_NO_INIT;
47 #else
48 	rgb_color color = fStyle->Color();
49 	return fStyle && *(uint32*)&color != *(uint32*)&fColor ?
50 		B_OK : B_NO_INIT;
51 #endif
52 }
53 
54 // Perform
55 status_t
56 SetColorCommand::Perform()
57 {
58 	// toggle the color
59 	rgb_color previous = fStyle->Color();
60 	fStyle->SetColor(fColor);
61 	fColor = previous;
62 
63 	return B_OK;
64 }
65 
66 // Undo
67 status_t
68 SetColorCommand::Undo()
69 {
70 	return Perform();
71 }
72 
73 // GetName
74 void
75 SetColorCommand::GetName(BString& name)
76 {
77 	name << B_TRANSLATE("Change Color");
78 }
79 
80 // CombineWithNext
81 bool
82 SetColorCommand::CombineWithNext(const Command* command)
83 {
84 	const SetColorCommand* next
85 		= dynamic_cast<const SetColorCommand*>(command);
86 
87 	if (next && next->fTimeStamp - fTimeStamp < 1000000) {
88 		fTimeStamp = next->fTimeStamp;
89 		// NOTE: next was already performed, but
90 		// when undoing, we want to use our
91 		// remembered color
92 		return true;
93 	}
94 	return false;
95 }
96 
97