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 "SetGradientCommand.h" 10 11 #include <new> 12 #include <stdio.h> 13 14 #include "GradientTransformable.h" 15 #include "Style.h" 16 17 using std::nothrow; 18 19 // constructor 20 SetGradientCommand::SetGradientCommand(Style* style, 21 const Gradient* gradient) 22 : Command(), 23 fStyle(style), 24 fGradient(gradient ? new (nothrow) Gradient(*gradient) : NULL) 25 { 26 } 27 28 // destructor 29 SetGradientCommand::~SetGradientCommand() 30 { 31 delete fGradient; 32 } 33 34 // InitCheck 35 status_t 36 SetGradientCommand::InitCheck() 37 { 38 if (!fStyle) 39 return B_NO_INIT; 40 if (fGradient && fStyle->Gradient()) { 41 if (*fGradient == *fStyle->Gradient()) { 42 printf("SetGradientCommand::InitCheck() - same gradients\n"); 43 return B_ERROR; 44 } 45 } 46 return B_OK; 47 } 48 49 // Perform 50 status_t 51 SetGradientCommand::Perform() 52 { 53 // clone the new gradient for handling over to the style 54 Gradient* clone = NULL; 55 if (fGradient) { 56 clone = new (nothrow) Gradient(*fGradient); 57 if (!clone) 58 return B_NO_MEMORY; 59 } 60 61 if (fStyle->Gradient()) { 62 // remember the current gradient of the style 63 if (fGradient) 64 *fGradient = *fStyle->Gradient(); 65 else { 66 fGradient = new (nothrow) Gradient(*fStyle->Gradient()); 67 if (!fGradient) { 68 delete clone; 69 return B_NO_MEMORY; 70 } 71 } 72 } else { 73 // the style didn't have a gradient set 74 delete fGradient; 75 fGradient = NULL; 76 } 77 78 // remove the gradient or set the new one 79 fStyle->SetGradient(clone); 80 delete clone; 81 82 return B_OK; 83 } 84 85 // Undo 86 status_t 87 SetGradientCommand::Undo() 88 { 89 return Perform(); 90 } 91 92 // GetName 93 void 94 SetGradientCommand::GetName(BString& name) 95 { 96 name << _GetString(/*EDIT_GRADIENT*/0, "Edit Gradient"); 97 } 98 99 // CombineWithNext 100 bool 101 SetGradientCommand::CombineWithNext(const Command* command) 102 { 103 const SetGradientCommand* next 104 = dynamic_cast<const SetGradientCommand*>(command); 105 106 if (next && next->fTimeStamp - fTimeStamp < 1000000) { 107 fTimeStamp = next->fTimeStamp; 108 // NOTE: next was already performed, but 109 // when undoing, we want to use our 110 // remembered gradient 111 return true; 112 } 113 return false; 114 } 115 116