IMPROVING THE VIEW

The first item that you can fix is the updating of all the document view windows when an element is drawn. The problem arises because only the view in which you draw an element knows about it. Each view is acting independently of the others, and there is no communication between them. If you can arrange for all the views to be notified when an element is added to the document, they can take the appropriate action.

Updating Multiple Views

The document class contains the UpdateAllViews() function to help with this particular problem. The function provides a means for the document to send a message to all its views. You just need to call it from the AddElement() function in CSketcherDoc that adds a new element:

  void AddElement(std::shared_ptr<CElement>& pElement)
  {
    m_Sketch.push_back(pElement);
    UpdateAllViews(nullptr, 0, pElement.get());   // Tell all the views
  }

The inherited UpdateAllViews() function is called whenever an element is added to the document. This communicates with the views by calling the OnUpdate() member of each view. The UpdateAllViews() function has three parameters, two of which have default values. The arguments that you can supply are:

1. A pointer to the current view object. This identifies the view for which the OnUpdate() function should not be called. This is a useful feature when the current view is up to date and you have a pointer to it. You could arrange for this if you needed to by adding an extra parameter of type CSketcherView* ...

Get Ivor Horton's Beginning Visual C++ 2012 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.