home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cset21v1.zip / IBMCPP / SAMPLES / ICLUI / LISTBOX / ALISTBOX.CPP next >
Text File  |  1993-05-12  |  11KB  |  231 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. #define  IC_TRACE_DEVELOP
  34.                                         // Include IBM UI class headers:
  35. #include <iapp.hpp>                     // IApplication
  36. #include <istattxt.hpp>                 // IStaticText
  37. #include <ilistbox.hpp>                 // IListBox
  38. #include <irect.hpp>                    // IRectangle
  39. #include <imenubar.hpp>                 // IMenuBar
  40. #include <istring.hpp>                  // IString
  41. #include <iinfoa.hpp>                   // IInfoArea
  42. #include <itrace.hpp>                   // ITrace
  43. #include <imsgbox.hpp>                  // IMessageBox
  44. #include <iexcbase.hpp>                 // IException
  45.  
  46. #include "alistbox.h"                   // include our Symbolic definitions
  47. #include "alistbox.hpp"                 // include AListBox Class headers
  48.  
  49. /******************************************************************************/
  50. /* main  - Application entry point                                            */
  51. /******************************************************************************/
  52. void main()
  53. {
  54.   AListBox mainWindow (WND_MAIN);       // create main window
  55.   IApplication::current().run();        // start message loop processing
  56. } /* end main */
  57.  
  58. /******************************************************************************/
  59. /* AListBox :: AListBox - constructor for main window                         */
  60. /******************************************************************************/
  61. AListBox :: AListBox(unsigned long windowId)
  62.   : IFrameWindow( IFrameWindow::defaultStyle()  //Call the IFrame Constructor
  63.                 | IFrameWindow::accelerator,    //with defaultSytles+accelerator
  64.                   windowId),                    //and window Id
  65.  
  66.     listbox(WND_HELLO, this, this, IRectangle(),//Create listbox
  67.              (IListBox::defaultStyle()
  68.                | IListBox::multipleSelect)
  69.                & ~IListBox::horizontalScroll),
  70.     infoArea(this),                     //Create information area
  71.     statusLine(WND_STATUS, this, this), //Create status line
  72.     menuBar(WND_MAIN, this),            //Create menu bar
  73.     excptHandler(this)                  //Create exception handler
  74. {
  75.   IFUNCTRACE_DEVELOP();
  76.   setExceptionFunction(&excptHandler);  // register exception handler
  77.   setClient(&listbox);                  // set listbox as client
  78.  
  79.                                        // add status line as frame extension
  80.   statusLine.setAlignment(             //Set alignment for status line
  81.     IStaticText::centerLeft);
  82.   setStatus();
  83.   addExtension(&statusLine,            //add status line as a frame extension
  84.     IFrameWindow::aboveClient, 30UL);
  85.  
  86.   handleEventsFor(this);               // set self as command handler
  87.  
  88.   sizeTo(ISize(400,300));              // set the size of main window
  89.   update();                            // update main window
  90.   setFocus();                          // set focus to main window
  91.   show();                              // set to show main window
  92.  
  93. } /* end AListBox :: AListBox(...) */
  94.  
  95. /******************************************************************************/
  96. /* AListBox :: setStatus - set the status line text                           */
  97. /******************************************************************************/
  98. void AListBox :: setStatus()
  99. {
  100.   IFUNCTRACE_DEVELOP();
  101.   IResourceLibrary reslib=IApplication::current().userResourceLibrary();
  102.   IString          str=reslib.loadString(STR_INFO);
  103.   str += listbox.count();                     // get number of lines in listbox
  104.   statusLine.setText((char *) str);           // and display
  105. }
  106.  
  107. /******************************************************************************/
  108. /* AListBox :: command - command handler                                      */
  109. /******************************************************************************/
  110. Boolean AListBox :: command(ICommandEvent & cmdEvent)
  111. {
  112.   IFUNCTRACE_DEVELOP();
  113.   ITRACE_DEVELOP(IString("command id = ") + IString(cmdEvent.commandId()));
  114.   Boolean fProcessed = true;                  // assume event processed
  115.   switch (cmdEvent.commandId()) {
  116.  
  117.     case MI_SELECT_ALL:
  118.       listbox.selectAll();
  119.       break;
  120.  
  121.     case MI_DESELECT_ALL:
  122.       listbox.deselectAll();
  123.       break;
  124.  
  125.     case MI_ADD_ITEMS:
  126.       listbox.addAsLast("some of the classes used in this example");
  127.       listbox.addAsLast("IListBox");
  128.       listbox.addAsLast("IInfoArea");
  129.       listbox.addAsLast("IMessageBox");
  130.       listbox.addAsLast("IException");
  131.       setStatus();
  132.       break;
  133.  
  134.     case MI_READ_SEL_ITEMS:
  135.       {
  136.       ITRACE_DEVELOP( IString("number of selected items = ") +
  137.                       IString(listbox.numberOfSelections()) );
  138.       IListBox::Cursor lbCursor(listbox);     // create listbox cursor
  139.       for (lbCursor.setToFirst(); lbCursor.isValid(); lbCursor.setToNext())
  140.          {
  141.          IString  str(listbox.elementAt(lbCursor));
  142.          unsigned long ul = lbCursor.asIndex();
  143.          ITRACE_DEVELOP(IString(ul) + IString(" - ") + str);
  144.          /* ... process string or index ... */
  145.          }
  146.       ITRACE_DEVELOP("end of list of selected items");
  147.       break;
  148.       }
  149.  
  150.     case MI_READ_ALL_ITEMS:
  151.       {
  152.       IListBox::Cursor lbCursor(listbox, IListBox::Cursor::allItems);
  153.       for (lbCursor.setToFirst(); lbCursor.isValid(); lbCursor.setToNext())
  154.          {
  155.          IString  str(listbox.elementAt(lbCursor));
  156.          unsigned long ul = lbCursor.asIndex();
  157.          ITRACE_DEVELOP(IString(ul) + IString(" - ") + str);
  158.          /* ... process string or index ... */
  159.          }
  160.       break;
  161.       }
  162.  
  163.     case MI_GEN_EXCPT:
  164.       {
  165.       try                               //Force an exception
  166.          {
  167.          IApplication::current().setUserResourceLibrary(IString("NOTFOUND"));
  168.          }
  169.       catch (IAccessError &exc)                  // catch access exception
  170.          {                                       // trying to load the dll
  171.          ITrace trc("inside catch block");
  172.          const char *exText;
  173.          unsigned long exId = exc.errorId();
  174.          unsigned long cnt  = exc.textCount();
  175.          ITRACE_DEVELOP( IString("error id = ") + IString(exId) );
  176.          listbox.addAsLast( IString(exc.name()) );
  177.          listbox.addAsLast( IString("error id = ") + IString(exId) );
  178.          listbox.addAsLast( IString("text count = ") + IString(cnt) );
  179.          for (unsigned long i = 0; i < cnt; ++i)
  180.             {
  181.             exText = exc.text(i);
  182.             listbox.addAsLast(IString(i) + IString(" - ") + IString(exText));
  183.             ITRACE_DEVELOP(IString(i) + IString(exText));
  184.             }
  185.          IApplication::current().setUserResourceLibrary(0);
  186.          setStatus();
  187.          }
  188.       break;
  189.       }
  190.  
  191.     case MI_GEN_EXCPT_UNH:
  192.       {
  193.       IResourceLibrary reslib=
  194.         IApplication::current().userResourceLibrary();
  195.       IString str(reslib.loadString(9999));//string resource not found exception
  196.       break;
  197.       }
  198.  
  199.     default:
  200.       fProcessed = false;              // event not processed
  201.       break;
  202.   } /* end switch */
  203.  
  204.   return fProcessed;
  205. } /* end HelloWindow :: command(...) */
  206.  
  207.  
  208. /******************************************************************************/
  209. /* AExceptionFn :: handleException - exception handler function               */
  210. /******************************************************************************/
  211. Boolean AExceptionFn::handleException (IException& exception, IEvent& event)
  212. {
  213.   IFUNCTRACE_DEVELOP();
  214.   unsigned long cnt  = exception.textCount();
  215.   const char *text = (cnt > 0) ? exception.text( cnt-1 )
  216.                                : "No error text available" ;
  217.   IString str( text );
  218.   ITRACE_DEVELOP( exception.name() );
  219.   ITRACE_DEVELOP( IString("text count = ") + IString(cnt) );
  220.   ITRACE_DEVELOP( str );
  221.   IMessageBox msgbox( owner );
  222.   msgbox.setTitle( IString(exception.name()) + IString(":") +
  223.                    IString(exception.errorId()) );
  224.   msgbox.show( (char *)str ,
  225.                IMessageBox::okButton         |
  226.                IMessageBox::informationIcon  |
  227.                IMessageBox::applicationModal |
  228.                IMessageBox::moveable         );
  229.   return true;                                    // stop rethrow of exception
  230. }
  231.