www.pudn.com > GroupCombo_demo.zip > ComboGroup.cpp


// ComboGroup.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "ComboGroup.h" 
#include "DynamicCombo.h" 
#include "resource.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CComboGroup 
 
CComboGroup::CComboGroup() 
{ 
    pComboListHead = NULL; 
    pStrListHead = NULL; 
    numBoxesInGroup = 0; 
    numStringsAdded = 0; 
    // Insert "NOT SELECTED" string into all the combo boxes  
    AddSelectionString(_T(" NOT SELECTED")); 
} 
 
CComboGroup::~CComboGroup() 
{ 
    DeleteAllStrings(); 
} 
 
 
BEGIN_MESSAGE_MAP(CComboGroup, CStatic) 
	//{{AFX_MSG_MAP(CComboGroup) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CComboGroup message handlers 
 
/************************************************************* 
/* AddSelectionString(CString string)                        * 
/*                                                           * 
/* Add a string that selectable string for this group. Any   * 
/* combo box in the group can select this string provided    * 
/* no other box in the group has already selected it         * 
/* Creates the master string list.                           * 
/*************************************************************/ 
void CComboGroup::AddSelectionString(CString string) 
{ 
    CStrNode * pNode = pStrListHead; 
    if(pStrListHead == NULL) 
    { 
        pNode = pStrListHead = new CStrNode; 
    } 
    else 
    { 
        while(pNode->pNext!=NULL) 
        { 
            pNode = pNode->pNext; 
        } 
        pNode->pNext = new CStrNode; 
        pNode = pNode->pNext; 
    } 
     
    pNode->s = string; 
    numStringsAdded++; 
} 
 
/************************************************************* 
/* AddComboBox(CDynamicCombo* box)                           * 
/*                                                           * 
/* Add each of the combo boxes that will be in this group    * 
/*************************************************************/ 
void CComboGroup::AddComboBox(CDynamicCombo* box) 
{ 
    // Add a new dynamic combo box to the list and 
    // initialize the index, etc. and strings. 
    numBoxesInGroup++; 
    CString str = GetListString(numBoxesInGroup); 
    // Add the default string and the next choice 
    box->AddString(GetListString(0)); 
    box->AddString(str); 
    box->InitializeDynamicCombo(1, numBoxesInGroup, &str); 
    box->SetItemData(1, numBoxesInGroup); 
    box->SetOwner(this); 
    if(pComboListHead == NULL) 
    { 
        pComboListHead =  box; 
        pComboListHead->pNext = NULL; 
    } 
    else 
    { 
        CDynamicCombo* pLastNode = pComboListHead; 
        while(pLastNode->pNext != NULL) 
        { 
            pLastNode = pLastNode->pNext; 
        } 
        pLastNode->pNext = box; 
        pLastNode->pNext->pNext = NULL; 
    } 
     
} 
 
/************************************************************* 
/* GetListString(int strIndex)                               * 
/*                                                           * 
/* Get the string indicated by the master string list        * 
/*************************************************************/ 
CString CComboGroup::GetListString(int strIndex) 
{ 
    CStrNode * pNode = pStrListHead; 
    for(int i = 0; ipNext; 
    } 
         
    return pNode->s; 
} 
 
 
void CComboGroup::SelChanged(CDynamicCombo* box) 
{ 
    CString str; 
 
    if(box->currentIndex != 0) 
    { 
        //Previous selection was a valid selection 
        MakeSelectionStringAvail(&box->currentString, box->boxNum); 
    } 
    if(box->GetCurSel() != 0) 
    { 
        //New selection is valid selection 
        box->GetLBText(box->GetCurSel(), str); 
        MakeSelectedStringUnAvail(&str, box->boxNum); 
    } 
    box->currentIndex = box->GetCurSel(); 
    box->currentString = str; 
 
     
} 
 
void CComboGroup::MakeSelectionStringAvail(CString *str, int excludeBox) 
{ 
    CDynamicCombo* box = pComboListHead; 
    while(box!=NULL) 
    { 
        if(box->boxNum!=excludeBox) 
        { 
            box->AddString(*str); 
        } 
        box = box->pNext; 
    } 
} 
 
void CComboGroup::MakeSelectedStringUnAvail(CString *str, int excludeBox) 
{ 
    CDynamicCombo* box = pComboListHead; 
    while(box!=NULL) 
    { 
        if(box->boxNum!=excludeBox) 
        { 
            box->DeleteString(box->FindString(-1, *str)); 
        } 
        box=box->pNext; 
    } 
} 
 
void CComboGroup::DeleteAllStrings() 
{ 
    CStrNode * pNode = pStrListHead; 
    while(pNode != NULL) 
    { 
        pStrListHead = pNode->pNext; 
        delete pNode; 
        pNode = pStrListHead; 
    }     
} 
 
/************************************************************* 
/* AllValuesSelected()                                       * 
/*                                                           * 
/* Verify that all the Combo boxes have selected a value     * 
/* and that none have selected " NOT SELECTED"               * 
/*************************************************************/ 
bool CComboGroup::AllValuesSelected() 
{ 
    CDynamicCombo* box = pComboListHead; 
    while(box!=NULL) 
    { 
        if(box->GetCurSel() == 0) return false; 
        box = box->pNext; 
    } 
    return true; 
} 
 
/************************************************************* 
/* AddRemainingStrings()                                     * 
/*                                                           * 
/* Add any strings to the Combo boxes that have not been     * 
/* selected, but are in the Master List                      * 
/*************************************************************/ 
void CComboGroup::AddRemainingStrings() 
{ 
    CDynamicCombo* box = pComboListHead; 
     
    while(box!=NULL) 
    { 
        for(int startIndex = numStringsAdded-(numStringsAdded - numBoxesInGroup)+1; startIndexAddString(GetListString(startIndex)); 
        } 
        box = box->pNext; 
    } 
} 
 
/************************************************************* 
/* GetBoxSelStr(int BoxNum)                                  * 
/*                                                           * 
/* Return the string that is currently selceted by a         * 
/* combo box                                                 * 
/*************************************************************/ 
CString CComboGroup::GetBoxSelStr(int BoxNum) 
{ 
    CString str = ""; 
    CDynamicCombo* box = pComboListHead; 
    while(box->boxNum != BoxNum) 
    { 
        box = box->pNext; 
    } 
    box->GetLBText(box->GetCurSel(), str); 
    return str; 
} 
 
/************************************************************* 
/* GetCurIndex(int BoxNum)                                   * 
/*                                                           * 
/* Return the index number of the currently selected string  * 
/* for a specified combo box                                 * 
/*************************************************************/ 
UINT CComboGroup::GetCurIndex(int BoxNum) 
{ 
    CString s = GetBoxSelStr(BoxNum); 
    CStrNode * node = pStrListHead; 
    int i = 0; 
    if(node != NULL) 
    { 
        while(node->s != s && node != NULL) 
        { 
            node = node->pNext; 
            i++; 
        } 
    } 
    return i;         
}