home *** CD-ROM | disk | FTP | other *** search
/ Mastering Visual Basic 6 / mastvb6.iso / numega / sc501.exe / data1.cab / Examples / BCTREE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-25  |  7.4 KB  |  214 lines

  1. /*
  2.  * BCTree.cpp
  3.  * $Header: /bcsample/BUGBNCHX/MAINERR/BCTREE.CPP 1     5/28/96 1:11p Dave $
  4.  *
  5.  * Description:
  6.  *  Standard helper functions that mimic the support given by the MFC
  7.  *   CTreeCtrl class are implemented here.  Also included are 
  8.  *   additional functions to help with Expanding/Collapsing all nodes
  9.  *   under a specific tree item and getting the LPARAM info from the
  10.  *   selected item.  
  11.  *
  12.  * Notes:
  13.  *  <implementation notes go here>
  14.  *
  15.  ***********************************************************************
  16.  *
  17.  * Nu-Mega Technologies, Inc.
  18.  * P.O. Box 7780
  19.  * Nashua, NH 03060
  20.  *
  21.  * (c) Copyright 1994, 1995 Nu-Mega Technologies, Inc.
  22.  * ALL RIGHTS RESERVED.
  23.  *
  24.  ***********************************************************************
  25.  *
  26.  **********************************************************************/
  27. #include "stdafx.h"
  28. #include "BCTree.h"
  29.  
  30. ////////////////////////////////////////////////////////////////////////
  31. // Standard tree control helpers
  32. //
  33. // All of the following functions are of the flavor of sending a message
  34. //  to the specified HWND.
  35. HTREEITEM TreeCtrl_InsertItem ( HWND hWnd , LPTV_INSERTSTRUCT lpInsertStruct )
  36.    ASSERT ( IsWindow ( hWnd ) ) ;  
  37.    return ( HTREEITEM ) SendMessage ( hWnd                     , 
  38.                                       TVM_INSERTITEM           ,  
  39.                                       0                        , 
  40.                                       ( LPARAM ) lpInsertStruct ) ; 
  41. }
  42.  
  43. BOOL TreeCtrl_DeleteAllItems ( HWND hWnd ) 
  44.    ASSERT ( IsWindow ( hWnd ) ) ;  
  45.    return ( BOOL ) SendMessage( hWnd               ,
  46.                                 TVM_DELETEITEM     , 
  47.                                 0                  , 
  48.                                 ( LPARAM ) TVI_ROOT ) ; 
  49. }
  50.  
  51. BOOL TreeCtrl_GetItem ( HWND hWnd , TV_ITEM* pItem )
  52.    ASSERT ( IsWindow ( hWnd ) ) ; 
  53.    return ( BOOL ) SendMessage ( hWnd            , 
  54.                                  TVM_GETITEM     , 
  55.                                  0               , 
  56.                                  ( LPARAM ) pItem ) ; 
  57. }
  58.  
  59. HTREEITEM TreeCtrl_GetParentItem ( HWND hWnd , HTREEITEM hItem )
  60.    ASSERT ( IsWindow ( hWnd ) ) ; 
  61.    return ( HTREEITEM ) SendMessage ( hWnd            , 
  62.                                       TVM_GETNEXTITEM , 
  63.                                       TVGN_PARENT     , 
  64.                                       ( LPARAM ) hItem ); 
  65. }
  66.  
  67. HTREEITEM TreeCtrl_GetChildItem ( HWND hWnd , HTREEITEM hItem )
  68. {
  69.    ASSERT ( IsWindow ( hWnd ) ) ; 
  70.    return ( HTREEITEM ) SendMessage ( hWnd            , 
  71.                                       TVM_GETNEXTITEM , 
  72.                                       TVGN_CHILD      , 
  73.                                       ( LPARAM ) hItem ); 
  74. }
  75.  
  76. HTREEITEM TreeCtrl_GetSelectedItem ( HWND hWnd )
  77.    ASSERT ( IsWindow ( hWnd ) ) ; 
  78.    return ( HTREEITEM ) SendMessage ( hWnd            , 
  79.                                       TVM_GETNEXTITEM , 
  80.                                       TVGN_CARET      , 
  81.                                       0                ); 
  82. }
  83.  
  84. HTREEITEM TreeCtrl_GetNextItem ( HWND hWnd       , 
  85.                                  HTREEITEM hItem , 
  86.                                  UINT nCode       )
  87. {
  88.    ASSERT ( IsWindow ( hWnd ) ) ; 
  89.    return ( HTREEITEM ) SendMessage ( hWnd, 
  90.                                       TVM_GETNEXTITEM, 
  91.                                       nCode, 
  92.                                       ( LPARAM ) hItem ) ; 
  93. }
  94.  
  95. HTREEITEM TreeCtrl_Select ( HWND hWnd       , 
  96.                             HTREEITEM hItem , 
  97.                             UINT nCode       )
  98.    ASSERT ( IsWindow ( hWnd ) ) ; 
  99.    return ( HTREEITEM ) SendMessage ( hWnd            , 
  100.                                       TVM_SELECTITEM  , 
  101.                                       nCode           , 
  102.                                       ( LPARAM ) hItem ); 
  103. }
  104.  
  105. BOOL TreeCtrl_EnsureVisible ( HWND hWnd , HTREEITEM hItem )
  106. {
  107.    ASSERT ( IsWindow ( hWnd ) ) ; 
  108.    return ( BOOL ) SendMessage ( hWnd              , 
  109.                                  TVM_ENSUREVISIBLE , 
  110.                                  0                 , 
  111.                                  ( LPARAM ) hItem   ) ; 
  112. }
  113.  
  114. BOOL TreeCtrl_Expand ( HWND hWnd       , 
  115.                        HTREEITEM hItem , 
  116.                        UINT nCode       )
  117. {
  118.    ASSERT ( IsWindow ( hWnd ) ) ; 
  119.    return ( BOOL ) SendMessage ( hWnd            , 
  120.                                  TVM_EXPAND      , 
  121.                                  nCode           , 
  122.                                  ( LPARAM ) hItem ) ; 
  123. }
  124.  
  125. ////////////////////////////////////////////////////////////////////////
  126. // Tree Control Extensions
  127. //
  128. // Additional work is needed to get the tree control functionality 
  129. //  desired.
  130.  
  131. // Expand/Collapse all items in the tree.
  132. void TreeCtrl_ExpandAll ( HWND hWnd , UINT nCode )
  133. {
  134.    // In the NT 3.51 RC, the Tree control will not notify the
  135.    //  parent window that the selected item has changed when we collapse
  136.    //  the tree.  To work around this we will force all collapse all's
  137.    //  to select the first item.
  138.    HTREEITEM hFirstItem = TreeCtrl_GetNextItem ( hWnd , NULL , TVGN_CHILD ) ;
  139.    HTREEITEM hItem = hFirstItem ;
  140.    while ( NULL != hItem )
  141.    {
  142.       TreeCtrl_Expand ( hWnd , hItem , nCode ) ;
  143.       TreeCtrl_ExpandCollapseAllBelow ( hWnd , hItem , nCode ) ;
  144.       hItem = TreeCtrl_GetNextItem ( hWnd , hItem , TVGN_NEXT ) ;
  145.    }
  146.    if ( TVE_COLLAPSE == nCode )
  147.       TreeCtrl_Select ( hWnd , hFirstItem , TVGN_CARET ) ;
  148. }
  149.  
  150.  
  151. // Extends the Expand function to do the operation on all items below
  152. //  the specified node.
  153. void TreeCtrl_ExpandCollapseAllBelow ( HWND hWnd       ,
  154.                                        HTREEITEM hItem ,
  155.                                        UINT      nCode  )
  156. {
  157.    // The degenerative case.
  158.    if ( NULL == hItem )
  159.       return ;
  160.    else
  161.    {
  162.       // Get the first child of the node passed in.
  163.       HTREEITEM hChild = TreeCtrl_GetChildItem ( hWnd , hItem ) ;
  164.       while ( NULL != hChild )
  165.       {
  166.          // Expand the child.
  167.          TreeCtrl_Expand ( hWnd , hChild , nCode ) ;
  168.          // Go all the way diagonally, expanding each
  169.          //  node as we come to it.
  170.          HTREEITEM hChildTwo = TreeCtrl_GetChildItem ( hWnd , hChild ) ;
  171.          while ( NULL != hChildTwo )
  172.          {
  173.             // Expand the child.
  174.             TreeCtrl_Expand ( hWnd , hChildTwo , nCode ) ;
  175.             hChildTwo = TreeCtrl_GetChildItem ( hWnd , hChildTwo ) ;
  176.             // Do ALL the expansion on the child.  And I thought
  177.             //  I would never see recursion after college!
  178.             TreeCtrl_ExpandCollapseAllBelow ( hWnd , hChildTwo , nCode ) ;
  179.          }
  180.          // Get the next child sibling.
  181.          hChild = TreeCtrl_GetNextItem ( hWnd , hChild , TVGN_NEXT ) ;
  182.       }
  183.    }
  184. }
  185.  
  186. // Gets the data out of the selected item and returns it
  187. //  as LPVOID.
  188. LPVOID TreeCtrl_GetSelectedLParam ( HWND hWnd ) 
  189. {
  190.    ASSERT ( IsWindow ( hWnd ) ) ;
  191.  
  192.    // Get the currently selected item.
  193.    HTREEITEM hItem = TreeCtrl_GetSelectedItem ( hWnd ) ;
  194.    ASSERT ( NULL != hItem ) ;
  195.  
  196.    if ( NULL == hItem )
  197.       return ( NULL ) ;
  198.  
  199.    // Get the information out of the selected item.
  200.    TV_ITEM stTVI ;
  201.    memset ( &stTVI , NULL , sizeof ( TV_ITEM ) ) ;
  202.    stTVI.hItem = hItem ;
  203.    VERIFY ( TreeCtrl_GetItem ( hWnd , &stTVI ) ) ;
  204.    return ( (void*) stTVI.lParam ) ;
  205.  
  206. }
  207.  
  208.