home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / ioc / listbox / alistbox.cpp next >
Encoding:
C/C++ Source or Header  |  1996-02-22  |  11.0 KB  |  239 lines

  1. /******************************************************************************
  2. * .FILE:         alistbox.cpp                                                 *
  3. *                                                                             *
  4. * .DESCRIPTION:  List Box Sample Program:  Class Implementation               *
  5. *                                                                             *
  6. * .CLASSES:      AListBox                                                     *
  7. *                ACommandHandler                                              *
  8. *                                                                             *
  9. * .COPYRIGHT:                                                                 *
  10. *    Licensed Material - Program-Property of IBM                              *
  11. *    (C) Copyright IBM Corp. 1992, 1996 - All Rights Reserved                 *
  12. *                                                                             *
  13. * .DISCLAIMER:                                                                *
  14. *   The following [enclosed] code is sample code created by IBM               *
  15. *   Corporation.  This sample code is not part of any standard IBM product    *
  16. *   and is provided to you solely for the purpose of assisting you in the     *
  17. *   development of your applications.  The code is provided 'AS IS',          *
  18. *   without warranty of any kind.  IBM shall not be liable for any damages    *
  19. *   arising out of your use of the sample code, even if they have been        *
  20. *   advised of the possibility of such damages.                               *
  21. *                                                                             *
  22. * .NOTE: WE RECOMMEND USING A FIXED SPACE FONT TO LOOK AT THE SOURCE          *
  23. *                                                                             *
  24. ******************************************************************************/
  25.  
  26. #include <ibase.hpp>
  27. #include <iapp.hpp>
  28. #include <istattxt.hpp>
  29. #include <ilistbox.hpp>
  30. #include <irect.hpp>
  31. #include <imenubar.hpp>
  32. #include <istring.hpp>
  33. #include <iinfoa.hpp>
  34. #include <itrace.hpp>
  35. #include <imsgbox.hpp>
  36. #include <iexcbase.hpp>
  37. #include <ireslib.hpp>
  38. #include <icoordsy.hpp>
  39. #include "alistbox.h"
  40. #include "alistbox.hpp"
  41.  
  42. /*****************************************************************************
  43. * main  - Application entry point                                            *
  44. *****************************************************************************/
  45. int main()
  46. {
  47.   ICoordinateSystem::setApplicationOrientation(
  48.           ICoordinateSystem::originLowerLeft );
  49.   AListBox mainWindow (WND_MAIN);
  50.   IApplication::current().run();
  51.   return 0;
  52. } /* end main */
  53.  
  54.  
  55. /******************************************************************************
  56. * Class AListBox :: AListBox - constructor for main window                    *
  57. *                                                                             *
  58. * Define itself as an IFrame Window                                           *
  59. * Create a listbox.                                                           *
  60. * Create the trace listbox.                                                   *
  61. * Create the info area, the status line, and the menu bar.                    *
  62. ******************************************************************************/
  63. AListBox :: AListBox(unsigned long windowId)
  64.   : IFrameWindow( IFrameWindow::defaultStyle()
  65.             | IFrameWindow::accelerator
  66.             | IFrameWindow::minimizedIcon,
  67.             windowId),
  68.     listbox(WND_HELLO, this, this, IRectangle(),  //Create listbox
  69.             (IListBox::defaultStyle()
  70.             | IListBox::multipleSelect)
  71.             & ~IListBox::horizontalScroll),
  72.     listbox2(WND_HELLO, this, this, IRectangle(), //Create trace listbox
  73.             IListBox::defaultStyle()
  74.             & ~IListBox::horizontalScroll),
  75.     infoArea(this),
  76.     statusLine(WND_STATUS, this, this),
  77.     menuBar(WND_MAIN, this),
  78.     commandhandler(this,&listbox)
  79. {
  80. /*----------------------------------------------------------------------------|
  81. | Set the alignment of the status line and add it to the frame extension.     |
  82. -----------------------------------------------------------------------------*/
  83.   statusLine.setAlignment(
  84.     IStaticText::centerLeft);
  85.   addExtension(&statusLine,
  86.     IFrameWindow::aboveClient, 30UL);
  87.   setStatus();
  88.  
  89. /*----------------------------------------------------------------------------|
  90. | Add the listbox as an extension 1/2 the size of the client and set the      |
  91. | trace listbox as the rest of the client space.                              |
  92. -----------------------------------------------------------------------------*/
  93.   addExtension(&listbox,
  94.     IFrameWindow::leftOfClient, 0.5);
  95.   setClient(&listbox2);
  96.  
  97.   commandhandler.handleEventsFor(this);
  98.  
  99. /*----------------------------------------------------------------------------|
  100. | Set the size of the main window, update the main window, set focus and then |
  101. | show the main window.                                                       |
  102. -----------------------------------------------------------------------------*/
  103.   sizeTo(ISize(400,300));
  104.   update();
  105.   setFocus();
  106.   show();
  107.  
  108. } /* end AListBox :: AListBox(...) */
  109.  
  110. /******************************************************************************
  111. * class AListBox :: output - output status data to listbox2                   *
  112. ******************************************************************************/
  113. AListBox& AListBox :: output( const IString& astr )
  114. {
  115. /*----------------------------------------------------------------------------|
  116. | Add the string to listbox2                                                  |
  117. | If the listbox has more than 7 lines, scroll                                |
  118. -----------------------------------------------------------------------------*/
  119.   listbox2.addAsLast( astr );
  120.   if ( listbox2.count() > 7 )
  121.     listbox2.setTop( listbox2.count()-7 );
  122.   return *this;
  123. }
  124.  
  125. /******************************************************************************
  126. * class AListBox :: setStatus - set the status line text                      *
  127. ******************************************************************************/
  128. AListBox& AListBox :: setStatus()
  129. {
  130. /*----------------------------------------------------------------------------|
  131. | Get the resource library                                                    |
  132. | Load a string from the resource library                                     |
  133. | Concatenate the count to the string                                         |
  134. | Set the infoarea text to the string                                         |
  135. -----------------------------------------------------------------------------*/
  136.   IResourceLibrary reslib;
  137.   IString str=reslib.loadString(STR_INFO);
  138.   str += listbox.count();
  139.   statusLine.setText(str);
  140.   return *this;
  141. }
  142.  
  143. /******************************************************************************
  144. * class ACommandHandler::ACommandHandler - Constructor for the command handler*
  145. *                                                                             *
  146. * Stores pointers to the main window and the listbox.                         *
  147. ******************************************************************************/
  148. ACommandHandler::ACommandHandler(AListBox *alb,IListBox *lb1)
  149.     : alistbox(alb)
  150.     , listbox1(lb1)
  151. {
  152. }
  153.  
  154.  
  155. /******************************************************************************
  156. * AListBox :: command - command handler                                       *
  157. ******************************************************************************/
  158. IBase::Boolean ACommandHandler :: command(ICommandEvent & cmdEvent)
  159. {
  160.   IResourceLibrary rlib;
  161.   Boolean fProcessed = true;
  162.  
  163.   switch (cmdEvent.commandId()) {
  164.  
  165.     case MI_SELECT_ALL:
  166. /*----------------------------------------------------------------------------|
  167. | Select all the items in listbox1                                            |
  168. -----------------------------------------------------------------------------*/
  169.       listbox1->selectAll();
  170.       break;
  171.  
  172.     case MI_DESELECT_ALL:
  173. /*----------------------------------------------------------------------------|
  174. | Deselect all the items in listbox1                                          |
  175. -----------------------------------------------------------------------------*/
  176.       listbox1->deselectAll();
  177.       break;
  178.  
  179.     case MI_ADD_ITEMS:
  180. /*----------------------------------------------------------------------------|
  181. | Add string items from the resource library to listbox1                      |
  182. | Update the status                                                           |
  183. -----------------------------------------------------------------------------*/
  184.       listbox1->addAsLast(STR_LIST_ITEM1);
  185.       listbox1->addAsLast(STR_LIST_ITEM2);
  186.       listbox1->addAsLast(STR_LIST_ITEM3);
  187.       listbox1->addAsLast(STR_LIST_ITEM4);
  188.       listbox1->addAsLast(STR_LIST_ITEM5);
  189.       alistbox->setStatus();
  190.       break;
  191.  
  192.     case MI_READ_SEL_ITEMS:
  193.       {
  194. /*----------------------------------------------------------------------------|
  195. | Create a listbox cursor for selected items                                  |
  196. | Output each line to alistbox                                                |
  197. | Output the number of selected items                                         |
  198. -----------------------------------------------------------------------------*/
  199.         IListBox::Cursor lbCursor( *listbox1 );
  200.         for ( lbCursor.setToFirst(); lbCursor.isValid(); lbCursor.setToNext() )
  201.         {
  202.           alistbox->output( IString(
  203.                             IString( lbCursor.asIndex() )
  204.                             + " - "
  205.                             + listbox1->elementAt( lbCursor )));
  206.         }
  207.         alistbox->output( IString( rlib.loadString( STR_SELECTEDITEMS )
  208.                           + IString( listbox1->numberOfSelections() )));
  209.         break;
  210.       }
  211.  
  212.     case MI_READ_ALL_ITEMS:
  213.       {
  214. /*----------------------------------------------------------------------------|
  215. | Create a listbox cursor for selected items                                  |
  216. | Output each line to alistbox                                                |
  217. -----------------------------------------------------------------------------*/
  218.         IListBox::Cursor lbCursor( *listbox1, IListBox::Cursor::allItems );
  219.         for ( lbCursor.setToFirst(); lbCursor.isValid(); lbCursor.setToNext() )
  220.         {
  221.           alistbox->output( IString(
  222.                             IString( lbCursor.asIndex() )
  223.                             + " - "
  224.                             + listbox1->elementAt( lbCursor )));
  225.         }
  226.         alistbox->output( IString( rlib.loadString( STR_NUMBERITEMS )
  227.                           + IString( listbox1->count() )));
  228.         break;
  229.       }
  230.  
  231.  
  232.     default:
  233.       fProcessed = false;              // event not processed
  234.       break;
  235.   } /* end switch */
  236.  
  237.   return fProcessed;
  238. } /* end command(...) */
  239.