////////////////////////////////////////////////////////////////////////////////
//
//     
//
////////////////////////////////////////////////////////////////////////////////
unit step12_1;

interface
uses
  LDefin2D, ksAuto, Libtool, Windows, ksApi7;

var
  newKompasAPI : IApplication;                                //  Application 7
////////////////////////////////////////////////////////////////////////////////

  procedure  LIBRARYENTRY( command: WORD  ); Pascal;
  function   LIBRARYNAME : PChar;            Pascal;
  function   LIBRARYID   : Cardinal;         Pascal;
  procedure  MyDLLProc(Reason: Integer);
  function   LibIsOnApplication7 : Integer;  stdcall;
  function   LibToolBarId( barType,                                //    ( 0 -  , 1 -    )
                           index : Integer ) : Integer; stdcall;   //  

  function   LibInterfaceNotifyEntry( kApp : PIDispatch ) : Integer; stdcall;

  procedure  ClosePropertyManager( mes : boolean );                //   
  procedure  CreateAndSubscriptionPropertyManager( mes : boolean );//   
  procedure  UpdateSlideBox;                                       //  
  procedure  UpdateSlideBox1( docRef : Reference );                //  
  procedure  AdviseDoc( pDoc : Reference );                      //  

  function   LoadStr( ID : Integer ) : string;                     //    
  function   LibMessage( str : string; flags : integer ) : Integer;//   
  function   LibMessageByID( strId, flags : Integer ) : Integer;   //   
  procedure  GetNewKompasAPI;

implementation

uses
  LtDefine,

  cBaseEvents,
  PropMen,
  ApplicationEvents,
  DocumentEvents, ksConstTLB,
  forms;

var
  propMng      : IPropertyManager;                            //   
  slideBox     : IPropertySlideBox;                           //       

  g_Rect   : TRect = ( Left : 20; Top : 20; Right : 270; Bottom : 320 );  //  


const

  g_Layout : PropertyManagerLayout = pmAlignRight; //pmFloating           //  

//------------------------------------------------------------------------------
// LibraryName
//---
procedure MyDLLProc(Reason: Integer);
begin
  if Reason = DLL_PROCESS_DETACH then begin
    cBaseEvent.TerminateEvents;
    ClosePropertyManager( false );
    newKompasAPI       := nil;
    Application.Handle := 0;
  end;
end;


//------------------------------------------------------------------------------
// LibraryName
//---
function LIBRARYNAME: PChar; pascal;
begin
  Result := '    (Delphi)';
end;

//------------------------------------------------------------------------------
// LibraryId
//---
function LIBRARYID: UINT; pascal;
begin
  Result := 100;  // IDR_LIB
end;

//------------------------------------------------------------------------------
// LibraryEntry
//---
procedure LIBRARYENTRY(command: WORD); pascal;
begin
  Application.Handle := GetHWindow;

  case command of
    1 : begin                                      //  1
          CreateAndSubscriptionPropertyManager( true );
        end;
    2 : begin                                      //  2
          ClosePropertyManager( true );
        end
  end;
end;

//-----------------------------------------------------------------------------
//      
// ---
function LibInterfaceNotifyEntry( kApp : PIDispatch ) : Integer; stdcall;
begin
  Application.Handle := GetHWindow;
  newKompasAPI := IDispatch(kApp) As IApplication;        //   
  if newKompasAPI <> nil then
    CreateAndSubscriptionPropertyManager( false ); //   
  Result := 0;
end;

//-------------------------------------------------------------------------------
//   API
// ---
function LibIsOnApplication7 : Integer; stdcall;
begin
  Result := 1;
end;

//-------------------------------------------------------------------------------
//      
// ---
function LibToolBarId( barType,                               //    ( 0 -  , 1 -    )
                       index : Integer ) : Integer; stdcall;  //  
begin
	if ( barType = 0 ) then
  begin
    if ( index = 0 ) then
      Result := 2000
    else
      Result := -1;
  end
	else
		Result := -1;
end;

//------------------------------------------------------------------------------
//   
//---
procedure  CreateAndSubscriptionPropertyManager( mes : boolean );
var
 tab        : IPropertyTab;
 tabs       : IPropertyTabs;
 control    : IPropertyControl;
 collection : IPropertyControls;
 docRef     : Reference;
begin
  GetNewKompasAPI;
  if ( newKompasAPI <> nil ) and ( propMng = nil ) then
    propMng := newKompasAPI.CreatePropertyManager( true ); 
  if ( propMng <> nil ) then
  begin
    NewApplicationEvent();
    propMng.Layout := g_Layout;
    propMng.Caption := '    Delphi';
    propMng.SetGabaritRect( g_Rect.left, g_Rect.top, g_Rect.right, g_Rect.bottom );
    propMng.SpecToolbar := pnEnterEscCreateSaveSearchHelp;
    NewPropertyManagerEvent( IDispatch(propMng) );
    tabs := propMng.PropertyTabs;
    tab  := tabs.Add('  ');

    //   
    collection := tab.PropertyControls;
    docRef     := ksGetCurrentDocument(0);
    AdviseDoc( docRef );
    //   
    control    := collection.Add( ksControlSlideBox );
    slideBox   := control As IPropertySlideBox;
    if slideBox <> nil then
    begin
      with slideBox do
      begin
        SlideType          := ksKompasDocument;             //  
        DrawingSlide       := docRef;                       //  reference 
        Value              := docRef <> 0;
        CheckBoxVisibility := docRef <> 0;
        Hint               := 'Hint  ';
        Tips               := 'Tips  ';
        Id                 := 10000;
        Name               := ' ';
        NameVisibility     := ksNameHorizontalVisible;
      end
    end;
    propMng.ShowTabs();
    collection := nil;
    tab := nil;
    tabs := nil;
  end
  else begin
    propMng.Visible := true;    //         
    if  mes then
      LibMessage( '  ', MB_OK );
  end
end;

//------------------------------------------------------------------------------
//   
//---
procedure ClosePropertyManager( mes : boolean );                          //   
begin
  if propMng <> nil then
  begin
    g_Layout := propMng.Layout;
    propMng.GetGabaritRect( g_Rect.Left, g_Rect.Top, g_Rect.Right, g_Rect.Bottom );
    cBaseEvent.TerminateEventsByParam( ntPropertyManagerNotify, 0, 0, nil );
    slideBox := nil;
    propMng.HideTabs();
    propMng := nil;
  end
  else
    if mes then
      LibMessage( '  ', MB_OK );
end;

//------------------------------------------------------------------------------
//  
//---
procedure  UpdateSlideBox;                                                 //  
begin
  if slideBox <> nil then
    slideBox.UpdateParam;
end;

//------------------------------------------------------------------------------
//  
//---
procedure  UpdateSlideBox1( docRef : Reference );                          //  
begin
  if slideBox <> nil then
  begin
    slideBox.DrawingSlide       := docRef;
    slideBox.Value              := docRef <> 0;
    slideBox.CheckBoxVisibility := docRef <> 0;
    slideBox.UpdateParam();
  end
end;

//  
procedure  AdviseDoc( pDoc : Reference );
var
  docType : Integer;
begin
	if pDoc = 0 then
		pDoc := ksGetCurrentDocument( 0 )
	else
    SetObjParam(pDoc, nil, 0, DOCUMENT_STATE);

	if pDoc <> 0 then
  begin
		docType := ksGetDocumentType( pDoc );
		if NewDocumentEvent( pDoc ) <> nil then
    begin
			case docType of
		  	lt_DocSheetStandart ,
			  lt_DocSheetUser     ,
			  lt_DocFragment      :
				  NewObj2DEvent( pDoc, ALL_OBJ );
      end
		end
	end
end;

//-------------------------------------------------------------------------------
//     API
// ---
procedure GetNewKompasAPI;
var
  disp : IDispatch;
begin
	if newKompasAPI = nil then
  begin
    disp := IDispatch(CreateKompasApplication);
    newKompasAPI := disp As IApplication;
  end
end;

//----------------------------------------------------------------------------------------------
//    
//---
function LoadStr( ID : Integer ) : string;
var
  buf : array [0..255] of char;
begin
  ksConvertLangStrEx( hInstance, ID, buf, 255 );
  Result := buf;
end;

//------------------------------------------------------------------------------
//  
// ---
function LibMessage( str : string; flags : integer ) : Integer;
var
 enabse : Integer;
begin
  enabse := IsEnableTaskAccess();  //  
  if enabse <> 0 then             //     
    EnableTaskAccess(0);          //  

    //                                      
  Result := Application.MessageBox( PChar(str), PChar(LoadStr(LIBRARYID)), flags );

  if enabse <> 0 then                         //      
    EnableTaskAccess(1);              //    


end;

//------------------------------------------------------------------------------
//  
// ---
function LibMessageByID( strId, flags : Integer ) : Integer;
begin
  Result := LibMessage( LoadStr(strId), flags );
end;

end.


