Click here to Skip to main content
15,895,808 members
Articles / Desktop Programming / WTL

TDXML: XML-Based Task Dialogs with a Visual Task Dialog Editor

Rate me:
Please Sign up or sign in to vote.
4.92/5 (67 votes)
19 Mar 200713 min read 100.7K   1.4K   90  
A library and a visual editor that make it easy to build task dialogs and use them in your C++ applications
#include "stdafx.h"
#include "resource.h"
#include "RadioButtonsToolsPage.h"
#include "EditRadioButtonDlg.h"

/////////////////////////////////////////////////////////////////////////////

struct CTDRadioButtonInfo
{
    int id;
    CString sMainText;
};

/////////////////////////////////////////////////////////////////////////////

CRadioButtonsToolsPage::CRadioButtonsToolsPage() :
    m_nNextButtonID(1500), m_bUseDefaultButton(true)
{
}

CRadioButtonsToolsPage::~CRadioButtonsToolsPage()
{
}


/////////////////////////////////////////////////////////////////////////////
// Message handlers

BOOL CRadioButtonsToolsPage::OnInitDialog ( HWND hwndFocus, LPARAM lParam )
{
    DoDataExchange();
    EnableThemeDialogTexture ( m_hWnd, ETDT_ENABLETAB );

    // Control initialization
    m_wndDefaultBtnCombo.AddString ( _S(IDS_DEFAULT_BTN_FIRST) );
    m_wndDefaultBtnCombo.SetCurSel(0);

    m_wndList.SetExtendedListViewStyle ( LVS_EX_FULLROWSELECT|LVS_EX_INFOTIP );

    m_wndList.InsertColumn ( 0, _S(IDS_BTN_LIST_COL1_HDR), LVCFMT_LEFT, 100, 0 );
    m_wndList.InsertColumn ( 1, _S(IDS_BTN_LIST_COL2_HDR), LVCFMT_LEFT, 100, 1 );

    return TRUE;
}


/////////////////////////////////////////////////////////////////////////////
// Command handlers

void CRadioButtonsToolsPage::OnAdd ( UINT uCode, int nID, HWND hwndCtrl )
{
CEditRadioButtonDlg dlg;

    dlg.m_id = m_nNextButtonID;

    if ( IDOK != dlg.DoModal ( m_hWnd ) )
        return;

CTDRadioButtonInfo* pInfo = new CTDRadioButtonInfo;
CString sID;
int nIdx;

    if ( NULL == pInfo )
        return;

    pInfo->id = dlg.m_id;
    pInfo->sMainText = dlg.m_sMainText;

    sID.Format ( _T("%d"), dlg.m_id );

    nIdx = m_wndList.InsertItem ( m_wndList.GetItemCount(), sID );
    m_wndList.SetItemText ( nIdx, 1, dlg.m_sMainText );
    m_wndList.SetItemData ( nIdx, (DWORD_PTR) pInfo );

    m_wndList.EnsureVisible ( nIdx, false );
    m_nNextButtonID = dlg.m_id + 1;

    // Insert this new button into the combo box
    AddButtonToComboBox ( pInfo->id, sID );

    GetTopLevelParent().SendMessage ( UWM_REBUILD_DLG );
}

void CRadioButtonsToolsPage::OnEdit ( UINT uCode, int nID, HWND hwndCtrl )
{
int nSelIdx = m_wndList.GetSelectedIndex();
CTDRadioButtonInfo* pInfo = NULL;
CEditRadioButtonDlg dlg;
CString sID;
int nOldID, nNewID;

    if ( -1 == nSelIdx )
        return;

    pInfo = (CTDRadioButtonInfo*) m_wndList.GetItemData ( nSelIdx );
    ATLASSERT(NULL != pInfo);

    nOldID = pInfo->id;
    dlg.m_id = pInfo->id;
    dlg.m_sMainText = pInfo->sMainText;

    if ( IDOK != dlg.DoModal ( m_hWnd ) )
        return;

    // Update the data struct and the list ctrl
    nNewID = dlg.m_id;
    pInfo->id = nNewID;
    pInfo->sMainText = dlg.m_sMainText;

    sID.Format ( _T("%d"), dlg.m_id );

    m_wndList.SetItemText ( nSelIdx, 0, sID );
    m_wndList.SetItemText ( nSelIdx, 1, dlg.m_sMainText );

    m_wndList.SetColumnWidth ( 0, LVSCW_AUTOSIZE_USEHEADER );
    m_wndList.EnsureVisible ( nSelIdx, false );

    // Update the button list combo box
    if ( nOldID != nNewID )
        {
        DeleteButtonFromComboBox ( nOldID );
        AddButtonToComboBox ( nNewID, sID );
        }

    GetTopLevelParent().SendMessage ( UWM_REBUILD_DLG );
}

void CRadioButtonsToolsPage::OnDelete ( UINT uCode, int nID, HWND hwndCtrl )
{
int nSelIdx = m_wndList.GetSelectedIndex();

    if ( -1 == nSelIdx )
        return;

CTDRadioButtonInfo* pInfo = (CTDRadioButtonInfo*) m_wndList.GetItemData ( nSelIdx );
int nButtonID = pInfo->id;

    ATLASSERT(NULL != pInfo);
    m_wndList.DeleteItem ( nSelIdx );
    DeleteButtonFromComboBox ( nButtonID );

    GetTopLevelParent().SendMessage ( UWM_REBUILD_DLG );
}

void CRadioButtonsToolsPage::OnClearAll ( UINT uCode, int nID, HWND hwndCtrl )
{
int i, cItems;

    m_wndList.DeleteAllItems();

    // Remove custom buttons from the combo box. If the combo's current selection
    // is a custom button, reset it to the first item in the combo.
    for ( cItems = m_wndDefaultBtnCombo.GetCount(), i = cItems - 1; i >= 0; i-- )
        {
        if ( m_wndDefaultBtnCombo.GetItemData(i) > 0 )
            {
            if ( m_wndDefaultBtnCombo.GetCurSel() == i )
                m_wndDefaultBtnCombo.SetCurSel(0);

            m_wndDefaultBtnCombo.DeleteString(i);
            }
        }

    GetTopLevelParent().SendMessage ( UWM_REBUILD_DLG );
}

void CRadioButtonsToolsPage::OnMoveUp ( UINT uCode, int nID, HWND hwndCtrl )
{
int nSelIdx = m_wndList.GetSelectedIndex();

    if ( nSelIdx < 1 )
        return;

    MoveListCtrlItem ( nSelIdx, nSelIdx - 1 );
    GetTopLevelParent().SendMessage ( UWM_REBUILD_DLG );
}

void CRadioButtonsToolsPage::OnMoveDown ( UINT uCode, int nID, HWND hwndCtrl )
{
int nSelIdx = m_wndList.GetSelectedIndex();

    if ( -1 == nSelIdx || nSelIdx == m_wndList.GetItemCount() - 1 )
        return;

    MoveListCtrlItem ( nSelIdx, nSelIdx + 1 );
    GetTopLevelParent().SendMessage ( UWM_REBUILD_DLG );
}

void CRadioButtonsToolsPage::OnResetIDs ( UINT uCode, int nID, HWND hwndCtrl )
{
int i, cItems, nNewID = 1500;

    // Remove custom buttons from the combobox
    for ( cItems = m_wndDefaultBtnCombo.GetCount(), i = cItems - 1; i >= 0; i-- )
        {
        if ( m_wndDefaultBtnCombo.GetItemData(i) > 0 )
            {
            if ( m_wndDefaultBtnCombo.GetCurSel() == i )
                m_wndDefaultBtnCombo.SetCurSel(0);

            m_wndDefaultBtnCombo.DeleteString(i);
            }
        }

    // Update the buttons' data structs and the list ctrl.
    for ( i = 0, cItems = m_wndList.GetItemCount(); i < cItems; i++ )
        {
        CString sID;
        CTDRadioButtonInfo* pInfo = (CTDRadioButtonInfo*) m_wndList.GetItemData(i);
        ATLASSERT(NULL != pInfo);

        sID.Format ( _T("%d"), nNewID );

        m_wndList.SetItemText ( i, 0, sID );
        pInfo->id = nNewID;
        nNewID++;

        int idx = m_wndDefaultBtnCombo.AddString ( sID );
        m_wndDefaultBtnCombo.SetItemData ( idx, pInfo->id );
        }

    GetTopLevelParent().SendMessage ( UWM_REBUILD_DLG );
}

void CRadioButtonsToolsPage::OnUseDefaultButton ( UINT uCode, int nID, HWND hwndCtrl )
{
    if ( !DoDataExchange(true) )
        return;

    m_wndDefaultBtnCombo.EnableWindow ( IsDlgButtonChecked ( nID ) );
    GetTopLevelParent().SendMessage ( UWM_SHOW_APPLY_FLAG );
}


/////////////////////////////////////////////////////////////////////////////
// Notification handlers

void CRadioButtonsToolsPage::OnComboboxSelchanged ( UINT uCode, int nID, HWND hwndCtrl )
{
    GetTopLevelParent().SendMessage ( UWM_SHOW_APPLY_FLAG );
}

LRESULT CRadioButtonsToolsPage::OnListDeleteitem ( NMHDR* phdr )
{
NMLISTVIEW* pnmlv = (NMLISTVIEW*) phdr;
CTDRadioButtonInfo* pInfo = (CTDRadioButtonInfo*) m_wndList.GetItemData ( pnmlv->iItem );

    if ( NULL != pInfo )
        delete pInfo;

    return 0;   // retval ignored
}

LRESULT CRadioButtonsToolsPage::OnListKeydown ( NMHDR* phdr )
{
NMLVKEYDOWN* pnmlv = (NMLVKEYDOWN*) phdr;

    if ( VK_DELETE == pnmlv->wVKey && m_wndList.GetSelectedIndex() != -1 )
        SendMessage ( WM_COMMAND, IDC_DELETE );

    return 0;   // retval ignored
}

LRESULT CRadioButtonsToolsPage::OnListDblclk ( NMHDR* phdr )
{
NMITEMACTIVATE* pnmia = (NMITEMACTIVATE*) phdr;

    if ( -1 != pnmia->iItem )
        SendMessage ( WM_COMMAND, IDC_EDIT );

    return 0;   // retval ignored
}


/////////////////////////////////////////////////////////////////////////////
// Operations

bool CRadioButtonsToolsPage::FillInTDConfig ( TASKDIALOGCONFIG& tdc )
{
    m_lsButtonText.clear();
    m_vecButtons.clear();

    for ( int i = 0; i < m_wndList.GetItemCount(); i++ )
        {
        TASKDIALOG_BUTTON rButtonInfo;
        CTDRadioButtonInfo* pInfo = (CTDRadioButtonInfo*) m_wndList.GetItemData(i);

        ATLASSERT(NULL != pInfo);

        m_lsButtonText.push_back ( pInfo->sMainText );

        rButtonInfo.nButtonID = pInfo->id;
        rButtonInfo.pszButtonText = m_lsButtonText.back();

        m_vecButtons.push_back ( rButtonInfo );
        }

    if ( !m_vecButtons.empty() )
        {
        tdc.cRadioButtons = m_vecButtons.size();
        tdc.pRadioButtons = &m_vecButtons[0];
        }

    if ( m_bUseDefaultButton )
        tdc.nDefaultRadioButton = m_wndDefaultBtnCombo.GetItemData ( m_wndDefaultBtnCombo.GetCurSel() );
    else
        tdc.dwFlags |= TDF_NO_DEFAULT_RADIO_BUTTON;

    return true;
}

void CRadioButtonsToolsPage::OnTDNavigated()
{
}

bool CRadioButtonsToolsPage::SaveToXML ( IXMLDOMDocumentPtr& pDoc, DWORD& dwFlags )
try
{
IXMLDOMElementPtr pButtons = pDoc->createElement ( "radio_buttons" );
int nDefaultID, i, cButtons;

    if ( !DoDataExchange(true) )
        return false;

    nDefaultID = (int) m_wndDefaultBtnCombo.GetItemData ( m_wndDefaultBtnCombo.GetCurSel() );

    if ( !m_bUseDefaultButton )
        pButtons->setAttribute ( "default", "none" );
    else if ( nDefaultID > 0 )
        pButtons->setAttribute ( "default", nDefaultID );

    for ( i = 0, cButtons = m_wndList.GetItemCount(); i < cButtons; i++ )
        {
        CTDRadioButtonInfo* pInfo = (CTDRadioButtonInfo*) m_wndList.GetItemData(i);
        IXMLDOMElementPtr pBtn = pDoc->createElement ( "button" );
        IXMLDOMElementPtr pText = pDoc->createElement ( "text" );

        ATLASSERT(NULL != pInfo);

        pBtn->setAttribute ( "id", pInfo->id );
        pText->text = (_bstr_t)(LPCTSTR) pInfo->sMainText;
        pBtn->appendChild ( pText );
        pButtons->appendChild ( pBtn );
        }

    pDoc->documentElement->appendChild ( pButtons );

    return true;
}
catch ( _com_error& e )
{
    (void) e;
    ATLTRACE(_T("Exception while building XML: %s\n"), e.ErrorMessage());
    return false;
}


/////////////////////////////////////////////////////////////////////////////
// Other methods

void CRadioButtonsToolsPage::MoveListCtrlItem ( int nOldIdx, int nNewIdx )
{
CTDRadioButtonInfo* pInfo = (CTDRadioButtonInfo*) m_wndList.GetItemData ( nOldIdx );
CString sID;

    ATLASSERT(NULL != pInfo);

    m_wndList.GetItemText ( nOldIdx, 0, sID );
    m_wndList.SetItemData ( nOldIdx, 0 );   // keep the struct from being free'd
    m_wndList.DeleteItem ( nOldIdx );

LVITEM item = { LVIF_TEXT|LVIF_STATE };

    item.iItem = nNewIdx;
    item.state = LVIS_SELECTED|LVIS_FOCUSED;
    item.stateMask = LVIS_SELECTED|LVIS_FOCUSED;
    item.pszText = sID.GetBuffer(0);

    m_wndList.InsertItem ( &item );
    m_wndList.SetItemText ( nNewIdx, 1, pInfo->sMainText );
    m_wndList.SetItemData ( nNewIdx, (DWORD_PTR) pInfo );
    m_wndList.EnsureVisible ( nNewIdx, false );
}

void CRadioButtonsToolsPage::AddButtonToComboBox ( int id, LPCTSTR szButtonName )
{
int i, cItems;

    for ( i = 0, cItems = m_wndDefaultBtnCombo.GetCount(); i< cItems; i++ )
        {
        DWORD_PTR nextID = m_wndDefaultBtnCombo.GetItemData(i);

        if ( id < (int) nextID )
            break;
        }

    if ( i == cItems )
        i = -1;

    i = m_wndDefaultBtnCombo.InsertString ( i, szButtonName );
    m_wndDefaultBtnCombo.SetItemData ( i, id );
}

void CRadioButtonsToolsPage::DeleteButtonFromComboBox ( int id )
{
int i, cItems, nCurSelIdx;

    nCurSelIdx = m_wndDefaultBtnCombo.GetCurSel();

    for ( i = 0, cItems = m_wndDefaultBtnCombo.GetCount(); i < cItems; i++ )
        {
        DWORD_PTR nextID = m_wndDefaultBtnCombo.GetItemData(i);

        if ( id == (int) nextID )
            {
            if ( i == nCurSelIdx )
                m_wndDefaultBtnCombo.SetCurSel(0);

            m_wndDefaultBtnCombo.DeleteString(i);
            return;
            }
        }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior) VMware
United States United States
Michael lives in sunny Mountain View, California. He started programming with an Apple //e in 4th grade, graduated from UCLA with a math degree in 1994, and immediately landed a job as a QA engineer at Symantec, working on the Norton AntiVirus team. He pretty much taught himself Windows and MFC programming, and in 1999 he designed and coded a new interface for Norton AntiVirus 2000.
Mike has been a a developer at Napster and at his own lil' startup, Zabersoft, a development company he co-founded with offices in Los Angeles and Odense, Denmark. Mike is now a senior engineer at VMware.

He also enjoys his hobbies of playing pinball, bike riding, photography, and Domion on Friday nights (current favorite combo: Village + double Pirate Ship). He would get his own snooker table too if they weren't so darn big! He is also sad that he's forgotten the languages he's studied: French, Mandarin Chinese, and Japanese.

Mike was a VC MVP from 2005 to 2009.

Comments and Discussions