// DM2GDEDView.cpp : インプリメンテーション ファイル
//

#include "stdafx.h"
#include "DM2GDED.h"
#include "DM2GDEDDoc.h"
#include "DM2GDEDView.h"

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

#define IDW_GRID 0x0100

BOOL AFXAPI AfxIsDescendant(HWND hWndParent, HWND hWndChild);

/////////////////////////////////////////////////////////////////////////////
// CDM2GDEDView

IMPLEMENT_DYNCREATE(CDM2GDEDView, CView)

CDM2GDEDView::CDM2GDEDView()
	: m_pEdCtx(NULL)
{

}

CDM2GDEDView::~CDM2GDEDView()
{
	InitEdCtx(NULL);
}

BEGIN_MESSAGE_MAP(CDM2GDEDView, CView)
	//{{AFX_MSG_MAP(CDM2GDEDView)
	ON_WM_SIZE()
	ON_WM_ERASEBKGND()
	ON_WM_SETFOCUS()
	ON_WM_CREATE()
	ON_COMMAND(ID_CTRL_TREEVIEW, OnCtrlTreeview)
	ON_COMMAND(ID_CTRL_GRIDVIEW, OnCtrlGridview)
	//}}AFX_MSG_MAP
	ON_COMMAND_EX(ID_NEXT_PANE, OnNextPane)
	ON_COMMAND_EX(ID_PREV_PANE, OnNextPane)
	ON_UPDATE_COMMAND_UI(ID_NEXT_PANE, OnUpdateNextPane)
	ON_UPDATE_COMMAND_UI(ID_PREV_PANE, OnUpdateNextPane)
	ON_COMMAND(ID_UPDATE_CURRENT_PANE, OnUpdatePane)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDM2GDEDView 描画

void CDM2GDEDView::OnDraw(CDC* pDC)
{
	CDocument* pDoc = GetDocument();
	// TODO: この位置に描画用のコードを追加してください
}

/////////////////////////////////////////////////////////////////////////////
// CDM2GDEDView 診断

#ifdef _DEBUG
void CDM2GDEDView::AssertValid() const
{
	CView::AssertValid();
}

void CDM2GDEDView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CDM2GDEDView メッセージ ハンドラ

void CDM2GDEDView::OnGDUpdate(UINT nHint, void *pHint)
{
	if (!m_pEdCtx) return;

	switch (nHint) {
	case PM_V_RELOAD:
		break;
	case PM_V_SEL_TREE_ITEM:
		OnSelTreeItem(*(TSelTreeItem *)pHint);
		break;
	}
}

void CDM2GDEDView::InitEdCtx(CGDEdCtx *pEdCtx)
{
	if (m_pEdCtx)
		m_pEdCtx->RemoveCB(this),
		m_pEdCtx->Release();
	if (m_pEdCtx = pEdCtx)
		m_pEdCtx->AddRef(),
		m_pEdCtx->AddCB(this);

	if (m_pGrid) m_pGrid->InitEdCtx(pEdCtx);
}

CDM2GDEDDoc* CDM2GDEDView::GetDocument()
{
	return static_cast<CDM2GDEDDoc*>(CView::GetDocument());
}

void CDM2GDEDView::OnInitialUpdate() 
{
	CView::OnInitialUpdate();

	InitEdCtx(GetDocument()->GetEdCtx(FALSE));

	CWnd *pParentWnd;
	if (pParentWnd = GetParent())
		pParentWnd->ModifyStyleEx(WS_EX_CLIENTEDGE, 0, SWP_FRAMECHANGED);
}

void CDM2GDEDView::OnSize(UINT nType, int cx, int cy)
{
	CView::OnSize(nType, cx, cy);
	
	RepositionBars(0, 0xffff, 0x0100);
	
}

void CDM2GDEDView::OnSelTreeItem(TSelTreeItem &rItem)
{
	m_pGrid->CreateList(rItem.lower, rItem.upper);
}

BOOL CDM2GDEDView::OnEraseBkgnd(CDC* pDC) 
{
	return TRUE;
//	
//	return CView::OnEraseBkgnd(pDC);
}

BOOL CDM2GDEDView::OnNextPane(UINT nID)
{
	m_fr.RotateFocus((nID == ID_NEXT_PANE) ? +1 : -1);
	HWND hWnd = m_fr.FindRecentFocus();
	if (hWnd) ::SetFocus(hWnd);
	return TRUE;
}

void CDM2GDEDView::OnUpdateNextPane(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(TRUE);
}

void CDM2GDEDView::OnSetFocus(CWnd* pOldWnd) 
{
	CView::OnSetFocus(pOldWnd);
	
	HWND hWnd = m_fr.FindRecentFocus();
	if (hWnd) ::SetFocus(hWnd);
}

BOOL CDM2GDEDView::PreCreateWindow(CREATESTRUCT& cs) 
{
	cs.lpszName = "List";
	
	return CView::PreCreateWindow(cs);
}

int CDM2GDEDView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;

	CGDEdCtx *pEdCtx = GetDocument()->GetEdCtx(FALSE);

	CCreateContext *pContext = (CCreateContext *)lpCreateStruct->lpCreateParams;

	if (false
		|| !m_wndSplitter.CreateStatic(this, 1, 2, 0 |WS_CHILD |WS_VISIBLE, 0x0100)
		|| !m_wndSplitter.CreateView(0, 0, RUNTIME_CLASS(CDTreeDock), CSize(200, 200), pContext)
		|| !m_wndSplitter.CreateView(0, 1, RUNTIME_CLASS(CDGridCtrl), CSize(200, 200), pContext)
	) {
		return -1;
	}
	m_pVw = static_cast<CDTreeDock *>(m_wndSplitter.GetPane(0, 0));
	m_pVw->Init(pEdCtx);
	m_pVw->ModifyStyle(0, WS_TABSTOP);
	m_pGrid = static_cast<CDGridCtrl *>(m_wndSplitter.GetPane(0, 1));
	m_pGrid->EnableSelection(FALSE);
	m_pGrid->ModifyStyleEx(WS_EX_CLIENTEDGE, 0, SWP_FRAMECHANGED);
	m_pGrid->ModifyStyle(0, WS_TABSTOP);

	m_fr.listHwnd.push_back(m_pVw->GetSafeHwnd());
	m_fr.listHwnd.push_back(m_pGrid->GetSafeHwnd());

	return 0;
}

void CDM2GDEDView::PostNcDestroy() 
{
	if (m_pGrid) { delete m_pGrid; m_pGrid = NULL; }

	CView::PostNcDestroy();
}

void CDM2GDEDView::OnCtrlTreeview() 
{
	CWnd *pWnd = m_wndSplitter.GetPane(0, 0);
	if (pWnd && pWnd->IsWindowEnabled()) {
		pWnd->SetFocus();
	}
}

void CDM2GDEDView::OnCtrlGridview() 
{
	CWnd *pWnd = m_wndSplitter.GetPane(0, 1);
	if (pWnd && pWnd->IsWindowEnabled()) {
		pWnd->SetFocus();
	}
}

void CDM2GDEDView::OnUpdatePane()
{
	m_fr.MoveFocusTo(::GetFocus());
}
