home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / VSCPPv8.zip / VACPP / IBMCPP / samples / IOC / NOTIFY / NOTIFY.CPP < prev    next >
Text File  |  1995-05-01  |  7KB  |  194 lines

  1. /******************************************************************************
  2. * NOTIFY SAMPLE APPLICATION - notify.cpp                                      *
  3. * DISCLAIMER OF WARRANTIES:                                                   *
  4. *   The following [enclosed] code is sample code created by IBM Corporation.  *
  5. *   This sample code is not part of any standard IBM product and is provided  *
  6. *   to you solely for the purpose of assisting you in the development of your *
  7. *   applications. The code is provided "AS IS", without warranty of any kind. *
  8. *   IBM shall not be liable for any damages arising out of your use of sample *
  9. *   code, even if they have been advised of the possibility of such damages.  *
  10. * COPYRIGHT: Copyright (C) International Business Machines Corp., 1992,1995.  *
  11. *                                                                             *
  12. * This program demonstrates notifications. An entryfield is displayed that    *
  13. * will only accept characters between 'a' and 'z'.  If data is entered that   *
  14. * does not match this criteria, a message warning is displayed.
  15. *******************************************************************************/
  16. #ifndef _IBASE_                         //Make sure ibase.hpp is included
  17.   #include <ibase.hpp>                  //  since that is where IC_<environ>
  18. #endif                                  //  is defined.
  19. #include <iapp.hpp>
  20. #include <iframe.hpp>
  21. #include <imenubar.hpp>
  22. #include <ientryfd.hpp>
  23. #include <isetcv.hpp>
  24. #include <icmdhdr.hpp>
  25. #include <istattxt.hpp>
  26. #include <istring.hpp>
  27. #include <imsgbox.hpp>
  28. #include <ipushbut.hpp>
  29. #include <iobservr.hpp>
  30. #include <inotifev.hpp>
  31. #include "notify.hpp"
  32. #include "notify.h"
  33.  
  34. class CustomerObject;
  35.  
  36. IFrameWindow* frame;
  37. IEntryField*  ef;
  38. IPushButton*  pb;
  39. CustomerObject* co;
  40. IStaticText* display;
  41.  
  42. class CustomerObject {
  43. public:
  44. CustomerObject& storeName(IString name)
  45.   {
  46.     firstName = name;
  47.     return *this;
  48.   }
  49. IString name()
  50.   {
  51.     return firstName;
  52.   }
  53.  
  54. private:
  55. IString
  56.   firstName;
  57. };
  58.  
  59. /*********************************************************************/
  60. /* Declare the entryField observer class.  Override the              */
  61. /* dispatchNotificationEvent function and process the notifications  */
  62. /* needed.                                                           */
  63. /*********************************************************************/
  64. class efObserver : public IObserver {
  65. public:
  66. virtual IObserver
  67.   &dispatchNotificationEvent (const INotificationEvent& anEvent)
  68.   {
  69.     if (anEvent.notificationId() == IEntryField::textId)
  70.     {
  71.       IWindow& window = ((IWindow&)anEvent.notifier());
  72.       IEntryField* entryField = ((IEntryField*)&window);
  73.       IString text(entryField->text());
  74.       if (!(text.isAlphabetic()))
  75.       {
  76.         pb->disable();
  77.         IMessageBox msgBox(frame);
  78.         msgBox.setTitle("Notification information");
  79.         IString
  80.           str("Illegal characters entered.  Only alphabetic chars allowed");
  81.         msgBox.show(str, IMessageBox::information);
  82.       }
  83.       else
  84.       {
  85.         pb->enable();
  86.       }
  87.     }
  88.     else if (anEvent.notificationId() == IEntryField::enableId)
  89.     {
  90.       /**************************************************************/
  91.       /* If the entryField is enabled, enable our pushbutton... if  */
  92.       /* the entryField is disabled, disable our pushbutton.        */
  93.       /**************************************************************/
  94.       if (anEvent.eventData().asUnsignedLong())
  95.       {
  96.         pb->enable();
  97.       }
  98.       else
  99.       {
  100.         pb->disable();
  101.       }
  102.     }
  103.     else if (anEvent.notificationId() == IButton::buttonClickId)
  104.     {
  105.       /***************************************/
  106.       /* Store the data in our bogus object. */
  107.       /***************************************/
  108.       co->storeName(ef->text());
  109.     }
  110.     return *this;
  111.   }
  112. };
  113.  
  114. //*************************************************************************
  115. // main  - Application entry point                                        *
  116. //*************************************************************************
  117. int main()
  118. {
  119.   /************************************/
  120.   /* Create our bogus customer object */
  121.   /************************************/
  122.   co = new CustomerObject();
  123.  
  124.   /*************************************************************/
  125.   /* Create the frame window and give it an icon and menu bar. */
  126.   /*************************************************************/
  127.   frame = new IFrameWindow("Notify Sample",WND_MAIN);
  128.   frame->setIcon( WND_MAIN );
  129.   MyMenuCommandHandler* menuCmdHandler = new MyMenuCommandHandler();
  130.   menuCmdHandler->handleEventsFor(frame);
  131.   IMenuBar* menuBar = new IMenuBar(MAIN_MENU, frame);
  132.  
  133.   /*******************************************************************/
  134.   /* Create a canvas with a statictext, entryfield and a pushbutton. */
  135.   /*******************************************************************/
  136.   ISetCanvas* sc  = new ISetCanvas(SC_1, frame, frame);
  137.   IStaticText* st = new IStaticText(ST_1, sc, sc);
  138.   st->setText("alphabetic data only");
  139.   ef = new IEntryField(EF_1, sc, sc);
  140.   pb = new IPushButton(PB_1, sc, sc);
  141.   pb->setText("Enter");
  142.   pb->disable();
  143.  
  144.   /*************************************************************/
  145.   /* Create our observer and tell it to observe the entryfield */
  146.   /* and pushbutton.                                           */
  147.   /*************************************************************/
  148.   efObserver* entryFieldObserver = new efObserver();
  149.   entryFieldObserver->handleNotificationsFor(*ef);
  150.   entryFieldObserver->handleNotificationsFor(*pb);
  151.  
  152.   /**********************************************************/
  153.   /* Enable the entryfield and pushbutton for notifications */
  154.   /**********************************************************/
  155.   ef->enableNotification();
  156.   pb->enableNotification();
  157.  
  158.   /****************************************************************/
  159.   /* Add another static text field to display the contents of our */
  160.   /* customer object.                                             */
  161.   /****************************************************************/
  162.   display = new IStaticText(ST_2, sc, sc);
  163.  
  164.   frame->setClient(sc);
  165.   frame->setFocus();
  166.   frame->show();
  167.  
  168.   IApplication::current().run();
  169.  
  170.   return 0;
  171. }
  172.  
  173. IBase::Boolean MyMenuCommandHandler::command(ICommandEvent& event)
  174. {
  175.   switch(event.commandId())
  176.   {
  177.     case MI_DISABLEEF:
  178.       ef->disable();
  179.       return true;
  180.     break;
  181.  
  182.     case MI_ENABLEEF:
  183.       ef->enable();
  184.       return true;
  185.     break;
  186.  
  187.     case MI_SHOW:
  188.       display->setText(co->name());
  189.       return true;
  190.     break;
  191.   }
  192.   return false;
  193. }
  194.