home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pwrgu2.zip / POWERGU2.EXE / GENHDRS / MOUSECLK / MOUSECLK.CPP < prev    next >
Text File  |  1995-07-25  |  8KB  |  289 lines

  1. //************************************************************
  2. // Resuable Handlers - Mouse Handler Example
  3. //
  4. // Copyright (C) 1994, Law, Leong, Love, Olson, Tsuji.
  5. // All Rights Reserved.
  6. //************************************************************
  7. #include <icolor.hpp>
  8. #include <ientryfd.hpp>
  9. #include <ifont.hpp>
  10. #include <iframe.hpp>
  11. #include <iapp.hpp>
  12. #include <igroupbx.hpp>
  13. #include <ilistbox.hpp>
  14. #include <imchdr.hpp>
  15. #include <imcelcv.hpp>
  16. #include <ipoint.hpp>
  17. #include <iradiobt.hpp>
  18. #include <isetcv.hpp>
  19. #include <istattxt.hpp>
  20. #include <istring.hpp>
  21. #include <icconst.h>
  22.  
  23. #define ID_HEADING           1
  24. #define ID_LIST_BOX          2
  25. #define ID_CANVAS            3
  26. #define ID_STATIC_TEXT       4
  27. #define ID_RADIO_BUTTON      5
  28. #define ID_GROUP_BOX         6
  29. #define ID_ENTRY_FIELD       7
  30.  
  31. class MouseClickWindow : public IVBase {
  32. public:
  33.   MouseClickWindow ( );
  34. MouseClickWindow
  35.  &showClickInfo    ( const IWindow* eventWindow,
  36.                      const IWindowHandle& clicked,
  37.                      IString button,
  38.                      IString stateChange,
  39.                      const IPoint& mousePosition );
  40. protected:
  41. IString
  42.   nameForId        ( unsigned long id );
  43. private:
  44. IFrameWindow
  45.   frame;
  46. IMultiCellCanvas
  47.   clientWindow;
  48. IStaticText
  49.   heading;
  50. IListBox
  51.   listBox;
  52. ISetCanvas
  53.   canvas;
  54. IStaticText
  55.   staticText;
  56. IRadioButton
  57.   radioButton;
  58. IGroupBox
  59.   groupBox;
  60. IEntryField
  61.   entryField;
  62. unsigned long
  63.   clickEntries,
  64.   maxClickEntries;
  65. class MouseClickHandler : public IMouseClickHandler {
  66.   public:
  67.   MouseClickHandler ( MouseClickWindow* window )
  68.     : clickWindow( window )
  69.     { }
  70.   protected:
  71.   virtual Boolean
  72.     mouseClicked    ( IMouseClickEvent& event );
  73.   private:
  74.   IString
  75.     textForButton   ( IMouseClickEvent::MouseNumber button );
  76.   IString
  77.     textForAction   ( IMouseClickEvent::MouseAction action );
  78.   MouseClickWindow
  79.    *clickWindow;
  80.   };
  81. MouseClickHandler
  82.   mouseHdr;
  83. MouseClickWindow (const MouseClickWindow&);
  84. MouseClickWindow& operator=(const MouseClickWindow&);
  85. };
  86.  
  87. void main ( )
  88. {
  89.   MouseClickWindow window;
  90.   IApplication::current().run();
  91. }
  92.  
  93. MouseClickWindow :: MouseClickWindow ( )
  94.   : frame( "Mouse-Click Example" ),
  95.     clientWindow( IC_FRAME_CLIENT_ID, &frame, &frame ),
  96.     heading( ID_HEADING, &clientWindow, &clientWindow ),
  97.     listBox( ID_LIST_BOX, &clientWindow, &clientWindow ),
  98.     canvas( ID_CANVAS, &clientWindow, &clientWindow ),
  99.     staticText( ID_STATIC_TEXT, &canvas, &canvas ),
  100.     radioButton( ID_RADIO_BUTTON, &canvas, &canvas ),
  101.     groupBox( ID_GROUP_BOX, &canvas, &canvas ),
  102.     entryField( ID_ENTRY_FIELD, &canvas, &canvas ),
  103.     clickEntries( 0 ),
  104.     maxClickEntries( 100 ),
  105.     mouseHdr( this )
  106. {
  107.   heading
  108.    .setText( "      window()      handleOnTop()"
  109.                + IString( "  mouseNumber() mouseAction()" )
  110.                + IString( " mousePosition()" ));
  111.  
  112.   staticText
  113.    .setColor( IStaticText::foreground, IColor::red )
  114.    .setColor( IStaticText::background, IColor::yellow )
  115.    .setText( "Static Text" );
  116.  
  117.   radioButton
  118.    .setColor( IButton::foreground, IColor::darkGreen )
  119.    .setColor( IButton::background, IColor::yellow )
  120.    .setText( "Radio Button" );
  121.  
  122.   groupBox.setText( "Group Box  " );  // Adjust for calcMinimumSize.
  123.  
  124.   entryField
  125.    .setLimit( 16 )
  126.    .setText( "Entry Field" );
  127.  
  128.   clientWindow
  129.    .addToCell( &heading, 2, 2 )
  130.    .addToCell( &listBox, 2, 3 )
  131.    .addToCell( &canvas, 2, 5 )
  132.    .setColumnWidth( 2, 0, true )
  133.    .setColumnWidth( 3, IMultiCellCanvas::defaultCell().width() )
  134.    .setRowHeight( 3, 0, true )
  135.    .setRowHeight( 6, IMultiCellCanvas::defaultCell().height() );
  136.  
  137.   IFont fixedFont( "Courier", 8 );  // For alignment.
  138.   clientWindow.setFont( fixedFont );
  139.  
  140.   canvas.setColor( ISetCanvas::background, IColor::green );
  141.  
  142.   mouseHdr              // Attach to all windows.
  143.    .handleEventsFor( &frame )
  144.    .handleEventsFor( &clientWindow )
  145.    .handleEventsFor( &heading )
  146.    .handleEventsFor( &listBox )
  147.    .handleEventsFor( &canvas )
  148.    .handleEventsFor( &staticText )
  149.    .handleEventsFor( &radioButton )
  150.    .handleEventsFor( &groupBox )
  151.    .handleEventsFor( &entryField );
  152.  
  153.   frame
  154.    .setClient( &clientWindow )
  155.    .setFocus()
  156.    .show();
  157. }
  158.  
  159. MouseClickWindow&
  160.   MouseClickWindow :: showClickInfo ( const IWindow* eventWindow,
  161.                                       const IWindowHandle& clicked,
  162.                                       IString button,
  163.                                       IString stateChange,
  164.                                       const IPoint& mousePosition )
  165. {
  166.   if (listBox.count() > maxClickEntries)
  167.   {                     // Don't let the list box grow too big.
  168.      listBox.remove( 0 );  // Remove top entry.
  169.   }
  170.  
  171.   IString handlerWindow = nameForId( eventWindow->id() );
  172.   IWindow* window = IWindow::windowWithHandle( clicked );
  173.   IString clickedWindow =
  174.              nameForId( window ? window->id() : 0 );
  175.   IString match( (eventWindow == window) ? "*" : " " );
  176.  
  177.   listBox.addAsLast( IString( ++clickEntries ) + ": "
  178.                        + match + handlerWindow + "  "
  179.                        + clickedWindow + "  "
  180.                        + button + "   "
  181.                        + stateChange + "  "
  182.                        + mousePosition.asString() );
  183.   listBox.setTop( listBox.count() );
  184.  
  185.   return *this;
  186. }
  187.  
  188. IString MouseClickWindow :: nameForId ( unsigned long id )
  189. {
  190.   IString name;
  191.   switch (id)
  192.   {
  193.      case IC_DEFAULT_FRAME_ID:
  194.         name = "Frame window ";
  195.         break;
  196.      case IC_FRAME_CLIENT_ID:
  197.         name = "Client window";
  198.         break;
  199.      case ID_HEADING:
  200.         name = "Heading text ";
  201.         break;
  202.      case ID_LIST_BOX:
  203.         name = "List Box     ";
  204.         break;
  205.      case ID_CANVAS:
  206.         name = "Green canvas ";
  207.         break;
  208.      case ID_STATIC_TEXT:
  209.         name = "Static Text  ";
  210.         break;
  211.      case ID_RADIO_BUTTON:
  212.         name = "Radio Button ";
  213.         break;
  214.      case ID_GROUP_BOX:
  215.         name = "Group Box    ";
  216.         break;
  217.      case ID_ENTRY_FIELD:
  218.         name = "Entry Field  ";
  219.         break;
  220.      default:
  221.         name = "Other        ";
  222.         break;
  223.   } /* endswitch */
  224.  
  225.   return name;
  226. }
  227.  
  228. Boolean MouseClickWindow::MouseClickHandler :: mouseClicked
  229.                                  ( IMouseClickEvent& event )
  230. {
  231.   IString button = this->textForButton( event.mouseNumber() );
  232.   IString action = this->textForAction( event.mouseAction() );
  233.   clickWindow->showClickInfo( event.window(),
  234.                               event.handleOnTop(),
  235.                               button, action,
  236.                               event.mousePosition() );
  237.   return false;         // Allow someone else to also process.
  238. }
  239.  
  240. IString MouseClickWindow::MouseClickHandler :: textForButton
  241.                         ( IMouseClickEvent::MouseNumber button )
  242. {
  243.   IString buttonText;
  244.   switch (button)
  245.   {
  246.      case (IMouseClickEvent::button1):
  247.         buttonText = "Button 1   ";
  248.         break;
  249.      case (IMouseClickEvent::button2):
  250.         buttonText = "Button 2   ";
  251.         break;
  252.      case (IMouseClickEvent::button3):
  253.         buttonText = "Button 3   ";
  254.         break;
  255.      case (IMouseClickEvent::button12):
  256.         buttonText = "Buttons 1&2";
  257.         break;
  258.      default:
  259.         buttonText = "Unknown    ";
  260.         break;
  261.   }
  262.   return buttonText;
  263. }
  264.  
  265. IString MouseClickWindow::MouseClickHandler :: textForAction
  266.                         ( IMouseClickEvent::MouseAction action )
  267. {
  268.   IString actionText;
  269.   switch (action)
  270.   {
  271.      case (IMouseClickEvent::click):
  272.         actionText = "Single click";
  273.         break;
  274.      case (IMouseClickEvent::doubleClick):
  275.         actionText = "Double click";
  276.         break;
  277.      case (IMouseClickEvent::down):
  278.         actionText = "Down        ";
  279.         break;
  280.      case (IMouseClickEvent::up):
  281.         actionText = "Up          ";
  282.         break;
  283.      default:
  284.         actionText = "Unknown     ";
  285.         break;
  286.   }
  287.   return actionText;
  288. }
  289.