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