home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / VSCPPv8.zip / VACPP / IBMCPP / samples / IOC / LISTBOX / ALISTBOX.CPP next >
Text File  |  1995-05-01  |  12KB  |  254 lines

  1. /******************************************************************************/
  2. /* List Box Sample Program                                                    */
  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. /* List Box Sample Program                                                    */
  18. /*   key functions:                                                           */
  19. /*      - create a main window                                                */
  20. /*      - run the current application                                         */
  21. /*      - use listbox as client area                                          */
  22. /*      - use listbox cursors to retrieve items from listbox                  */
  23. /*      - use infoarea control                                                */
  24. /*      - have a static text control as a frame extension                     */
  25. /*      - demonstrate the use of tracing macros                               */
  26. /*      - try-catch block around a piece of code                              */
  27. /*      - extract information from an exception object                        */
  28. /*      - register a default exception handler                                */
  29. /*      - create and display a message box                                    */
  30. /*      - process 'Command' events generated by menu selection                */
  31. /*      - load strings from resource bound to the exe                         */
  32. /******************************************************************************/
  33.  
  34. #ifndef _IBASE_                         //Make sure ibase.hpp is included
  35.   #include <ibase.hpp>                  //  since that is where IC_<environ>
  36. #endif                                  //  is defined.
  37. #define  IC_TRACE_DEVELOP
  38.                                         // Include IBM UI class headers:
  39. #include <iapp.hpp>                     // IApplication
  40. #include <istattxt.hpp>                 // IStaticText
  41. #include <ilistbox.hpp>                 // IListBox
  42. #include <irect.hpp>                    // IRectangle
  43. #include <imenubar.hpp>                 // IMenuBar
  44. #include <istring.hpp>                  // IString
  45. #include <iinfoa.hpp>                   // IInfoArea
  46. #include <itrace.hpp>                   // ITrace
  47. #include <imsgbox.hpp>                  // IMessageBox
  48. #include <iexcbase.hpp>                 // IException
  49.  
  50. #include "alistbox.h"                   // include our Symbolic definitions
  51. #include "alistbox.hpp"                 // include AListBox Class headers
  52.  
  53. /******************************************************************************/
  54. /* main  - Application entry point                                            */
  55. /******************************************************************************/
  56. int main()
  57. {
  58.   AListBox mainWindow (WND_MAIN);       // create main window
  59.   IApplication::current().run();        // start message loop processing
  60.   return 0;
  61. } /* end main */
  62.  
  63. /******************************************************************************/
  64. /* AListBox :: AListBox - constructor for main window                         */
  65. /******************************************************************************/
  66. AListBox :: AListBox(unsigned long windowId)
  67.   : IFrameWindow( IFrameWindow::defaultStyle()  //Call the IFrame Constructor
  68.             | IFrameWindow::accelerator         //with defaultSytles+accelerator
  69.             | IFrameWindow::minimizedIcon,      //+an icon
  70.             windowId),                          //and window Id
  71.     listbox(WND_HELLO, this, this, IRectangle(),  //Create listbox
  72.             (IListBox::defaultStyle()
  73.             | IListBox::multipleSelect)
  74.             & ~IListBox::horizontalScroll),
  75.     listbox2(WND_HELLO, this, this, IRectangle(), //Create trace listbox
  76.             IListBox::defaultStyle()
  77.             & ~IListBox::horizontalScroll),
  78.     infoArea(this),                     //Create information area
  79.     statusLine(WND_STATUS, this, this), //Create status line
  80.     menuBar(WND_MAIN, this),            //Create menu bar
  81.     excptHandler(this)                  //Create exception handler
  82. {
  83.   statusLine.setAlignment(             //Set alignment for status line
  84.     IStaticText::centerLeft);
  85.   addExtension(&statusLine,            //add status line as a frame extension
  86.     IFrameWindow::aboveClient, 30UL);
  87.   setStatus();
  88.  
  89.   setExceptionFunction(&excptHandler);  // register exception handler
  90.   addExtension(&listbox,                // set listbox as 1/2 of client
  91.     IFrameWindow::leftOfClient, 0.5);
  92.   setClient(&listbox2);                 // set listbox2 as rest of client
  93.  
  94.   handleEventsFor(this);               // set self as command handler
  95.  
  96.   sizeTo(ISize(400,300));              // set the size of main window
  97.   update();                            // update main window
  98.   setFocus();                          // set focus to main window
  99.   show();                              // set to show main window
  100.  
  101. } /* end AListBox :: AListBox(...) */
  102.  
  103. /******************************************************************************/
  104. /* AListBox :: output - output status data to listbox2                        */
  105. /******************************************************************************/
  106. AListBox& AListBox :: output( const IString& astr )
  107. {
  108.   listbox2.addAsLast( astr );
  109.   if ( listbox2.count() > 7 )
  110.     listbox2.setTop( listbox2.count()-7 );
  111.   return *this;
  112. }
  113.  
  114. /******************************************************************************/
  115. /* AListBox :: setStatus - set the status line text                           */
  116. /******************************************************************************/
  117. AListBox& AListBox :: setStatus()
  118. {
  119.   IResourceLibrary reslib=IApplication::current().userResourceLibrary();
  120.   IString          str=reslib.loadString(STR_INFO);
  121.   str += listbox.count();                     // get number of lines in listbox
  122.   statusLine.setText(str);                    // and display
  123.   return *this;
  124. }
  125.  
  126. /******************************************************************************/
  127. /* AListBox :: command - command handler                                      */
  128. /******************************************************************************/
  129. IBase::Boolean AListBox :: command(ICommandEvent & cmdEvent)
  130. {
  131.   output(IString("command id = ") + IString(cmdEvent.commandId()));
  132.   Boolean fProcessed = true;                  // assume event processed
  133.   switch (cmdEvent.commandId()) {
  134.  
  135.     case MI_SELECT_ALL:
  136.       listbox.selectAll();
  137.       break;
  138.  
  139.     case MI_DESELECT_ALL:
  140.       listbox.deselectAll();
  141.       break;
  142.  
  143.     case MI_ADD_ITEMS:
  144.       listbox.addAsLast("some of the classes used in this example");
  145.       listbox.addAsLast("IListBox");
  146.       listbox.addAsLast("IInfoArea");
  147.       listbox.addAsLast("IMessageBox");
  148.       listbox.addAsLast("IException");
  149.       setStatus();
  150.       break;
  151.  
  152.     case MI_READ_SEL_ITEMS:
  153.       {
  154.       output( IString("number of selected items = ") +
  155.                       IString(listbox.numberOfSelections()) );
  156.       IListBox::Cursor lbCursor(listbox);     // create listbox cursor
  157.       for (lbCursor.setToFirst(); lbCursor.isValid(); lbCursor.setToNext())
  158.          {
  159.          IString  str(listbox.elementAt(lbCursor));
  160.          unsigned long ul = lbCursor.asIndex();
  161.          output(IString(ul) + IString(" - ") + str);
  162.          /* ... process string or index ... */
  163.          }
  164.       output("end of list of selected items");
  165.       break;
  166.       }
  167.  
  168.     case MI_READ_ALL_ITEMS:
  169.       {
  170.       IListBox::Cursor lbCursor(listbox, IListBox::Cursor::allItems);
  171.       for (lbCursor.setToFirst(); lbCursor.isValid(); lbCursor.setToNext())
  172.          {
  173.          IString  str(listbox.elementAt(lbCursor));
  174.          unsigned long ul = lbCursor.asIndex();
  175.          output(IString(ul) + IString(" - ") + str);
  176.          /* ... process string or index ... */
  177.          }
  178.       break;
  179.       }
  180.  
  181.     case MI_GEN_EXCPT:
  182.       {
  183.       try                               //Force an exception
  184.          {
  185.          IApplication::current().setUserResourceLibrary(IString("NOTFOUND"));
  186.          }
  187.       catch (IAccessError &exc)                  // catch access exception
  188.          {                                       // trying to load the dll
  189.          output("inside catch block");
  190.          const char *exText;
  191.          unsigned long exId = exc.errorId();
  192.          unsigned long cnt  = exc.textCount();
  193.          output( IString("error id = ") + IString(exId) );
  194.          listbox.addAsLast( IString(exc.name()) );
  195.          listbox.addAsLast( IString("error id = ") + IString(exId) );
  196.          listbox.addAsLast( IString("text count = ") + IString(cnt) );
  197.          for (unsigned long i = 0; i < cnt; ++i)
  198.             {
  199.             exText = exc.text(i);
  200.             listbox.addAsLast(IString(i) + IString(" - ") + IString(exText));
  201.             output(IString(i) + IString(exText));
  202.             }
  203.          IApplication::current().setUserResourceLibrary(0);
  204.          setStatus();
  205.          }
  206.       break;
  207.       }
  208.  
  209.     case MI_GEN_EXCPT_UNH:
  210.       {
  211.       IResourceLibrary reslib=
  212.         IApplication::current().userResourceLibrary();
  213.       IString str(reslib.loadString(9999));//string resource not found exception
  214.       break;
  215.       }
  216.  
  217.     default:
  218.       fProcessed = false;              // event not processed
  219.       break;
  220.   } /* end switch */
  221.  
  222.   return fProcessed;
  223. } /* end command(...) */
  224.  
  225.  
  226. /******************************************************************************/
  227. /* AExceptionFn :: handleException - exception handler function               */
  228. /******************************************************************************/
  229. IBase::Boolean AExceptionFn::handleException (IException& exception, IEvent& event)
  230. {
  231.   unsigned long cnt  = exception.textCount();
  232.   const char *text = (cnt > 0) ? exception.text( cnt-1 )
  233.                                : "No error text available" ;
  234.   IString str( text );
  235.   IString titleText( IApplication::current().userResourceLibrary().
  236.                      loadString(MI_MSGBOX_TITLE));
  237.   owner->output( titleText );
  238.   owner->output( exception.name() );
  239.   owner->output( IString("text count = ") + IString(cnt) );
  240.   owner->output( str );
  241.   IMessageBox msgbox( owner );
  242.   msgbox.setTitle( titleText +
  243.                    IString(" ") +
  244.                    IString(exception.name()) +
  245.                    IString(":") +
  246.                    IString(exception.errorId()) );
  247.   msgbox.show( (char *)str ,
  248.                IMessageBox::okButton         |
  249.                IMessageBox::informationIcon  |
  250.                IMessageBox::applicationModal |
  251.                IMessageBox::moveable         );
  252.   return true;                                    // stop rethrow of exception
  253. }
  254.