home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / include / ilistcv2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-22  |  9.4 KB  |  312 lines

  1. /*******************************************************************************
  2. * FILE NAME: ilistcv2.c                                                        *
  3. *                                                                              *
  4. * DESCRIPTION:                                                                 *
  5. *   This file contains the implementation of private classes/functions         *
  6. *   declared in both ilistcvw.hpp and icombovw.hpp                             *
  7. *                                                                              *
  8. * COPYRIGHT:                                                                   *
  9. *   IBM Open Class Library                                                     *
  10. *   (C) Copyright International Business Machines Corporation 1992, 1996       *
  11. *   Licensed Material - Program-Property of IBM - All Rights Reserved.         *
  12. *   US Government Users Restricted Rights - Use, duplication, or disclosure    *
  13. *   restricted by GSA ADP Schedule Contract with IBM Corp.                     *
  14. *                                                                              *
  15. *******************************************************************************/
  16.  
  17. #include <inotifev.hpp>
  18.  
  19. #ifdef IC_USE_CB
  20.   #define IC_CLASSNAME     ICollectionViewComboBox<Element,Collection>
  21.   #define IC_CLASSDATA     fCollectionViewComboBoxData
  22.   #define IC_BASECLASSNAME IBaseComboBox
  23. #else
  24.   #define IC_CLASSNAME     ICollectionViewListBox<Element,Collection>
  25.   #define IC_CLASSDATA     fCollectionViewListBoxData
  26.   #define IC_BASECLASSNAME IBaseListBox
  27. #endif
  28.  
  29.  
  30. template <class Element, class Collection>
  31.   Collection*  IC_CLASSNAME:: items( ) const
  32. {
  33.   return  (Collection *)IC_CLASSDATA->fCollectionViewData->fCollectionObserver.collection();
  34. }
  35.  
  36. template <class Element, class Collection>
  37.   IC_CLASSNAME&   IC_CLASSNAME::
  38.     setItems( Collection* collection )
  39. {
  40.   IC_CLASSDATA->fCollectionViewData->fCollectionObserver.setCollection( collection );
  41.  
  42.   return *this;
  43. }
  44.  
  45. template <class Element, class Collection>
  46.   IStringGenerator<Element>&  IC_CLASSNAME::
  47.     stringGenerator ( )
  48. {
  49.   return  IC_CLASSDATA->fCollectionViewData->fGenerator;
  50. }
  51.  
  52. template <class Element, class Collection>
  53.   IStringGenerator<Element>&
  54.     IC_CLASSNAME::
  55.       setStringGenerator ( const IStringGenerator<Element>&  newGenerator )
  56. {
  57.   IC_CLASSDATA->fCollectionViewData->fGenerator =
  58.      (IStringGenerator<Element>&)newGenerator;
  59.  
  60.   IC_CLASSDATA->saveSelectionState = true;
  61.   elementsChanged ( );
  62.   IC_CLASSDATA->saveSelectionState = false;
  63.  
  64.   return  IC_CLASSDATA->fCollectionViewData->fGenerator;
  65. }
  66.  
  67.  
  68. template <class Element, class Collection>
  69.   IC_CLASSNAME& IC_CLASSNAME :: elementsChanged ( )
  70. {
  71.   ICursor*                  pCursor;
  72.   Collection*               collection;
  73. #ifdef IC_USE_CB
  74.   ISequence<unsigned long>  selectionIndexes( IC_BASECLASSNAME::numberOfSelections());
  75. #else
  76.   long                      selection;
  77. #endif
  78.  
  79.   collection = items( );
  80. #ifdef __WINDOWS__
  81.   if ( isWindowValid( this ) )
  82.   {
  83. #endif
  84.   IWindow::enableUpdate( false );
  85.  
  86.   /* Don't forget the case where collection is NULL  */
  87.   if ( collection )
  88.   {
  89.     if ( ( collection->numberOfElements() > 0 ) &&
  90.          IC_CLASSDATA->saveSelectionState )
  91.     {
  92. #ifdef IC_USE_CB
  93.       IC_BASECLASSNAME::Cursor    cursor( *this );
  94.       forCursor( cursor )
  95.         selectionIndexes.add( cursor.asIndex());
  96. #else
  97.       selection  = IC_BASECLASSNAME::selection();
  98. #endif
  99.     }
  100.     else   // this is a removeall so deselect list box items
  101.     {
  102.       IC_BASECLASSNAME::deselectAll( );
  103.     }
  104.   }
  105.  
  106.   /*  Clear list box and repopulate w/ current string representations */
  107.   deleteAll( );
  108.   if ( collection )
  109.   {
  110.     pCursor = collection->newCursor();
  111.     forCursor( *pCursor )
  112.     {
  113.       insert( ICollectionViewConstants::listEnd,
  114.         IC_CLASSDATA->fCollectionViewData->fGenerator.stringFor( collection->elementAt( *pCursor ) ) );
  115.     }
  116.     delete pCursor;
  117.   }
  118.  
  119.   if ( !IC_BASECLASSNAME::isEmpty( ) &&
  120.        IC_CLASSDATA->saveSelectionState )
  121. #ifdef IC_USE_CB
  122.     for ( unsigned long i = selectionIndexes.numberOfElements(); i; --i )
  123.        IC_BASECLASSNAME::select( selectionIndexes.elementAtPosition(i), true );
  124. #else
  125.     if ( selection != ICollectionViewConstants::listNone )
  126.       IC_BASECLASSNAME::select( selection, true );
  127. #endif
  128.  
  129.   IWindow::enableUpdate( );
  130.   IWindow::show( );
  131. #ifdef __WINDOWS__
  132.   }
  133. #endif
  134.   return *this;
  135. }
  136.  
  137.  
  138. template <class Element, class Collection>
  139.   IC_CLASSNAME&  IC_CLASSNAME::
  140.     elementAdded ( unsigned long position, const Element&  element )
  141. {
  142.   insert( position - 1,
  143.     IC_CLASSDATA->fCollectionViewData->fGenerator.stringFor( element ) );
  144.   return *this;
  145. }
  146.  
  147. template <class Element, class Collection>
  148.   IC_CLASSNAME&  IC_CLASSNAME::
  149.     elementDeleted ( unsigned long position )
  150. {
  151.   deleteItem( position - 1 );
  152.  
  153.   return *this;
  154. }
  155.  
  156.  
  157. template <class Element, class Collection>
  158.   IC_CLASSNAME&  IC_CLASSNAME::
  159.     elementChanged ( unsigned long  position, const Element&  element )
  160. {
  161.   IC_BASECLASSNAME::setItemText(
  162.      position - 1,
  163.      IC_CLASSDATA->fCollectionViewData->fGenerator.stringFor(element) );
  164.   INotificationEvent  anEvent(
  165.      IC_CLASSNAME::itemChangedId,
  166.     *this,
  167.      true ,
  168.      IEventData((void *)&element) );
  169.   notifyObservers( anEvent );
  170.   return *this;
  171. }
  172.  
  173. template <class Element, class Collection>
  174.   IC_CLASSNAME&  IC_CLASSNAME :: collectionReplaced( )
  175. {
  176. #ifdef __WINDOWS__
  177.   if ( isWindowValid( this ) )
  178.   {
  179. #endif
  180.     INotificationEvent  anEvent( IC_CLASSNAME::itemsId,
  181.                                 *this,
  182.                                  true,
  183.                                  IEventData( (void *)items( ) ) );
  184.     notifyObservers( anEvent );
  185. #ifdef __WINDOWS__
  186.   }
  187. #endif
  188.  
  189.   return *this;
  190. }
  191.  
  192.  
  193. template <class Element, class Collection>
  194.   unsigned long IC_CLASSNAME::
  195.     selectedCollectionPosition ( )
  196. {
  197.   return IC_BASECLASSNAME::selection( ) + 1;
  198. }
  199.  
  200. template <class Element, class Collection>
  201.   IC_CLASSNAME&
  202.     IC_CLASSNAME::
  203.       select ( unsigned long collectionPosition ,
  204.                Boolean       select )
  205. {
  206.   if ( (collectionPosition > 0 ) &&
  207.        (collectionPosition <= IC_BASECLASSNAME::count()) )
  208.     IC_BASECLASSNAME::select( collectionPosition - 1, select );
  209.  
  210.   return *this;
  211. }
  212.  
  213. template <class Element, class Collection>
  214.   IC_CLASSNAME&
  215.     IC_CLASSNAME::
  216.       deselect ( unsigned long collectionPosition )
  217. {
  218.   return select( collectionPosition, false );
  219. }
  220.  
  221. template <class Element, class Collection>
  222.   Element       IC_CLASSNAME::
  223.     selectedElement ( ) const
  224. {
  225.   unsigned long                    selectedPosition;
  226.   IPartOrderedCollection<Element>* aCollection = items( );
  227.   IAutoPointer<ICursor>            pCursor( aCollection->newCursor( ), IINIT );
  228.  
  229.   selectedPosition = IC_BASECLASSNAME::selection( );
  230.   if ( selectedPosition == ICollectionViewConstants::listNone )
  231.   {
  232.     ITHROWLIBRARYERROR( IC_NO_SELECTION,
  233.                         IBaseErrorInfo::invalidRequest,
  234.                         IException::recoverable );
  235.   }
  236.  
  237.   aCollection->setToPosition( selectedPosition + 1, *pCursor );
  238.  
  239.   return aCollection->elementAt( *pCursor );
  240.  
  241. }
  242.  
  243. template <class Element, class Collection>
  244.   unsigned long IC_CLASSNAME::
  245.     insert( unsigned long index,
  246.             const char*   text )
  247. {
  248.    incrementChangeCount( );   // add may result in an invalid cursor.
  249.    unsigned long anIndex = count();
  250.    if ((long)anIndex > (long)index)
  251.      anIndex = index;
  252.  
  253.    IEventResult evt = handle().sendEvent(
  254. #ifndef IC_USE_CB
  255.                                ICollectionViewConstants::listInsert,
  256. #else
  257.                                ICollectionViewConstants::comboInsert,
  258. #endif
  259.                                IEventParameter1(anIndex),
  260.                                IEventParameter2((unsigned long) text));
  261.  
  262.    if ( ( evt.asLong() == ICollectionViewConstants::listMemoryError ) ||
  263.         ( evt.asLong() == ICollectionViewConstants::listError ) )
  264.      ITHROWGUIERROR("ICollectionViewConstants::listInsert");
  265.  
  266.    return evt.asUnsignedLong();  // Return index of inserted item.
  267. }
  268.  
  269.  
  270. template <class Element, class Collection>
  271.   unsigned long IC_CLASSNAME::
  272.     deleteItem ( unsigned long index )
  273. {
  274.    Boolean selected = isSelected( index );
  275.  
  276.    incrementChangeCount( );   // add may result in an invalid cursor.
  277.    IEventResult evt = handle().sendEvent(
  278. #ifndef IC_USE_CB
  279.                                ICollectionViewConstants::listDelete,
  280. #else
  281.                                ICollectionViewConstants::comboDelete,
  282. #endif
  283.                                IEventParameter1(index),
  284.                                IEventParameter2(0));
  285.  
  286.    if ( selected )
  287.    {
  288.      INotificationEvent  anEvent( IC_CLASSNAME::selectId,
  289.                                  *this );
  290.      notifyObservers( anEvent );
  291.    }
  292.  
  293.  
  294.    return evt.asUnsignedLong();
  295. }
  296.  
  297. template <class Element, class Collection>
  298.   IC_CLASSNAME& IC_CLASSNAME::
  299.       deleteAll ( )
  300. {
  301.    IEventResult evt = handle().sendEvent(
  302. #ifndef IC_USE_CB
  303.                                ICollectionViewConstants::listDeleteAll,
  304. #else
  305.                                ICollectionViewConstants::comboDeleteAll,
  306. #endif
  307.                                IEventParameter1(0),
  308.                                IEventParameter2(0));
  309.    incrementChangeCount( );   // add may result in an invalid cursor.
  310.    return *this;
  311. }
  312.