////////////////////////////////////////////////////////////////////////////////
//
// cPropMen.cpp -      
//
////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"

#include "cPropMen.h"


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

extern AFX_MODULE_STATE* pModuleState;
extern HINSTANCE g_hInstance;
extern IApplicationPtr appl;

CRect g_Rect( 20, 20, 270, 320 ); // 

PropertyManagerObject * PropertyManagerObject::macroObj = NULL;


//------------------------------------------------------------------------------
//  
// ---
int LibMessage( LPCTSTR str, int flags ) 
{
  int res = 0;
  
  if ( str && str[0] )                    //      
  {                  
    int enabse = ::IsEnableTaskAccess();  //  
    if ( enabse )                         //     
      ::EnableTaskAccess(0);              //  
    
    //                                      
    res = MessageBox( (HWND) GetHWindow(), str, LoadStr(IDR_LIBID), flags );
    
    if ( enabse )                         //      
      ::EnableTaskAccess(1);              //    
  }
  return res;
}


//------------------------------------------------------------------------------
//  
// ---
int LibMessage( int strId, int flags ) {
  return ::LibMessage( LoadStr(strId), flags );
}


//------------------------------------------------------------------------------
//  ,    
// ---
void DumpError(_com_error& e)
{
	_bstr_t bstrSource(e.Source());
	_bstr_t bstrDescription(e.Description());
	CString str;
	str.Format(_T("Error = %08lx"),   e.Error());
	str += _T("\nMessage     :"); str += e.ErrorMessage();
	str += _T("\nSource      :"); str += bstrSource;
	str += _T("\nDescription :"); str += bstrDescription;
  ::LibMessage( str, MB_OK | MB_ICONERROR);	
}


//-------------------------------------------------------------------------------
//
// ---
_bstr_t GetFeatureName( IEntity * entity )
{
  _bstr_t name(_T(""));
  IFeaturePtr feature( entity->GetFeature(), false );
  if ( feature )
    name = feature->GetName();
  return name;
}


//----------------------------------------------------------------------------------------------
// 
// ---
PropertyManagerObject::PropertyManagerObject() 
  : m_rowIndex         ( 0    )  //  
  , m_flagMode         ( 0    )
  , m_objectIndex      ( 0    )
{ 
}     


//----------------------------------------------------------------------------------------------
// 
// ---
PropertyManagerObject::~PropertyManagerObject() 
{  
  m_paramGrid        = NULL;  //       
  m_slideBox         = NULL;  //   
  m_curentCollection = NULL;  //   
  m_propTabs         = NULL;  //  
  m_procParam        = NULL;  //  
//    m_posLiaderParam   = NULL;
  m_rowIndex         =    0;  //  
}     


//----------------------------------------------------------------------------------------------
//    
// ---
void PropertyManagerObject::AddStringToGrig( long paramID, LPCTSTR value ) 
{
  if ( m_paramGrid ) 
  {
    m_paramGrid->CellText[m_rowIndex  ][1] = _bstr_t( value );
    m_paramGrid->CellText[m_rowIndex++][0] = _bstr_t( ::LoadStr(paramID) ); //    
  }
}  


//-------------------------------------------------------------------------------
//   
// ---
bool PropertyManagerObject::InitProcessParam()
{
  bool res = true;
  if ( m_procParam == NULL ) 
  {
    res = false;
    //    
    NewApplicationEvent();
    //   
    m_procParam = appl->CreateProcessParam();
    m_procParam->AutoReduce = false;     
    m_procParam->Caption = _bstr_t(LoadStr( IDS_CAPTION ));
    m_procParam->SpecToolbar = pnEnterEscHelp;
    //      
    new PropertyManagerEvent( m_procParam, *this );   
    IPropertyTabsPtr tabs( m_procParam->PropertyTabs );
    if ( tabs )
    { 
      // 
      IPropertyTabPtr tab( tabs->Add(_bstr_t(LoadStr(IDS_TAB_NAME))) );
      if ( tab )
      {
        //   
        m_curentCollection = tab->PropertyControls;
        reference docRef = ::ksGetCurrentDocument(0);
        //  Edit   
        IPropertyEditPtr diameter( CreateControl(ksControlEditReal) );
        if ( diameter )
        {  
          InitPropertyControl( diameter, ID_DIAMETER, ID_DIAMETER, ID_DIAMETER, TRUE, ksNameAlwaysVisible, TRUE );
          diameter->Value = GetDiameter();
        }
        
        CreateSeparator( ksSeparatorDownName );
        IPropertyMultiButtonPtr btn( CreateMultiButton(ksCheckButton) );
        if ( btn )
        {
          InitPropertyControl( btn, IDP_CHOICE1, 0, 0, TRUE, ksNameVerticalVisible );   
          AddButton( btn, ID_CHOICE1, true );
          btn->ButtonTips[ID_CHOICE1] = _bstr_t(LoadStr(ID_FACE1));
          btn->ButtonHint[ID_CHOICE1] = _bstr_t(LoadStr(ID_FACE1));
        }
        
        
        IPropertyEditPtr edit( m_curentCollection->Add(ksControlEditStr) );       //  1
        if ( edit )
        {
          InitPropertyControl( edit, ID_FACE1, ID_FACE1, ID_FACE1, TRUE, ksNameNoVisible );   
          edit->ReadOnly = TRUE;
          IEntityPtr face1( GetFace1() );
          if ( face1 )
            edit->PutValue( GetFeatureName(face1) );
        }
        CreateSeparator( ksSeparatorDownName );
        
        btn = CreateMultiButton(ksCheckButton);
        if ( btn )
        {
          InitPropertyControl( btn, IDP_CHOICE2, ID_FACE2, ID_FACE2, TRUE, ksNameVerticalVisible );   
          AddButton( btn, ID_CHOICE2, false );
          btn->ButtonTips[ID_CHOICE2] = _bstr_t(LoadStr(ID_FACE2));
          btn->ButtonHint[ID_CHOICE2] = _bstr_t(LoadStr(ID_FACE2));
        }
        
        edit = m_curentCollection->Add(ksControlEditStr);                         //  - 
        if ( edit )
        {
          InitPropertyControl( edit, ID_FACE2, ID_FACE2, ID_FACE2, TRUE, ksNameNoVisible );   
          edit->ReadOnly  = TRUE;
          IEntityPtr face2( GetFace2() );
          if ( face2 )
            edit->PutValue( GetFeatureName(face2) );
        }

        res = true;
      }
    }
  }
  return res;
}


//-------------------------------------------------------------------------------
//   
// ---
void PropertyManagerObject::CreatePropertyManager( bool mes )
{
//    if ( m_propMng == NULL ) 
//    {
//      //    
//      NewApplicationEvent();
//      //   
//      m_propMng = appl->CreatePropertyManager( TRUE/*FALSE*/ );
//      m_propMng->Layout = pmAlignRight;
//      m_propMng->Caption = _bstr_t(LoadStr( IDS_CAPTION ));
//      m_propMng->SetGabaritRect( g_Rect.left, g_Rect.top, g_Rect.right, g_Rect.bottom );
//      m_propMng->SpecToolbar = pnEnterEscHelp;
//      //      
//      new PropertyManagerEvent( m_propMng, *this );   
//      IPropertyTabsPtr tabs( m_propMng->PropertyTabs );
//      if ( tabs )
//      { 
//        // 
//        IPropertyTabPtr tab( tabs->Add(_bstr_t(LoadStr(IDS_TAB_NAME))) );
//        if ( tab )
//        {
//          //   
//          m_curentCollection = tab->PropertyControls;
//          reference docRef = ::ksGetCurrentDocument(0);
//          //  Edit   
//          IPropertyEditPtr diameter( CreateControl(ksControlEditReal) );
//          if ( diameter )
//          {  
//            InitPropertyControl( diameter, ID_DIAMETER, 0, 0, TRUE, ksNameAlwaysVisible, TRUE );
//            diameter->Value = GetDiameter();
//          }
//          //  Edit    1- 
//          IPropertyEditPtr face1( CreateControl(ksControlEditStr) );
//          if ( face1 )
//          {
//            InitPropertyControl( face1, ID_FACE1, 0, 0, FALSE, ksNameAlwaysVisible, TRUE );
//            IEntityPtr facet1( GetFace1() );
//            if ( facet1 )
//              face1->PutValue( GetFeatureName(facet1) );
//          }
//          //     1- 
//          IPropertyTextButtonPtr choise1( CreateControl(ksControlTextButton) );
//          if ( choise1 )
//            InitPropertyControl( choise1, ID_CHOICE1, 0, 0, TRUE, ksNameAlwaysVisible, TRUE );
//          //  Edit    2- 
//          IPropertyEditPtr face2( CreateControl(ksControlEditStr) );
//          if ( face2 )
//          {
//            InitPropertyControl( face2, ID_FACE2, 0, 0, FALSE, ksNameAlwaysVisible, TRUE );
//            IEntityPtr facet2( GetFace2() );
//            if ( facet2 )
//              face2->PutValue( GetFeatureName(facet2) );
//          }
//          //     2- 
//          IPropertyTextButtonPtr choise2( CreateControl(ksControlTextButton) );
//          if ( choise2 )
//            InitPropertyControl( choise2, ID_CHOICE2, 0, 0, TRUE, ksNameAlwaysVisible, TRUE );
//          m_propMng->ShowTabs();
//          ChoiceFaces( ID_FACE1, ID_FACE2 );
//        }
//      }
//    }
//    else {
//      //       ,  
//      m_propMng->Visible = true; 
//      if ( mes ) {
//        LibMessage( "  ", 0 );
//      }
//    }
}


//----------------------------------------------------------------------------------------------
//    
// ---
const TCHAR * DoubleToStr( double value ) 
{
  static TCHAR res[20];
  ::_stprintf( res, _T("%g"), value );
  return res;
}
  

//----------------------------------------------------------------------------------------------
//    
// ---
void PropertyManagerObject::AddDoubleToGrig( long paramID, double value ) 
{
  if ( m_paramGrid ) 
  {
    m_paramGrid->CellText[m_rowIndex  ][1] = _bstr_t( ::DoubleToStr(value) );  //  
    m_paramGrid->CellText[m_rowIndex++][0] = _bstr_t( ::LoadStr(paramID)   );  //  
  }
}  


//-------------------------------------------------------------------------------
//  ontrol  
// ---
IPropertyControlPtr PropertyManagerObject::CreateControl( ControlTypeEnum conType )
{
  IPropertyControlPtr control( NULL );
  if ( m_curentCollection ) 
  {
    control = m_curentCollection->Add( conType );
  }  
  return control;
}


//----------------------------------------------------------------------------------------------
//  
// ---
bool PropertyManagerObject::CreateTab( long tabID, BOOL visible, BOOL active ) 
{
  bool res = false;

  if ( tabID ) 
  { 
    if ( m_propTabs ) 
    {
      _bstr_t tmpBstr( ::LoadStr(tabID) ); 
      IPropertyTabPtr propTab = m_propTabs->Add( tmpBstr );

      if ( propTab ) 
      {
        m_curentCollection = propTab->PropertyControls;
        propTab->Active    = active;

        if ( !visible )
          propTab->Visible = visible;

        res = true;
      }
    }
  }

  return res;
}


//----------------------------------------------------------------------------------------------
//   ontrol'
// ---
void PropertyManagerObject::InitPropertyControl( IPropertyControl * control, long ctrlID, 
                                                 UINT hint, UINT tips, BOOL enable, 
                                                 PropertyControlNameVisibility nameVisibility, 
                                                 BOOL visible ) 
{
  if ( control ) 
  {
    _bstr_t tmpBstr( LoadStr(ctrlID) ); 
    control->Name           = tmpBstr;
    control->Id             = ctrlID;
    tmpBstr = hint ? LoadStr(hint) : _T(""); 
    control->Hint           = tmpBstr;
    tmpBstr = tips ? LoadStr(tips) : _T(""); 
    control->Tips           = tmpBstr;
    control->Enable         = enable;
    control->Visible        = visible;
    control->NameVisibility = nameVisibility;
  }
}


//----------------------------------------------------------------------------------------------
//    
// ---
IPropertyEditPtr PropertyManagerObject::CreateEditReal( double minVal, double maxVal )
{
  IPropertyEditPtr realEdit( NULL );
  
  if ( m_curentCollection ) 
  {
    realEdit = m_curentCollection->Add( ksControlEditReal );
  
    if ( realEdit ) 
    {
      if ( minVal != maxVal )
        realEdit->SetValueRange( _variant_t(minVal), _variant_t(maxVal) );
    }
  }

  return realEdit;
}


//----------------------------------------------------------------------------------------------
//  
// ---
IPropertyListPtr PropertyManagerObject::CreateList( double minVal, double maxVal )
{
  IPropertyListPtr realList( NULL );
  
  if ( m_curentCollection ) 
  {
    realList = m_curentCollection->Add( ksControlListReal );
  
    if ( realList ) 
    {
      if ( minVal != maxVal )
        realList->SetValueRange( _variant_t(minVal), _variant_t(maxVal) );
      realList->ReadOnly = TRUE;
    }
  }

  return realList;
}


//----------------------------------------------------------------------------------------------
//     
// ---
IPropertyListPtr PropertyManagerObject::CreateStringList( CString matVal )
{
  IPropertyListPtr realList( NULL );
  
  if ( m_curentCollection ) 
  {
    realList = m_curentCollection->Add( ksControlListStr );
		
    if ( realList ) 
    {
      realList->ReadOnly = TRUE;
    }
  }

  return realList;
}


//----------------------------------------------------------------------------------------------
//  
// ---
IPropertySeparatorPtr PropertyManagerObject::CreateSeparator( SeparatorTypeEnum type ) 
{
  IPropertySeparatorPtr rSepar( NULL );

  if ( m_curentCollection ) 
  {
    rSepar = m_curentCollection->Add( ksControlSeparator );

    if ( rSepar )
      rSepar->SeparatorType = type;
  }

  return rSepar;
}


//----------------------------------------------------------------------------------------------
//  CheckBox- 
// ---
IPropertyCheckBoxPtr PropertyManagerObject::CreateCheckBox( bool checked ) 
{
  IPropertyCheckBoxPtr check( NULL );

  if ( m_curentCollection ) 
  {
    check = m_curentCollection->Add( ksControlCheckBox );

    if ( check )
      check->Value = _variant_t( checked );
  }

  return check;
}


//----------------------------------------------------------------------------------------------
//  SpinEdit /* */
// ---
IPropertySpinEditPtr PropertyManagerObject::CreateSpinList( double minVal, double maxVal ) 
{
  IPropertySpinEditPtr realList( NULL );

  if ( m_curentCollection ) 
  {
    realList = m_curentCollection->Add( ksControlSpinInt );
    if ( realList ) 
		{
      if ( minVal != maxVal )
        realList->SetValueRange( _variant_t(minVal), _variant_t(maxVal) );
      realList->ReadOnly = FALSE;
    }
  }

  return realList;
}


//----------------------------------------------------------------------------------------------
//   
// ---
IPropertyMultiButtonPtr PropertyManagerObject::CreateMultiButton( ButtonTypeEnum type )
{
  IPropertyMultiButtonPtr buttons( NULL );
  
  if ( m_curentCollection ) 
  {
    buttons = m_curentCollection->Add( ksControlMultiButton );

    if ( buttons ) 
    {
      buttons->ButtonsType = type;
      buttons->ResModule   = _variant_t( (long)g_hInstance );
    }
  }

  return buttons;
}


//----------------------------------------------------------------------------------------------
//    
// ---
void PropertyManagerObject::AddButton( IPropertyMultiButtonPtr & buttons, long btnID, 
                                       bool cheched, bool enable )
{
  AddButton2( buttons, btnID, btnID, cheched, enable );
}


//----------------------------------------------------------------------------------------------
//    
// ---
void PropertyManagerObject::AddButton2( IPropertyMultiButtonPtr & buttons, long btnID, 
                                        long bmpID, bool cheched, bool enable )
{
  if ( buttons ) 
  {
    buttons->AddButton( btnID, _variant_t(bmpID), -1 );
    buttons->ButtonEnable [ btnID ] = enable;
    buttons->ButtonChecked[ btnID ] = cheched;
    _bstr_t tmpBstr( LoadStr(btnID) ); 
    buttons->ButtonTips   [ btnID ] = tmpBstr;
  }
}


//-------------------------------------------------------------------------------
//  
// ---
void UnSelectFaces( IEntityPtr face1, IEntityPtr face2 )
{
  IDocument3DPtr doc( ksGetActive3dDocument(), false );
  if ( doc )
  {
//      ISelectionMngPtr sel( doc->GetSelectionMng(), false );
//      if ( sel )
//      {
//        if ( face1 )
//          sel->Unselect( face1 );
//        if ( face2 )
//          sel->Unselect( face2 );
//      }  
    IChooseMngPtr chMng( doc->GetChooseMng(), false );
    if ( chMng )
    {
      chMng->UnChooseAll();
    }  
  } 
}


//----------------------------------------------------------------------------------------------
//  
// ---
void PropertyManagerObject::EndProcess() 
{
  //  
  IEntity * obj1 = GetFace1( false );
  IEntity * obj2 = GetFace1( false );
  UnSelectFaces( obj1, obj2 ); 

  //   
  ABaseEvent::TerminateEvents();
  //  
  delete PropertyManagerObject::macroObj;
  PropertyManagerObject::macroObj = NULL;
}


//----------------------------------------------------------------------------------------------
//  
// ---
IPropertyControlPtr PropertyManagerObject::GetPropertyControl( int ctrlID ) 
{
  IPropertyControlPtr control(NULL);
  IPropertyTabsPtr tabs( /*m_propMng ? m_propMng->PropertyTabs :*/ m_procParam ? m_procParam->PropertyTabs  :NULL );
  if ( tabs ) 
    for ( long i = 0, c = tabs->Count; i < c && control == NULL; i++ ) 
    {
      IPropertyTabPtr tab( tabs->Item[_variant_t(i)] );
      IPropertyControlsPtr ctrls( tab ? tab->PropertyControls : NULL );
      
      if ( ctrls ) 
      {
        _bstr_t name( ::LoadStr(ctrlID) );
        control = ctrls->Item[_variant_t(name)];
      }
    }

  return control;
}


//----------------------------------------------------------------------------------------------
//   
// ---
void PropertyManagerObject::SetControlEnable( long ctrlID, bool enabled ) 
{
  IPropertyControlPtr control( GetPropertyControl(ctrlID) );
  if ( control ) 
    control->Enable = enabled;
}


//----------------------------------------------------------------------------------------------
//   
// ---
IPropertyUserControlPtr PropertyManagerObject::CreateUserControl( long ctrlID, 
                                                                  long height, 
                                                                  long width ) 
{
  IPropertyUserControlPtr userCtrl( NULL );

  if ( m_curentCollection ) 
  {
    userCtrl = m_curentCollection->Add( ksControlUser );

    if ( userCtrl ) 
    {

    }
  }

  return userCtrl;
}


//-------------------------------------------------------------------------------
//     
// ---
void PropertyManagerObject::OnDestroyOCX( long ctrlID ) 
{

}


//-----------------------------------------------------------------------------
//    
// ---
void PropertyManagerObject::RepeatCommand() 
{
//    if ( m_propMng )
//      m_propMng->RepeatCommand();
}



////////////////////////////////////////////////////////////////////////////////
//
// PropertyManagerEvent -    
//
////////////////////////////////////////////////////////////////////////////////
//-------------------------------------------------------------------------------
//
// ---
PropertyManagerEvent::PropertyManagerEvent( LPDISPATCH              manager, 
                                            PropertyManagerObject & obj )
  : ABaseEvent( manager, DIID_ksPropertyManagerNotify )
  , m_obj( obj )
{
  Advise();
}


//-------------------------------------------------------------------------------
//
// ---
PropertyManagerEvent::~PropertyManagerEvent()
{
}


//-------------------------------------------------------------------------------
//  
// ---
BEGIN_EVENTSINK_MAP( PropertyManagerEvent, ABaseEvent )
  // 1 - prButtonClick        -  .
  ON_EVENT( PropertyManagerEvent, (unsigned int)-1, prButtonClick,        ButtonClick,        VTS_I4                   )
  // 2 - prChangeControlValue -    
  ON_EVENT( PropertyManagerEvent, (unsigned int)-1, prChangeControlValue, ChangeControlValue, VTS_DISPATCH             )
  // 3 - prControlCommand     -   
  ON_EVENT( PropertyManagerEvent, (unsigned int)-1, prControlCommand,     ControlCommand,     VTS_DISPATCH VTS_I4      )
  // 4 - prButtonUpdate       -    
  ON_EVENT( PropertyManagerEvent, (unsigned int)-1, prButtonUpdate,       ButtonUpdate,       VTS_I4 VTS_PI4 VTS_PBOOL )
  // 5 - prProcessActivate    -  
  ON_EVENT( PropertyManagerEvent, (unsigned int)-1, prProcessActivate,    ProcessActivate,    VTS_NONE                 )  
  // 6 - prProcessDeactivate  -                                                                    
  ON_EVENT( PropertyManagerEvent, (unsigned int)-1, prProcessDeactivate,  ProcessDeactivate,  VTS_NONE                 )  
  // 7 - prCommandHelp -                                                                                    
  ON_EVENT( PropertyManagerEvent, (unsigned int)-1, prCommandHelp,        CommandHelp,        VTS_I4                   )
END_EVENTSINK_MAP()


//-----------------------------------------------------------------------------
// prChangeControlValue -     
// ---
afx_msg BOOL PropertyManagerEvent::ChangeControlValue( LPDISPATCH  iCtrl )
{
  IPropertyControlPtr control( iCtrl );
  
  if ( control ) 
  {
    _variant_t v      = control->Value;
    long       ctrlID = control->Id;  
    m_obj.OnChangeControlValue( ctrlID, v );
  }
  return TRUE;
}


//-------------------------------------------------------------------------------
//   
// ---
void OpenHelp( int Id ) 
{

}



//-----------------------------------------------------------------------------
// prChangeControlValue -     
// ---
BOOL PropertyManagerEvent::ButtonClick( long buttonID )
{
  BOOL res = TRUE;
  //  
  if ( buttonID == pbEnter )
  {
    res = FALSE;
    IEntity * face1 = m_obj.GetFace1(false/*AddRef*/);
    IEntity * face2 = m_obj.GetFace2(false/*AddRef*/);
    if ( face1 && face2 && face1 != face2 )
    {
      m_obj.Create();
      res = TRUE;
    }  
  }

  //  
  if ( buttonID == pbHelp )
  {
    OpenHelp( 0 );
  } 
  return TRUE;
}


//-------------------------------------------------------------------------------
//   
// ---
BOOL WINAPI FilterProc( IEntity * entity )
{
  return entity && entity->IsIt(o3d_face);
}


int WINAPI SELECTCALLBACKPROC( IEntity * face, IRequestInfo * info ) 
{
  int res = 0;
  IDocument3DPtr doc( ksGetActive3dDocument(), false );
  if ( (bool)doc && face && face->IsIt( o3d_face ) )
  {
    //     
    IChooseMngPtr selectFace( doc->GetChooseMng(), false );
    if ( selectFace )
    {
      selectFace->UnChooseAll();
      //      Edit
      int index =  PropertyManagerObject::macroObj->m_objectIndex;
      IPropertyControlPtr edit = PropertyManagerObject::macroObj->GetPropertyControl( index ? ID_FACE2 : ID_FACE1 );
      if ( edit )
      { 
        edit->PutValue( GetFeatureName( face ) );
        if ( index == 1 )
        {
          PropertyManagerObject::macroObj->SetFace2( IEntityPtr(face) );
          if ( face == PropertyManagerObject::macroObj->GetFace1() )
            PropertyManagerObject::macroObj->SetFace1( IEntityPtr() );
        }
        else if ( index == 0 )
        {
          PropertyManagerObject::macroObj->SetFace1( IEntityPtr(face) );
          PropertyManagerObject::macroObj->m_objectIndex = 1;
          if ( face == PropertyManagerObject::macroObj->GetFace2() )
            PropertyManagerObject::macroObj->SetFace2( IEntityPtr() );
        }
        res = 1;
      }
      //   
      selectFace->Choose( PropertyManagerObject::macroObj->GetFace1() );
      selectFace->Choose( PropertyManagerObject::macroObj->GetFace2() );
    }
  }
  return res;
}

//-----------------------------------------------------------------------------
//
// ---
void ExecuteLibCommand( int command )
{
  if ( ::ReturnResult() )
    ::MessageBoxResult();
  
  ILibraryManagerPtr      manager( appl  ? appl->LibraryManager                       : NULL );
  IProceduresLibrariesPtr coll   ( manager ? manager->ProceduresLibraries                 : NULL );
  IProceduresLibraryPtr   lib    ( coll    ? coll->Item[_variant_t(::LoadStr( IDR_LIBID ))]: NULL );
  
  if ( lib )
    lib->Execute( command, NULL, TRUE/*post*/ );
}

long choiceID = 0;
//-------------------------------------------------------------------------------
//  
// ---
void PropertyManagerObject::ChoiceFaces()
{
  IEntityPtr face( NULL );
  //     
  IDocument3DPtr doc( ksGetActive3dDocument(), false );
  if ( doc )
  {
    IRequestInfoPtr rInfo( doc->GetRequestInfo(NULL), false );
    if ( rInfo )
    {
      rInfo->SetProcessParam( m_procParam );
      rInfo->SetFilterCallBack( FilterProc );
      rInfo->SetCallBack( SELECTCALLBACKPROC );

      doc->UserGetPlacementAndEntity( 0 );
    }  
  }
}


//-----------------------------------------------------------------------------
// prControlCommand   
// ---
BOOL PropertyManagerEvent::ControlCommand( LPDISPATCH ctrl, long buttonID ) 
{
  switch( buttonID )
  {
    //  1- 
    case ID_CHOICE1:
    {
      if ( PropertyManagerObject::macroObj ) //      
      {
        PropertyManagerObject::macroObj->m_objectIndex = 0;
        IPropertyMultiButtonPtr check( PropertyManagerObject::macroObj->GetPropertyControl(IDP_CHOICE2) );
        if ( check )
        {
          check->ButtonChecked[ID_CHOICE2] = false;
        }  
      }
      break;
    }
    
    //  2- 
    case ID_CHOICE2:
    {
      if ( PropertyManagerObject::macroObj ) //      
      {
        PropertyManagerObject::macroObj->m_objectIndex = 1;
        IPropertyMultiButtonPtr check( PropertyManagerObject::macroObj->GetPropertyControl(IDP_CHOICE1) );
        if ( check )
        {
          check->ButtonChecked[ID_CHOICE1] = false;
        }  
      }
      break;
    }
  }
  return TRUE;
}


//-----------------------------------------------------------------------------
// prButtonUpdate        -    .
// ---
BOOL PropertyManagerEvent::ButtonUpdate( long buttonID, long * check, 
                                         VARIANT_BOOL * enable ) 
{
  if ( buttonID == pbEnter && enable )
  {
    IEntity * face1 = m_obj.GetFace1(false/*AddRef*/);
    IEntity * face2 = m_obj.GetFace2(false/*AddRef*/);
    *enable = face1 && face2 && face1 != face2;
  }
  return TRUE;
}


//-----------------------------------------------------------------------------
// prProcessActibate     -  .
// ---
BOOL PropertyManagerEvent::ProcessActivate() 
{
  return TRUE;
}


//-----------------------------------------------------------------------------
// prProcessDeactivate   -  .
// ---
BOOL PropertyManagerEvent::ProcessDeactivate() 
{
  return TRUE;
}


//-------------------------------------------------------------------------------
//
// ---
BOOL PropertyManagerEvent::CommandHelp( long buttonID )
{
  OpenHelp( 0 );

  return TRUE;
}


