home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / powergui / editctls / logon / logon.cpp next >
Encoding:
C/C++ Source or Header  |  1996-10-29  |  5.9 KB  |  214 lines

  1. //************************************************************
  2. // Edit Controls - Using an Edit Handler example
  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 <iedithdr.hpp>
  11. #include <ientryfd.hpp>
  12. #include <iframe.hpp>
  13. #include <imcelcv.hpp>
  14. #include <ipushbut.hpp>
  15. #include <istattxt.hpp>
  16. #include <istring.hpp>
  17. #include "logon.h"
  18.  
  19. IString password;
  20.  
  21. // Command handler to capture menu commands.
  22. class CommandHandler : public ICommandHandler
  23. {
  24. public:
  25.   CommandHandler ( IFrameWindow* frame,
  26.                    IStaticText* status )
  27.             : aFrame(frame),
  28.               aStatus(status) {}
  29. protected:
  30. virtual Boolean
  31.   command              ( ICommandEvent& event );
  32. private:
  33. IFrameWindow
  34.  *aFrame;
  35. IStaticText
  36.  *aStatus;
  37. CommandHandler ( const CommandHandler&);
  38. CommandHandler& operator= ( const CommandHandler&);
  39. };
  40.  
  41. // Command handler to verify and dismiss logon panel.
  42. class LogonHandler : public ICommandHandler
  43. {
  44. public:
  45.   LogonHandler( IFrameWindow* frame,
  46.                 IStaticText* status )
  47.             : frame(frame), status(status) {}
  48. protected:
  49. virtual Boolean
  50.   command              ( ICommandEvent& cmd )
  51.   {
  52.     // Check password and display correct message.
  53.     if (password == "Rules")
  54.       status->setText(SI_LOGONPASSED);
  55.     else
  56.       status->setText(SI_LOGONFAILED);
  57.     frame->dismiss( cmd.commandId() );
  58.     return true;
  59.   }
  60. private:
  61. IFrameWindow
  62.  *frame;
  63. IStaticText
  64.  *status;
  65. };
  66.  
  67. // Edit handler that checks password for alphabetic characters.
  68. class PasswordHandler : public IEditHandler
  69. {
  70. public:
  71.   PasswordHandler( IEntryField* ef, IPushButton* pb )
  72.             : entryPW(ef), OkButton(pb) {}
  73. protected:
  74. virtual Boolean
  75.   edit        ( IControlEvent& evt )
  76.   {
  77.     // Get the password contents on each update.
  78.     password = entryPW->text();
  79.  
  80.     // If a non-alphabetic key is entered, disable the OK
  81.     // push button so the dialog cannot be dismissed.
  82.     if (!(password.isAlphabetic()))
  83.       OkButton->disable();
  84.     else
  85.       OkButton->enable();
  86.     return true;
  87.   }
  88. private:
  89. IEntryField
  90.  *entryPW;
  91. IPushButton
  92.  *OkButton;
  93. };
  94.  
  95. // Logon frame window to prompt for logon id and password.
  96. class LogonView : public IFrameWindow
  97. {
  98. public:
  99.   LogonView ( unsigned long windowId,
  100.               IWindow* owner,
  101.               IStaticText* client );
  102. private:
  103. IMultiCellCanvas
  104.   clClient;
  105. IStaticText
  106.   clLogonIdPrompt,
  107.   clLogonPasswordPrompt;
  108. IEntryField
  109.   clLogonId,
  110.   clLogonPassword;
  111. IPushButton
  112.   clOkButton;
  113. LogonHandler
  114.   logonHandler;
  115. PasswordHandler
  116.   pwHandler;
  117. LogonView ( const LogonView&);
  118. LogonView& operator= ( const LogonView&);
  119. };
  120.  
  121. void main()
  122. {
  123. // Create a frame window with a menubar.
  124. IFrameWindow
  125.   frame( "Edit Control Example", MAIN_MENU,
  126.          IFrameWindow::defaultStyle() | IFrameWindow::menuBar );
  127.  
  128. // Create a client text field for displaying login status.
  129. // Create a command handler to handle the menu commands.
  130. IStaticText
  131.   statusArea(ID_STATUS, &frame, &frame);
  132. CommandHandler
  133.   commandHandler(&frame, &statusArea);
  134. commandHandler
  135.   .handleEventsFor(&frame);
  136.  
  137. // Set the focus and show the application.
  138. frame
  139.   .setClient(&statusArea)
  140.   .setFocus()
  141.   .show();
  142. IApplication::current().run();
  143. }
  144.  
  145. // Command override for menu commands.
  146. Boolean CommandHandler::command( ICommandEvent& event )
  147. {
  148.   switch(event.commandId())
  149.   {
  150.     case MI_LOGON:
  151.     {
  152.       LogonView* logonPanel = new LogonView( ID_LOGON, aFrame,
  153.                                              aStatus );
  154.       logonPanel->setFocus();
  155.       logonPanel->showModally();
  156.       return true;
  157.     }
  158. #ifdef IC_WIN
  159.     case IC_ID_CLOSE:
  160.       aFrame->close();
  161.       return(true);
  162. #endif
  163.   }
  164. return false;
  165. }
  166.  
  167. // LogonView constructor, which creates the logon prompting panel.
  168. LogonView :: LogonView( unsigned long windowId, IWindow* owner,
  169.                         IStaticText* client )
  170.     : IFrameWindow ( windowId, 0, owner, IRectangle( 50, 50, 320, 200 ),
  171.                      IFrameWindow::defaultStyle() |
  172.                      IFrameWindow::dialogBackground ),
  173.       clClient( IC_FRAME_CLIENT_ID, this, this ),
  174.       clLogonIdPrompt( ID_LOGONIDPMT, &clClient, &clClient ),
  175.       clLogonPasswordPrompt( ID_LOGONPWPMT, &clClient, &clClient ),
  176.       clLogonId( ID_LOGONID, &clClient, &clClient ),
  177.       clLogonPassword( ID_LOGONPW, &clClient, &clClient, IRectangle(),
  178.                        IEntryField::defaultStyle() | IEntryField::unreadable ),
  179.       clOkButton( ID_OKBUTTON, &clClient, &clClient, IRectangle(),
  180.                   IPushButton::defaultStyle() | IPushButton::defaultButton ),
  181.       logonHandler( this, client ),
  182.       pwHandler( &clLogonPassword, &clOkButton )
  183. {
  184.   // Add the controls created above into the multicell canvas.
  185.   clClient
  186.    .addToCell( &clLogonIdPrompt,        2, 2 )
  187.    .addToCell( &clLogonId,              4, 2 )
  188.    .addToCell( &clLogonPasswordPrompt,  2, 4 )
  189.    .addToCell( &clLogonPassword,        4, 4 )
  190.    .addToCell( &clOkButton,             2, 6 )
  191.    .setRowHeight( 7, IMultiCellCanvas::defaultCell().height() )
  192.    .setColumnWidth( 5, IMultiCellCanvas::defaultCell().width() );
  193.  
  194.   // Initialize the text fields from the resource file.
  195.   clLogonIdPrompt.setText( ID_LOGONIDPMT );
  196.   clLogonPasswordPrompt.setText( ID_LOGONPWPMT );
  197.   clLogonId
  198.    .setLimit( 12 )
  199.    .setText( ID_LOGONID )
  200.    .enableTabStop();
  201.   clOkButton.setText( ID_OKBUTTON );
  202.   clOkButton.disable();
  203.   clLogonPassword
  204.    .setLimit( 12 )
  205.    .enableTabStop();
  206.   this->setClient( &clClient );
  207.   setAutoDeleteObject( true );
  208.  
  209.   // Attach the handlers for verifying input.
  210.   logonHandler.handleEventsFor( this );
  211.   pwHandler.handleEventsFor( &clLogonPassword );
  212. }
  213.  
  214.