// $Revision: 1.1 $ // 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 TCreateTileCommand class declaration. This macro defines the // TaligentTypeExtension utility functions. kOriginal version is the version // level of the command. TaligentTypeExtensionMacro(TCreateTileCommand); // This is the constructor used in this program, specifying the type of tile to create. TCreateTileCommand::TCreateTileCommand(TTile::EType tileType, const TGPoint& position, const TGPoint& size) // kSerialUndo must be specified in the base class constructor to make this // command undoable. The default is kCantUndo. : TCommandOn(TCommandOn::kSerialUndo), fNewTileType(tileType), fNewTileIndex(-1), fNewTilePosition(position), fNewTileSize(size) {} // Default constructor, required by Taligent Type Extensions TCreateTileCommand::TCreateTileCommand() // kSerialUndo must be specified in the base class constructor to make this // command undoable. The default is kCantUndo. : TCommandOn(TCommandOn::kSerialUndo), fNewTileType(TTile::kRock), fNewTileIndex(-1) {} // Copy constructor, required by Taligent Type Extensions TCreateTileCommand::TCreateTileCommand(const TCreateTileCommand ©) : TCommandOn(copy), fNewTileType(copy.fNewTileType), fNewTilePosition(copy.fNewTilePosition), fNewTileSize(copy.fNewTileSize), fNewTileIndex(copy.fNewTileIndex) {} TCreateTileCommand::~TCreateTileCommand() {} // Out streaming operator, required by Taligent Type Extensions TStream& TCreateTileCommand::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 TCreateTileCommand data, convert enum to short. (short) fNewTileType >>= toStream; fNewTilePosition >>= toStream; fNewTileSize >>= toStream; fNewTileIndex >>= toStream; return toStream; } // In streaming operator, required by Taligent Type Extensions TStream& TCreateTileCommand::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); // Stream in the TCreateTileCommand data, converting // short back to enum. temp <<= fromStream; fNewTileType = (TTile::EType) temp; fNewTilePosition <<= fromStream; fNewTileSize <<= fromStream; fNewTileIndex <<= 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 TCreateTileCommand::HandleDoBegin(TTilesSelection& target) { // Create a new TTile at a default location, with a default size, color // and text label add to the model. TStandardText newText (fNewTileType == TTile::kRock ? "Rock" : (fNewTileType == TTile::kPaper ? "Paper" : "Scissors")); TTile* newTile = new TTile (fNewTileType, TRGBColor(1,0,0), fNewTilePosition, fNewTileSize, newText); fNewTileIndex = target.GetModelForWriting()->AdoptTile(newTile); // For convenience, set the target selection to select the newly created Tile target.SelectTile (fNewTileIndex); } // 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 TCreateTileCommand::HandleUndo(TTilesSelection& target) { TTile* newTile = target.GetModelForWriting()->OrphanTile(fNewTileIndex); delete newTile; fNewTileIndex = -1; } // 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 TCreateTileCommand::HandleRedo(TTilesSelection& target) { HandleDoBegin(target); } // 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(); } // This macro call matches a macro call to TaligentTypeExtensionDeclarationsMacro // in the TChangeTextCommand class declaration. This macro defines the // TaligentTypeExtension utility functions. kOriginal version is the version // level of the command. TaligentTypeExtensionMacro(TChangeTextCommand) ; // This is the constructor used in this program, specifying the "change to" Text TChangeTextCommand::TChangeTextCommand(const TStandardText& newText) // kSerialUndo must be specified in the base class constructor to make this // command undoable. The default is kCantUndo. : TCommandOn(TCommandOn::kSerialUndo), fNewText(newText), fNumTiles(0), fOldText(NIL) {} // Default constructor, required by Taligent Type Extensions TChangeTextCommand::TChangeTextCommand() // kSerialUndo must be specified in the base class constructor to make this // command undoable. The default is kCantUndo. : TCommandOn(TCommandOn::kSerialUndo), fNewText(""), fNumTiles(0), fOldText(NIL) {} // Copy constructor, required by Taligent Type Extensions TChangeTextCommand::TChangeTextCommand(const TChangeTextCommand ©) : TCommandOn(copy), fNewText(copy.fNewText), fNumTiles(copy.fNumTiles) { // allocate enough space to hold old Texts fOldText = new TStandardText[fNumTiles]; for (short i = 0; i < fNumTiles; i++) { fOldText[i] = copy.fOldText[i]; } } TChangeTextCommand::~TChangeTextCommand() { // Delete any allocated "old Texts" information delete [] fOldText; } // Out streaming operator, required by Taligent Type Extensions TStream& TChangeTextCommand::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 TChangeTextCommand data fNewText >>= toStream; fNumTiles >>= toStream; for (short i = 0; i < fNumTiles; i++) { fOldText[i] >>= toStream; } return toStream; } // In streaming operator, required by Taligent Type Extensions TStream& TChangeTextCommand::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 [] fOldText; // Stream in the TChangeTextCommand data fNewText <<= fromStream; fNumTiles <<= fromStream; // Allocate enough memory to hold old Texts. fOldText = new TStandardText[fNumTiles]; for (short i = 0; i < fNumTiles; i++) { fOldText[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 Texts). // Commands assume that the caller has taken care of locking the model void TChangeTextCommand::HandleDoBegin(TTilesSelection& target) { TileIndex i, lowBound; lowBound = target.GetLowBound(); fNumTiles = target.GetHighBound() - lowBound + 1; // Allocate enough memory to hold old Texts fOldText = new TStandardText[fNumTiles]; // Save the old Texts for undo capability for (i=0; i < fNumTiles; i++) { fOldText[i] = target.GetText(i+lowBound); } // Change the Text of all TTiles in the selection target.SetTextAll (fNewText); target.GetModelForWriting()->ModelChanged(); } // 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 TChangeTextCommand::HandleUndo(TTilesSelection& target) { TileIndex i, lowBound; lowBound = target.GetLowBound(); // Change the Texts of all the TTiles in the selection back to the // saved "old Texts" for (i=0; i < fNumTiles; i++) { target.SetText (i+lowBound, fOldText[i]); } target.GetModelForWriting()->ModelChanged(); } // 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 TChangeTextCommand::HandleRedo(TTilesSelection& target) { // Change the Text of all TTiles in the selection target.SetTextAll (fNewText); target.GetModelForWriting()->ModelChanged(); } // 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 TChangeTextCommand::HandleCanDo(const TTilesSelection& target) const { return !target.IsEmpty(); } // This macro call matches a macro call to TaligentTypeExtensionDeclarationsMacro // in the TMoveCommand class declaration. This macro defines the // TaligentTypeExtension utility functions. kOriginal version is the version // level of the command. TaligentTypeExtensionMacro(TMoveCommand); // Default (void) constructor, required by TaligentTypeExtension and used in this program TMoveCommand::TMoveCommand () : // kSerialUndo must be specified in the base class constructor to make this // command undoable. The default is kCantUndo. TCommandOn(TCommandOn::kSerialUndo), fOldPosition(0,0), fDelta(0,0) // initially no delta to apply {} // Destructor TMoveCommand::~TMoveCommand () {} // Copy constructor, required by TaligentTypeExtension TMoveCommand::TMoveCommand (const TMoveCommand ©) : TCommandOn(copy), fOldPosition(copy.fOldPosition), fDelta(copy.fDelta) {} // Stream out operator, required by TaligentTypeExtension TStream& TMoveCommand::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 TMoveCommand data fOldPosition >>= toStream; fDelta >>= toStream; return toStream; } // Stream in operator, required by TaligentTypeExtension TStream& TMoveCommand::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); // Stream in the TMoveCommand data fOldPosition <<= fromStream; fDelta <<= fromStream; break; } // New versions would be added here to support changes default: throw TInvalidVersionError(); } return fromStream; } // HandleDoBegin should store any information necessary for undo. void TMoveCommand::HandleDoBegin (TTilesSelection& target) { // Get the position of the target and save fOldPosition = target.GetPosition(); // Move the target to the new position, applying any // delta which already may have been set by a call to MoveBy. target.SetPosition (fOldPosition + fDelta); } void TMoveCommand::HandleUndo (TTilesSelection& target) { // Move the target back to the original position, effectively undoing // all incremental steps. target.SetPosition (fOldPosition); } void TMoveCommand::HandleRedo (TTilesSelection& target) { // Move the target back to the new position, effectively redoing all // incremental steps. target.SetPosition (fOldPosition + fDelta); } void TMoveCommand::HandleDoIncrement (TTilesSelection& target) { // Move the target to the new position. This should be called after // a MoveBy call. target.SetPosition (fOldPosition + fDelta); } void TMoveCommand::MoveBy (const TGPoint& delta) { // Adjust the new position by the delta. This just // updates the data. The move does not actually occur until // HandleDoIncrement() is called. fDelta = delta; }