//------------------------------------------------------------------------------
// File: StartDialog.cpp
//
// Desc: DirectShow sample code - MultiVMR9 GamePlayer
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//------------------------------------------------------------------------------

#include "stdafx.h"
#include "GamePlayer.h"
#include "StartDialog.h"
#include <Cderr.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#include <vip.h>
CGamePlayerSession* g_pSession = NULL;
extern vip* VP;

static int partymode = 1;

static int running[256];

static int maxcap = 0;
static int ask = 0;


extern HINSTANCE g_hInstance;

const UINT_PTR g_TimerID = 0x54321;
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
    CAboutDlg();

// Dialog Data
    enum { IDD = IDD_ABOUTBOX };

    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
    DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CStartDialog dialog


/******************************Public*Routine******************************\
* CStartDialog
* constructor
\**************************************************************************/
CStartDialog::CStartDialog(CWnd* pParent /*=NULL*/)
    : CDialog(CStartDialog::IDD, pParent)
    , m_pSession( NULL )
    , m_nTimer( NULL )
{
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

/******************************Public*Routine******************************\
* DoDataExchange
* 
\**************************************************************************/
void CStartDialog::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_LIST_SOURCES, m_ListSources);
}

/******************************Public*Routine******************************\
* MESSAGE_MAP
* 
\**************************************************************************/
BEGIN_MESSAGE_MAP(CStartDialog, CDialog)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    //}}AFX_MSG_MAP
    ON_BN_CLICKED(IDC_BUTTON_ADD, OnBnClickedButtonAdd)
    ON_BN_CLICKED(IDC_BUTTON_DELETE, OnBnClickedButtonDelete)
    ON_BN_CLICKED(IDC_BUTTON_START, OnBnClickedButtonStart)
    ON_BN_CLICKED(IDC_BUTTON_STOP, OnBnClickedButtonStop)
    ON_WM_DESTROY()
    ON_WM_TIMER()
END_MESSAGE_MAP()

// CStartDialog message handlers

/******************************Public*Routine******************************\
* OnInitDialog
* 
\**************************************************************************/
BOOL CStartDialog::OnInitDialog()
{
    HRESULT hr = S_OK;
    CDialog::OnInitDialog();

	//VP->debug("walk (init dialog)");

    // Add "About..." menu item to system menu.

    // IDM_ABOUTBOX must be in the system command range.
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000);

    CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
        CString strAboutMenu;
        strAboutMenu.LoadString(IDS_ABOUTBOX);
        if (!strAboutMenu.IsEmpty())
        {
            pSysMenu->AppendMenu(MF_SEPARATOR);
            pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
        }
    }

    m_ListSources.DeleteColumn(0);
    m_ListSources.InsertColumn(0, TEXT("media files"), LVCFMT_LEFT, 300, 0);


    for (int i=0; i <256 ; i++) running[i] = 0;

CString all[] = {
"capture.avi",
"art-must-be-beautiful.wmv",
"blue-period.wmv",
"china-ring.wmv",
"city-of-angels.wmv",
"cleaning-the-mirror-1.wmv",
"cleaning-the-mirror-2.wmv", //5
"dissolution.wmv",
"dozing-consciousness.wmv",
"dragon-heads-1.wmv",
"dragon-heads-2.wmv",
"dragon-heads-4.wmv", // 10
"freeing-the-memory.wmv",
"image-of-happiness.wmv",
"in-between.wmv",
"lost-souls.wmv",
"night-sea-crossing-conjunction.wmv", //15
"night-sea-crossing-observer.wmv",
"red-period.wmv",
"relation-in-time.wmv",
"spirit-house-dissolution.wmv",
"spirit-house-dozing-consciousness.wmv", // 20
"spirit-house-insomnia.wmv",
"spirit-house-lost-souls.wmv",
"spirit-house-luminosity.wmv",
"stromboli.wmv",
"terminal-garden.wmv", //25
"terra-degla-dea-madre.wmv",
"the-onion.wmv",
"thomas-lips-1975.wmv",
"thomas-lips-1993.wmv",
"waves.wmv", //30
""}; // AE

int msel1[] = { 19, 9, 14, 25, 29, 7, 12, 19, -1 }; 
int msel2[] = { 0, 12, -1};
int msel3[] = {  -1};

int* msel = (int*) msel1;

    int xvar = -1;
    
	//VP->debug("walk (ask video)");
	xvar = 5;
	if (!scene::ds("nopreset") && VP) 
		xvar = VP->ivalq("X:interact(option(X))");
	//VP->debug("walk (got video)");
    

	//if (!VP || !xvar) VP->debug(vip::fm("what? [%d]",xvar));
	if (!VP)  VP->debug(vip::fm("what? [%d]",xvar));
  
	CString sources[256] = { "\\?.wmv", "\\?.wmv", "\\?.wmv", "\\?.wmv", 
		"\\?.wmv", "\\?.wmv", "\\?.wmv", "\\?.wmv", "\\?.wmv", "\\end.wmv" };
/*
	CString sources[256] = { "\\?.wmv", "..\\media\\dummy.wmv", "..\\media\\dummy.wmv", "..\\media\\dummy.wmv", 
		"..\\media\\dummy.wmv", "..\\media\\dummy.wmv", "..\\media\\dummy.wmv", "..\\media\\dummy.wmv", 
		"..\\media\\dummy.wmv","..\\media\\dummy.wmv", "..\\media\\dummy.wmv", "..\\media\\dummy.wmv",
		"..\\media\\dummy.wmv", "..\\media\\dummy.wmv", "..\\media\\dummy.wmv", "..\\media\\dummy.wmv", 
		"..\\media\\dummy.wmv","..\\media\\dummy.wmv", "..\\media\\dummy.wmv", "..\\media\\dummy.wmv",
		"..\\media\\dummy.wmv", "..\\media\\dummy.wmv", "..\\media\\dummy.wmv", "..\\media\\dummy.wmv", 
		"..\\media\\dummy.wmv","..\\media\\dummy.wmv", "..\\media\\dummy.wmv", "..\\media\\dummy.wmv",
		"..\\media\\dummy.wmv", "..\\media\\dummy.wmv", "..\\media\\dummy.wmv", "..\\media\\dummy.wmv", 
		"..\\media\\dummy.wmv","..\\media\\dummy.wmv", "..\\media\\dummy.wmv", "..\\media\\dummy.wmv",
		"..\\media\\dummy.wmv", "..\\media\\dummy.wmv", "..\\media\\dummy.wmv", "..\\media\\dummy.wmv", 
		"..\\media\\dummy.wmv","..\\media\\dummy.wmv", "..\\media\\dummy.wmv", "..\\media\\dummy.wmv",
		"..\\media\\dummy.wmv", "..\\media\\dummy.wmv", "..\\media\\dummy.wmv", "..\\media\\dummy.wmv", "\\end.wmv" };
*/	
	maxcap = 2;
	int premax = 0;
	ask = 0; // no prompting
	//sources[0] = "d:\\vip\\media\\capture.avi";
	int select_sources = 1; // from cases
	int select_list = 0; // from all

	int ch = xvar; //3; // 3:dossier
	maxcap = 4; // local
	if (1) {
	switch (ch) {
		case 0:
	       sources[0] = "d:\\vip\\media\\?.wmv";
		   //sources[1] = "d:\\vip\\media\\imm-coldplay-05.wmv";
		   sources[2] = "";
		   premax = 1;
		   break;
		case 1: {
			
			char* key = VP->svalq("X:selection(X)");
			//premax = VP->ivalq(vip::fm("X:video('%s'(X))",key));
			premax = VP->ivalq(vip::fm("X:program(%s,X)",key));
			char* p = 0; int k = 0;
			//VP->eval( vip::fm("X:resource(video(%s),X)",key));
			VP->eval( vip::fm("X:video(%s,X)",key));

			if (key) while ( (k < premax) && (p = VP->result()) ) {
				//VP->debug(vip::fm("%s(%d) %s",key,k,p?p:"?"));
				char c = *p; 
				if (c >= '0' && c <= '9') 
					sources[k] = vip::fm("//%s.wmv",p);
				else sources[k] = CString(p);
				
				k++;
			}
			for (int k=0; k < premax; k++) {
                char* rx = VP->svalq(vip::fm("X:running(%s,%d,X)",key,k+1));
				//VP->debug(vip::fm("running: %d %s",k+1,rx));
				if (rx) running[k] = 1; // should be true
			}
				
			}; break;
		case 2:
		   sources[0] = "..\\media\\?.wmv";
		   sources[1] = "..\\media\\isabelle.avi";
		   sources[2] = "..\\media\\sebas.avi";
		   sources[3] = "..\\media\\merijn.avi";
		   sources[4] = "";
		   premax = 4;
		   break;
	   	case 3:
		   sources[0] = "\\1.wmv";
		   sources[1] = "..\\media\\imm-coldplay-05.wmv";
		   sources[2] = "\\2.wmv";
		   sources[3] = "..\\media\\abramovic\\thomas-lips-1975.wmv";
		   sources[4] = "\\3.wmv";
		   

		   sources[5] = "..\\media\\abramovic\\in-between.wmv";

		   //sources[5] = "d:\\video\\dragon-heads-1.wmv";
		   //sources[6] = "d:\\video\\relation-in-time.wmv";
		   //sources[6] = "d:\\video\\relation-in-time.wmv";
		   sources[6] = "d:\\CD\\edgecodes\\edgecodes-hr.wmv";
		   sources[7] = "..\\media\\abramovic\\dissolution.wmv";
		   sources[8] = "";
		   sources[3] = "..\\media\\video\\nederlands-design.wmv";
		   premax = 8;
		   //running[1] = 1;
		   //running[3] = 1;
		   //running[5] = 1;
		   //running[6] = 1;
		   //running[7] = 1;
		   //scene::ds("run:2"); // +1
		   //scene::ds("run:4");
		   //scene::ds("run:6");
		   break;
	   	case 4:
		   sources[0] = "\\3.wmv";
		   sources[1] = "\\1.wmv";
		   sources[2] = "\\2.wmv";
		   sources[3] = "d:\\tamtam\\clockwork.wmv";
		   sources[4] = "d:\\tamtam\\ogrish-murder.wmv";
		   sources[5] = "d:\\tamtam\\ogrish-suicide.wmv";
		   sources[6] = "";
		   premax = 6;
		   running[3] = 1;
		   running[4] = 1;
		   running[5] = 1;
		   scene::ds("run:4"); // +1
		   break;
	   case 5:
		   sources[0] = "\\0.wmv";
		   
		   premax = 1;
		   running[1] = 1;
		   
		   break;

	};
	}

	//VP->debug("walk (pre-dialog)");

	if (0) {
	int nItems = m_ListSources.GetItemCount();
	for (int i=0; i < premax; i++) {
	    // CString str = "d:\\casus\\relation-in-time.wmv";
	    if (sources[i] && (sources[i][0] != '\0')) m_ListSources.InsertItem( nItems+i, sources[i]);
	}
	}
    
	if (select_list) {
	int nItems = m_ListSources.GetItemCount();
	char* spath = "d:\\video";
	
	int*p = msel; int i = 0;
	while (p && *p != -1) {
		char buf[1024];
		sprintf(buf,"%s\\%s",spath,all[*p]);
		m_ListSources.InsertItem( nItems+i, buf);
		p++; i++;
	    }
	}
		

	if (select_sources) {
	int nItems = m_ListSources.GetItemCount();
	for (int i=0; i < premax; i++) {
	    // CString str = "d:\\casus\\relation-in-time.wmv";
	    if (sources[i] && (sources[i][0] != '\0')) m_ListSources.InsertItem( nItems+i, sources[i]);
	}
	}


    // 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
    
    m_nTimer = SetTimer( g_TimerID, 500, 0);

    UpdateControls_();

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

/******************************Public*Routine******************************\
* OnSysCommand
* 
\**************************************************************************/
void CStartDialog::OnSysCommand(UINT nID, LPARAM lParam)
{
    if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    {
        CAboutDlg dlgAbout;
        dlgAbout.DoModal();
    }
    else
    {
        CDialog::OnSysCommand(nID, lParam);
    }
}

// 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.

/******************************Public*Routine******************************\
* OnPaint
* 
\**************************************************************************/
void CStartDialog::OnPaint() 
{
    if (IsIconic())
    {
        CPaintDC dc(this); // 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();
    }
}

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

/******************************Public*Routine******************************\
* OnBnClickedButtonAdd
* response to "Add" button
\**************************************************************************/
void CStartDialog::OnBnClickedButtonAdd()
{
    TCHAR achPath[MAX_PATH];
    TCHAR achFilePath[MAX_PATH];
    OPENFILENAME ofn;
    DWORD dwErr = 0;

    GetCurrentDirectory( MAX_PATH, achPath);

    static TCHAR szFilter[]  = TEXT("Video Files (.mpg;.mpeg;.avi;.wmv;.asf)\0*.mpg;*.mpeg;*.avi;*.wmv;*.asf\0") \
                               TEXT("All Files (*.*)\0*.*\0\0");
    lstrcpy(achFilePath, TEXT(""));

    ZeroMemory( &ofn, sizeof(ofn) );
    ofn.lStructSize = sizeof(OPENFILENAME);
    ofn.hwndOwner           = NULL;
    ofn.hInstance           = NULL;
    ofn.lpstrFilter         = szFilter;
    ofn.nFilterIndex        = 1;
    ofn.lpstrCustomFilter   = NULL;
    ofn.nMaxCustFilter      = 0;
    ofn.lpstrFile           = achFilePath;
    ofn.nMaxFile            = MAX_PATH;
    ofn.lpstrFileTitle      = NULL;
    ofn.nMaxFileTitle       = 0;
    ofn.lpstrInitialDir     = NULL;
    ofn.lpstrFileTitle      = NULL;
    ofn.nMaxFileTitle       = 0;
    ofn.lpstrInitialDir     = NULL;
    ofn.nFileOffset         = 0;
    ofn.nFileExtension      = 0;
    ofn.lpstrDefExt         = NULL;
    ofn.lCustData           = 0L;
    ofn.lpfnHook            = NULL;
    ofn.lpTemplateName  = NULL; 

    if( TRUE == GetOpenFileName(&ofn ))
    {
        int nItem = m_ListSources.GetItemCount();
        CString str = ofn.lpstrFile;
        m_ListSources.InsertItem( nItem, str);
    }
    UpdateData( FALSE);
    SetCurrentDirectory( achPath );
}

/******************************Public*Routine******************************\
* OnBnClickedButtonDelete
* response to "Delete" button
\**************************************************************************/
void CStartDialog::OnBnClickedButtonDelete()
{
    int nItem = m_ListSources.GetNextItem( -1, LVNI_SELECTED|LVNI_FOCUSED);
    if( -1 == nItem )
        return;

    m_ListSources.DeleteItem(nItem);
    UpdateData( FALSE);
}

/******************************Public*Routine******************************\
* OnBnClickedButtonStart
* response to "Start" button
\**************************************************************************/
void CStartDialog::OnBnClickedButtonStart()
{
    HRESULT hr = S_OK;
    int nItems = 0;
    DWORD_PTR dwID = NULL;
    CVMR9Subgraph* pSource = NULL;
    
    if( m_pSession )
        return;

    m_pSession = g_pSession = new CGamePlayerSession();
    if( FAILED(hr))
    {
        AfxMessageBox(TEXT("Failed to create a new CGamePlayerSession"), MB_OK, 0);
        return;
    }

    hr = m_pSession->Initialize();
    if( FAILED(hr))
    {
        AfxMessageBox(TEXT("Failed to initialize CGamePlayerSession"), MB_OK, 0);
        return;
    }

    //nItems = m_ListSources.GetItemCount();
	//CString str = "d:\casus\relation-in-time.wmv";
	//m_ListSources.InsertItem( nItems, str);

	nItems = m_ListSources.GetItemCount();

    for( int i=0; i<nItems; i++)
    {
        TCHAR achPath[MAX_PATH];
        WCHAR wcPath[MAX_PATH];

        m_ListSources.GetItemText( i, 0, achPath, MAX_PATH);
#ifdef UNICODE
        wcscpy( wcPath, achPath);
#else
        MultiByteToWideChar(CP_ACP, 0, achPath, -1, wcPath, MAX_PATH); 
#endif

	    char buf[512];
		char* p = &buf[0];
		WCHAR* q = &wcPath[0];
		int ll = wcslen(wcPath);
		for (int kk = 0; kk < ll; kk++) *p++ = *q++;
		*p = '\0';
		
		//sprintf(buf,"length: %d",wcslen(wcPath));
		char name[256];
		p = &buf[strlen(buf)-1];
		while ((*p != '\\') && (*p != '/')) p--;
		wsprintf(name,"%s",p+1);
		//VP->debug(name);
		static int tb = maxcap; //2;
		int b = 0;
		int num = 0;
		ask = 0;
		if (!strcmp("capture.avi",name)) { b = 1; num = 0; }
		if (!strcmp("?.wmv",name)) { ask = 1; b = 0; num = 0; }
		if (!strcmp("unknown.wmv",name)) { ask = 1; b = 0; num = 0; }
		if (!strcmp("capture.0.wmv",name) || !strcmp("0.wmv",name)) { b = 1; num = 0; }
		if (!strcmp("capture.1.wmv",name) || !strcmp("1.wmv",name)) { b = 1; num = 1; }
		if (!strcmp("capture.2.wmv",name) || !strcmp("2.wmv",name)) { b = 1; num = 2; }
		if (!strcmp("capture.3.wmv",name) || !strcmp("3.wmv",name)) { b = 1; num = 3; }
		if (!strcmp("capture.4.wmv",name) || !strcmp("4.wmv",name)) { b = 1; num = 4; }
		if (!strcmp("capture.5.wmv",name) || !strcmp("5.wmv",name)) { b = 1; num = 5; }
		if (!strcmp("capture.6.wmv",name) || !strcmp("6.wmv",name)) { b = 1; num = 6; }
		if (!strcmp("capture.7.wmv",name) || !strcmp("7.wmv",name)) { b = 1; num = 7; }
		if (!strcmp("capture.8.wmv",name) || !strcmp("8.wmv",name)) { b = 1; num = 8; }
		if (!strcmp("capture.9.wmv",name) || !strcmp("9.wmv",name)) { b = 1; num = 9; }

		//sscanf(name,"%d",num);
		//num = atoi(name); //name[0] - '0';
		if ((num < 0) || (num > 9)) num = 0;

		if (b || (num != 0)) {
         
		//ask = 1;
		char buf[256];
		sprintf(buf,"capture %d (%d)",num,i);
		hr = IDYES;
		if (ask) hr = AfxMessageBox(buf,MB_YESNO | MB_ICONQUESTION, 0 );
		if (hr != IDYES) num = 0;
		hr = m_pSession->AddCapture( dwID, num );
		b = 1;
		

        //if( tb > 1) hr = AfxMessageBox(name,MB_YESNO | MB_ICONQUESTION, 0 );
		} else {
		char buf[256];
		sprintf(buf,"play from capture (%d)?",i);
        //int fask = 0;
		if (ask && tb-- > 0) hr = AfxMessageBox(buf,MB_YESNO | MB_ICONQUESTION, 0 );
		else hr = IDNO;
		if (hr == IDYES) {
		hr = m_pSession->AddCapture( dwID );
		b = 1;
		} else {
			b = 0;
			if (ask) {
				char* path = vip::find_video(2,0,0,0); MultiByteToWideChar(CP_ACP, 0, path, -1, wcPath, MAX_PATH); 
				//VP->debug(vip::fm("asked: %s",path));
			}
			hr = m_pSession->AddSource( wcPath, dwID );
			//hr = m_pSession->AddCapture(  dwID, -1, wcPath );
		}
		}
		
		
		if( FAILED(hr))
        {
            CString strMsg;
            strMsg.Format(TEXT("Failed to add source\r\n%s\r\nto the session. Error code 0x%08x"),
                achPath, hr);
            AfxMessageBox( strMsg, MB_OK, 0);
        }
        pSource = m_pSession->GetSubgraph( dwID);

		if (b)  pSource->capture = 1;
		pSource->_id = i+1;
		char pbuf[64];
		sprintf(pbuf,"run:%d",i+1);
		
		if (running[i]) { scene::ds(pbuf,1); } //pSource->capture = 1; } 

		VP->set_total(i+1);
		//scene::ds("total",i+1);

		//scene::ds("run:1",1); // AE!

        if (pSource)
        {
			if (pSource->capture) {  scene::ds(pbuf,1); }
            pSource->Run();
			if ( !pSource->capture ) pSource->Pause();
			
        }
        m_ListSources.SetItemData( i, dwID);
    }

	//VP->debug(vip::fm("total: %d",scene::_total()));

    if( FAILED(hr))
    {
        CString strMsg;
        strMsg.Format(TEXT("Failed to run the session. Error code 0x%08x"), hr);
        AfxMessageBox( strMsg, MB_OK, 0);
    }
    UpdateControls_();
}

/******************************Public*Routine******************************\
* OnBnClickedButtonStop
* response to "Stop" button
\**************************************************************************/
void CStartDialog::OnBnClickedButtonStop()
{
    if( m_pSession )
    {
        delete m_pSession;
        m_pSession = NULL;
    }

    UpdateControls_();
}

/******************************Public*Routine******************************\
* OnDestroy
* WM_DESTROY
\**************************************************************************/
void CStartDialog::OnDestroy()
{
    CDialog::OnDestroy();

    if( m_pSession )
    {
        delete m_pSession;
        m_pSession = NULL;
    }

    ::KillTimer( GetSafeHwnd(), m_nTimer);
}

/******************************Public*Routine******************************\
* OnTimer
\**************************************************************************/
void CStartDialog::OnTimer(UINT nIDEvent)
{
    if( m_pSession )
    {
        m_pSession->LoopSources();
    }

    CDialog::OnTimer(nIDEvent);
}

/////////////////////////////// PRIVATE DOMAIN //////////////////////////////

/******************************Private*Routine*****************************\
* UpdateControls_
* udate controls depending on the state of the application
\**************************************************************************/
void CStartDialog::UpdateControls_()
{
    if( GetDlgItem(IDC_BUTTON_START))
        GetDlgItem(IDC_BUTTON_START)->EnableWindow( m_pSession ? FALSE : TRUE );

    if( GetDlgItem(IDC_BUTTON_STOP))
        GetDlgItem(IDC_BUTTON_STOP)->EnableWindow( m_pSession ? TRUE : FALSE );

    if( GetDlgItem(IDC_BUTTON_ADD))
        GetDlgItem(IDC_BUTTON_ADD)->EnableWindow( m_pSession ? FALSE : TRUE );

    if( GetDlgItem(IDC_BUTTON_DELETE))
        GetDlgItem(IDC_BUTTON_DELETE)->EnableWindow( m_pSession ? FALSE : TRUE );

    if( GetDlgItem(IDOK))
        GetDlgItem(IDOK)->EnableWindow( m_pSession ? FALSE : TRUE );

    UpdateData( FALSE );
}