// VD_GUIDlg.cpp : implementation file
//

#include "stdafx.h"
#include "VD_GUI.h"
#include "VD_GUIDlg.h"



#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CVD_GUIDlg dialog


CVD_GUIDlg::CVD_GUIDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CVD_GUIDlg::IDD, pParent), m_iRect(15,35,719,323)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_showWelcome = true;
	results.vehCount = 0;
	results.carCount = 0;
	results.trCount = 0;
	results.actCount = 0;
	for(int i=0;i<NUMBER_OF_LANES;i++)
		results.avgSpeed[i] = 0;
	for(int i=0;i<NUMBER_OF_LANES;i++)
		results.density[i] = 0;
	for(int i=0;i<NUMBER_OF_LANES;i++)
		results.flow[i] = 0;
	for(int i=0;i<NUMBER_OF_LANES;i++)
		results.LOS[i] = 'A';
	algs.carContours=false;
	algs.carRects=false;
	algs.roadContours=false;
	algs.roadSigns=false;
	algs.shadowMask=false;
	algs.path=false;
	algs.speed=false;
	algs.length=false;
	algs.lanes=false;
	algs.useLanes=false;
	algs.fixedLanes=0;
	cs.height = 0;
	cs.ver_angle = 0;
	cs.angle_range_x = 0;
	cs.angle_range_y = 0;
		
	GetSystemTime(&results.time);
}

void CVD_GUIDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_SCENE_HEIGHT, cs.height);
	DDX_Text(pDX, IDC_SCENE_ANGLE, cs.ver_angle);
	DDX_Text(pDX, IDC_SCENE_ARANGEY, cs.angle_range_y);
	DDX_Text(pDX, IDC_SCENE_ARANGEX, cs.angle_range_x);
	DDX_Check(pDX, IDC_CARCONTOURS, algs.carContours);
	DDX_Check(pDX, IDC_CARRECTS, algs.carRects);
	DDX_Check(pDX, IDC_ROADCONTOURS, algs.roadContours);
	DDX_Check(pDX, IDC_ROADSIGNS, algs.roadSigns);
	DDX_Check(pDX, IDC_SHADOWMASK, algs.shadowMask);
	DDX_Check(pDX, IDC_PATH, algs.path);
	DDX_Check(pDX, IDC_SPEED, algs.speed);
	DDX_Check(pDX, IDC_LENGTH, algs.length);
	DDX_Check(pDX, IDC_LANES, algs.lanes);
	DDX_Check(pDX, IDC_USELANES, algs.useLanes);
	DDX_Radio(pDX, IDC_FIXED1, algs.fixedLanes);
	DDX_Text(pDX, IDC_VEHCOUNT, results.vehCount);
	DDX_Text(pDX, IDC_CARSCOUNT, results.carCount);
	DDX_Text(pDX, IDC_TRCOUNT, results.trCount);
	DDX_Text(pDX, IDC_LANE1, results.avgSpeed[0]);
	DDX_Text(pDX, IDC_LANE2, results.avgSpeed[1]);
	DDX_Text(pDX, IDC_LANE3, results.avgSpeed[2]);
	DDX_Text(pDX, IDC_LANE4, results.avgSpeed[3]);
	DDX_Text(pDX, IDC_LANE5, results.avgSpeed[4]);
	DDX_Text(pDX, IDC_LANE6, results.avgSpeed[5]);
	DDX_Text(pDX, IDC_DENS1, results.density[0]);
	DDX_Text(pDX, IDC_DENS2, results.density[1]);
	DDX_Text(pDX, IDC_DENS3, results.density[2]);
	DDX_Text(pDX, IDC_DENS4, results.density[3]);
	DDX_Text(pDX, IDC_DENS5, results.density[4]);
	DDX_Text(pDX, IDC_DENS6, results.density[5]);
	DDX_Text(pDX, IDC_FLOW1, results.flow[0]);
	DDX_Text(pDX, IDC_FLOW2, results.flow[1]);
	DDX_Text(pDX, IDC_FLOW3, results.flow[2]);
	DDX_Text(pDX, IDC_FLOW4, results.flow[3]);
	DDX_Text(pDX, IDC_FLOW5, results.flow[4]);
	DDX_Text(pDX, IDC_FLOW6, results.flow[5]);
	DDX_Text(pDX, IDC_LOS1, results.LOS[0]);
	DDX_Text(pDX, IDC_LOS2, results.LOS[1]);
	DDX_Text(pDX, IDC_LOS3, results.LOS[2]);
	DDX_Text(pDX, IDC_LOS4, results.LOS[3]);
	DDX_Text(pDX, IDC_LOS5, results.LOS[4]);
	DDX_Text(pDX, IDC_LOS6, results.LOS[5]);
	DDX_Text(pDX, IDC_ACTCOUNT, results.actCount);
}

BEGIN_MESSAGE_MAP(CVD_GUIDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_WM_DESTROY()
	ON_WM_LBUTTONDOWN()
	ON_BN_CLICKED(IDC_ADD, &CVD_GUIDlg::OnBnClickedAdd)
	ON_LBN_SELCHANGE(IDC_LIST, &CVD_GUIDlg::OnLbnSelchangeList)
	ON_BN_CLICKED(IDC_ADDFOLDER, &CVD_GUIDlg::OnBnClickedAddfolder)
	ON_BN_CLICKED(IDC_SAVEINI, &CVD_GUIDlg::OnBnClickedSaveIni)
	ON_BN_CLICKED(IDC_USELANES, &CVD_GUIDlg::OnBnClickedUselanes)
END_MESSAGE_MAP()


// CVD_GUIDlg message handlers

BOOL CVD_GUIDlg::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
	//tab control initialization
	
	// Create the IPL image
	
	m_Image = cvCreateImage( cvSize(VIEW_WIDTH,VIEW_HEIGHT), IPL_DEPTH_8U, 3 );

	
	m_settings = 0;

	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 CVD_GUIDlg::OnPaint()
{
	CPaintDC dc(this);
	if (IsIconic())
	{
		 // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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();
	}

	CDC* pDC = &dc;
	int font_size;
	int x,y,x2,y2;
	
	if(m_showWelcome) {
		font_size = 70;
		x = 250;
		y = 70;
		x2 = 230;
		y2 = 170;
	}
	else {
		if(!m_Image) m_Image = cvCreateImage( cvSize(VIEW_WIDTH,VIEW_HEIGHT), IPL_DEPTH_8U, 3 );
		
		font_size = 20;
		x = 310;
		y = 10;
		x2 = 375;
		y2 = 10;

		//Initialize the BMP display buffer
		bmi = (BITMAPINFO*)buffer;
		bmih = &(bmi->bmiHeader);
		memset( bmih, 0, sizeof(*bmih));
		bmih->biSize = sizeof(BITMAPINFOHEADER); 
		bmih->biWidth = VIEW_WIDTH;
		bmih->biHeight = -VIEW_HEIGHT;
		bmih->biPlanes = 1; 
		bmih->biCompression = BI_RGB;
		bmih->biBitCount = 8 * m_Image->nChannels;
		palette = bmi->bmiColors;
		if (m_Image->nChannels == 1)
			{
			for( int i = 0; i < 256; i++ )
			{
				 palette[i].rgbBlue = palette[i].rgbGreen = palette[i].rgbRed = (BYTE)i;
				 palette[i].rgbReserved = 0;
			}
		}

		SetStretchBltMode(pDC->GetSafeHdc(), COLORONCOLOR);
		StretchDIBits(pDC->GetSafeHdc(), m_iRect.left, m_iRect.top, m_iRect.right-m_iRect.left,
			m_iRect.bottom-m_iRect.top, 0, 0, VIEW_WIDTH, VIEW_HEIGHT, m_Image->imageData, 
			(BITMAPINFO*)bmi, DIB_RGB_COLORS, SRCCOPY);
	}

	CFont font;
	VERIFY(font.CreateFont(
	   font_size,                        // nHeight
	   0,                         // nWidth
	   0,                         // nEscapement
	   0,                         // nOrientation
	   FW_NORMAL,                 // nWeight
	   FALSE,                     // bItalic
	   FALSE,                     // bUnderline
	   0,                         // cStrikeOut
	   ANSI_CHARSET,              // nCharSet
	   OUT_DEFAULT_PRECIS,        // nOutPrecision
	   CLIP_DEFAULT_PRECIS,       // nClipPrecision
	   DEFAULT_QUALITY,           // nQuality
	   DEFAULT_PITCH | FF_SWISS,  // nPitchAndFamily
	   _T("Arial")));                 // lpszFacename

	// Do something with the font just created...
	//CClientDC dc(this);  
	CFont* def_font = pDC->SelectObject(&font);
	pDC->SetBkMode(TRANSPARENT);

	pDC->TextOutA(x,y,_T("Vehicle"));
	pDC->TextOutA(x2,y2,_T("Detection"));
	pDC->SelectObject(def_font);

	// Done with the font.  Delete the font object.
	font.DeleteObject(); 
	
}

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


void CVD_GUIDlg::OnDestroy()
{
	SaveIniFile();
	CDialog::OnDestroy();
	cvReleaseImage(&m_Image);
	// TODO: Add your message handler code here
}

void CVD_GUIDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	

	CDialog::OnLButtonDown(nFlags, point);
}

void CVD_GUIDlg::OnBnClickedAdd()
{
	CFileDialog dlgAdd(TRUE, "jpg");
	m_pszFile = new TCHAR[2048]; //set a 2K buffer to hold selected files
	m_pszFile[0] = '\0';

	dlgAdd.m_ofn.Flags |= OFN_ALLOWMULTISELECT | OFN_EXPLORER;
	dlgAdd.m_ofn.lpstrTitle = "Add File(s)";
	dlgAdd.m_ofn.lpstrFile = m_pszFile; 
	dlgAdd.m_ofn.nMaxFile = 2048;
	
	if(dlgAdd.DoModal() == IDOK) {
		POSITION pos ( dlgAdd.GetStartPosition() );
		CListBox *box = (CListBox*) GetDlgItem(IDC_LIST);
		box->SetFocus();
		box->ResetContent();
		m_slFiles.RemoveAll();
		CString fn;

		while( pos ) {
			m_FileName = dlgAdd.GetNextPathName( pos );
			
			fn = m_FileName.Right(m_FileName.GetLength() - m_FileName.ReverseFind('\\') -1);
			
			box->AddString(fn);
			
			
			m_slFiles.AddTail(m_FileName);
			m_FileName.ReleaseBuffer();
			
		}
	}

	
	if(!VD_Initialize(m_slFiles,iniFile)) AfxMessageBox("Error during creating of background");
}

void CVD_GUIDlg::OnLbnSelchangeList()
{
	CListBox *box = (CListBox*) GetDlgItem(IDC_LIST);
	CString fn;
	
	// TODO: Add your control notification handler code here
	int index = box->GetCurSel();

	fn = m_slFiles.GetAt(m_slFiles.FindIndex(box->GetCurSel()));

	unsigned int ms1,ms2;
	
	ms1 = GetTickCount();
	
	LoadSettings();
	cvReleaseImage(&m_Image);

	//****************************************
	//********    Vehicle detection    *******
	//****************************************
	m_Image = (IplImage*) cvClone(VD_Core((char *)(LPCTSTR) fn,results,m_settings));							
	//****************************************
	for(int i=0;i<NUMBER_OF_LANES;i++)
		if(results.avgSpeed[i] == -1) results.avgSpeed[i]=0;


	m_settings = 0;
	m_showWelcome = false;

	ms2 = GetTickCount();

	CString str;

	str.Format(_T("%d ms"),ms2-ms1);
	SetDlgItemText(IDC_TIME,str);
	str.Format(_T("%2d.%d.%4d"),results.time.wDay,results.time.wMonth,results.time.wYear);
	SetDlgItemText(IDC_IMDATE,str);
	str.Format(_T("%02d:%02d:%02d.%03d"),results.time.wHour,results.time.wMinute,results.time.wSecond,results.time.wMilliseconds);
	SetDlgItemText(IDC_IMTIME,str);

	UpdateData(FALSE);
	
	InvalidateRect(m_iRect,false);
	InvalidateRect(CRect(300,0,450,35));
}



void CVD_GUIDlg::OnBnClickedAddfolder()
{
	BROWSEINFO bi = { 0 };
    bi.lpszTitle = _T("Pick a Directory");
    LPITEMIDLIST pidl = SHBrowseForFolder ( &bi );
    TCHAR path[MAX_PATH];
	if ( pidl != NULL )
    {
        // get the name of the folder
        
        if ( SHGetPathFromIDList ( pidl, path ) )
        {
            //MessageBox(path);
        }
				
		IMalloc * imalloc = 0;
        if ( SHGetMalloc ( &imalloc ))
        {
            imalloc->Free ( pidl );
            imalloc->Release ( );
        }   

		CFileFind finder;
		CListBox *box = (CListBox*) GetDlgItem(IDC_LIST);

		//horizontal list init
		CString    str;
		CSize      sz;
		int        dx = 0;
		TEXTMETRIC tm;
		CDC*       pDC = box->GetDC();
		CFont*     pFont = box->GetFont();

		// Select the listbox font, save the old font
		CFont* pOldFont = pDC->SelectObject(pFont);
		// Get the text metrics for avg char width
		pDC->GetTextMetrics(&tm); 

		box->ResetContent();
		m_slFiles.RemoveAll();
		// build a string with wildcards
		CString ss(path);
		CString strWildcard = ss +  _T("\\*.jpg");

		// start working for files
		BOOL bWorking = finder.FindFile(strWildcard);
		int i = 0;
		while (bWorking)
		{
			bWorking = finder.FindNextFile();
			
			box->AddString(finder.GetFileName());
			m_FileName = finder.GetFilePath();
			m_slFiles.AddTail(m_FileName);
			
			box->GetText(i, str);
			sz = pDC->GetTextExtent(str);
			// Add the avg width to prevent clipping
			sz.cx += tm.tmAveCharWidth;

			if (sz.cx > dx)
				dx = sz.cx;
			i++;
		}
		
		// Select the old font back into the DC
		pDC->SelectObject(pOldFont);
		box->ReleaseDC(pDC);

		// Set the horizontal extent so every character of all strings 
		// can be scrolled to.
		box->SetHorizontalExtent(dx);

		finder.Close();
		iniFile.Empty();

		if(m_slFiles.GetSize() > 0) {
			CFileFind finder2;
			strWildcard = ss + _T("\\*.ini");
			
			bWorking = finder2.FindFile(strWildcard);
			
			while (bWorking)
			{
			
				bWorking = finder2.FindNextFile(); 	
				iniFile = finder2.GetFilePath();
			}
			
			if(iniFile.IsEmpty()) iniFile = ss + _T("\\VD_scene_parameters.ini");
			
			finder2.Close();
			
			if(!VD_Initialize(m_slFiles,iniFile)) AfxMessageBox("Error during creating of background");

			CIniReader iniReader((char*)(LPCTSTR) iniFile);
			cs.height = iniReader.ReadFloat("CAMERA", "height", 1000);
			cs.ver_angle = iniReader.ReadFloat("CAMERA", "ver_angle", 1.3);
			cs.angle_range_x = iniReader.ReadFloat("CAMERA", "angle_range_x", 1.0);
			cs.angle_range_y = iniReader.ReadFloat("CAMERA", "angle_range_y", 0.5);
			m_settings = iniReader.ReadInteger("SETTINGS", "Control&Display", 0);
			ShowSettings();
			UpdateData(FALSE);
		}
	}
}

void CVD_GUIDlg::SaveIniFile()
{
	UpdateData(TRUE);
	LPTSTR p = iniFile.GetBuffer();
	CIniWriter iniWriter(p);
	
	iniWriter.WriteFloat("CAMERA", "height", cs.height);   
	iniWriter.WriteFloat("CAMERA", "ver_angle", cs.ver_angle); 
	iniWriter.WriteFloat("CAMERA", "angle_range_x", cs.angle_range_x); 
	iniWriter.WriteFloat("CAMERA", "angle_range_y", cs.angle_range_y);
	LoadSettings();
	iniWriter.WriteInteger("SETTINGS", "Control&Display", m_settings);
	iniFile.ReleaseBuffer();
}

void CVD_GUIDlg::OnBnClickedSaveIni()
{
	SaveIniFile();
	m_settings |= RELOADINI;
}

void CVD_GUIDlg::LoadSettings()
{
	UpdateData(TRUE);
	if(algs.carContours) m_settings|=CARCONTOURS;
	if(algs.carRects) m_settings|=CARRECTS;
	if(algs.roadContours) m_settings|=ROADCONTOURS;
	if(algs.roadSigns) m_settings|=ROADSIGNS;
	if(algs.shadowMask) m_settings|=SHADOWMASK;
	if(algs.path) m_settings|=PATH;
	if(algs.speed) m_settings|=SPEED;
	if(algs.length) m_settings|=LENGTH;
	if(algs.lanes) m_settings|=LANES;
	if(algs.useLanes) {
		m_settings|=USELANES;	
		if(algs.fixedLanes == 0) m_settings|=FIXEDLANES;
	}
}

void CVD_GUIDlg::ShowSettings()
{
	
	if(m_settings & CARCONTOURS) algs.carContours = true;
	else algs.carContours = false;
	if(m_settings & CARRECTS) algs.carRects = true;
	else algs.carRects = false;
	if(m_settings & ROADCONTOURS) algs.roadContours = true;
	else algs.roadContours = false;
	if(m_settings & ROADSIGNS) algs.roadSigns = true;
	else algs.roadSigns = false;
	if(m_settings & SHADOWMASK) algs.shadowMask = true;
	else algs.shadowMask = false;
	if(m_settings & PATH) algs.path = true;
	else algs.path = false;
	if(m_settings & SPEED) algs.speed = true;
	else algs.speed = false;
	if(m_settings & LENGTH) algs.length = true;
	else algs.length = false;
	if(m_settings & LANES) algs.lanes = true;
	else algs.lanes = false;
	if(m_settings & USELANES) {
		algs.useLanes = true;	
		if(m_settings & FIXEDLANES) algs.fixedLanes = false;
		else algs.fixedLanes = true;
	}
	else
		algs.useLanes = false;

	CButton *rb = (CButton*) GetDlgItem(IDC_FIXED1);
	if(algs.useLanes) rb->EnableWindow(true);
	else rb->EnableWindow(false);
	rb = (CButton*) GetDlgItem(IDC_FIXED2);
	if(algs.useLanes) rb->EnableWindow(true);
	else rb->EnableWindow(false);

	CStatic* s;
	if(algs.useLanes) {
		rb = (CButton*) GetDlgItem(IDC_FIXED1);
		rb->EnableWindow(true);
		rb = (CButton*) GetDlgItem(IDC_FIXED2);
		rb->EnableWindow(true);

		s = (CStatic*) GetDlgItem(IDC_DENS1);
		s->EnableWindow(true);
		s = (CStatic*) GetDlgItem(IDC_DENS2);
		s->EnableWindow(true);
		s = (CStatic*) GetDlgItem(IDC_DENS3);
		s->EnableWindow(true);
		s = (CStatic*) GetDlgItem(IDC_DENS4);
		s->EnableWindow(true);
		s = (CStatic*) GetDlgItem(IDC_DENS5);
		s->EnableWindow(true);
		s = (CStatic*) GetDlgItem(IDC_DENS6);
		s->EnableWindow(true);

		s = (CStatic*) GetDlgItem(IDC_FLOW1);
		s->EnableWindow(true);
		s = (CStatic*) GetDlgItem(IDC_FLOW2);
		s->EnableWindow(true);
		s = (CStatic*) GetDlgItem(IDC_FLOW3);
		s->EnableWindow(true);
		s = (CStatic*) GetDlgItem(IDC_FLOW4);
		s->EnableWindow(true);
		s = (CStatic*) GetDlgItem(IDC_FLOW5);
		s->EnableWindow(true);
		s = (CStatic*) GetDlgItem(IDC_FLOW6);
		s->EnableWindow(true);

		s = (CStatic*) GetDlgItem(IDC_LOS1);
		s->EnableWindow(true);
		s = (CStatic*) GetDlgItem(IDC_LOS2);
		s->EnableWindow(true);
		s = (CStatic*) GetDlgItem(IDC_LOS3);
		s->EnableWindow(true);
		s = (CStatic*) GetDlgItem(IDC_LOS4);
		s->EnableWindow(true);
		s = (CStatic*) GetDlgItem(IDC_LOS5);
		s->EnableWindow(true);
		s = (CStatic*) GetDlgItem(IDC_LOS6);
		s->EnableWindow(true);
		
	}
	else {
		rb = (CButton*) GetDlgItem(IDC_FIXED1);
		rb->EnableWindow(false);
		rb = (CButton*) GetDlgItem(IDC_FIXED2);
		rb->EnableWindow(false);

		s = (CStatic*) GetDlgItem(IDC_DENS1);
		s->EnableWindow(false);
		s = (CStatic*) GetDlgItem(IDC_DENS2);
		s->EnableWindow(false);
		s = (CStatic*) GetDlgItem(IDC_DENS3);
		s->EnableWindow(false);
		s = (CStatic*) GetDlgItem(IDC_DENS4);
		s->EnableWindow(false);
		s = (CStatic*) GetDlgItem(IDC_DENS5);
		s->EnableWindow(false);
		s = (CStatic*) GetDlgItem(IDC_DENS6);
		s->EnableWindow(false);

		s = (CStatic*) GetDlgItem(IDC_FLOW1);
		s->EnableWindow(false);
		s = (CStatic*) GetDlgItem(IDC_FLOW2);
		s->EnableWindow(false);
		s = (CStatic*) GetDlgItem(IDC_FLOW3);
		s->EnableWindow(false);
		s = (CStatic*) GetDlgItem(IDC_FLOW4);
		s->EnableWindow(false);
		s = (CStatic*) GetDlgItem(IDC_FLOW5);
		s->EnableWindow(false);
		s = (CStatic*) GetDlgItem(IDC_FLOW6);
		s->EnableWindow(false);

		s = (CStatic*) GetDlgItem(IDC_LOS1);
		s->EnableWindow(false);
		s = (CStatic*) GetDlgItem(IDC_LOS2);
		s->EnableWindow(false);
		s = (CStatic*) GetDlgItem(IDC_LOS3);
		s->EnableWindow(false);
		s = (CStatic*) GetDlgItem(IDC_LOS4);
		s->EnableWindow(false);
		s = (CStatic*) GetDlgItem(IDC_LOS5);
		s->EnableWindow(false);
		s = (CStatic*) GetDlgItem(IDC_LOS6);
		s->EnableWindow(false);
	}	
}

void CVD_GUIDlg::OnBnClickedUselanes()
{
	UpdateData(TRUE);
	CButton *rb;
	CStatic* s;
	if(algs.useLanes) {
		rb = (CButton*) GetDlgItem(IDC_FIXED1);
		rb->EnableWindow(true);
		rb = (CButton*) GetDlgItem(IDC_FIXED2);
		rb->EnableWindow(true);

		s = (CStatic*) GetDlgItem(IDC_DENS1);
		s->EnableWindow(true);
		s = (CStatic*) GetDlgItem(IDC_DENS2);
		s->EnableWindow(true);
		s = (CStatic*) GetDlgItem(IDC_DENS3);
		s->EnableWindow(true);
		s = (CStatic*) GetDlgItem(IDC_DENS4);
		s->EnableWindow(true);
		s = (CStatic*) GetDlgItem(IDC_DENS5);
		s->EnableWindow(true);
		s = (CStatic*) GetDlgItem(IDC_DENS6);
		s->EnableWindow(true);

		s = (CStatic*) GetDlgItem(IDC_FLOW1);
		s->EnableWindow(true);
		s = (CStatic*) GetDlgItem(IDC_FLOW2);
		s->EnableWindow(true);
		s = (CStatic*) GetDlgItem(IDC_FLOW3);
		s->EnableWindow(true);
		s = (CStatic*) GetDlgItem(IDC_FLOW4);
		s->EnableWindow(true);
		s = (CStatic*) GetDlgItem(IDC_FLOW5);
		s->EnableWindow(true);
		s = (CStatic*) GetDlgItem(IDC_FLOW6);
		s->EnableWindow(true);

		s = (CStatic*) GetDlgItem(IDC_LOS1);
		s->EnableWindow(true);
		s = (CStatic*) GetDlgItem(IDC_LOS2);
		s->EnableWindow(true);
		s = (CStatic*) GetDlgItem(IDC_LOS3);
		s->EnableWindow(true);
		s = (CStatic*) GetDlgItem(IDC_LOS4);
		s->EnableWindow(true);
		s = (CStatic*) GetDlgItem(IDC_LOS5);
		s->EnableWindow(true);
		s = (CStatic*) GetDlgItem(IDC_LOS6);
		s->EnableWindow(true);
		
	}
	else {
		rb = (CButton*) GetDlgItem(IDC_FIXED1);
		rb->EnableWindow(false);
		rb = (CButton*) GetDlgItem(IDC_FIXED2);
		rb->EnableWindow(false);

		s = (CStatic*) GetDlgItem(IDC_DENS1);
		s->EnableWindow(false);
		s = (CStatic*) GetDlgItem(IDC_DENS2);
		s->EnableWindow(false);
		s = (CStatic*) GetDlgItem(IDC_DENS3);
		s->EnableWindow(false);
		s = (CStatic*) GetDlgItem(IDC_DENS4);
		s->EnableWindow(false);
		s = (CStatic*) GetDlgItem(IDC_DENS5);
		s->EnableWindow(false);
		s = (CStatic*) GetDlgItem(IDC_DENS6);
		s->EnableWindow(false);

		s = (CStatic*) GetDlgItem(IDC_FLOW1);
		s->EnableWindow(false);
		s = (CStatic*) GetDlgItem(IDC_FLOW2);
		s->EnableWindow(false);
		s = (CStatic*) GetDlgItem(IDC_FLOW3);
		s->EnableWindow(false);
		s = (CStatic*) GetDlgItem(IDC_FLOW4);
		s->EnableWindow(false);
		s = (CStatic*) GetDlgItem(IDC_FLOW5);
		s->EnableWindow(false);
		s = (CStatic*) GetDlgItem(IDC_FLOW6);
		s->EnableWindow(false);

		s = (CStatic*) GetDlgItem(IDC_LOS1);
		s->EnableWindow(false);
		s = (CStatic*) GetDlgItem(IDC_LOS2);
		s->EnableWindow(false);
		s = (CStatic*) GetDlgItem(IDC_LOS3);
		s->EnableWindow(false);
		s = (CStatic*) GetDlgItem(IDC_LOS4);
		s->EnableWindow(false);
		s = (CStatic*) GetDlgItem(IDC_LOS5);
		s->EnableWindow(false);
		s = (CStatic*) GetDlgItem(IDC_LOS6);
		s->EnableWindow(false);
	}	
	UpdateData(FALSE);
	
}
