// $Revision: 1.2 $ // Copyright (C) 1994, 1995 Taligent, Inc. All rights reserved. // TilesCommands.C #ifndef TaligentSample_TILESCOMMANDS #include "TilesCommands.h" #endif // This macro call matches a macro call to TaligentTypeExtensionDeclarationsMacro // in the TChangeColorCommand class declaration. This macro defines the // TaligentTypeExtension utility functions. kOriginal version is the version // level of the command. TaligentTypeExtensionMacro(TChangeColorCommand) ; // This is the constructor used in this program, specifying the "change to" color TChangeColorCommand::TChangeColorCommand(const TRGBColor& newColor) // kSerialUndo must be specified in the base class constructor to make this // command undoable. The default is kCantUndo. : TCommandOn(TCommandOn::kSerialUndo), fNewColor(newColor), fNumTiles(0), fOldColor(NIL) {} // Default constructor, required by Taligent Type Extensions TChangeColorCommand::TChangeColorCommand() // kSerialUndo must be specified in the base class constructor to make this // command undoable. The default is kCantUndo. : TCommandOn(TCommandOn::kSerialUndo), fNewColor(TRGBColor(0,0,0)), fNumTiles(0), fOldColor(NIL) {} // Copy constructor, required by Taligent Type Extensions TChangeColorCommand::TChangeColorCommand(const TChangeColorCommand ©) : TCommandOn(copy), fNewColor(copy.fNewColor), fNumTiles(copy.fNumTiles) { // allocate enough space to hold old colors fOldColor = new TRGBColor[fNumTiles]; for (short i = 0; i < fNumTiles; i++) { fOldColor[i] = copy.fOldColor[i]; } } TChangeColorCommand::~TChangeColorCommand() { // Delete any allocated "old colors" information delete [] fOldColor; } // Out streaming operator, required by Taligent Type Extensions TStream& TChangeColorCommand::operator>>=(TStream& toStream) const { // Place the version number onto the stream ::WriteVersion(toStream, kOriginalVersion); // Stream out the base class data TCommandOn::operator>>=(toStream); // Stream out TChangeColorCommand data fNewColor >>= toStream; fNumTiles >>= toStream; for (short i = 0; i < fNumTiles; i++) { fOldColor[i] >>= toStream; } return toStream; } // In streaming operator, required by Taligent Type Extensions TStream& TChangeColorCommand::operator<<=(TStream& fromStream) { // Read in the version VersionInfo version = ::ReadVersion(fromStream, kOriginalVersion, kOriginalVersion); switch (version) { case kOriginalVersion: { short temp; // Stream in base class portion TCommandOn::operator<<=(fromStream); // Is this needed?? Well, it can't hurt for now. delete [] fOldColor; // Stream in the TChangeColorCommand data fNewColor <<= fromStream; fNumTiles <<= fromStream; // Allocate enough memory to hold old colors. fOldColor = new TRGBColor[fNumTiles]; for (short i = 0; i < fNumTiles; i++) { fOldColor[i] <<= fromStream; } break; } // New versions would be added here to support changes in the TTilesSelection default: throw TInvalidVersionError(); } return fromStream; } // Execute the command on the selection. Save information needed to undo (i.e., the old colors). // Commands assume that the caller has taken care of locking the model void TChangeColorCommand::HandleDoBegin(TTilesSelection& target) { TileIndex i, lowBound; lowBound = target.GetLowBound(); fNumTiles = target.GetHighBound() - lowBound + 1; // Allocate enough memory to hold old colors fOldColor = new TRGBColor[fNumTiles]; // Save the old colors for undo capability for (i=0; i < fNumTiles; i++) { fOldColor[i] = target.GetColor(i+lowBound); } // Change the color of all TTiles in the selection target.SetColorAll (fNewColor); } // Use the information saved in HandleDoBegin to undo the effects of the command. // Commands assume that the caller has taken care of locking the model void TChangeColorCommand::HandleUndo(TTilesSelection& target) { TileIndex i, lowBound; lowBound = target.GetLowBound(); // Change the colors of all the TTiles in the selection back to the // saved "old colors" for (i=0; i < fNumTiles; i++) { target.SetColor (i+lowBound, fOldColor[i]); } } // Similar to HandleDoBegin except there is no need to save information for undo purposes. // Commands assume that the caller has taken care of locking the model void TChangeColorCommand::HandleRedo(TTilesSelection& target) { // Change the color of all TTiles in the selection target.SetColorAll (fNewColor); } // HandleCanDo() returns true when the command can be performed, in // this case, when the target is not empty. HandleCanDo is used by // the Presentation Framework to activate/deactive menu items when // the command is bound to the menu item with a TCommandControlStateOn<>. bool TChangeColorCommand::HandleCanDo(const TTilesSelection& target) const { return !target.IsEmpty(); }