////////////////////////////////////////////////////////////////////////////////
//
// 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;


//------------------------------------------------------------------------------
//  
// ---
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 );
  return 0;
}


//------------------------------------------------------------------------------
//  ,    
// ---
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);	
}


//----------------------------------------------------------------------------------------------
// 
// ---
PropertyManagerObject::PropertyManagerObject() 
  : m_procParam        ( NULL )  //  
  , m_propTabs         ( NULL )  //  
  , m_curentCollection ( NULL )  //   
  , m_paramGrid        ( NULL )  //       
  , m_slideBox         ( NULL )  //   
  , m_rowIndex         ( 0    )  //  
  , m_posLiaderParam   ( NULL )
  , m_flagMode         ( 0    )
{ 
}     


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


//----------------------------------------------------------------------------------------------
//   
// ---
bool PropertyManagerObject::InitProcessParam( long toolBarID, SpecPropertyToolBarEnum toolBarType, long firstTabID ) 
{
  bool res = false;
//  GetNewKompasAPI();
  if ( newKompasAPI )
  {
    try 
    { 
      m_procParam = newKompasAPI->CreateProcessParam();                 // newKompasAPI->CreateProcessParam();                 //   
      if ( m_procParam ) 
      {
        new PropertyManagerEvent( m_procParam, *this );                 //     
        
        //    
        m_procParam->SpecToolbar = toolBarType;                         //       
        _bstr_t tmpBstr( LoadStr( toolBarID ) );
        m_procParam->Caption     = tmpBstr;                             //     
        m_procParam->AutoReduce  = !m_flagMode;                           //  
         
        if ( m_flagMode ) 
        {
          m_procParam->DefaultControlFix = ksAllFix;
        }
        
        //     
        m_propTabs = m_procParam->PropertyTabs;                       
        
        //    
        if ( firstTabID ) 
        {
          CreateTab( firstTabID, TRUE, TRUE );
          //       
          ShowControls();
          
        }
        
        res = true;
      }
    }
    //  
    catch(_com_error &e)
    {
      DumpError(e); //    
    }    
    catch( LPCTSTR mes ) 
    {
      LibMessage( mes, MB_ICONERROR | MB_OK ); //    
    }  
    catch (...)
    {

    }
  }
  return res;
}


//----------------------------------------------------------------------------------------------
//    
// ---
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) ); //    
  }
}  


//----------------------------------------------------------------------------------------------
//    
// ---
const char * DoubleToStr( double value ) 
{
  static char res[20];
  ::sprintf( res, "%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)   );  //  
  }
}  


//----------------------------------------------------------------------------------------------
//  
// ---
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;
}


//----------------------------------------------------------------------------------------------
//  
// ---
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) : ""; 
    control->Hint           = tmpBstr;
    tmpBstr = tips ? LoadStr(tips) : ""; 
    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 PropertyManagerObject::EndProcess() 
{
  ABaseEvent::TerminateEvents( m_procParam );
  m_paramGrid        = NULL;  //       
  m_slideBox         = NULL;  //   
  m_curentCollection = NULL;  //   
  m_propTabs         = NULL;  //  
  m_procParam        = NULL;  //  
  m_posLiaderParam   = NULL;
  m_rowIndex         =    0;  //  
}


//----------------------------------------------------------------------------------------------
//  
// ---
IPropertyControlPtr PropertyManagerObject::GetPropertyControl( int ctrlID ) 
{
  IPropertyControlPtr control(NULL);

  if ( m_propTabs ) 
    for ( long i = 0, c = m_propTabs->Count; i < c && control == NULL; i++ ) 
    {
      IPropertyTabPtr tab( m_propTabs->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 ) 
    {
//       userCtrl->NameVisibility = ksNameHorizontalVisible;
//       _bstr_t conrolID( ::LoadStr(ctrlID + 2000) );
//       userCtrl->SetOCXControl( conrolID );
//       conrolID = ::LoadStr( ctrlID + 2001 )
//       userCtrl->Name   = conrolID; 
//       userCtrl->Height = height;
//       userCtrl->Width  = width; 
//        
//       new APropertyUserControlEvent( userCtrl, ctrlID, *this );
    }
  }

  return userCtrl;
}


//-------------------------------------------------------------------------------
//     
// ---
void PropertyManagerObject::OnDestroyOCX( long ctrlID ) 
{
//  if ( ctrlID == IDP_HATCH_PARAM )
//    hatchCtrl = NULL;
}


////////////////////////////////////////////////////////////////////////////////
//
// 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 ) 
{
//  char libHlp[260] = "";
//  if ( ::GetFullName( LIB_HELP, libHlp, 260 ) )
//    ::ksOpenHelpFile( libHlp, HELP_CONTEXT, Id );
}


//-----------------------------------------------------------------------------
// prChangeControlValue -     
// ---
BOOL PropertyManagerEvent::ButtonClick( long buttonID )
{
  if ( buttonID == pbHelp ) 
  {
    OpenHelp( 1 );
    return TRUE;
  }  

  return m_obj.OnButtonClick( buttonID );
}


//-----------------------------------------------------------------------------
// prControlCommand   
// ---
BOOL PropertyManagerEvent::ControlCommand( LPDISPATCH ctrl, long buttonID ) 
{
  m_obj.OnButtonClick(buttonID); 
  return TRUE;
}


//-----------------------------------------------------------------------------
// prButtonUpdate        -    .
// ---
BOOL PropertyManagerEvent::ButtonUpdate( long buttonID, long * check, 
                                         VARIANT_BOOL * enable ) 
{
  return m_obj.OnButtonUpdate( buttonID, check, enable );
}


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


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


//-------------------------------------------------------------------------------
//
// ---
BOOL PropertyManagerEvent::CommandHelp( long buttonID )
{
//  if ( !m_obj.OnCommandHelp(buttonID) )
//    OpenHelp( buttonID );

  return TRUE;
}


////////////////////////////////////////////////////////////////////////////////
//
// ATreeViewEvent  -    ocx 
//
////////////////////////////////////////////////////////////////////////////////

//-------------------------------------------------------------------------------
// 
// ---
ATreeViewEvent::ATreeViewEvent( ITreeViewPtr & tree, ATreeViewEventCallBack & obj )
  : ABaseEvent( tree, DIID_ITreeViewEvents )
  , m_obj( obj )
{
  Advise();
}

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

//-------------------------------------------------------------------------------
//  
// ---
BEGIN_EVENTSINK_MAP(ATreeViewEvent, ABaseEvent)
//  11   ON_EVENT(ATreeViewEvent, (unsigned int)-1, 1, BeforeLabelEdit, VTS_PI2 )  
//  11   ON_EVENT(ATreeViewEvent, (unsigned int)-1, 2, AfterLabelEdit,  VTS_PI2 VTS_PBSTR )
  ON_EVENT(ATreeViewEvent, (unsigned int)-1, 3, Collapse,        VTS_DISPATCH )
  ON_EVENT(ATreeViewEvent, (unsigned int)-1, 4, Expand,          VTS_DISPATCH )
  ON_EVENT(ATreeViewEvent, (unsigned int)-1, 5, NodeClick,       VTS_DISPATCH )
//  11   ON_EVENT(ATreeViewEvent, (unsigned int)-1, 6, NodeCheck,       VTS_DISPATCH )
END_EVENTSINK_MAP()
  

//  11 //-----------------------------------------------------------------------------
//  11 /// 
//  11 // ---
//  11 void ATreeViewEvent::BeforeLabelEdit ( short * Cancel )
//  11 {
//  11   if ( ::YesNo("  ?") == 0 )
//  11     *Cancel = true;
//  11 }
//  11   
//  11 
//  11 //-----------------------------------------------------------------------------
//  11 /// 
//  11 // ---
//  11 void ATreeViewEvent::AfterLabelEdit ( short * Cancel, BSTR * NewString )
//  11 {
//  11   if ( ::YesNo(" ?") == 0 )
//  11     *Cancel = true;
//  11 }
//  11   

//-----------------------------------------------------------------------------
/// 
// ---
void ATreeViewEvent::Collapse ( struct INode * Node )
{
  if ( Node )
    m_obj.OnExpandOrCollapse( Node, true );
}
  

//-----------------------------------------------------------------------------
/// 
// ---
void ATreeViewEvent::Expand ( struct INode * Node )
{
  if ( Node )
    m_obj.OnExpandOrCollapse( Node, false );
}
  

//-----------------------------------------------------------------------------
/// 
// ---
void ATreeViewEvent::NodeClick ( struct INode * Node )
{
  m_obj.OnNodeClick( Node );
}
 

//  11 //-----------------------------------------------------------------------------
//  11 /// 
//  11 // ---
//  11 void ATreeViewEvent::NodeCheck ( struct INode * Node )
//  11 {
//  11 }


//  11 ////////////////////////////////////////////////////////////////////////////////
//  11 //
//  11 // AToolBarCtrlEvent  -    ocx toolbar'a
//  11 //
//  11 ////////////////////////////////////////////////////////////////////////////////
//  11 
//  11 //-------------------------------------------------------------------------------
//  11 // 
//  11 // ---
//  11 AToolBarCtrlEvent::AToolBarCtrlEvent( LPDISPATCH ctrl )
//  11   : ABaseEvent( ctrl, DIID_IToolbarEvents )
//  11 {
//  11   Advise();
//  11 }
//  11 
//  11 //-------------------------------------------------------------------------------
//  11 // 
//  11 // ---
//  11 AToolBarCtrlEvent::~AToolBarCtrlEvent()
//  11 {
//  11 }
//  11 
//  11 //-------------------------------------------------------------------------------
//  11 //  
//  11 // ---
//  11 BEGIN_EVENTSINK_MAP(AToolBarCtrlEvent, ABaseEvent)
//  11   ON_EVENT(AToolBarCtrlEvent, (unsigned int)-1, 1, ButtonClick,     VTS_DISPATCH )  
//  11   ON_EVENT(AToolBarCtrlEvent, (unsigned int)-1, 2, Change,          VTS_NONE )
//  11   ON_EVENT(AToolBarCtrlEvent, (unsigned int)-1, 3, ButtonMenuClick, VTS_DISPATCH )
//  11   ON_EVENT(AToolBarCtrlEvent, (unsigned int)-1, 4, ButtonDropDown,  VTS_DISPATCH )
//  11 END_EVENTSINK_MAP()
//  11   
//  11 
//  11 //-----------------------------------------------------------------------------
//  11 /// 
//  11 // ---
//  11 void AToolBarCtrlEvent::ButtonClick ( struct IButton * Button )
//  11 {
//  11   ::Message("ToolBar ButtonClick");
//  11 }
//  11 
//  11 
//  11 //-----------------------------------------------------------------------------
//  11 /// 
//  11 // ---
//  11 void AToolBarCtrlEvent::Change()
//  11 {
//  11 }
//  11 
//  11 //-----------------------------------------------------------------------------
//  11 /// 
//  11 // ---
//  11 void AToolBarCtrlEvent::ButtonMenuClick ( struct IButtonMenu * ButtonMenu )
//  11 {
//  11   ::Message("ButtonMenuClick");
//  11 }
//  11 
//  11 
//  11 //-----------------------------------------------------------------------------
//  11 /// 
//  11 // ---
//  11 void AToolBarCtrlEvent::ButtonDropDown ( struct IButton * Button )
//  11 {
//  11 }
   
 
//  11 ////////////////////////////////////////////////////////////////////////////////
//
// ADVCTreeEvents          -    ocx  
//
////////////////////////////////////////////////////////////////////////////////

//-------------------------------------------------------------------------------
// 
// ---
ADVCTreeEvents::ADVCTreeEvents( _DVCTree * ctrl, ADVCTreeEventsCallBack & obj  )
  : ABaseEvent( ctrl, DIID__DVCTreeEvents )
  , m_obj( obj ) 
{
  Advise();
}

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

//-------------------------------------------------------------------------------
//  
// ---
BEGIN_EVENTSINK_MAP(ADVCTreeEvents, ABaseEvent)
  ON_EVENT(ADVCTreeEvents, (unsigned int)-1, 1, MenuCommand, VTS_I4 )  
END_EVENTSINK_MAP()
  

//-----------------------------------------------------------------------------
/// 
// ---
void ADVCTreeEvents::MenuCommand( long Id )
{
  m_obj.OnMenuCommand( Id );
}
  
