home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ICLUI.ZIP / HELLO5 / AHELLOW5.CPP < prev    next >
Text File  |  1993-03-09  |  26KB  |  357 lines

  1. /******************************************************************************/
  2. /* HELLO WORLD SAMPLE PROGRAM - Version 5: Class Implementation (AHELLOW5.CPP)*/
  3. /*                                                                            */
  4. /* COPYRIGHT: Copyright (C) International Business Machines Corp., 1992,1993. */
  5. /*                                                                            */
  6. /* DISCLAIMER OF WARRANTIES:                                                  */
  7. /*   The following [enclosed] code is sample code created by IBM              */
  8. /*   Corporation.  This sample code is not part of any standard IBM product   */
  9. /*   and is provided to you solely for the purpose of assisting you in the    */
  10. /*   development of your applications.  The code is provided "AS IS",         */
  11. /*   without warranty of any kind.  IBM shall not be liable for any damages   */
  12. /*   arising out of your use of the sample code, even if they have been       */
  13. /*   advised of the possibility of such damages.                              */
  14. /******************************************************************************/
  15. // NOTE: WE RECOMMEND USING A FIXED SPACE FONT TO LOOK AT THE SOURCE
  16. //**************************************************************************
  17. // C++ Hello World History and Key Functions:                              *
  18. //   Version 1:                                                            *
  19. //      - Creates and runs a simple application                            *
  20. //      - Creates the main window (IFrameWindow)                           *
  21. //      - Creates a static text control set to "Hello, World!" as the      *
  22. //         client window                                                   *
  23. //                                                                         *
  24. //   Version 2: (lines with v2 in column 79-80)                            *  v2
  25. //      - Create Main Window (AHellowWindow) as subclass of IFrameWindow   *   .
  26. //      - Gets the "Hello, World!!" text string and other items from a     *   .
  27. //         resource file                                                   *   .
  28. //      - Sets the window title from a resource file                       *   .
  29. //      - Creates and sets the information area at the bottom of the       *   .
  30. //         client area                                                     *  v2
  31. //                                                                         *
  32. //   Version 3: (lines with v3 in column 79-80)                            *  v3
  33. //      - Add Alignment (Left, Center, Right) Menu Bar                     *   .
  34. //      - Add Command Processing to set the "Hello, World" alignment       *   .
  35. //      - Place check in front of the Left, Center or Right Menu Item      *   .
  36. //      - Create and maintain Status Line with current alignment status    *  v3
  37. //                                                                         *
  38. //   Version 4: (lines with v4 in column 79-80)                            *  v4
  39. //      - Add Accelerator Keys for Left (F7), Center(F8), Right(F9)        *   .
  40. //      - Modify Left, Center and Right Menu Items to show function key    *   .
  41. //      - Modify Menu Bar to create Edit Menu and "text..." Menu Item      *   .
  42. //      - Add ADialogText to allow user to change "Hello, World" text      *   .
  43. //      - Add Push Buttons & Set Canvas to change alignment                *  v4
  44. //                                                                         *
  45. //   Version 5: (lines with v5 in column 79-80)                            *  v5
  46. //      - Code a new control (AEarthWindow) using PM Graphics calls        *   .
  47. //      - Add AEarthWindow to the bottom of the client area                *   .
  48. //      - Add Help Windows for the Main, Dialog, Entry Field Windows       *   .
  49. //      - Use Split Canvas as the Client Area                              *   .
  50. //      - Add ListBox to Client Area to change "Hello, World" text         *  v5
  51. //**************************************************************************
  52.                                         //Include IBM UI class headers:
  53. #include <iapp.hpp>                     //IApplication Class
  54. #include <istattxt.hpp>                 //IStaticText Class
  55. #include <iinfoa.hpp>                   //IInfoArea Class                     v2
  56. #include <imenubar.hpp>                 //IMenuBar Class                      v3
  57. #include <ifont.hpp>                    //IFont                               v3
  58. #include <istring.hpp>                  //IString Class                       v4
  59. #include <isetcv.hpp>                   //ISetCanvas Class                    v4
  60. #include <ipushbut.hpp>                 //IPushButton Class                   v4
  61. #include <ihelp.hpp>                    //IHelpWindow Class                   v5
  62. #include <ihelphdr.hpp>                 //IHelpHandler Class                  v5
  63. #include <isplitcv.hpp>                 //ISplitCanvas Class                  v5
  64. #include <ilistbox.hpp>                 //IListBox                            v5
  65.  
  66. #include "ahellow5.hpp"                 //Include AHelloWindow Class headers  v2
  67. #include "ahellow5.h"                   //Include our Symbolic definitions    v2
  68. #include "adialog5.hpp"                 //ATextDialog Class                   v4
  69. #include "aearthw5.hpp"                 //AEarthWindow Class                  v5
  70.  
  71. //*************************************************************************
  72. // main  - Application entry point                                        *
  73. //*************************************************************************
  74. void main()                             //Main Procedure with no parameters
  75. {
  76.   AHelloWindow mainWindow (WND_MAIN);   //Create our main window on the
  77.                                         // desktop
  78.   IApplication::current().run();        //Get the current application and
  79.                                         // run it
  80. } /* end main */
  81.  
  82. //**************************************************************************
  83. // AHelloWindow :: AHelloWindow - Constructor for our main window          *
  84. //**************************************************************************
  85. AHelloWindow :: AHelloWindow(unsigned long windowId)
  86.   : IFrameWindow (                      //Call IFrameWindow constructor       v2
  87.     IFrameWindow::defaultStyle()        //  Use default plus                  v2
  88.     | IFrameWindow::minimizedIcon       //  Get Minimized Icon from  RC file  v2
  89.     | IFrameWindow::menuBar             //  Get Menu Bar from RC file         v3
  90.     | IFrameWindow::accelerator,        //  Get Accelerator Table from RC filev4
  91.     windowId)                           //  Main Window ID
  92. {
  93.   setupClient();                        //Setup Client Window                 v5
  94.   setupStatusArea();                    //Setup Status Area                    .
  95.   setupInfoArea();                      //Setup Information Area              v5
  96.   setupButtons();                       //Setup Buttons                       v4
  97.   setupMenuBar();                       //Setup Menu Bar                      v5
  98.   setupHelp();                          //Setup Help                          v5
  99.  
  100.   sizeTo(ISize(400,300));               //Set the size of main window         v2
  101.   setFocus();                           //Set focus to main window
  102.   show();                               //Set to show main window
  103.  
  104. } /* end AHelloWindow :: AHelloWindow(...) */
  105.  
  106. //**************************************************************************  v4
  107. // AHelloWindow :: setupButtons                                            *   .
  108. //   Setup Buttons                                                         *   .
  109. //**************************************************************************   .
  110. Boolean AHelloWindow :: setupButtons()  //Setup Buttons                        .
  111. {                                       //                                     .
  112.   ISetCanvas    * buttons;              //Define canvas of buttons            v4
  113.   IPushButton   * helpButton;           //Define Help Button                  v5
  114.                                         //                                    v4
  115.   buttons=new ISetCanvas(WND_BUTTONS,   //Create a Set Canvas for Buttons      .
  116.     this, this) ;                       //  Parent and Owner=me                .
  117.   buttons->setMargin(ISize());          //Set Canvas Margins to zero           .
  118.   buttons->setPad(ISize());             //Set Button Canvas Pad to zero        .
  119.   leftButton=new IPushButton(MI_LEFT,   //Create Left Push Button              .
  120.     buttons, buttons, IRectangle(),     //  Parent, Owner=Button Canvas        .
  121.     IPushButton::defaultStyle() |       //  Use Default Styles plus            .
  122.     IControl::tabStop);                 //  tabStop                            .
  123.   leftButton->setText(STR_LEFTB);       //Set Left Button Text                 .
  124.   centerButton=new IPushButton(MI_CENTER,//Create Left Push Button             .
  125.     buttons, buttons, IRectangle(),     //  Parent, Owner=Button Canvas        .
  126.     IPushButton::defaultStyle() |       //  Use Default Styles plus            .
  127.     IControl::tabStop);                 //  tabStop                            .
  128.   centerButton->setText(STR_CENTERB);   //Set Center Button Text               .
  129.   rightButton=new IPushButton(MI_RIGHT, //Create Right Push Button             .
  130.     buttons, buttons, IRectangle(),     //  Parent, Owner=Button Canvas        .
  131.     IPushButton::defaultStyle() |       //  Use Default Styles plus            .
  132.     IControl::tabStop);                 //  tabStop                            .
  133.   rightButton->setText(STR_RIGHTB);     //Set Right Button Text               v4
  134.   helpButton=new IPushButton(MI_HELP,   //Create Right Push Button            v5
  135.     buttons, buttons, IRectangle(),     //  Parent, Owner=Button Canvas        .
  136.     IPushButton::defaultStyle() |       //  Use Default Styles plus            .
  137.     IPushButton::help |                 //  Help Style                         .
  138.     IControl::tabStop);                 //  tabStop                            .
  139.   helpButton->setText(STR_HELPB);       //Set Help Button Text                v5
  140.   addExtension(buttons,                 //Add Buttons Canvas                  v4
  141.     IFrameWindow::belowClient,          //  below client and specify           .
  142.     30UL);                              //  unsigned long height in pixels     .
  143.   return true;                          //Return                               .
  144. } /* end AHelloWindow :: setupButtons() */                                  //v4
  145.  
  146. //**************************************************************************  v5
  147. // AHelloWindow :: setupHelp()                                             *   .
  148. //   Setup Help                                                            *   .
  149. //**************************************************************************   .
  150. Boolean AHelloWindow :: setupHelp()     //Setup Help Area                      .
  151. {                                       //                                     .
  152.   help=new IHelpWindow(HELP_TABLE,      //Create Help Window Object            .
  153.     this);                              //Setup Help info                      .
  154.   help->addLibraries("AHELLOW5.HLP");   //  set self, help table filename      .
  155.   help->setTitle(STR_HTITLE);           //Set the Help Window Title            .
  156.                                         //                                     .
  157.   AHelpHandler* phelpHandler=           //Create Custon Help Handler to        .
  158.     new AHelpHandler();                 //  handle the Keys Help               .
  159.   phelpHandler->handleEventsFor(this);  //Start Help Handler                   .
  160.   return true;                          //                                     .
  161. } /* end AHelloWindow :: setupHelp() */                                     //v5
  162.  
  163. //**************************************************************************  v5
  164. // AHelloWindow :: setupClient()                                           *   .
  165. //   Setup Client                                                          *   .
  166. //**************************************************************************   .
  167. Boolean AHelloWindow :: setupClient()   //Setup Client Window                  .
  168. {                                       //                                     .
  169.   clientWindow=new ISplitCanvas(        //Create Canvas                        .
  170.     WND_CANVAS, this, this);            //  with Window Id, parent, owner      .
  171.   setClient(clientWindow);              //Set canvas as Client Window          .
  172.  
  173.   helloCanvas=new ISplitCanvas(         //Create Hello Canvas                  .
  174.     WND_HCANVAS, clientWindow,          //  with Window Id, parent             .
  175.     clientWindow);                      //  and owner                          .
  176.   helloCanvas->setOrientation(          //Set the orientation                  .
  177.     ISplitCanvas::horizontalSplit);     //  to horizontal                     v5
  178.  
  179.   hello=new IStaticText(WND_HELLO,      //Create Static Text Control
  180.     helloCanvas, helloCanvas);          //  Pass in client as owner & parent  v5
  181.   hello->setText(STR_HELLO);            //Set text                            v2
  182.  
  183.   earthWindow  = new AEarthWindow       //Create Earth Graphic Window         v5
  184.     (WND_EARTH, helloCanvas);           //  Set Window ID, client-owner/parentv5
  185.  
  186.   hello->setAlignment(                  //Set Alignment to Center in both
  187.     IStaticText::centerCenter);         //    directions
  188.  
  189.   listBox=new IListBox(WND_LISTBOX,     //Create ListBox                      v5
  190.     clientWindow, clientWindow,         //  Parent/Owner is ClientWindow       .
  191.     IRectangle(),                       //                                     .
  192.     IListBox::defaultStyle() |          //                                     .
  193.     IControl::tabStop |                 //  Set Tab Stop                       .
  194.     IListBox::noAdjustPosition);        //  Allow the Canvas to control size   .
  195.   listBox->addAsc("Hello, World!");     //Add "Hello, World!"                  .
  196.   listBox->addAsc("Hi, World!");        //Add "Hi, World!"                     .
  197.   listBox->addAsc("Howdy, World!");     //Add "Howdy, World!"                  .
  198.   listBox->addAsc("Alo, Mundo!");       //Add Portuguese Version               .
  199.   listBox->addAsc("Ola, Mondo!");       //Add Spain                            .
  200.   listBox->addAsc("Hallo wereld!");     //Add Dutch                            .
  201.   listBox->addAsc("Hallo Welt!");       //Add German                           .
  202.   listBox->addAsc("Bonjour le monde!"); //Add French                           .
  203.   ISelectHandler::handleEventsFor(listBox);//Set self as select event handler  .
  204.                                         //                                     .
  205.   clientWindow->setSplitWindowPercentage(//Set the Window Percentage for       .
  206.     helloCanvas, 60);                   //  the helloCanvas to 60              .
  207.   clientWindow->setSplitWindowPercentage(//Set the Window Percentage for       .
  208.     listBox, 40);                       //  the listBox to 40                  .
  209.                                         //                                     .
  210.   return true;                          //                                     .
  211. } /* end AHelloWindow :: setupClient() */                                   //v5
  212.  
  213. //**************************************************************************  v5
  214. // AHelloWindow :: setupInfoArea()                                         *   .
  215. //   Setup Information Area                                                *   .
  216. //**************************************************************************   .
  217. Boolean AHelloWindow :: setupInfoArea() //Setup Information Area               .
  218. {                                       //                                    v5
  219.   infoArea=new IInfoArea(this);         //Create the information area         v2
  220.   infoArea->setInactiveText(STR_INFO);  //Set information area text from RC   v2
  221.   setExtensionSize(infoArea,            //                                    v5
  222.     (int)IFont(infoArea).maxCharHeight());//and specify height                 .
  223.   return true;                          //                                     .
  224. } /* end AHelloWindow :: setupInfoArea() */                                 //v5
  225.  
  226. //**************************************************************************  v5
  227. // AHelloWindow :: setupMenuBar()                                          *   .
  228. //   Setup Menu Bar                                                        *   .
  229. //**************************************************************************   .
  230. Boolean AHelloWindow :: setupMenuBar()  //Setup Menu Bar                       .
  231. {                                       //                                     .
  232.   ICommandHandler::handleEventsFor(this);//Set self as command event handler  v5
  233.   menuBar=new IMenuBar(WND_MAIN,        //Create Menu Bar for main window     v3
  234.     this);                              //  Set self as parent                 .
  235.   menuBar->checkItem(MI_CENTER);        //Place Check on Center Menu Item     v3
  236.   return true;                          //                                    v5
  237. } /* end AHelloWindow :: setupMenuBar() */                                  //v5
  238.  
  239. //**************************************************************************  v5
  240. // AHelloWindow :: setupStatusArea()                                       *   .
  241. //   Setup Statue Area                                                     *   .
  242. //**************************************************************************   .
  243. Boolean AHelloWindow :: setupStatusArea()//Setup Status Area                   .
  244. {                                       //                                    v5
  245.   statusLine=new IStaticText            //Create Status Area using Static Textv3
  246.     (WND_STATUS, this, this);           //  Window ID, Parent, Owner Parameters.
  247.   statusLine->setText(STR_CENTER);      //Set Status Text to "Center" from Res .
  248.   addExtension(statusLine,              //Add Status Line above the client     .
  249.     IFrameWindow::aboveClient,          //  and specify the height             .
  250.     IFont(statusLine).maxCharHeight()); //  and specify height                v3
  251.   return true;                          //                                    v5
  252. } /* end AHelloWindow :: setupStatusArea() */                               //v5
  253.  
  254. //**************************************************************************  v5
  255. // AHelloWindow :: setText(...)                                            *   .
  256. //   Set Text                                                              *   .
  257. //**************************************************************************   .
  258. Boolean AHelloWindow :: setText(const char* text)//Set Text using String       .
  259. {                                       //                                     .
  260.   hello->setText(text);                 //Set Text in Control                  .
  261.   return true;                          //Return                               .
  262. } /* end AHelloWindow :: setText(...) */                                    //v5
  263.  
  264. //**************************************************************************  v5
  265. // AHelloWindow :: selected(...)                                           *   .
  266. //   Handle selected command from list box                                 *   .
  267. //                                                                         *   .
  268. // Note: It would be easy to change this selected member function to enter *   .
  269. //**************************************************************************   .
  270. Boolean AHelloWindow :: selected(IControlEvent & evt)                       // .
  271. {                                                                           // .
  272.   IListBox::Cursor lbCursor(*listBox);  //List Box Cursor                      .
  273.   lbCursor.setToFirst();                //Set to first item selected           .
  274.   setText(listBox->elementAt(lbCursor));//Set Hello Text to Selected Item      .
  275.   return true;                          //Return Command Processed             .
  276. } /* end AHelloWindow :: selected(...) */                                   //v5
  277.  
  278. //**************************************************************************  v3
  279. // AHelloWindow :: command                                                 *   .
  280. //   Handle menu commands                                                  *   .
  281. //**************************************************************************   .
  282. Boolean AHelloWindow :: command(ICommandEvent & cmdEvent)                   // .
  283. {                                                                           //v3
  284.   IString temp;                         //String to pass in/out from dialog   v4
  285.   unsigned short value;                 //Return value from dialog            v4
  286.  
  287.   switch (cmdEvent.commandId()) {       //Get command id                      v3
  288.  
  289.     case MI_CENTER:                     //Code to Process Center Command Item v3
  290.       hello->setAlignment(              //Set alignment of hello text to       .
  291.         IStaticText::centerCenter);     //  center-vertical, center-horizontal .
  292.       statusLine->setText(STR_CENTER);  //Set Status Text to "Center" from Res .
  293.       menuBar->checkItem(MI_CENTER);    //Place Check on Center Menu Item      .
  294.       menuBar->uncheckItem(MI_LEFT);    //Uncheck Left Menu Item               .
  295.       menuBar->uncheckItem(MI_RIGHT);   //Uncheck Right Menu Item              .
  296.       return(true);                     //Return command processed             .
  297.       break;                            //                                    v3
  298.  
  299.     case MI_LEFT:                       //Code to Process Left Command Item   v3
  300.       hello->setAlignment(              //Set alignment of hello text to       .
  301.         IStaticText::centerLeft);       //  center-vertical, left-horizontal   .
  302.       statusLine->setText(STR_LEFT);    //Set Status Text to "Left" from Res   .
  303.       menuBar->uncheckItem(MI_CENTER);  //Uncheck Center Menu Item             .
  304.       menuBar->checkItem(MI_LEFT);      //Place Check on Left Menu Item        .
  305.       menuBar->uncheckItem(MI_RIGHT);   //Uncheck Right Menu Item              .
  306.       return(true);                     //Return command processed             .
  307.       break;                            //                                    v3
  308.  
  309.     case MI_RIGHT:                      //Code to Process Right Command Item  v3
  310.       hello->setAlignment(              //Set alignment of hello text to       .
  311.         IStaticText::centerRight);      //  center-vertical, right-horizontal  .
  312.       statusLine->setText(STR_RIGHT);   //Set Status Text to "Right" from Res  .
  313.       menuBar->uncheckItem(MI_CENTER);  //Uncheck Center Menu Item             .
  314.       menuBar->uncheckItem(MI_LEFT);    //Uncheck Left Menu Item               .
  315.       menuBar->checkItem(MI_RIGHT);     //Place Check on Right Menu Item       .
  316.       return(true);                     //Return command processed             .
  317.       break;                            //                                    v3
  318.  
  319.     case MI_TEXT:                       //Code to Process Text Command        v4
  320.       {
  321.       temp=hello->text();               //Get current Hello text               .
  322.       infoArea->setInactiveText(        //Set Info Area to Dialog Active       .
  323.         STR_INFODLG);                   //  Text from Resource File            .
  324.       ATextDialog * textDialog=new      //Create a Text Dialog                 .
  325.         ATextDialog(temp, this);        //                                     .
  326.       textDialog->showModally();        //Show this Text Dialog as Modal       .
  327.       value=textDialog->result();       //Get result (eg OK or Cancel)         .
  328.       if (value != DID_CANCEL)          //Set new string if not canceled       .
  329.         hello->setText(temp);           //Set Hello to Text from Dialog        .
  330.       infoArea->setText(STR_INFO);      //Set Info Text to "Normal" from Res   .
  331.       delete textDialog;                //Delete textDialog                    .
  332.       return(true);                     //Return Command Processed             .
  333.       break;                            //                                    v4
  334.       }
  335.  
  336.     case MI_GENERAL_HELP:               //Code to Process Help for help       v5
  337.       help->show(IHelpWindow::general); //Show General Help Panel              .
  338.       return(true);                     //Return command processed             .
  339.       break;                            //                                    v5
  340.  
  341.   } /* end switch */                    //                                    v3
  342.  
  343.   return(false);                        //Return command not processed        v3
  344. } /* end AHelloWindow :: command(...) */                                    //v3
  345.  
  346. //**************************************************************************  v5
  347. // AHelpHandler :: keysHelpId                                              *   .
  348. //   Handle the keys help request event                                    *   .
  349. //   This overrides the default provided by IBMCLASS                       *   .
  350. //**************************************************************************   .
  351. Boolean AHelpHandler :: keysHelpId(IEvent& evt) //                             .
  352. {                                                //                            .
  353.   evt.setResult(1000);                           //1000=keys help id in        .
  354.                                                  //  ahellow5.ipf file         .
  355.   return true;                                   //Return command processed    .
  356. } /* end AHelpHandler :: keysHelpId(...) */                                 //v5
  357.