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 "Gradient.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 return B_NO_MEMORY; 69 } 70 } else { 71 // the style didn't have a gradient set 72 delete fGradient; 73 fGradient = NULL; 74 } 75 76 // remove the gradient or set the new one 77 fStyle->SetGradient(clone); 78 delete clone; 79 80 return B_OK; 81 } 82 83 // Undo 84 status_t 85 SetGradientCommand::Undo() 86 { 87 return Perform(); 88 } 89 90 // GetName 91 void 92 SetGradientCommand::GetName(BString& name) 93 { 94 name << _GetString(/*EDIT_GRADIENT*/0, "Edit Gradient"); 95 } 96 97 // CombineWithNext 98 bool 99 SetGradientCommand::CombineWithNext(const Command* command) 100 { 101 const SetGradientCommand* next 102 = dynamic_cast<const SetGradientCommand*>(command); 103 104 if (next && next->fTimeStamp - fTimeStamp < 1000000) { 105 fTimeStamp = next->fTimeStamp; 106 // NOTE: next was already performed, but 107 // when undoing, we want to use our 108 // remembered gradient 109 return true; 110 } 111 return false; 112 } 113 114