home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / powergui / genhdrs / enablcls / enablcls.cpp next >
Text File  |  1996-10-29  |  5KB  |  168 lines

  1. //*********************************************************
  2. // Reusable Commands - Disable the Close Command
  3. //
  4. // Copyright (C) 1994, Law, Leong, Love, Olson, Tsuji.
  5. // Copyright (c) 1997 John Wiley & Sons, Inc. 
  6. // All Rights Reserved.
  7. //*********************************************************
  8. #include <iapp.hpp>
  9. #include <icmdhdr.hpp>
  10. #include <iframe.hpp>
  11. #include <imnitem.hpp>
  12. #include <imsgbox.hpp>
  13. #include <ipushbut.hpp>
  14. #include <isetcv.hpp>
  15. #include <istattxt.hpp>
  16. #include <isysmenu.hpp>
  17. #include <icconst.h>
  18.  
  19. #ifdef IC_PM
  20.   // Define the system command identifier not originally
  21.   // included in VisualAge for C++ for OS/2, V3.0.
  22.   #ifndef IC_ID_CLOSE
  23.     #define IC_ID_CLOSE  0x8004
  24.   #endif
  25. #endif
  26.  
  27. #define ID_TOGGLE_CLOSE  100
  28.  
  29. class CloseTestWindow : public IFrameWindow {
  30. public:
  31.   CloseTestWindow ( );
  32. protected:
  33. Boolean
  34.   processCommand ( ICommandEvent& event );
  35. private:
  36.   CloseTestWindow ( const CloseTestWindow& );
  37. CloseTestWindow
  38.  &operator=       ( const CloseTestWindow& );
  39. IStaticText
  40.   instructions;
  41. ISetCanvas
  42.   buttons;
  43. IPushButton
  44.   closeButton,
  45.   enableDisableButton;
  46. ISystemMenu
  47.   systemMenu;
  48. ICommandConnectionTo< CloseTestWindow >
  49.   cmdConnection;
  50. static const IString
  51.   closeInstructions,
  52.   cannotCloseInstructions,
  53.   enableCloseString,
  54.   disableCloseString;
  55. }; // CloseTestWindow
  56.  
  57. void main ( )
  58. {
  59.   CloseTestWindow
  60.     frame;
  61.   frame
  62.    .setFocus()
  63.    .show();
  64.   IApplication::current().run();
  65. }
  66.  
  67. const IString
  68.   CloseTestWindow::closeInstructions( "You can close the window."
  69.                      "  However, try disabling the Close command." ),
  70.   CloseTestWindow::cannotCloseInstructions( "You cannot close the"
  71.                      " window until you enable the Close command." ),
  72.   CloseTestWindow::enableCloseString( "Enable the Close Command" ),
  73.   CloseTestWindow::disableCloseString( "Disable the Close Command" );
  74.  
  75. CloseTestWindow::CloseTestWindow ( )
  76.   : IFrameWindow( "Disabling the Close System Command" ),
  77.     instructions( IC_FRAME_CLIENT_ID, this, this ),
  78.     buttons( 1, this, this ),
  79.     closeButton( IC_ID_CLOSE, &buttons, &buttons ),
  80.     enableDisableButton( ID_TOGGLE_CLOSE, &buttons, &buttons ),
  81.     systemMenu( this ),
  82.     cmdConnection( *this, CloseTestWindow::processCommand )
  83. {
  84.   instructions
  85.    .setText( closeInstructions );
  86.   closeButton
  87.    .enableSystemCommand()
  88.    .enableDefault()
  89.    .setText( "Close the Window" )
  90.    .enableTabStop()
  91.    .enableGroup();
  92.   enableDisableButton
  93.    .setText( disableCloseString );
  94.   cmdConnection
  95.    .handleEventsFor( this );
  96.  
  97.   (*this)
  98.    .setClient( &instructions )
  99.    .addExtension( &buttons, IFrameWindow::belowClient );
  100. }
  101.  
  102. // This is the function that the ICommandConnectionTo<> object calls.
  103. IBase::Boolean
  104.   CloseTestWindow::processCommand ( ICommandEvent& event )
  105. {
  106.   Boolean
  107.     stopProcessingEvent = false;
  108.   switch ( event.commandId() )
  109.   {
  110.     case IC_ID_CLOSE:
  111.     {    // Assume this is a system command.
  112.        IMessageBox
  113.          msgBox( this );
  114.        const char
  115.         *text;
  116.  
  117.        // When the Close system menu item is disabled, our command
  118.        // handler usually is not called to process this system
  119.        // command.  However, it is called when a user presses an
  120.        // accelerator key for the Close command (Alt+F4)--even
  121.        // though the system eventually ignores this request.  So
  122.        // we must check if the system menu choice is disabled before
  123.        // processing the Close command.
  124.        if ( systemMenu.isItemEnabled( IC_ID_CLOSE ) )
  125.        {      // Close is enabled.
  126.           text = "The window is closing.  Select OK to continue.";
  127.        }
  128.        else
  129.        {
  130.           text = "The Close command is disabled.  You must enable"
  131.                  " it to close the window.";
  132.        }
  133.  
  134.        msgBox
  135.         .show( text,
  136.                IMessageBox::okButton
  137.                  | IMessageBox::informationIcon
  138.                  | IMessageBox::moveable );
  139.        break;
  140.     }
  141.     case ID_TOGGLE_CLOSE:
  142.     {    // Assume that this is an application command.
  143.        // Toggle the enabled state of Close on the system menu.
  144.        Boolean
  145.          enableClose = ! systemMenu.isItemEnabled( IC_ID_CLOSE );
  146.                    // Negate the current state.
  147.        systemMenu
  148.         .enableItem( IC_ID_CLOSE, enableClose );
  149.  
  150.        // Toggle the enabled state of the Close button.
  151.        closeButton
  152.         .enable( enableClose );
  153.        instructions
  154.         .setText( enableClose ? closeInstructions :
  155.                                 cannotCloseInstructions );
  156.        enableDisableButton
  157.         .setText( enableClose ? disableCloseString :
  158.                                 enableCloseString );
  159.  
  160.        stopProcessingEvent = true;
  161.        break;
  162.     }
  163.     default:
  164.        break;
  165.   }
  166.   return stopProcessingEvent;
  167. }
  168.