// cpacdgol.cpp : implementation file
//

#include "stdafx.h"

#include "EDAcIRes.h"
#include "EDACdgol.hpp"
#include "cpa_nll.hpp"
#include "EDACstrg.hpp"
#include "EDACinst.hpp"

#undef CPA_WANTS_IMPORT
#undef CPA_EXPORT
#define CPA_WANTS_EXPORT
    #include "_Actors.hpp"
#undef CPA_WANTS_EXPORT
#define CPA_WANTS_IMPORT

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

/////////////////////////////////////////////////////////////////////////////
// EdActors_EditObjectListsDialog dialog


EdActors_EditObjectListsDialog::EdActors_EditObjectListsDialog(tdstCarEditObjectListsDialogArgs *p_stArgs, CWnd* pParent /*=NULL*/)
	: CDialog(EdActors_EditObjectListsDialog::IDD, &g_oBaseFrame)
{
 m_p_stArgs = p_stArgs;
	//{{AFX_DATA_INIT(EdActors_EditObjectListsDialog)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
 //bbb 25/03/97 {
 // create font
 PLOGFONT plf = (PLOGFONT) LocalAlloc(LPTR, sizeof(LOGFONT));
 lstrcpy(plf->lfFaceName, "Times New Roman");
 plf->lfWeight = FW_BOLD ;
 plf->lfHeight = 36;
 plf->lfEscapement = 0;
 m_oBoldFont.CreateFontIndirect(plf);
 //bbb 25/03/97 }
}

//bbb 25/03/97 {
EdActors_EditObjectListsDialog::~EdActors_EditObjectListsDialog()
{
 m_oBoldFont.DeleteObject();
}
//bbb 25/03/97 }


void EdActors_EditObjectListsDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(EdActors_EditObjectListsDialog)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(EdActors_EditObjectListsDialog, CDialog)
	//{{AFX_MSG_MAP(EdActors_EditObjectListsDialog)
	ON_BN_CLICKED(IDC_BUTTON_OLC_DEFAULT, OnOlcSetDefaultButtonClicked)
	ON_BN_CLICKED(IDC_BUTTON_OLC_RENAME, OnOlcRenameButtonClicked)
	ON_CBN_SELCHANGE(IDC_COMBO_OLC_OBJLISTS, OnOlcObjListsComboSelChange)
	ON_EN_CHANGE(IDC_EDIT_OLC_NAME, OnOlcEditNameChange)
	ON_WM_DRAWITEM()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// EdActors_EditObjectListsDialog message handlers

void EdActors_EditObjectListsDialog::OnOlcSetDefaultButtonClicked()
{
 // set the default value
 m_p_stArgs->stOut.p_oDefaultSelectedObjectList = m_p_stArgs->stOut.p_oLastSelectedObjectList;
 //tell the list that the actor's model uses this list as default
 m_p_stArgs->stOut.p_oDefaultSelectedObjectList->fn_bMakeActorUseThisList(
  (EdActors_EditorActorInstance *) m_p_stArgs->stIn.p_oEditorActor
  ->m_fn_pclGetModel()->m_fn_pclGetEditorActor()
 );
 // and disable the button since the values are equal
 m_p_oListSetDefaultButton->EnableWindow(FALSE);
}

void EdActors_EditObjectListsDialog::OnOlcRenameButtonClicked()
{
	//normally the "change name" button is pressed only when relevant.
 CString oNewListName;

 //which entry is seleted ?
 short wIndex = m_p_oListNameCombo->GetCurSel();
 // get the data pointer (pointer on the EdtList reachable object)
 EdtList *p_oDataPtr = (EdtList *) m_p_oListNameCombo->GetItemDataPtr(wIndex);

 //read the new name in the edit
 m_p_oListNameEdit->GetWindowText(oNewListName);

 //tell the object that its name changed.
 if ( p_oDataPtr->fn_eRename(M_MAKECHAR(oNewListName)) == E_mc_None )
 { // if the object was successfully renamed
  // change the text in the item of the combo too
  m_p_oListNameCombo->DeleteString(wIndex);
  m_p_oListNameCombo->InsertString(wIndex, M_MAKECHAR(oNewListName));
  m_p_oListNameCombo->SetItemDataPtr(wIndex, (void *) p_oDataPtr);
  m_p_oListNameCombo->SetCurSel(wIndex);

  //since the combo and edit display the same text, "rename" is disabled
  m_p_oListRenameButton->EnableWindow(FALSE);
  //but the "set default" button may be enabled if the entry is not already the default one
  m_p_oListSetDefaultButton->EnableWindow(
   m_p_stArgs->stOut.p_oLastSelectedObjectList != m_p_stArgs->stOut.p_oDefaultSelectedObjectList
  );
 }
}

void EdActors_EditObjectListsDialog::OnOlcObjListsComboSelChange()
{
	// TODO: Add your control notification handler code here
 short wIndex = m_p_oListNameCombo->GetCurSel();

 if ( wIndex == CB_ERR )
 {
  //no list is selected for the actor
  m_p_stArgs->stOut.p_oLastSelectedObjectList = NULL;
  //disable all controls in the dialog
  m_p_oListNameEdit->EnableWindow(FALSE);
  m_p_oListNameEdit->SetWindowText("");
  m_p_oListRenameButton->EnableWindow(FALSE);
  m_p_oListSetDefaultButton->EnableWindow(FALSE);
 }
 else
 {
  //this list is selected for the actor
  m_p_stArgs->stOut.p_oLastSelectedObjectList = (class EdtList *) m_p_oListNameCombo->GetItemDataPtr(wIndex);
  //put the name in the edit
  CString oSelectedName;
  m_p_oListNameCombo->GetLBText(wIndex, oSelectedName);
  m_p_oListNameEdit->SetWindowText(oSelectedName);
  // since the names are equal, the "rename" button is disabled
  m_p_oListRenameButton->EnableWindow(FALSE);
  // the "default" button is enabled only if selected list is not already marked as default
  m_p_oListSetDefaultButton->EnableWindow(
   m_p_stArgs->stOut.p_oLastSelectedObjectList != m_p_stArgs->stOut.p_oDefaultSelectedObjectList
  );
 }

}

void EdActors_EditObjectListsDialog::OnOlcEditNameChange()
{
 CString oNewNameString;
 m_p_oListNameEdit->GetWindowText(oNewNameString);

 //disable the "rename" and "set default" button if the text is empty
 if ( oNewNameString.IsEmpty() )
 {
  m_p_oListRenameButton->EnableWindow(FALSE);
  m_p_oListSetDefaultButton->EnableWindow(FALSE);
 }
 else // there is text in the edit
 {
  short wNbLists = m_p_oListNameCombo->GetCount();
  if ( wNbLists > 0 ) //if the combo is not empty
  {
   CString oComboString;

   for ( short wIndex = 0; wIndex < wNbLists; wIndex ++ )
   {
    // if we find a name in the combo that is the string in the edit,
    // the "rename" button is disabled, but the "set default" may be enabled
    // if the name is of a list that is not set as default
    m_p_oListNameCombo->GetLBText(wIndex, oComboString);
    if ( oComboString == oNewNameString )
    {
     m_p_oListRenameButton->EnableWindow(FALSE);
     m_p_oListSetDefaultButton->EnableWindow(
      m_p_stArgs->stOut.p_oLastSelectedObjectList != m_p_stArgs->stOut.p_oDefaultSelectedObjectList
     );
     return;
    }
   }

   //here, there are items in the combo, that are different from the edit

   //the names in the combo and the edit must match to enable "set default"
   m_p_oListSetDefaultButton->EnableWindow(FALSE);

   short wSelectedIndex = m_p_oListNameCombo->GetCurSel();
   if ( wSelectedIndex != CB_ERR ) //if an item is selected in the combo
    m_p_oListRenameButton->EnableWindow(TRUE);
   else //no item is selected in the combo
    m_p_oListRenameButton->EnableWindow(FALSE);
  }
  else //the combo is empty, but the edit is not: buttons are disabled
  {
   m_p_oListRenameButton->EnableWindow(FALSE);
   m_p_oListSetDefaultButton->EnableWindow(FALSE);
  }
 }
}

BOOL EdActors_EditObjectListsDialog::OnInitDialog()
{
 short wIndex, wOk, wInstanceIndex = 0;
 short wNbObjectLists = 0; //bbb 25/03/97

 CDialog::OnInitDialog();

 m_p_stArgs->stOut.p_oLastSelectedObjectList = NULL;
 m_p_stArgs->stOut.p_oDefaultSelectedObjectList = m_p_stArgs->stIn.p_oDefaultSelectedObjectList;

 m_p_oListNameEdit           = (CEdit *)     GetDlgItem(IDC_EDIT_OLC_NAME);
 m_p_oListNameCombo          = (CComboBox *) GetDlgItem(IDC_COMBO_OLC_OBJLISTS);
 m_p_oListRenameButton       = (CButton *)   GetDlgItem(IDC_BUTTON_OLC_RENAME);
 m_p_oListSetDefaultButton   = (CButton *)   GetDlgItem(IDC_BUTTON_OLC_DEFAULT);
 //bbb 25/03/97 {
 m_p_oStaticTitle            = (CStatic *)   GetDlgItem(IDC_STATIC_OLC_LISTNAMES);
 m_p_oButtonMessage          = (CButton *)   GetDlgItem(IDC_BUTTON_OLC_NOOBJECTLIST);
 //bbb 25/03/97 }

 //bbb 04/03/97 tout le paté
 //ask the name list for the list of lists of objects it handles
 CPA_tdoNameList *p_oNameList =
((CPA_Editor_ObjectListData *) m_p_stArgs->stIn.p_oEditorActor
  ->m_fn_p_oFindDataByName(m_p_stArgs->stIn.p_c_szObjectListType))->m_p_oNameList;
 CPA_List<EdtList> *p_oListOfObjectLists = NULL;
 if ( p_oNameList ) //bbb 25/03/97 tout dedans
 {
  p_oListOfObjectLists = p_oNameList->m_pEdtListHandler->fn_oGetListOfEdtLists();
  wNbObjectLists = p_oListOfObjectLists->GetCount();
 }

 wOk = 0;
 m_p_oListNameCombo->ResetContent();
 if ( p_oListOfObjectLists && wNbObjectLists ) //bbb 25/03/97 test
 {
  //short wNbObjectLists = p_oListOfObjectLists->GetCount(); //bbb 25/03/97

  for ( wIndex = 0; wIndex < wNbObjectLists; wIndex ++ )
  {
   //get the list of objects
   EdtList *p_oObjectList = (EdtList *) p_oListOfObjectLists->FindElementFromIndex(wIndex);
   if ( p_oObjectList->fn_bIsOfType(C_szEdtListTypeName) )  //TODO-B 21/02/97: a virer + tard...
   {
    wOk = 1;
    //add its name in the combo
    short wPos = m_p_oListNameCombo->AddString(p_oObjectList->GetName());
    //set the pointer
    m_p_oListNameCombo->SetItemDataPtr(wPos, p_oObjectList);

    if ( p_oObjectList == m_p_stArgs->stIn.p_oCurrentObjectList )
     wInstanceIndex = wIndex;
   }
  }
 } //bbb 05/03/97 fin du test
 //bbb 25/03/97 {
 else //there is no object list
 {
  //hide all object-list related controls
  m_p_oListNameCombo->ShowWindow(FALSE);
  m_p_oListNameEdit->ShowWindow(FALSE);
  m_p_oListRenameButton->ShowWindow(FALSE);
  m_p_oListSetDefaultButton->ShowWindow(FALSE);
  m_p_oStaticTitle->ShowWindow(FALSE);
  m_p_oButtonMessage->ShowWindow(TRUE);
 }
 //bbb 25/03/97 }
 // clear the pointer to avoid later mistakes
 p_oListOfObjectLists = NULL;

 if ( wOk )
 {
  //these controls have to be enabled/disabled
  m_p_oListNameCombo->EnableWindow(TRUE);
  m_p_oListNameEdit->EnableWindow(TRUE);
  //we will put the object list name in the edit, hence "rename" is disabled
  m_p_oListRenameButton->EnableWindow(FALSE);
  //just to be sure, because it will be set properly by OnOlcObjListsComboSelChange()
  m_p_oListSetDefaultButton->EnableWindow(FALSE);
  //make the combo list the entry used is the instance
  m_p_oListNameCombo->SetCurSel(wInstanceIndex);
  OnOlcObjListsComboSelChange();
 }

 return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

//bbb 25/03/97 {
void EdActors_EditObjectListsDialog::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	if ( nIDCtl == IDC_BUTTON_OLC_NOOBJECTLIST )
 {
  CDC         *pDC = CDC::FromHandle(lpDrawItemStruct->hDC);

  //fill the rectangle with a solid color
  pDC->FillSolidRect( &lpDrawItemStruct->rcItem, pDC->GetBkColor() );
  //set the text color and font
  COLORREF    xTextColor = GetSysColor(COLOR_ACTIVECAPTION);
  CFont       *xFntPrev = pDC->SelectObject( &m_oBoldFont );
  pDC->SetTextColor(xTextColor);
  //draw the text
  CString csButtonText = "There is no list\nto select";
  pDC->DrawText(csButtonText, &lpDrawItemStruct->rcItem, DT_CENTER | DT_VCENTER );
  //restore the previous font
  pDC->SelectObject( xFntPrev );
 }
 else
 	CDialog::OnDrawItem(nIDCtl, lpDrawItemStruct);
}
//bbb 25/03/97 }
