home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / powergui / notebook / smrtguid / smrtchce.cpp < prev    next >
C/C++ Source or Header  |  1996-10-29  |  6KB  |  225 lines

  1. //************************************************************
  2. // Notebook Control - Smart Guide Notebook
  3. //
  4. // Classes:  SingleChoiceSmartPage
  5. //
  6. // Copyright (C) 1994, Law, Leong, Love, Olson, Tsuji.
  7. // Copyright (c) 1997 John Wiley & Sons, Inc.
  8. // All Rights Reserved.
  9. //************************************************************
  10. #include <irect.hpp>
  11. #include <isplitcv.hpp>
  12. #include <isetcv.hpp>
  13. #include <istring.hpp>
  14. #include <ilistbox.hpp>
  15. #include <iradiobt.hpp>
  16. #include <iselhdr.hpp>
  17. #include <ievtdata.hpp>
  18. #include <iseq.h>
  19. #include "smrtguid.hpp"
  20.  
  21. ///
  22. ///  Define the data stored with each Choice in
  23. ///  our SingleChoiceSmartPage
  24. class SmartChoice
  25. {
  26. public:
  27.    SmartChoice ( const IString& choiceName,
  28.                  unsigned long  helpId)
  29.       : fChoiceName (choiceName),
  30.         fHelpId (helpId)
  31.     {}
  32. IString
  33.   choiceName    ( ) const
  34.   { return fChoiceName; }
  35.  
  36. unsigned long
  37.   helpId        ( ) const
  38.   { return fHelpId;  }
  39.  
  40. private:
  41. IString
  42.   fChoiceName;
  43. unsigned long
  44.   fHelpId;
  45. };
  46.  
  47.  
  48. class SmartChoiceList : public ISequence<SmartChoice>
  49. {
  50. public:
  51.   SmartChoiceList () : ISequence<SmartChoice>(10) {}
  52.  
  53. };
  54.  
  55. class SmartChoiceSelectHandler : public ISelectHandler
  56. {
  57. public:
  58.   SmartChoiceSelectHandler ( IWindow* commandEventReceiver)
  59.     : fCommandEventReceiver( *commandEventReceiver) {}
  60.  
  61. protected:
  62. virtual Boolean
  63.   selected ( IControlEvent& event )
  64.   {
  65.      fCommandEventReceiver.sendEvent(IWindow::command,
  66.                         IEventParameter1(CID_REFRESH) );
  67.      return true;
  68.   }
  69. private:
  70.  
  71. IWindow
  72.  &fCommandEventReceiver;
  73. SmartChoiceSelectHandler ( const SmartChoiceSelectHandler&);
  74. SmartChoiceSelectHandler& operator = ( const SmartChoiceSelectHandler&);
  75. };
  76.  
  77.  
  78.  
  79. static const unsigned long kNotSelected = (unsigned long)-1;
  80.  
  81.  
  82.  
  83. IWindow* SingleChoiceSmartPage::createAndOrphanPage (
  84.                                 IWindow*          parent,
  85.                                 IWindow*          owner,
  86.                                 const IRectangle& initialRect)
  87. {
  88.   // Create a split canvas to house the multicell of our parent on
  89.   // top and our own multicell on the bottom.
  90.   // Note: Create the split canvas invisible to minimize layout.
  91.   ISplitCanvas* client = new ISplitCanvas(WID_CLIENTCANVAS, parent, owner, initialRect,
  92.                             ISplitCanvas::horizontal | ISplitCanvas::noSplitBars  );
  93.  
  94.   // Create a select handler
  95.   fSelectHandler = new SmartChoiceSelectHandler(client);
  96.  
  97.   // Ask our parent to create its window in the set canvas.
  98.   Inherited::createAndOrphanPage( client,
  99.                                   client,
  100.                                   initialRect);
  101.  
  102.   // Create our own set canvas for our choices.
  103.   ISetCanvas* setCanvas = new ISetCanvas (WID_SELECTCANVAS, client, client);
  104.  
  105.   unsigned long selectedChoice = currentChoice();
  106.  
  107.   // If greater than 4 choices, create listbox, else create radio buttons
  108.   if (fNumberOfChoices > 4)
  109.   {
  110.      IListBox* listBox = new IListBox( WID_SELECTLISTBOX,
  111.                                        setCanvas,
  112.                                        setCanvas);
  113.      fSelectHandler->handleEventsFor(listBox);
  114.      for(unsigned long choices=0; choices<fNumberOfChoices; choices++)
  115.      {
  116.        listBox->addAsLast(choiceTextAtIndex(choices));
  117.      }
  118.      (*listBox)
  119.        .select(selectedChoice)
  120.        .setAutoDeleteObject( );
  121.      fSelectionWindow = listBox;
  122.   }
  123.   else {
  124.      setCanvas->setDeckOrientation(ISetCanvas::vertical);
  125.      fSelectHandler->handleEventsFor(setCanvas);
  126.      for(unsigned long choices=0; choices<fNumberOfChoices; choices++)
  127.      {
  128.        IRadioButton* button = new IRadioButton( WID_SELECTRADIO+choices,
  129.                                                 setCanvas,
  130.                                                 setCanvas);
  131.        if (choices==0)
  132.           button->enableGroup();
  133.  
  134.        (*button)
  135.          .setText(choiceTextAtIndex(choices))
  136.          .setAutoDeleteObject( );
  137.  
  138.        if(choices == selectedChoice)
  139.        {
  140.          button->select();
  141.          // Save this as the button to query selection later.
  142.          fSelectionWindow = button;
  143.        }
  144.      }
  145.   }
  146.   client->show();
  147.   return client;
  148. }
  149.  
  150. SingleChoiceSmartPage& SingleChoiceSmartPage::addChoice(
  151.                         const IString& choiceText,
  152.                         unsigned long  helpId)
  153. {
  154.   // Allocate the Smart Choice list if necessary.
  155.   if (fSmartChoiceList == 0)
  156.      fSmartChoiceList = new SmartChoiceList;
  157.  
  158.   fSmartChoiceList->addAsLast(SmartChoice(choiceText, helpId));
  159.   fNumberOfChoices++;
  160.   return *this;
  161. }
  162.  
  163. IString SingleChoiceSmartPage::choiceTextAtIndex ( unsigned long index )
  164. {
  165.    // Collections are one based.
  166.    unsigned long sequenceIndex = index+1;
  167.    const SmartChoice& smartChoice =
  168.                  fSmartChoiceList->elementAtPosition(sequenceIndex);
  169.    return smartChoice.choiceName();
  170. }
  171.  
  172.  
  173. unsigned long SingleChoiceSmartPage::currentChoice ( ) const
  174. {
  175.   unsigned long selectedIndex = fSelectedChoice;
  176.   if(fSelectionWindow!=0)
  177.   {
  178.     if (fNumberOfChoices > 4)
  179.       selectedIndex = ((IListBox*)fSelectionWindow)->selection();
  180.     else
  181.       selectedIndex = ((IRadioButton*)fSelectionWindow)->selectedIndex();
  182.   }
  183.  
  184.   return selectedIndex;
  185. }
  186.  
  187. Boolean SingleChoiceSmartPage::isOKToClose ( IString&  ) const
  188. {
  189.   if (fSelectedChoice != kNotSelected)
  190.      return true;
  191.   else
  192.      return false;
  193. }
  194.  
  195. unsigned long SingleChoiceSmartPage::numberOfChoices ( ) const
  196. {
  197.    if (fSmartChoiceList != 0)
  198.       return fSmartChoiceList->numberOfElements();
  199.    else
  200.       return 0;
  201. }
  202.  
  203. unsigned long SingleChoiceSmartPage::helpId ( ) const
  204. {
  205.   if (fSelectedChoice != kNotSelected)
  206.      return Inherited::helpId();
  207.   else
  208.   {
  209.      const SmartChoice& smartChoice = fSmartChoiceList->elementAtPosition(fSelectedChoice+1);
  210.      return smartChoice.helpId();
  211.   }
  212. }
  213.  
  214. SingleChoiceSmartPage& SingleChoiceSmartPage::setSelectedChoice ( unsigned long selectedChoice )
  215. {
  216.   fSelectedChoice = selectedChoice;
  217.   return *this;
  218. }
  219. ISize SingleChoiceSmartPage::minimumSize ( ) const
  220. {
  221.   // Need to implement
  222.   return ISize(100,100);
  223. }
  224.  
  225.