home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / VSCPPv8.zip / VACPP / IBMCPP / samples / COMPILER / UILINES / PMLINES.CPP < prev    next >
Text File  |  1995-06-01  |  15KB  |  381 lines

  1. /*+--------------------------------------------------------------------------+*/
  2. /*|                                                                          |*/
  3. /*| PROGRAM NAME: PMLINES                                                    |*/
  4. /*| -------------                                                            |*/
  5. /*|  A Simple OS/2 Presentation Manager Graphics Demonstration Program       |*/
  6. /*|                                                                          |*/
  7. /*| COPYRIGHT:                                                               |*/
  8. /*| ----------                                                               |*/
  9. /*|  Copyright (C) International Business Machines Corp., 1992, 1993.        |*/
  10. /*|                                                                          |*/
  11. /*| DISCLAIMER OF WARRANTIES:                                                |*/
  12. /*| -------------------------                                                |*/
  13. /*|  The following [enclosed] code is sample code created by IBM Corporation.|*/
  14. /*|  This sample code is not part of any standard IBM product and is provided|*/
  15. /*|  to you solely for the purpose of assisting you in the development of    |*/
  16. /*|  your applications.  The code is provided "AS IS", without warranty of   |*/
  17. /*|  any kind.  IBM shall not be liable for any damages arising out of your  |*/
  18. /*|  use of the sample code, even if they have been advised of the           |*/
  19. /*|  possibility of such damages.                                            |*/
  20. /*|                                                                          |*/
  21. /*| REVISION LEVEL: 1.1                                                      |*/
  22. /*|                                                                          |*/
  23. /*| CHANGE HISTORY:                                                          |*/
  24. /*| Rel Programmer        Stamp Date     Description                         |*/
  25. /*| --- ----------------- ----- -------- ------------------------------------|*/
  26. /*| 1.0 Noel Sales        njCs  12/16/92 Port the PMLINES program to code it |*/
  27. /*|                                      in C++ and to implement the PM      |*/
  28. /*|                                      interface using the User Interface  |*/
  29. /*|                                      Class Library. The original PMLINES |*/
  30. /*|                                      program is coded in C and uses the  |*/
  31. /*|                                      PM API directly.                    |*/
  32. /*| 1.1 Bill Sarantakos   bjs   09/05/95 Minor fixes                         |*/
  33. /*|                                                                          |*/
  34. /*+--------------------------------------------------------------------------+*/
  35.  
  36. /*+--------------------------------------------------------------------------+*/
  37. /*|                                                                          |*/
  38. /*| The following features of the User Interface Class Library are           |*/
  39. /*| demonstrated by this program:                                            |*/
  40. /*|  1. Window Creation.                                                     |*/
  41. /*|  2. Event Handling                                                       |*/
  42. /*|  3. Simple multitasking                                                  |*/
  43. /*|  4. Use of GPI or native PM API with User Interface Library objects      |*/
  44. /*|                                                                          |*/
  45. /*+--------------------------------------------------------------------------+*/
  46.  
  47. /*+--------------------------------------------------------------------------+*/
  48. /*| The following includes are for PM functions and macros                   |*/
  49. /*+--------------------------------------------------------------------------+*/
  50. #define INCL_NOCOMMON
  51. #define INCL_GPIPRIMITIVES
  52. #define INCL_WINMESSAGEMGR
  53. #define INCL_WINWINDOWMGR
  54. #include <os2.h>
  55.  
  56. /*+--------------------------------------------------------------------------+*/
  57. /*| Include headers for the User Interface Class Library                     |*/
  58. /*+--------------------------------------------------------------------------+*/
  59. #include <iapp.hpp>           // IApplication
  60. #include <ireslib.hpp>        // IResourceLibrary/IResourceId
  61. #include <itrace.hpp>         // ITrace
  62. #include <imsgbox.hpp>
  63.  
  64. /*+--------------------------------------------------------------------------+*/
  65. /*| include our class declarations                                           |*/
  66. /*+--------------------------------------------------------------------------+*/
  67. #include "pmlines.hpp"
  68.  
  69. /*+--------------------------------------------------------------------------+*/
  70. /*| include our symbols definitions                                          |*/
  71. /*+--------------------------------------------------------------------------+*/
  72. #include "pmlines.h"
  73.  
  74. /*+--------------------------------------------------------------------------+*/
  75. /*| main                                                                     |*/
  76. /*|  Application entry point                                                 |*/
  77. /*+--------------------------------------------------------------------------+*/
  78. int main()
  79. {
  80.    MyWindow myWin(ID_WINDOW);     /* Create the Main Window    */
  81.  
  82.    /* Display the application window */
  83.    myWin.setFocus().show();
  84.  
  85.    IApplication::current().run(); /* Run the application       */
  86.    return 0;
  87. }
  88.  
  89. /*+--------------------------------------------------------------------------+*/
  90. /*| MyWindow::MyWindow                                              nCs 16/12|*/
  91. /*|   Constructor for our main window                                        |*/
  92. /*+--------------------------------------------------------------------------+*/
  93. MyWindow::MyWindow( unsigned long windowId )
  94.          :IFrameWindow( classDefaultStyle | accelerator,
  95.                         windowId ),
  96.           menubar( windowId, this ),
  97.           myClient( ID_CLIENT, this, this )
  98. {
  99.    /* Set ourselves in the event handler stack. */
  100.    ICommandHandler::handleEventsFor( this );
  101.    IPaintHandler::handleEventsFor( &myClient );
  102.    IMouseClickHandler::handleEventsFor( &myClient );
  103.    IFrameHandler::handleEventsFor( this );
  104.  
  105.    /* Set the client to the frame window */
  106.    setClient( &myClient );
  107.  
  108.    /* Compute the initial Client Area dimensions */
  109.    long   cx, cy, bx, by, ex, ey;
  110.    ISize  ClientSize = myClient.size();
  111.  
  112.    myClient.SetcxClient( cx = ClientSize.width() );
  113.    myClient.SetcyClient( cy = ClientSize.height() );
  114.    bx = cx / 2;
  115.    by = cy / 2;
  116.    myClient.SetBeginPoint( IPoint( bx, by ) );
  117.    ex = bx / 2;
  118.    ey = by / 2;
  119.    myClient.SetEndPoint( IPoint( ex, ey ) );
  120.  
  121.    /* Dispatch the DrawLines function on a separate thread. */
  122.    /* tell library to initialize thread when created.       */
  123.    thread.start( new IThreadMemberFn<MyClientWindow>( myClient,
  124.                      MyClientWindow::DrawLines), true  );
  125.  }
  126.  
  127. IBase::Boolean MyWindow::closed( IFrameEvent &event )
  128.  
  129.   {
  130.   myClient.SetThreadMessage( WM_USER_END_THREAD );
  131.   IThread::current().waitFor( thread );
  132.   return false;
  133.   }
  134.  
  135. /*+--------------------------------------------------------------------------+*/
  136. /*| MyWindow :: mouseClicked                                        nCs 16/12|*/
  137. /*|   Handle mouse Clicked Event                                             |*/
  138. /*+--------------------------------------------------------------------------+*/
  139. IBase::Boolean MyWindow::mouseClicked( IMouseClickEvent &event )
  140. {
  141.    if ( IMouseClickEvent::button1 == event.mouseNumber() &&
  142.         IMouseClickEvent::click   == event.mouseAction() )
  143.    {
  144.       /* Change the foreground color */
  145.       setFocus();
  146.       myClient.IncColorCount();
  147.  
  148.       long Fc = myClient.GetFcolor();
  149.       Fc++;
  150.       myClient.SetFcolor( Fc );
  151.  
  152.       if ( myClient.GetFcolor() == myClient.GetBcolor() )
  153.       {
  154.          Fc++;
  155.          myClient.SetFcolor( Fc );
  156.       }
  157.       if ( myClient.GetFcolor() >= CLR_PALEGRAY )
  158.          myClient.SetFcolor( CLR_BLUE );
  159.  
  160.       myClient.SetThreadMessage( WM_USER_REPAINT );
  161.       return true;
  162.    }
  163.  
  164.    else if ( IMouseClickEvent::button1     == event.mouseNumber() &&
  165.              IMouseClickEvent::doubleClick == event.mouseAction() )
  166.    {
  167.       /* Change the background color */
  168.       setFocus();
  169.  
  170.       if ( myClient.GetBcolor() == CLR_PALEGRAY )
  171.          myClient.SetBcolor( CLR_BLUE );
  172.       else
  173.       {
  174.          long Bc = myClient.GetBcolor();
  175.          Bc++;
  176.          myClient.SetBcolor(Bc);
  177.       }
  178.       if ( myClient.GetFcolor() == myClient.GetBcolor() )
  179.          myClient.SetFcolor( CLR_BLUE );
  180.  
  181.       myClient.SetThreadMessage(WM_USER_REPAINT);
  182.       return true;
  183.    }
  184.  
  185.    else if ( IMouseClickEvent::button2 == event.mouseNumber() &&
  186.              IMouseClickEvent::click   == event.mouseAction() )
  187.    {
  188.       /* Reset the drawing cycle */
  189.       myClient.SetThreadMessage( WM_USER_REPAINT );
  190.       return true;
  191.    }
  192.    else
  193.       return false;
  194. }
  195.  
  196. /*+--------------------------------------------------------------------------+*/
  197. /*| MyWindow :: paintWindow                                         nCs 16/12|*/
  198. /*|   Handle paint Event                                                     |*/
  199. /*+--------------------------------------------------------------------------+*/
  200. IBase::Boolean MyWindow::paintWindow( IPaintEvent& event )
  201. {
  202.  
  203.    IPresSpaceHandle
  204.       hps = event.presSpaceHandle();
  205.    long
  206.       cx, cy, bx, by, ex, ey;
  207.    ISize
  208.       ClientSize = myClient.size();
  209.  
  210.    myClient.SetcxClient( cx = ClientSize.width() );
  211.    myClient.SetcyClient( cy = ClientSize.height() );
  212.    bx = cx / 2;
  213.    by = cy / 2;
  214.    myClient.SetBeginPoint( IPoint( bx,by ) );
  215.    ex = bx / 2;
  216.    ey = by / 2;
  217.    myClient.SetEndPoint( IPoint( ex, ey ) );
  218.  
  219.    myClient.SetThreadMessage( WM_USER_REPAINT );
  220.    return false;
  221. }
  222.  
  223. /*+--------------------------------------------------------------------------+*/
  224. /*| MyWindow :: command                                             nCs 16/12|*/
  225. /*|   Handle command event                                                   |*/
  226. /*+--------------------------------------------------------------------------+*/
  227. IBase::Boolean MyWindow::command( ICommandEvent& cmdevt )
  228. {
  229.    switch( cmdevt.commandId() )
  230.    {
  231.       case IDM_EXITPROG:
  232.       {
  233.          myClient.SetThreadMessage( WM_USER_END_THREAD );
  234.          IThread::current().waitFor( thread );
  235.          postEvent( WM_CLOSE, 0, 0 );
  236.          return true;
  237.       }
  238.  
  239.       case IDM_RESUME:
  240.          return true;
  241.  
  242.       case IDM_HELPINSTRUCTIONS:
  243.       {
  244.          IMessageBox
  245.             Mbox( this );
  246.          Mbox.setTitle( "UILINES" );
  247.          Mbox.show( INSTRUCTIONS, IMessageBox::information );
  248.          return true;
  249.       }
  250.  
  251.       case IDM_HELPABOUT:
  252.       {
  253.          IMessageBox
  254.             Mbox( this );
  255.          Mbox.setTitle( "UILINES" );
  256.          Mbox.show( ABOUT, IMessageBox::information );
  257.          return true;
  258.       }
  259.    }
  260.    return false;
  261. }
  262.  
  263. /*+--------------------------------------------------------------------------+*/
  264. /*| Define MyClientWindow Methods                                            |*/
  265. /*+--------------------------------------------------------------------------+*/
  266. MyClientWindow::MyClientWindow(unsigned long Id,
  267.                                IWindow* parent,
  268.                                IWindow* owner)
  269.               : ICanvas(Id, parent, owner),
  270.                 MyParent(parent),
  271.                 lFcolour(CLR_BLUE),
  272.                 lBcolour(CLR_DARKGRAY),
  273.                 cxClient(0),cyClient(0),
  274.                 fcolourcounter(0l)
  275. {
  276.    ptl1.x = ptl1.y = 0;
  277.    ptl2.x = ptl2.y = 0;
  278.    SetThreadMessage(WM_USER_REPAINT);
  279. }
  280.  
  281. /*+--------------------------------------------------------------------------+*/
  282. /*| DrawLines                                                                |*/
  283. /*+--------------------------------------------------------------------------+*/
  284.  
  285. void MyClientWindow::DrawLines( )
  286. {
  287.    /* Declare local variables.                                                */
  288.    LONG  delta1x = START_DELTA_X;           /* start point delta values       */
  289.    LONG  delta1y = START_DELTA_Y;
  290.    LONG  delta2x = END_DELTA_X;             /* end point delta values         */
  291.    LONG  delta2y = END_DELTA_Y;
  292.  
  293.    /* Change the priority class of this thread to idle class.  */
  294.    IThread::current().setPriority( IApplication::idleTime, 0 );
  295.  
  296.    /* Get a presentation space. */
  297.    hps = presSpace();
  298.  
  299.    /* Run drawing loop */
  300.    while ( GetThreadMessage() != WM_USER_END_THREAD )
  301.    {
  302.       if ( GetThreadMessage() == WM_USER_REPAINT )
  303.       {
  304.           rclClient = IRectangle(IPoint(0,0), size()).asRECTL();
  305.           WinFillRect( hps, &rclClient, GetBcolor() );
  306.           SetThreadMessage(WM_USER_REPAINT+1);
  307.       }
  308.       else
  309.       {
  310.          /* Increment the foreground colour if necessary. */
  311.          IncColorCount();
  312.          long ColorCount = GetColorCount();
  313.          if ( ColorCount > ((cxClient + cyClient ) >> 1 ))
  314.          {
  315.             SetColorCount(1);     /* Reset fcolourcounter and  */
  316.             long Fc = GetFcolor(); /* increment foreground color*/
  317.             Fc++;
  318.             SetFcolor(Fc);
  319.  
  320.             /* Foreground and background colors must differ */
  321.  
  322.             if (GetFcolor() == GetBcolor())
  323.             {
  324.                Fc++;
  325.                SetFcolor(Fc);
  326.             }
  327.  
  328.             /* Make sure the foregound colour does not exceed CLR_PALEGRAY. */
  329.  
  330.             if (GetFcolor() >= CLR_PALEGRAY )
  331.                SetFcolor(CLR_BLUE);
  332.          }
  333.  
  334.          /* Add deltas to the start and end points for a line.    */
  335.          /* If the start point would be invalid, negate the delta.*/
  336.  
  337.          if ( ( ptl1.x + delta1x ) > cxClient )
  338.             delta1x = -delta1x;
  339.  
  340.          if ( ( ptl1.x + delta1x ) < 1 )
  341.             delta1x = -delta1x;
  342.  
  343.          if ( ( ptl1.y + delta1y ) > cyClient )
  344.             delta1y = -delta1y;
  345.  
  346.          if ( ( ptl1.y + delta1y ) < 1 )
  347.             delta1y = -delta1y;
  348.  
  349.          /* Add delta to start point. */
  350.  
  351.          ptl1.x += delta1x;
  352.          ptl1.y += delta1y;
  353.  
  354.          /* If the end point would be invalid, negate the delta. */
  355.  
  356.          if ( ( ptl2.x + delta2x ) > cxClient )
  357.             delta2x = -delta2x;
  358.          if ( ( ptl2.x + delta2x ) < 1 )
  359.             delta2x = -delta2x;
  360.          if ( ( ptl2.y + delta2y ) > cyClient )
  361.             delta2y = -delta2y;
  362.          if ( ( ptl2.y + delta2y ) < 1 )
  363.             delta2y = -delta2y;
  364.  
  365.          /* Add delta to end point.*/
  366.  
  367.          ptl2.x += delta2x;
  368.          ptl2.y += delta2y;
  369.  
  370.          /* Now draw the line.*/
  371.  
  372.          GpiSetColor( hps, GetFcolor());
  373.          GpiMove( hps, &ptl1 );         /* Move to start point */
  374.          GpiLine( hps, &ptl2 );         /* Draw new line       */
  375.       }
  376.    }
  377.  
  378.    /* Clean up and terminate drawing thread.*/
  379.    releasePresSpace( hps );
  380. }
  381.