home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / VSCPPv8.zip / VACPP / IBMCPP / samples / IOC / HELLO3 / AHELLOW3.CPP next >
C/C++ Source or Header  |  1995-04-07  |  17KB  |  226 lines

  1. /*****************************************************************************
  2. * HELLO WORLD SAMPLE PROGRAM - Version 3: Class Implementation (ahellow3.cpp)*
  3. *                                                                            *
  4. * COPYRIGHT: Copyright (C) International Business Machines Corp., 1992,1995. *
  5. *                                                                            *
  6. * DISCLAIMER OF WARRANTIES:                                                  *
  7. *   The following [enclosed] code is sample code created by IBM              *
  8. *   Corporation.  This sample code is not part of any standard IBM product   *
  9. *   and is provided to you solely for the purpose of assisting you in the    *
  10. *   development of your applications.  The code is provided "AS IS",         *
  11. *   without warranty of any kind.  IBM shall not be liable for any damages   *
  12. *   arising out of your use of the sample code, even if they have been       *
  13. *   advised of the possibility of such damages.                              *
  14. *****************************************************************************/
  15. //NOTE: WE RECOMMEND USING A FIXED-SPACE FONT TO LOOK AT THE SOURCE.
  16. /**************************************************************************   V1
  17. * C++ Hello World History and Key Functions:                              *   V1
  18. *   Version 1: (lines with V1 in column 79-80)                            *   V1
  19. *      - Creates and runs a simple application                            *   V1
  20. *      - Creates the main window (IFrameWindow)                           *   V1
  21. *      - Creates a static text control set to "Hello, World!!!" as the    *   V1
  22. *         client window                                                   *   V1
  23. *                                                                         *   V2
  24. *   Version 2: (lines with V2 in column 79-80)                            *   V2
  25. *      - Creates a main window (AHellowWindow) derived from IFrameWindow  *   V2
  26. *      - Gets the "Hello, World!!!" text string and other items from a    *   V2
  27. *         resource file                                                   *   V2
  28. *      - Sets the window title from a resource file                       *   V2
  29. *      - Creates and sets the information area below the client window    *   V2
  30. *                                                                         *   V3
  31. *   Version 3: (lines with V3 in column 79-80)                            *   V3
  32. *      - Adds alignment menu bar with left, center, and right menu items  *   V3
  33. *      - Adds command processing for aligning the                         *   V3
  34. *         "Hello, World!!!" text                                          *   V3
  35. *      - Places check mark in front of selected menu item                 *   V3
  36. *      - Creates and maintains current alignment status line              *   V3
  37. **************************************************************************/
  38. //Include User Interface Class Library class headers:
  39. #ifndef _IBASE_                         //Make sure ibase.hpp is included
  40.   #include <ibase.hpp>                  //  since that is where IC_<environ>
  41. #endif                                  //  is defined.
  42. #include <iapp.hpp>                     //IApplication class                  V1
  43. #include <ifont.hpp>                    //IFont class                         V3
  44.  
  45. #include "ahellow3.hpp"                 //Include AHelloWindow class headers  v3
  46. #include "ahellow3.h"                   //Include symbolic definitions        v3
  47.  
  48. /**************************************************************************   V1
  49. * main  - Application entry point for Hello World Version 3.              *   v3
  50. *         This simple application does the following:                     *   V1
  51. *           1) Creates a new object mainWindow of class AHelloWindow      *   v2
  52. *           2) Sets Hello World window alignment                          *   V3
  53. *           3) Sets the size of mainWindow                                *   v3
  54. *           4) Sets the window focus to mainWindow                        *   v3
  55. *           5) Displays the mainWindow                                    *   v3
  56. *           6) Starts the events processing for the application           *   v3
  57. **************************************************************************/
  58. int main()                                                                  //V1
  59. {                                                                           //V1
  60.   AHelloWindow mainWindow (WND_MAIN);                                       //V2
  61.   mainWindow.setAlignment(AHelloWindow::left);                              //V3
  62.   mainWindow.sizeTo(ISize(400,300));                                        //V1
  63.   mainWindow.setFocus();                                                    //V1
  64.   mainWindow.show();                                                        //V1
  65.   IApplication::current().run();                                            //V1
  66.   return 0;
  67. } /* end main */                                                            //V1
  68.  
  69. /**************************************************************************   V2
  70. * AHelloWindow :: AHelloWindow - Constructor for the main window          *   V2
  71. * Construct the IFrameWindow using the default style plus minimizedIcon,  *   V2
  72. *   which gets the Icon identified in the resource file and associates it *   V2
  73. *   with the main window.                                                 *   V2
  74. * Create a menu bar object for the main window menu bar that was loaded   *   V3
  75. *   from the resource file.  menuBar is used by setAlignment to           *   V3
  76. *   manipulate check marks on the menu items.                             *   V3
  77. * Create a static text object for displaying the status of the            *   V3
  78. *   Hello World text alignment.                                           *   V3
  79. * Create the hello world static text to be used as the client window.     *   V2
  80. * Create the Hello World information area object from IInfoArea class.    *   V2
  81. *   The information area is automatically added as an extension below     *   V2
  82. *   the client window of the frame.                                       *   V2
  83. * Create a command handler to process command events from menu item       *   v3
  84. *   selections.                                                           *   v3
  85. **************************************************************************/ //V2
  86.  
  87. AHelloWindow :: AHelloWindow(unsigned long windowId)                        //V2
  88.   : IFrameWindow(IFrameWindow::defaultStyle() |                             //V2
  89.                  IFrameWindow::minimizedIcon,                               //V2
  90.                  windowId)                                                  //V2
  91.    ,menuBar(windowId, this)                                                 //V3
  92.    ,statusLine(WND_STATUS, this, this)                                      //V3
  93.    ,hello(WND_HELLO, this, this)                                            //V2
  94.    ,infoArea(this)                                                          //V2
  95.    ,commandHandler(this)                                                    //V3
  96. {                                                                           //V2
  97.  
  98. /*------------------------- Setup the Client Window ----------------------|   V2
  99. |  Set the hello world static text window as the client window.           |   V2
  100. |------------------------------------------------------------------------*/ //V2
  101.   setClient(&hello);                                                        //V2
  102.  
  103. /*------------------------- Setup the Status Line ------------------------|   V3
  104. |  Add the status line as an extension to the frame above the client      |   V3
  105. |    window with the height calculated from the maximum height of a       |   V3
  106. |    character in the current font.                                       |   V3
  107. |------------------------------------------------------------------------*/ //V3
  108.   addExtension(&statusLine, IFrameWindow::aboveClient,                      //V3
  109.                  IFont(&statusLine).maxCharHeight());                       //V3
  110.  
  111. /*---------------------------- Set Text Values ---------------------------|   V2
  112. |  Set the values for the text controls from strings in the resource file.|   V2
  113. |    The infoArea inactive text is displayed when no menu item is active. |   V2
  114. |------------------------------------------------------------------------*/ //V2
  115.   hello.setText(STR_HELLO);                                                 //V2
  116.   infoArea.setInactiveText(STR_INFO);                                       //V2
  117.  
  118. /*----------------------- Setup the Command Handler ----------------------|   V3
  119. |  Have the command handler handle commands sent from the frame window.   |   V3
  120. |    The command handler's command() function is called to process        |   V3
  121. |    menu item selections.                                                |   V3
  122. |------------------------------------------------------------------------*/ //V3
  123.   commandHandler.handleEventsFor(this);                                     //V3
  124.  
  125. /*----------------------- Set Default Alignment --------------------------|   V2
  126. | Align the static text, set the status line, and set the check mark in   |   v3
  127. |   the menu bar.                                                         |   v3
  128. |------------------------------------------------------------------------*/ //V2
  129.   setAlignment(center);                                                     //v3
  130.  
  131. } /* end AHelloWindow :: AHelloWindow(...) */                               //V2
  132.  
  133. /**************************************************************************   V3
  134. * AHelloWindow :: ~AHelloWindow - Destructor for the main window          *   V3
  135. *   Stop handling command events for the frame.                           *   V3
  136. **************************************************************************/ //V3
  137. AHelloWindow :: ~AHelloWindow()                                             //V3
  138. {                                                                           //V3
  139.   commandHandler.stopHandlingEventsFor(this);                               //V3
  140.  
  141. } /* end AHelloWindow :: ~AHelloWindow() */                                 //V3
  142.  
  143. /**************************************************************************   V3
  144. * AHelloWindow :: setAlignment - Align static text in client window       *   V3
  145. **************************************************************************/ //V3
  146. AHelloWindow &                                                              //V3
  147.   AHelloWindow :: setAlignment(Alignment alignment)                         //V3
  148. {                                                                           //V3
  149. /*--------------------- Update Window for New Alignment ------------------|   V3
  150. |  Depending on the value passed, update the window as follows:           |   V3
  151. |    Set the alignment of the static text control in the client window.   |   V3
  152. |    Set the text of the alignment status line static text control.       |   V3
  153. |    Check the selected menu item; remove check marks from the other two. |   V3
  154. |------------------------------------------------------------------------*/ //V3
  155.   switch(alignment)                                                         //V3
  156.   {                                                                         //V3
  157.   case left:                                                                //V3
  158.     hello.setAlignment(                                                     //V3
  159.      IStaticText::centerLeft);                                              //V3
  160.     statusLine.setText(STR_LEFT);                                           //V3
  161.     menuBar.uncheckItem(MI_CENTER);                                         //V3
  162.     menuBar.checkItem(MI_LEFT);                                             //V3
  163.     menuBar.uncheckItem(MI_RIGHT);                                          //V3
  164.     break;                                                                  //V3
  165.   case center:                                                              //V3
  166.     hello.setAlignment(                                                     //V3
  167.       IStaticText::centerCenter);                                           //V3
  168.     statusLine.setText(STR_CENTER);                                         //V3
  169.     menuBar.checkItem(MI_CENTER);                                           //V3
  170.     menuBar.uncheckItem(MI_LEFT);                                           //V3
  171.     menuBar.uncheckItem(MI_RIGHT);                                          //V3
  172.     break;                                                                  //V3
  173.  case right:                                                                //V3
  174.     hello.setAlignment(                                                     //V3
  175.       IStaticText::centerRight);                                            //V3
  176.     statusLine.setText(STR_RIGHT);                                          //V3
  177.     menuBar.uncheckItem(MI_CENTER);                                         //V3
  178.     menuBar.uncheckItem(MI_LEFT);                                           //V3
  179.     menuBar.checkItem(MI_RIGHT);                                            //V3
  180.     break;                                                                  //V3
  181.  }                                                                          //V3
  182.  return (*this);                        //Return a reference to the frame     V3
  183.  
  184. } /* end AHelloWindow :: setAlignment(...) */                               //V3
  185.  
  186. /**************************************************************************   V3
  187. * ACommandHandler :: ACommandHandler - constructor for the command handler*   V3
  188. *       Construct the command handler from a pointer to the AHelloWindow  *   V3
  189. *       that events will be handled for.                                  *   V3
  190. **************************************************************************/ //V3
  191. ACommandHandler :: ACommandHandler(AHelloWindow *helloFrame)                //V3
  192. {                                                                           //V3
  193.    frame=helloFrame;                     //Save frame to be handled           V3
  194. } /* end ACommandHandler :: ACommandHandler(...) */                         //V3
  195.  
  196. /**************************************************************************   V3
  197. * ACommandHandler :: command                                              *   V3
  198. *   Handle menu commands                                                  *   V3
  199. **************************************************************************/ //V3
  200. IBase::Boolean                                                              //V3
  201.   ACommandHandler :: command(ICommandEvent & cmdEvent)                      //V3
  202. {                                                                           //V3
  203.   Boolean eventProcessed(true);         //Assume event will be processed      V3
  204.  
  205. /*--------------------- Process command events ---------------------------|   V3
  206. |  Depending on the command event ID, call the AHelloWindow::setAlignment |   V3
  207. |    function with the appropriate AHelloWorld::Alignment value.          |   V3
  208. |------------------------------------------------------------------------*/ //V3
  209.   switch (cmdEvent.commandId()) {                                           //V3
  210.     case MI_CENTER:                                                         //V3
  211.       frame->setAlignment(AHelloWindow::center);                            //V3
  212.       break;                                                                //V3
  213.     case MI_LEFT:                                                           //V3
  214.       frame->setAlignment(AHelloWindow::left);                              //V3
  215.       break;                                                                //V3
  216.     case MI_RIGHT:                                                          //V3
  217.       frame->setAlignment(AHelloWindow::right);                             //V3
  218.       break;                                                                //V3
  219.  
  220.     default:                            //Otherwise,                          V3
  221.       eventProcessed=false;             //  the event wasn't processed        V3
  222.   } /* end switch */                                                        //V3
  223.  
  224.   return(eventProcessed);                                                   //V3
  225. } /* end ACommandHandler :: command(...) */                                 //V3
  226.