Code walkthrough

This section contains code listings for the changes you need to make to the Tiles program to implement embedding:

This code is in the directory ./TilesTutorial/07.Embedding/Tiles. New code is shown here in bold.

TTilesModel

Following is the new declaration for the TTilesModel class, found in the file
./TilesTutorial/07.Embedding/Tiles/TilesModel.h:

      // Copyright (C) 1995 Taligent, Inc. All rights reserved.
      class TTilesModel : public TGUIEmbedderModel
      {
          // TTilesModel function declarations are unchanged.
      
      private:
          // Adds a value to identify version two.
          enum            {kOriginalVersion, kVersionTwo};
          TArrayOf<TTile> fTiles;
      };
The new source is in the file ./TilesTutorial/07.Embedding/Tiles/TilesModel.C. Explicit calls to base class functions are changed to call TGUIEmbedderModel in the following functions:

The new base class and version are also declared in the ModelDefinitionsMacro call:

    ModelDefinitionsMacro(TTilesModel, kVersionTwo, TGUIEmbedderModel);
The input streaming operator is also modified to support streaming in both versions of TTilesModel (the only change to the output streaming operator is that it streams base class data using the TGUIEmbedderModel operator):

      // Copyright (C) 1995 Taligent, Inc. All rights reserved.
      TStream&
      TTilesModel::operator<<=(TStream& fromStream)
      {
          VersionInfo version = ::ReadVersion(fromStream, kOriginalVersion, kVersionTwo);
          
          switch (version) {
          case kOriginalVersion: {
              // Streams in data from the appropriate base class, depending on the 
              // version being streamed in.
              TModel::operator<<=(fromStream);
              fTiles.DeleteAll();
              fTiles <<= fromStream;
              break;
          }
          case kVersionTwo: {
              // Streams in data from the appropriate base class, depending on the 
              // version being streamed in.
              TGUIEmbedderModel::operator<<=(fromStream);
              fTiles.DeleteAll();
              fTiles <<= fromStream;
              break;
          }
          default:
              throw TInvalidVersionError();
          }
          return fromStream;
      }

TTilesView

Following is the new declaration for the TTilesView class, found in the file
./TilesTutorial/07.Embedding/Tiles/TileView.h:

      // Copyright (C) 1995 Taligent, Inc. All rights reserved.
      class TTilesView : public TGUIEmbedderModelView 
      {
          // TTilesView function declarations and data members are unchanged.
      };
The new source is in the file ./TilesTutorial/07.Embedding/Tiles/TilesView.C. Explicit calls to base class functions are changed to call TGUIEmbedderModelView in the following functions:

TTilesSelection

Following is the new declaration for the TTilesSelection class, found in the file
./TilesTutorial/07.Embedding/Tiles/TilesSelections.h:

      // Copyright (C) 1995 Taligent, Inc. All rights reserved.
      class TTilesSelection : public TGUIEmbedderModelSelectionFor<TTilesModel>
      {
              // Existing TTilesSelection functions and data members are unchanged.
      
      public:
          // Called to Copy data.
          virtual void    CopyDataIntoModelSubclass(TTilesModel& theDestModel) const;
          // Called to Cut data.
          virtual void    MoveDataIntoModelSubclass(TTilesModel& theDestModel);
          // Called to Paste data.
          virtual void    MoveDataOutofModelSubclass(TTilesModel& theSrcModel);
      };
The new source is in the file ./TilesTutorial/07.Embedding/Tiles/TilesSelections.C. Explicit calls to base class functions are changed to call TGUIEmbedderModelSelectionFor<TTilesModel> in the following functions:

Following is the code for the three new functions. Like other selection functions, these functions assume that the caller has already taken care of getting a read or write lock on the model as appropriate. This code is in the file
./TilesTutorial/07.Embedding/Tiles/TilesSelections.C:

      // Copyright (C) 1995 Taligent, Inc. All rights reserved.
      void
      TTilesSelection::CopyDataIntoModelSubclass( TTilesModel& destModel ) const
      {
          TileIndex i;
          // Verifies that there is selected data to copy.
          if (!IsEmpty()) {
              const TTilesModel* sourceModel = GetModelForReading();
      
              // Creates a copy of each selected tile and has the destination model adopt it.
              for (i = fLowBound; i <= fHighBound; i++) {
                  destModel.AdoptTile(new TTile(*(sourceModel->GetTileForReading(i))));
              }
          }
      }
      
      void
      TTilesSelection::MoveDataIntoModelSubclass(TTilesModel& destModel )
      {
          TileIndex i;
          if (!IsEmpty()) {
              TTilesModel* sourceModel = GetModelForWriting();
      
              // Creates a copy of each selected tile and has the destination model adopt it.
              for (i = fLowBound; i <= fHighBound; i++) {
                  destModel.AdoptTile(new TTile(*(sourceModel->GetTileForReading(i))));
              }
              
              // Deletes each selected tile from the source model.
              for (i = fLowBound; i <= fHighBound; i++) {
                  TTile* tile = sourceModel->OrphanTile(fLowBound);
                  delete tile;
              }
              
              // Resets selection to empty, because previously selected tiles were deleted.
              DeselectAll();
          }
      }
      
      
      void
      TTilesSelection::MoveDataOutofModelSubclass(TTilesModel& sourceModel )
      {
          TileIndex i;
          TTilesModel* destModel = GetModelForWriting();
      
          TileIndex numTilesToCopy = sourceModel.GetNumTiles();
          if (numTilesToCopy > 0) {
              if (!IsEmpty()) {
                  // Deletes any selected tiles in the source model.
                  for (i = fLowBound; i <= fHighBound; i++) {
                      TTile* tile = destModel->OrphanTile(fLowBound);
                      delete tile;
                  }
                  
                  // Resets selection to empty.
                  DeselectAll();
              }
              
              // Copies first tile and stores its index as the low bound.
              fLowBound = fHighBound = destModel->
    AdoptTile(new TTile(*(sourceModel.GetTileForReading(0)))); // Copies remaining tiles and stores the last index as the high bound. for (i = 1; i < numTilesToCopy; i++) { fHighBound = destModel->AdoptTile( new TTile(*(sourceModel.GetTileForReading(i)))); } // Resets state to indicate a selection has been set. SetState(kNotEmpty); } }


[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker