// wiz3Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "wiz3.h"
#include "wiz3Dlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CWiz3Dlg dialog

CWiz3Dlg::CWiz3Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CWiz3Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CWiz3Dlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CWiz3Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWiz3Dlg)
	DDX_Control(pDX, IDC_DETALII, m_lcDetalii);
	DDX_Control(pDX, IDC_LISTA_FISIERE, m_lbFisiere);
	DDX_Control(pDX, IDC_ARBORE_DIRECTOARE, m_treeDirectoare);
	DDX_Control(pDX, IDC_DIRECTOR_PRINCIPAL, m_cbDirPrinc);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CWiz3Dlg, CDialog)
	//{{AFX_MSG_MAP(CWiz3Dlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_CBN_SELCHANGE(IDC_DIRECTOR_PRINCIPAL, OnSelchangeDirectorPrincipal)
	ON_NOTIFY(TVN_SELCHANGED, IDC_ARBORE_DIRECTOARE, OnSelchangedArboreDirectoare)
	ON_LBN_SELCHANGE(IDC_LISTA_FISIERE, OnSelchangeListaFisiere)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWiz3Dlg message handlers

BOOL CWiz3Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	PopulezCombo();
    m_lcDetalii.InsertColumn(0,"Nume Fisier", LVCFMT_LEFT, 120);
    m_lcDetalii.InsertColumn(1,"Atribute", LVCFMT_CENTER, 120);
    m_lcDetalii.InsertColumn(2,"Dimensiune Kb", LVCFMT_RIGHT, 100);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CWiz3Dlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CWiz3Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CWiz3Dlg::PopulezCombo()
{
   TCHAR	TamponCale[MAX_PATH];
   TCHAR	TamponCopieCale[MAX_PATH];
   GetWindowsDirectory(TamponCale, MAX_PATH);
   strcpy(TamponCopieCale,TamponCale);
   TamponCopieCale[2]=0;
   m_cbDirPrinc.AddString(TamponCopieCale);
   m_cbDirPrinc.AddString(TamponCale);
   GetSystemDirectory(TamponCale, MAX_PATH);
   m_cbDirPrinc.AddString(TamponCale);

   GetCurrentDirectory(MAX_PATH, TamponCale);
   m_cbDirPrinc.AddString(TamponCale);
   nIndex=m_cbDirPrinc.SelectString(-1,TamponCopieCale);
   m_cbDirPrinc.GetLBText(nIndex, m_strSelect);
   PopulezArbore();
   PopulezLista();
}

void CWiz3Dlg::OnSelchangeDirectorPrincipal() 
{
	// TODO: Add your control notification handler code here
   nIndex = m_cbDirPrinc.GetCurSel();
   if (nIndex != CB_ERR) 
   {
      m_cbDirPrinc.GetLBText(nIndex, m_strSelect);
	  m_strCopie=m_strSelect;
      PopulezArbore();
	  PopulezLista();
   }
	
}

void CWiz3Dlg::PopulezArbore()
{
   m_treeDirectoare.DeleteAllItems();

   HTREEITEM  hLitere[27];

   for (int nChar='A';nChar<='Z'; nChar++)
      hLitere[nChar-'A']=m_treeDirectoare.InsertItem((TCHAR*)&nChar);

   hLitere[26]=m_treeDirectoare.InsertItem("+");

   HANDLE hGasesc;
   WIN32_FIND_DATA dataGasesc;
   BOOL bMaiSunt = TRUE;
   CString strFisier;

   hGasesc=FindFirstFile(m_strSelect+"\\*.*", &dataGasesc);

   while (hGasesc != INVALID_HANDLE_VALUE && bMaiSunt)
   {
	  if(dataGasesc.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
      {	
         int nChar=dataGasesc.cFileName[0];
         if (islower(nChar)) nChar -= 32;
         if (isalpha(nChar)) nChar -= 'A';
         else nChar=26;
		 if (strcmp(dataGasesc.cFileName,"."))
	      if (strcmp(dataGasesc.cFileName,".."))
            m_treeDirectoare.InsertItem(dataGasesc.cFileName,hLitere[nChar]);
      }
      bMaiSunt=FindNextFile(hGasesc, &dataGasesc);
   }
   FindClose(hGasesc);
}

void CWiz3Dlg::PopulezLista()
{
   m_lbFisiere.ResetContent();

   HANDLE hGasesc;
   WIN32_FIND_DATA dataGasesc;
   BOOL bMaiSunt = TRUE;

   hGasesc=FindFirstFile(m_strSelect+"\\*.*", &dataGasesc);
   while (hGasesc != INVALID_HANDLE_VALUE && bMaiSunt)
   {
      if(dataGasesc.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE)
	     m_lbFisiere.AddString(dataGasesc.cFileName);
       bMaiSunt=FindNextFile(hGasesc, &dataGasesc);
   }
   FindClose(hGasesc);

}



void CWiz3Dlg::OnSelchangedArboreDirectoare(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
	HTREEITEM it=pNMTreeView->itemNew.hItem;
	CString dir;
	m_strSelect=m_strCopie;
	dir="\\"+m_treeDirectoare.GetItemText(it);
	if (pNMTreeView->action==TVC_BYMOUSE) 
	{
		m_strSelect+=dir;
		PopulezLista();
	}
	*pResult = 0;
}

void CWiz3Dlg::OnSelchangeListaFisiere() 
{
	// TODO: Add your control notification handler code here
   int nNrSelectate=m_lbFisiere.GetSelCount();
   m_strLista.RemoveAll();
   if (nNrSelectate)	
   {
       CString strSelectate;

	   LPINT pItemi=new int[nNrSelectate];
       m_lbFisiere.GetSelItems(nNrSelectate, pItemi);

       for(int i=0;i<nNrSelectate;i++) 
	   {
           m_lbFisiere.GetText(pItemi[i], strSelectate);
           m_strLista.AddTail(strSelectate);
	   }
  }
   PopulezControlLista();
	
}

void CWiz3Dlg::PopulezControlLista()
{
   m_lcDetalii.DeleteAllItems();
   POSITION Pos;

   int nItemi;
   CString strSelectate;
   CString strGasite;

   HANDLE hGasesc;
   WIN32_FIND_DATA dataGasesc;

   for (Pos=m_strLista.GetHeadPosition(); Pos != NULL;) 
   {
		strSelectate=m_strLista.GetAt(Pos);
		nItemi=m_lcDetalii.InsertItem(0, strSelectate);
		strGasite=m_strSelect+"\\"+strSelectate;
		hGasesc=FindFirstFile(strGasite, &dataGasesc);

		double nDimensiuneFisier=0;

		if (dataGasesc.dwFileAttributes)
			nDimensiuneFisier = (double)(dataGasesc.nFileSizeHigh*MAXWORD+dataGasesc.nFileSizeLow)/1024;
        strSelectate.Format("%6.3f", nDimensiuneFisier);
        m_lcDetalii.SetItemText(nItemi,1,FormezAtribute(dataGasesc.dwFileAttributes));
		m_lcDetalii.SetItemText(nItemi,2,strSelectate);
		FindClose(hGasesc);
        m_strLista.GetNext(Pos);
   }
}

CString CWiz3Dlg::FormezAtribute(DWORD Atr)
{

	CString temp;
	if (Atr&1) temp+="RO ";
	if (Atr&2) temp+="Hy ";
	if (Atr&4) temp+="Sy ";
	if (Atr&32) temp+="Arc ";
	if (Atr&256) temp+="Tmp";
	return temp;
}
