// GistogramPropDlg.cpp : implementation file
//

#include "stdafx.h"
#include "GistogramPropDlg.h"
#include "SelectDSColumnDlg.h"
#include "PropertyDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CGistogramPropDlg dialog
enum {aplColumnHeaderCol, aplColumnShow, aplColumnTitle, aplColumnWidth, aplColumnColor};

CGistogramPropDlg::CGistogramPropDlg(CWnd* pParent /*=NULL*/)
	: CPageDialog(CGistogramPropDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CGistogramPropDlg)
	m_sName = _T("");
	m_iAngel = 0;
	m_iVolume = 0;
	//}}AFX_DATA_INIT
	m_DefcolorInd = 0;
	m_DefColors[0]=RGB(220, 0, 0);
	m_DefColors[1]=RGB(36, 0, 220);
	m_DefColors[2]=RGB(0, 220, 0);
	m_DefColors[3]=RGB(220, 0, 220);
	m_DefColors[4]=RGB(255, 70, 70);
	m_DefColors[5]=RGB(150, 150, 150);
	m_DefColors[6]=RGB(0, 0, 0);
	
	m_ShowModes.Add(APL_T("Значение поля"));
	m_ShowModes.Add(APL_T("Количество строк в группе"));
	m_ShowModes.Add(APL_T("Сумма в группе"));
	m_ShowModes.Add(APL_T("Среднее арифметическое в группе"));

	m_pGistogram = NULL;
}


void CGistogramPropDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGistogramPropDlg)
	DDX_Control(pDX, IDC_GISTORRAM_COLUMN, m_column_btn);
	DDX_Control(pDX, IDC_GISTOGRAM_CYCLE, m_cycle_btn);
	DDX_Control(pDX, IDC_GISTOGRAM_TIMELINE, m_timeline_btn);
	DDX_Control(pDX, IDC_SPIN_VOLUME, m_volume_spin);
	DDX_Control(pDX, IDC_SPIN_ANGEL, m_angel_spin);
	DDX_Control(pDX, IDC_SELECT_DS_COLUMN, m_select_ds_btn);
	DDX_Control(pDX, IDC_ADD_COLUMN, m_add_btn);
	DDX_Control(pDX, IDC_DELETE, m_delete_btn);
	DDX_Control(pDX, IDC_DS_COLUMNS, m_DSColumnCtrl);
	DDX_Control(pDX, IDC_LINES_LIST, m_LinesListCtrl);
	DDX_Text(pDX, IDC_NAME, m_sName);
	DDX_Text(pDX, IDC_ANGEL, m_iAngel);
	DDV_MinMaxInt(pDX, m_iAngel, -90, 90);
	DDX_Text(pDX, IDC_VOLUME, m_iVolume);
	DDV_MinMaxInt(pDX, m_iVolume, 0, 100);
	//}}AFX_DATA_MAP
	DDX_Control(pDX, IDC_SORT_TYPE, m_sort_type_ctrl);
}


BEGIN_MESSAGE_MAP(CGistogramPropDlg, CDialog)
	//{{AFX_MSG_MAP(CGistogramPropDlg)
	ON_BN_CLICKED(IDC_ADD_COLUMN, OnAddColumn)
	ON_BN_CLICKED(IDC_DELETE, OnDelete)
	ON_STARTEDIT(IDC_LINES_LIST, OnLinesListCtrlStartEdir)
	ON_BN_CLICKED(IDC_SELECT_DS_COLUMN, OnSelectDsColumn)
	ON_CBN_SELCHANGE(IDC_DS_COLUMNS, OnSelchangeDsColumns)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGistogramPropDlg message handlers

BOOL CGistogramPropDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	if(m_pGistogram==NULL)
	{
		EndDialog(0);
		return FALSE;
	}

	m_angel_spin.SetRange(-90, 90);
	m_volume_spin.SetRange(0, 100);
	
	m_select_ds_btn.SetIcon(aplGetButtonIco(APL_24_SELECT));
	m_select_ds_btn.SetWindowText(_T(""));
	m_add_btn.SetIcon(aplGetButtonIco(APL_24_PLUS));
	m_add_btn.SetWindowText(_T(""));
	m_delete_btn.SetIcon(aplGetButtonIco(APL_24_DELETE));
	m_delete_btn.SetWindowText(_T(""));
	
	//Заполняем поля ИД
	int i;
	int ind;
	CString buf;
	
	m_DSColumnCtrl.SetItemData(m_DSColumnCtrl.AddString(_T("")), 0);
	m_DSColumnCtrl.SetCurSel(0);
	for(i=0; i<m_pGistogram->m_ReportMgr->m_header->GetSize(); i++)
		m_DSColumnCtrl.SetItemData(m_DSColumnCtrl.AddString(m_pGistogram->m_ReportMgr->m_header->GetColumn(i)->GetName()), (DWORD)m_pGistogram->m_ReportMgr->m_header->GetColumn(i));
	for(i=0; i<m_pGistogram->m_ReportMgr->m_base_query->GetSize(); i++)
		m_DSColumnCtrl.SetItemData(m_DSColumnCtrl.AddString(m_pGistogram->m_ReportMgr->m_base_query->GetColumn(i)->GetName()), (DWORD)m_pGistogram->m_ReportMgr->m_base_query->GetColumn(i));
	
	m_sName = m_pGistogram->m_name;
	m_pGroupCol = m_pGistogram->m_pGroupColumn;
	if(m_pGroupCol)
		SelectColumn(m_pGroupCol);

	if(m_pGistogram->m_ViewType==APL_GISTOGRAM_VIEW_TYPE_CYCLE)
		m_cycle_btn.SetCheck(TRUE);
	else if(m_pGistogram->m_ViewType==APL_GISTOGRAM_VIEW_TYPE_TIMELINE)
		m_timeline_btn.SetCheck(TRUE);
	else
		m_column_btn.SetCheck(TRUE);

	CRect cl_rect;
	m_LinesListCtrl.GetClientRect(&cl_rect);
	m_LinesListCtrl.InsertColumn(aplColumnHeaderCol, APL_T("Поле"), LVCFMT_LEFT, (int)(cl_rect.Width()*0.25));
	m_LinesListCtrl.InsertColumn(aplColumnShow, APL_T("Отображать"), LVCFMT_LEFT, (int)(cl_rect.Width()*0.2));
	m_LinesListCtrl.InsertColumn(aplColumnTitle, APL_T("Наименование"), LVCFMT_LEFT, (int)(cl_rect.Width()*0.3));
	m_LinesListCtrl.InsertColumn(aplColumnWidth, APL_T("Толщина линии"), LVCFMT_LEFT, (int)(cl_rect.Width()*0.12));
	m_LinesListCtrl.InsertColumn(aplColumnColor, APL_T("Цвет"), LVCFMT_LEFT, (int)(cl_rect.Width()*0.12));
	m_LinesListCtrl.ModifyStyle(0, LVS_OWNERDRAWFIXED);
	
	for(i=0; i<m_pGistogram->m_Elements.GetSize(); i++)
	{
		ind =	m_LinesListCtrl.InsertItem(m_LinesListCtrl.GetItemCount(), m_pGistogram->m_Elements[i]->m_pDSColumn->GetName());
		switch(m_pGistogram->m_Elements[i]->m_show_mode)
		{
		case aplShowValue: buf = APL_T("Значение поля"); break;
		case aplShowSumm: buf = APL_T("Сумма значений в группе"); break;
		case aplShowCount: buf = APL_T("Количество строк в группе"); break;
		case aplShowAverage: buf = APL_T("Среднее арифметическое значений в группе"); break;
		}
		m_LinesListCtrl.SetItemText(ind, aplColumnShow, buf);
		m_LinesListCtrl.SetItemText(ind, aplColumnTitle, m_pGistogram->m_Elements[i]->m_name);
		buf.Format(_T("%g"), (double)m_pGistogram->m_Elements[i]->m_line_width/10);
		m_LinesListCtrl.SetItemText(ind, aplColumnWidth, buf);
		m_LinesListCtrl.SetItemBkColor(ind, aplColumnColor, m_pGistogram->m_Elements[i]->m_color);
		m_LinesListCtrl.SetItemData(ind, (DWORD)m_pGistogram->m_Elements[i]);
		
		if(m_pGroupCol!=NULL)
			m_LinesListCtrl.SetItemEditType(ind, aplColumnShow, aplComboCtrl, &m_ShowModes);
		m_LinesListCtrl.SetItemEditType(ind, aplColumnTitle, aplTextCtrl);
		m_LinesListCtrl.SetItemEditType(ind, aplColumnColor, aplUser);
		m_LinesListCtrl.SetItemEditType(ind, aplColumnWidth, aplRealCtrl);
	}

	m_iAngel = m_pGistogram->m_Angel;
	m_iVolume = m_pGistogram->m_Volume/10;

	m_sort_type_ctrl.InsertString(aplAZ, APL_T("По возрастанию"));
	m_sort_type_ctrl.InsertString(aplZA, APL_T("По убыванию"));
	m_sort_type_ctrl.InsertString(aplSortNone, APL_T("Отсутствует"));
	m_sort_type_ctrl.SetCurSel(m_pGistogram->m_SortDirection);

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

bool CGistogramPropDlg::UpdateDlg()
{
	if(!UpdateData()) return false;
	int cur = m_DSColumnCtrl.GetCurSel();
	m_pGistogram->m_pGroupColumn = cur>-1?(CHeaderColumn*)m_DSColumnCtrl.GetItemData(cur):NULL;

	//Удаляем старые
	int i;
	CDiagramItem *pItem;
	CString buf;

	for(i=0; i<m_deleted_items.GetSize(); i++)
	{
		m_deleted_items[i]->Delete();
		m_pGistogram->m_Elements.Remove(m_pGistogram->m_Elements.Find(m_deleted_items[i]));
	}
	m_deleted_items.Clear();
	//добавляем/обновляем новые
	for(i=0; i<m_added_items.GetSize(); i++)
		m_pGistogram->m_Elements.Add(m_added_items[i]);
	m_added_items.Clear();

	//обновляем все
	for(i=0; i<m_LinesListCtrl.GetItemCount(); i++)
	{
		pItem = (CDiagramItem*)m_LinesListCtrl.GetItemData(i);
		pItem->m_line_width = (int)(__atof(m_LinesListCtrl.GetItemText(i, aplColumnWidth))*10);
		pItem->m_name = m_LinesListCtrl.GetItemText(i, aplColumnTitle);
		buf = m_LinesListCtrl.GetItemText(i, aplColumnShow);
		if(buf==APL_T("Значение поля"))
			pItem->m_show_mode=aplShowValue;
		else if(buf==APL_T("Количество строк в группе"))
			pItem->m_show_mode=aplShowCount;
		else if(buf==APL_T("Сумма в группе"))
			pItem->m_show_mode=aplShowSumm;
		else if(buf==APL_T("Среднее арифметическое в группе"))
			pItem->m_show_mode=aplShowAverage;
	}
	if(m_cycle_btn.GetCheck())
		m_pGistogram->m_ViewType = APL_GISTOGRAM_VIEW_TYPE_CYCLE;
	else if(m_timeline_btn.GetCheck())
		m_pGistogram->m_ViewType = APL_GISTOGRAM_VIEW_TYPE_TIMELINE;
	else
		m_pGistogram->m_ViewType = APL_GISTOGRAM_VIEW_TYPE_COLUMN;
	m_pGistogram->m_Angel = m_iAngel;
	m_pGistogram->m_Volume = m_iVolume*10;
	m_pGistogram->m_SortDirection = m_sort_type_ctrl.GetCurSel();

	return true;
}

void CGistogramPropDlg::OnAddColumn() 
{
	UpdateData();
	CSelectDSColumnDlg dlg;
	dlg.m_ReportMgr = m_pGistogram->m_ReportMgr;
	dlg.m_bShowEmptyColumn = false;
	if(dlg.DoModal()==IDOK)
	{
		int ind;
		CString buf;
		CHeaderColumn* pCol = dlg.m_selected;
		if(pCol)
		{
			CDiagramItem *pItem = new CDiagramItem(m_pGistogram->m_ReportMgr);
			buf.Format(_T("%g"), (double)pItem->m_line_width/10);
			ind =	m_LinesListCtrl.InsertItem(m_LinesListCtrl.GetItemCount(), pCol->GetName());
					m_LinesListCtrl.SetItemText(ind, aplColumnShow, APL_T("Значение поля"));
					m_LinesListCtrl.SetItemText(ind, aplColumnTitle, pCol->GetName());
					m_LinesListCtrl.SetItemEditType(ind, aplColumnTitle, aplTextCtrl);
					m_LinesListCtrl.SetItemText(ind, aplColumnWidth, buf);
					m_LinesListCtrl.SetItemEditType(ind, aplColumnWidth, aplRealCtrl);
					m_LinesListCtrl.SetItemBkColor(ind, aplColumnColor, m_DefColors[m_DefcolorInd]);
					m_LinesListCtrl.SetItemEditType(ind, aplColumnColor, aplUser);

					pItem->m_color = m_DefColors[m_DefcolorInd++];
					if(m_DefcolorInd>6) m_DefcolorInd = 0;
					pItem->m_name = pCol->GetName();
					pItem->m_pDSColumn = pCol;
					pItem->m_show_mode = aplShowValue;
					m_added_items.Add(pItem);
					m_LinesListCtrl.SetItemData(ind, (DWORD)pItem);

					OnSelchangeDsColumns();
		}
	}
	UpdateData(FALSE);
}

void CGistogramPropDlg::OnDelete() 
{
	int cur = m_LinesListCtrl.GetNextItem(-1, LVIS_SELECTED);
	if(cur<0) return;

	CDiagramItem *pItem = (CDiagramItem*)m_LinesListCtrl.GetItemData(cur);
	if(m_added_items.Find(pItem)>-1)
	{
		m_added_items.Remove(m_added_items.Find(pItem));
		delete pItem;
	}
	else
		m_deleted_items.Add(pItem);
	m_LinesListCtrl.DeleteItem(cur);
}

void CGistogramPropDlg::OnLinesListCtrlStartEdir(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMLISTVIEW lpNMListView = (LPNMLISTVIEW)pNMHDR;

	*pResult = 0;

	if(lpNMListView->iSubItem==aplColumnColor)
	{
		CDiagramItem *pItem = (CDiagramItem*)m_LinesListCtrl.GetItemData(lpNMListView->iItem);
		if(pItem==NULL) return;
		CColorDialog dlg(pItem->m_color);
		dlg.m_cc.Flags |= CC_FULLOPEN | CC_RGBINIT;
		dlg.m_cc.lpCustColors = new COLORREF[16];
		dlg.m_cc.lpCustColors[0] = pItem->m_color;
		dlg.m_cc.lpCustColors[1]=RGB(220, 0, 0);
		dlg.m_cc.lpCustColors[2]=RGB(36, 0, 220);
		dlg.m_cc.lpCustColors[3]=RGB(0, 220, 0);
		dlg.m_cc.lpCustColors[4]=RGB(220, 0, 220);
		dlg.m_cc.lpCustColors[5]=RGB(255, 70, 70);
		dlg.m_cc.lpCustColors[6]=RGB(150, 150, 150);
		dlg.m_cc.lpCustColors[7]=RGB(0, 0, 0);
		dlg.m_cc.lpCustColors[8]=RGB(255, 255, 255);
		dlg.m_cc.lpCustColors[9]=RGB(255, 255, 255);
		dlg.m_cc.lpCustColors[10]=RGB(255, 255, 255);
		dlg.m_cc.lpCustColors[11]=RGB(255, 255, 255);
		dlg.m_cc.lpCustColors[12]=RGB(255, 255, 255);
		dlg.m_cc.lpCustColors[13]=RGB(255, 255, 255);
		dlg.m_cc.lpCustColors[14]=RGB(255, 255, 255);
		dlg.m_cc.lpCustColors[15]=RGB(255, 255, 255);

		if(dlg.DoModal())
		{
			pItem->m_color = dlg.m_cc.rgbResult;
			m_LinesListCtrl.SetItemBkColor(lpNMListView->iItem, lpNMListView->iSubItem, dlg.m_cc.rgbResult);
		}
		delete dlg.m_cc.lpCustColors;
	}
}

void CGistogramPropDlg::OnSelectDsColumn() 
{
	CSelectDSColumnDlg dlg;
	dlg.m_ReportMgr = m_pGistogram->m_ReportMgr;
	if(dlg.DoModal()==IDOK)
		SelectColumn(dlg.m_selected);
}

void CGistogramPropDlg::SelectColumn(CHeaderColumn *pColumn)
{
	int i;
	for(i=0; i<m_DSColumnCtrl.GetCount(); i++)
	{
		if(m_DSColumnCtrl.GetItemData(i)==(DWORD)pColumn)
		{
			m_DSColumnCtrl.SetCurSel(i);
			return;
		}
	}
	m_DSColumnCtrl.SetCurSel(0);
}

BOOL CGistogramPropDlg::DestroyWindow() 
{
	int i;
	for(i=0; i<m_added_items.GetSize(); i++)
		delete m_added_items[i];
	
	return CPageDialog::DestroyWindow();
}


void CGistogramPropDlg::OnCancel() 
{
	if(m_LinesListCtrl.EndEdit(false)) return;

	CWnd* wnd = GetParent();
	ASSERT(wnd);
	wnd = wnd->GetParent();
	CPropertyDlg* dlg = (CPropertyDlg*)wnd;
	dlg->OnCancel();
}

void CGistogramPropDlg::OnOK() 
{
	if(m_LinesListCtrl.EndEdit()) return;
	if(!UpdateData()) return;

	CWnd* wnd = GetParent();
	ASSERT(wnd);
	wnd = wnd->GetParent();
	CPropertyDlg* dlg = (CPropertyDlg*)wnd;
	dlg->OnOK();
}

void CGistogramPropDlg::OnSelchangeDsColumns() 
{
	int cur = m_DSColumnCtrl.GetCurSel();
	int i;
	if(cur>-1)
	{
		if(m_DSColumnCtrl.GetItemData(cur)>0)
		{
			for(i=0; i<m_LinesListCtrl.GetItemCount(); i++)
				m_LinesListCtrl.SetItemEditType(i, aplColumnShow, aplComboCtrl, &m_ShowModes);
			return;
		}
	}
	for(i=0; i<m_LinesListCtrl.GetItemCount(); i++)
	{
		m_LinesListCtrl.SetItemEditType(i, aplColumnShow, aplNone);
		m_LinesListCtrl.SetItemText(i, aplColumnShow, APL_T("Значение поля"));
	}
}

bool CGistogramPropDlg::TestUpdate()
{
	if(!UpdateData()) return false;

	return true;
}
