// dlginput.cpp : implementation file
//

#include "stdafx.h"
#include <direct.h>
#include <shlobj.h>
#include "ACP_Base.h"
#include "ITF.h"

#include "dlginput.hpp"

#include "TUT.h"
#include "..\Main\Inc\_EditID.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDialogInput dialog
#define M_DeleteOnEnd( string, charact )\
{\
	if( string . GetLength() )\
	{\
		string . MakeReverse();\
		while( string[0] == charact )		string = string . Mid( 1 );\
		string . MakeReverse();\
	}\
}

CDialogInput::CDialogInput(const CString _csStaticText, const CString _csPathText  /*=""*/,
						   const CString _csFileText /*=""*/, BOOL _bRecursive /*=FALSE*/, CWnd* pParent /*=NULL*/)
	: CDialog(CDialogInput::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDialogInput)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_csStaticText = _csStaticText;
	m_csPathText = _csPathText;
	m_csFileText = _csFileText;
	m_bRecursive = _bRecursive;

}


void CDialogInput::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDialogInput)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDialogInput, CDialog)
	//{{AFX_MSG_MAP(CDialogInput)
    ON_WM_DRAWITEM()
	ON_WM_DESTROY()
	ON_WM_CANCELMODE()
	ON_BN_CLICKED(IDC_BUTTON_READ, OnBtRead)
	ON_BN_CLICKED(IDC_BUTTON_BROWSE, OnBtBrowse)
	ON_LBN_SELCHANGE(IDC_LIST_FILE, OnSelChange)
	ON_LBN_DBLCLK(IDC_LIST_FILE, OnDoubleClick)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDialogInput message handlers

void CDialogInput::OnOK() 
{
	// TODO: Add extra validation here
	GetDlgItem( IDC_EDIT_TEXT ) -> GetWindowText( m_csEditText );	
	if( m_csEditText . GetLength() )
	{
		GetDlgItem( IDC_EDIT_PATH ) -> GetWindowText( m_csPathText );
		m_bRecursive = ((CButton*) GetDlgItem( IDC_CHECK_RECURSIVE )) -> GetCheck();
		CDialog::OnOK();
	}
}

BOOL CDialogInput::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	GetDlgItem( IDC_EDIT_TEXT ) -> SetWindowText( m_csFileText );
	GetDlgItem( IDC_EDIT_PATH ) -> SetWindowText( m_csPathText );
	GetDlgItem( IDC_STATIC_TEXT ) -> SetWindowText( m_csStaticText );
	((CButton*) GetDlgItem( IDC_CHECK_RECURSIVE )) -> SetCheck( m_bRecursive );
	GetDlgItem( IDC_EDIT_TEXT ) -> SetFocus();
	if( m_csPathText . GetLength() )
		m_fn_vFillList();
	//
    TUT_M_vGetTutDll();
    TUT_M_vRegisterControlID( IDC_EDIT_TEXT,	"TUT_INPUT_TEXT",	TUT_e_TextEdit );
    TUT_M_vRegisterControlID( IDOK,				"TUT_INPUT_OK",		TUT_e_Button );
    TUT_M_vRegisterControlID( IDCANCEL,			"TUT_INPUT_CANCEL",	TUT_e_Button );
    TUT_M_vRegisterControlID( IDC_BUTTON_READ,  "TUT_INPUT_READ",	TUT_e_Button );
    TUT_M_vRegisterControlID( IDC_BUTTON_BROWSE,"TUT_INPUT_BROWSE",	TUT_e_Button );
    TUT_M_vRegisterControlID( IDC_LIST_FILE,	"TUT_INPUT_LIST",	TUT_e_ListBox );
    TUT_M_vRegisterControlID( IDC_EDIT_PATH,	"TUT_INPUT_PATH",	TUT_e_TextEdit );

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CDialogInput::OnDestroy() 
{
    TUT_M_vGetTutDll();
    TUT_M_vUnregisterControlID( IDC_EDIT_TEXT		);
    TUT_M_vUnregisterControlID( IDOK				);
    TUT_M_vUnregisterControlID( IDCANCEL			);
    TUT_M_vUnregisterControlID( IDC_BUTTON_READ		);
    TUT_M_vUnregisterControlID( IDC_BUTTON_BROWSE	);
    TUT_M_vUnregisterControlID( IDC_LIST_FILE		);
    TUT_M_vUnregisterControlID( IDC_EDIT_PATH		);

	CDialog::OnDestroy();
}

void CDialogInput::OnBtBrowse()
{
    // TODO: Add your control notification handler code here
    BROWSEINFO bi; 
    LPSTR lpBuffer = (LPSTR)malloc(_MAX_PATH); 
    LPITEMIDLIST pidlBrowse;    // PIDL selected by user 
  

    // Fill in the BROWSEINFO structure. 
    bi.hwndOwner = m_hWnd; 
    bi.pidlRoot = NULL; 
    bi.pszDisplayName = lpBuffer; 
    bi.lpszTitle = "Choose a Path"; 
    bi.ulFlags = 0; 
    bi.lpfn = NULL; 
    bi.lParam = 0; 
 
    // Browse for a folder and return its PIDL. 
    pidlBrowse = SHBrowseForFolder(&bi); 
    if (pidlBrowse != NULL)
    {  
        if (SHGetPathFromIDList(pidlBrowse, lpBuffer)) 
        {
			GetDlgItem( IDC_EDIT_PATH ) -> SetWindowText( lpBuffer );
        }
    } 
 
    // Clean up. 
    free(lpBuffer);     
    
}

void CDialogInput::OnBtRead()
{
	m_fn_vFillList();
}

void CDialogInput::OnSelChange()
{
	CListBox *pLB = (CListBox*) GetDlgItem( IDC_LIST_FILE );
	int		 iIndex = pLB -> GetCurSel();

	if( iIndex != LB_ERR )
	{
		CString csText;
		CString csPath;

		GetDlgItem( IDC_EDIT_PATH ) -> GetWindowText( csPath );

		M_DeleteOnEnd( csPath, '\\' )
		
		pLB -> GetText( iIndex, csText );
		if( csText [0] == '\\' )
		{
			GetDlgItem( IDC_EDIT_TEXT ) -> SetWindowText( csPath + csText + ".tut" );
		}
		else
		{
			GetDlgItem( IDC_EDIT_TEXT ) -> SetWindowText( csPath + "\\" + csText + ".tut" );
		}
	}
}

void CDialogInput::OnDoubleClick()
{
	CListBox *pLB = (CListBox*) GetDlgItem( IDC_LIST_FILE );
	int		 iIndex = pLB -> GetCurSel();

	if( iIndex != LB_ERR )
		OnOK();
}

void CDialogInput::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDIS)
{
    if ( (nIDCtl == IDC_LIST_FILE) &&
         (GetDlgItem( IDC_LIST_FILE )->IsWindowEnabled() ) &&
         (lpDIS->itemID != LB_ERR) )
    {
		CDC         *pDC = CDC::FromHandle( lpDIS->hDC );
		CString		csText , csPath;
		CListBox	*pLB = (CListBox*) GetDlgItem( IDC_LIST_FILE );
		BOOL        bSelected = (lpDIS->itemState & ODS_SELECTED );
		int			iPos;
		CSize		oSize;

		pLB -> GetText(lpDIS->itemID, csText ) ;
		iPos = csText . ReverseFind( '\\' );
		if( iPos != -1 )
		{
			iPos++;
			csPath = csText . Left( iPos );
			oSize = pDC -> GetTextExtent( csPath );

			pDC->FillSolidRect( &lpDIS->rcItem, GetSysColor( COLOR_WINDOW ));
			pDC->SetBkMode( TRANSPARENT);

			pDC->SetTextColor( RGB(0, 0, 255) );
			lpDIS->rcItem.left += 1;
			pDC->DrawText( csPath , &lpDIS->rcItem, DT_SINGLELINE | DT_LEFT | DT_VCENTER );

			lpDIS->rcItem.left += oSize . cx;
			pDC->SetTextColor( RGB(255, 0, 0) );
			pDC->DrawText( csText . Mid( iPos ) , &lpDIS->rcItem, DT_SINGLELINE | DT_LEFT | DT_VCENTER );

			lpDIS->rcItem.left -= oSize . cx;
			lpDIS->rcItem.left -= 1;
		}
		else
		{
			lpDIS->rcItem.left += 1;

			pDC->SetTextColor( RGB(255, 0, 0) );
			pDC->DrawText( csText , &lpDIS->rcItem, DT_SINGLELINE | DT_LEFT | DT_VCENTER );

			lpDIS->rcItem.left -= 1;
		}
		if (bSelected)
			pDC->InvertRect( &lpDIS->rcItem );
    }
    else
        CDialog::OnDrawItem(nIDCtl, lpDIS);
}

void CDialogInput::m_fn_vFillList()
{
	CListBox *pLB = (CListBox*) GetDlgItem( IDC_LIST_FILE );

	pLB -> ResetContent();

	// get path
	CString csPath;
	BOOL	bRecursive;

	GetDlgItem( IDC_EDIT_PATH ) -> GetWindowText( csPath );
	bRecursive = ((CButton*) GetDlgItem( IDC_CHECK_RECURSIVE )) -> GetCheck();

	M_DeleteOnEnd( csPath, '\\' )
		
	if( ! csPath . IsEmpty() )
		m_fn_vRecurFillList( csPath , "" , bRecursive );
	else
		MessageBox( "The Path is empty" , "Error" , MB_OK + MB_ICONERROR );
}

void CDialogInput::m_fn_vRecurFillList( const CString _csBeginPathName , const CString _csCurrentPathName, BOOL _bRecursive )
{
    WIN32_FIND_DATA stWFD;                  // structure for searching file
    HANDLE          hFD;
    BOOL            bContinue;                    // handle for searching file
	CListBox		*pLB = (CListBox*) GetDlgItem( IDC_LIST_FILE );

    // search for files
    hFD = FindFirstFile( _csBeginPathName + _csCurrentPathName + "\\*.tut", &stWFD );
    bContinue = ( hFD != INVALID_HANDLE_VALUE );
    while( bContinue )
    {
        if ( !(stWFD.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
        {
			CString csFileName;
			
			if( _csCurrentPathName . GetLength() )
				csFileName = _csCurrentPathName + "\\" + stWFD.cFileName;
			else
				csFileName = stWFD.cFileName;

            pLB -> AddString( csFileName . Left( csFileName . GetLength() - 4 ) );
        }
        bContinue = FindNextFile( hFD, &stWFD);
    }

    // close FindData handle
    FindClose(hFD);

    // search for directory
    if( _bRecursive )
    {
        hFD = FindFirstFile( _csBeginPathName + _csCurrentPathName + "\\*", &stWFD );
        bContinue = ( hFD != INVALID_HANDLE_VALUE );
        while( bContinue )
        {
            if ((stWFD.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && (stWFD.cFileName[0] != '.') )
            {
                // appel recursif
                m_fn_vRecurFillList( _csBeginPathName , _csCurrentPathName + "\\" + stWFD.cFileName, TRUE );
            }
            bContinue = FindNextFile( hFD, &stWFD);
        }

        // close FindData handle
        FindClose(hFD);
    }
}
