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

  1. /******************************************************************************
  2. * .FILE:         ahellow5.cpp                                                 *
  3. *                                                                             *
  4. * .DESCRIPTION:  Hello World Sample Program Version 5: Class Implementation   *
  5. *                                                                             *
  6. * .CLASSES:      AHelloWindow                                                 *
  7. *                ACommandHandler                                              *
  8. *                ASelectHandler                                               *
  9. *                AHelpHandler                                                 *
  10. *                AEarthWindow                                                 *
  11. *                ATextDialog                                                  *
  12. *                                                                             *
  13. * .COPYRIGHT:                                                                 *
  14. *    Licensed Material - Program-Property of IBM                              *
  15. *    (C) Copyright IBM Corp. 1992, 1996 - All Rights Reserved                 *
  16. *                                                                             *
  17. * .DISCLAIMER:                                                                *
  18. *   The following [enclosed] code is sample code created by IBM               *
  19. *   Corporation.  This sample code is not part of any standard IBM product    *
  20. *   and is provided to you solely for the purpose of assisting you in the     *
  21. *   development of your applications.  The code is provided 'AS IS',          *
  22. *   without warranty of any kind.  IBM shall not be liable for any damages    *
  23. *   arising out of your use of the sample code, even if they have been        *
  24. *   advised of the possibility of such damages.                               *
  25. *                                                                             *
  26. * .NOTE: WE RECOMMEND USING A FIXED SPACE FONT TO LOOK AT THE SOURCE          *
  27. *                                                                             *
  28. ******************************************************************************/
  29. #include <ibase.hpp>
  30. #include <iapp.hpp>
  31. #include <ifont.hpp>
  32. #include <istring.hpp>
  33. #include <imsgbox.hpp>
  34. #include <icoordsy.hpp>
  35. #include "aearthw5.hpp"
  36. #include "ahellow5.hpp"
  37. #include "ahellow5.h"
  38. #include "adialog5.hpp"
  39.  
  40. /**************************************************************************
  41. * main  - Application entry point for Hello World Version 5.              *
  42. *                                                                         *
  43. * Creates a new object mainWindow of class AHelloWindow                   *
  44. * Sets Hello World window alignment                                       *
  45. * Sets the size of mainWindow                                             *
  46. * Sets the window focus to mainWindow                                     *
  47. * Displays the mainWindow                                                 *
  48. * Starts the events processing for the application                        *
  49. **************************************************************************/
  50. int main()
  51. {
  52.   ICoordinateSystem::setApplicationOrientation(
  53.           ICoordinateSystem::originLowerLeft );
  54.   #ifdef USE_IPF
  55.   IHelpWindow::setDefaultStyle( IHelpWindow::defaultStyle()
  56.                                 | IHelpWindow::ipfCompatible );
  57.   #endif
  58.   AHelloWindow mainWindow (WND_MAIN);
  59.   mainWindow.setTextAlignment(AHelloWindow::left);
  60.   mainWindow.sizeTo(ISize(400,300));
  61.   mainWindow.setFocus();
  62.   mainWindow.show();
  63.   IApplication::current().run();
  64.   return 0;
  65. } /* end main */
  66.  
  67. /**************************************************************************
  68. * Class AHelloWindow :: AHelloWindow - Constructor for the main window    *
  69. *                                                                         *
  70. * Construct the IFrameWindow using the default style plus minimizedIcon,  *
  71. *   which gets the Icon identified in the resource file and associates it *
  72. *   with the main window.  The accelerator style causes the accelerator   *
  73. *   table to be loaded from the resource file.                            *
  74. * Create a menu bar object for the main window menu bar that was loaded   *
  75. *   from the resource file.  menuBar is used by setAlignment to           *
  76. *   manipulate check marks on the menu items.                             *
  77. * Create a static text object for displaying the status of the            *
  78. *   Hello World text alignment.                                           *
  79. * Create the clientWindow split canvas to be used as the client window    *
  80. *   of this frame and let its orientation default to vertical.            *
  81. * Create the helloCanvas split canvas making the clientWindow its parent  *
  82. *   window and orient it horizontally.                                    *
  83. * Create the hello and earthWindow static text objects as children of     *
  84. *   the helloCanvas.  hello is placed in the topmost window of the        *
  85. *   horizontal split canvas because it is created first.  earthWindow     *
  86. *   is placed in the bottom window of the split canvas.                   *
  87. * Create the listBox window making the clientWindow its parent window     *
  88. *   and enabling it for tabbing and preventing it from being resized      *
  89. *   due to an item being too long.  helloCanvas is placed in the          *
  90. *   leftmost window of the vertical split canvas because it is created    *
  91. *   first.  listBox is placed in the right window of the split canvas.    *
  92. * Create a set canvas that will contain the push buttons and will be      *
  93. *   added to the frame as an extension.                                   *
  94. * Create each push button with the set canvas as parent and owner.        *
  95. * Additionally for Help, specify help style and noPointerFocus style.     *
  96. *   noPointerFocus prevents the mouse from changing the input focus       *
  97. *   to the Help push button when you select it using the mouse.  This     *
  98. *   allows you to use contextual help for the control with the input      *
  99. *   focus rather than for the Help push button itself.                    *
  100. * Create the Hello World information area object from IInfoArea class.    *
  101. *   The information area is automatically added as an extension below     *
  102. *   the client window of the frame.                                       *
  103. * Create a command handler to process command events from menu item,      *
  104. *   push button, and accelerator key selections.                          *
  105. * Create a select handler to process selections made in the list box.     *
  106. * Create an IHelpWindow object that is displayed when help is requested   *
  107. *   from a help menu item, the help push button, or the help accelerator  *
  108. *   key, normally F1.                                                     *
  109. * An IHelpHandler object, helpHandler is created implicitly, see class    *
  110. *   definition for AHelloWindow in ahellow5.hpp.                          *
  111. **************************************************************************/
  112.  
  113. AHelloWindow :: AHelloWindow(unsigned long windowId)
  114.   : IFrameWindow(IFrameWindow::defaultStyle() |
  115.                  IFrameWindow::minimizedIcon  |
  116.                  IFrameWindow::accelerator,
  117.                  windowId)
  118.    ,menuBar(windowId, this)
  119.    ,clientWindow(WND_CANVAS, this, this)
  120.    ,helloCanvas(WND_HCANVAS,
  121.                 &clientWindow,
  122.                 &clientWindow,
  123.                 IRectangle(),
  124.                 IWindow::visible |
  125.                 ISplitCanvas::horizontal)
  126.    ,hello(WND_HELLO,
  127.           &helloCanvas,
  128.           &helloCanvas)
  129.    ,statusLine(WND_STATUS, this, this)
  130.    ,earthWindow(WND_EARTH, &helloCanvas)
  131.    ,listBox(WND_LISTBOX,
  132.             &clientWindow,
  133.             &clientWindow,
  134.             IRectangle(),
  135.             IListBox::defaultStyle() |
  136.             IControl::tabStop |
  137.             IListBox::noAdjustPosition)
  138.    ,buttons(WND_BUTTONS, this, this)
  139.    ,leftButton(MI_LEFT, &buttons, &buttons)
  140.    ,centerButton(MI_CENTER, &buttons, &buttons)
  141.    ,rightButton(MI_RIGHT, &buttons, &buttons)
  142.    ,helpButton(MI_HELP,
  143.                &buttons,
  144.                &buttons, IRectangle(),
  145.                IPushButton::defaultStyle() |
  146.                IPushButton::help |
  147.                IButton::noPointerFocus)
  148.    ,infoArea(this)
  149.    ,commandHandler(this)
  150.    ,selectHandler(this)
  151.    ,helpWindow(HELP_TABLE,this)
  152. {
  153.  
  154. /*------------------------------------------------------------------------|
  155. |  Set the clientWindow split canvas as the client window for the         |
  156. |    AHelloWorld frame.                                                   |
  157. |  Set the HelloCanvas object to occupy 60% of the client canvas.         |
  158. |  Set the list box object to occupy 40% of the client canvas.            |
  159. |------------------------------------------------------------------------*/
  160.   setClient(&clientWindow);
  161.   clientWindow.setSplitWindowPercentage(&helloCanvas, 60);
  162.   clientWindow.setSplitWindowPercentage(&listBox, 40);
  163.  
  164. /*------------------------------------------------------------------------|
  165. |  Add the status line as an extension to the frame above the client      |
  166. |    window with the height calculated from the maximum height of a       |
  167. |    character in the current font.                                       |
  168. |------------------------------------------------------------------------*/
  169.   addExtension(&statusLine, IFrameWindow::aboveClient,
  170.                  IFont(&statusLine).maxCharHeight());
  171.  
  172. /*------------------------------------------------------------------------|
  173. |  Add the different language versions of Hello World to the list box in  |
  174. |    ascending order.  Hello World text strings are stored in the         |
  175. |    resource file with IDs beginning with HI_WORLD with each subsequent  |
  176. |    ID incremented up to HI_WORLD + HI_COUNT - 1.                        |
  177. |  Have the select handler handle selections made in the list box.        |
  178. |    The select handler's selected() function is called to process        |
  179. |    list box selections.                                                 |
  180. |------------------------------------------------------------------------*/
  181.   for (int i=0;i<HI_COUNT;i++ )
  182.      listBox.addAscending(HI_WORLD+i);
  183.   selectHandler.handleEventsFor(&listBox);
  184.  
  185. /*------------------------------------------------------------------------|
  186. |  Change the size of the information area using the maximum height of    |
  187. |    a character in the current font.                                     |
  188. |------------------------------------------------------------------------*/
  189.   setExtensionSize(&infoArea, IFont(&infoArea).maxCharHeight());
  190.  
  191. /*------------------------------------------------------------------------|
  192. |  Set the values for the text controls from strings in the resource file.|
  193. |    The infoArea inactive text is displayed when no menu item is active. |
  194. |------------------------------------------------------------------------*/
  195.   hello.setText(STR_HELLO);
  196.   leftButton.setText(STR_LEFTB);
  197.   centerButton.setText(STR_CENTERB);
  198.   rightButton.setText(STR_RIGHTB);
  199.   helpButton.setText(STR_HELPB);
  200.   infoArea.setInactiveText(STR_INFO);
  201.  
  202. /*------------------------------------------------------------------------|
  203. |  Enable the first button as a tab stop.                                 |
  204. |  Set the canvas margins and padding between buttons to zero.            |
  205. |  Add the set canvas that contains the push buttons to the frame window  |
  206. |    as an extension immediately below the client window with a height    |
  207. |    equal to the minimum height of the push buttons.  You must set the   |
  208. |    text in the push buttons first.                                      |
  209. |------------------------------------------------------------------------*/
  210.   leftButton.enableTabStop();
  211.   buttons.setMargin(ISize());
  212.   buttons.setPad(ISize());
  213.   addExtension(&buttons, IFrameWindow::belowClient,
  214.                 (unsigned long)buttons.minimumSize().height());
  215.  
  216. /*------------------------------------------------------------------------|
  217. |  Have the command handler handle commands sent from the frame window.   |
  218. |    The command handler's command() function is called to process        |
  219. |    menu item, push button, and accelerator key selections.              |
  220. |------------------------------------------------------------------------*/
  221.   commandHandler.handleEventsFor(this);
  222.  
  223. /*------------------------------------------------------------------------|
  224. |   Add the help library to the help window using addLibraries().         |
  225. |   Set the help window title from a string in the resource file.         |
  226. |   Begin handling help events for the frame window.                      |
  227. |------------------------------------------------------------------------*/
  228.    try
  229.    {
  230.       helpWindow.addLibraries( "ahellow5.hlp" );
  231.       helpWindow.setTitle(STR_HTITLE);
  232.       helpHandler.handleEventsFor(this);
  233.    }
  234.    catch( ... )
  235.    {
  236.       IMessageBox
  237.          msgBox( this );
  238.       msgBox.show( STR_HELP_NOT_FOUND, IMessageBox::warning );
  239.    }
  240.  
  241. /*------------------------------------------------------------------------|
  242. | Align the static text, set the status line, and set the check mark in   |
  243. |   the menu bar.                                                         |
  244. |------------------------------------------------------------------------*/
  245.   setTextAlignment(center);
  246.  
  247. } /* end AHelloWindow :: AHelloWindow(...) */
  248.  
  249. /**************************************************************************
  250. * Class AHelloWindow :: ~AHelloWindow - Destructor for the main window    *
  251. *                                                                         *
  252. * Stop handling command events for the frame.                             *
  253. * Stop handling select events for the list box.                           *
  254. * Stop handling help events for the frame.                                *
  255. **************************************************************************/
  256. AHelloWindow :: ~AHelloWindow()
  257. {
  258.   commandHandler.stopHandlingEventsFor(this);
  259.   selectHandler.stopHandlingEventsFor(&listBox);
  260.   helpHandler.stopHandlingEventsFor(this);
  261.  
  262. } /* end AHelloWindow :: ~AHelloWindow() */
  263.  
  264. /**************************************************************************
  265. * Class AHelloWindow :: setAlignment - Align static text in client window *
  266. **************************************************************************/
  267. AHelloWindow &
  268.   AHelloWindow :: setTextAlignment(const Alignment alignment)
  269. {
  270. /*------------------------------------------------------------------------|
  271. |  Depending on the value passed, update the window as follows:           |
  272. |    Set the alignment of the static text control in the client window.   |
  273. |    Set the text of the alignment status line static text control.       |
  274. |    Check the selected menu item; remove check marks from the other two. |
  275. |------------------------------------------------------------------------*/
  276.   switch(alignment)
  277.   {
  278.   case left:
  279.     hello.setAlignment(IStaticText::centerLeft);
  280.     statusLine.setText(STR_LEFT);
  281.     menuBar.uncheckItem(MI_CENTER);
  282.     menuBar.checkItem(MI_LEFT);
  283.     menuBar.uncheckItem(MI_RIGHT);
  284.     break;
  285.   case center:
  286.     hello.setAlignment(IStaticText::centerCenter);
  287.     statusLine.setText(STR_CENTER);
  288.     menuBar.checkItem(MI_CENTER);
  289.     menuBar.uncheckItem(MI_LEFT);
  290.     menuBar.uncheckItem(MI_RIGHT);
  291.     break;
  292.  case right:
  293.     hello.setAlignment(IStaticText::centerRight);
  294.     statusLine.setText(STR_RIGHT);
  295.     menuBar.uncheckItem(MI_CENTER);
  296.     menuBar.uncheckItem(MI_LEFT);
  297.     menuBar.checkItem(MI_RIGHT);
  298.     break;
  299.  }
  300.  return (*this);                        //Return a reference to the frame
  301.  
  302. } /* end AHelloWindow :: setAlignment(...) */
  303.  
  304. /**************************************************************************
  305. * Class AHelloWindow :: editText -- Creates and shows a dialog window     *
  306. *   for inputting text that will be used  to replace the text string      *
  307. **************************************************************************/
  308. AHelloWindow &
  309.   AHelloWindow :: editText()
  310. {
  311. /*------------------------------------------------------------------------|
  312. |  Store the current value of the text to be changed.                     |
  313. |  Set the text in the information area from the dialog information       |
  314. |    string in the resource file.                                         |
  315. |------------------------------------------------------------------------*/
  316.   IString textValue(hello.text());
  317.   infoArea.setInactiveText(STR_INFODLG);
  318.  
  319. /*------------------------------------------------------------------------|
  320. |  Create a new text dialog with textValue as the string to edit and      |
  321. |    AHelloWindow as the owner window.                                    |
  322. |  Show the dialog modally.  This means that the owner window cannot have |
  323. |    the focus back until the dialog is ended.                            |
  324. |------------------------------------------------------------------------*/
  325.   ATextDialog textDialog(textValue,this);
  326.   textDialog.showModally();
  327.  
  328. /*------------------------------------------------------------------------|
  329. |  If the OK button was used to end the dialog, then the static text,     |
  330. |    hello, is set to the textValue string.  Else, it is not changed.     |
  331. |  Reset the information area inactive text.                              |
  332. |------------------------------------------------------------------------*/
  333.   if (textDialog.result() == DID_OK)
  334.         hello.setText(textValue);
  335.   infoArea.setInactiveText(STR_INFO);
  336.  
  337.   return (*this);                       //Return a reference to the frame
  338. }  /* end AHelloWindow :: editText() */
  339.  
  340. /**************************************************************************
  341. * Class AHelloWindow :: setTextFromListBox -- Public function used by     *
  342. *   non-AHelloWindow functions to set the Hello World text from the first *
  343. *   selected item in the AHelloWindow listBox.                            *
  344. **************************************************************************/
  345. AHelloWindow &
  346.   AHelloWindow :: setTextFromListBox()
  347. {
  348.  
  349. /*------------------------------------------------------------------------|
  350. |  Create a cursor to the list box.  Using the default filter for a       |
  351. |    list box cursor, selectedItems, causes the setToFirst() function     |
  352. |    to position the cursor to the first selected item.                   |
  353. |  Set the hello IStaticText control text value.                          |
  354. |------------------------------------------------------------------------*/
  355.   IListBox::Cursor lbCursor(listBox);
  356.   lbCursor.setToFirst();
  357.   hello.setText(listBox.elementAt(lbCursor));
  358.  
  359.   return (*this);                       //Return a reference to the frame
  360. }  /* end AHelloWindow :: setTextFromListBox() */
  361.  
  362. /**************************************************************************
  363. * Class ACommandHandler :: ACommandHandler - constructor for the command  *
  364. *   handler                                                               *
  365. *                                                                         *
  366. * Construct the command handler from a pointer to the AHelloWindow that   *
  367. *   events will be handled for.                                           *
  368. **************************************************************************/
  369. ACommandHandler :: ACommandHandler(AHelloWindow *helloFrame)
  370.    : frame(helloFrame)
  371. {
  372. } /* end ACommandHandler :: ACommandHandler(...) */
  373.  
  374. /**************************************************************************
  375. * Class ACommandHandler :: command - Handle menu and button commands      *
  376. **************************************************************************/
  377. IBase::Boolean
  378.   ACommandHandler :: command(ICommandEvent & cmdEvent)
  379. {
  380.   Boolean eventProcessed(true);         //Assume event will be processed
  381.  
  382. /*------------------------------------------------------------------------|
  383. |  Depending on the command event ID, call the AHelloWindow::setAlignment |
  384. |    function with the appropriate AHelloWorld::Alignment value.          |
  385. |  Do this except when the Text menu item is selected;                    |
  386. |    then call the AHelloWindow::editText function for changing the       |
  387. |    Hello World text using a dialog.                                     |
  388. |------------------------------------------------------------------------*/
  389.   switch (cmdEvent.commandId()) {
  390.     case MI_CENTER:
  391.       frame->setTextAlignment(AHelloWindow::center);
  392.       break;
  393.     case MI_LEFT:
  394.       frame->setTextAlignment(AHelloWindow::left);
  395.       break;
  396.     case MI_RIGHT:
  397.       frame->setTextAlignment(AHelloWindow::right);
  398.       break;
  399.     case MI_TEXT:
  400.       frame->editText();
  401.       break;
  402.     default:
  403. /*------------------------------------------------------------------------|
  404. | The event was not processed                                             |
  405. -------------------------------------------------------------------------*/
  406.       eventProcessed=false;
  407.   } /* end switch */
  408.  
  409.   return(eventProcessed);
  410. } /* end ACommandHandler :: command(...) */
  411.  
  412. /**************************************************************************
  413. * Class ASelectHandler :: ASelectHandler - constructor for the select     *
  414. *   handler                                                               *
  415. *                                                                         *
  416. * Construct the select handler from a pointer to the AHelloWindow         *
  417. *   that will be changed as a result of the selection.                    *
  418. **************************************************************************/
  419. ASelectHandler :: ASelectHandler(AHelloWindow *helloFrame)
  420.    :frame(helloFrame)
  421. {
  422. } /* end ASelectHandler :: ASelectHandler(...) */
  423.  
  424.  
  425. /**************************************************************************
  426. * Class ASelectHandler :: selected - Handle items selected within the     *
  427. *   list box.                                                             *
  428. **************************************************************************/
  429. IBase::Boolean
  430.   ASelectHandler :: selected(IControlEvent & evt)
  431. {
  432.  
  433. /*------------------------------------------------------------------------|
  434. |  Call the AHelloWindow::setTextFromListBox function for the frame used  |
  435. |    to construct this select handler.                                    |
  436. |------------------------------------------------------------------------*/
  437.   frame->setTextFromListBox();
  438.  
  439.   return (true);                        //Event is always processed
  440. } /* end ASelectHandler :: selected(...) */
  441.  
  442. /**************************************************************************
  443. * Class AHelpHandler :: keysHelpId - Handle the keys help request event   *
  444. **************************************************************************/
  445. IBase::Boolean
  446.   AHelpHandler :: keysHelpId(IEvent& evt)
  447. {
  448.   evt.setResult(1000);                  //1000=keys help ID in
  449.                                         //  ahellow5.ipf file
  450.  
  451.   return (true);                        //Event is always processed
  452. } /* end AHelpHandler :: keysHelpId(...) */
  453.