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

  1. /******************************************************************************/
  2. /* HELLO WORLD SAMPLE PROGRAM - Version 4: Class Implementation (AHELLOW4.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.                                         //Include IBM UI class headers:
  46. #include <iapp.hpp>                     //IApplication Class
  47. #include <istattxt.hpp>                 //IStaticText Class
  48. #include <iinfoa.hpp>                   //IInfoArea Class                     v2
  49. #include <imenubar.hpp>                 //IMenuBar Class                      v3
  50. #include <ifont.hpp>                    //IFont                               v3
  51. #include <istring.hpp>                  //IString Class                       v4
  52. #include <isetcv.hpp>                   //ISetCanvas Class                    v4
  53. #include <ipushbut.hpp>                 //IPushButton Class                   v4
  54.  
  55. #include "ahellow4.hpp"                 //Include AHelloWindow Class headers  v2
  56. #include "ahellow4.h"                   //Include our Symbolic definitions    v2
  57. #include "adialog4.hpp"                 //ATextDialog Class                   v4
  58.  
  59. //*************************************************************************
  60. // main  - Application entry point                                        *
  61. //*************************************************************************
  62. void main()                             //Main Procedure with no parameters
  63. {
  64.   AHelloWindow mainWindow (WND_MAIN);   //Create our main window on the
  65.                                         // desktop
  66.   IApplication::current().run();        //Get the current application and
  67.                                         // run it
  68. } /* end main */
  69.  
  70. //**************************************************************************
  71. // AHelloWindow :: AHelloWindow - Constructor for our main window          *
  72. //**************************************************************************
  73. AHelloWindow :: AHelloWindow(unsigned long windowId)
  74.   : IFrameWindow (                      //Call IFrameWindow constructor       v2
  75.     IFrameWindow::defaultStyle()        //  Use default plus                  v2
  76.     | IFrameWindow::minimizedIcon       //  Get Minimized Icon from  RC file  v2
  77.     | IFrameWindow::accelerator,        //  Get Accelerator Table from RC filev4
  78.     windowId)                           //  Main Window ID
  79. {
  80.   hello = new IStaticText(WND_HELLO,    //Create Static Text Control
  81.     this, this);                        //  Pass in myself as owner & parent
  82.   hello->setText(STR_HELLO);            //Set text in Static Text Control     v2
  83.   hello->setAlignment(                  //Set Alignment to Center in both
  84.     IStaticText::centerCenter);         //    directions
  85.   setClient(hello);                     //Set hello control as Client Window
  86.  
  87.   infoArea=new IInfoArea(this);         //Create the information area         v2
  88.   infoArea->setInactiveText(STR_INFO);  //Set information area text from RC   v2
  89.  
  90.   statusLine=new IStaticText            //Create Status Area using Static Textv3
  91.     (WND_STATUS, this, this);           //  Window ID, Parent, Owner Parameters.
  92.   statusLine->setText(STR_CENTER);      //Set Status Text to "Center" from Res .
  93.   addExtension(statusLine,              //Add Status Line above the client     .
  94.     IFrameWindow::aboveClient,          //  and specify the height             .
  95.     IFont(statusLine).maxCharHeight()); //  and specify height                v3
  96.  
  97.   handleEventsFor(this);                //Set self as event handler (commands)v3
  98.   menuBar=new IMenuBar(WND_MAIN, this); //Create Menu Bar for main window      .
  99.   menuBar->checkItem(MI_CENTER);        //Place Check on Center Menu Item     v3
  100.  
  101.   setupButtons();                       //Setup Buttons                       v4
  102.  
  103.   sizeTo(ISize(400,300));               //Set the size of main window         v2
  104.   setFocus();                           //Set focus to main window
  105.   show();                               //Set to show main window
  106.  
  107. } /* end AHelloWindow :: AHelloWindow(...) */
  108.  
  109. //**************************************************************************  v4
  110. // AHelloWindow :: setupButtons                                            *   .
  111. //   Setup Buttons                                                         *   .
  112. //**************************************************************************   .
  113. Boolean AHelloWindow :: setupButtons()  //Setup Buttons                        .
  114. {                                       //                                     .
  115.   ISetCanvas    * buttons;              //Define canvas of buttons             .
  116.                                         //                                     .
  117.   buttons=new ISetCanvas(WND_BUTTONS,   //Create a Set Canvas for Buttons      .
  118.     this, this) ;                       //  Parent and Owner=me                .
  119.   buttons->setMargin(ISize());          //Set Canvas Margins to zero           .
  120.   buttons->setPad(ISize());             //Set Button Canvas Pad to zero        .
  121.   leftButton=new IPushButton(MI_LEFT,   //Create Left Push Button              .
  122.     buttons, buttons, IRectangle(),     //  Parent, Owner=Button Canvas        .
  123.     IPushButton::defaultStyle() |       //  Use Default Styles plus            .
  124.     IControl::tabStop);                 //  tabStop                            .
  125.   leftButton->setText(STR_LEFTB);       //Set Left Button Text                 .
  126.   centerButton=new IPushButton(MI_CENTER,//Create Left Push Button             .
  127.     buttons, buttons, IRectangle(),     //  Parent, Owner=Button Canvas        .
  128.     IPushButton::defaultStyle() |       //  Use Default Styles plus            .
  129.     IControl::tabStop);                 //  tabStop                            .
  130.   centerButton->setText(STR_CENTERB);   //Set Center Button Text               .
  131.   rightButton=new IPushButton(MI_RIGHT, //Create Right Push Button             .
  132.     buttons, buttons, IRectangle(),     //  Parent, Owner=Button Canvas        .
  133.     IPushButton::defaultStyle() |       //  Use Default Styles plus            .
  134.     IControl::tabStop);                 //  tabStop                            .
  135.   rightButton->setText(STR_RIGHTB);     //Set Right Button Text                .
  136.   addExtension(buttons,                 //Add Buttons Canvas                   .
  137.     IFrameWindow::belowClient,          //  below client and                   .
  138.     30UL);                              //  specify height in pixels           .
  139.   return true;                          //Return                               .
  140. } /* end AHelloWindow :: setupButtons() */                                  //v4
  141.  
  142. //**************************************************************************  v3
  143. // AHelloWindow :: command                                                 *   .
  144. //   Handle menu commands                                                  *   .
  145. //**************************************************************************   .
  146. Boolean AHelloWindow :: command(ICommandEvent & cmdEvent)                   // .
  147. {                                                                           //v3
  148.   IString temp;                         //String to pass in/out from dialog   v4
  149.   unsigned short value;                 //Return value from dialog            v4
  150.   switch (cmdEvent.commandId()) {       //Get command id                      v3
  151.  
  152.     case MI_CENTER:                     //Code to Process Center Command Item v3
  153.       hello->setAlignment(              //Set alignment of hello text to       .
  154.         IStaticText::centerCenter);     //  center-vertical, center-horizontal .
  155.       statusLine->setText(STR_CENTER);  //Set Status Text to "Center" from Res .
  156.       menuBar->checkItem(MI_CENTER);    //Place Check on Center Menu Item      .
  157.       menuBar->uncheckItem(MI_LEFT);    //Uncheck Left Menu Item               .
  158.       menuBar->uncheckItem(MI_RIGHT);   //Uncheck Right Menu Item              .
  159.       return(true);                     //Return command processed             .
  160.       break;                            //                                    v3
  161.  
  162.     case MI_LEFT:                       //Code to Process Left Command Item   v3
  163.       hello->setAlignment(              //Set alignment of hello text to       .
  164.         IStaticText::centerLeft);       //  center-vertical, left-horizontal   .
  165.       statusLine->setText(STR_LEFT);    //Set Status Text to "Left" from Res   .
  166.       menuBar->uncheckItem(MI_CENTER);  //Uncheck Center Menu Item             .
  167.       menuBar->checkItem(MI_LEFT);      //Place Check on Left Menu Item        .
  168.       menuBar->uncheckItem(MI_RIGHT);   //Uncheck Right Menu Item              .
  169.       return(true);                     //Return command processed             .
  170.       break;                            //                                    v3
  171.  
  172.     case MI_RIGHT:                      //Code to Process Right Command Item  v3
  173.       hello->setAlignment(              //Set alignment of hello text to       .
  174.         IStaticText::centerRight);      //  center-vertical, right-horizontal  .
  175.       statusLine->setText(STR_RIGHT);   //Set Status Text to "Right" from Res  .
  176.       menuBar->uncheckItem(MI_CENTER);  //Uncheck Center Menu Item             .
  177.       menuBar->uncheckItem(MI_LEFT);    //Uncheck Left Menu Item               .
  178.       menuBar->checkItem(MI_RIGHT);     //Place Check on Right Menu Item       .
  179.       return(true);                     //Return command processed             .
  180.       break;                            //                                    v3
  181.  
  182.     case MI_TEXT:                       //Code to Process Text Command        v4
  183.       {
  184.       temp=hello->text();               //Get current Hello text               .
  185.       infoArea->setInactiveText(        //Set Info Area to Dialog Active       .
  186.         STR_INFODLG);                   //  Text from Resource File            .
  187.       ATextDialog * textDialog=new      //Create a Text Dialog                 .
  188.         ATextDialog(temp, this);        //                                     .
  189.       textDialog->showModally();        //Show this Text Dialog as Modal       .
  190.       value=textDialog->result();       //Get result (eg OK or Cancel)         .
  191.       if (value != DID_CANCEL)          //Set new string if not canceled       .
  192.         hello->setText(temp);           //Set Hello to Text from Dialog        .
  193.       infoArea->setText(STR_INFO);      //Set Info Text to "Normal" from Res   .
  194.       delete textDialog;                //Delete textDialog                    .
  195.       return(true);                     //Return Command Processed             .
  196.       break;                            //                                    v4
  197.       }
  198.  
  199.   } /* end switch */                    //                                    v3
  200.  
  201.   return(false);                        //Return command not processed        v3
  202. } /* end HelloWindow :: command(...) */                                     //v3
  203.