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

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //                                              SAMPLE CODE
  3. //
  4. // FileName: ACDFVw2.cpp
  5. //
  6. // ClassName: ACompDocFwkView
  7. //
  8. // Description: Compound Document Framework (SimpleServer) View
  9. //              This sample is to illustrates the use of notification and the use of
  10. //              keyboard, mouse and command handlers in the server application. When
  11. //              a notification is recieved from the model, if the handleNotification method
  12. //              was NOT overridden the drawContents would have been called and the notification ID
  13. //              not checked.
  14. //              In this case the handleNotification method was overridden as a result different data
  15. //              is set depending on the NotificationId
  16. //              The view contains 2 entry fields to allow the user to change the model data
  17. //              by entering the new data and hitting either newline or enter. Using the keyboard
  18. //              handler to trap these events.
  19. //              Doubleclick on either of the entry fields to change the string in MyObject
  20. //              in the model, using the mouse handler trap this event.
  21. //              The pushbutton and the menu option perform the same function which is
  22. //              to update both the strings in the model.
  23. ///////////////////////////////////////////////////////////////////////////////
  24. #include "ACDFMdl2.hpp"
  25. #include "ACDFVw2.hpp"
  26. #include "ACDFRes2.h"   // Resource id file
  27.  
  28. #include <iframe.hpp>   // this is need to set the icon
  29. #include <igstring.hpp>
  30. #include <igrect.hpp>
  31. #include <igrafctx.hpp>
  32. #include <imsgbox.hpp>
  33. #include <assert.h>
  34. #include <iguibndl.hpp> // must be include after windows.h
  35.  
  36.  
  37. ACompDocFwkView::ACompDocFwkView(IGUIBundle& bundle ) :
  38.     IView(bundle),
  39.     fButton(ID_BUTTON,this,this),
  40.     fDataStaticField1(ID_STATIC1, this, this),
  41.     fDataEntryField1 (ID_ENTRY1, this, this),
  42.     fDataStaticField2(ID_STATIC2, this, this),
  43.     fDataEntryField2 (ID_ENTRY2, this, this),
  44.     fMyObjectField (ID_OBJECT, this, this),
  45.     fButtonHandler( *this, handleButtonPressed),
  46.     fKeyboardHandler(*this, handleVirtualKeyPress),
  47.     fMouseHandler( *this, handleMouseClicked)
  48. {   IFUNCTRACE_DEVELOP();
  49.     bundle.frameWindow().setIcon(IC_ACDF2);   // Set the window icon
  50. }
  51.  
  52. ACompDocFwkView::~ACompDocFwkView()
  53. // Destructor - stop all the handlers
  54. {   IFUNCTRACE_DEVELOP();
  55.     fButtonHandler.stopHandlingEventsFor( this );
  56.     fKeyboardHandler.stopHandlingEventsFor(&fDataEntryField1 );
  57.     fKeyboardHandler.stopHandlingEventsFor(&fDataEntryField2 );
  58.  
  59.     fMouseHandler.stopHandlingEventsFor( &fDataEntryField1 );
  60.     fMouseHandler.stopHandlingEventsFor( &fDataEntryField2 );
  61. }
  62.  
  63. ACompDocFwkModel* ACompDocFwkView::getModelPointer() const
  64. // Get the pointer to the model
  65. {   IFUNCTRACE_DEVELOP();
  66.     ACompDocFwkModel* theModel = NULL;
  67.     ::dynamicCastTo( theModel,model() ); // Downcasts the model pointer
  68.     return theModel;
  69. }
  70.  
  71. void ACompDocFwkView::initialize()
  72. // Intialize code e.g build menus
  73. {   IFUNCTRACE_DEVELOP();
  74.  
  75.     IView::initialize();
  76.     bundle().objectView().setBackgroundColor(IColor(64,128,128)); // Set the color to green
  77.  
  78.     ACompDocFwkModel* fOurModel = getModelPointer();
  79.     assert(fOurModel != NULL);
  80.  
  81.     fButton.setText(STR_UPDATE);
  82.  
  83.     fDataStaticField1.setText(fOurModel->getString1());
  84.     fDataStaticField2.setText(fOurModel->getString2());
  85.     fMyObjectField.setText(fOurModel->getPointerMyString());
  86.  
  87.     // Start handling events for the keyboard, mouse and commands(buttons)
  88.     fMouseHandler.handleEventsFor(&fDataEntryField1);
  89.     fMouseHandler.handleEventsFor(&fDataEntryField2);
  90.  
  91.     fKeyboardHandler.setVirtualKeyMemberFn  (handleVirtualKeyPress);
  92.     fKeyboardHandler.handleEventsFor(&fDataEntryField1);
  93.     fKeyboardHandler.handleEventsFor(&fDataEntryField2);
  94.  
  95.     fButtonHandler.handleEventsFor( this );
  96. }
  97.  
  98.  
  99. ACompDocFwkView& ACompDocFwkView::sizeTo(const ISize& newSize)
  100. {   IFUNCTRACE_DEVELOP();
  101.     IView::sizeTo(newSize);
  102.     SizeFieldsToScreen(newSize);
  103.     return *this;
  104. }
  105.  
  106. ACompDocFwkView& ACompDocFwkView::moveSizeTo(const IRectangle& newRect)
  107. {  IFUNCTRACE_DEVELOP();
  108.    IView::moveSizeTo(newRect);
  109.    SizeFieldsToScreen(newRect.size());
  110.    return *this;
  111. }
  112.  
  113. void ACompDocFwkView::SizeFieldsToScreen(const ISize& myWindowSize)
  114. // Resize the fields depending on the screen size
  115. {   IFUNCTRACE_DEVELOP();
  116.  
  117.     int i = myWindowSize.coord1() / 2;
  118.  
  119.     fDataStaticField1.moveSizeTo(IRectangle(IPoint((i + 5),10), IPoint((i + i) ,40)));
  120.     fDataEntryField1.moveSizeTo(IRectangle(IPoint(0,10), IPoint((i - 5) ,40)));
  121.  
  122.     fDataStaticField2.moveSizeTo(IRectangle(IPoint((i + 5),50), IPoint((i + i) ,80)));
  123.     fDataEntryField2.moveSizeTo(IRectangle(IPoint(0,50), IPoint((i - 5) ,80)));
  124.  
  125.     fMyObjectField.moveSizeTo(IRectangle(IPoint((i - 60), 90),
  126.                                 IPoint((i  + 60), 120)));
  127.  
  128.     fButton.moveSizeTo(IRectangle(IPoint((i - 40), (myWindowSize.coord2() - 30)),
  129.                                 IPoint((i  + 40), myWindowSize.coord2())));
  130. }
  131.  
  132. void ACompDocFwkView::drawContents( IPresSpaceHandle& hdl,
  133.                   const IRectangle& invalidArea,
  134.                   Boolean metaFile )
  135. //  We can override "drawContents" if we want to optimize our drawing
  136. //  or do things differently when rendering to a meta-file (where controls
  137. //  won't be displayed).
  138. {   IFUNCTRACE_DEVELOP();
  139.  
  140.     ACompDocFwkModel* fOurModel = getModelPointer();
  141.     assert(fOurModel != NULL);
  142.  
  143.     if (metaFile)
  144.     {
  145.     // Place the embedded image code here !!!!
  146.         IString str1;
  147.         IString str2;
  148.         IString ptrstr;
  149.         IGraphicContext ctxt ( hdl );
  150.         ctxt.setFillColor(IColor::white);
  151.         IGString myString1(fOurModel->getString1(),IPoint(100,20));
  152.         IGString myString2(fOurModel->getString2(),IPoint(100,50));
  153.         IGString myPointer(fOurModel->getPointerMyString(),IPoint(100,80));
  154.         IGRectangle myRect(IRectangle(IPoint(10,10),IPoint(300,100)));
  155.  
  156.         if  (fOurModel->getString1().length() > 0)
  157.             str1 = IApplication::current().userResourceLibrary().loadString(STR_STRING1);
  158.         else
  159.             str1 = IApplication::current().userResourceLibrary().loadString(STR_STRING1_EMPTY);
  160.  
  161.         if  (fOurModel->getString2().length() > 0)
  162.             str2 = IApplication::current().userResourceLibrary().loadString(STR_STRING2);
  163.         else
  164.             str2 = IApplication::current().userResourceLibrary().loadString(STR_STRING2_EMPTY);
  165.  
  166.         if  (fOurModel->getPointerMyString().length() > 0)
  167.             ptrstr = IApplication::current().userResourceLibrary().loadString(STR_POINTER);
  168.         else
  169.             ptrstr = IApplication::current().userResourceLibrary().loadString(STR_POINTER_EMPTY);
  170.  
  171.         IGString gStr1(str1,IPoint(20,20));
  172.         IGString gStr2(str2,IPoint(20,50));
  173.         IGString gPtrStr(ptrstr,IPoint(20,80));
  174.  
  175.         myRect.drawOn(ctxt);
  176.         myString1.drawOn( ctxt );
  177.         myString2.drawOn( ctxt );
  178.         gStr1.drawOn( ctxt );
  179.         gStr2.drawOn( ctxt );
  180.         myPointer.drawOn( ctxt );
  181.         gPtrStr.drawOn( ctxt );
  182.     }
  183.  
  184.     fDataStaticField1.setText(fOurModel->getString1());
  185.     fDataStaticField2.setText(fOurModel->getString2());
  186.     fMyObjectField.setText(fOurModel->getPointerMyString());
  187. }
  188. void ACompDocFwkView::handleFileNew()
  189. // This is to clear al the fields when file new is selected
  190. {   IFUNCTRACE_DEVELOP();
  191.     ACompDocFwkModel* fOurModel = getModelPointer();
  192.     assert(fOurModel != NULL);
  193.  
  194.     fDataEntryField1.removeAll();
  195.     fDataEntryField2.removeAll();
  196. }
  197.  
  198. void ACompDocFwkView::handleNotification( const INotificationEvent& event)
  199. // Handle the notification events
  200. {   IFUNCTRACE_DEVELOP();
  201.  
  202.     ACompDocFwkModel* fOurModel = getModelPointer();
  203.     assert(fOurModel != NULL);
  204.  
  205.     if (event.notificationId() == fOurModel->kDataStringChange1 )
  206.     {
  207.         fDataEntryField1.setText(fOurModel->getString1());
  208.         fDataStaticField1.setText(fOurModel->getString1());
  209.     }
  210.     else if (event.notificationId() == fOurModel->kDataStringChange2 )
  211.     {
  212.         fDataEntryField2.setText(fOurModel->getString2());
  213.         fDataStaticField2.setText(fOurModel->getString2());
  214.     }
  215.     else if (event.notificationId() == fOurModel->kDataPointerChange )
  216.     {
  217.         fMyObjectField.setText(fOurModel->getPointerMyString());
  218.     }
  219.     else
  220.     refresh();  // will refresh the screen
  221. }
  222.  
  223.  
  224. Boolean ACompDocFwkView::handleButtonPressed(ICommandEvent& event)
  225. // Catch the button pressed and menu commands
  226. {   IFUNCTRACE_DEVELOP();
  227.     ACompDocFwkModel* fOurModel = getModelPointer();
  228.     assert(fOurModel != NULL);
  229.  
  230.     switch(event.commandId())
  231.     {
  232.         case MI_CHANGE:
  233.         case ID_BUTTON:
  234.         {
  235.             fOurModel->setString1(fDataEntryField1.text());
  236.             fOurModel->setString2(fDataEntryField2.text());
  237.             return true;
  238.         }
  239.     }
  240.  
  241.     return false;  //no processing was done
  242.  
  243. }
  244.  
  245. Boolean ACompDocFwkView::handleVirtualKeyPress( IKeyboardEvent &event )
  246. // Catch the key pressed update model
  247. {   IFUNCTRACE_DEVELOP();
  248.  
  249.     if (!event.isVirtual())
  250.         return false;
  251.  
  252.     ACompDocFwkModel* fOurModel = getModelPointer();
  253.     assert(fOurModel != NULL);
  254.  
  255.     Boolean fboolean = false;
  256.     switch (event.virtualKey())
  257.     {
  258.         case IKeyboardEvent::enter:
  259.         case IKeyboardEvent::newLine:
  260.         {
  261.             IWindow *pWindow = windowWithHandle(event.controlHandle());
  262.             unsigned long int winId = pWindow->id();
  263.             switch (winId)
  264.             {
  265.                 case ID_ENTRY1 :
  266.                 {
  267.                     fOurModel->setString1(fDataEntryField1.text());
  268.                     fboolean = true;
  269.                     break;
  270.                 }
  271.                 case ID_ENTRY2 :
  272.                 {
  273.                     fOurModel->setString2(fDataEntryField2.text());
  274.                     fboolean = true;
  275.                     break;
  276.                 }
  277.             }
  278.             break;
  279.         }
  280.     }
  281.     return fboolean;
  282. }
  283.  
  284. Boolean ACompDocFwkView::handleMouseClicked(IMouseClickEvent& event)
  285. // Catch the mouse click to update pointer
  286. {   IFUNCTRACE_DEVELOP();
  287.  
  288.     ACompDocFwkModel* fOurModel = getModelPointer();
  289.     assert(fOurModel != NULL);
  290.  
  291.     Boolean bReturnBool = false;
  292.  
  293.     switch (event.mouseAction())
  294.     {
  295.         case IMouseClickEvent::doubleClick:
  296.         {
  297.             IWindow *pWindow = windowWithHandle(event.windowUnderPointer());
  298.             unsigned long int winId = pWindow->id();
  299.             switch (winId)
  300.             {
  301.                 case ID_ENTRY1 :
  302.                 {
  303.                     fOurModel->setString1(fDataEntryField1.text());
  304.                     fOurModel->setPointerMyString(fDataEntryField1.text());
  305.                     bReturnBool = true;
  306.                     break;
  307.                 }
  308.                 case ID_ENTRY2 :
  309.                 {
  310.                     fOurModel->setString2(fDataEntryField2.text());
  311.                     fOurModel->setPointerMyString(fDataEntryField2.text());
  312.                     bReturnBool = true;
  313.                     break;
  314.                 }
  315.             }
  316.         }
  317.     }
  318.     return bReturnBool;
  319. }
  320.  
  321.