home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / toolba.zip / TOOLBAR.CPP < prev    next >
Text File  |  1994-09-17  |  10KB  |  381 lines

  1. /************************************************************************
  2. * Toolbar.cpp - demonstration program class implementation              *
  3. * --------------------------------------------------------------------- *
  4. * Demonstrate a toolbar class and simple graphical display.             *
  5. *                                                                       *
  6. * Written by: Barry Lay                                                 *
  7. * Date:       16 September 1994                                         *
  8. *                                                                       *
  9. ************************************************************************/
  10.  
  11. #include <iapp.hpp>                     //IApplication Class
  12. #include <istattxt.hpp>                 //IStaticText Class
  13. #include <istring.hpp>                  //IString Class
  14. #include <ibmpctl.hpp>                  //IBitmapControl Class
  15. #include <iinfoa.hpp>
  16. #include <ifont.hpp>
  17.  
  18. #define INCL_WIN
  19. #define INCL_GPIPRIMITIVES
  20. #include <os2.h>
  21.  
  22. #include "atimehdr.hpp"
  23. #include "amhlphdr.hpp"
  24. #include "atoolbar.hpp"
  25. #include "ainfoa.hpp"
  26. #include "toolbar.hpp"
  27. #include "toolbar.h"
  28.  
  29.  
  30. lineSegment::lineSegment( IPoint start, IPoint end, IColor colour ) :
  31.    lineStart( start ),
  32.    lineEnd( end ),
  33.    lineColour( colour )
  34. {} /* end lineSegment::lineSegment( ... ) */
  35.  
  36. /* ---------------------
  37.    private member access
  38.    --------------------- */
  39.  
  40. void lineSegment::setStart( IPoint start )
  41. {
  42.    lineStart = start;
  43. }  /* end lineSegment::setStart( ... ) */
  44.  
  45. void lineSegment::setEnd( IPoint end )
  46. {
  47.    lineEnd = end;
  48. }  /* end lineSegment::setEnd() */
  49.  
  50. void lineSegment::setColour( IColor colour )
  51. {
  52.    lineColour = colour;
  53. }  /* end lineSegment::setColour() */
  54.  
  55. IPoint lineSegment::start()
  56. {
  57.    return lineStart;
  58. }  /* end lineSegment::start() */
  59.  
  60. IPoint lineSegment::end()
  61. {
  62.    return lineEnd;
  63. }  /* end lineSegment::end() */
  64.  
  65. IColor lineSegment::colour()
  66. {
  67.    return lineColour;
  68. }  /* end lineSegment::colour() */
  69.  
  70. /* -----------------------------------------------------
  71.    draw the line segment in the given presentation space
  72.    ----------------------------------------------------- */
  73.  
  74. Boolean lineSegment::draw( IPresSpaceHandle hps )
  75. {
  76.    if( !GpiSetColor(hps, lineColour.index()) )
  77.       return false;
  78.    POINTL s = lineStart.asPOINTL();
  79.    POINTL e = lineEnd.asPOINTL();
  80.    if( !GpiMove(hps, &s) )
  81.       return false;
  82.    return GpiLine( hps, &e );
  83. }  /* end lineSegment::draw( ... ) */
  84.  
  85.  
  86. /* -----------------------
  87.    main application window
  88.    ----------------------- */
  89.  
  90. MainWin::MainWin( unsigned long windowId )
  91.     : IFrameWindow ( IFrameWindow::defaultStyle() | IFrameWindow::menuBar,
  92.       windowId )
  93. {
  94.    currentId = 0;
  95.    setupMenuBar( windowId );
  96.    setupInfoArea();
  97.    setupClient();
  98.    createToolBar();
  99.  
  100.    setFocus();
  101.    show();
  102.  
  103.    /* set up flashing lines */
  104.  
  105.    setTimeout( DRAW_TIMEOUT );
  106.    ATimeHandler::handleEventsFor( this );
  107.  
  108. } /* end MainWin::MainWin( ... ) */
  109.  
  110.  
  111. MainWin::~MainWin()
  112. {
  113.    destroyToolBar();
  114.    destroySideBar();
  115.    ATimeHandler::stopHandlingEventsFor( this );
  116.    AMicroHelpHandler::stopHandlingEventsFor( this );
  117. }  /* end MainWin::~MainWin() */
  118.  
  119.  
  120. void MainWin::setupMenuBar( unsigned long windowId )
  121. {
  122.    menuBar=new IMenuBar( windowId, this );
  123.    fToolBarActive = false;
  124.    fSideBarActive = false;
  125.    ICommandHandler::handleEventsFor( this );
  126. } /* end MainWin::setupMenuBar( ... ) */
  127.  
  128.  
  129. void MainWin::setupClient()
  130. {
  131.    clientWindow = new ICanvas( WND_CANVAS, this, this );
  132.    clientWindow->setAutoDeleteObject();
  133.    currentColour = new IColor( IColor::white );
  134.    clientWindow->setColor( ICanvas::background, *currentColour );
  135.    setClient( clientWindow );
  136.    IPaintHandler::handleEventsFor( clientWindow );
  137.    IResizeHandler::handleEventsFor( clientWindow );
  138.  
  139.    srand( 17 );
  140.    targetLines = NUM_LINES;
  141.    numLines = 0;
  142. }  /* end MainWin::setupClient() */
  143.  
  144.  
  145. void MainWin::setupInfoArea()
  146. {
  147.    infoArea = new AInfoArea( this );
  148.    infoArea->setInactiveText( MICRO_DEFAULT );
  149.    infoArea->setHelpId( MICRO_HELP_MSG );
  150.    setExtensionSize( infoArea, (int)IFont(infoArea).maxCharHeight() );
  151. }  /* end MainWin::setupInfoArea() */
  152.  
  153.  
  154. void MainWin::createToolBar()
  155. {
  156.    if (!fToolBarActive)
  157.    {
  158.       myToolBar = new AToolBar( WND_BAR, this );
  159.       myToolBar->setMessageId( MICRO_HELP_MSG );
  160.  
  161.       myToolBar->addButton( TBS_1 );
  162.       myToolBar->addButton( TBS_2 );
  163.       myToolBar->addButton( TBS_3 );
  164.       myToolBar->addButton( TBS_4 );
  165.       myToolBar->addSpace( 10 );
  166.       myToolBar->addButton( TBL_1 );
  167.       myToolBar->addButton( TBL_2 );
  168.       myToolBar->addSpace( 10 );
  169.       myToolBar->addButton( TBB_1 );
  170.       myToolBar->addButton( TBB_2 );
  171.  
  172.       addExtension( myToolBar, IFrameWindow::aboveClient,
  173.          (int)myToolBar->minimumSize().height() );
  174.       fToolBarActive = true;
  175.    }
  176. }  /* end MainWin::createToolBar() */
  177.  
  178.  
  179. void MainWin::destroyToolBar()
  180. {
  181.    if (fToolBarActive)
  182.    {
  183.       removeExtension( myToolBar );
  184.       delete myToolBar;
  185.       fToolBarActive = false;
  186.    }
  187.    setFocus();
  188. }  /* end MainWin::destroyToolBar() */
  189.  
  190.  
  191. void MainWin::createSideBar()
  192. {
  193.    if ( !fSideBarActive )
  194.    {
  195.       mySideBar = new AToolBar( WND_SIDE, this, 
  196.          ISetCanvas::verticalDecks | ISetCanvas::packTight | 
  197.          ISetCanvas::topAlign | ISetCanvas::centerAlign |
  198.          IWindow::visible );
  199.       myToolBar->setMessageId( MICRO_HELP_MSG );
  200.  
  201.       myToolBar->addButton( TBS_1 );
  202.       myToolBar->addButton( TBS_2 );
  203.       myToolBar->addButton( TBS_3 );
  204.       myToolBar->addButton( TBS_4 );
  205.       myToolBar->addSpace( 10 );
  206.       myToolBar->addButton( TBL_1 );
  207.       myToolBar->addButton( TBL_2 );
  208.       myToolBar->addSpace( 10 );
  209.       myToolBar->addButton( TBB_1 );
  210.       myToolBar->addButton( TBB_2 );
  211.  
  212.       addExtension( mySideBar,IFrameWindow::leftOfClient,
  213.          (int)mySideBar->minimumSize().width() );
  214.       fSideBarActive = true;
  215.    }
  216. }  /* end MainWin::createSideBar() */
  217.  
  218.  
  219. void MainWin::destroySideBar()
  220. {
  221.    if ( fSideBarActive )
  222.    {
  223.       removeExtension( mySideBar );
  224.       delete mySideBar;
  225.       fSideBarActive = false;
  226.    }
  227.    setFocus();
  228. }  /* end MainWin::destroySideBar() */
  229.  
  230.  
  231. Boolean MainWin::command(ICommandEvent& cEvent)    //Define command member function
  232. {
  233.    switch ( cEvent.commandId() )
  234.    {
  235.    case MI_HALF:                    /* slow timer down by half */
  236.    case TBS_1:
  237.       if ( timeout() == 0 ) {
  238.          setTimeout( oldTime );
  239.       } else {
  240.          setTimeout( timeout() * 2 );
  241.       } /* endif */
  242.       break;
  243.    case MI_STOP:                    /* stop timer */
  244.    case TBS_2:
  245.       if ( timeout() != 0 ) {
  246.          oldTime = timeout();
  247.          setTimeout( 0 );
  248.       } /* endif */
  249.       break;
  250.    case MI_GO:                      /* restart timer */
  251.    case TBS_3:
  252.       if ( timeout() == 0 ) {
  253.          setTimeout( oldTime );
  254.       } /* endif */
  255.       break;
  256.    case MI_TWICE:                   /* speed timer up twice */
  257.    case TBS_4:
  258.       if ( timeout() == 0 ) {
  259.          setTimeout( oldTime );
  260.       } else {
  261.          setTimeout( timeout() / 2);
  262.       } /* endif */
  263.       break;
  264.  
  265.    case MI_LESS:                    /* reduce number of lines */
  266.    case TBL_1:
  267.       if ( targetLines > 1 ) {
  268.          targetLines--;
  269.       } /* endif */
  270.       break;
  271.    case MI_MORE:                    /* increase number of lines */
  272.    case TBL_2:
  273.       targetLines++;
  274.       break;
  275.  
  276.    case MI_BLACK:                   /* black background */
  277.    case TBB_1:
  278.       *currentColour = IColor::black;
  279.       clientWindow->setColor( ICanvas::background, *currentColour );
  280.       clientWindow->refresh();
  281.       break;
  282.    case MI_WHITE:                   /* white background */
  283.    case TBB_2:
  284.       *currentColour = IColor::white;
  285.       clientWindow->setColor( ICanvas::background, *currentColour );
  286.       clientWindow->refresh();
  287.       break;
  288.  
  289.    default:
  290.       return false;
  291.    }                        
  292.    return true;
  293. }  /* end Toolbar::command( ... ) */
  294.  
  295.  
  296. Boolean MainWin::paintWindow( IPaintEvent &evt )
  297. {
  298.    evt.clearBackground( *currentColour );
  299.    lines.removeAll();
  300.    numLines = 0;
  301.    return true;
  302. }  /* end MainWin::paintWindow( ... ) */
  303.  
  304.  
  305. Boolean MainWin::windowResize( IResizeEvent &evt )
  306. {
  307.    clientWindow->refresh();
  308.    return true;
  309. }  /* end MainWin::windowResize( ... ) */
  310.  
  311.  
  312. Boolean MainWin::tick( IEvent &evt )
  313. {
  314.    IPresSpaceHandle  hps( client()->presSpace() );
  315.    unsigned long     xLine, yLine;
  316.    unsigned long     nStart, nEnd, nOther;
  317.    IColor            colour( IColor::white );
  318.    IColor            colours[NUM_COLOURS] =
  319.    {
  320.       IColor::blue,     IColor::red,       IColor::pink,     IColor::green,
  321.       IColor::cyan,     IColor::yellow,    IColor::darkBlue, IColor::darkRed,
  322.       IColor::darkPink, IColor::darkGreen, IColor::darkCyan, IColor::brown
  323.    };
  324.    lineSegment       line;
  325.  
  326.    IPoint   start;
  327.    IPoint   end;
  328.  
  329.    IRectangle bounds = clientWindow->rect();
  330.    yIncr = bounds.height() / 21.0;
  331.    xIncr = bounds.width() / 21.0;
  332.  
  333.    GpiSetMix( hps, FM_XOR );
  334.  
  335.    if ( numLines >= targetLines ) {
  336.       lines.dequeue( line );
  337.       line.draw( hps );
  338.       numLines--;
  339.    } /* endif */
  340.  
  341.    if ( numLines >= targetLines ) {
  342.       client()->releasePresSpace( hps );
  343.       return true;
  344.    } /* endif */
  345.  
  346.    nStart = rand() * 12 / RAND_MAX + 1;
  347.    nEnd = rand() * (15 - nStart) / RAND_MAX + nStart + 6;
  348.    nOther = rand() * 18 /RAND_MAX + 2;
  349.    if( rand() * 2 / RAND_MAX == 0 )
  350.    {
  351.       xLine = xIncr * nStart;
  352.       yLine = yIncr * nOther;
  353.       start = IPoint( xIncr * nStart, yIncr * nOther );
  354.       end = IPoint( xIncr * nEnd, yIncr * nOther );
  355.    }
  356.    else
  357.    {
  358.       xLine = xIncr * nOther;
  359.       yLine = yIncr * nStart;
  360.       start = IPoint( xIncr * nOther, yIncr * nStart );
  361.       end = IPoint( xIncr * nOther, yIncr * nEnd );
  362.    }
  363.  
  364.    colour = colours[rand() * NUM_COLOURS / RAND_MAX];
  365.    line = lineSegment( start, end, colour );
  366.    line.draw( hps );
  367.    numLines++;
  368.    lines.enqueue( line );
  369.  
  370.    client()->releasePresSpace( hps );
  371.    return true;
  372. }  /* end MainWin::tick( ... ) */
  373.  
  374.  
  375. void main()
  376. {
  377.   MainWin mainWindow( WND_MAIN );
  378.   IApplication::current().run();
  379. } /* end main */
  380.  
  381.