Code walkthrough

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

TChangeColorCommand:: HandleCanDo

Following is the declaration of the HandleCanDo function, found in the header file ./TilesTutorial/04.CurrentSelection/Tiles/TilesCommands.h. HandleCanDo is a protected function:

    virtual bool HandleCanDo(const TTilesSelection& target) const;
HandleCanDo is implemented in
./TilesTutorial/04.CurrentSelection/Tiles/TilesCommands.C:

      // Copyright (C) 1995 Taligent, Inc. All rights reserved.
      bool    
      TChangeColorCommand::HandleCanDo(const TTilesSelection& target) const
      {
          // Returns True if the selection is not empty.
          return !target.IsEmpty();
      }

TTilesSelection:: ContainsTile

Following is the declaration of the ContainsTile function, found in the header file ./TilesTutorial/04.CurrentSelection/Tiles/TilesSelections.h. ContainsTile is a public function:

    bool ContainsTile(TileIndex) const;
ContainsTile is implemented in
./TilesTutorial/04.CurrentSelection/Tiles/TilesSelections.C:

      // Copyright (C) 1995 Taligent, Inc. All rights reserved.
      bool
      TTilesSelection::ContainsTile (TileIndex whichTile) const
      {
          // Verifies that the selection is not empty AND that the index is within the selected range.
          return (!IsEmpty() && whichTile >= fLowBound && whichTile <= fHighBound);
      }

TTilesView:: GetTilesSelection

GetTilesSelection is declared in the file ./TilesTutorial/04.CurrentSelection/Tiles/TilesView.h. GetTilesSelection is a public function:

    const TTilesSelection* GetTilesSelection () const;
GetTilesSelection is implemented in the file ./TilesTutorial/04.CurrentSelection/Tiles/TilesView.C:

      // Copyright (C) 1995 Taligent, Inc. All rights reserved.
      const TTilesSelection *
      TTilesView::GetTilesSelection () const
      {
          // Gets the current selection via the GUI bundle.
          const TModelSelection* modelSelection = GetCurrentModelSelection();
      
          // Casts the model selection to a TTilesSelection.
          if (modelSelection) {
              TTilesSelection* tilesSelection;
              DynamicCastTo(tilesSelection, modelSelection);
              return tilesSelection;
          }
          else {
              return 0;
          }
      }

TTilesView::DrawContents

Following are the changes to the DrawContents function, found in the file
./TilesTutorial/04.CurrentSelection/Tiles/TilesView.C. New and modified code is shown in bold.

      // Copyright (C) 1995 Taligent, Inc. All rights reserved.
      void
      TTilesView::DrawContents(TGrafPort& port) const
      {
          const TModelPointerTo<TTilesModel> model(GetModelReference());
          
          TGArea area;
          GetBounds(area);
          TGrafBundle whitebg (new TColorPaint(TRGBColor(.9,1,1)), TGrafBundle::kFill);
          port.Draw (area, whitebg);
      
          // Gets a pointer to the current selection. If there is none, this will be zero.
          const TTilesSelection* currentSelection = GetTilesSelection();
          bool inCurrentSelection = FALSE;
      
          int numTiles = model->GetNumTiles();
          for (int i = 0; i < numTiles; i++) {
              const TTile* tile = model->GetTileForReading(i);
              
              // Determines if each tile is selected and passes it to DrawTile.
              // DrawTile highlights selected tiles.
              if (currentSelection && currentSelection->ContainsTile(i)) {
                  inCurrentSelection = TRUE;
              }
              else {
                  inCurrentSelection = FALSE;
              }
              
              DrawTile(port, tile, inCurrentSelection);
          }
      }

TTilesView::DrawTile

Following is the new DrawTile declaration, found in the header
./TilesTutorial/04.CurrentSelection/Tiles/TilesView.C. New and modified code is shown in bold.

      // Copyright (C) 1995 Taligent, Inc. All rights reserved.
      // Takes an argument indicating whether the tile to draw is selected.
      void 
      TTilesView::DrawTile (TGrafPort& port, const TTile* tile, bool inCurrentSelection) const
      {
          TGrafBundle* bundle = new TGrafBundle(new TColorPaint(tile->GetColor()), 
                  new TColorPaint(TColorPaint::GetBlack()),
                  // If the tile is selected, draws a frame around it. Otherwise, it just uses a fill color.
                   inCurrentSelection ? TGrafBundle::kFillAndFrame : TGrafBundle::kFill);
                  bundle->AdoptFramePen(new TSolidPen(3.0, TPen::kOutsetFrame));
          
          MGraphic* tileGraphic = ::CopyPointer(fTilesGraphics[tile->GetType()]);
          
          tileGraphic->ScaleBy(tile->GetSize());
          tileGraphic->TranslateBy(tile->GetPosition());
          
          tileGraphic->AdoptBundle(bundle);
          
          tileGraphic->Draw(port);
          delete tileGraphic;
      }

TTilesView::MouseDown

Following are the changes to the MouseDown function, found in the file
./TilesTutorial/04.CurrentSelection/Tiles/TilesView.C. New and modified code is shown in bold:

      // Copyright (C) 1995 Taligent, Inc. All rights reserved.
      bool
      TTilesView::MouseDown (TMouseDownEvent& mouseDownEvent)
      {
          TRGBColor newColor;
          TileIndex whichTile;
          TGPoint mousePoint = mouseDownEvent.GetEventPosition();
      
          const TModelPointerTo<TTilesModel> model(GetModelReference());
          
          for (whichTile = model->GetNumTiles() - 1; whichTile >= 0; whichTile--) {
              if (model->GetTileForReading(whichTile)->ContainsPoint(mousePoint)) {
                  break;
              }
          }
          
          // If a tile was clicked on, selects that tile. Otherwise, creates an empty selection. The 
          // selection is passed to the GUI bundle as the current selection.
          TTilesSelection* selection = (TTilesSelection*) model->CreateSelection ();
          if (whichTile >= 0) {
              selection->SelectTile (whichTile);
              AdoptCurrentModelSelection (selection);
          }
          else {
              selection->DeselectAll();
              AdoptCurrentModelSelection(selection);
          }
          
          return TRUE;
      }

TTilesView:: HandleAfterConnectionToViewRoot

Following are the changes to the HandleAfterConnectionToViewRoot function, found in the file ./TilesTutorial/04.CurrentSelection/Tiles/TilesView.C. New and modified code is shown in bold:

      // Copyright (C) 1995 Taligent, Inc. All rights reserved.
      void
      TTilesView::HandleAfterConnectionToViewRoot()
      {
          TDocumentComponentView::HandleAfterConnectionToViewRoot ();
          BuildTilesGraphicsMapToModel();
          SetAllocatedArea (TGArea(TGRect(TGPoint(0,0), TGPoint(200, 100))));
          SetCoordinateView(TViewHandle(*this));
          
          // Gets read-only access to the model.
          const TModelPointerTo<TTilesModel> model(GetModelReference());
          
          // Creates an empty selection on the model and passes it to the GUI
          // bundle as the current selection.
          TTilesSelection* selection = (TTilesSelection*) model->CreateSelection();
          selection->DeselectAll();
          
          AdoptCurrentModelSelection(selection);
      }


[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