////////////////////////////////////////////////////////////////////////////////
//
//      
//
////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "resource.h"

#ifndef __ATLCONV_H__
#include "ATLCONV.H"
#endif

#ifndef __PROPMEN_H
#include "PropMen.h"
#endif

#ifndef __LIBTOOL_H
#include <libtool.h>
#endif 

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

//------------------------------------------------------------------------------
//
// ---
extern AFX_EXTENSION_MODULE StepDLL;


//------------------------------------------------------------------------------
//
// ---
extern IApplicationPtr pNewKompasAPI;
extern void GetNewKompasAPI();

//------------------------------------------------------------------------------
//
// ---
#define LIB_HELP _T("Gayka.hlp") //  


//------------------------------------------------------------------------------
//
// ---
LPCTSTR DoubleToStr( double value );      //  ,    
LPTSTR LoadStr( int id );                 //  ,    
int LibMessage( LPCTSTR str, int flags ); //  ,  
int LibMessage( int strId, int flags );   //  ,  
void DumpError( _com_error& e );          //  ,    
void ShowError();                         //  ,    


//----------------------------------------------------------------------------------------------
// 
// ---
PropertyManagerObject::PropertyManagerObject() :
  procParam        (NULL), //  
  propTabs         (NULL), //  
  curentCollection (NULL), //   
  paramGrid        (NULL), //       
  slideBox         (NULL), //   
  rowIndex         (0),    //  
  flagMode         (0)     //    -
{ 
}     


//----------------------------------------------------------------------------------------------
// 
// ---
PropertyManagerObject::~PropertyManagerObject() 
{  
  paramGrid        = NULL; //       
  slideBox         = NULL; //   
  curentCollection = NULL; //   
  propTabs         = NULL; //  
  procParam        = NULL; //  
  rowIndex         = 0;    //  
  flagMode         = 0;    //    -
}     


//----------------------------------------------------------------------------------------------
//   
// ---
bool PropertyManagerObject::InitProcessParam( long toolBarID, SpecPropertyToolBarEnum toolBarType, long firstTabID ) 
{
  bool res = false;
  GetNewKompasAPI();
  if ( pNewKompasAPI )
  {
    try 
    { 
      procParam = pNewKompasAPI->CreateProcessParam();                 // newKompasAPI->CreateProcessParam();                 //   
      if ( procParam ) 
      {
        new PropertyManagerEvent( procParam, *this );                 //     
        
        //    
        procParam->SpecToolbar = toolBarType;                         //       
        _bstr_t tmpBstr( LoadStr( toolBarID ) );
        procParam->Caption     = tmpBstr;                              //     
        procParam->AutoReduce  = !flagMode;                            //  

        if ( flagMode ) 
        {
          procParam->DefaultControlFix = ksAllFix;
        }

        //     
        propTabs = procParam->PropertyTabs;                       

        //    
        if ( firstTabID ) 
        {
          CreateTab( firstTabID, TRUE, TRUE );
          //       
          ShowControls();
          
          //   
          slideBox = curentCollection->Add( ksControlSlideBox );            
          slideBox->SlideType      = ksSlide;                 
          slideBox->DrawingSlide   = (long)GetSlideID(); 
          slideBox->Hint           = _T("Hint  ");
          slideBox->Tips           = _T("Tips  ");
          slideBox->Id             = 10000;
          slideBox->Name           = _T(" ");
          slideBox->NameVisibility = ksNameHorizontalVisible;
        }
   
        int  paramCount = ParamCount();                             //     ?
        if ( paramCount )
        {
          //  
          paramGrid = curentCollection->Add( ksControlGrid );
          if ( paramGrid ) 
          {
            paramGrid->ColumnCount      = 2;                        //   
            paramGrid->FixedColumnCount = 1;                        //     
            paramGrid->FixedRowCount    = 1;                        //  
            paramGrid->RowCount         = paramCount + 1;           //     
            paramGrid->AutoSizeColumns  = TRUE;                     //    
            paramGrid->Hint             = _T("Hint  ");
            paramGrid->Tips             = _T("Tips  ");
            paramGrid->Id               = 10001;
            paramGrid->Name             = _T(" ");
            paramGrid->NameVisibility   = ksNameHorizontalVisible;

            //  
            rowIndex = 0;  
            AddStringToGrig( IDS_PARAM_CAPTION, LoadStr(IDS_VALUE_CAPTION) );             
           
            //     
            ShowParam(); 
            
            //    
            paramGrid->Height = paramGrid->RowCount * 20;                           
          }
        }
        res = true;
      }
    }
    //  
    catch(_com_error &e)
    {
		  DumpError(e); //    
    }    
    catch( LPCTSTR mes ) 
    {
      LibMessage( mes, MB_ICONERROR | MB_OK ); //    
    }    
  }
  return res;
}


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


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


//----------------------------------------------------------------------------------------------
//  
// ---
bool PropertyManagerObject::CreateTab( long tabID, BOOL visible, BOOL active ) 
{
  bool res = false;
  if ( tabID ) 
  { 
    if ( propTabs ) 
    {                          
      _bstr_t tmpBstr( LoadStr(tabID) ); 
      IPropertyTabPtr propTab = propTabs->Add( tmpBstr );
      if ( propTab ) 
      {
        curentCollection = propTab->PropertyControls;
        if ( !visible )
          propTab->Visible = visible;
        propTab->Active = active;
        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) : _T(""); 
    control->Hint           = tmpBstr;
    tmpBstr = tips ? LoadStr(tips) : _T(""); 
    control->Tips           = tmpBstr;
    control->Enable         = enable;
    control->Visible        = visible;
    control->NameVisibility = nameVisibility;
  }
}


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

  return realList;
}


//----------------------------------------------------------------------------------------------
//     
// ---
IPropertyListPtr PropertyManagerObject::CreateStringList()
{
  IPropertyListPtr stringList( NULL );
  if ( curentCollection ) 
  {
    stringList = curentCollection->Add( ksControlListStr );
    if ( stringList ) 
       stringList->ReadOnly = TRUE;
  }

  return stringList;
}

//----------------------------------------------------------------------------------------------
//     
// ---
IPropertyListPtr PropertyManagerObject::CreateIntList()
{
  IPropertyListPtr intList( NULL );
  if ( curentCollection ) 
  {
    intList = curentCollection->Add( ksControlListInt );
    if ( intList ) 
       intList->ReadOnly = TRUE;
  }

  return intList;
}


//----------------------------------------------------------------------------------------------
//  
// ---
IPropertySeparatorPtr PropertyManagerObject::CreateSeparator( SeparatorTypeEnum type ) 
{
  IPropertySeparatorPtr rSepar( NULL );
  if ( curentCollection ) 
  {
    rSepar = curentCollection->Add( ksControlSeparator );
    if ( rSepar ) 
    {
      rSepar->SeparatorType = type;
    }
  }
  return rSepar;
}


//----------------------------------------------------------------------------------------------
//  CheckBox- 
// ---
IPropertyCheckBoxPtr PropertyManagerObject::CreateCheckBox( bool checked ) 
{
  IPropertyCheckBoxPtr check( NULL );
  if ( curentCollection ) 
  {
    check = curentCollection->Add( ksControlCheckBox );
    if ( check ) 
    {
      check->Value = _variant_t( checked );
    }
  }
  return check;
}


//----------------------------------------------------------------------------------------------
//   
// ---
IPropertyMultiButtonPtr PropertyManagerObject::CreateMultiButton( ButtonTypeEnum type )
{
  IPropertyMultiButtonPtr buttons( NULL );
  if ( curentCollection ) 
  {
    buttons = curentCollection->Add( ksControlMultiButton );
    if ( buttons ) 
    {
      buttons->ButtonsType = type;
      buttons->ResModule   = _variant_t( (long)StepDLL.hModule );
    }
  }
  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() 
{
  BaseEvent::TerminateEvents();
}


//----------------------------------------------------------------------------------------------
//  
// ---
IPropertyControlPtr PropertyManagerObject::GetPropertyControl( int ctrlID ) 
{
  IPropertyControlPtr control(NULL);
  if ( propTabs ) 
  {
    for ( long i = 0, c = propTabs->Count; i < c && control == NULL; i++ ) 
    {
      IPropertyTabPtr tab( 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), false );
  if ( control ) 
    control->Enable = enabled;
}


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


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

//-------------------------------------------------------------------------------
//  
// ---
BEGIN_EVENTSINK_MAP(PropertyManagerEvent, BaseEvent)
  // 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 )
END_EVENTSINK_MAP()


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


//-----------------------------------------------------------------------------
// prChangeControlValue -     
// ---
afx_msg BOOL PropertyManagerEvent::ButtonClick(long buttonID)
{
  if ( buttonID == pbHelp ) 
    ksOpenHelpFileT( LIB_HELP, HELP_CONTEXT, 1);
  return true;
}


//-----------------------------------------------------------------------------
// prControlCommand   
// ---
afx_msg BOOL PropertyManagerEvent::ControlCommand(LPDISPATCH ctrl, long buttonID ) 
{
  obj.OnButtonClick(buttonID); 
  obj.RedrawPhantom();  
  return true;
}
