unit ApplicationEvents;

interface
uses
  Windows,
  Libtool,
  LDefin2D,
  classes,
  cBaseEvents,
  ks2DCOM_TLB,
  ksConstTLB,
  ComObj,
  ActiveX,
  step12_1,
  LtDefine;

type

PApplicationEvent = ^ApplicationEvent;
ApplicationEvent = class (cBaseEvent, IKompasObjectNotify )
public
  constructor Create(); overload;
  destructor  Destroy(); override;

  function    Advise(): boolean; override;

  //    true   notifyType  
  function IsNotifyProcess(notifyType: SYSINT): WORDBOOL; virtual; stdcall;
  // koCreateDocument -   
  function CreateDocument( pDoc : Reference; docType : Integer ) : WORDBOOL; virtual; stdcall;
     // koOpenDocumenBegin -   
  function BeginOpenDocument( fileName: PChar ) : WORDBOOL; virtual; stdcall;
  // koOpenDocumen -  
  function OpenDocument( newDoc : Reference; docType : integer ) : WORDBOOL; virtual; stdcall;
  // koActiveDocument -     
  function ChangeActiveDocument( newDoc : Reference; docType : integer ) : WORDBOOL; virtual; stdcall;
  // koApplicatinDestroy -  
  function ApplicationDestroy() : WORDBOOL; virtual; stdcall;
  // koBeginCreate -   
  function BeginCreate( docType: Integer ) : WORDBOOL; virtual; stdcall;
  // koBeginOpenFile -   
  function BeginOpenFile() : WORDBOOL; virtual; stdcall;
  // koBeginCloseAllDocument -   
  function BeginCloseAllDocument() : WORDBOOL; virtual; stdcall;
  // koKeyDown
  function  KeyDown(var key: Integer; flags: Integer; sysKey: WordBool): WordBool; virtual; stdcall;
  // koKeyUp
  function  KeyUp(var key: Integer; flags: Integer; sysKey: WordBool): WordBool; virtual; stdcall;
  // koKeyPress
  function  KeyPress(var key: Integer; sysKey: WordBool): WordBool; virtual; stdcall;
  // koBeginRequestFiles
  function  BeginRequestFiles(requestID: Integer; var files: OleVariant): WordBool; virtual; stdcall;

end;

function NewApplicationEvent() : ApplicationEvent;

implementation

function ApplicationEvent.IsNotifyProcess( notifyType: SYSINT ) : WORDBOOL;
begin
  Result := ( notifyType >= koCreateDocument ) And ( notifyType <= koBeginCloseAllDocument )
end;

function ApplicationEvent.CreateDocument( pDoc: Integer; docType: SYSINT ) : WORDBOOL;
begin
  AdviseDoc( pDoc );
  UpdateSlideBox1( pDoc );
  Result := true;
end;

  // koOpenDocumenBegin -   
function ApplicationEvent.BeginOpenDocument( fileName: PChar ) : WORDBOOL;
begin
  Result := true;
end;

  // koOpenDocumen -  
function ApplicationEvent.OpenDocument( newDoc : Reference; docType : integer ) : WORDBOOL;
begin
  AdviseDoc( newDoc );
  UpdateSlideBox1( newDoc );
  Result := true;
end;

// koActiveDocument -     
function ApplicationEvent.ChangeActiveDocument( newDoc : Reference; docType : integer ) : WORDBOOL;
begin
  UpdateSlideBox1( newDoc );
  Result := true;
end;

// koApplicatinDestroy -  
function ApplicationEvent.ApplicationDestroy() : WORDBOOL;
begin
  cBaseEvent.TerminateEvents;
  ClosePropertyManager( false );
  newKompasAPI       := nil;
  Result := true;
end;

// koBeginCreate -   
function ApplicationEvent.BeginCreate( docType: Integer ) : WORDBOOL;
begin
  Result := true;
end;

// koBeginOpenFile -   
function ApplicationEvent.BeginOpenFile() : WORDBOOL;
begin
  Result := true;
end;

// koBeginOpenFile -   
function ApplicationEvent.BeginCloseAllDocument() : WORDBOOL;
begin
  Result := true;
end;

// koKeyDown
function  ApplicationEvent.KeyDown(var key: Integer; flags: Integer; sysKey: WordBool): WordBool;
begin
  Result := TRUE;
end;

// koKeyUp
function ApplicationEvent.KeyUp(var key: Integer; flags: Integer; sysKey: WordBool): WordBool;
begin
  Result := TRUE;
end;

// koKeyPress
function ApplicationEvent.KeyPress(var key: Integer; sysKey: WordBool): WordBool;
begin
  Result := TRUE;
end;

// koBeginRequestFiles
function ApplicationEvent.BeginRequestFiles(requestID: Integer; var files: OleVariant): WordBool; 
begin
  Result := TRUE;
end;

destructor ApplicationEvent.Destroy();
begin
  inherited Destroy;
end;

constructor ApplicationEvent.Create();
begin
  inherited Create( ntKompasObjectNotify, 0, 0, 0 );
end;

function NewApplicationEvent() : ApplicationEvent;
begin
  Result := nil;
	if cBaseEvent.FindEvents( ntKompasObjectNotify, 0, 0, nil ) = nil then
  begin
	  Result := ApplicationEvent.Create();
		if not Result.Advise() then
    begin
      Result.Free;
			Result := nil;
		end
	end
end;

//------------------------------------------------------------------------------
// Advise
//---
function ApplicationEvent.Advise() : boolean;
begin
  if not m_advise and ( m_params.ifType <> 0 ) then
    m_advise := boolean( ksConnectionAdvise( @m_params, PIUnknown(IKompasObjectNotify(self)) ) );
	Result := m_advise;
end;

end.
