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

  1. /******************************************************************************
  2. * .FILE:         ahellow4.cpp                                                 *
  3. *                                                                             *
  4. * .DESCRIPTION:  Hello World Sample Program Version 3:  Class Implementation  *
  5. *                                                                             *
  6. * .CLASSES:      ACommandHandler                                              *
  7. *                AHellowWindow                                                *
  8. *                ATextDialog                                                  *
  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 <ifont.hpp>
  29. #include <istring.hpp>
  30. #include <icoordsy.hpp>
  31. #include "ahellow4.hpp"
  32. #include "ahellow4.h"
  33. #include "adialog4.hpp"
  34.  
  35. /**************************************************************************
  36. * main  - Application entry point for Hello World Version 4.              *
  37. *                                                                         *
  38. * Creates a new object mainWindow of class AHelloWindow                   *
  39. * Sets Hello World window alignment                                       *
  40. * Sets the size of mainWindow                                             *
  41. * Sets the window focus to mainWindow                                     *
  42. * Displays the mainWindow                                                 *
  43. * Starts the events processing for the application                        *
  44. **************************************************************************/
  45. int main()
  46. {
  47.   ICoordinateSystem::setApplicationOrientation(
  48.           ICoordinateSystem::originLowerLeft );
  49.   AHelloWindow mainWindow (WND_MAIN);
  50.   mainWindow.setTextAlignment(AHelloWindow::left);
  51.   mainWindow.sizeTo(ISize(400,300));
  52.   mainWindow.setFocus();
  53.   mainWindow.show();
  54.   IApplication::current().run();
  55.   return 0;
  56. } /* end main */
  57.  
  58. /**************************************************************************
  59. * Class AHelloWindow :: AHelloWindow - Constructor for the main window    *
  60. *                                                                         *
  61. * Define itself as an IFrameWindow                                        *
  62. * Create a menu bar                                                       *
  63. * Create status line                                                      *
  64. * Create static text in client window.                                    *
  65. * Create a set canvas that contains the push buttons                      *
  66. * Create the push buttons that are in the set canvas                      *
  67. * Create information area object                                          *
  68. * Create a command handler to process command events from                 *
  69. **************************************************************************/
  70.  
  71. AHelloWindow :: AHelloWindow(const unsigned long windowId)
  72.   :IFrameWindow(IFrameWindow::defaultStyle() |
  73.                 IFrameWindow::minimizedIcon  |
  74.                 IFrameWindow::accelerator,
  75.                 windowId)
  76.   ,menuBar(windowId, this)
  77.   ,statusLine(WND_STATUS, this, this)
  78.   ,hello(WND_HELLO, this, this)
  79.   ,buttons(WND_BUTTONS, this, this)
  80.   ,leftButton(MI_LEFT, &buttons, &buttons)
  81.   ,centerButton(MI_CENTER, &buttons, &buttons)
  82.   ,rightButton(MI_RIGHT, &buttons, &buttons)
  83.   ,infoArea(this)
  84.   ,commandHandler(this)
  85. {
  86.  
  87. /*------------------------------------------------------------------------|
  88. |  Set the hello world static text window as the client window.           |
  89. |------------------------------------------------------------------------*/
  90.   setClient(&hello);
  91.  
  92. /*------------------------------------------------------------------------|
  93. |  Add the status line as an extension to the frame above the client      |
  94. |    window with the height calculated from the maximum height of a       |
  95. |    character in the current font.                                       |
  96. |------------------------------------------------------------------------*/
  97.   addExtension(&statusLine, IFrameWindow::aboveClient,
  98.                  IFont(&statusLine).maxCharHeight());
  99.  
  100. /*------------------------------------------------------------------------|
  101. |  Set the values for the text controls from strings in the resource file.|
  102. |    The infoArea inactive text is displayed when no menu item is active. |
  103. |------------------------------------------------------------------------*/
  104.   hello.setText(STR_HELLO);
  105.   leftButton.setText(STR_LEFTB);
  106.   centerButton.setText(STR_CENTERB);
  107.   rightButton.setText(STR_RIGHTB);
  108.   infoArea.setInactiveText(STR_INFO);
  109.  
  110. /*------------------------------------------------------------------------|
  111. |  Enable the first button as a tab stop.                                 |
  112. |  Set the canvas margins and padding between buttons to zero.            |
  113. |  Add the set canvas that contains the push buttons to the frame window  |
  114. |    as an extension immediately below the client window with a height    |
  115. |    equal to the minimum height of the push buttons.  You must set the   |
  116. |    text in the push buttons first.                                      |
  117. |------------------------------------------------------------------------*/
  118.   leftButton.enableTabStop();
  119.   buttons.setMargin(ISize());
  120.   buttons.setPad(ISize());
  121.   addExtension(&buttons, IFrameWindow::belowClient,
  122.                 (unsigned long)buttons.minimumSize().height());
  123.  
  124. /*------------------------------------------------------------------------|
  125. |  Have the command handler handle commands sent from the frame window.   |
  126. |    The command handler's command() function is called to process        |
  127. |    menu item, push button, and accelerator key selections.              |
  128. |------------------------------------------------------------------------*/
  129.   commandHandler.handleEventsFor(this);
  130.  
  131. /*------------------------------------------------------------------------|
  132. | Align the static text, set the status line, and set the check mark in   |
  133. |   the menu bar.                                                         |
  134. |------------------------------------------------------------------------*/
  135.   setTextAlignment(center);
  136.  
  137. } /* end AHelloWindow :: AHelloWindow(...) */
  138.  
  139. /**************************************************************************
  140. * Class AHelloWindow :: ~AHelloWindow - Destructor for the main window    *
  141. **************************************************************************/
  142. AHelloWindow :: ~AHelloWindow()
  143. {
  144.   commandHandler.stopHandlingEventsFor(this);
  145.  
  146. } /* end AHelloWindow :: ~AHelloWindow() */
  147.  
  148. /**************************************************************************
  149. * Class AHelloWindow :: setTextAlignment - Align static text in client    *
  150. *   window                                                                *
  151. **************************************************************************/
  152. AHelloWindow &
  153.   AHelloWindow :: setTextAlignment(const Alignment alignment)
  154. {
  155. /*------------------------------------------------------------------------|
  156. |  Depending on the value passed, update the window as follows:           |
  157. |    Set the alignment of the static text control in the client window.   |
  158. |    Set the text of the alignment status line static text control.       |
  159. |    Check the selected menu item; remove check marks from the other two. |
  160. |------------------------------------------------------------------------*/
  161.   switch(alignment)
  162.   {
  163.   case left:
  164.     hello.setAlignment(IStaticText::centerLeft);
  165.     statusLine.setText(STR_LEFT);
  166.     menuBar.uncheckItem(MI_CENTER);
  167.     menuBar.checkItem(MI_LEFT);
  168.     menuBar.uncheckItem(MI_RIGHT);
  169.     break;
  170.   case center:
  171.     hello.setAlignment(IStaticText::centerCenter);
  172.     statusLine.setText(STR_CENTER);
  173.     menuBar.checkItem(MI_CENTER);
  174.     menuBar.uncheckItem(MI_LEFT);
  175.     menuBar.uncheckItem(MI_RIGHT);
  176.     break;
  177.  case right:
  178.     hello.setAlignment(IStaticText::centerRight);
  179.     statusLine.setText(STR_RIGHT);
  180.     menuBar.uncheckItem(MI_CENTER);
  181.     menuBar.uncheckItem(MI_LEFT);
  182.     menuBar.checkItem(MI_RIGHT);
  183.     break;
  184.  }
  185.  return (*this);                        //Return a reference to the frame
  186.  
  187. } /* end AHelloWindow :: setAlignment(...) */
  188.  
  189. /**************************************************************************
  190. * Class AHelloWindow :: editText -- Creates a dialog window to edit the   *
  191. *   "Hello World!!" static text string                                    *
  192. **************************************************************************/
  193. AHelloWindow &
  194.   AHelloWindow :: editText()
  195. {
  196. /*------------------------------------------------------------------------|
  197. |  Store the current value of the text to be changed.                     |
  198. |  Set the text in the information area from the dialog information       |
  199. |    string in the resource file.                                         |
  200. |------------------------------------------------------------------------*/
  201.   IString textValue(hello.text());
  202.   infoArea.setInactiveText(STR_INFODLG);
  203.  
  204. /*------------------------------------------------------------------------|
  205. |  Create a new text dialog with textValue as the string to edit and      |
  206. |    AHelloWindow as the owner window.                                    |
  207. |  Show the dialog modally.  This means that the owner window cannot have |
  208. |    the focus back until the dialog is ended.                            |
  209. |------------------------------------------------------------------------*/
  210.   ATextDialog textDialog(textValue,this);
  211.   textDialog.showModally();
  212.  
  213. /*------------------------------------------------------------------------|
  214. |  If the OK button was used to end the dialog, then the static text,     |
  215. |    hello, is set to the textValue string.  Else, it is not changed.     |
  216. |  Reset the information area inactive text.                              |
  217. |------------------------------------------------------------------------*/
  218.   if (textDialog.result() == DID_OK)
  219.         hello.setText(textValue);
  220.   infoArea.setInactiveText(STR_INFO);
  221.  
  222.   return (*this);                       //Return a reference to the frame
  223. }  /* end AHelloWindow :: editText() */
  224.  
  225. /**************************************************************************
  226. * Class ACommandHandler :: ACommandHandler - Constructor for the Command  *
  227. *   Handler for the main window.                                          *
  228. *                                                                         *
  229. * Store the frame that events will be handled for                         *
  230. **************************************************************************/
  231. ACommandHandler :: ACommandHandler(AHelloWindow *helloFrame)
  232.   :frame(helloFrame)
  233. {
  234. } /* end ACommandHandler :: ACommandHandler(...) */
  235.  
  236. /**************************************************************************
  237. * Class ACommandHandler :: command -- Handles menu and button commands    *
  238. *   for the main window.                                                  *
  239. **************************************************************************/
  240. IBase::Boolean ACommandHandler :: command(ICommandEvent & cmdEvent)
  241. {
  242.   Boolean eventProcessed(true);         //Assume event will be processed
  243.  
  244. /*------------------------------------------------------------------------|
  245. |  Depending on the command event ID, call the AHelloWindow::setAlignment |
  246. |    function with the appropriate AHelloWorld::Alignment value.          |
  247. |  Do this except when the Text menu item is selected;                    |
  248. |    then call the AHelloWindow::editText function for changing the       |
  249. |    Hello World text using a dialog.                                     |
  250. |------------------------------------------------------------------------*/
  251.   switch (cmdEvent.commandId()) {
  252.     case MI_CENTER:
  253.       frame->setTextAlignment(AHelloWindow::center);
  254.       break;
  255.     case MI_LEFT:
  256.       frame->setTextAlignment(AHelloWindow::left);
  257.       break;
  258.     case MI_RIGHT:
  259.       frame->setTextAlignment(AHelloWindow::right);
  260.       break;
  261.     case MI_TEXT:
  262.       frame->editText();
  263.       break;
  264.  
  265.     default:
  266. /*------------------------------------------------------------------------|
  267. | The event was not processed                                             |
  268. |------------------------------------------------------------------------*/
  269.       eventProcessed=false;
  270.   } /* end switch */
  271.  
  272.   return(eventProcessed);
  273. } /* end ACommandHandler :: command(...) */
  274.