//  Beta release only.
//
//  Copyright(c) Ivyware Pty Ltd 2006  (all rights reserved)
//               MELBOURNE, VICTORIA, AUSTRALIA, 3130
//
//  Provided no fee is charged (either by the distributer or user)
//  permission to use, copy, modify, and distribute this software and
//  its documentation for any purpose and without fee is hereby granted,
//  provided that the above copyright notice appear in all copies and
//  that both that copyright notice and this permission notice appear
//  in supporting documentation.
//
//  No waranty or suitablity for purpose is implied.
//
//
//  Implementation for CComboBox extensions
//

#include "StdAfx.h"
#include "CComboBox_Ext.h"
#include "P2Pevent.h"

//
//  Fetches text for selected item in CComboBox
//
//  Parameters:  CComboBox oCComboBox
//               Control from which selected text is to be retrieved
//
//  Returns:     CString
//               Selected text
CString
CComboBox_GetCurSelText ( CComboBox& oCComboBox )
{
    CString strSelectedText;
    int idx = oCComboBox.GetCurSel ( );
    if ( idx >= 0 )
      oCComboBox.GetLBText ( idx, strSelectedText );
    return strSelectedText;
}

DWORD_PTR
CComboBox_GetCurSelData ( CComboBox& oCComboBox )
{
    CString strSelectedText;
    int idx = oCComboBox.GetCurSel ( );
    if ( idx >= 0 )
      return oCComboBox.GetItemData ( idx );
    return ~(DWORD_PTR)0;
}

//
//  Inserts item and associated data in CComboBox
//
//  Parameters:  CComboBox oCComboBox
//               Control into which item is to be inserted
//
//  Returns:     int
//               Inserted index as per CComboBox::InsertString()
int
CComboBox_InsertStringData ( CComboBox& oCComboBox
                           , LPCTSTR lpszString, DWORD_PTR dwItemData )
{
    int idx = oCComboBox.InsertString ( -1, lpszString );
    if ( idx >= 0 )
      idx = oCComboBox.SetItemData ( idx, dwItemData );
    return idx;
}

///////////////////////////////////////////////////////////////////////////////
//  CComboBox serialisation 

//
//  Serialises CComboBox contents
//
//  Parameters:  CComboBox& oCComboBox
//               Object to be serialised
//
//               P3PmsgNode& oNodeCombo
//               Serialisation node
//
//  Returns:     int
//               Entries serialised
P2PmsgMFC_EXT int
CComboBox_Serialise ( CComboBox& oCComboBox, P3PmsgNode& oNodeCombo )
{
    // Drop all existing entries
    oNodeCombo.Truncate ( );
    CString strLBText;
    for ( int i = 0; i < oCComboBox.GetCount(); i++ )
    {
      oCComboBox.GetLBText ( i, strLBText );
      P3PmsgData oData ( oCComboBox.GetItemData(i) );
      oNodeCombo += P3PmsgNode ( strLBText, oData );
    }
    // Current selection
    P3PmsgData oData(oCComboBox.GetCurSel());
    oNodeCombo.r_Attr().DeclareField(_T("CurSel"),oData);
    return oCComboBox.GetCount();
}
P2PmsgMFC_EXT int
CComboBox_Deserialise ( CComboBox& oCComboBox, P3PmsgNode& oNodeCombo )
{
    if ( oNodeCombo.GetCount() <= 0 )
      return 0;
    try
    {
      P3PmsgCurs oCurs(oNodeCombo);
      for ( int i = 0; oCurs.Goto(i); i++ )
        CComboBox_InsertStringData ( oCComboBox
                                   , oCurs.r_name().c_name(), 0 );
      P3PmsgField oCurSel = oNodeCombo.r_Attr().DeclareField(_T("CurSel"),(int)0).r_Object();
      if ( oCurSel.c_int() >= (int)oNodeCombo.GetCount() )
        oCurSel.c_int() = oNodeCombo.GetCount() - 1;
      oCComboBox.SetCurSel ( oCurSel.c_int() );
    }
    catch_pP2Pevent_Cancel
    catch_pCException_Cancel
    catch_ALL_Cancel
    return 0;
}
