// $Revision: 1.2 $ // Copyright (C) 1994, 1995 Taligent, Inc. All rights reserved. // TilesSelections.C #ifndef TaligentSample_TILESSELECTIONS #include "TilesSelections.h" #endif // This macro call matches a macro call to TaligentTypeExtensionDeclarationsMacro // in the TTilesSelection class declaration. This macro defines the // TaligentTypeExtension utility functions. kOriginal version is the version // level of the selection. TaligentTypeExtensionMacro(TTilesSelection); // This macro call matches a macro call to DynamicCastDeclarationsMacro // in the TTilesSelection class declaration. This macro defines the // dynamic cast utility functions. Use DynamicCastDefinitionsMacroTwo // if there are two base classes, etc. DynamicCastDefinitionsMacroOne(TTilesSelection, TGUIModelSelectionFor); // constructor used to create a TTilesSelection in this program TTilesSelection::TTilesSelection(const TModelReference& theModel) : TGUIModelSelectionFor(theModel) { // This selection always starts out undefined SetUndefined(); } // default constructor, required by Taligent Type Extensions TTilesSelection::TTilesSelection() : TGUIModelSelectionFor() { // This selection always starts out undefined SetUndefined(); } // copy constructor, required by Taligent Type Extensions TTilesSelection::TTilesSelection(const TTilesSelection& copy) : TGUIModelSelectionFor(copy), fLowBound(copy.fLowBound), fHighBound(copy.fHighBound) {} TTilesSelection::~TTilesSelection() {} // Stream out operatorTStream&, required by Taligent Type Extensions // Must match operator<<=. Should stream out a version level. TStream& TTilesSelection::operator>>=( TStream& toStream ) const { // Place the version number onto the stream ::WriteVersion(toStream, kOriginalVersion); // Stream out the base class data TGUIModelSelectionFor::operator>>=(toStream); // Stream out TTilesSelection data fLowBound >>= toStream; fHighBound >>= toStream; return toStream; } // Stream in operator, required by Taligent Type Extensions // Must match operator>>=. Should be able to handle different versions. TStream& TTilesSelection::operator<<=( TStream& fromStream ) { // Read in the version VersionInfo version = ::ReadVersion(fromStream, kOriginalVersion, kOriginalVersion); switch (version) { case kOriginalVersion: { TGUIModelSelectionFor::operator<<=(fromStream); fLowBound <<= fromStream; fHighBound <<= fromStream; break; } // New versions would be added here to support changes in the TTilesSelection default: throw TInvalidVersionError(); } return fromStream; } // SetColorAll will set the color of all TTiles in the selection to the color parameter. // The caller does not worry whether the selection is on a single tile, on the whole model // or if the selection is empty. void TTilesSelection::SetColorAll (const TRGBColor& color) { TileIndex i; if (!IsEmpty()) { // Selections assume that the caller has taken care of locking the model. TTilesModel* model = GetModelForWriting(); for (i = fLowBound; i <= fHighBound; i++) { model->GetTileForWriting(i)->SetColor(color); } } } // SetColor allows the caller to set the color of a specific TTile in the selection. // SetColor does not allow modification of TTiles outside of the range of the selection. // If the selection is empty, nothing happens. void TTilesSelection::SetColor (TileIndex whichTile, const TRGBColor& color) { if (!IsEmpty()) { if (whichTile >= fLowBound && whichTile <= fHighBound) { // Selections assume that the caller has taken care of locking the model. GetModelForWriting()->GetTileForWriting(whichTile)->SetColor(color); } else { // Throw an exception? ::qprintf ("TTilesSelection::SetColor(): illegalTile\n"); } } } // GetColor returns the color of a specific TTile in the selection. TRGBColor TTilesSelection::GetColor (TileIndex whichTile) const { if (!IsEmpty() && whichTile >= fLowBound && whichTile <= fHighBound) { // Selections assume that the caller has taken care of locking the model. return GetModelForReading()->GetTileForReading(whichTile)->GetColor(); } else { // Throw an exception? ::qprintf ("TTilesSelection::GetColor(): illegalTile\n"); return TRGBColor(0,0,0); } } // Set the position of the selected tile. Works only if one TTile is selected. void TTilesSelection::SetPosition (const TGPoint& point) { // Can only work on a single tile selection if (!IsEmpty() && fLowBound == fHighBound) { // Selections assume that the caller has taken care of locking the model. GetModelForWriting()->GetTileForWriting(fLowBound)->SetPosition(point); } } // Get the position of the selected tile. Works only if one TTile is selected. TGPoint TTilesSelection::GetPosition () { // Can only work on a single tile selection if (!IsEmpty() && fLowBound == fHighBound) { // Selections assume that the caller has taken care of locking the model. return GetModelForReading()->GetTileForReading(fLowBound)->GetPosition(); } else { return TGPoint(0,0); } } TileIndex TTilesSelection::GetLowBound () const { return fLowBound; } TileIndex TTilesSelection::GetHighBound () const { return fHighBound; } // This function is called by the Framework when the user chooses "Select All" // and is also used whenever a whole model selection is needed. // The base class function SelectWholeModel() must be called. void TTilesSelection::SelectWholeModel() { //have to set the state correctly on select whole model. TGUIModelSelectionFor::SelectWholeModel(); fLowBound = 0; // Selections assume that the caller has taken care of locking the model. fHighBound = GetModelForReading()->GetNumTiles() - 1; if (GetState() == kEmpty && fHighBound != -1) SetState(kNotEmpty); } // This function is called when an empty selection is needed // The base class function DeselectAll() must be called. void TTilesSelection::DeselectAll() { TGUIModelSelectionFor::DeselectAll(); // Now IsEmpty() will return true fLowBound = 0; fHighBound = 0; } // This function is called whenever the selection is undefined and cannot be // operated on by a command. // The base class function SetUndefined() must be called. void TTilesSelection::SetUndefined() { // The default for SetUndefined is DeselectAll TGUIModelSelectionFor::SetUndefined(); // Now IsDefined() will return false fLowBound = 0; fHighBound = 0; } void TTilesSelection::SelectTile(TileIndex tileIndex) { // Selections assume that the caller has taken care of locking the model. TileIndex maxIndex = GetModelForReading()->GetNumTiles() - 1; // Check for illegal selection if (tileIndex >= 0 && tileIndex <= maxIndex) { fLowBound = tileIndex; fHighBound = tileIndex; SetState (kNotEmpty); // Now IsEmpty() will return false and IsDefined() will return true } else { // illegal selection DeselectAll(); } } bool TTilesSelection::ContainsTile (TileIndex whichTile) const { return (!IsEmpty() && whichTile >= fLowBound && whichTile <= fHighBound); }