* Copyright 2006, Haiku.
* Distributed under the terms of the MIT License.
*
* Authors:
* Stephan Aßmus <superstippi@gmx.de>
*/
#include "SetGradientCommand.h"
#include <new>
#include <stdio.h>
#include <Catalog.h>
#include <Locale.h>
#include "GradientTransformable.h"
#include "Style.h"
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "Icon-O-Matic-SetGradientCmd"
using std::nothrow;
SetGradientCommand::SetGradientCommand(Style* style,
const Gradient* gradient)
: Command(),
fStyle(style),
fGradient(gradient ? new (nothrow) Gradient(*gradient) : NULL)
{
}
SetGradientCommand::~SetGradientCommand()
{
if (fGradient != NULL)
fGradient->ReleaseReference();
}
status_t
SetGradientCommand::InitCheck()
{
if (!fStyle)
return B_NO_INIT;
if (fGradient && fStyle->Gradient()) {
if (*fGradient == *fStyle->Gradient()) {
printf("SetGradientCommand::InitCheck() - same gradients\n");
return B_ERROR;
}
}
return B_OK;
}
status_t
SetGradientCommand::Perform()
{
Gradient* clone = NULL;
if (fGradient) {
clone = new (nothrow) Gradient(*fGradient);
if (!clone)
return B_NO_MEMORY;
}
if (fStyle->Gradient()) {
if (fGradient)
*fGradient = *fStyle->Gradient();
else {
fGradient = new (nothrow) Gradient(*fStyle->Gradient());
if (!fGradient) {
delete clone;
return B_NO_MEMORY;
}
}
} else if (fGradient != NULL) {
fGradient->ReleaseReference();
fGradient = NULL;
}
fStyle->SetGradient(clone);
delete clone;
return B_OK;
}
status_t
SetGradientCommand::Undo()
{
return Perform();
}
void
SetGradientCommand::GetName(BString& name)
{
name << B_TRANSLATE("Edit gradient");
}
bool
SetGradientCommand::CombineWithNext(const Command* command)
{
const SetGradientCommand* next
= dynamic_cast<const SetGradientCommand*>(command);
if (next && next->fTimeStamp - fTimeStamp < 1000000) {
fTimeStamp = next->fTimeStamp;
return true;
}
return false;
}