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