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 "GradientTransformable.h" 15 #include "Style.h" 16 17 using std::nothrow; 18 19 // constructor 20 SetColorCommand::SetColorCommand(Style* style, 21 const rgb_color& color) 22 : Command(), 23 fStyle(style), 24 fColor(color) 25 { 26 } 27 28 // destructor 29 SetColorCommand::~SetColorCommand() 30 { 31 } 32 33 // InitCheck 34 status_t 35 SetColorCommand::InitCheck() 36 { 37 #ifdef __HAIKU__ 38 return fStyle && fStyle->Color() != fColor ? B_OK : B_NO_INIT; 39 #else 40 rgb_color color = fStyle->Color(); 41 return fStyle && *(uint32*)&color != *(uint32*)&fColor ? 42 B_OK : B_NO_INIT; 43 #endif 44 } 45 46 // Perform 47 status_t 48 SetColorCommand::Perform() 49 { 50 // toggle the color 51 rgb_color previous = fStyle->Color(); 52 fStyle->SetColor(fColor); 53 fColor = previous; 54 55 return B_OK; 56 } 57 58 // Undo 59 status_t 60 SetColorCommand::Undo() 61 { 62 return Perform(); 63 } 64 65 // GetName 66 void 67 SetColorCommand::GetName(BString& name) 68 { 69 name << "Change Color"; 70 } 71 72 // CombineWithNext 73 bool 74 SetColorCommand::CombineWithNext(const Command* command) 75 { 76 const SetColorCommand* next 77 = dynamic_cast<const SetColorCommand*>(command); 78 79 if (next && next->fTimeStamp - fTimeStamp < 1000000) { 80 fTimeStamp = next->fTimeStamp; 81 // NOTE: next was already performed, but 82 // when undoing, we want to use our 83 // remembered color 84 return true; 85 } 86 return false; 87 } 88 89