Borland Online And The Cobb Group Present:


January, 1995 - Vol. 2 No. 1


Listing B: CARS4.CPP
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include 
#include "automobi.h"

// Main document class
class _DOCVIEWCLASS TGarageDoc :
  public TDocument
{ public:
   TGarageDoc(TDocument* parent) :
     currentIndex(0)
   { sprintf(docName,
             "Car Document #%d",
             docCount++);
     arrayOfCars[0] = new
      Automobile("Ford", "Probe GT", 92);
     arrayOfCars[1] = new
      Automobile("Honda", "Civic", 92);
     arrayOfCars[2] = new
      Automobile("Datsun", "240Z", 72); }

   ~TGarageDoc()
   { delete arrayOfCars[0];
     delete arrayOfCars[1];
     delete arrayOfCars[2]; }

   // TDocument overrides
   BOOL IsOpen() { return TRUE; }

   // Utility functions
   const char* GetCarModel(int idx)
   { return arrayOfCars[idx]->getModel(); }

   const char* GetCarMake(int idx)
   { return arrayOfCars[idx]->getMake(); }

   const char* GetDocName()
   { return docName; }
   
   int GetCurrent() { return currentIndex; }

   void SetCurrent(int newIdx)
   { currentIndex = newIdx;
     NotifyViews(vnCommit); }

   void DisplayCarInfo(int idx)
   { sprintf(description,
       "You selected a 19%d %s %s \n from %s",
       arrayOfCars[currentIndex]->getYear(),
       arrayOfCars[currentIndex]->getMake(),
       arrayOfCars[currentIndex]->getModel(),
       docName);
     TApplication* app =
       GetDocManager().GetApplication();
     app->GetMainWindow()->
       MessageBox(description, "Car Info"); }

  private:
   Automobile* arrayOfCars[3];
   int currentIndex;
   char docName[30];
   static int docCount;
   char description[80];
};

int TGarageDoc::docCount = 1;

// 1st View Class
class _DOCVIEWCLASS TModelView :
  public TListBox, public TView
{ public:
   TModelView(TGarageDoc& doc, 
              TWindow* parent = 0):
     TView(doc),
     TListBox(parent,
              GetNextViewId(),
              0,0,40,90),
     carDoc(doc)
   { Attr.Style &= ~(WS_BORDER | LBS_SORT);
     Attr.Style |= (WS_HSCROLL | WS_VSCROLL); }

   ~TModelView()
   { Destroy(); }
   
   // TView overrides
   static const char* StaticName()
   { return "Model View"; }

   LPCSTR GetViewName()
   { return StaticName(); }

   TWindow* GetWindow()
   { return (TWindow*)this; }

   // TListBox overrides
   BOOL SetDocTitle(LPCSTR, int index)
   { return TListBox::SetDocTitle(carDoc.
        GetDocName(), index); }

   BOOL Create()
   { TListBox::Create();
     InsertString(GetCarData(0), 0);
     InsertString(GetCarData(1), 1);
     InsertString(GetCarData(2), 2);
     SetSelIndex(carDoc.GetCurrent());
     return TRUE; }

  protected:
   virtual const char * GetCarData(int idx)
   { return carDoc.GetCarModel(idx); }

   BOOL VnIsWindow(HWND hWnd)
   { return HWindow == hWnd; }

   void CmSelChange()
   { carDoc.SetCurrent(GetSelIndex()); }

   void CmDoubleClick()
   { carDoc.DisplayCarInfo(GetSelIndex()); }

   BOOL VnCommit(BOOL)
   { SetSelIndex(carDoc.GetCurrent());
     return TRUE; }

   TGarageDoc& carDoc;
   DECLARE_RESPONSE_TABLE(TModelView);
};

// Create the 1st document template
DEFINE_DOC_TEMPLATE_CLASS(TGarageDoc, \
                          TModelView, \
                          ModelTemplate);
ModelTemplate modelTemp("Model View of Doc",
                      "*.*",
                      0,
                      "",
                      dtAutoDelete);

DEFINE_RESPONSE_TABLE1(TModelView, TListBox)
  EV_VN_ISWINDOW,
  EV_VN_COMMIT,
  EV_NOTIFY_AT_CHILD(LBN_SELCHANGE, CmSelChange),
  EV_NOTIFY_AT_CHILD(LBN_DBLCLK, CmDoubleClick),
END_RESPONSE_TABLE;

class _DOCVIEWCLASS TBrandView : 
  public TModelView
{ public:
   TBrandView(TGarageDoc& doc, 
              TWindow* parent = 0):
     TModelView(doc, parent) {}

   // TModelView overrides
   static const char* StaticName()
   { return "Brand View"; }

   LPCSTR GetViewName()
   { return StaticName(); }

  protected:
   virtual const char * GetCarData(int idx)
   { return carDoc.GetCarMake(idx); }
};

// Create the 2nd document template
DEFINE_DOC_TEMPLATE_CLASS(TGarageDoc, \
                          TBrandView, \
                          BrandTemplate);
BrandTemplate brandTemp("Brand View of Doc",
                      "*.*",
                      0,
                      "",
                      dtAutoDelete);

// Main application class
class TCarsIVApp : public TApplication
{ private:
   TFrameWindow* frame; // Main Window
   TMDIClient* client;  // Client region

  public:
   TCarsIVApp()
   {DocManager = new TDocManager(dmMDI | dmMenu);
    frame = new TMDIFrame("Car Document Viewer",
                    0, *(client=new TMDIClient));
    frame->AssignMenu(1); }

   void InitMainWindow()
   { MainWindow = frame; }

  protected:
   // Message response functions
   void EvViewCars(TView& view)
   { TMDIChild* child =
       new TMDIChild(*client,
                     view.GetViewName(),
                     view.GetWindow());
     child->Create(); }

   void EvClose(TView&) {}
   
  DECLARE_RESPONSE_TABLE(TCarsIVApp);
};

DEFINE_RESPONSE_TABLE1(TCarsIVApp,TApplication)
  EV_OWLVIEW(dnCreate ,EvViewCars),
  EV_OWLVIEW(dnClose, EvClose),
END_RESPONSE_TABLE;

int
OwlMain(int, char**)

Return to "C++ design using OWL 2.0 - Implementing MVC designs using OWLís Doc/View architecture"


Copyright (c) 1996 The Cobb Group, a division of Ziff-Davis Publishing Company. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of Ziff-Davis Publishing Company is prohibited. The Cobb Group and The Cobb Group logo are trademarks of Ziff-Davis Publishing Company.