September, 1994 - Vol. 1 No. 9


Listing C: CARS2.CPP

#define Uses_TKeys
#define Uses_TApplication
#define Uses_TRect
#define Uses_TDialog
#define uses_TButton
#define Uses_TMenuBar
#define Uses_TSubMenu
#define Uses_TMenuItem
#define Uses_TStatusLine
#define Uses_TStatusItem
#define Uses_TStatusDef
#define Uses_TDeskTop
#define Uses_TListBox
#define Uses_TStringCollection
#define Uses_TScrollBar
#include <tv.h>

#include <string.h>

#include "automobi.h"

const ViewCarsCmd = 100;

void CarViewController::displayModel()
{
  TStringCollection *carModelStrings =
    new TStringCollection(3, 1);

  char* temp;
  for(int carCount = 0;
      carCount < 3;
      ++carCount)
  {
    temp = new char[20];
    strcpy(temp, array[carCount]->getModel());
    carModelStrings->
      atInsert(carCount, temp);
  }

  TDialog *d =
    new TDialog(TRect( 25, 6, 55, 15), "CARS" );
  TScrollBar *scroll =
    new TScrollBar(TRect(27, 2, 28, 7));
  TListBox *carList =
    new TListBox(TRect( 3, 2, 27, 7),
                 1, scroll);

  carList->newList(carModelStrings);

  d->insert(carList);
  d->insert(scroll);

  d->insert(new TButton(TRect( 16, 9, 28, 11 ),
                        "Cancel",
                        "cmCancel, bfNormal));

  TProgram::deskTop->execView( d );
  d->destroy(d);
  delete carModelStrings;
}

class TCarsApp : public TApplication
{
  Automobile* arrayOfCars[3];
  void viewCars();

public:

  TCarsApp();
  ~TCarsApp();

  virtual void handleEvent( TEvent& event );
  static TMenuBar *initMenuBar( TRect );
  static TStatusLine *initStatusLine( TRect );
};

TCarsApp::TCarsApp() :
    TProgInit( &TCarsApp::initStatusLine,
               &TCarsApp::initMenuBar,
               &TCarsApp::initDeskTop
             )
{
  arrayOfCars[0] = new
    Automobile("Ford", "Probe GT", 92);
  arrayOfCars[1] = new
    Automobile("Honda", "Civic", 92);
  arrayOfCars[2] = new
    Automobile("Datsun", "240Z", 72);
}

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

void TCarsApp::viewCars()
{
  CarViewController control(arrayOfCars);
  control.displayModel();
}

void TCarsApp::handleEvent(TEvent& event)
{
  TApplication::handleEvent(event);
  if(event.what == evCommand)
  {
    switch(event.message.command)
    {
      case ViewCarsCmd:
        viewCars();
        clearEvent( event );
        break;
      default:
        break;
    }
  }
}

TMenuBar *TCarsApp::initMenuBar(TRect r)
{
  r.b.y = r.a.y+1;

  return new TMenuBar(r,
    *new TSubMenu("~C~ars", kbAltH ) +
      *new TMenuItem("~V~iew Cars...",
                     ViewCarsCmd, kbAltG) +
       newLine() +
      *new TMenuItem("E~x~it",
                     cmQuit, cmQuit,
                     hcNoContext, "Alt-X"));
}

TStatusLine *TCarsApp::initStatusLine(TRect r)
{
  r.a.y = r.b.y-1;
  return new TStatusLine( r,
      *new TStatusDef(0, 0xFFFF) +
          *new TStatusItem("~Alt-X~ Exit",
                           kbAltX, cmQuit) +
          *new TStatusItem(0, kbF10, cmMenu));
}

int main()
{
  TCarsApp cars;
  cars.run();
  return 0;
}

Return to "C++ Design - Implementing a Model-View-Controller design in C++"


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.