home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / ioc / acdf5 / acdfvw5.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-22  |  10.2 KB  |  304 lines

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //                                              SAMPLE CODE
  3. //
  4. // FileName: ACDFVw5.cpp
  5. //
  6. // ClassName: ACompDocFwkView
  7. //
  8. // Description: Compound Document Framework Streaming Server View
  9. //                              This sample show how to add elements to a collection class
  10. //              and stream the colllection out and in.
  11. //              Basically the screen allows the user to capture Name and Age
  12. //              and add them to a list (collection). The type of list the
  13. //              name is added to depends on which of the radio buttons have 
  14. //              been selected
  15. ///////////////////////////////////////////////////////////////////////////////
  16. #include "ACDFMdl5.hpp"
  17. #include "ACDFVw5.hpp"
  18. #include "acdfres5.h"
  19.  
  20. #include <inotifev.hpp>
  21. #include <icolor.hpp>
  22. #include <igstring.hpp>
  23. #include <igrect.hpp>
  24. #include <igrafctx.hpp>
  25. #include <imsgbox.hpp>
  26. #include <assert.h>
  27. #include <iframe.hpp>
  28. #include <iguibndl.hpp> // must be include after windows.h
  29. #include <ititle.hpp>
  30.  
  31. ACompDocFwkView::ACompDocFwkView(IGUIBundle& bundle ) :
  32.     IView(bundle),
  33.     fDataName (ID_NAME_FIELD,  this, this),
  34.     fDataAge (ID_AGE_FIELD,  this, this),
  35.     fPersonListBox (ID_LIST_FIELD,  this, this),
  36.     fStaticName (ID_NAME_STATIC,  this, this),
  37.     fStaticAge (ID_AGE_STATIC,  this, this),
  38.     fStaticList (ID_LIST_STATIC,  this, this),
  39.     fButtonAdd(ID_ADD_PUSH,  this, this),
  40.     fButtonDelete(ID_DEL_PUSH, this, this),
  41.     fRadioSeq (ID_RADIO_SEQ,  this, this),
  42.     fRadioKey (ID_RADIO_KEY,  this, this),
  43.     fCommandHandler(*this, handleCommand),
  44.     fSelectHandler(*this)
  45. // Constructor
  46. {   IFUNCTRACE_DEVELOP();
  47.     bundle.frameWindow().setIcon(IC_ACDF5);
  48. }
  49.  
  50. ACompDocFwkView::~ACompDocFwkView()
  51. // Destructor  - Stop handling events
  52. {   IFUNCTRACE_DEVELOP(); 
  53.     fCommandHandler.stopHandlingEventsFor(this);
  54.     fSelectHandler.stopHandlingEventsFor(this);
  55. }
  56.  
  57. ACompDocFwkModel* ACompDocFwkView::getModelPointer() const
  58. // Get the pointer to the model 
  59. {   IFUNCTRACE_DEVELOP();
  60.     ACompDocFwkModel* fOurModel = NULL;
  61.     ::dynamicCastTo( fOurModel,model() );
  62.     return fOurModel;
  63. }
  64.  
  65. void ACompDocFwkView::initialize()
  66. // Intialize code - set the size of the fields on the view
  67. {   IFUNCTRACE_DEVELOP();
  68.     IView::initialize();
  69.     bundle().objectView().setBackgroundColor(IColor(64,128,128)); // Set the color to green
  70.  
  71.     fStaticName.moveSizeTo(IRectangle(IPoint(5,5),IPoint(55,35)));
  72.     fStaticName.setText(STR_NAME); 
  73.     fStaticAge.moveSizeTo(IRectangle(IPoint(5,40),IPoint(55,70)));
  74.     fStaticAge.setText(STR_AGE);
  75.     fStaticList.moveSizeTo(IRectangle(IPoint(5,110),IPoint(55,140)));
  76.     fStaticList.setText(STR_LIST);
  77.  
  78.     fDataName.moveSizeTo(IRectangle(IPoint(70,5),IPoint(200 ,35)));
  79.     fDataAge.moveSizeTo(IRectangle(IPoint(70,40),IPoint(200 ,70)));
  80.     fPersonListBox.moveSizeTo(IRectangle(IPoint(70,110),IPoint(200 ,250)));
  81.     
  82.     fButtonAdd.moveSizeTo(IRectangle(IPoint(220,5), IPoint(300,35)));
  83.     fButtonDelete.moveSizeTo(IRectangle(IPoint(220,40), IPoint(300,70)));
  84.     fRadioSeq.moveSizeTo(IRectangle(IPoint(220,110),IPoint(350,140)));
  85.     fRadioKey.moveSizeTo(IRectangle(IPoint(220,150),IPoint(350,180)));
  86.     fButtonAdd.setText(STR_ADD);
  87.     fButtonDelete.setText(STR_DELETE);
  88.     fRadioSeq.setText(STR_SEQUENCE);
  89.     fRadioKey.setText(STR_KEYED);
  90.     fRadioSeq.click();
  91.     fRadioSeq.disableTabStop();
  92.     
  93.     ITitle myTitle(&bundle().frameWindow());
  94.     myTitle.setText (IApplication::current().userResourceLibrary().loadString(ID_ACDF5));
  95.  
  96.     fDataName.enableTabStop();
  97.     fCommandHandler.handleEventsFor(this);
  98.     fSelectHandler.handleEventsFor(this);
  99. }
  100.  
  101. void ACompDocFwkView::drawContents( IPresSpaceHandle& hdl,
  102.                   const IRectangle& invalidArea,
  103.                   Boolean metaFile )
  104. //  We can override "drawContents" if we want to optimize our drawing
  105. //  or do things differently when rendering to a meta-file (where controls
  106. //  won't be displayed).
  107. {   IFUNCTRACE_DEVELOP();
  108.     if (metaFile)
  109.     {
  110.     // Place the embedded image here. This will be displayed when server in
  111.     // embedded but inactive.
  112.         IGraphicContext ctxt ( hdl );
  113.         ctxt.setFillColor(IColor::white);
  114.         IGRectangle myRect(IRectangle(IPoint(20,20), IPoint(300,100)));
  115.         myRect.drawOn( ctxt );
  116.  
  117.         if (fSelectedPerson.getName().length() > 0 )
  118.         {
  119.             IGString strName (IApplication::current().userResourceLibrary().loadString(STR_NAME),IPoint(25,20));
  120.             IGString strAge  (IApplication::current().userResourceLibrary().loadString(STR_AGE), IPoint(25,50));
  121.             IGString myStrName(fSelectedPerson.getName(),IPoint(100,20));
  122.             IGString myStrAge(fSelectedPerson.getAge(),IPoint(100,50));
  123.             strName.drawOn( ctxt );
  124.             myStrName.drawOn( ctxt );
  125.             strAge.drawOn( ctxt );
  126.             myStrAge.drawOn( ctxt );
  127.         }
  128.         else
  129.         {
  130.             IGString str (IApplication::current().userResourceLibrary().loadString(STR_NOTHING),IPoint(20,20));
  131.             str.drawOn( ctxt );
  132.         }
  133.         
  134.     }
  135. }
  136.  
  137. void ACompDocFwkView::handleNotification( const INotificationEvent& event)
  138. // Handle the notification events
  139. {   IFUNCTRACE_DEVELOP();
  140.  
  141.     ACompDocFwkModel* fOurModel = getModelPointer();
  142.     assert(fOurModel != NULL);
  143.  
  144.     if (event.notificationId() == fOurModel->kSelectionChanged )
  145.     {   
  146.         fSelectedPerson = fOurModel->getSelectedPerson();
  147.     }    
  148.     else  // This is to update on List change
  149.     {
  150.         fPersonListBox.removeAll();
  151.         TPerson myPerson = fOurModel->getPersonFirstList();
  152.     
  153.         while (myPerson.getName().length() != 0)
  154.         {
  155.             fPersonListBox.addAsLast(myPerson.getName());       
  156.             myPerson = fOurModel->getPersonNextList();
  157.         }
  158.     }
  159. }
  160. void ACompDocFwkView::handleSelectAll(Boolean select)
  161. // Handle the file select all to show that all has been selected
  162. {   IFUNCTRACE_DEVELOP();
  163.  
  164.     unsigned char myRed,myBlue,myGreen;
  165.  
  166.     IColor myColor (fDataName.backgroundColor());
  167.     myRed = (255 - myColor.redMix());
  168.     myGreen = (255 - myColor.greenMix());
  169.     myBlue = (255 - myColor.blueMix());
  170.     fDataName.setBackgroundColor(IColor(myRed,myGreen,myBlue));
  171.  
  172.     myColor = fDataName.foregroundColor();
  173.     myRed = (255 - myColor.redMix());
  174.     myGreen = (255 - myColor.greenMix());
  175.     myBlue = (255 - myColor.blueMix());
  176.     fDataName.setForegroundColor(IColor(myRed,myGreen,myBlue));
  177.     
  178.     myColor = fDataAge.backgroundColor();
  179.     myRed = (255 - myColor.redMix());
  180.     myGreen = (255 - myColor.greenMix());
  181.     myBlue = (255 - myColor.blueMix());
  182.     fDataAge.setBackgroundColor(IColor(myRed,myGreen,myBlue));    
  183.  
  184.     myColor = fDataAge.foregroundColor();
  185.     myRed = (255 - myColor.redMix());
  186.     myGreen = (255 - myColor.greenMix());
  187.     myBlue = (255 - myColor.blueMix());
  188.     fDataAge.setForegroundColor(IColor(myRed,myGreen,myBlue));    
  189.     
  190.     myColor = fPersonListBox.backgroundColor();
  191.     myRed = (255 - myColor.redMix());
  192.     myGreen = (255 - myColor.greenMix());
  193.     myBlue = (255 - myColor.blueMix());
  194.     fPersonListBox.setBackgroundColor(IColor(myRed,myGreen,myBlue));    
  195.  
  196.     myColor = fPersonListBox.foregroundColor();
  197.     myRed = (255 - myColor.redMix());
  198.     myGreen = (255 - myColor.greenMix());
  199.     myBlue = (255 - myColor.blueMix());
  200.     fPersonListBox.setForegroundColor(IColor(myRed,myGreen,myBlue));    
  201.     refresh();
  202. }
  203.  
  204. void ACompDocFwkView::handleFileNew()
  205. // Handle the file new to clear the screen
  206. {   IFUNCTRACE_DEVELOP();
  207.     fDataName.removeAll();
  208.     fDataAge.removeAll();
  209.     fRadioSeq.select();
  210. }
  211.  
  212. Boolean ACompDocFwkView::handleCommand(ICommandEvent& event)
  213. // Handle all the menu and push button commands
  214. {   IFUNCTRACE_DEVELOP();
  215.  
  216.     Boolean bReturn = false;
  217.     switch( event.commandId() )
  218.     {
  219.         case ID_ADD_PUSH:
  220.         case MI_ADD:
  221.         {
  222.             addToSeq();
  223.             fDataName.removeAll();
  224.             fDataAge.removeAll();
  225.             bReturn = true;
  226.             break;                  
  227.         }
  228.         case ID_DEL_PUSH:
  229.         case MI_DELETE:
  230.         {
  231.             deleteFromSeq();
  232.             fDataName.removeAll();
  233.             fDataAge.removeAll();
  234.             bReturn = true;
  235.             break;
  236.         }
  237.     }
  238.     return bReturn;
  239. }
  240.  
  241. Boolean ACompDocFwkView::handleSelected(IControlEvent& event)
  242. // Handle all the selections on Radio button and List Boxes
  243. {   IFUNCTRACE_DEVELOP();
  244.  
  245.     ACompDocFwkModel* fOurModel = getModelPointer();
  246.     assert(fOurModel != NULL);
  247.  
  248.     Boolean bReturn = false;
  249.                 
  250.     switch( event.controlId() )
  251.     {
  252.         case ID_RADIO_SEQ:
  253.         {
  254.             fOurModel->setCollectionType(fOurModel->kSeq);
  255.             fDataName.removeAll();
  256.             fDataAge.removeAll();
  257.             bReturn = true;
  258.             break;                  
  259.         }
  260.         case ID_RADIO_KEY:
  261.         {
  262.             fOurModel->setCollectionType(fOurModel->kKey);
  263.             fDataName.removeAll();
  264.             fDataAge.removeAll();
  265.             bReturn = true;
  266.             break;
  267.         }
  268.         case ID_LIST_FIELD:
  269.         {
  270.             long mySelection = fPersonListBox.selection();
  271.             IString myText =  fPersonListBox.itemText(mySelection);
  272.             TPerson myPerson = fOurModel->getPersonList((mySelection+1),myText);
  273.             fDataName.setText(myPerson.getName());
  274.             fDataAge.setText(myPerson.getAge());
  275.             bReturn = true;
  276.             break;
  277.         }
  278.     }
  279.     return bReturn;
  280. }
  281.  
  282. void ACompDocFwkView::addToSeq()
  283. // Add to the list
  284. {   IFUNCTRACE_DEVELOP();
  285.  
  286.     ACompDocFwkModel* fOurModel = getModelPointer();
  287.     assert(fOurModel != NULL);
  288.  
  289.     TPerson myPerson(fDataName.text(), fDataAge.text());
  290.     fOurModel->addToList(myPerson);
  291. }       
  292.     
  293. void ACompDocFwkView::deleteFromSeq()
  294. // Delete from the list
  295. {   IFUNCTRACE_DEVELOP();
  296.  
  297.     ACompDocFwkModel* fOurModel = getModelPointer();
  298.     assert(fOurModel != NULL);
  299.  
  300.     TPerson myPerson(fDataName.text(), fDataAge.text());
  301.     fOurModel->deleteFromList(myPerson);
  302. }       
  303.  
  304.