treeitems.h
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 19k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. //-------------------------------------------------------------------------//
  2. //
  3. //  TreeItems.h
  4. //
  5. //-------------------------------------------------------------------------//
  6. #ifndef __TREEITEMS_H__
  7. #define __TREEITEMS_H__
  8. #include "resource.h"
  9. #include "Dictionary.h"
  10. #include "PriorityLst.h"
  11. //-------------------------------------------------------------------------//
  12. //  Forwards
  13. class  CPropertyTreeCtl ;
  14. class  CPropertyTreeItem ;
  15. class  CPropertyFolder ;
  16. class  CProperty ;
  17. class  CMetrics ;
  18. //-------------------------------------------------------------------------//
  19. const int CCH_PROPERTY_TEXT = MAX_PATH ;
  20. //  Tier constants
  21. //  BUGBUG: These should be calculated on tree view insertion in order
  22. //  to support multilevel hierarchy.
  23. enum PROPTREE_TIERS {
  24.     NIL_TIER,
  25.     FOLDER_TIER,      
  26.     PROPERTY_TIER,   
  27. } ;
  28. //-------------------------------------------------------------------------//
  29. class CPropertyTreeItem
  30. //-------------------------------------------------------------------------//
  31. {
  32. public:
  33.     //  Construction, destruction, initialization
  34.     CPropertyTreeItem( CPropertyTreeCtl* pCtl ) ;
  35.     virtual ~CPropertyTreeItem() {}
  36.                                  
  37. public:
  38.     //  Utility Methods
  39.     static  CPropertyTreeItem*  GetTreeItem( const TV_ITEM& tvi ) ;
  40.     static  HINSTANCE           GetModuleInstance() ;
  41.     static  HINSTANCE           GetResourceInstance() ;
  42.     CPropertyTreeItem*          GetNext( PROPTREE_ITEM_TYPE type, BOOL bExpand ) const ;
  43.     CPropertyTreeItem*          GetPrev( PROPTREE_ITEM_TYPE type, BOOL bExpand ) const ;
  44.     operator HTREEITEM() const ;
  45.     //  General function and access methods
  46.     virtual USHORT     Type() const =0 ;
  47.             LPCTSTR    GetName() const ;
  48.             LPCTSTR    GetDisplayName() const ;
  49.             LPCTSTR    GetDescription() const ;
  50.     virtual ULONG      GetAccess() const =0 ;
  51.  
  52.     virtual int        ItemWidth( BOOL bTextOnly ) const;
  53.  
  54.     virtual void       SetName( LPCTSTR pszName ) ;
  55.     virtual void       SetDisplayName( LPCTSTR pszDisplayName ) ;
  56.     virtual void       SetDescription( LPCTSTR pszDescr ) ;
  57.     virtual BOOL       IsCompositeMismatch() const { return FALSE ; }
  58.     virtual BOOL       IsMultiline() ;
  59.     static  LPTSTR     MultilineToSingleline( LPCTSTR pszValue ) ;
  60.     virtual LPCTSTR    ValueText() ;
  61.     virtual BOOL       OnValueChanging( CPropVariant& varNew ) ;
  62.     virtual void       OnValueChanged( const CPropVariant& varNew ) ;
  63.     virtual BOOL       OnValueRestore()     { return FALSE ; }
  64.     //  Property source methods
  65.     virtual HRESULT    AddSource( const VARIANT* pvarSrc, ULONG dwAccess, const PROPVARIANT* pVal ) ;
  66.     virtual HRESULT    RemoveSource( const VARIANT* pvarSrc ) ;
  67.     virtual LONG       SourceCount() const ;
  68.     virtual BOOL       Reconcile( HWND hwndTree, LONG cMasterSourceCount ) ;
  69.     //  Tree View methods
  70.     virtual HTREEITEM  Insert( HWND hwndTree, HTREEITEM hParent, HTREEITEM hInsertAfter = TVI_LAST )=0 ;
  71.     virtual void       Remove() ;
  72.     virtual int        TierNumber() const = 0 ;
  73.     virtual int        InsertChildren() ;
  74.     virtual LRESULT    OnTreeItemCallback( TV_DISPINFO* pDI, BOOL& bHandled ) ;
  75.     virtual LRESULT    Draw( LPNMTVCUSTOMDRAW, CMetrics* ) ;
  76.     virtual void       HitTest( LPTVHITTESTINFO pHTI ) ;
  77.     virtual BOOL       GetLabelRect( OUT LPRECT prc, BOOL fTextBox = FALSE ) ;
  78.     virtual BOOL       GetValueRect( OUT LPRECT prc, BOOL fTextBox = FALSE ) ;
  79.     virtual BOOL       OnKillFocus( UINT uAction = TVC_UNKNOWN, CPropertyTreeItem* pItemNew = NULL ) ;
  80.     //  TreeView order index among siblings
  81.     virtual void       SetOrder( int i )=0 ;
  82.     virtual int        Order() const = 0 ;
  83.     //  Array of sort direction indicators for child items.
  84.     virtual HTREEITEM  GetSortTarget( int iCol )    { return m_hTreeItem ; }
  85.     int*               ChildSortDir() ;
  86.     //  Pend sort methods.
  87.     virtual void       PendChildSort()   {} // indicates that the item's children should be resorted.
  88.     virtual void       UnpendChildSort() {} // indicates that the item's children do not require a resort.
  89.     virtual BOOL       ChildSortPending() const ; // Retrieves the child sort pending status of the item.
  90.     //  In-Place Edit Control methods
  91.     virtual HWND       EditControl()  { return NULL ; }
  92.     virtual HWND       ShowEditControl( UINT nShowCmd, LPCRECT prcEdit, CMetrics* ) ;
  93.     virtual void       RepositionEditControl( LPCRECT prcEdit, CMetrics* ) ;
  94.     virtual LRESULT    OnEditControlCommand( UINT nID, UINT nCode, HWND hwndEdit, CMetrics*, BOOL& bHandled ) ;
  95.     virtual LRESULT    OnEditControlNotify( NMHDR* pHdr, CMetrics*, BOOL& bHandled ) ;
  96.     //  Miscellaneous methods:
  97.     virtual void       DisplayQtipText( HWND hwndQtip ) ;
  98.     //  Non-virtual TreeView implementation methods
  99. protected:
  100.     HTREEITEM          Insert( HWND hwndTree, TV_INSERTSTRUCT* pTVI ) ;
  101.     BOOL               UpdateChildCount( int cChildren ) ;
  102.     virtual BOOL       DrawValueText() ;
  103.     CPropertyTreeCtl*  Control() ;
  104.     const HWND&        TreeHwnd() const ;
  105.     //  Data
  106. protected:    
  107.     //  1st dimension indices and upper bound for m_szText array.
  108.     enum     {
  109.         iName,
  110.         iDisplayName,
  111.         iDescr,
  112.         cPropertyText   // array upper bound
  113.     } ;
  114.     TCHAR  m_szText[3][CCH_PROPERTY_TEXT] ;
  115.     int    m_xLabel, m_cxLabel,     // start, width of label text box
  116.            m_xValue, m_cxValue ;    // start, width of value text box.
  117.     int    m_iChildSortDir[2] ;
  118.     
  119. private:
  120.     CPropertyTreeCtl* m_pCtl ;
  121.     HTREEITEM         m_hTreeItem ;
  122.     static TCHAR      m_szCompositeMismatch[CCH_PROPERTY_TEXT] ;
  123. } ;
  124. //-------------------------------------------------------------------------//
  125. class CPropertyFolder  : public CPropertyTreeItem
  126. //-------------------------------------------------------------------------//
  127. {
  128. public:
  129.     CPropertyFolder( CPropertyTreeCtl* pCtl, const PROPFOLDERITEM& folder ) ;
  130.     virtual ~CPropertyFolder() ;
  131.     //  Overrides of CPropertyTreeItem:
  132.     virtual BOOL       Initialize() ;
  133.     virtual USHORT     Type() const     { return PIT_FOLDER ; }
  134.     virtual ULONG      GetAccess() const { return m_folderitem.dwAccess ; }
  135.     virtual HRESULT    AddSource( const VARIANT* pvarSrc, ULONG dwAccess, const PROPVARIANT* pvarVal = NULL ) ;
  136.     virtual HRESULT    RemoveSource( const VARIANT* pvarSrc ) ;
  137.     virtual LONG       SourceCount() const ;
  138.     virtual BOOL       Reconcile( HWND hwndTree, LONG cMasterSourceCount ) ;
  139.     virtual HTREEITEM  Insert( HWND hwndTree, HTREEITEM hParent = TVI_ROOT, HTREEITEM hInsertAfter = TVI_LAST ) ;
  140.     virtual int        InsertChildren() ;
  141.     virtual int        TierNumber() const  { return FOLDER_TIER ; }
  142.     virtual LRESULT    OnTreeItemCallback( TV_DISPINFO* pDI, BOOL& bHandled ) ;
  143.     virtual void       SetOrder( int i )    { m_folderitem.iOrder = i ; }
  144.     virtual HTREEITEM  GetSortTarget( int iCol ) ;
  145.     virtual int        Order() const        { return m_folderitem.iOrder ; }
  146.     virtual BOOL       ChildSortPending() const { return m_fSortPending ; }
  147.     virtual void       PendChildSort()          { m_fSortPending = TRUE ; } 
  148.     virtual void       UnpendChildSort()        { m_fSortPending = FALSE ; } 
  149. protected:
  150.     PROPFOLDERITEM          m_folderitem ;
  151.     CSourceDictionaryLite   m_srcs ;
  152.     BOOL                    m_fSortPending ;
  153. } ;
  154. //-------------------------------------------------------------------------//
  155. class CProperty  : public CPropertyTreeItem
  156. //-------------------------------------------------------------------------//
  157. {
  158. public:
  159.     CProperty( CPropertyTreeCtl* pCtl, const PROPERTYITEM& item ) ;
  160.     virtual ~CProperty() ;
  161.     virtual BOOL        Initialize() ;
  162.     const CPropVariant& Value() const               { return m_varComposite ; }
  163.     virtual void        SetDirty( BOOL bDirty ) ;
  164.     virtual BOOL        IsDirty() const             { return m_bDirty ; }
  165.     virtual HRESULT     Apply() ;
  166.     
  167.     //  Overrides of CPropertyTreeItem
  168. public:
  169.     virtual USHORT      Type() const      { return PIT_PROPERTY ; }
  170.     virtual ULONG       GetAccess() const { return m_propitem.dwAccess ; }
  171.     virtual BOOL        IsCompositeMismatch() const { return m_bCompositeMismatch ; }
  172.     virtual HTREEITEM   Insert( HWND hwndTree, HTREEITEM hParent, HTREEITEM hInsertAfter = TVI_LAST ) ;
  173.     virtual LRESULT     OnTreeItemCallback( TV_DISPINFO* pDI, BOOL& bHandled ) ;
  174.     virtual void        Remove() ;
  175.     virtual int         TierNumber() const { return PROPERTY_TIER ; }
  176.     virtual HRESULT     AddSource( const VARIANT* pvarSrc, ULONG dwAccess, const PROPVARIANT* pvarVal ) ;
  177.     virtual HRESULT     RemoveSource( const VARIANT* pvarSrc ) ;
  178.     virtual LONG        SourceCount() const ;
  179.     virtual BOOL        Reconcile( HWND hwndTree, LONG cMasterSourceCount ) ;
  180.     virtual LPCTSTR     ValueText() ;            // returns the cached formatted value text
  181.     virtual HRESULT     AssignValueFromText( BSTR bstrValue, BOOL bMakeDirty ) ;
  182.     virtual HRESULT     InitializeSelectionValues()     { return S_OK ; }
  183.     virtual HRESULT     AddSelectionValue( const PROPVARIANT* pvarVal, BSTR bstrDisplayText = NULL ) ;
  184.     virtual LONG        SelectionValueCount() const ;
  185.     virtual void        SetOrder( int i )    { m_propitem.iOrder = i ; }
  186.     virtual int         Order() const        { return m_propitem.iOrder ; }
  187. protected:
  188.     virtual void        HitTest( LPTVHITTESTINFO pHTI ) ;
  189.     virtual HWND        EditControl()  { return IsWindow( m_hwndEdit ) ? m_hwndEdit : NULL ; }
  190.     virtual HWND        ShowEditControl( UINT nShowCmd, LPCRECT prcEdit, CMetrics* ) ;
  191.     virtual void        RepositionEditControl( LPCRECT prcEdit, CMetrics* ) ;
  192.     virtual HWND        CreateEditControl( LPCRECT prc, CMetrics* ) ;
  193.     virtual void        PositionEditControl( HWND hwndCtl, LPCRECT prcItem, CMetrics* ) ;
  194.     virtual void        InitializeEditControl( HWND hwndCtl, CMetrics* ) ;
  195.     virtual LRESULT     OnEditControlCommand( UINT, UINT, HWND, CMetrics*, BOOL&) ;
  196.     virtual BOOL        OnKillFocus( UINT, CPropertyTreeItem* ) ;
  197.     virtual HRESULT     GetEditedValue( CPropVariant& varNew ) ;
  198.     virtual HRESULT     ConvertEditedValue( LPCTSTR pszVal, CPropVariant& varNew );
  199.     virtual BOOL        OnValueChanging( CPropVariant& varNew ) ;
  200.     virtual void        OnValueChanged( const CPropVariant& varNew ) ;
  201.     virtual BOOL        OnValueRestore() ;
  202.     virtual void        MakeCompositeValue() ;
  203.     virtual HRESULT     AddSelectionValue( CSelectionList&, const PROPVARIANT*, BSTR = NULL ) ;
  204.     virtual LPCTSTR     ValueTextFormat() ;      // returns the formatting string for the property's value.
  205.     virtual ULONG       ValueTextFormatFlags() ; // returns the formatting flags for the property's value.
  206.     virtual LPCTSTR     FormatValueText( ) ;     // formats the value text
  207.     virtual void        SetValueText( LPCTSTR pszText ) ;   // caches the formatted value text
  208.     virtual BOOL        DrawValueText() ;        // draws the cached formatted value text
  209.     virtual HRESULT     AssignValue( LPCTSTR pszValue, BOOL bMakeDirty = FALSE ) ;
  210.     virtual HRESULT     AssignValue( IN const CPropVariant* pvarValue, IN OPTIONAL LPCTSTR pszValue = NULL, IN OPTIONAL BOOL bMakeDirty = NULL ) ;
  211.     PROPERTYITEM        m_propitem ;
  212.     CValueDictionary    m_values ;
  213.     CPropVariant        m_varComposite ;
  214.     LPTSTR              m_pszValue ;
  215.     HWND                m_hwndEdit ;
  216.     BOOL                m_bDirty,
  217.                         m_bCompositeMismatch,
  218.                         m_bCtlInitialized ;
  219. } ;
  220. //-------------------------------------------------------------------------//
  221. class CComboBoxProperty  : public CProperty
  222. //-------------------------------------------------------------------------//
  223. {
  224. public:
  225.     CComboBoxProperty( CPropertyTreeCtl* pCtl, const PROPERTYITEM& propitem )
  226.         : CProperty( pCtl, propitem ) {}
  227. protected:
  228.     virtual int InsertUniqueComboValue( HWND hwndCbo, int idx, CPropVariant* pVar, LPCTSTR pszText ) ;
  229.     virtual int FindComboValue( HWND hwndCbo, const CPropVariant& var, ULONG uFlags ) const ;
  230.     virtual LRESULT  OnEditControlCommand( UINT, UINT, HWND, CMetrics*, BOOL& ) ;
  231. } ;
  232. //-------------------------------------------------------------------------//
  233. class CMruProperty  : public CComboBoxProperty
  234. //-------------------------------------------------------------------------//
  235. {
  236. public:
  237.     CMruProperty( CPropertyTreeCtl* pCtl, const PROPERTYITEM& propitem, int nLimit = 50 ) 
  238.         :  CComboBoxProperty( pCtl, propitem ), m_nLimit( nLimit ) {}
  239.     virtual  void  InitializeEditControl( HWND, CMetrics* ) ;
  240. protected:
  241.     virtual HRESULT InitializeSelectionValues() ;
  242.     virtual HRESULT AddSelectionValue( const PROPVARIANT*, BSTR = 0L ) ;
  243.     virtual LONG    SelectionValueCount() const  { return m_mru.Count() ; }
  244.     virtual HRESULT AssignValue( IN const CPropVariant* pvarValue, IN OPTIONAL LPCTSTR pszValue = NULL, IN OPTIONAL BOOL bMakeDirty = NULL ) ;
  245.     CSelectionList  m_mru ;
  246.     int             m_nLimit ;
  247. } ;
  248. //-------------------------------------------------------------------------//
  249. class CDateProperty : public CProperty
  250. //-------------------------------------------------------------------------//
  251. {
  252. public:
  253.     CDateProperty( CPropertyTreeCtl* pCtl, const PROPERTYITEM& propitem ) 
  254.         : CProperty( pCtl, propitem ) {}
  255. protected:
  256.     virtual HRESULT ConvertEditedValue( LPCTSTR pszVal, CPropVariant& varNew );
  257.     virtual void    InitializeEditControl( HWND hwndCtl, CMetrics* pMetrics ) ;
  258.     virtual ULONG   ValueTextFormatFlags()    { return VAR_DATEVALUEONLY ; }
  259. } ;
  260. //-------------------------------------------------------------------------//
  261. class CTextProperty : public CProperty
  262. //-------------------------------------------------------------------------//
  263. {
  264. public:
  265.     CTextProperty( CPropertyTreeCtl* pCtl, const PROPERTYITEM& propitem, int cchMax = 0xFF )
  266.         : CProperty( pCtl, propitem ), m_cchMax( cchMax ) {}
  267.     virtual BOOL IsMultiline() ;
  268. protected:
  269.     int m_cchMax ;
  270. } ;
  271. //-------------------------------------------------------------------------//
  272. class CEnumProperty : public CComboBoxProperty
  273. //-------------------------------------------------------------------------//
  274. {
  275. public:
  276.     CEnumProperty( CPropertyTreeCtl* pCtl, const PROPERTYITEM& propitem ) :
  277.         CComboBoxProperty( pCtl, propitem ) {}
  278.     virtual void    InitializeEditControl( HWND, CMetrics* ) ;
  279.     virtual BOOL    OnKillFocus( UINT, CPropertyTreeItem* ) ;
  280.     virtual BOOL    OnValueChanging( CPropVariant& varNew ) ;
  281.     virtual BOOL    OnValueRestore() ;
  282.     virtual HRESULT AssignValue( IN const CPropVariant* pvarValue, IN OPTIONAL LPCTSTR pszValue = NULL, IN OPTIONAL BOOL bMakeDirty = NULL ) ;
  283. protected:
  284.     virtual HRESULT AddSelectionValue( const PROPVARIANT*, BSTR = 0L ) ;
  285.     virtual LONG    SelectionValueCount() const  { return m_choices.Count() ; }
  286.     virtual LPCTSTR FormatValueText( ) ;     // formats the value text
  287.     CSelectionList  m_choices ;
  288. } ;
  289. //-------------------------------------------------------------------------//
  290. //  Inline implementation - CPropertyTreeItem
  291. inline CPropertyTreeItem::operator HTREEITEM() const { 
  292.     return m_hTreeItem ; 
  293. }
  294. inline CPropertyTreeItem* CPropertyTreeItem::GetTreeItem( const TV_ITEM& tvi ) { 
  295.     return (CPropertyTreeItem*)tvi.lParam ; 
  296. }
  297. inline CPropertyTreeCtl* CPropertyTreeItem::Control()   {
  298.     return m_pCtl ;
  299. }
  300. inline USHORT CPropertyTreeItem::Type() const  {
  301.     return PIT_NIL ;
  302. }
  303. inline LPCTSTR CPropertyTreeItem::GetName() const   { 
  304.     return m_szText[iName] ; 
  305. }
  306. inline LPCTSTR CPropertyTreeItem::GetDisplayName() const  { 
  307.     return m_szText[iDisplayName] ;
  308. }
  309. inline LPCTSTR CPropertyTreeItem::GetDescription() const  { 
  310.     return m_szText[iDescr] ;
  311. }
  312. inline int CPropertyTreeItem::ItemWidth( BOOL bTextOnly ) const  {
  313.     if( *m_szText[iDisplayName] == 0 ) return 0 ;
  314.     return bTextOnly ? m_cxValue + m_xValue : m_cxValue - m_xLabel ;
  315. }
  316. inline LRESULT CPropertyTreeItem::OnEditControlCommand( UINT, UINT, HWND, CMetrics*, BOOL& bHandled )    {
  317.     bHandled = FALSE ;
  318.     return 0L ;
  319. }
  320. inline LRESULT CPropertyTreeItem::OnEditControlNotify( NMHDR*, CMetrics*, BOOL& bHandled )    {
  321.     bHandled = FALSE ;
  322.     return 0L ;
  323. }
  324. inline HRESULT CPropertyTreeItem::AddSource( const VARIANT*, ULONG, const PROPVARIANT* ) { 
  325.     return E_NOTIMPL ; 
  326. }
  327. inline HRESULT CPropertyTreeItem::RemoveSource( const VARIANT* ) { 
  328.     return E_NOTIMPL ;
  329. }
  330. inline LONG CPropertyTreeItem::SourceCount() const { 
  331.     return 0 ;
  332. }
  333. inline BOOL CPropertyTreeItem::Reconcile( HWND, LONG )  {
  334.     return FALSE ;
  335. }
  336. inline int CPropertyTreeItem::InsertChildren()  { 
  337.     return 0 ;
  338. }
  339. inline void CPropertyTreeItem::Remove() {
  340.     HWND hwndTree = TreeHwnd() ;
  341.     if( hwndTree && TreeView_GetSelection( hwndTree )==m_hTreeItem && 
  342.         IsWindowVisible( hwndTree ) )
  343.         TreeView_SelectItem( hwndTree, NULL ) ;
  344.     m_hTreeItem = NULL ;
  345. }
  346. inline HWND CPropertyTreeItem::ShowEditControl( UINT, LPCRECT, CMetrics* ) { 
  347.     return NULL ; 
  348. }
  349. inline void CPropertyTreeItem::RepositionEditControl( LPCRECT, CMetrics* ) {
  350. }
  351. inline BOOL CPropertyTreeItem::OnKillFocus( UINT, CPropertyTreeItem* ) { 
  352.     return TRUE ;
  353. }
  354. inline LPCTSTR CPropertyTreeItem::ValueText() { 
  355.     return NULL ;
  356. }
  357. inline BOOL CPropertyTreeItem::OnValueChanging( CPropVariant& ) { 
  358.     return TRUE ;
  359. }
  360. inline void CPropertyTreeItem::OnValueChanged( const CPropVariant& ) {
  361. }
  362. inline int* CPropertyTreeItem::ChildSortDir()  { 
  363.     return m_iChildSortDir ;
  364. }
  365. inline BOOL CPropertyTreeItem::ChildSortPending() const { 
  366.     return FALSE ;
  367. }
  368. inline BOOL CPropertyTreeItem::DrawValueText() { 
  369.     return FALSE ;
  370. }
  371. #define ASSIGN_PROPITEM_TEXT(psz, i) 
  372.     if((psz)) lstrcpyn( m_szText[(i)], (psz), CCH_PROPERTY_TEXT ) ; 
  373.     else *m_szText[(i)] = (TCHAR)0
  374. inline void CPropertyTreeItem::SetName( LPCTSTR pszName )   { 
  375.     ASSIGN_PROPITEM_TEXT( pszName, iName ) ;
  376. }
  377. inline void CPropertyTreeItem::SetDisplayName( LPCTSTR pszDisplayName ) { 
  378.     ASSIGN_PROPITEM_TEXT( pszDisplayName, iDisplayName ) ;
  379. }
  380. inline void CPropertyTreeItem::SetDescription( LPCTSTR pszDescr )   { 
  381.     ASSIGN_PROPITEM_TEXT( pszDescr, iDescr ) ;
  382. }
  383. inline BOOL CPropertyTreeItem::IsMultiline() {
  384.     return FALSE ;
  385. }
  386. inline HRESULT CProperty::AddSelectionValue( const PROPVARIANT*, BSTR ) {
  387.     return S_OK ;
  388. }
  389. inline LONG CProperty::SelectionValueCount() const  {
  390.     return 0L ;
  391. }
  392. inline LPCTSTR CProperty::ValueTextFormat()    {
  393.     return NULL ;
  394. }
  395. inline ULONG CProperty::ValueTextFormatFlags() {
  396.     return 0L ;
  397. }
  398. inline LPCTSTR CProperty::ValueText()           { 
  399.     return m_pszValue ? m_pszValue : FormatValueText() ; 
  400. }
  401. #endif __TREEITEMS_H__