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