home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / powergui / latour / shopping / puritemv.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-29  |  8.3 KB  |  278 lines

  1. /************************************************************
  2. / Tour of the UICL - Shopping List Example Program
  3. /
  4. / Copyright (C) 1994, Law, Leong, Love, Olson, Tsuji.
  5. / Copyright (c) 1997 John Wiley & Sons, Inc. 
  6. / All Rights Reserved.
  7. ************************************************************/
  8. #include <ibase.hpp>
  9. #ifdef IC_PM
  10.   #define INCL_WINSTDFILE   // For DID_xxx file dialog IDs.
  11.   #include <os2.h>
  12. #else
  13.   #include <windows.h>
  14. #endif
  15.  
  16. #include <ifont.hpp>
  17. #include <ifiledlg.hpp>
  18. #include <ilistbox.hpp>
  19. #include <imsgbox.hpp>
  20. #include <ireslib.hpp>
  21. #include <isysmenu.hpp>
  22. #include <ititle.hpp>
  23. #include <ibmpctl.hpp>
  24. #include <imcelcv.hpp>
  25. #include <ipushbut.hpp>
  26. #include <igbitmap.hpp>
  27.  
  28. #include "puritem.hpp"
  29. #include "puritemv.hpp"
  30. #include "shopping.h"
  31.  
  32.  
  33. //===================== PurchaseItemView ======================
  34.  
  35. PurchaseItemView :: PurchaseItemView
  36.                          ( PurchaseItem& purchaseItem,
  37.                            unsigned long windowId,
  38.                            IWindow* owner )
  39.   : IFrameWindow ( windowId, 0, owner, IRectangle(),
  40.                    IFrameWindow::defaultStyle()
  41.                     | IFrameWindow::dialogBackground ),
  42.     fPurchaseItem( purchaseItem ),
  43.     fNotebook( ID_PURCHASEITEMBOOK, this, this ),
  44.     fCmdHandler( this, purchaseItem ),
  45.     fPage1( ID_PURCHASEITEMPAGE1, &fNotebook, &fNotebook ),
  46.     fPage2( ID_PURCHASEITEMPAGE2, &fNotebook, &fNotebook ),
  47.     fNamePrompt( ID_NAMEPROMPT, &fPage1, &fPage1 ),
  48.     fName( ID_NAME, &fPage1, &fPage1 ),
  49.     fQuantityPrompt( ID_QUANTITYPROMPT, &fPage1, &fPage1 ),
  50.     fQuantity( ID_QUANTITY, &fPage1, &fPage1 ),
  51.     fManufacturerPrompt( ID_MANUFACTURERPROMPT,
  52.                           &fPage1, &fPage1 ),
  53.     fManufacturer( ID_MANUFACTURER, &fPage1, &fPage1 ),
  54.     fPricePrompt( ID_PRICEPROMPT, &fPage1, &fPage1 ),
  55.     fPrice( ID_PRICE, &fPage1, &fPage1 ),
  56.     fNotesPrompt( ID_NOTESPROMPT, &fPage1, &fPage1 ),
  57.     fNotes( ID_NOTES, &fPage1, &fPage1, IRectangle(),
  58.              IMultiLineEdit::classDefaultStyle |
  59.              IMultiLineEdit::ignoreTab ),
  60.     fBitmapButton( ID_BITMAPBUTTON, &fPage2, &fPage2),
  61.     fBookBitmap( ID_POWERBITMAP, &fPage2, &fPage2,
  62.                   IResourceId( ID_THEBOOKBITMAP ) )
  63. {
  64.   ITitle title( this, fPurchaseItem.name(),
  65.                 "Shopping List Settings View" );
  66.   if (owner)
  67.   {                     // Not a primary window.
  68.      this->removeFromWindowList();  // Remove from switch list.
  69.   }
  70.   this->setClient( &fNotebook );
  71.  
  72.   IColor background =
  73.     fNotebook.backgroundColor( );
  74.   fNotebook
  75.    .setMinorTabSize( ISize( 0, 0 ) )
  76.    .setBinding( INotebook::spiral )
  77.    .setPageBackgroundColor( background )
  78.    .setMajorTabBackgroundColor( background );
  79.  
  80.   // Set up first page of the notebook.
  81.   this->initializePage1();
  82.  
  83.   // Set up second page of the notebook.
  84.   this->initializePage2();
  85.  
  86.   // Add the pages to the notebook.
  87.   INotebook::PageSettings
  88.     pageInfo( "1st", 0,
  89.               INotebook::PageSettings::autoPageSize
  90.                | INotebook::PageSettings::majorTab );
  91.   fNotebook.addFirstPage( pageInfo, &fPage1 );
  92.   pageInfo.setTabText( "2nd" );
  93.   fNotebook.addLastPage( pageInfo, &fPage2 );
  94.  
  95.   // Size the frame window.
  96.   IRectangle rectFrame =
  97.     this->frameRectFor( IRectangle( IPoint( 0, 0 ),
  98.                                     fNotebook.minimumSize() ));
  99.   this->sizeTo( rectFrame.size() );
  100. }
  101.  
  102. PurchaseItemView& PurchaseItemView :: initializePage1 ( )
  103. {
  104.   // Locate controls on the multicell canvas.
  105.   fPage1
  106.    .addToCell( &fNamePrompt,         2, 2 )
  107.    .addToCell( &fName,               4, 2 )
  108.    .addToCell( &fQuantityPrompt,     2, 4 )
  109.    .addToCell( &fQuantity,           4, 4 )
  110.    .addToCell( &fManufacturerPrompt, 2, 6 )
  111.    .addToCell( &fManufacturer,       4, 6 )
  112.    .addToCell( &fPricePrompt,        2, 8 )
  113.    .addToCell( &fPrice,              4, 8 )
  114.    .addToCell( &fNotesPrompt,        2, 10 )
  115.    .addToCell( &fNotes,              4, 10, 1, 2 );
  116.  
  117.   // Allow MLE control to expand vertically with the window.
  118.   fPage1.setRowHeight( 11, 0, true );
  119.  
  120.   // Allow MLE and entry fields to expand horizontally.
  121.   fPage1.setColumnWidth( 4, 0, true );
  122.  
  123.   // Create bottom and right margins.
  124.   ISize defaultCell = IMultiCellCanvas::defaultCell();
  125.   fPage1
  126.    .setRowHeight( 12, defaultCell.height() )
  127.    .setColumnWidth( 5, defaultCell.width() );
  128.  
  129.   // Set up the child controls.
  130.   fNamePrompt.setText( "Name" );
  131.   fName
  132.    .setLimit(60)
  133.    .setText( fPurchaseItem.name() )
  134.    .enableTabStop()
  135.    .enableGroup();
  136.   fQuantityPrompt.setText( "Quantity" );
  137.   fQuantity
  138.    .setText( fPurchaseItem.quantity() )
  139.    .enableTabStop()
  140.    .enableGroup();
  141.   fManufacturerPrompt.setText( "Manufacturer" );
  142.   fManufacturer
  143.    .setText( fPurchaseItem.manufacturer() )
  144.    .enableTabStop()
  145.    .enableGroup();
  146.   fPricePrompt.setText( "Price" );
  147.   fPrice
  148.    .setText( IString( fPurchaseItem.price() ))
  149.    .enableTabStop()
  150.    .enableGroup();
  151.   fNotesPrompt.setText( "Notes" );
  152.   fNotes
  153.    .setText( fPurchaseItem.notes() )
  154.    .enableTabStop()
  155.    .enableGroup();
  156.   ISize maxSize = IFont( &fNotes ).maxSize();
  157.   fNotes.setMinimumSize( maxSize * 3 );
  158.  
  159.   return *this;
  160. }
  161.  
  162. PurchaseItemView& PurchaseItemView :: initializePage2 ( )
  163. {
  164.   fPage2
  165.     .addToCell( &fBookBitmap,   2, 2 )
  166.     .addToCell( &fBitmapButton, 2, 4 )
  167.     .setColumnWidth( 1, 0, true)
  168.     .setColumnWidth( 3, 0, true)
  169.     .setRowHeight( 1, 0, true)
  170.     .setRowHeight( 3, 0, true)
  171.     .setRowHeight( 5, 0, true);
  172.  
  173.   fBitmapButton
  174.    .setText("Change Bitmap")
  175.    .enableTabStop()
  176.    .enableGroup();
  177.  
  178.  
  179.   fPage2.sizeTo( fBookBitmap.displaySize() );
  180.  
  181.   return *this;
  182. }
  183.  
  184. IBase::Boolean PurchaseItemView :: validated ( )
  185. {
  186.   Boolean valid = true;
  187.   if (fName.text().strip() == IString())
  188.   {
  189.      IPageHandle page = fNotebook.firstPage();
  190.      fNotebook.turnToPage( page );
  191.      fName.setFocus();
  192.      IMessageBox msg( this );
  193.      msg.show( "You must specify a name.",
  194.                IMessageBox::okButton
  195.                 | IMessageBox::errorIcon
  196.                 | IMessageBox::moveable );
  197.      valid = false;
  198.   }
  199.   return valid;
  200. }
  201.  
  202. PurchaseItemView& PurchaseItemView :: updateDataObject ( )
  203. {
  204.   fPurchaseItem
  205.    .setName( fName.text().strip() )
  206.    .setQuantity( fQuantity.text().strip() )
  207.    .setManufacturer( fManufacturer.text().strip() )
  208.    .setPrice( fPrice.text().asDouble() )
  209.    .setNotes( fNotes.text().strip() );
  210. // temporary until problem with cnr emphasis is fixed
  211. #ifdef IC_PM
  212.    fPurchaseItem.setClosed();
  213. #endif
  214.   return *this;
  215. }
  216.  
  217. PurchaseItemView&
  218.   PurchaseItemView :: updateBitmap ( const IString& fileName )
  219. {
  220.   IString upperFileName = IString::upperCase(fileName);
  221.   if (fileName != IString() && upperFileName.includes(".BMP"))
  222.   {
  223.      IGBitmap bitmap(fileName);
  224.      IBitmapHandle bitmapHandle = bitmap.handle();
  225.      fBookBitmap.setBitmap (bitmapHandle);
  226.   }
  227.   return *this;
  228. }
  229.  
  230. //================ PurchaseItemView::Handler ==================
  231.  
  232. PurchaseItemView::CmdHandler :: CmdHandler ( PurchaseItemView* frame,
  233.                                              PurchaseItem& purchaseItem )
  234.             : fPurchaseItemView(frame), fPurchaseItem(purchaseItem)
  235. {
  236.   handleEventsFor( frame->client() );
  237. }
  238.  
  239. IBase::Boolean PurchaseItemView::CmdHandler :: systemCommand
  240.                                           ( ICommandEvent& event )
  241. {
  242.   Boolean stopProcessing = false;
  243.   if (event.commandId() == ISystemMenu::idClose)
  244.   {                // View is being closed.
  245.      if (fPurchaseItemView->validated())
  246.      {             // Save any changes.
  247.         fPurchaseItemView->updateDataObject();
  248.      }
  249.      else
  250.      {             // Error, don't close the window.
  251.         stopProcessing = true;
  252.      }
  253.   }
  254.  
  255.   return stopProcessing;
  256. }
  257.  
  258. IBase::Boolean PurchaseItemView::CmdHandler :: command
  259.                                           ( ICommandEvent& event )
  260. {
  261.   Boolean stopProcessing = false;
  262.   if (event.commandId() == ID_BITMAPBUTTON)
  263.   {
  264.      // set up the file dialog as a child of the desktop.
  265.      IFileDialog::Settings settings;
  266.      settings.setFileName( "*.bmp" );
  267.  
  268.      IFileDialog fileDlg( 0, fPurchaseItemView, settings );
  269.      if ( fileDlg.pressedOK() )
  270.      {
  271.         fPurchaseItemView->updateBitmap(fileDlg.fileName());
  272.      }
  273.      stopProcessing = true;
  274.   }
  275.  
  276.   return stopProcessing;
  277. }
  278.