// SetAccessContentAndRecursive.cpp : implementation file
//

#include "stdafx.h"
#include "resource.h"
#include <apl_gui.h>
#include "SetAccessContentAndRecursive.h"

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


bool ShowDlgAccessProperties(CaplAPI* m_api,CString titul,CString &pattern,
							 LPCTSTR name0,bool *val0,
							 LPCTSTR name1,bool *val1,LPCTSTR name2,bool *val2,
							 LPCTSTR name3,bool *val3,LPCTSTR name4,bool *val4,
							 LPCTSTR name5,bool *val5,LPCTSTR name6,bool *val6,
							 LPCTSTR name7,bool *val7,LPCTSTR name8,bool *val8,
							 LPCTSTR name9,bool *val9,LPCTSTR name10,bool *val10,
							 LPCTSTR name11,bool *val11,LPCTSTR name12,bool *val12,
							 LPCTSTR name13,bool *val13,LPCTSTR name14,bool *val14)
{
	if(m_api==0)return false;

	CaplSetResourceHandle setres(module_inst);

	CSetAccessContentAndRecursive dlg;
	dlg.m_api=m_api;
	dlg.m_titul=titul;

	dlg.m_name[0]=(val0!=0 && name0!=0)?name0:_T("");
	dlg.m_name[1]=(val1!=0 && name1!=0)?name1:_T("");
	dlg.m_name[2]=(val2!=0 && name2!=0)?name2:_T("");
	dlg.m_name[3]=(val3!=0 && name3!=0)?name3:_T("");
	dlg.m_name[4]=(val4!=0 && name4!=0)?name4:_T("");
	dlg.m_name[5]=(val5!=0 && name5!=0)?name5:_T("");
	dlg.m_name[6]=(val6!=0 && name6!=0)?name6:_T("");
	dlg.m_name[7]=(val7!=0 && name7!=0)?name7:_T("");
	dlg.m_name[8]=(val8!=0 && name8!=0)?name8:_T("");
	dlg.m_name[9]=(val9!=0 && name9!=0)?name9:_T("");
	dlg.m_name[10]=(val10!=0 && name10!=0)?name10:_T("");
	dlg.m_name[11]=(val11!=0 && name11!=0)?name11:_T("");
	dlg.m_name[12]=(val12!=0 && name12!=0)?name12:_T("");
	dlg.m_name[13]=(val13!=0 && name13!=0)?name13:_T("");
	dlg.m_name[14]=(val14!=0 && name14!=0)?name14:_T("");
	dlg.m_ValChk[0]=(val0==0)?FALSE:(*val0?TRUE:FALSE);
	dlg.m_ValChk[1]=(val1==0)?FALSE:(*val1?TRUE:FALSE);
	dlg.m_ValChk[2]=(val2==0)?FALSE:(*val2?TRUE:FALSE);
	dlg.m_ValChk[3]=(val3==0)?FALSE:(*val3?TRUE:FALSE);
	dlg.m_ValChk[4]=(val4==0)?FALSE:(*val4?TRUE:FALSE);
	dlg.m_ValChk[5]=(val5==0)?FALSE:(*val5?TRUE:FALSE);
	dlg.m_ValChk[6]=(val6==0)?FALSE:(*val6?TRUE:FALSE);
	dlg.m_ValChk[7]=(val7==0)?FALSE:(*val7?TRUE:FALSE);
	dlg.m_ValChk[8]=(val8==0)?FALSE:(*val8?TRUE:FALSE);
	dlg.m_ValChk[9]=(val9==0)?FALSE:(*val9?TRUE:FALSE);
	dlg.m_ValChk[10]=(val10==0)?FALSE:(*val10?TRUE:FALSE);
	dlg.m_ValChk[11]=(val11==0)?FALSE:(*val11?TRUE:FALSE);
	dlg.m_ValChk[12]=(val12==0)?FALSE:(*val12?TRUE:FALSE);
	dlg.m_ValChk[13]=(val13==0)?FALSE:(*val13?TRUE:FALSE);
	dlg.m_ValChk[14]=(val14==0)?FALSE:(*val14?TRUE:FALSE);

	if(dlg.DoModal()==IDCANCEL){
		//Обратная перестановка по завершении 
		return false;
	}
	if(val0!=0)*val0=(dlg.m_ValChk[0]==TRUE);
	if(val1!=0)*val1=(dlg.m_ValChk[1]==TRUE);
	if(val2!=0)*val2=(dlg.m_ValChk[2]==TRUE);
	if(val3!=0)*val3=(dlg.m_ValChk[3]==TRUE);
	if(val4!=0)*val4=(dlg.m_ValChk[4]==TRUE);
	if(val5!=0)*val5=(dlg.m_ValChk[5]==TRUE);
	if(val6!=0)*val6=(dlg.m_ValChk[6]==TRUE);
	if(val7!=0)*val7=(dlg.m_ValChk[7]==TRUE);
	if(val8!=0)*val8=(dlg.m_ValChk[8]==TRUE);
	if(val9!=0)*val9=(dlg.m_ValChk[9]==TRUE);
	if(val10!=0)*val10=(dlg.m_ValChk[10]==TRUE);
	if(val11!=0)*val11=(dlg.m_ValChk[11]==TRUE);
	if(val12!=0)*val12=(dlg.m_ValChk[12]==TRUE);
	if(val13!=0)*val13=(dlg.m_ValChk[13]==TRUE);
	if(val14!=0)*val14=(dlg.m_ValChk[14]==TRUE);
	pattern=dlg.m_pattern;
	return true;
}

/////////////////////////////////////////////////////////////////////////////
// CSetAccessContentAndRecursive dialog


CSetAccessContentAndRecursive::CSetAccessContentAndRecursive(CWnd* pParent /*=NULL*/)
	: CaplTranslateDialog(CSetAccessContentAndRecursive::IDD, pParent)
{
	int i;
	//{{AFX_DATA_INIT(CSetAccessContentAndRecursive)
	for(i=0;i<NUM_CHECKBOXES;i++){
		m_ValChk[i] = FALSE;
	}
	//}}AFX_DATA_INIT
//	m_mode=_T("PrdInst");
	m_titul=_T("");
	m_pattern=_T("");
	m_hand_access=true;
	m_api=0;

	for(i=0;i<NUM_CHECKBOXES;i++){
		m_name[i]=_T("");
	}
}


void CSetAccessContentAndRecursive::DoDataExchange(CDataExchange* pDX)
{
	CaplTranslateDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSetAccessContentAndRecursive)
	DDX_Control(pDX, IDC_GROUP_TYPE, m_GropupType);
	DDX_Control(pDX, IDC_GROUP_PARAMS, m_GropupParams);
	DDX_Control(pDX, IDC_RADIO_ACCESS_PATTERN, m_CtrlRAPattern);
	DDX_Control(pDX, IDC_RADIO_ACCESS_HAND, m_CtrlRAHand);
	DDX_Control(pDX, IDC_COMBO_PATTERN, m_CtrlPatterns);
	DDX_Control(pDX, IDC_CHECK_0, m_CtrlChk[0]);
	DDX_Control(pDX, IDC_CHECK_1, m_CtrlChk[1]);
	DDX_Control(pDX, IDC_CHECK_2, m_CtrlChk[2]);
	DDX_Control(pDX, IDC_CHECK_3, m_CtrlChk[3]);
	DDX_Control(pDX, IDC_CHECK_4, m_CtrlChk[4]);
	DDX_Control(pDX, IDC_CHECK_5, m_CtrlChk[5]);
	DDX_Control(pDX, IDC_CHECK_6, m_CtrlChk[6]);
	DDX_Control(pDX, IDC_CHECK_7, m_CtrlChk[7]);
	DDX_Control(pDX, IDC_CHECK_8, m_CtrlChk[8]);
	DDX_Control(pDX, IDC_CHECK_9, m_CtrlChk[9]);
	DDX_Control(pDX, IDC_CHECK_10, m_CtrlChk[10]);
	DDX_Control(pDX, IDC_CHECK_11, m_CtrlChk[11]);
	DDX_Control(pDX, IDC_CHECK_12, m_CtrlChk[12]);
	DDX_Control(pDX, IDC_CHECK_13, m_CtrlChk[13]);
	DDX_Control(pDX, IDC_CHECK_14, m_CtrlChk[14]);

	DDX_Control(pDX, IDOK, m_CtrlOk);
	DDX_Control(pDX, IDCANCEL, m_CtrlCancel);
	DDX_Check(pDX, IDC_CHECK_0, m_ValChk[0]);
	DDX_Check(pDX, IDC_CHECK_1, m_ValChk[1]);
	DDX_Check(pDX, IDC_CHECK_2, m_ValChk[2]);
	DDX_Check(pDX, IDC_CHECK_3, m_ValChk[3]);
	DDX_Check(pDX, IDC_CHECK_4, m_ValChk[4]);
	DDX_Check(pDX, IDC_CHECK_5, m_ValChk[5]);
	DDX_Check(pDX, IDC_CHECK_6, m_ValChk[6]);
	DDX_Check(pDX, IDC_CHECK_7, m_ValChk[7]);
	DDX_Check(pDX, IDC_CHECK_8, m_ValChk[8]);
	DDX_Check(pDX, IDC_CHECK_9, m_ValChk[9]);
	DDX_Check(pDX, IDC_CHECK_10, m_ValChk[10]);
	DDX_Check(pDX, IDC_CHECK_11, m_ValChk[11]);
	DDX_Check(pDX, IDC_CHECK_12, m_ValChk[12]);
	DDX_Check(pDX, IDC_CHECK_13, m_ValChk[13]);
	DDX_Check(pDX, IDC_CHECK_14, m_ValChk[14]);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSetAccessContentAndRecursive, CaplTranslateDialog)
	//{{AFX_MSG_MAP(CSetAccessContentAndRecursive)
	ON_BN_CLICKED(IDC_RADIO_ACCESS_HAND, OnRadioAccess)
	ON_BN_CLICKED(IDC_RADIO_ACCESS_PATTERN, OnRadioAccess)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSetAccessContentAndRecursive message handlers
void CSetAccessContentAndRecursive::HideUpperCheck(int start)
{
	int i;
	for(i=start;i<NUM_CHECKBOXES;i++){
		m_CtrlChk[i].ShowWindow(SW_HIDE);
	}
}

BOOL CSetAccessContentAndRecursive::OnInitDialog() 
{
	CaplTranslateDialog::OnInitDialog();
	CPoint point;
	CRect rect;
	
	// TODO: Add extra initialization here
	if(m_titul!=_T("")){
		SetWindowText(m_titul);
	}
	m_CtrlRAHand.SetCheck(1);
	m_CtrlRAPattern.SetCheck(0);
	aplExtent access_patterns;
	CaplInstance* inst_pat;
	int i,pos;
	int num_controls=0;
	CString buf;

	if(m_api!=0)
	{
		OnRadioAccess();
		m_api->m_data.GetEntityExtent(m_api->m_appr_mgr.e_apl_acc_pat,access_patterns);
		if(access_patterns.GetSize()==0)
		{
			m_CtrlRAPattern.EnableWindow(FALSE);
			m_CtrlPatterns.EnableWindow(FALSE);
			//m_CtrlRAPattern.SetWindowText(APL_T("Назначение уровня доступа по шаблону невозможно-Шаблоны не заданы!"));
			m_CtrlRAPattern.SetWindowText( APL_T("Назначение уровня доступа по шаблону - Шаблоны не заданы!"));
		}
		else
		{
			for(i=0;i<access_patterns.GetSize();i++)
			{
				inst_pat=access_patterns.GetAt(i);
				if(inst_pat==0)continue;
				m_api->m_data.GetAttr(inst_pat,m_api->m_appr_mgr.a_apl_acc_pat_name,buf);
				pos=m_CtrlPatterns.InsertString(i,buf);
				m_CtrlPatterns.SetItemData(pos,(DWORD)inst_pat);
			}
			m_CtrlPatterns.SetCurSel(0);
		}
	}
	else
	{
		m_CtrlRAPattern.EnableWindow(FALSE);
		m_CtrlPatterns.EnableWindow(FALSE);
	}
	num_controls=0;
	// устанавливаем заголовки чекбоксов
	for(i=0;i<NUM_CHECKBOXES;i++)
	{
		if(m_name[i].IsEmpty())
		{
			num_controls=i;
			HideUpperCheck(i);
			break;
		}
		else
		{
			num_controls=i+1;
			m_CtrlChk[i].SetWindowText(m_name[i]);
		}
	}

	// передвигаем кнопки Ok и Отмена и уменьшаем размер диалога
	point=CPoint(0,19*(NUM_CHECKBOXES-num_controls)-10);
	if(num_controls==0)
	{
		point.y+=20;
	}
	m_CtrlOk.GetWindowRect(rect);ScreenToClient(rect);rect-=point;m_CtrlOk.MoveWindow(rect);
	m_CtrlCancel.GetWindowRect(rect);ScreenToClient(rect);rect-=point;m_CtrlCancel.MoveWindow(rect);
	this->GetWindowRect(rect);rect.bottom-=point.y;this->MoveWindow(rect);
	if(num_controls==0)
	{
		m_GropupParams.ShowWindow(SW_HIDE);
	}
	else
	{
		m_GropupParams.GetWindowRect(rect);ScreenToClient(rect);rect.bottom-=point.y;m_GropupParams.MoveWindow(rect);
	}
	OnRadioAccess();
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CSetAccessContentAndRecursive::OnRadioAccess() 
{
	// TODO: Add your control notification handler code here
	if(m_CtrlPatterns.GetCount()==0)return;
	m_hand_access=!m_hand_access;
	m_CtrlRAPattern.SetCheck(m_hand_access?0:1);
	m_CtrlRAHand.SetCheck(m_hand_access?1:0);
	if(m_hand_access){
		m_CtrlPatterns.EnableWindow(FALSE);
	}else{
		m_CtrlPatterns.EnableWindow(TRUE);
	}
}


void CSetAccessContentAndRecursive::OnOK() 
{
	// TODO: Add extra validation here
	if(m_hand_access || m_api==0){
		m_pattern=_T("");
	}else{
		int cur;
		CaplInstance* inst_pat;
		cur=m_CtrlPatterns.GetCurSel();
		inst_pat=(CaplInstance*)m_CtrlPatterns.GetItemData(cur);
		m_api->m_data.GetAttr(inst_pat,m_api->m_appr_mgr.a_apl_acc_pat_pattern,m_pattern);
	}
	CaplTranslateDialog::OnOK();
}
