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