home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cset21v1.zip / IBMCPP / WKFRAME / UILINES / PMLINES.CPP next >
Text File  |  1993-03-30  |  15KB  |  398 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.0                                                      |*/
  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. /*|                                                                          |*/
  33. /*+--------------------------------------------------------------------------+*/
  34.  
  35. /*+--------------------------------------------------------------------------+*/
  36. /*|                                                                          |*/
  37. /*| The following features of the User Interface Class Library are           |*/
  38. /*| demonstrated by this program:                                            |*/
  39. /*|  1. Window Creation.                                                     |*/
  40. /*|  2. Event Handling                                                       |*/
  41. /*|  3. Simple multitasking                                                  |*/
  42. /*|  4. Use of GPI or native PM API with User Interface Library objects      |*/
  43. /*|                                                                          |*/
  44. /*+--------------------------------------------------------------------------+*/
  45.  
  46. /*+--------------------------------------------------------------------------+*/
  47. /*| The following includes are for thread handling only.                     |*/
  48. /*+--------------------------------------------------------------------------+*/
  49. #define  INCL_DOSPROCESS
  50. #define  INCL_DOSSEMAPHORES
  51. #define  INCL_DOSERRORS
  52. #include <os2.h>
  53.  
  54. /*+--------------------------------------------------------------------------+*/
  55. /*| Include headers for the User Interface Class Library                     |*/
  56. /*+--------------------------------------------------------------------------+*/
  57. #include <iapp.hpp>           // IApplication
  58. #include <ireslib.hpp>        // IResourceLibrary/IResourceId
  59. #include <itrace.hpp>         // ITrace
  60. #include <imsgbox.hpp>
  61.  
  62. /*+--------------------------------------------------------------------------+*/
  63. /*| include our class declarations                                           |*/
  64. /*+--------------------------------------------------------------------------+*/
  65. #include "pmlines.hpp"
  66.  
  67. /*+--------------------------------------------------------------------------+*/
  68. /*| include our symbols definitions                                          |*/
  69. /*+--------------------------------------------------------------------------+*/
  70. #include "pmlines.h"
  71.  
  72. /*+--------------------------------------------------------------------------+*/
  73. /*| main                                                                     |*/
  74. /*|  Application entry point                                                 |*/
  75. /*+--------------------------------------------------------------------------+*/
  76. void main()
  77. {
  78.    IApplication app();
  79.    MyWindow myWin(ID_WINDOW);     /* Create the Main Window    */
  80.  
  81.    IApplication::current().run(); /* Run the application       */
  82.  
  83. }/* end main */
  84.  
  85. /*+--------------------------------------------------------------------------+*/
  86. /*| MyWindow::MyWindow                                              nCs 16/12|*/
  87. /*|   Constructor for our main window                                        |*/
  88. /*+--------------------------------------------------------------------------+*/
  89. MyWindow::MyWindow( unsigned long windowId )
  90.          :IFrameWindow( animated | border | sizingBorder | windowList |
  91.                         IFrameWindow::accelerator |
  92.                         classDefaultStyle,
  93.                         windowId )
  94. {
  95.    /* Set ourselves in the event handler stack. */
  96.  
  97.    addHandler((ICommandHandler *)this);
  98.    addHandler((IPaintHandler *)this);
  99.    addHandler((IMouseClickHandler *)this);
  100.  
  101.    /* Set the main menu */
  102.  
  103.    IMenuBar* pabmnMain=new IMenuBar(ID_WINDOW,this);
  104.  
  105.    /* Instantiate the Client */
  106.  
  107.    pMyClient = new MyClientWindow(ID_CLIENT,this,this);
  108.    setClient(pMyClient);
  109.  
  110.    /* Display the application window */
  111.  
  112.    setFocus();
  113.    show();
  114.  
  115.    /* Compute the initial Client Area dimensions */
  116.  
  117.    long   cx, cy, bx, by, ex, ey;
  118.    ISize  ClientSize = pMyClient->size();
  119.  
  120.    pMyClient->SetcxClient(cx= ClientSize.width());
  121.    pMyClient->SetcyClient(cy= ClientSize.height());
  122.    bx= cx / 2;
  123.    by= cy / 2;
  124.    pMyClient->SetBeginPoint(IPoint(bx,by));
  125.    ex= bx / 2;
  126.    ey= ex / 2;
  127.    pMyClient->SetEndPoint(IPoint(ex,ey));
  128.  
  129.    /* Dispatch the DrawLines function on a separate thread. */
  130.  
  131.    Thread.start(new IThreadMemberFn<MyClientWindow> (*pMyClient,
  132.                     MyClientWindow::DrawLines));
  133.  
  134. }/* end MyWindow :: MyWindow(...) */
  135.  
  136. /*+--------------------------------------------------------------------------+*/
  137. /*| MyWindow :: mouseClicked                                        nCs 16/12|*/
  138. /*|   Handle mouse Clicked Event                                             |*/
  139. /*+--------------------------------------------------------------------------+*/
  140. Boolean MyWindow :: mouseClicked(IMouseClickEvent &evt)
  141. {
  142.    if (IMouseClickEvent::button1 == evt.mouseNumber() &&
  143.        IMouseClickEvent::click   == evt.mouseAction())
  144.    {
  145.       /* Change the foreground color */
  146.  
  147.       setFocus();
  148.       pMyClient->IncColorCount();
  149.  
  150.       long Fc= pMyClient->GetFcolor();
  151.       Fc++;
  152.       pMyClient->SetFcolor(Fc);
  153.  
  154.       if (pMyClient->GetFcolor() == pMyClient->GetBcolor())
  155.       {
  156.          Fc++;
  157.          pMyClient->SetFcolor(Fc);
  158.       }
  159.       if (pMyClient->GetFcolor() >= CLR_PALEGRAY)
  160.          pMyClient->SetFcolor(CLR_BLUE);
  161.  
  162.       pMyClient->SetThreadMessage(WM_USER_REPAINT);
  163.       return true;
  164.    }
  165.  
  166.    else if (IMouseClickEvent::button1     == evt.mouseNumber() &&
  167.             IMouseClickEvent::doubleClick == evt.mouseAction())
  168.    {
  169.       /* Change the background color */
  170.  
  171.       setFocus();
  172.  
  173.       if (pMyClient->GetBcolor() == CLR_PALEGRAY)
  174.          pMyClient->SetBcolor(CLR_BLUE);
  175.       else
  176.       {
  177.          long Bc= pMyClient->GetBcolor();
  178.          Bc++;
  179.          pMyClient->SetBcolor(Bc);
  180.       }
  181.       if (pMyClient->GetFcolor() == pMyClient->GetBcolor())
  182.          pMyClient->SetFcolor(CLR_BLUE);
  183.  
  184.       pMyClient->SetThreadMessage(WM_USER_REPAINT);
  185.       return true;
  186.    }
  187.  
  188.    else if (IMouseClickEvent::button2 == evt.mouseNumber() &&
  189.             IMouseClickEvent::click   == evt.mouseAction())
  190.    {
  191.       /* Reset the drawing cycle */
  192.  
  193.       pMyClient->SetThreadMessage(WM_USER_REPAINT);
  194.       return true;
  195.    }
  196.    else
  197.       return false;
  198. }
  199.  
  200. /*+--------------------------------------------------------------------------+*/
  201. /*| MyWindow :: paintWindow                                         nCs 16/12|*/
  202. /*|   Handle paint Event                                                     |*/
  203. /*+--------------------------------------------------------------------------+*/
  204. Boolean MyWindow :: paintWindow(IPaintEvent& evt)
  205. {
  206.  
  207.    IPresSpaceHandle   hps= presSpace();
  208.    long   cx, cy, bx, by, ex, ey;
  209.    ISize  ClientSize= pMyClient->size();
  210.  
  211.    pMyClient->SetcxClient(cx= ClientSize.width());
  212.    pMyClient->SetcyClient(cy= ClientSize.height());
  213.    bx= cx / 2;
  214.    by= cy / 2;
  215.    pMyClient->SetBeginPoint(IPoint(bx,by));
  216.    ex= bx / 2;
  217.    ey= ex / 2;
  218.    pMyClient->SetEndPoint(IPoint(ex,ey));
  219.  
  220.    releasePresSpace(hps);
  221.  
  222.    pMyClient->SetThreadMessage(WM_USER_REPAINT);
  223.    return true;
  224. }
  225.  
  226. /*+--------------------------------------------------------------------------+*/
  227. /*| MyWindow :: command                                             nCs 16/12|*/
  228. /*|   Handle command event                                                   |*/
  229. /*+--------------------------------------------------------------------------+*/
  230. Boolean MyWindow :: command(ICommandEvent& cmdevt)
  231. {
  232.   switch(cmdevt.commandId())
  233.   {
  234.      case IDM_EXITPROG:
  235.      {
  236.         pMyClient->SetThreadMessage(WM_USER_END_THREAD);
  237.         IThreadId   Tid= Thread.id();
  238.         DosWaitThread((PTID)&Tid, DCWW_WAIT);
  239.         WinPostMsg(handle(), WM_CLOSE, 0L, 0L);
  240.         return true;
  241.      }
  242.  
  243.      case IDM_RESUME:
  244.         return true;
  245.  
  246.      case IDM_HELPINSTRUCTIONS:
  247.      {
  248.         IMessageBox   *Mbox= new IMessageBox(this);
  249.         Mbox->setTitle("PMLINES");
  250.         Mbox->show(INSTRUCTIONS, IMessageBox::information);
  251.         delete Mbox;
  252.         return true;
  253.      }
  254.  
  255.      case IDM_HELPABOUT:
  256.      {
  257.         IMessageBox   *Mbox= new IMessageBox(this);
  258.         Mbox->setTitle("PMLINES");
  259.         Mbox->show(ABOUT,IMessageBox::information);
  260.         delete Mbox;
  261.         return true;
  262.      }
  263.   }
  264.   return false;
  265. }
  266.  
  267. /*+--------------------------------------------------------------------------+*/
  268. /*| Define MyClientWindow Methods                                            |*/
  269. /*+--------------------------------------------------------------------------+*/
  270. MyClientWindow::MyClientWindow(unsigned long Id,
  271.                                IWindow* parent,
  272.                                IWindow* owner)
  273.               : ICanvas(Id, parent, owner),
  274.                 MyParent(parent),
  275.                 lFcolour(CLR_BLUE),
  276.                 lBcolour(CLR_DARKGRAY),
  277.                 cxClient(0),cyClient(0),
  278.                 fcolourcounter(0l)
  279. {
  280.    ptl1.x= ptl1.y= 0;
  281.    ptl2.x= ptl2.y= 0;
  282.    SetThreadMessage(WM_USER_REPAINT);
  283. }
  284.  
  285. /*+--------------------------------------------------------------------------+*/
  286. /*| DrawLines                                                                |*/
  287. /*+--------------------------------------------------------------------------+*/
  288.  
  289. void MyClientWindow::DrawLines( )
  290. {
  291.    /* Declare local variables.                                                */
  292.  
  293.    LONG  delta1x = START_DELTA_X;           /* start point delta values       */
  294.    LONG  delta1y = START_DELTA_Y;
  295.    LONG  delta2x = END_DELTA_X;             /* end point delta values         */
  296.    LONG  delta2y = END_DELTA_Y;
  297.  
  298.    /* Change the priority class of this thread to idle class.  */
  299.  
  300.    DosSetPriority( PRTYS_THREAD, PRTYC_IDLETIME, 0L, 0UL );
  301.  
  302.    /* Initialize thread to PM. */
  303.  
  304.    habThread2 = WinInitialize( 0UL );
  305.  
  306.    /* Get a presentation space. */
  307.  
  308.    hps= MyParent->presSpace();
  309.  
  310.    /* Run drawing loop */
  311.  
  312.    while ( GetThreadMessage() != WM_USER_END_THREAD )
  313.    {
  314.       if ( GetThreadMessage() == WM_USER_REPAINT )
  315.       {
  316.           rclClient= rect().asRECTL();
  317.           WinFillRect( hps, &rclClient, GetBcolor() );
  318.           SetThreadMessage(WM_USER_REPAINT+1);
  319.       }
  320.  
  321.       else
  322.       {
  323.          /* Increment the foreground colour if necessary. */
  324.  
  325.          IncColorCount();
  326.          long   ColorCount= GetColorCount();
  327.          if ( ColorCount > ((cxClient + cyClient ) >> 1 ))
  328.          {
  329.             SetColorCount(1);     /* Reset fcolourcounter and  */
  330.             long Fc= GetFcolor(); /* increment foreground color*/
  331.             Fc++;
  332.             SetFcolor(Fc);
  333.  
  334.             /* Foreground and background colors must differ */
  335.  
  336.             if (GetFcolor() == GetBcolor())
  337.             {
  338.                Fc++;
  339.                SetFcolor(Fc);
  340.             }
  341.  
  342.             /* Make sure the foregound colour does not exceed CLR_PALEGRAY. */
  343.  
  344.             if (GetFcolor() >= CLR_PALEGRAY )
  345.                SetFcolor(CLR_BLUE);
  346.          }
  347.  
  348.          /* Add deltas to the start and end points for a line.    */
  349.          /* If the start point would be invalid, negate the delta.*/
  350.  
  351.          if ( ( ptl1.x + delta1x ) > cxClient )
  352.             delta1x = -delta1x;
  353.  
  354.          if ( ( ptl1.x + delta1x ) < 1 )
  355.             delta1x = -delta1x;
  356.  
  357.          if ( ( ptl1.y + delta1y ) > cyClient )
  358.             delta1y = -delta1y;
  359.  
  360.          if ( ( ptl1.y + delta1y ) < 1 )
  361.             delta1y = -delta1y;
  362.  
  363.          /* Add delta to start point. */
  364.  
  365.          ptl1.x += delta1x;
  366.          ptl1.y += delta1y;
  367.  
  368.          /* If the end point would be invalid, negate the delta. */
  369.  
  370.          if ( ( ptl2.x + delta2x ) > cxClient )
  371.             delta2x = -delta2x;
  372.          if ( ( ptl2.x + delta2x ) < 1 )
  373.             delta2x = -delta2x;
  374.          if ( ( ptl2.y + delta2y ) > cyClient )
  375.             delta2y = -delta2y;
  376.          if ( ( ptl2.y + delta2y ) < 1 )
  377.             delta2y = -delta2y;
  378.  
  379.          /* Add delta to end point.*/
  380.  
  381.          ptl2.x += delta2x;
  382.          ptl2.y += delta2y;
  383.  
  384.          /* Now draw the line.*/
  385.  
  386.          GpiSetColor( hps, GetFcolor());
  387.          GpiMove( hps, &ptl1 );         /* Move to start point */
  388.          GpiLine( hps, &ptl2 );         /* Draw new line       */
  389.       }
  390.    }
  391.  
  392.    /* Clean up and terminate drawing thread.*/
  393.  
  394.    MyParent->releasePresSpace( hps );
  395.    WinTerminate( habThread2 );
  396.    DosExit( EXIT_THREAD, 0UL );
  397. }
  398.