/*******************************************************************

Copyright (c) 2004 Open Mobile Alliance, Ltd.  All Rights Reserved.

This software is made available under the usage and distribution terms 
covered by the Common Public License 1.0 and by using this software you
are bound by the terms and conditions of the same.

EXCEPT AS EXPRESSLY SET FORTH IN THE COMMON PUBLIC LICENSE,
THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, 
ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, 
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
   
Each recipient is solely responsible for determining the 
appropriateness of using and distributing this program and assumes 
all risks associated with its exercise of rights under the Common 
Public License, including but not limited to the risks and 
costs of program errors, compliance with applicable laws, 
damage to or loss of data, programs or equipment, and 
unavailability or interruption of operations.

********************************************************************/
// UIAChoice.cpp : implementation file
//

#include "stdafx.h"
#include "scts3devman.h"
#include "UIAChoice.h"

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

/////////////////////////////////////////////////////////////////////////////
// CUIAChoice dialog


CUIAChoice::CUIAChoice(SctsSemaphore& _lock,std::list<char *>& _items,int& _choice,CWnd* pParent /*=NULL*/)
	: CDialog(CUIAChoice::IDD, pParent),lock(_lock),items(_items),choice(&_choice),choices(0)
{
	//{{AFX_DATA_INIT(CUIAChoice)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


CUIAChoice::CUIAChoice(SctsSemaphore& _lock,std::list<char *>& _items,std::list<int>& _choices,CWnd* pParent /*=NULL*/)
	: CDialog(CUIAChoice::IDD, pParent),lock(_lock),items(_items),choice(0),choices(&_choices)
{
	//{{AFX_DATA_INIT(CUIAChoice)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CUIAChoice::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CUIAChoice)
    DDX_Control(pDX, IDC_CHOICE, m_choice);
    //}}AFX_DATA_MAP
    DDX_Control(pDX, IDC_CHOICES, m_choices);
}


BEGIN_MESSAGE_MAP(CUIAChoice, CDialog)
	//{{AFX_MSG_MAP(CUIAChoice)
	ON_WM_DESTROY()
	ON_WM_CLOSE()
	ON_LBN_DBLCLK(IDC_CHOICE, OnDblclkChoice)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CUIAChoice message handlers

BOOL CUIAChoice::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	SetWindowText(items.front());
	items.pop_front();
	GetDlgItem(IDC_DISPLAY)->SetWindowText(items.front());
	items.pop_front();
    /*
     * Change to support Multi Choices. 
     */
    if (!choices)
    {
	    while (!items.empty())
	    {
		    m_choice.AddString(items.front());
            m_choices.AddString(items.front());
		    items.pop_front();
	    }
	    m_choice.SetCurSel(0);
        m_choices.EnableWindow(FALSE);
        m_choices.ShowWindow(FALSE);
    }
    else
    {
	    while (!items.empty())
	    {
		    m_choices.AddString(items.front());
            m_choices.AddString(items.front());
		    items.pop_front();
	    }
	    m_choices.SetCurSel(0);
        m_choice.EnableWindow(FALSE);
        m_choice.ShowWindow(FALSE);
    }
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CUIAChoice::OnDestroy() 
{
	CDialog::OnDestroy();
    /*
     * Change to support Multi Choices. 
     */
    if (!choices)
    {
	    *choice=m_choice.GetCurSel()+1;
    }
    else
    {
        int nCount=m_choices.GetSelCount();
        if (nCount)
        {
            int *listBoxSel=new int[nCount];
            m_choices.GetSelItems(nCount, listBoxSel);
            while(nCount)
            {
                choices->push_back(listBoxSel[--nCount]);
            }
        }
    }
	lock.Unlock();
	delete this;
}

void CUIAChoice::OnClose() 
{
    /*
     * Change to support Multi Choices. 
     */
    if (!choices)
    {
	    *choice=m_choice.GetCurSel()+1;
    }
    else
    {
        int nCount=m_choices.GetSelCount();
        if (nCount)
        {
            int *listBoxSel=new int[nCount];
            m_choices.GetSelItems(nCount, listBoxSel);
            while(nCount)
            {
                choices->push_back(listBoxSel[--nCount]);
            }
        }
    }
	CDialog::OnClose();
	lock.Unlock();
	delete this;
}

void CUIAChoice::OnOK()
{
	OnDestroy();
}

void CUIAChoice::OnCancel()
{
	OnDestroy();
}

void CUIAChoice::OnDblclkChoice() 
{
	OnDestroy();
}
