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_TRANSLATION_CONTEXT
22#define B_TRANSLATION_CONTEXT "Icon-O-Matic-SetColorCmd"
23
24
25using std::nothrow;
26
27// constructor
28SetColorCommand::SetColorCommand(Style* style,
29								 const rgb_color& color)
30	: Command(),
31	  fStyle(style),
32	  fColor(color)
33{
34}
35
36// destructor
37SetColorCommand::~SetColorCommand()
38{
39}
40
41// InitCheck
42status_t
43SetColorCommand::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
55status_t
56SetColorCommand::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
67status_t
68SetColorCommand::Undo()
69{
70	return Perform();
71}
72
73// GetName
74void
75SetColorCommand::GetName(BString& name)
76{
77	name << B_TRANSLATE("Change color");
78}
79
80// CombineWithNext
81bool
82SetColorCommand::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