home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cset21v1.zip / IBMCPP / SAMPLES / ICLUI / HELLO5 / AHELLOW5.CPP < prev    next >
Text File  |  1993-05-12  |  26KB  |  358 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.     IButton::noPointerFocus |           //  noPointerFocus                     .
  139.     IControl::tabStop);                 //  tabStop                            .
  140.   helpButton->setText(STR_HELPB);       //Set Help Button Text                v5
  141.   addExtension(buttons,                 //Add Buttons Canvas                  v4
  142.     IFrameWindow::belowClient,          //  below client and specify           .
  143.     30UL);                              //  unsigned long height in pixels     .
  144.   return true;                          //Return                               .
  145. } /* end AHelloWindow :: setupButtons() */                                  //v4
  146.  
  147. //**************************************************************************  v5
  148. // AHelloWindow :: setupHelp()                                             *   .
  149. //   Setup Help                                                            *   .
  150. //**************************************************************************   .
  151. Boolean AHelloWindow :: setupHelp()     //Setup Help Area                      .
  152. {                                       //                                     .
  153.   help=new IHelpWindow(HELP_TABLE,      //Create Help Window Object            .
  154.     this);                              //Setup Help info                      .
  155.   help->addLibraries("AHELLOW5.HLP");   //  set self, help table filename      .
  156.   help->setTitle(STR_HTITLE);           //Set the Help Window Title            .
  157.                                         //                                     .
  158.   AHelpHandler* phelpHandler=           //Create Custon Help Handler to        .
  159.     new AHelpHandler();                 //  handle the Keys Help               .
  160.   phelpHandler->handleEventsFor(this);  //Start Help Handler                   .
  161.   return true;                          //                                     .
  162. } /* end AHelloWindow :: setupHelp() */                                     //v5
  163.  
  164. //**************************************************************************  v5
  165. // AHelloWindow :: setupClient()                                           *   .
  166. //   Setup Client                                                          *   .
  167. //**************************************************************************   .
  168. Boolean AHelloWindow :: setupClient()   //Setup Client Window                  .
  169. {                                       //                                     .
  170.   clientWindow=new ISplitCanvas(        //Create Canvas                        .
  171.     WND_CANVAS, this, this);            //  with Window Id, parent, owner      .
  172.   setClient(clientWindow);              //Set canvas as Client Window          .
  173.  
  174.   helloCanvas=new ISplitCanvas(         //Create Hello Canvas                  .
  175.     WND_HCANVAS, clientWindow,          //  with Window Id, parent             .
  176.     clientWindow);                      //  and owner                          .
  177.   helloCanvas->setOrientation(          //Set the orientation                  .
  178.     ISplitCanvas::horizontalSplit);     //  to horizontal                     v5
  179.  
  180.   hello=new IStaticText(WND_HELLO,      //Create Static Text Control
  181.     helloCanvas, helloCanvas);          //  Pass in client as owner & parent  v5
  182.   hello->setText(STR_HELLO);            //Set text                            v2
  183.  
  184.   earthWindow  = new AEarthWindow       //Create Earth Graphic Window         v5
  185.     (WND_EARTH, helloCanvas);           //  Set Window ID, client-owner/parentv5
  186.  
  187.   hello->setAlignment(                  //Set Alignment to Center in both
  188.     IStaticText::centerCenter);         //    directions
  189.  
  190.   listBox=new IListBox(WND_LISTBOX,     //Create ListBox                      v5
  191.     clientWindow, clientWindow,         //  Parent/Owner is ClientWindow       .
  192.     IRectangle(),                       //                                     .
  193.     IListBox::defaultStyle() |          //                                     .
  194.     IControl::tabStop |                 //  Set Tab Stop                       .
  195.     IListBox::noAdjustPosition);        //  Allow the Canvas to control size   .
  196.   listBox->addAscending("Hello, World!");    //Add "Hello, World!"             .
  197.   listBox->addAscending("Hi, World!");       //Add "Hi, World!"                .
  198.   listBox->addAscending("Howdy, World!");    //Add "Howdy, World!"             .
  199.   listBox->addAscending("Alo, Mundo!");      //Add Portuguese Version          .
  200.   listBox->addAscending("Ola, Mondo!");      //Add Spain                       .
  201.   listBox->addAscending("Hallo wereld!");    //Add Dutch                       .
  202.   listBox->addAscending("Hallo Welt!");      //Add German                      .
  203.   listBox->addAscending("Bonjour le monde!");//Add French                      .
  204.   ISelectHandler::handleEventsFor(listBox);//Set self as select event handler  .
  205.                                         //                                     .
  206.   clientWindow->setSplitWindowPercentage(//Set the Window Percentage for       .
  207.     helloCanvas, 60);                   //  the helloCanvas to 60              .
  208.   clientWindow->setSplitWindowPercentage(//Set the Window Percentage for       .
  209.     listBox, 40);                       //  the listBox to 40                  .
  210.                                         //                                     .
  211.   return true;                          //                                     .
  212. } /* end AHelloWindow :: setupClient() */                                   //v5
  213.  
  214. //**************************************************************************  v5
  215. // AHelloWindow :: setupInfoArea()                                         *   .
  216. //   Setup Information Area                                                *   .
  217. //**************************************************************************   .
  218. Boolean AHelloWindow :: setupInfoArea() //Setup Information Area               .
  219. {                                       //                                    v5
  220.   infoArea=new IInfoArea(this);         //Create the information area         v2
  221.   infoArea->setInactiveText(STR_INFO);  //Set information area text from RC   v2
  222.   setExtensionSize(infoArea,            //                                    v5
  223.     (int)IFont(infoArea).maxCharHeight());//and specify height                 .
  224.   return true;                          //                                     .
  225. } /* end AHelloWindow :: setupInfoArea() */                                 //v5
  226.  
  227. //**************************************************************************  v5
  228. // AHelloWindow :: setupMenuBar()                                          *   .
  229. //   Setup Menu Bar                                                        *   .
  230. //**************************************************************************   .
  231. Boolean AHelloWindow :: setupMenuBar()  //Setup Menu Bar                       .
  232. {                                       //                                     .
  233.   ICommandHandler::handleEventsFor(this);//Set self as command event handler  v5
  234.   menuBar=new IMenuBar(WND_MAIN,        //Create Menu Bar for main window     v3
  235.     this);                              //  Set self as parent                 .
  236.   menuBar->checkItem(MI_CENTER);        //Place Check on Center Menu Item     v3
  237.   return true;                          //                                    v5
  238. } /* end AHelloWindow :: setupMenuBar() */                                  //v5
  239.  
  240. //**************************************************************************  v5
  241. // AHelloWindow :: setupStatusArea()                                       *   .
  242. //   Setup Statue Area                                                     *   .
  243. //**************************************************************************   .
  244. Boolean AHelloWindow :: setupStatusArea()//Setup Status Area                   .
  245. {                                       //                                    v5
  246.   statusLine=new IStaticText            //Create Status Area using Static Textv3
  247.     (WND_STATUS, this, this);           //  Window ID, Parent, Owner Parameters.
  248.   statusLine->setText(STR_CENTER);      //Set Status Text to "Center" from Res .
  249.   addExtension(statusLine,              //Add Status Line above the client     .
  250.     IFrameWindow::aboveClient,          //  and specify the height             .
  251.     IFont(statusLine).maxCharHeight()); //  and specify height                v3
  252.   return true;                          //                                    v5
  253. } /* end AHelloWindow :: setupStatusArea() */                               //v5
  254.  
  255. //**************************************************************************  v5
  256. // AHelloWindow :: setText(...)                                            *   .
  257. //   Set Text                                                              *   .
  258. //**************************************************************************   .
  259. Boolean AHelloWindow :: setText(const char* text)//Set Text using String       .
  260. {                                       //                                     .
  261.   hello->setText(text);                 //Set Text in Control                  .
  262.   return true;                          //Return                               .
  263. } /* end AHelloWindow :: setText(...) */                                    //v5
  264.  
  265. //**************************************************************************  v5
  266. // AHelloWindow :: selected(...)                                           *   .
  267. //   Handle selected command from list box                                 *   .
  268. //                                                                         *   .
  269. // Note: It would be easy to change this selected member function to enter *   .
  270. //**************************************************************************   .
  271. Boolean AHelloWindow :: selected(IControlEvent & evt)                       // .
  272. {                                                                           // .
  273.   IListBox::Cursor lbCursor(*listBox);  //List Box Cursor                      .
  274.   lbCursor.setToFirst();                //Set to first item selected           .
  275.   setText(listBox->elementAt(lbCursor));//Set Hello Text to Selected Item      .
  276.   return true;                          //Return Command Processed             .
  277. } /* end AHelloWindow :: selected(...) */                                   //v5
  278.  
  279. //**************************************************************************  v3
  280. // AHelloWindow :: command                                                 *   .
  281. //   Handle menu commands                                                  *   .
  282. //**************************************************************************   .
  283. Boolean AHelloWindow :: command(ICommandEvent & cmdEvent)                   // .
  284. {                                                                           //v3
  285.   IString temp;                         //String to pass in/out from dialog   v4
  286.   unsigned short value;                 //Return value from dialog            v4
  287.  
  288.   switch (cmdEvent.commandId()) {       //Get command id                      v3
  289.  
  290.     case MI_CENTER:                     //Code to Process Center Command Item v3
  291.       hello->setAlignment(              //Set alignment of hello text to       .
  292.         IStaticText::centerCenter);     //  center-vertical, center-horizontal .
  293.       statusLine->setText(STR_CENTER);  //Set Status Text to "Center" from Res .
  294.       menuBar->checkItem(MI_CENTER);    //Place Check on Center Menu Item      .
  295.       menuBar->uncheckItem(MI_LEFT);    //Uncheck Left Menu Item               .
  296.       menuBar->uncheckItem(MI_RIGHT);   //Uncheck Right Menu Item              .
  297.       return(true);                     //Return command processed             .
  298.       break;                            //                                    v3
  299.  
  300.     case MI_LEFT:                       //Code to Process Left Command Item   v3
  301.       hello->setAlignment(              //Set alignment of hello text to       .
  302.         IStaticText::centerLeft);       //  center-vertical, left-horizontal   .
  303.       statusLine->setText(STR_LEFT);    //Set Status Text to "Left" from Res   .
  304.       menuBar->uncheckItem(MI_CENTER);  //Uncheck Center Menu Item             .
  305.       menuBar->checkItem(MI_LEFT);      //Place Check on Left Menu Item        .
  306.       menuBar->uncheckItem(MI_RIGHT);   //Uncheck Right Menu Item              .
  307.       return(true);                     //Return command processed             .
  308.       break;                            //                                    v3
  309.  
  310.     case MI_RIGHT:                      //Code to Process Right Command Item  v3
  311.       hello->setAlignment(              //Set alignment of hello text to       .
  312.         IStaticText::centerRight);      //  center-vertical, right-horizontal  .
  313.       statusLine->setText(STR_RIGHT);   //Set Status Text to "Right" from Res  .
  314.       menuBar->uncheckItem(MI_CENTER);  //Uncheck Center Menu Item             .
  315.       menuBar->uncheckItem(MI_LEFT);    //Uncheck Left Menu Item               .
  316.       menuBar->checkItem(MI_RIGHT);     //Place Check on Right Menu Item       .
  317.       return(true);                     //Return command processed             .
  318.       break;                            //                                    v3
  319.  
  320.     case MI_TEXT:                       //Code to Process Text Command        v4
  321.       {
  322.       temp=hello->text();               //Get current Hello text               .
  323.       infoArea->setInactiveText(        //Set Info Area to Dialog Active       .
  324.         STR_INFODLG);                   //  Text from Resource File            .
  325.       ATextDialog * textDialog=new      //Create a Text Dialog                 .
  326.         ATextDialog(temp, this);        //                                     .
  327.       textDialog->showModally();        //Show this Text Dialog as Modal       .
  328.       value=textDialog->result();       //Get result (eg OK or Cancel)         .
  329.       if (value != DID_CANCEL)          //Set new string if not canceled       .
  330.         hello->setText(temp);           //Set Hello to Text from Dialog        .
  331.       infoArea->setInactiveText(STR_INFO);//Set information area text from RC  .
  332.       delete textDialog;                //Delete textDialog                    .
  333.       return(true);                     //Return Command Processed             .
  334.       break;                            //                                    v4
  335.       }
  336.  
  337.     case MI_GENERAL_HELP:               //Code to Process Help for help       v5
  338.       help->show(IHelpWindow::general); //Show General Help Panel              .
  339.       return(true);                     //Return command processed             .
  340.       break;                            //                                    v5
  341.  
  342.   } /* end switch */                    //                                    v3
  343.  
  344.   return(false);                        //Return command not processed        v3
  345. } /* end AHelloWindow :: command(...) */                                    //v3
  346.  
  347. //**************************************************************************  v5
  348. // AHelpHandler :: keysHelpId                                              *   .
  349. //   Handle the keys help request event                                    *   .
  350. //   This overrides the default provided by IBMCLASS                       *   .
  351. //**************************************************************************   .
  352. Boolean AHelpHandler :: keysHelpId(IEvent& evt) //                             .
  353. {                                                //                            .
  354.   evt.setResult(1000);                           //1000=keys help id in        .
  355.                                                  //  ahellow5.ipf file         .
  356.   return true;                                   //Return command processed    .
  357. } /* end AHelpHandler :: keysHelpId(...) */                                 //v5
  358.