TSpecialShapeView responds to notifications on a single shape

A new class, TSpecialShapeView, registers interest in a single shape. It holds a TShape and responds to notifications about the addition or removal of a shape sharing the same ID.

This example also shows how you can respond to notifications by adding or removing interests from a connection.

Here is the interface for TSpecialShapeView. Besides the handlers, it has a setter for the shape in which it is interested. It also has a message that changes when a matching shape is added or removed.

      class TSpecialShapeView : public TSimpleView, public virtual VViewInitialize {
      public:
          MCollectibleDeclarationsMacro(TSpecialShapeView);
      
                                  TSpecialShapeView(TShapeList* shapeList,
                                      const TGPoint& size,
                                      const TGPoint& locationInParent = TGPoint::kOrigin);
                                  TSpecialShapeView(const TSpecialShapeView&);
          virtual                 ~TSpecialShapeView();
      
                  const TShape&   GetSpecialShape() const;
          virtual void            SetSpecialShape(const TShape&);
      
      protected:
          virtual void            DrawContents(TGrafPort&) const;
          virtual void            SetMessage(const TText&);
      
          virtual void            HandleAddedSpecialShape(const TNotification&);
          virtual void            HandleRemovedSpecialShape(const TNotification&);
      
      private:
          TShapeList*             fShapeList;
          TShape*                 fSpecialShape;
          Boolean                 fShapeWasInList;
          TTextDisplay            fMessage;
          TMemberFunctionConnectionTo<TSpecialShapeView> fConnection;
      };
Instead of registering for interest in its constructor, TSpecialShapeView simply calls Connect. Instead, interests are added (and removed) only as necessary.

When SetSpecialShape changes the shape that the view is interested in, the view informs the connection. First it removes any interests for the old shape, then adds the appropriate interests for the new shape.

      void TSpecialShapeView::SetSpecialShapeID(const TGlobalID& shapeID)
      {
          if (fShapeID == shapeID) {
              return;
          }
      
          if (fSpecialShape) {
              if (fShapeWasInList) {
                  fConnection.RemoveInterest(
                      fShapeList->GetRemovedThisShapeInterest(*fSpecialShape));
              } else {
                  fConnection.RemoveInterest(
                      fShapeList->GetAddedThisShapeInterest(*fSpecialShape));
              }
          }
      
          fSpecialShape = ::Copy(fSpecialShape);
          fShapeWasInList = fShapeList->Member(*fSpecialShape) != NIL;
      
          if (fShapeWasInList) {
              fConnection.AddInterest(fShapeList->GetRemovedThisShapeInterest(*fSpecialShape),
                  &TSpecialShapeView::HandleRemovedSpecialShape);
          } else {
              fConnection.AddInterest(fShapeList->GetAddedThisShapeInterest(*fSpecialShape),
                  &TSpecialShapeView::HandleAddedSpecialShape);
          }
      
          SetMessage(TStandardText("New Special Shape"));
      }
The handlers also change the interests in the connection. Once the shape is added, we only need to be informed about its removal, and vice versa. Only the implementation of HandleAddedSpecialShape is shown.

      void TSpecialShapeView::HandleAddedSpecialShape(const TNotification& /* notification */)
      {
          fConnection.RemoveInterest(fShapeList->GetAddedThisShapeInterest(*fSpecialShape));
          fConnection.AddInterest(fShapeList->GetRemovedThisShapeInterest(*fSpecialShape),
              &TSpecialShapeView::HandleRemovedSpecialShape);
          fShapeWasInList = TRUE;
      
          SetMessage(TStandardText("Added Special Shape"));
      }
The following diagram illustrates how the classes in this example work together when a notification is sent.



[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