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

  1. /******************************************************************************
  2. * .FILE:         notify.cpp                                                   *
  3. *                                                                             *
  4. * .DESCRIPTION:  Notify Sample Application: Class Implementation              *
  5. *                                                                             *
  6. * .CLASSES:      CustomerObject                                               *
  7. *                efObserver                                                   *
  8. *                MenuCommandHandler                                           *
  9. *                                                                             *
  10. * .COPYRIGHT:                                                                 *
  11. *    Licensed Material - Program-Property of IBM                              *
  12. *    (C) Copyright IBM Corp. 1992, 1996 - All Rights Reserved                 *
  13. *                                                                             *
  14. * .DISCLAIMER:                                                                *
  15. *   The following [enclosed] code is sample code created by IBM               *
  16. *   Corporation.  This sample code is not part of any standard IBM product    *
  17. *   and is provided to you solely for the purpose of assisting you in the     *
  18. *   development of your applications.  The code is provided 'AS IS',          *
  19. *   without warranty of any kind.  IBM shall not be liable for any damages    *
  20. *   arising out of your use of the sample code, even if they have been        *
  21. *   advised of the possibility of such damages.                               *
  22. *                                                                             *
  23. * .NOTE: WE RECOMMEND USING A FIXED SPACE FONT TO LOOK AT THE SOURCE          *
  24. *                                                                             *
  25. ******************************************************************************/
  26. #include <ibase.hpp>
  27. #include <iapp.hpp>
  28. #include <iframe.hpp>
  29. #include <ientryfd.hpp>
  30. #include <isetcv.hpp>
  31. #include <istattxt.hpp>
  32. #include <imsgbox.hpp>
  33. #include <ipushbut.hpp>
  34. #include <ireslib.hpp>
  35. #include <imenubar.hpp>
  36. #include <icoordsy.hpp>
  37. #include "notify.hpp"
  38. #include "notify.h"
  39.  
  40.  
  41. /*******************************************************************************
  42. * Global Varaibles that are used by various functions                          *
  43. *******************************************************************************/
  44. IFrameWindow
  45.  *frame;
  46. IEntryField
  47.  *ef;
  48. IPushButton
  49.  *pb;
  50. CustomerObject
  51.  *co;
  52. IStaticText
  53.  *display;
  54.  
  55.  
  56. /*******************************************************************************
  57. * main  - Application entry point                                              *
  58. *******************************************************************************/
  59. int main()
  60. {
  61.   ICoordinateSystem::setApplicationOrientation(
  62.           ICoordinateSystem::originLowerLeft );
  63.  
  64.   IResourceLibrary
  65.     reslib;
  66.  
  67. /*-----------------------------------------------------------------------------|
  68. | Create our bogus customer object                                             |
  69. ------------------------------------------------------------------------------*/
  70.   co = new CustomerObject;
  71.  
  72. /*-----------------------------------------------------------------------------|
  73. | Create the frame window and give it an icon and menu bar.                    |
  74. ------------------------------------------------------------------------------*/
  75.   frame = new IFrameWindow(reslib.loadString(WND_MAIN),WND_MAIN);
  76.   frame->setIcon( WND_MAIN );
  77.   MenuCommandHandler* menuCmdHandler = new MenuCommandHandler();
  78.   menuCmdHandler->handleEventsFor(frame);
  79.   IMenuBar* menuBar = new IMenuBar(MAIN_MENU, frame);
  80.  
  81. /*-----------------------------------------------------------------------------|
  82. | Create a canvas with a statictext, entryfield and a pushbutton.              |
  83. ------------------------------------------------------------------------------*/
  84.   ISetCanvas* sc  = new ISetCanvas(SC_1, frame, frame);
  85.   IStaticText* st = new IStaticText(ST_1, sc, sc);
  86.   st->setText(STR_ALPHA_ONLY);
  87.   ef = new IEntryField(EF_1, sc, sc);
  88.   pb = new IPushButton(PB_1, sc, sc);
  89.   pb->setText(STR_ENTER);
  90.   pb->disable();
  91.  
  92. /*-----------------------------------------------------------------------------|
  93. | Create our observer and tell it to observe the entryfield and pushbutton.    |
  94. ------------------------------------------------------------------------------*/
  95.   efObserver* entryFieldObserver = new efObserver();
  96.   entryFieldObserver->handleNotificationsFor(*ef);
  97.   entryFieldObserver->handleNotificationsFor(*pb);
  98.  
  99. /*-----------------------------------------------------------------------------|
  100. | Enable the entryfield and pushbutton for notifications                       |
  101. ------------------------------------------------------------------------------*/
  102.   ef->enableNotification();
  103.   pb->enableNotification();
  104.  
  105. /*-----------------------------------------------------------------------------|
  106. | Add another static text field to display the contents of our customer object.|
  107. ------------------------------------------------------------------------------*/
  108.   display = new IStaticText(ST_2, sc, sc);
  109.  
  110.   frame->setClient(sc);
  111.   frame->setFocus();
  112.   frame->show();
  113.  
  114.   IApplication::current().run();
  115.  
  116.   return 0;
  117. }
  118.  
  119.  
  120. /*******************************************************************************
  121. * Class CustomerObject::storeName - stores the customer name                   *
  122. *******************************************************************************/
  123. CustomerObject
  124.  &CustomerObject::storeName(IString name)
  125. {
  126.   firstName = name;
  127.   return *this;
  128. }
  129.  
  130. /*******************************************************************************
  131. * Class CustomerObject::name - returns the customer name                       *
  132. *******************************************************************************/
  133. IString
  134.   CustomerObject::name()
  135. {
  136.   return firstName;
  137. }
  138.  
  139. /*******************************************************************************
  140. * Class efObserver::dispatchNotificationEvent - Dispatches events related to   *
  141. *   the entryfield.                                                            *
  142. *******************************************************************************/
  143.  
  144. IObserver
  145.   &efObserver::dispatchNotificationEvent (const INotificationEvent& anEvent)
  146. {
  147.   if (anEvent.notificationId() == IEntryField::textId)
  148.   {
  149.     IWindow& window = ((IWindow&)anEvent.notifier());
  150.     IEntryField* entryField = ((IEntryField*)&window);
  151.     IString text(entryField->text());
  152.     if (!(text.isAlphabetic()))
  153.     {
  154.       pb->disable();
  155.       IMessageBox msgBox(frame);
  156.       msgBox.setTitle(TITLE_NOTIF_INF);
  157.       IResourceLibrary
  158.         reslib;
  159.       IString
  160.         str(reslib.loadString(STR_ILLEGAL_CHARS));
  161.        msgBox.show(str, IMessageBox::information);
  162.     }
  163.     else
  164.     {
  165.       pb->enable();
  166.     }
  167.   }
  168.   else if (anEvent.notificationId() == IEntryField::enableId)
  169.   {
  170. /*---------------------------------------------------------------------------|
  171. | If the entryField is enabled, enable our pushbutton...                     |
  172. | if the entryField is disabled, disable our pushbutton.                     |
  173. ----------------------------------------------------------------------------*/
  174.     if (anEvent.eventData().asUnsignedLong())
  175.     {
  176.       pb->enable();
  177.     }
  178.     else
  179.     {
  180.       pb->disable();
  181.     }
  182.   }
  183.   else if (anEvent.notificationId() == IButton::buttonClickId)
  184.   {
  185. /*-----------------------------------------------------------------------------|
  186. | Store the data in our bogus object.                                          |
  187. ------------------------------------------------------------------------------*/
  188.     co->storeName(ef->text());
  189.   }
  190.   return *this;
  191. }
  192.  
  193.  
  194. /******************************************************************************
  195. * MenuCommandHandler::command - Handles all menu events                       *
  196. ******************************************************************************/
  197. IBase::Boolean MenuCommandHandler::command(ICommandEvent& event)
  198. {
  199.   switch(event.commandId())
  200.   {
  201.     case MI_DISABLEEF:
  202.       ef->disable();
  203.       return true;
  204.     case MI_ENABLEEF:
  205.       ef->enable();
  206.       return true;
  207.     case MI_SHOW:
  208.       display->setText(co->name());
  209.       return true;
  210.   }
  211.   return false;
  212. }
  213.