// AssignationDlg.cpp : implementation file
//

#include "stdafx.h"
#include "skoko.h"
#include "AssignationDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAssignationDlg dialog


CAssignationDlg::CAssignationDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAssignationDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAssignationDlg)
	m_recursive = TRUE;
	m_curOwner = _T("");
	m_startDir = _T("");
	m_newOwner = _T("");
	m_anyOwner = FALSE;
	m_selectFiles = 2;
	//}}AFX_DATA_INIT
}


void CAssignationDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAssignationDlg)
	DDX_Check(pDX, IDC_CHECK_RECURSIVE, m_recursive);
	DDX_Text(pDX, IDC_EDIT_CURRENT_OWNER, m_curOwner);
	DDX_Text(pDX, IDC_EDIT_DIR, m_startDir);
	DDX_Text(pDX, IDC_EDIT_NEW_OWNER, m_newOwner);
	DDX_Check(pDX, IDC_CHECK_ANY_OWNER, m_anyOwner);
	DDX_Radio(pDX, IDC_RADIO_ALL_FILES, m_selectFiles);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CAssignationDlg, CDialog)
	//{{AFX_MSG_MAP(CAssignationDlg)
	ON_BN_CLICKED(IDC_BUTTON_SELECT_CUR_OWNER, OnButtonSelectCurOwner)
	ON_BN_CLICKED(IDC_BUTTON_SELECT_DIR, OnButtonSelectDir)
	ON_BN_CLICKED(IDC_BUTTON_SELECT_NEW_OWNER, OnButtonSelectNewOwner)
	ON_BN_CLICKED(IDC_CHECK_ANY_OWNER, OnCheckAnyOwner)
	ON_BN_CLICKED(IDC_RADIO_ALL_FILES, OnRadioSelectFiles)
	ON_BN_CLICKED(IDC_RADIO_FILES_IN_DIR, OnRadioSelectFiles)
	ON_BN_CLICKED(IDC_RADIO_SELECTED_FILES, OnRadioSelectFiles)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAssignationDlg message handlers

void CAssignationDlg::OnButtonSelectCurOwner() 
{
  m_userSelDlg.m_allowNoOwner = TRUE;
  if (IDOK == m_userSelDlg.DoModal())
  {
    UpdateData();
    m_curOwner = m_userSelDlg.m_sSelectedUser;
    UpdateData(FALSE);
  }
}

void CAssignationDlg::OnButtonSelectDir() 
{
	if (IDOK == m_dirListDlg.DoModal())
  {
    UpdateData();
    m_startDir = m_dirListDlg.SelectedDir();
    UpdateData(FALSE);
  }
}

void CAssignationDlg::OnButtonSelectNewOwner() 
{
  m_userSelDlg.m_allowNoOwner = FALSE;
  if (IDOK == m_userSelDlg.DoModal())
  {
    UpdateData();
    m_newOwner = m_userSelDlg.m_sSelectedUser;
    UpdateData(FALSE);
  }
}

void CAssignationDlg::OnCheckAnyOwner() 
{
  UpdateData();
  GetDlgItem(IDC_EDIT_CURRENT_OWNER)->EnableWindow(!m_anyOwner);
  GetDlgItem(IDC_BUTTON_SELECT_CUR_OWNER)->EnableWindow(!m_anyOwner);
}

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

  OnCheckAnyOwner();
  OnRadioSelectFiles();

  if (m_selectFiles == 0)
  {
    // Если изначально выбрано "все файлы", значит
    // выделенных файлов нет, поэтому соответствующая
    // кнопка недоступна:
    GetDlgItem(IDC_RADIO_SELECTED_FILES)->EnableWindow(FALSE);
  }
  else if (m_selectFiles == 2)
  {
    // Если изначально выбрано "Файлы в каталоге", значит
    // список пуст, поэтому соответствующие кнопки недоступны:
    GetDlgItem(IDC_RADIO_ALL_FILES)->EnableWindow(FALSE);
    GetDlgItem(IDC_RADIO_SELECTED_FILES)->EnableWindow(FALSE);
  }
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CAssignationDlg::OnRadioSelectFiles() 
{
	UpdateData();
  
  const bool enableDirSelection = m_selectFiles == 2;
  GetDlgItem(IDC_EDIT_DIR)->EnableWindow(enableDirSelection);
  GetDlgItem(IDC_CHECK_RECURSIVE)->EnableWindow(enableDirSelection);
  GetDlgItem(IDC_BUTTON_SELECT_DIR)->EnableWindow(enableDirSelection);
}
