//  Beta release only.
//
//  Copyright(c) Ivyware Pty Ltd 2011  (all rights reserved)
//               MELBOURNE, VICTORIA, AUSTRALIA, 3130
//
//  Provided no fee is charged (either by the distributer or user)
//  permission to use, copy, modify, and distribute this software and
//  its documentation for any purpose and without fee is hereby granted,
//  provided that the above copyright notice appear in all copies and
//  that both that copyright notice and this permission notice appear
//  in supporting documentation.
//
//  No waranty or suitablity for purpose is implied.
//
//
//  P2PmsgTreeCtrl prototypes and definitions.
//  NOTES: Integrates the CTreeCtrl and P2PmsgMgr objects
//
#pragma   once

#include "P2PeerTarget.h"
#include "P2PmsgMgr.h"
#include "P2PmsgMFC.h"

//  Pre-definitions
typedef CMap<P2Pos, P2Pos, HTREEITEM, HTREEITEM> CMap_HTREE_P2Pos;

class P2PmsgMFC_EXT CP2PmsgTreeCtrl : public CTreeCtrl, public P2PeerTarget
{
	  DECLARE_DYNAMIC(CP2PmsgTreeCtrl)

    // Constructors and destructor
    public:
	      CP2PmsgTreeCtrl();
	    virtual
       ~CP2PmsgTreeCtrl();
      bool
        Connect ( P2PmsgMgr *pP2PmsgMgr, P2Pos posRoot
                , HWND hWndTriggers );
      void
        RecyleThisObject ( );

    // Notifications
    public:
      bool
        NotifyP2Pmsg_AddNode   ( CWnd *pCWndNotify
                               , const P3PmsgNode& oNodeParent
                               , const P3PmsgNode& oNodeChild );
      bool
        NotifyP2Pmsg_MergeNode ( CWnd *pCWndNotify
                               , const P3PmsgNode& oNode );

    // Operations
    public:
      HTREEITEM
        InsertP2Pmsg ( HTREEITEM hItemParent, const P3PmsgNode& oNode );
      HTREEITEM
        InsertP2Pmsg ( HTREEITEM hItemParent, const P3PmsgField& oField );
      HTREEITEM
        UpdateP2Pmsg ( HTREEITEM hItem, const P3PmsgNode& oNode );

    // CTreeCtrl extensions
    public:
	    void
        ShuffleItemUp ( HTREEITEM hItem );
	    void
        ShuffleItemDown ( HTREEITEM hItem );
      static LPARAM
        MoveItemCB ( CTreeCtrl *pThis
                   , HTREEITEM hItemSource, HTREEITEM hItemDestin );

    // Attributes
    protected:
      CMap_HTREE_P2Pos  m_oCMapHTREE;
      HTREEITEM         m_hRootItem;
      P2PmsgMgr        *m_pP2PmsgMgr_Ext;
      P2PmsgMgr        *m_pP2PmsgMgr;
      HWND              m_hWndTriggers;

    // Life cycle management
    public:
      UINT
        AddRef ( );
      UINT
        Release ( );

    // Properties
    public:
      HTREEITEM
        IsItem ( P2Pos pos );
      P2PmsgMgr&
        GetP2PmsgMgr ( );
      P3PmsgNode
        GetItemNode ( HTREEITEM hItem );
      P3PmsgAttr
        GetItemAttr ( HTREEITEM hItem );
      //virtual int
      //  GetImage ( P2Pos posP2Pobject );
      //virtual int
      //  GetSelectedImage ( P2Pos posP2Pobject );

    // MFC Virtual overrides
    protected:
	  virtual BOOL
        OnNotify ( WPARAM wParam, LPARAM lParam, LRESULT *pResult );

    // MFC message map
    protected:
	DECLARE_MESSAGE_MAP()
    public:
      LRESULT
        OnP2PmsgTrigINSERT ( WPARAM wParam, LPARAM lParam );
      LRESULT
        OnP2PmsgTrigUPDATE ( WPARAM wParam, LPARAM lParam );
      LRESULT
        OnP2PmsgTrigDELETE ( WPARAM wParam, LPARAM lParam );
      afx_msg void
        OnTvnDeleteItem ( NMHDR *pNMHDR, LRESULT *pResult );
      afx_msg BOOL
        OnTvnGetDispInfo ( NMHDR *pNMHDR, LRESULT *pResult );
      afx_msg BOOL
        OnTvnItemExpanding ( NMHDR *pNMHDR, LRESULT *pResult );
      afx_msg void
        OnNMDblclk ( NMHDR *pNMHDR, LRESULT *pResult );
};

//
//  
P2PmsgMFC_EXT bool
CTreeCtrl_EverExpanded ( CTreeCtrl *pCTreeCtrl, HTREEITEM hItem );
P2PmsgMFC_EXT void
CTreeCtrl_ClearUserData( CTreeCtrl *pCTreeCtrl, HTREEITEM hItem );

//
//  Windows message loop integration prototypes and definitions
//  NOTES: Definition sequence is designed to trap use of the
//         message number elsewhere
const DWORD WM_APP_0x0001       = WM_APP + 0x0001;
const DWORD WM_P2Pmsg_MergeNode = WM_APP_0x0001;
const DWORD WM_APP_0x0002       = WM_APP + 0x0002;
const DWORD WM_P2Pmsg_AddNode   = WM_APP_0x0002;
const DWORD WM_APP_0x0003       = WM_APP + 0x0003;
const DWORD WM_P2Pmsg_DropNode  = WM_APP_0x0003;

