////////////////////////////////////////////////////////////////////////////////
//
//   ActiveX Control-
//  -  
// <VCTree.h>
//
////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "VCTree.h"
#include "TreeDlg.h"
#include "VCTreeCtl.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

char buf[20];

/////////////////////////////////////////////////////////////////////////////
// TreeDlg dialog
TreeDlg::TreeDlg(CVCTreeCtrl &ctrl, CWnd* pParent /*=NULL*/)
	: CDialog(TreeDlg::IDD, pParent)
  , m_ctrl  (ctrl)
{
	//{{AFX_DATA_INIT(TreeDlg)
	
	//}}AFX_DATA_INIT
}



//-----------------------------------------------------------------------------
/// 
// ---
void TreeDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(TreeDlg)
	DDX_Control(pDX, IDC_TREE_VIEW_CTRL, m_TreeView);
  DDX_Control(pDX, IDC_TOOLBAR_CTRL,   m_ToolBar);
	//}}AFX_DATA_MAP
}


//-----------------------------------------------------------------------------
/// 
// ---
BEGIN_MESSAGE_MAP(TreeDlg, CDialog)
	//{{AFX_MSG_MAP(TreeDlg)
	ON_WM_CREATE()
	ON_WM_SIZE()
  ON_WM_GETDLGCODE()
  ON_COMMAND_RANGE( IDM_MIN, IDM_MAX, OnContextMenuCommand )
  
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


//-----------------------------------------------------------------------------
/// 
// ---
UINT TreeDlg::OnGetDlgCode()
{
  return DLGC_WANTARROWS | DLGC_WANTALLKEYS | DLGC_WANTCHARS | DLGC_WANTTAB;
}


//-----------------------------------------------------------------------------
/// 
// ---
BOOL TreeDlg::OnInitDialog() 
{
 	CDialog::OnInitDialog();
	return TRUE; // return TRUE unless you set the focus to a control
               // EXCEPTION: OCX Property Pages should return FALSE
}


//-----------------------------------------------------------------------------
/// 
// ---
int TreeDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
  	AFX_MANAGE_STATE(_afxModuleAddrThis);

	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// Create the style
	DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_TABSTOP | LVS_REPORT;
  if ( m_ToolBar.Create(NULL, dwStyle, CRect(0, 0, 0, 0), this, IDC_TOOLBAR_CTRL) &&
       m_TreeView.Create(NULL, dwStyle, CRect(0, 0, 0, 0), this, IDC_TREE_VIEW_CTRL ) ) 
  {
    m_ToolBar->AllowCustomize = FALSE; //      
    m_ToolBar->Appearance = ccFlat; 
    m_ToolBar->Style = tbrFlat;
    m_ToolBar->Wrappable = FALSE;

    m_TreeView.SetFireWnd( this );
    return 0;
  }
	else
	  return -1;
}


//-----------------------------------------------------------------------------
/// 
// ---
void TreeDlg::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);
	
  CRect toolBarRect( 0,0,0,0 );

  if ( ::IsWindow(m_ToolBar.m_hWnd) )
  {
    IButtonsPtr buttons ( m_ToolBar->GetButtons() );
    
    if ( (bool)buttons && buttons->GetCount() == 0 )
      m_ToolBar.ShowWindow( SW_HIDE );
    else
    {
      m_ToolBar->ButtonHeight;
      m_ToolBar.GetClientRect( &toolBarRect );
      m_ToolBar.ShowWindow( SW_SHOW ); 
      m_ToolBar.SetWindowPos(  NULL, 0, 0, cx, toolBarRect.Size().cy, SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER );
      m_ToolBar.RedrawWindow();
      m_ToolBar.GetClientRect( &toolBarRect );
      m_ToolBar->ButtonHeight;
    }
  }

 	if ( ::IsWindow(m_TreeView.m_hWnd) )
		m_TreeView.MoveWindow(CRect(0, toolBarRect.Size().cy + 1, cx, cy), TRUE);
}


//-----------------------------------------------------------------------------
/// 
// ---
LPDISPATCH TreeDlg::GetTree()
{
  if ( m_TreeView.ip )
    m_TreeView.ip->AddRef();

  return m_TreeView.ip;
}


//-----------------------------------------------------------------------------
/// 
// ---
LPDISPATCH TreeDlg::GetToolBar()
{
  if ( m_ToolBar.ip )
    m_ToolBar.ip->AddRef();

  return m_ToolBar.ip;
}



//-----------------------------------------------------------------------------
/// 
// ---
LPDISPATCH TreeDlg::CreateImageList()
{
  LPDISPATCH res = NULL;
 	// Create the style
	DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_TABSTOP | LVS_REPORT;
  CImageListOleCtrl * newList = new CImageListOleCtrl;
  m_ImageLists.Add( newList );
  
  if ( newList->Create(NULL, dwStyle, CRect(0, 0, 0, 0), this, IDC_IMAGELIST_CTRL_START + m_ImageLists.MaxIndex() ) ) 
  {
    if ( newList->ip )
    {
      newList->ip->AddRef();
      res = newList->ip;
    }
    else
      ASSERT(FALSE);
  }
  else
    ASSERT(FALSE);


  return res;
}


//-----------------------------------------------------------------------------
/// 
// ---
void TreeDlg::SetContextMenu(long val) 
{
  m_TreeView.SetContextMenu( (HMENU)val );
}


//-----------------------------------------------------------------------------
/// 
// ---
BOOL TreeDlg::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo) 
{
	return CDialog::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}


//-----------------------------------------------------------------------------
/// 
// ---
void TreeDlg::OnContextMenuCommand ( UINT id )
{
  m_ctrl.FireMenuCommand( id );
}


//-----------------------------------------------------------------------------
/// 
// ---
BEGIN_MESSAGE_MAP(CTreeOleCtrl, CTreeOleCtrlBase)
	//{{AFX_MSG_MAP(TreeDlg)
	ON_WM_CONTEXTMENU()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


//-----------------------------------------------------------------------------
/// 
// ---
BEGIN_EVENTSINK_MAP(CTreeOleCtrl, CTreeOleCtrlBase)
  ON_EVENT_REFLECT(CTreeOleCtrl, 0xfffffda1,  MouseUp, VTS_I2 VTS_I2 VTS_I4 VTS_I4 )
END_EVENTSINK_MAP()


//-----------------------------------------------------------------------------
/// 
// ---
void CTreeOleCtrl::MouseUp ( short Button, short Shift, OLE_XPOS_PIXELS x,
                                                        OLE_YPOS_PIXELS y )
{
  if ( Button == 2 )
  {
    CPoint point(x, y );
    ClientToScreen( &point );
    ShowContextMenu( point.x, point.y );
  }
}

//-----------------------------------------------------------------------------
///  
// ---
void CTreeOleCtrl::ShowContextMenu( int x, int y )
{
  if ( :: IsMenu(m_hPopupMenu) )
  {
    ::TrackPopupMenu( m_hPopupMenu, TPM_LEFTALIGN, x, y, 0, m_MenuParent ? *m_MenuParent : *(CWnd*)this, NULL );
    RedrawWindow( NULL, NULL, RDW_ERASE | RDW_INVALIDATE | RDW_FRAME | RDW_ALLCHILDREN );
  }  
}


//  K11 //-----------------------------------------------------------------------------
//  K11 ///  Node  
//  K11 // point    
//  K11 // ---
//  K11 void CTreeOleCtrl::SelectNode( UINT nFlags, CPoint point )
//  K11 {
//  K11   // Get a device context.
//  K11   CDC * hdc = GetDC();
//  K11   if ( hdc )
//  K11   {
//  K11     float xx = point.x * 1440.0f/hdc->GetDeviceCaps(LOGPIXELSX);
//  K11     float yy = point.y * 1440.0f/hdc->GetDeviceCaps(LOGPIXELSY);
//  K11     INodePtr node = ip->HitTest ( xx, yy );
//  K11 
//  K11     if ( node != ip->SelectedItem )
//  K11       ip->SelectedItem = node;
//  K11   }
//  K11   else
//  K11     ASSERT(FALSE); //  
//  K11 }
