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

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //                                              SAMPLE CODE
  3. //
  4. // FileName: ACDFVw4.cpp
  5. //
  6. // ClassName: ACompDocFwkView
  7. //
  8. // Description: Compound Document Framework Container View 
  9. //              This is a container view which inherits from IView.
  10. //              This sample illustrates how the embedded object can be
  11. //              manipulated (shrink and grow)  on the screen. It also 
  12. //              shows how to programmatically select,unselect and launch the 
  13. //              embedded objects.                
  14. ///////////////////////////////////////////////////////////////////////////////
  15. #include "ACDFMdl4.hpp"
  16. #include "ACDFVw4.hpp"
  17. #include "acdfres4.h"
  18.  
  19. #include <igstring.hpp>
  20. #include <igrect.hpp>
  21. #include <igrafctx.hpp>
  22. #include <igelipse.hpp>
  23. #include <igline.hpp>
  24. #include <igarc.hpp>
  25. #include <imsgbox.hpp>
  26. #include <imenubar.hpp>
  27. #include <itbar.hpp>
  28. #include <iframe.hpp>   //This is needed to set the icon for the frame window
  29. #include <itbarbut.hpp>
  30. #include <ireslib.hpp>
  31. #include <assert.h>
  32.  
  33. #include <iguibndl.hpp> // must be include after windows.h
  34.  
  35.  
  36. ACompDocFwkView::ACompDocFwkView(IGUIBundle& bundle ) :
  37.     IView(bundle),
  38.     fCommandHandler( *this, handleCommand)
  39. // Constructor - resize the window
  40. {   IFUNCTRACE_DEVELOP();
  41.     bundle.frameWindow().setIcon(IC_ACDF4);
  42. }
  43.  
  44. ACompDocFwkView::~ACompDocFwkView()
  45. // Destructor - stop all the handlers
  46. {   IFUNCTRACE_DEVELOP();
  47.     
  48.     fCommandHandler.stopHandlingEventsFor( this );
  49.     fCommandHandler.stopHandlingEventsFor( &bundle().objectToolBar() );
  50. }
  51.  
  52. ACompDocFwkModel* ACompDocFwkView::getModelPointer() const
  53. // Get the pointer to the model 
  54. {   IFUNCTRACE_DEVELOP();
  55.     ACompDocFwkModel* theModel = NULL;
  56.     ::dynamicCastTo( theModel,model() );
  57.     return theModel;
  58. }
  59.  
  60. void ACompDocFwkView::initialize()
  61. // Intialize code e.g build menus
  62. {   IFUNCTRACE_DEVELOP();
  63.  
  64.     IView::initialize();
  65.     bundle().objectView().setBackgroundColor(IColor(64,128,128)); // Set the color to light green
  66.  
  67.     // Manually adding the toolbar buttons to the existing toolbar
  68.     IToolBar& myToolBar = bundle().objectToolBar();
  69.     IToolBarButton* myButton;
  70.  
  71.     myButton = new IToolBarButton (TB_GROW, &myToolBar, &myToolBar);
  72.     myToolBar.addAsLast( myButton, true);
  73.     myButton = new IToolBarButton (TB_SHRINK, &myToolBar, &myToolBar);
  74.     myToolBar.addAsLast( myButton, true);
  75.     myButton = new IToolBarButton (TB_NEXT, &myToolBar, &myToolBar);
  76.     myToolBar.addAsLast( myButton, true);
  77.     myButton = new IToolBarButton (TB_PREV, &myToolBar, &myToolBar);
  78.     myToolBar.addAsLast( myButton, true);
  79.     myButton = new IToolBarButton (TB_LAUNCH, &myToolBar, &myToolBar);
  80.     myToolBar.addAsLast( myButton, true);
  81.  
  82.     fCommandHandler.handleEventsFor( this );    // handle for menu's and pushbutton
  83.     fCommandHandler.handleEventsFor( &myToolBar ); // handle floating toolbar
  84. }
  85.  
  86. void ACompDocFwkView::drawContents( IPresSpaceHandle& hdl,
  87.                       const IRectangle& invalidArea,
  88.                       Boolean metaFile)
  89. //  We can override "drawContents" if we want to optimize our drawing
  90. //  or do things differently when rendering to a meta-file (where controls
  91. //  won't be displayed).
  92.  
  93. {       IFUNCTRACE_DEVELOP();
  94.  
  95.     if (metaFile)
  96.     {
  97.     // Place the embedded image code here !!!!
  98.         IGString myText (IApplication::current().userResourceLibrary().loadString(STR_METAFILE),IPoint(30,70));
  99.         IGraphicContext ctxt ( hdl );
  100.         IGEllipse myEllipse(IRectangle(IPoint(10,10),IPoint(210,50)));
  101.         IGLine myLine1(IPoint(10,30),IPoint(40,150));
  102.         IGLine myLine2(IPoint(210,30),IPoint(190,150));
  103.         IG3PointArc myArc(IPoint(40,150),IPoint(75,170),IPoint(190,150));
  104.  
  105.         myEllipse.drawOn( ctxt) ;
  106.         myLine1.drawOn( ctxt) ;
  107.         myLine2.drawOn( ctxt) ;
  108.         myArc.drawOn( ctxt) ;
  109.         myText.drawOn( ctxt) ;
  110.    }
  111. }
  112.  
  113. void ACompDocFwkView::handleSelectAll(Boolean select)
  114. // Handle select all by setting the background color to show that
  115. // select all has been selected
  116. {   IFUNCTRACE_DEVELOP();
  117.  
  118.     if (select)
  119.         bundle().objectView().setBackgroundColor(IColor(0,0,128));  // Set the color to dark Blue
  120.     else
  121.         bundle().objectView().setBackgroundColor(IColor(64,128,128)); // re-Set the color back to light green
  122. }
  123.  
  124. Boolean ACompDocFwkView::handleCommand(ICommandEvent& event)
  125. // Catch the button pressed and menu bars selected
  126. {   IFUNCTRACE_DEVELOP();
  127.     IMessageBox msg(IWindow::desktopWindow());
  128.     msg.setTitle(STR_VIEW);
  129.  
  130.     ACompDocFwkModel* ourModel = getModelPointer();
  131.     assert(ourModel != NULL);
  132.  
  133.     switch( event.commandId() )
  134.     {
  135.         case MI_GROW:
  136.         case TB_GROW:
  137.         {
  138.             // Get the current selected component from the embedder model
  139.             IEmbeddedComponent* mySelectedComponent = ourModel->selectedComponent();
  140.             if (mySelectedComponent )
  141.             {
  142.                 IRectangle myComponentArea = mySelectedComponent->area();
  143.                 myComponentArea.sizeBy( 2.0 );
  144.  
  145.                 mySelectedComponent ->resize( myComponentArea );
  146.             }
  147.             return true;
  148.         }
  149.         case MI_SHRINK:
  150.         case TB_SHRINK:
  151.         {
  152.             IEmbeddedComponent* mySelectedComponent = ourModel->selectedComponent();
  153.             if (mySelectedComponent )
  154.             {
  155.                 IRectangle myComponentArea = mySelectedComponent->area();
  156.                 myComponentArea.sizeBy( 0.5 );
  157.  
  158.                 mySelectedComponent ->resize( myComponentArea );
  159.             }
  160.             return true;
  161.         }
  162.         case MI_NEXT:
  163.         case TB_NEXT:
  164.         {
  165.             // Get the component list from the embedder model
  166.             IEmbedderModel::ComponentList myComponentList = ourModel->componentList();
  167.  
  168.             if ( myComponentList.isEmpty())
  169.                 return true;  // The list is emtpy therefore just exist
  170.  
  171.             // Get the number of components in the list (currently embedded)
  172.             unsigned long myNumberElements = myComponentList.numberOfElements();
  173.             ICursor* myCursor = myComponentList.newCursor();
  174.  
  175.             if (!myComponentList.setToFirst( *myCursor ))
  176.             {
  177.                 msg.show(IResourceId(STR_INVALID_CURSOR),IMessageBox::okButton);
  178.                 return true;
  179.             }
  180.             // Find out which component is selected inorder to get the next
  181.             IEmbeddedComponent* mySelectedComponent = ourModel->selectedComponent();
  182.             if (!mySelectedComponent )
  183.             {
  184.                 //Setting to the first component
  185.                 mySelectedComponent = myComponentList.elementAt( *myCursor);
  186.                 if ( mySelectedComponent )
  187.                 {
  188.                     ourModel->selectComponent (mySelectedComponent);
  189.                     refresh();
  190.                     return true;
  191.                 }
  192.             }
  193.             
  194.             for(int i=1;i <= myNumberElements;i++)
  195.             {
  196.                 if (mySelectedComponent ==(IEmbeddedComponent*) myComponentList.elementAtPosition(i))
  197.                 {
  198.                     myComponentList.setToPosition(i, *myCursor);
  199.                     if (myComponentList.setToNext(*myCursor) )
  200.                     {
  201.                         mySelectedComponent = myComponentList.elementAt(*myCursor);
  202.                     }
  203.                     else
  204.                     {
  205.                         // Set to the first if there is no next
  206.                         mySelectedComponent = (IEmbeddedComponent*)myComponentList.elementAtPosition(1);
  207.                     }
  208.                     ourModel->selectComponent (mySelectedComponent);
  209.                     refresh();
  210.                     return true;
  211.                 }
  212.             }
  213.         } 
  214.         case MI_PREV:
  215.         case TB_PREV:
  216.         {
  217.         // Get the component list from the embedder model
  218.             IEmbedderModel::ComponentList myComponentList = ourModel->componentList();
  219.  
  220.             if ( myComponentList.isEmpty())
  221.                 return true;
  222.  
  223.             unsigned long myNumberElements = myComponentList.numberOfElements();
  224.             ICursor* myCursor = myComponentList.newCursor();
  225.  
  226.             if (!myComponentList.setToFirst( *myCursor ))
  227.             {
  228.                 msg.show(IResourceId(STR_INVALID_CURSOR),IMessageBox::okButton);
  229.                 return true;
  230.             }
  231.  
  232.             IEmbeddedComponent* mySelectedComponent = ourModel->selectedComponent();
  233.             if (!mySelectedComponent )
  234.             {
  235.                 //Setting to the first component
  236.                 mySelectedComponent = myComponentList.elementAt( *myCursor);
  237.                 if ( mySelectedComponent )
  238.                 {
  239.                     ourModel->selectComponent (mySelectedComponent);
  240.                     refresh();  // Repaints the screen
  241.                     return true;
  242.                 }
  243.             }
  244.             
  245.             for(int i=1;i <= myNumberElements;i++)
  246.             {
  247.                 if (mySelectedComponent ==(IEmbeddedComponent*) myComponentList.elementAtPosition(i))
  248.                 {
  249.                     myComponentList.setToPosition(i, *myCursor);
  250.                     if (myComponentList.setToPrevious(*myCursor) )
  251.                     {
  252.                         mySelectedComponent = myComponentList.elementAt(*myCursor);
  253.                     }
  254.                     else
  255.                     {
  256.                         // Set to last if no previous
  257.                         mySelectedComponent = (IEmbeddedComponent*) myComponentList.elementAtPosition(myNumberElements);   
  258.                     }
  259.                     ourModel->selectComponent (mySelectedComponent);
  260.                     refresh(); // Repaints the screen
  261.                     return true;
  262.                 }
  263.             }
  264.         }
  265.         case MI_UNSELECT:
  266.         {
  267.             // Unselect the selected object
  268.             ourModel->selectComponent(NULL);
  269.             refresh();  // Repaints the screen
  270.             return true;
  271.         }
  272.         case MI_CASCADE:
  273.         {
  274.             // Get the component list from the embedder model
  275.             IEmbedderModel::ComponentList myComponentList = ourModel->componentList();
  276.  
  277.             if ( myComponentList.isEmpty())
  278.                 return true;
  279.  
  280.             unsigned long myNumberElements = myComponentList.numberOfElements();
  281.             ICursor* myCursor = myComponentList.newCursor();
  282.  
  283.             if (!myComponentList.setToFirst( *myCursor ))
  284.             {
  285.                 msg.show(IResourceId(STR_INVALID_CURSOR),IMessageBox::okButton);
  286.                 return true;
  287.             }
  288.  
  289.             IEmbeddedComponent* myComponent;
  290.             for(int i=1;i <= myNumberElements;i++)
  291.             {
  292.                 myComponent = (IEmbeddedComponent*)myComponentList.elementAtPosition(i);
  293.                 IRectangle myRect = myComponent->area();
  294.                 myRect.moveTo(IPoint((30*i),(30*i)));
  295.                 myComponent->resize(myRect);
  296.             }
  297.             return true;
  298.         }
  299.         case MI_HTILE: 
  300.         {
  301.         // Get the component list from the embedder model
  302.             IEmbedderModel::ComponentList myComponentList = ourModel->componentList();
  303.  
  304.             if ( myComponentList.isEmpty())
  305.                 return true;
  306.  
  307.             ISize myWindowSize = this->size();
  308.             IEmbeddedComponent* myComponent;
  309.             unsigned long myNumberElements = myComponentList.numberOfElements();
  310.  
  311.             if (myNumberElements == 1)
  312.             {
  313.                 myComponent = (IEmbeddedComponent*)myComponentList.elementAtPosition(1);
  314.                 IRectangle myRect = myComponent->area();
  315.                 myRect.sizeTo(myWindowSize);
  316.                 myComponent->resize(myRect);
  317.             }
  318.             ICursor* myCursor = myComponentList.newCursor();
  319.  
  320.             if (!myComponentList.setToFirst( *myCursor ))
  321.             {
  322.                 msg.show(IResourceId(STR_INVALID_CURSOR),IMessageBox::okButton);
  323.                 return true;
  324.             }
  325.  
  326.             // If there are more than 4 components screen gets to crowded 
  327.             if (myNumberElements < 4)
  328.             {
  329.                 for (int j=0,i=1;i <= myNumberElements;i++,j++)
  330.                 {
  331.                     myComponent = (IEmbeddedComponent*)myComponentList.elementAtPosition(i);
  332.                     IRectangle myRect = myComponent->area();
  333.                     int verticalHeight = myWindowSize.height() / myNumberElements;
  334.                     int verticalWidth = myWindowSize.width();
  335.                     myRect=IRectangle(IPoint(0,(j*verticalHeight)),IPoint(verticalWidth,((j+1)*verticalHeight)));
  336.                     myComponent->resize(myRect);
  337.                 }
  338.             }
  339.             else
  340.                 msg.show("There are more than 3 tiles, cannot tile");
  341.             
  342.             return true;
  343.         }
  344.         case MI_VTILE:
  345.         {
  346.         // Get the component list from the embedder model
  347.             IEmbedderModel::ComponentList myComponentList = ourModel->componentList();
  348.  
  349.             if ( myComponentList.isEmpty())
  350.                 return true;
  351.  
  352.             ISize myWindowSize = this->size();
  353.             IEmbeddedComponent* myComponent;
  354.             unsigned long myNumberElements = myComponentList.numberOfElements();
  355.  
  356.             if (myNumberElements == 1)
  357.             {
  358.                 myComponent = (IEmbeddedComponent*)myComponentList.elementAtPosition(1);
  359.                 IRectangle myRect = myComponent->area();
  360.                 myRect.sizeTo(myWindowSize);
  361.                 myComponent->resize(myRect);
  362.             }
  363.             ICursor* myCursor = myComponentList.newCursor();
  364.  
  365.             if (!myComponentList.setToFirst( *myCursor ))
  366.             {
  367.                 msg.show(IResourceId(STR_INVALID_CURSOR),IMessageBox::okButton);
  368.                 return true;
  369.             }
  370.  
  371.             if (myNumberElements < 4)
  372.             {
  373.                 for (int j=0,i=1;i <= myNumberElements;i++,j++)
  374.                 {
  375.                     myComponent = (IEmbeddedComponent*)myComponentList.elementAtPosition(i);
  376.                     IRectangle myRect = myComponent->area();
  377.                     int verticalHeight = myWindowSize.height() ;
  378.                     int verticalWidth = myWindowSize.width() / myNumberElements;
  379.                     myRect=IRectangle(IPoint((j*verticalWidth),0),IPoint(((j+1)*verticalWidth),verticalHeight));
  380.                     myComponent->resize(myRect);
  381.                 }
  382.             }
  383.             else
  384.                 msg.show("There are more than 3 tiles, cannot tile");
  385.             return true;
  386.         }
  387.         case MI_LAUNCH:
  388.         case TB_LAUNCH:
  389.         {
  390.             // Get the currently selected component and start it
  391.             IEmbeddedComponent* mySelectedComponent = ourModel->selectedComponent();
  392.             if (mySelectedComponent )
  393.             {
  394.                 mySelectedComponent ->goActive();
  395.             }
  396.             return true;
  397.         }
  398.     }               
  399.     return false;  // Command not handled
  400. }
  401.  
  402.