home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / ioc / lancelot / lgoodies.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-22  |  11.3 KB  |  243 lines

  1. /******************************************************************************
  2. * .FILE:         lgoodies.cpp                                                 *
  3. *                                                                             *
  4. * .DESCRIPTION:  Lancelot Sample Program:              Class Implementation   *
  5. *                                                                             *
  6. * .CLASSES:      LAskUser                                                     *
  7. *                LAskUserCommandHandler                                       *
  8. *                LWindow                                                      *
  9. *                                                                             *
  10. * .COPYRIGHT:                                                                 *
  11. *                                                                             *
  12. * .DISCLAIMER:                                                                *
  13. *                                                                             *
  14. * .NOTE: WE RECOMMEND USING A FIXED SPACE FONT TO LOOK AT THE SOURCE          *
  15. *                                                                             *
  16. ******************************************************************************/
  17.  
  18. #include "lgoodies.hpp"
  19. #include "lancelot.h"
  20.  
  21. /******************************************************************************
  22. * Class LAskUser - Constructor                                                *
  23. *   Up to 4 lines of text can be added to this frame window                   *
  24. ******************************************************************************/
  25. LAskUser::LAskUser( unsigned long id, IWindow* parent, IWindow* owner,
  26.                     const IString& theText1,
  27.                     const IString& theText2,
  28.                     const IString& theText3,
  29.                     const IString& theText4 )
  30.      :IFrameWindow         ( id, parent, owner, IRectangle(),
  31.                              titleBar | dialogBackground
  32.                              | dialogBorder | systemMenu ),
  33.       title                ( this, STR_ASKUSER_TITLE ),
  34.       canvas               ( ID_ASKUSER_CANVAS, this, this ),
  35.       bCanvas              ( ID_ASKUSER_BUTTON_CANVAS, &canvas, &canvas ),
  36.       staticText1          ( ID_ASKUSER_TEXT, &canvas, &canvas, IRectangle(),
  37.                              IStaticText::classDefaultStyle
  38.                              | IStaticText::wordBreak ),
  39.       staticText2          ( ID_ASKUSER_TEXT, &canvas, &canvas, IRectangle(),
  40.                              IStaticText::classDefaultStyle
  41.                              | IStaticText::wordBreak ),
  42.       staticText3          ( ID_ASKUSER_TEXT, &canvas, &canvas, IRectangle(),
  43.                              IStaticText::classDefaultStyle
  44.                              | IStaticText::wordBreak ),
  45.       staticText4          ( ID_ASKUSER_TEXT, &canvas, &canvas, IRectangle(),
  46.                              IStaticText::classDefaultStyle
  47.                              | IStaticText::wordBreak ),
  48.       entry                ( ID_ASKUSER_ENTRY, &canvas, &canvas, IRectangle(),
  49.                              IEntryField::classDefaultStyle
  50.                              | IControl::tabStop ),
  51.       okButton             ( ID_BUTTON_OK, &bCanvas, &bCanvas, IRectangle(),
  52.                              IPushButton::classDefaultStyle
  53.                              | IPushButton::defaultButton
  54.                              | IControl::tabStop | IControl::group ),
  55.       cancelButton         ( ID_BUTTON_CANCEL, &bCanvas, &bCanvas, IRectangle(),
  56.                              IPushButton::classDefaultStyle ),
  57.       cmdHdr               ( this ),
  58.       ok                   ( false )
  59. {
  60. /*-----------------------------------------------------------------------------
  61. | Set the text for each static text object                                    |
  62. -----------------------------------------------------------------------------*/
  63.    staticText1.setText( theText1 );
  64.    staticText2.setText( theText2 );
  65.    staticText3.setText( theText3 );
  66.    staticText4.setText( theText4 );
  67.    okButton.setText( STR_OK );
  68.    cancelButton.setText( STR_CANCEL );
  69.  
  70. /*-----------------------------------------------------------------------------
  71. | Add the objects to the canvas                                               |
  72. -----------------------------------------------------------------------------*/
  73.    canvas.addToCell( &staticText1,  2,  2, 10,  1 );
  74.    canvas.addToCell( &staticText2,  2,  3, 10,  1 );
  75.    canvas.addToCell( &staticText3,  2,  4, 10,  1 );
  76.    canvas.addToCell( &staticText4,  2,  5, 10,  1 );
  77.    canvas.addToCell( &entry,        2,  7, 10,  2 );
  78.    canvas.addToCell( &bCanvas,      3, 11 );
  79.  
  80. /*-----------------------------------------------------------------------------
  81. | Set the client to be the canvas                                             |
  82. -----------------------------------------------------------------------------*/
  83.     setClient( &canvas );
  84.  
  85. /*-----------------------------------------------------------------------------
  86. | Start handling command events                                               |
  87. -----------------------------------------------------------------------------*/
  88.     cmdHdr.handleEventsFor( this );
  89.  
  90. /*-----------------------------------------------------------------------------
  91. | Resize the window based on the minimum size of the canvas                   |
  92. -----------------------------------------------------------------------------*/
  93. #ifndef IC_MOTIF
  94.    moveSizeToClient( IRectangle( IPoint(
  95.                      IWindow::desktopWindow()->size().width()/2,
  96.                      IWindow::desktopWindow()->size().height()/2 ),
  97.                      canvas.minimumSize() ) );
  98. #else
  99.    moveSizeTo( IRectangle( IPoint(
  100.                IWindow::desktopWindow()->size().width()/2,
  101.                IWindow::desktopWindow()->size().height()/2 ),
  102.                canvas.minimumSize() ) );
  103. #endif
  104.  
  105. /*-----------------------------------------------------------------------------
  106. | Center the frame window                                                     |
  107. -----------------------------------------------------------------------------*/
  108.    moveSizeTo( rect().centerAt( IWindow::desktopWindow()->rect().center() ) );
  109.  
  110. /*-----------------------------------------------------------------------------
  111. | Set focus to the entryfield                                                 |
  112. | Show the frame window modally                                               |
  113. -----------------------------------------------------------------------------*/
  114.    entry.setFocus();
  115.    showModally();
  116. }
  117.  
  118.  
  119. /******************************************************************************
  120. * Class LAskUser - Destructor                                                 *
  121. ******************************************************************************/
  122. LAskUser::~LAskUser()
  123. {
  124.    cmdHdr.stopHandlingEventsFor( this );
  125. }
  126.  
  127.  
  128. /******************************************************************************
  129. * Class LAskUserCommandHandler - Constructor for command handler              *
  130. *                                                                             *
  131. * Create yourself as an ICommandHandler                                       *
  132. * Store the owner window                                                      *
  133. ******************************************************************************/
  134. LAskUserCommandHandler::LAskUserCommandHandler( LAskUser* owner )
  135.      :ICommandHandler(),
  136.       theOwner( owner )
  137. {}
  138.  
  139.  
  140. /******************************************************************************
  141. * Class LAskUserCommandHandler - Destructor                                   *
  142. ******************************************************************************/
  143. LAskUserCommandHandler::~LAskUserCommandHandler()
  144. {}
  145.  
  146.  
  147. /******************************************************************************
  148. * Class LAskUserCommandHandler :: command()                                   *
  149. *   Handle any command events for the frame window                            *
  150. ******************************************************************************/
  151. IBase::Boolean LAskUserCommandHandler::command( ICommandEvent& event )
  152. {
  153.    Boolean
  154.       retCode = false;
  155.  
  156.    switch ( event.commandId() )
  157.    {
  158. /*-----------------------------------------------------------------------------
  159. | If the user pressed the OK button,                                          |
  160. | - Set the pressed OK button flag to true                                    |
  161. | - Dismiss the frame window                                                  |
  162. -----------------------------------------------------------------------------*/
  163.       case ID_BUTTON_OK:
  164.       {
  165.          theOwner->setOk( true );
  166.          theOwner->dismiss();
  167.          retCode = true;
  168.          break;
  169.       }
  170. /*-----------------------------------------------------------------------------
  171. | If the user pressed the CANCEL button,                                      |
  172. | - Set the pressed OK button flag to false                                   |
  173. | - Dismiss the frame window                                                  |
  174. -----------------------------------------------------------------------------*/
  175.       case ID_BUTTON_CANCEL:
  176.       {
  177.          theOwner->setOk( false );
  178.          theOwner->dismiss();
  179.          retCode = true;
  180.          break;
  181.       }
  182.    }
  183.  
  184.    return retCode;
  185. }
  186.  
  187.  
  188. /******************************************************************************
  189. * Class LFrameWindow - Constructor                                            *
  190. *   Common frame window functions.                                            *
  191. ******************************************************************************/
  192. LFrameWindow::LFrameWindow()
  193. {}
  194.  
  195.  
  196. /******************************************************************************
  197. * Class LFrameWindow - Destructor                                             *
  198. ******************************************************************************/
  199. LFrameWindow::~LFrameWindow()
  200. {}
  201.  
  202.  
  203. /******************************************************************************
  204. * Class LFrameWindow::bestFit()                                               *
  205. *   If a frame window spans past the desktop window size,                     *
  206. *   recalculate the best location for the frame window.                       *
  207. ******************************************************************************/
  208. IPoint LFrameWindow::bestFit( IFrameWindow* frameWin )
  209. {
  210.    int
  211.       oldXPosition = frameWin->rect().left(),
  212.       oldYPosition = frameWin->rect().bottom();
  213.  
  214.    if ( oldXPosition < 0 )
  215.       oldXPosition = 0;
  216.    if ( oldYPosition < 0 )
  217.       oldYPosition = 0;
  218.  
  219.    ISize
  220.       desktopSize( IWindow::desktopWindow()->size() );
  221.    int
  222.       newXPosition = frameWin->rect().right() - desktopSize.width(),
  223.       newYPosition = frameWin->rect().top() - desktopSize.height();
  224.    if ( newXPosition > 0 )
  225.    {
  226.       newXPosition = oldXPosition - newXPosition;
  227.       if ( newXPosition < 0 )
  228.          newXPosition = 0;
  229.    }
  230.    else
  231.       newXPosition = oldXPosition;
  232.    if ( newYPosition > 0 )
  233.    {
  234.       newYPosition = oldYPosition - newYPosition;
  235.       if ( newYPosition < 0 )
  236.          newYPosition = 0;
  237.    }
  238.    else
  239.       newYPosition = oldYPosition;
  240.  
  241.    return IPoint( newXPosition, newYPosition );
  242. }
  243.