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

  1. /******************************************************************************
  2. * .FILE:         ahellow6.cpp                                                 *
  3. *                                                                             *
  4. * .DESCRIPTION:  Hello World Sample Program Version 6: Class Implementation   *
  5. *                                                                             *
  6. * .CLASSES:      AEarthWindow                                                 *
  7. *                ANoteBookWindow                                              *
  8. *                ASelectHandler                                               *
  9. *                AHelpHandler                                                 *
  10. *                AHelloTimeHandler                                            *
  11. *                APopUpHandler                                                *
  12. *                ANotebookWindow                                              *
  13. *                ACommandHandler                                              *
  14. *                                                                             *
  15. * .COPYRIGHT:                                                                 *
  16. *    Licensed Material - Program-Property of IBM                              *
  17. *    (C) Copyright IBM Corp. 1992, 1996 - All Rights Reserved                 *
  18. *                                                                             *
  19. * .DISCLAIMER:                                                                *
  20. *   The following [enclosed] code is sample code created by IBM               *
  21. *   Corporation.  This sample code is not part of any standard IBM product    *
  22. *   and is provided to you solely for the purpose of assisting you in the     *
  23. *   development of your applications.  The code is provided 'AS IS',          *
  24. *   without warranty of any kind.  IBM shall not be liable for any damages    *
  25. *   arising out of your use of the sample code, even if they have been        *
  26. *   advised of the possibility of such damages.                               *
  27. *                                                                             *
  28. * .NOTE: WE RECOMMEND USING A FIXED SPACE FONT TO LOOK AT THE SOURCE          *
  29. *                                                                             *
  30. ******************************************************************************/
  31. #include <iapp.hpp>
  32. #include <ifont.hpp>
  33. #include <istring.hpp>
  34. #include <idate.hpp>
  35. #include <itime.hpp>
  36. #include <imsgbox.hpp>
  37. #include <iprofile.hpp>
  38. #include <ifontdlg.hpp>
  39. #include <stdlib.h>
  40. #include <icoordsy.hpp>
  41.  
  42. #include "aearthw6.hpp"
  43. #include "ahellow6.hpp"
  44. #include "ahellow6.h"
  45. #include "adialog6.hpp"
  46. #define HELP_LIBRARIES "ahellow6.hlp"
  47. #define APPLICATION_NAME "Hello6"
  48.  
  49.  
  50. /**************************************************************************
  51. * main  - Application entry point for Hello World Version 6.              *
  52. *                                                                         *
  53. * Seeds the random number generator                                       *                                                                         *
  54. * Creates a new object mainWindow of class AHelloWindow                   *
  55. * Sets Hello World window alignment                                       *
  56. * Sets the size of mainWindow                                             *
  57. * Sets the window focus to mainWindow                                     *
  58. * Displays the mainWindow                                                 *
  59. * Starts the events processing for the application                        *
  60. **************************************************************************/
  61. int main()
  62. {
  63.   ICoordinateSystem::setApplicationOrientation(
  64.           ICoordinateSystem::originLowerLeft );
  65.   srand(time(NULL));
  66.   #ifdef USE_IPF
  67.   IHelpWindow::setDefaultStyle( IHelpWindow::defaultStyle()
  68.                                 | IHelpWindow::ipfCompatible );
  69.   #endif
  70.   AHelloWindow mainWindow (WND_MAIN);
  71.   mainWindow.setTextAlignment(AHelloWindow::left);
  72.   mainWindow.sizeTo(ISize(400,300));
  73.   mainWindow.setFocus();
  74.   mainWindow.show();
  75.   IApplication::current().run();
  76.   return 0;
  77. } /* end main */
  78.  
  79. /**************************************************************************
  80. * AHelloWindow :: AHelloWindow - Constructor for the main window          *
  81. *                                                                         *
  82. * Construct the IFrameWindow using the default style plus minimizedIcon,  *
  83. *   which gets the Icon identified in the resource file and associates it *
  84. *   with the main window.  The accelerator style causes the accelerator   *
  85. *   table to be loaded from the resource file.                            *
  86. *  Create a menu bar object for the main window menu bar that was loaded  *
  87. *    from the resource file.  menuBar is used by setAlignment to          *
  88. *    manipulate check marks on the menu items.                            *
  89. *  Create the statusCanvas split canvas making the AHelloWindow its       *
  90. *    parent window and let its orientation default to vertical.           *
  91. *  Create a static text object for displaying the status of the           *
  92. *    Hello World text alignment and make it the first child of the        *
  93. *    statusCanvas.  It will be the leftmost window of the statusCanvas.   *
  94. *  Create static text objects for displaying the date and time and        *
  95. *    make them children of the statusCanvas.  The date will be in the     *
  96. *    center window of the canvas while the time will be on the right.     *
  97. *  Create the clientWindow split canvas to be used as the client window   *
  98. *    of this frame and let its orientation default to vertical.           *
  99. *  Create the helloCanvas split canvas making the clientWindow its parent *
  100. *    window and orient it horizontally.                                   *
  101. *  Create the hello and earthWindow static text objects as children of    *
  102. *    the helloCanvas.  hello is placed in the topmost window of the       *
  103. *    horizontal split canvas because it is created first.  earthWindow    *
  104. *    is placed in the bottom window of the split canvas.                  *
  105. *  Create the listBox window making the clientWindow its parent window    *
  106. *    and enabling it for tabbing and preventing it from being resized     *
  107. *    due to an item being too long.  helloCanvas is placed in the         *
  108. *    leftmost window of the vertical split canvas because it is created   *
  109. *    first.  listBox is placed in the right window of the split canvas.   *
  110. *  Create a set canvas that will contain the push buttons and will be     *
  111. *    added to the frame as an extension.                                  *
  112. *  Create each push button with the set canvas as parent and owner.       *
  113. *  Additionally for Help, specify help style and noPointerFocus style.    *
  114. *    noPointerFocus prevents the mouse from changing the input focus      *
  115. *    to the Help push button when you select it using the mouse.  This    *
  116. *    allows you to use contextual help for the control with the input     *
  117. *    focus rather than for the Help push button itself.                   *
  118. *  Create the Hello World information area object from IInfoArea class.   *
  119. *    The information area is automatically added as an extension below    *
  120. *    the client window of the frame.                                      *
  121. *  Create a command handler to process command events from menu item,     *
  122. *    push button, and accelerator key selections.                         *
  123. *  Create a select handler to process selections made in the list box.    *
  124. *  Create an IHelpWindow object that is displayed when help is requested  *
  125. *    from a help menu item, the help push button, or the help accelerator *
  126. *    key, normally F1.                                                    *
  127. *  An IHelpHandler object, helpHandler is created implicitly, see class   *
  128. *    definition for AHelloWindow in ahellow5.hpp.                         *
  129. *  Initialize the date and time boolean objects that contain the state of *
  130. *    the data and time formats.                                           *
  131. **************************************************************************/
  132. AHelloWindow :: AHelloWindow(unsigned long windowId)
  133.   : IFrameWindow(IFrameWindow::defaultStyle() |
  134.                  IFrameWindow::minimizedIcon  |
  135.                  IFrameWindow::accelerator,
  136.                  windowId)
  137.    ,menuBar(windowId, this)
  138.    ,statusCanvas(WND_STATUSCANVAS,this, this)
  139.    ,statusLine(WND_STATUS, &statusCanvas, &statusCanvas)
  140.    ,statusDate(WND_DATE, &statusCanvas, &statusCanvas)
  141.    ,statusTime(WND_TIME, &statusCanvas, &statusCanvas)
  142.    ,clientWindow(WND_CANVAS, this, this)
  143.    ,helloCanvas(WND_HCANVAS,
  144.                 &clientWindow,
  145.                 &clientWindow,
  146.                 IRectangle(),
  147.                 IWindow::visible |
  148.                 ISplitCanvas::horizontal)
  149.    ,hello(WND_HELLO, &helloCanvas, &helloCanvas)
  150.    ,earthWindow(WND_EARTH, &helloCanvas)
  151.    ,listBox(WND_LISTBOX,
  152.             &clientWindow,
  153.             &clientWindow,
  154.             IRectangle(),
  155.             IListBox::defaultStyle() |
  156.             IControl::tabStop |
  157.             IListBox::noAdjustPosition)
  158.    ,buttons(WND_BUTTONS, this, this)
  159.    ,leftButton(MI_LEFT, &buttons, &buttons)
  160.    ,centerButton(MI_CENTER, &buttons, &buttons)
  161.    ,rightButton(MI_RIGHT, &buttons, &buttons)
  162.    ,helpButton(MI_HELP,
  163.                &buttons,
  164.                &buttons,
  165.                IRectangle(),
  166.                IPushButton::defaultStyle() |
  167.                IPushButton::help           |
  168.                IButton::noPointerFocus)
  169.    ,infoArea(this)
  170.    ,commandHandler(this)
  171.    ,selectHandler(this)
  172.    ,helpWindow(HELP_TABLE,this)
  173.    ,dateLong(false)
  174.    ,timeLong(true)
  175.    ,settingsNotebook( 0 )
  176. {
  177. /*------------------------------------------------------------------------|
  178. |  Set the clientWindow split canvas as the client window for the         |
  179. |    AHelloWorld frame.                                                   |
  180. |  Set the HelloCanvas object to occupy 60% of the client canvas.         |
  181. |  Set the list box object to occupy 40% of the client canvas.            |
  182. |------------------------------------------------------------------------*/
  183.   setClient(&clientWindow);
  184.   clientWindow.setSplitWindowPercentage(&helloCanvas, 60);
  185.   clientWindow.setSplitWindowPercentage(&listBox, 40);
  186.  
  187. /*------------------------------------------------------------------------|
  188. |  Add the status area as an extension to the frame above the client      |
  189. |    window with the height calculated from the maximum height of a       |
  190. |    character in the current font.                                       |
  191. |  Start processing timer events for updating the time.                   |
  192. |------------------------------------------------------------------------*/
  193.   addExtension(&statusCanvas, IFrameWindow::aboveClient,
  194.                  IFont(&statusLine).maxCharHeight());
  195.  
  196. /*------------------------------------------------------------------------|
  197. |  Add the different language versions of Hello World to the list box in  |
  198. |    ascending order.  Hello World text strings are stored in the         |
  199. |    resource file with IDs beginning with HI_WORLD with each subsequent  |
  200. |    ID incremented up to HI_WORLD + HI_COUNT - 1.                        |
  201. |  Have the select handler handle selections made in the list box.        |
  202. |    The select handler's selected() function is called to process        |
  203. |    list box selections.                                                 |
  204. |------------------------------------------------------------------------*/
  205.   for (int i=0;i<HI_COUNT;i++ )
  206.      listBox.addAscending(HI_WORLD+i);
  207.   selectHandler.handleEventsFor(&listBox);
  208.  
  209. /*------------------------------------------------------------------------|
  210. |  Change the size of the information area using the maximum height of    |
  211. |    a character in the current font.                                     |
  212. |------------------------------------------------------------------------*/
  213.   setExtensionSize(&infoArea, IFont(&infoArea).maxCharHeight());
  214.  
  215. /*------------------------------------------------------------------------|
  216. |  Set the values for the text controls from strings in the resource file.|
  217. |    The infoArea inactive text is displayed when no menu item is active. |
  218. |------------------------------------------------------------------------*/
  219.   hello.setText(STR_HELLO);
  220.   leftButton.setText(STR_LEFTB);
  221.   centerButton.setText(STR_CENTERB);
  222.   rightButton.setText(STR_RIGHTB);
  223.   helpButton.setText(STR_HELPB);
  224.   infoArea.setInactiveText(STR_INFO);
  225.  
  226. /*------------------------------------------------------------------------|
  227. |  Set the canvas margins and padding between buttons to zero.            |
  228. |  Add the set canvas that contains the push buttons to the frame window  |
  229. |    as an extension immediately below the client window with a height    |
  230. |    equal to the minimum height of the push buttons.  You must set the   |
  231. |    text in the push buttons first.                                      |
  232. |------------------------------------------------------------------------*/
  233.   leftButton.enableTabStop();
  234.   buttons.setMargin(ISize());
  235.   buttons.setPad(ISize());
  236.   addExtension(&buttons, IFrameWindow::belowClient,
  237.                 (unsigned long)buttons.minimumSize().height());
  238.  
  239. /*------------------------------------------------------------------------|
  240. |  Have the command handler handle commands sent from the frame window.   |
  241. |    The command handler's command() function is called to process        |
  242. |    menu item, push button, and accelerator key selections.              |
  243. |  Attach the command handler to the hello and earthWindow static text    |
  244. |    windows to handle command events generated for these windows by      |
  245. |    the pop-up menus.                                                    |
  246. |  Attach the pop-up menu handler to the hello and earthWindow static text|
  247. |    windows to handle pop-up menu requests made while the pointer is     |
  248. |    over these windows.  The makePopUpMenu function will be called when  |
  249. |    the user requests a pop-up menu using the mouse in these windows.    |
  250. |  Start the timer to update the time and date.                           |
  251. |------------------------------------------------------------------------*/
  252.   commandHandler.handleEventsFor(this);
  253.   commandHandler.handleEventsFor(&hello);
  254.   commandHandler.handleEventsFor(&earthWindow);
  255.   popUpHandler.handleEventsFor(&hello);
  256.   popUpHandler.handleEventsFor(&earthWindow);
  257.   timeTicker.setInterval(1000);
  258.   timeTicker.start(new TimeUpdate(this));
  259.  
  260. /*------------------------------------------------------------------------|
  261. |   Add the help library to the help window using addLibraries().         |
  262. |   Set the help window title from a string in the resource file.         |
  263. |   Begin handling help events for the frame window.                      |
  264. |------------------------------------------------------------------------*/
  265.    try
  266.    {
  267.       helpWindow.addLibraries( HELP_LIBRARIES );
  268.       helpWindow.setTitle(STR_HTITLE);
  269.       helpHandler.handleEventsFor(this);
  270.    }
  271.    catch( ... )
  272.    {
  273.       IMessageBox
  274.          msgBox( this );
  275.       msgBox.show( STR_HELP_NOT_FOUND, IMessageBox::warning );
  276.    }
  277.  
  278. /*------------------------------------------------------------------------|
  279. | Align the static text, set the status line, and set the check mark in   |
  280. |   the menu bar.                                                         |
  281. |------------------------------------------------------------------------*/
  282.   setTextAlignment(center);
  283.  
  284. } /* end AHelloWindow :: AHelloWindow(...) */
  285.  
  286. /**************************************************************************
  287. * Class AHelloWindow :: ~AHelloWindow - Destructor for the main window    *
  288. *                                                                         *
  289. * Stop handling command events for the frame.                             *
  290. * Stop handling select events for the list box.                           *
  291. * Stop handling help events for the frame.                                *
  292. * Stop handling timer events for the frame.                               *
  293. * Stop handling menu events for the frame.                                *
  294. **************************************************************************/
  295. AHelloWindow :: ~AHelloWindow()
  296. {
  297.   commandHandler.stopHandlingEventsFor(this);
  298.   commandHandler.stopHandlingEventsFor(&earthWindow);
  299.   commandHandler.stopHandlingEventsFor(&hello);
  300.   selectHandler.stopHandlingEventsFor(&listBox);
  301.   helpHandler.stopHandlingEventsFor(this);
  302.  
  303.   popUpHandler.stopHandlingEventsFor(&hello);
  304.   popUpHandler.stopHandlingEventsFor(&earthWindow);
  305.   timeTicker.stop();
  306. } /* end AHelloWindow :: ~AHelloWindow() */
  307.  
  308. /**************************************************************************
  309. * Class AHelloWindow :: setTextAlignment - Align static text in client    *
  310. *   window                                                                *
  311. **************************************************************************/
  312. AHelloWindow &
  313.   AHelloWindow :: setTextAlignment(const Alignment alignment)
  314. {
  315. /*--------------------- Update Window for New Alignment ------------------|
  316. |  Depending on the value passed, update the window as follows:           |
  317. |    Set the alignment of the static text control in the client window.   |
  318. |    Set the text of the alignment status line static text control.       |
  319. |    Check the selected menu item; remove check marks from the other two. |
  320. |------------------------------------------------------------------------*/
  321.   switch(alignment)
  322.   {
  323.   case left:
  324.     hello.setAlignment(IStaticText::centerLeft);
  325.     statusLine.setText(STR_LEFT);
  326.     menuBar.uncheckItem(MI_CENTER);
  327.     menuBar.checkItem(MI_LEFT);
  328.     menuBar.uncheckItem(MI_RIGHT);
  329.     break;
  330.   case center:
  331.     hello.setAlignment(IStaticText::centerCenter);
  332.     statusLine.setText(STR_CENTER);
  333.     menuBar.checkItem(MI_CENTER);
  334.     menuBar.uncheckItem(MI_LEFT);
  335.     menuBar.uncheckItem(MI_RIGHT);
  336.     break;
  337.  case right:
  338.     hello.setAlignment(IStaticText::centerRight);
  339.     statusLine.setText(STR_RIGHT);
  340.     menuBar.uncheckItem(MI_CENTER);
  341.     menuBar.uncheckItem(MI_LEFT);
  342.     menuBar.checkItem(MI_RIGHT);
  343.     break;
  344.  }
  345.  return (*this);                        //Return a reference to the frame
  346.  
  347. } /* end AHelloWindow :: setTextAlignment(...) */
  348.  
  349. /**************************************************************************
  350. * Class AHelloWindow :: editText - Create and show a dialog window for    *
  351. *   inputting text that will be used to replace the "Hello World!!!"      *
  352. *   static text string.                                                   *
  353. **************************************************************************/
  354. AHelloWindow &
  355.   AHelloWindow :: editText()
  356. {
  357. /*------------------------------------------------------------------------|
  358. |  Store the current value of the text to be changed.                     |
  359. |  Set the text in the information area from the dialog information       |
  360. |    string in the resource file.                                         |
  361. |------------------------------------------------------------------------*/
  362.   IString textValue(hello.text());
  363.   infoArea.setInactiveText(STR_INFODLG);
  364.  
  365. /*------------------------------------------------------------------------|
  366. |  Create a new text dialog with textValue as the string to edit and      |
  367. |    AHelloWindow as the owner window.                                    |
  368. |  Show the dialog modally.  This means that the owner window cannot have |
  369. |    the focus back until the dialog is ended.                            |
  370. |------------------------------------------------------------------------*/
  371.   ATextDialog textDialog(textValue,this);
  372.   textDialog.showModally();
  373.  
  374. /*------------------------------------------------------------------------|
  375. |  If the OK button was used to end the dialog, then the static text,     |
  376. |    hello, is set to the textValue string.  Else, it is not changed.     |
  377. |  Reset the information area inactive text.                              |
  378. |------------------------------------------------------------------------*/
  379.   if (textDialog.result() == DID_OK)
  380.         hello.setText(textValue);
  381.   infoArea.setInactiveText(STR_INFO);
  382.  
  383.   return (*this);                       //Return a reference to the frame
  384. }  /* end AHelloWindow :: editText() */
  385.  
  386. /**************************************************************************
  387. * Class AHelloWindow :: setTextFromListBox - Set the Hello World text     *
  388. *   from the first selected item in the AHelloWindow listBox.             *
  389. **************************************************************************/
  390. AHelloWindow &
  391.   AHelloWindow :: setTextFromListBox()
  392. {
  393.  
  394. /*------------------------------------------------------------------------|
  395. |  Create a cursor to the list box.  Using the default filter for a       |
  396. |    list box cursor, selectedItems, causes the setToFirst() function     |
  397. |    to position the cursor to the first selected item.                   |
  398. |  Set the hello IStaticText control text value.                          |
  399. |------------------------------------------------------------------------*/
  400.   IListBox::Cursor lbCursor(listBox);
  401.   lbCursor.setToFirst();
  402.   hello.setText(listBox.elementAt(lbCursor));
  403.  
  404.   return (*this);                       //Return a reference to the frame
  405. }  /* end AHelloWindow :: setTextFromListBox() */
  406.  
  407. /**************************************************************************
  408. * AHelloWindow :: setHelloFont - Starts a font dialog for changing the    *
  409. *   font of the Hello World text.                                         *
  410. **************************************************************************/
  411. AHelloWindow &
  412.   AHelloWindow :: setHelloFont()
  413. {
  414. /*------------------------------------------------------------------------|
  415. |  Set the text in the information area from the font dialog information  |
  416. |    string in the resource file.                                         |
  417. |  Create a font from the current font of the hello static text object.   |
  418. |  Create a font settings object from the tempFont object.                |
  419. |  Set the title to be used in the font dialog from a string resource in  |
  420. |    the resource file.                                                   |
  421. |  Start the font dialog by creating an IFontDialog object.  The font     |
  422. |    dialog is created as a modal dialog window with the desktopWindow    |
  423. |    as the parent, the AHelloWindow frame as the owner, resetButton      |
  424. |    style which supplies the user with a way to reset the fields in the  |
  425. |    dialog, and the font settings that were just created.                |
  426. |------------------------------------------------------------------------*/
  427.   infoArea.setInactiveText(STR_FONTDLGT);
  428.   IFont tempFont(&hello);
  429.   IFontDialog::Settings fontSettings(&tempFont);
  430.   fontSettings.setTitle(IResourceId(STR_FONTDLGT));
  431.   IFontDialog fontDialog( desktopWindow(), this,
  432.                           IFontDialog::resetButton, fontSettings);
  433.  
  434. /*------------------------------------------------------------------------|
  435. |  If the user pressed OK in the font dialog, then change the font for    |
  436. |    the hello IStaticText object using the tempFont object that was set  |
  437. |    by the font dialog.                                                  |
  438. |  Reset the information area inactive text.                              |
  439. |------------------------------------------------------------------------*/
  440.   if (fontDialog.pressedOK())
  441.   {
  442.     hello.setFont(tempFont);
  443.   }
  444.   infoArea.setInactiveText(STR_INFO);
  445.   return (*this);                       //Return a reference to the frame
  446. }  /* end AHelloWindow :: setHelloFont() */
  447.  
  448. /**************************************************************************
  449. * Class AHelloWindow :: readHelloSettings - Reads in user settings from   *
  450. *   an IProfile class profile.                                            *
  451. **************************************************************************/
  452. AHelloWindow &
  453.   AHelloWindow :: readHelloSettings()
  454. {
  455. /*------------------------------------------------------------------------|
  456. |  The try-catch blocks are used to prevent a missing profile from        |
  457. |    abending the program.                                                |
  458. |------------------------------------------------------------------------*/
  459.   try
  460.   {
  461. /*------------------------------------------------------------------------|
  462. |  A file named Hello6.ini.iclDB on AIX, or Hello6.ini on OS/2, is        |
  463. |   is expected to exist, and if so, will be opened and the default       |
  464. |   application name, Hello6, is set.                                     |
  465. |------------------------------------------------------------------------*/
  466.     IProfile profile(IString(APPLICATION_NAME)+".ini");
  467.     profile.setDefaultApplicationName(APPLICATION_NAME);
  468.  
  469. /*------------------------------------------------------------------------|
  470. |  Each Hello6 element in the profile is read using the elementWithKey or |
  471. |    integerWithKey function, where element is used for reading strings   |
  472. |    and integer is used for values.  The elements are identified using   |
  473. |    unique names that are defined by this application.                   |
  474. |  The appropriate AHelloWindow function is called to set the date and    |
  475. |    time formats.  The arguments for the functions are integers,         |
  476. |    representing true(1) and false(0).                                   |
  477. |  The Font... settings are used to reset the AHelloWindow::hello         |
  478. |    static text window font.                                             |
  479. |  The appropriate AEarthWindow functions are called to set the painting  |
  480. |    settings for the Earth window.                                       |
  481. |------------------------------------------------------------------------*/
  482.     enableDateLong(profile.integerWithKey("DateLong"));
  483.     enableTimeLong(profile.integerWithKey("TimeLong"));
  484.  
  485. IFont
  486.     helloFont(&hello);
  487.     helloFont.useVectorOnly(profile.integerWithKey("FontVector"));
  488.     helloFont.useNonPropOnly(profile.integerWithKey("FontNonProp"));
  489.     helloFont.setName(profile.elementWithKey("FontFace"));
  490.     helloFont.setPointSize(profile.integerWithKey("FontSize"));
  491.     helloFont.setBold(profile.integerWithKey("FontBold"));
  492.     helloFont.setItalic(profile.integerWithKey("FontItalic"));
  493.     hello.setFont(helloFont);
  494.  
  495.     earth()->enableTwinkle(profile.integerWithKey("Twinkling"));
  496.     earth()->enableBright(profile.integerWithKey("Bright"));
  497.     earth()->setLayers(profile.integerWithKey("Layers"));
  498.  
  499. long
  500.     hue=profile.integerWithKey("EarthColor");
  501.     if (hue==IColor(IColor::green).asRGBLong())
  502.       { earth()->setEarthColor(IColor(IColor::green)); }
  503.     else if (hue==IColor(IColor::white).asRGBLong())
  504.       { earth()->setEarthColor(IColor(IColor::white)); }
  505.     else if (hue==IColor(IColor::yellow).asRGBLong())
  506.       { earth()->setEarthColor(IColor(IColor::yellow)); }
  507.     else
  508.       { earth()->setEarthColor(IColor(IColor::cyan)); }
  509.  
  510. /*------------------------------------------------------------------------|
  511. |  A message box with an ok button is used to inform the user that the    |
  512. |    read settings function completed successfully.            .          |
  513. |------------------------------------------------------------------------*/
  514.     IMessageBox mbox(this);
  515.     mbox.setTitle(WND_MAIN).show(STR_READOK,IMessageBox::okButton);
  516.   }
  517.   catch (IException& exc)
  518.   {
  519. /*------------------------------------------------------------------------|
  520. |  A default exception message box is used to inform the user that the    |
  521. |    read settings function failed.                            .          |
  522. |------------------------------------------------------------------------*/
  523.     IMessageBox mbox(this);
  524.     mbox.show(exc);
  525.   }
  526.   return (*this);                       //Return a reference to the frame
  527. }  /* end AHelloWindow :: readHelloSettings() */
  528.  
  529. /**************************************************************************
  530. * Class AHelloWindow :: saveHelloSettings -Saves user settings in an      *
  531. *   IProfile class profile.                                               *
  532. **************************************************************************/
  533. AHelloWindow &
  534.   AHelloWindow :: saveHelloSettings()
  535. {
  536. /*------------------------------------------------------------------------|
  537. |  The try-catch blocks are used to prevent a profile error from          |
  538. |    abending the program.                                                |
  539. |------------------------------------------------------------------------*/
  540.   try
  541.   {
  542. /*------------------------------------------------------------------------|
  543. |  A file named Hello6.ini.iclDB on AIX, or Hello6.ini on OS/2, is        |
  544. |   is opened, if found, or created, if not found, and the default        |
  545. |   application name, Hello6, is set.                                     |
  546. |------------------------------------------------------------------------*/
  547.     IProfile profile(IString(APPLICATION_NAME)+".ini");
  548.     profile.setDefaultApplicationName(APPLICATION_NAME);
  549.  
  550. /*------------------------------------------------------------------------|
  551. |  The IProfile overloaded function, addOrReplaceElementWithKey, is used  |
  552. |    to save each setting value in the profile.  Numeric and boolean      |
  553. |    values are stored as long integers, while strings are stored as      |
  554. |    character strings.                                                   |
  555. |------------------------------------------------------------------------*/
  556.     profile.addOrReplaceElementWithKey(
  557.                     "DateLong", (long)isDateLong());
  558.     profile.addOrReplaceElementWithKey(
  559.                     "TimeLong", (long)isTimeLong());
  560.  
  561. IFont
  562.     helloFont(&hello);
  563.     profile.addOrReplaceElementWithKey(
  564.                     "FontVector", helloFont.isVectorOnly());
  565.     profile.addOrReplaceElementWithKey(
  566.                     "FontNonProp", helloFont.isNonPropOnly());
  567.     profile.addOrReplaceElementWithKey(
  568.                     "FontFace", helloFont.name());
  569.     profile.addOrReplaceElementWithKey(
  570.                     "FontSize", helloFont.pointSize());
  571.     profile.addOrReplaceElementWithKey(
  572.                     "FontBold", helloFont.isBold());
  573.     profile.addOrReplaceElementWithKey(
  574.                     "FontItalic", helloFont.isItalic());
  575.  
  576.     profile.addOrReplaceElementWithKey(
  577.                     "Twinkling",(long)earth()->isTwinkling());
  578.     profile.addOrReplaceElementWithKey(
  579.                     "Bright",   (long)earth()->isBright());
  580.     profile.addOrReplaceElementWithKey(
  581.                     "Layers",   (long)earth()->layers());
  582.     profile.addOrReplaceElementWithKey(
  583.                     "EarthColor",earth()->earthColor().asRGBLong());
  584.  
  585. /*------------------------------------------------------------------------|
  586. |  A message box with an ok button is used to inform the user that the    |
  587. |    save settings function completed successfully.            .          |
  588. |------------------------------------------------------------------------*/
  589.     IMessageBox mbox(this);
  590.     mbox.setTitle(WND_MAIN).show(STR_SAVEDOK,IMessageBox::okButton);
  591.   }
  592.   catch (IException& exc)
  593.   {
  594. /*------------------------------------------------------------------------|
  595. |  A default exception message box is used to inform the user that the    |
  596. |    save settings function failed.                            .          |
  597. |------------------------------------------------------------------------*/
  598.     IMessageBox mbox(this);
  599.     mbox.show(exc);
  600.   }
  601.  
  602.   return (*this);                       //Return a reference to the frame
  603. }  /* end AHelloWindow :: saveHelloSettings() */
  604.  
  605. /**************************************************************************
  606. * Class AHelloWindow :: openHelloSettings - Opens a settings notebook for *
  607. *   changing Hello World window attributes.                               *
  608. **************************************************************************/
  609. AHelloWindow &
  610.   AHelloWindow :: openHelloSettings()
  611. {
  612. /*------------------------------------------------------------------------|
  613. |  Check if the notebook already exists.                                  |
  614. |    Zero is returned if the notebook does not exist.                     |
  615. |------------------------------------------------------------------------*/
  616.  
  617.   if ( settingsNotebook )
  618.   {
  619.     delete settingsNotebook;
  620.   }
  621.  
  622.  
  623. /*------------------------------------------------------------------------|
  624. |  Using the new operator causes the ANotebookWindow object to be         |
  625. |    dynamically allocated.  This means that that the object will not go  |
  626. |    out of scope, therefore the application should delete it when it is  |
  627. |    finished with it.                                                    |
  628. |------------------------------------------------------------------------*/
  629.  
  630.   settingsNotebook = new ANotebookWindow(WND_NOTEBOOKFRAME,this);
  631.  
  632. /*------------------------------------------------------------------------|
  633. |  Move the notebook to ensure the entire window is visible on a small    |
  634. |    display.                                                             |
  635. |  The notebook is given control and shown by using setFocus and show.    |
  636. |    The restore function is used in case the window was created          |
  637. |    previously, but minimized.                                           |
  638. |------------------------------------------------------------------------*/
  639.   settingsNotebook->moveTo( IPoint(50,50) );
  640.   settingsNotebook->restore().setFocus().show();
  641.   return (*this);                       //Return a reference to the frame
  642. }  /* end AHelloWindow :: openHelloSettings() */
  643.  
  644. /**************************************************************************
  645. * ACommandHandler :: ACommandHandler - constructor for the command handler*
  646. **************************************************************************/
  647. ACommandHandler :: ACommandHandler(AHelloWindow *helloFrame)
  648. {
  649.    frame=helloFrame;                     //Save frame to be handled
  650. } /* end ACommandHandler :: ACommandHandler(...) */
  651.  
  652. /**************************************************************************
  653. * Class ACommandHandler :: command - Handle menu and button commands      *
  654. **************************************************************************/
  655. IBase::Boolean
  656.   ACommandHandler :: command(ICommandEvent & cmdEvent)
  657. {
  658.   Boolean eventProcessed(true);         //Assume event will be processed
  659.  
  660. /*--------------------- Process command events ---------------------------|
  661. |  Depending on the command event ID, call the AHelloWindow::setAlignment |
  662. |    function with the appropriate AHelloWorld::Alignment value.          |
  663. |  Do this except when the Text menu item is selected;                    |
  664. |    then call the AHelloWindow::editText function for changing the       |
  665. |    Hello World text using a dialog.                                     |
  666. |------------------------------------------------------------------------*/
  667.   switch (cmdEvent.commandId()) {
  668.     case MI_CENTER:
  669.       frame->setTextAlignment(AHelloWindow::center);
  670.       break;
  671.     case MI_LEFT:
  672.       frame->setTextAlignment(AHelloWindow::left);
  673.       break;
  674.     case MI_RIGHT:
  675.       frame->setTextAlignment(AHelloWindow::right);
  676.       break;
  677.     case MI_TEXT:
  678.       frame->editText();
  679.       break;
  680.     case MI_FONT:
  681.       frame->setHelloFont();
  682.       break;
  683.     case MI_READSETS:
  684.       frame->readHelloSettings();
  685.       break;
  686.     case MI_OPENSETS:
  687.       frame->openHelloSettings();
  688.       break;
  689.     case MI_SAVESETS:
  690.       frame->saveHelloSettings();
  691.       break;
  692.     case MI_TWINKLE:                    //Toggle the twinkle setting
  693.       frame->earth()->enableTwinkle(!frame->earth()->isTwinkling());
  694.       break;
  695.     case MI_BRIGHT:
  696.       frame->earth()->enableBright();
  697.       break;
  698.     case MI_DIM:
  699.       frame->earth()->disableBright();
  700.       break;
  701.  
  702.     default:                            //Otherwise,
  703.       eventProcessed=false;             //  the event wasn't processed
  704.   } /* end switch */
  705.  
  706.   return(eventProcessed);
  707. } /* end ACommandHandler :: command(...) */
  708.  
  709. /**************************************************************************
  710. * Class ASelectHandler :: ASelectHandler - constructor for the select     *
  711. *   handler                                                               *
  712. **************************************************************************/
  713. ASelectHandler :: ASelectHandler(AHelloWindow *helloFrame)
  714. {
  715.    frame=helloFrame;                     //Save frame to be handled
  716. } /* end ASelectHandler :: ASelectHandler(...) */
  717.  
  718.  
  719. /**************************************************************************
  720. * Class ASelectHandler :: selected - Handle items selected within the     *
  721. *   list box.                                                             *
  722. **************************************************************************/
  723. IBase::Boolean
  724.   ASelectHandler :: selected(IControlEvent & evt)
  725. {
  726.  
  727. /*------------------------------------------------------------------------|
  728. |  Call the AHelloWindow::setTextFromListBox function for the frame used  |
  729. |    to construct this select handler.                                    |
  730. |------------------------------------------------------------------------*/
  731.   frame->setTextFromListBox();
  732.  
  733.   return (true);                        //Event is always processed
  734. } /* end ASelectHandler :: selected(...) */
  735.  
  736. /**************************************************************************
  737. * Class AHelpHandler :: keysHelpId - Handle the keys help request event   *
  738. **************************************************************************/
  739. IBase::Boolean
  740.   AHelpHandler :: keysHelpId(IEvent& evt)
  741. {
  742.   evt.setResult(1000);                  //1000=keys help ID in
  743.                                         //  ahellow5.ipf file
  744.  
  745.   return (true);                        //Event is always processed
  746. } /* end AHelpHandler :: keysHelpId(...) */
  747. /**************************************************************************
  748. * Class TimeUpdate :: timeExpired - Handles timer events for updating       *
  749. *   the date and time.                                                    *
  750. **************************************************************************/
  751. void
  752.   TimeUpdate::timerExpired( unsigned long id)
  753. {
  754.   ahw->tickTime();
  755. }
  756.  
  757.  
  758. /**************************************************************************
  759. * Class AHelloWindow :: tickTime - Update the date and time using current *
  760. *   formats if necessary                                                  *
  761. **************************************************************************/
  762. AHelloWindow
  763.   &AHelloWindow::tickTime()
  764. {
  765.   IString
  766.     formattedDate,
  767.     formattedTime;
  768.  
  769. /*------------------------------------------------------------------------|
  770. |  If the date format is long, date should appear as August 10, 1994.     |
  771. |    Otherwise it should appear in the default locale format, eg 08/10/94.|
  772. |------------------------------------------------------------------------*/
  773.   if (isDateLong())
  774.   {
  775.     formattedDate =( (IDate().monthName()) +" "+
  776.               IString(IDate().dayOfMonth())+", "+
  777.               IString(IDate().year()));
  778.   }
  779.   else formattedDate = IDate().asString();
  780.  
  781. /*------------------------------------------------------------------------|
  782. |  If the time format is long, time should appear as 01:44:59 pm.         |
  783. |    Otherwise it should appear in shortened military time, eg 13:44.     |
  784. |------------------------------------------------------------------------*/
  785.   if (isTimeLong())
  786.   {
  787.     formattedTime = ITime().asString("%I:%M:%S %p");
  788.   }
  789.   else
  790.     formattedTime = ITime().asString("%H:%M");
  791.  
  792. /*------------------------------------------------------------------------|
  793. |  If the date and/or time have changed, update the status area.          |
  794. |------------------------------------------------------------------------*/
  795.   if (formattedDate != statusDate.text())
  796.     statusDate.setText(formattedDate);
  797.   if (formattedTime != statusTime.text())
  798.     statusTime.setText(formattedTime);
  799.   return (*this);
  800. } /* end AHelloWindow :: tickTime() */
  801.  
  802. /**************************************************************************
  803. * Class AHelloWindow :: earth - Accesses the earthWindow contained within *
  804. *     AHelloWindow class object.                                          *
  805. **************************************************************************/
  806. AEarthWindow
  807.   *AHelloWindow::earth()
  808. {
  809.   return (&earthWindow);
  810. } /* end AHelloWindow :: earth() */
  811.  
  812. /**************************************************************************
  813. * Class AHelloWindow :: isDateLong - Querys the state of the date format. *
  814. **************************************************************************/
  815. const IBase::Boolean
  816.   AHelloWindow::isDateLong()
  817. {
  818.   return (dateLong);
  819. } /* end AHelloWindow :: isDateLong() */
  820.  
  821. /**************************************************************************
  822. * Class AHelloWindow :: enableDateLong - Sets the date format to long.    *
  823. **************************************************************************/
  824. AHelloWindow
  825.   &AHelloWindow::enableDateLong(IBase::Boolean makingLong)
  826. {
  827.   dateLong = makingLong;
  828.   return (*this);
  829. } /* end AHelloWindow :: enableDateLong(...) */
  830.  
  831. /**************************************************************************
  832. * Class AHelloWindow :: disableDateLong - Sets the date format to short.  *
  833. **************************************************************************/
  834. AHelloWindow
  835.   &AHelloWindow::disableDateLong()
  836. {
  837.   dateLong = false;
  838.   return (*this);
  839. } /* end AHelloWindow :: disableDateLong() */
  840.  
  841. /**************************************************************************
  842. * Class AHelloWindow :: isTimeLong - Querys the state of the time format. *
  843. **************************************************************************/
  844. const IBase::Boolean
  845.   AHelloWindow::isTimeLong()
  846. {
  847.   return (timeLong);
  848. } /* end AHelloWindow :: isTimeLong() */
  849.  
  850. /**************************************************************************
  851. * Class AHelloWindow :: enableTimeLong - Set the time format to long.     *
  852. **************************************************************************/
  853. AHelloWindow
  854.   &AHelloWindow::enableTimeLong(IBase::Boolean makingLong)
  855. {
  856.   timeLong = makingLong;
  857.   return (*this);
  858. } /* end AHelloWindow :: enableTimeLong(...) */
  859.  
  860. /**************************************************************************
  861. * Class AHelloWindow :: disableTimeLong - Sets the time format to short.  *
  862. **************************************************************************/
  863. AHelloWindow
  864.   &AHelloWindow::disableTimeLong()
  865. {
  866.   timeLong = false;
  867.   return (*this);
  868. } /* end AHelloWindow :: disableTimeLong() */
  869.  
  870. /**************************************************************************
  871. * Class APopUpHandler :: makePopUpMenu - Creates a pop-up menu for        *
  872. *   windows attached to the handler when requested by the pointing device *
  873. **************************************************************************/
  874. IBase::Boolean APopUpHandler::makePopUpMenu(IMenuEvent &menuEvent)
  875. {
  876.   Boolean
  877.     eventProcessed(true);         //Assume event will be processed
  878.   IPopUpMenu
  879.    *popUpMenu;
  880. /*------------------------------------------------------------------------|
  881. |  The window pointer stored in the menu event points to the owner of     |
  882. |    the menu to be popped up.                                            |
  883. |------------------------------------------------------------------------*/
  884.   IWindow       *popUpOwner = menuEvent.window();
  885.   IStaticText   *hello;
  886.   AEarthWindow  *earth;
  887. /*------------------------------------------------------------------------|
  888. |  Determine which menu to pop up based on the window ID of the window    |
  889. |    for which the menu event is being handled.                           |
  890. |------------------------------------------------------------------------*/
  891.   switch (popUpOwner->id()) {
  892.     case WND_HELLO:
  893. /*------------------------------------------------------------------------|
  894. |  Create a pop-up menu for the menu WND_HELLOPOPUP.                      |
  895. |  The pointer, hello, is created by casting the popUpOwner to the type   |
  896. |    of the AHelloWindow::hello object.  The pointer is used to call      |
  897. |    the AHelloWindow::alignment function to determine which pop-up menu  |
  898. |    item to disable.                                                     |
  899. |------------------------------------------------------------------------*/
  900.       popUpMenu = new IPopUpMenu(WND_HELLOPOPUP,popUpOwner);
  901.  
  902.       if (popUpMenu)
  903.       {
  904.         hello = (IStaticText *)popUpOwner;
  905.         popUpMenu->enableItem(MI_LEFT,
  906.                         hello->alignment()!=IStaticText::centerLeft);
  907.         popUpMenu->enableItem(MI_CENTER,
  908.                         hello->alignment()!=IStaticText::centerCenter);
  909.         popUpMenu->enableItem(MI_RIGHT,
  910.                         hello->alignment()!=IStaticText::centerRight);
  911.       }
  912.                                         //If the pop-up wasn't found,
  913.       else eventProcessed=false;        //  the event wasn't processed
  914.       break;
  915.     case WND_EARTH:
  916. /*------------------------------------------------------------------------|
  917. |  If the earthWindow pop-up is requested, then create the pop-up menu    |
  918. |    dynamically, with the earthWindow as the owner.  This approach is    |
  919. |    used to create pop-up menus on demand.  The setAutoDeleteObject      |
  920. |    function is used to automatically delete the menu after the user     |
  921. |    has made a selection.                                                |
  922. |  The pointer, earth, is created by casting the popUpOwner to the type   |
  923. |    of the earthWindow object.  The pointer is used to call the          |
  924. |    AEarthWindow  isTwinkling and isBright functions.                    |
  925. |  The Twinkling menu item contains a check mark when isTwinkling is true.|
  926. |  Either the Bright or Dim menu item is disabled, depending on the       |
  927. |    result of isBright.                                                  |
  928. |------------------------------------------------------------------------*/
  929.       popUpMenu = new IPopUpMenu(WND_EARTHPOPUP,popUpOwner);
  930.       if (popUpMenu)
  931.       {
  932.         popUpMenu->setAutoDeleteObject();
  933.         earth = (AEarthWindow *)popUpOwner;
  934.         popUpMenu->checkItem(MI_TWINKLE,earth->isTwinkling());
  935.         if (earth->isBright())
  936.         { popUpMenu->disableItem(MI_BRIGHT); }
  937.         else
  938.         { popUpMenu->disableItem(MI_DIM); }
  939.       }
  940.                                         //If the pop-up wasn't created,
  941.       else eventProcessed=false;        //  the event wasn't processed
  942.       break;
  943.     default:                            //If the owner wasn't hello or eart
  944.       eventProcessed=false;             //  the event wasn't processed
  945.   } /* end switch */
  946. /*------------------------------------------------------------------------|
  947. |  If the pop-up menus were setup successfully, use the                   |
  948. |    IPopUpMenu::show function to show the menu at the position where the |
  949. |    pointing device was when the menu event occurred.                    |
  950. |------------------------------------------------------------------------*/
  951.   if (eventProcessed)
  952.     popUpMenu->show(menuEvent.mousePosition());
  953.   return(eventProcessed);
  954. } /* end APopUpHandler :: makePopUpMenu(...) */
  955.  
  956.